From af9afcb5ed3b01c9197672e930db2d1a1eab0abc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Sun, 30 Jun 2024 02:02:08 +0200 Subject: [PATCH 1/4] feat: add support for eip1193 --- package-lock.json | 36 ++++++++++++++++++++++++++---------- package.json | 5 ++++- src/sdk/index.ts | 14 +++++++++----- src/sdk/network.ts | 40 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 77 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index f7d891c..66b0343 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,6 @@ "@types/keccak": "^3.0.4", "bigint-buffer": "^1.1.5", "commander": "^11.0.0", - "ethers": "^6.6.4", "node-fetch": "^2.7.0", "node-tfhe": "^0.6.3", "sha3": "^2.1.4", @@ -34,6 +33,7 @@ "@types/node-fetch": "^2.6.11", "buffer": "^6.0.3", "crypto-browserify": "^3.12.0", + "ethers": "^6.13.1", "fetch-mock-jest": "^1.5.1", "jest": "^29.5.0", "jest-raw-loader": "^1.0.1", @@ -57,7 +57,8 @@ "node_modules/@adraffy/ens-normalize": { "version": "1.10.1", "resolved": "https://registry.npmjs.org/@adraffy/ens-normalize/-/ens-normalize-1.10.1.tgz", - "integrity": "sha512-96Z2IP3mYmF1Xg2cDm8f1gWGf/HUVedQ3FMifV4kG/PQ4yEP51xDtRAEfhVNt5f/uzpNkZHwWQuUcu6D6K+Ekw==" + "integrity": "sha512-96Z2IP3mYmF1Xg2cDm8f1gWGf/HUVedQ3FMifV4kG/PQ4yEP51xDtRAEfhVNt5f/uzpNkZHwWQuUcu6D6K+Ekw==", + "dev": true }, "node_modules/@ampproject/remapping": { "version": "2.2.1", @@ -1159,6 +1160,7 @@ "version": "1.3.2", "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.2.tgz", "integrity": "sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==", + "dev": true, "engines": { "node": ">= 16" }, @@ -1933,7 +1935,8 @@ "node_modules/aes-js": { "version": "4.0.0-beta.5", "resolved": "https://registry.npmjs.org/aes-js/-/aes-js-4.0.0-beta.5.tgz", - "integrity": "sha512-G965FqalsNyrPqgEGON7nIx1e/OVENSgiEIzyC63haUMuvNnwIgIjMs52hlTCKhkBny7A2ORNlfY9Zu+jmGk1Q==" + "integrity": "sha512-G965FqalsNyrPqgEGON7nIx1e/OVENSgiEIzyC63haUMuvNnwIgIjMs52hlTCKhkBny7A2ORNlfY9Zu+jmGk1Q==", + "dev": true }, "node_modules/ajv": { "version": "6.12.6", @@ -3129,6 +3132,7 @@ "version": "6.13.1", "resolved": "https://registry.npmjs.org/ethers/-/ethers-6.13.1.tgz", "integrity": "sha512-hdJ2HOxg/xx97Lm9HdCWk949BfYqYWpyw4//78SiwOLgASyfrNszfMUNB2joKjvGUdwhHfaiMMFFwacVVoLR9A==", + "dev": true, "funding": [ { "type": "individual", @@ -3156,6 +3160,7 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.2.0.tgz", "integrity": "sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==", + "dev": true, "dependencies": { "@noble/hashes": "1.3.2" }, @@ -3166,12 +3171,14 @@ "node_modules/ethers/node_modules/@types/node": { "version": "18.15.13", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.13.tgz", - "integrity": "sha512-N+0kuo9KgrUQ1Sn/ifDXsvg0TTleP7rIy4zOBGECxAljqvqfqpTfzx0Q1NUedOixRMBfe2Whhb056a42cWs26Q==" + "integrity": "sha512-N+0kuo9KgrUQ1Sn/ifDXsvg0TTleP7rIy4zOBGECxAljqvqfqpTfzx0Q1NUedOixRMBfe2Whhb056a42cWs26Q==", + "dev": true }, "node_modules/ethers/node_modules/tslib": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz", - "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==" + "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==", + "dev": true }, "node_modules/events": { "version": "3.3.0", @@ -6828,6 +6835,7 @@ "version": "8.17.1", "resolved": "https://registry.npmjs.org/ws/-/ws-8.17.1.tgz", "integrity": "sha512-6XQFvXTkbfUOZOKKILFG1PDK2NDQs4azKQl26T0YS5CxqWLgXajbPZ+h4gZekJyRqFU8pvnbAbbs/3TgRPy+GQ==", + "dev": true, "engines": { "node": ">=10.0.0" }, @@ -6911,7 +6919,8 @@ "@adraffy/ens-normalize": { "version": "1.10.1", "resolved": "https://registry.npmjs.org/@adraffy/ens-normalize/-/ens-normalize-1.10.1.tgz", - "integrity": "sha512-96Z2IP3mYmF1Xg2cDm8f1gWGf/HUVedQ3FMifV4kG/PQ4yEP51xDtRAEfhVNt5f/uzpNkZHwWQuUcu6D6K+Ekw==" + "integrity": "sha512-96Z2IP3mYmF1Xg2cDm8f1gWGf/HUVedQ3FMifV4kG/PQ4yEP51xDtRAEfhVNt5f/uzpNkZHwWQuUcu6D6K+Ekw==", + "dev": true }, "@ampproject/remapping": { "version": "2.2.1", @@ -7767,7 +7776,8 @@ "@noble/hashes": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.2.tgz", - "integrity": "sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==" + "integrity": "sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==", + "dev": true }, "@nodelib/fs.scandir": { "version": "2.1.5", @@ -8376,7 +8386,8 @@ "aes-js": { "version": "4.0.0-beta.5", "resolved": "https://registry.npmjs.org/aes-js/-/aes-js-4.0.0-beta.5.tgz", - "integrity": "sha512-G965FqalsNyrPqgEGON7nIx1e/OVENSgiEIzyC63haUMuvNnwIgIjMs52hlTCKhkBny7A2ORNlfY9Zu+jmGk1Q==" + "integrity": "sha512-G965FqalsNyrPqgEGON7nIx1e/OVENSgiEIzyC63haUMuvNnwIgIjMs52hlTCKhkBny7A2ORNlfY9Zu+jmGk1Q==", + "dev": true }, "ajv": { "version": "6.12.6", @@ -9285,6 +9296,7 @@ "version": "6.13.1", "resolved": "https://registry.npmjs.org/ethers/-/ethers-6.13.1.tgz", "integrity": "sha512-hdJ2HOxg/xx97Lm9HdCWk949BfYqYWpyw4//78SiwOLgASyfrNszfMUNB2joKjvGUdwhHfaiMMFFwacVVoLR9A==", + "dev": true, "requires": { "@adraffy/ens-normalize": "1.10.1", "@noble/curves": "1.2.0", @@ -9299,6 +9311,7 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.2.0.tgz", "integrity": "sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==", + "dev": true, "requires": { "@noble/hashes": "1.3.2" } @@ -9306,12 +9319,14 @@ "@types/node": { "version": "18.15.13", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.13.tgz", - "integrity": "sha512-N+0kuo9KgrUQ1Sn/ifDXsvg0TTleP7rIy4zOBGECxAljqvqfqpTfzx0Q1NUedOixRMBfe2Whhb056a42cWs26Q==" + "integrity": "sha512-N+0kuo9KgrUQ1Sn/ifDXsvg0TTleP7rIy4zOBGECxAljqvqfqpTfzx0Q1NUedOixRMBfe2Whhb056a42cWs26Q==", + "dev": true }, "tslib": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz", - "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==" + "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==", + "dev": true } } }, @@ -11996,6 +12011,7 @@ "version": "8.17.1", "resolved": "https://registry.npmjs.org/ws/-/ws-8.17.1.tgz", "integrity": "sha512-6XQFvXTkbfUOZOKKILFG1PDK2NDQs4azKQl26T0YS5CxqWLgXajbPZ+h4gZekJyRqFU8pvnbAbbs/3TgRPy+GQ==", + "dev": true, "requires": {} }, "y18n": { diff --git a/package.json b/package.json index e5ff153..becaa0b 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,9 @@ "require": "./lib/node.cjs" } }, + "engines": { + "node": ">=20" + }, "scripts": { "lint": "eslint src/", "build": "npm run build:lib && npm run build:bundle", @@ -45,7 +48,6 @@ "@types/keccak": "^3.0.4", "bigint-buffer": "^1.1.5", "commander": "^11.0.0", - "ethers": "^6.6.4", "node-fetch": "^2.7.0", "node-tfhe": "^0.6.3", "sha3": "^2.1.4", @@ -64,6 +66,7 @@ "@types/node-fetch": "^2.6.11", "buffer": "^6.0.3", "crypto-browserify": "^3.12.0", + "ethers": "^6.13.1", "fetch-mock-jest": "^1.5.1", "jest": "^29.5.0", "jest-raw-loader": "^1.0.1", diff --git a/src/sdk/index.ts b/src/sdk/index.ts index 93776d2..a33b89a 100644 --- a/src/sdk/index.ts +++ b/src/sdk/index.ts @@ -1,4 +1,5 @@ import { TfheCompactPublicKey } from 'node-tfhe'; +import type { Eip1193Provider } from 'ethers'; import { URL } from 'url'; import { fromHexString } from '../utils'; import { ZKInput } from './encrypt'; @@ -6,6 +7,8 @@ import { getPublicKeyFromNetwork, getPublicKeyFromCoprocessor, getChainIdFromNetwork, + getChainIdFromEip1193, + getPublicKeyFromEip1193, } from './network'; import { createEncryptedInput } from './encrypt'; import { generateKeypair, createEIP712, EIP712 } from './keypair'; @@ -22,6 +25,7 @@ type FhevmInstanceConfig = { chainId?: number; publicKey?: string; gatewayUrl?: string; + network?: Eip1193Provider; networkUrl?: string; coprocessorUrl?: string; }; @@ -51,7 +55,7 @@ export type FhevmInstance = { export const createInstance = async ( config: FhevmInstanceConfig, ): Promise => { - let { publicKey, networkUrl, gatewayUrl, coprocessorUrl } = config; + let { publicKey, networkUrl, network, gatewayUrl, coprocessorUrl } = config; if (gatewayUrl) { gatewayUrl = new URL(gatewayUrl).href; @@ -72,6 +76,8 @@ export const createInstance = async ( throw new Error('chainId must be a number.'); } else if (networkUrl) { chainId = await getChainIdFromNetwork(networkUrl); + } else if (network) { + chainId = await getChainIdFromEip1193(network); } else { throw new Error( "You didn't provide the chainId nor the network url to get it.", @@ -83,10 +89,8 @@ export const createInstance = async ( publicKey = data.publicKey; } else if (networkUrl && !publicKey) { publicKey = await getPublicKeyFromNetwork(networkUrl); - } - - if (networkUrl && !chainId) { - chainId = await getChainIdFromNetwork(networkUrl); + } else if (network && !publicKey) { + publicKey = await getPublicKeyFromEip1193(network); } if (publicKey && typeof publicKey !== 'string') diff --git a/src/sdk/network.ts b/src/sdk/network.ts index ddf627f..d848ce9 100644 --- a/src/sdk/network.ts +++ b/src/sdk/network.ts @@ -1,10 +1,27 @@ -import { fetchJSONRPC } from '../ethCall'; +import type { Eip1193Provider } from 'ethers'; +import { decodeAbiBytes, fetchJSONRPC } from '../ethCall'; +import { toHexString } from 'src/utils'; export const getPublicKeyCallParams = () => ({ to: '0x000000000000000000000000000000000000005d', data: '0xd9d47bb001', }); +export const getChainIdFromEip1193 = async (ethereum: Eip1193Provider) => { + const payload = { + method: 'eth_chainId', + params: [], + }; + + let chainId; + try { + chainId = await ethereum.request(payload); + } catch (e) { + throw new Error('Impossible to fetch chain id (wrong network?)'); + } + return Number(chainId); +}; + export const getChainIdFromNetwork = async (url: string) => { const payload = { jsonrpc: '2.0', @@ -24,11 +41,30 @@ export const getChainIdFromNetwork = async (url: string) => { try { chainId = await fetchJSONRPC(url, options); } catch (e) { - throw new Error('Impossible to fetch chain id (wrong networkUrl?)'); + throw new Error('Impossible to fetch chain id (wrong url?)'); } return Number(chainId); }; +export const getPublicKeyFromEip1193 = async (ethereum: Eip1193Provider) => { + const payload = { + method: 'eth_call', + params: [getPublicKeyCallParams(), 'latest'], + }; + + let publicKey; + try { + const rawPubKey = await ethereum.request(payload); + const decodedBytes = decodeAbiBytes(rawPubKey); + publicKey = `0x${toHexString(decodedBytes)}`; + } catch (e) { + throw new Error( + 'Impossible to fetch public key from network (wrong network?)', + ); + } + return publicKey; +}; + // Define the function to perform the eth_call export const getPublicKeyFromNetwork = async (url: string) => { // Create the JSON-RPC request payload From c0bc49cca534ca62f23d09da6f5c4335b4178174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Sun, 30 Jun 2024 02:04:32 +0200 Subject: [PATCH 2/4] chore: 0.5.2 --- package-lock.json | 7 +++++-- package.json | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 66b0343..582184e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "fhevmjs", - "version": "0.5.1", + "version": "0.5.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "fhevmjs", - "version": "0.5.1", + "version": "0.5.2", "license": "BSD-3-Clause-Clear", "dependencies": { "@types/keccak": "^3.0.4", @@ -52,6 +52,9 @@ "webpack": "^5.82.1", "webpack-cli": "^5.1.1", "webpack-merge": "^5.9.0" + }, + "engines": { + "node": ">=20" } }, "node_modules/@adraffy/ens-normalize": { diff --git a/package.json b/package.json index becaa0b..c299912 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fhevmjs", - "version": "0.5.1", + "version": "0.5.2", "description": "fhEVM SDK for blockchain using TFHE", "main": "lib/node.js", "types": "lib/node/node.d.ts", From a94ac6f3f4b57e2270a8fe6ce579e6495f282b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Sun, 30 Jun 2024 02:08:19 +0200 Subject: [PATCH 3/4] chore: use node 20 --- .github/workflows/main.yml | 2 +- .github/workflows/publish.yml | 2 +- .github/workflows/publishprerelease.yml | 2 +- .github/workflows/test.yml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5367946..637f2b8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,7 +11,7 @@ jobs: strategy: matrix: - node-version: [18.x] + node-version: [20.x] steps: - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index d848f48..262e470 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -11,7 +11,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: - node-version: 18.x + node-version: 20.x - run: npm ci - run: npm test - run: npm run build diff --git a/.github/workflows/publishprerelease.yml b/.github/workflows/publishprerelease.yml index 183d8ae..bd1bb5f 100644 --- a/.github/workflows/publishprerelease.yml +++ b/.github/workflows/publishprerelease.yml @@ -11,7 +11,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: - node-version: 18.x + node-version: 20.x - run: npm ci - run: npm test - run: npm run build diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c8e4c2a..fb63c85 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ jobs: strategy: matrix: - node-version: [18.x] + node-version: [20.x] steps: - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} From 9f368dda07c277fcd61db708639827db4aff0e2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Sun, 30 Jun 2024 02:09:44 +0200 Subject: [PATCH 4/4] fix: fix aliased path --- src/sdk/network.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdk/network.ts b/src/sdk/network.ts index d848ce9..c8e007c 100644 --- a/src/sdk/network.ts +++ b/src/sdk/network.ts @@ -1,6 +1,6 @@ import type { Eip1193Provider } from 'ethers'; import { decodeAbiBytes, fetchJSONRPC } from '../ethCall'; -import { toHexString } from 'src/utils'; +import { toHexString } from '../utils'; export const getPublicKeyCallParams = () => ({ to: '0x000000000000000000000000000000000000005d',