diff --git a/bin/fhevm.js b/bin/fhevm.js index c203960..c6cf803 100755 --- a/bin/fhevm.js +++ b/bin/fhevm.js @@ -1,36 +1,21 @@ #!/usr/bin/env node 'use strict'; -import { JsonRpcProvider, AbiCoder } from 'ethers'; import { program } from 'commander'; import { toHexString, prependHttps, throwError } from './utils.js'; -import { createInstance } from '../lib/node.cjs'; +import { createInstance, getChainIdFromNetwork } from '../lib/node.cjs'; const allowedBits = [8, 16, 32, 64]; const FHE_LIB_ADDRESS = '0x000000000000000000000000000000000000005d'; let _instance; -const getInstance = async (provider) => { + +const getInstance = async (networkUrl) => { if (_instance) return _instance; - // 1. Get chain id - let network; - try { - network = await provider.getNetwork(); - } catch (e) { - throwError('Network is unreachable'); - } - const chainId = +network.chainId.toString(); try { - const ret = await provider.call({ - to: FHE_LIB_ADDRESS, - // first four bytes of keccak256('fhePubKey(bytes1)') + 1 byte for library - data: '0xd9d47bb001', - }); - const decoded = AbiCoder.defaultAbiCoder().decode(['bytes'], ret); - const publicKey = decoded[0]; - _instance = await createInstance({ chainId, publicKey }); + _instance = await createInstance({ networkUrl }); } catch (e) { return throwError( "This network doesn't seem to use fhEVM or use an incompatible version.", @@ -41,14 +26,18 @@ const getInstance = async (provider) => { program .command('encrypt') - .argument('', 'number of bits (4, 8, 16, 32, 64)') - .argument('', 'integer to encrypt') - .action(async (bits, value, options) => { - if (!allowedBits.includes(+bits)) throwError('Invalid number of bits'); + .argument('[bits]', 'numbers of bits for values eg: [1,64]') + .argument('[values]', 'integers to encrypt eg: [1,39320]') + .action(async (bitsArr, valuesArr, options) => { const host = prependHttps(options.node); - const provider = new JsonRpcProvider(host); - const instance = await getInstance(provider); - const result = instance[`encrypt${bits}`](parseInt(value, 10)); + const instance = await getInstance(host); + const encryptedInput = instance.createEncryptedInput(); + bitsArr.forEach((bits, i) => { + if (!allowedBits.includes(+bits)) throwError('Invalid number of bits'); + const suffix = bits === 1 ? 'Bool' : bits === '160' ? 'Address' : bits; + encryptedInput[`add${suffix}`](parseInt(values[i], 10)); + }); + const result = await encryptedInput.encrypt(); console.log(`0x${toHexString(result)}`); }) .requiredOption('-n, --node ', 'url of the blockchain'); diff --git a/package-lock.json b/package-lock.json index 63d2b16..e4a29a3 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", - "crypto-js": "^4.1.1", "ethers": "^6.6.4", "node-fetch": "^2.7.0", "node-tfhe": "^0.6.3", @@ -2788,11 +2787,6 @@ "node": "*" } }, - "node_modules/crypto-js": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.2.0.tgz", - "integrity": "sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q==" - }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -9028,11 +9022,6 @@ "randomfill": "^1.0.3" } }, - "crypto-js": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.2.0.tgz", - "integrity": "sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q==" - }, "debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", diff --git a/package.json b/package.json index d865a36..d867682 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,6 @@ "@types/keccak": "^3.0.4", "bigint-buffer": "^1.1.5", "commander": "^11.0.0", - "crypto-js": "^4.1.1", "ethers": "^6.6.4", "node-fetch": "^2.7.0", "node-tfhe": "^0.6.3", diff --git a/src/sdk/index.ts b/src/sdk/index.ts index 94dab2c..93776d2 100644 --- a/src/sdk/index.ts +++ b/src/sdk/index.ts @@ -15,10 +15,11 @@ export { getPublicKeyCallParams, getPublicKeyFromCoprocessor, getPublicKeyFromNetwork, + getChainIdFromNetwork, } from './network'; type FhevmInstanceConfig = { - chainId: number; + chainId?: number; publicKey?: string; gatewayUrl?: string; networkUrl?: string; @@ -50,7 +51,7 @@ export type FhevmInstance = { export const createInstance = async ( config: FhevmInstanceConfig, ): Promise => { - let { networkUrl, gatewayUrl, coprocessorUrl } = config; + let { publicKey, networkUrl, gatewayUrl, coprocessorUrl } = config; if (gatewayUrl) { gatewayUrl = new URL(gatewayUrl).href; @@ -64,11 +65,18 @@ export const createInstance = async ( coprocessorUrl = new URL(coprocessorUrl).href; } - let chainId: number | undefined = config.chainId; - let publicKey: string | undefined = config.publicKey; - let tfheCompactPublicKey: TfheCompactPublicKey | undefined; - - if (typeof chainId !== 'number') throw new Error('chainId must be a number.'); + let chainId: number; + if (config.chainId && typeof config.chainId === 'number') { + chainId = config.chainId; + } else if (config.chainId && typeof config.chainId !== 'number') { + throw new Error('chainId must be a number.'); + } else if (networkUrl) { + chainId = await getChainIdFromNetwork(networkUrl); + } else { + throw new Error( + "You didn't provide the chainId nor the network url to get it.", + ); + } if (coprocessorUrl && !publicKey) { const data = await getPublicKeyFromCoprocessor(coprocessorUrl); @@ -84,6 +92,8 @@ export const createInstance = async ( if (publicKey && typeof publicKey !== 'string') throw new Error('publicKey must be a string'); + let tfheCompactPublicKey: TfheCompactPublicKey | undefined; + if (publicKey) { const buff = fromHexString(publicKey); try { diff --git a/src/sdk/reencrypt.ts b/src/sdk/reencrypt.ts index 2bea32f..fdb6cee 100644 --- a/src/sdk/reencrypt.ts +++ b/src/sdk/reencrypt.ts @@ -1,6 +1,5 @@ import { bytesToBigInt, fromHexString } from '../utils'; import { - u8vec_to_public_sig_key, u8vec_to_cryptobox_pk, default_client_for_centralized_kms, process_reencryption_resp_from_json, @@ -35,7 +34,6 @@ export const reencryptRequest = }; const response = await fetch(`${gatewayUrl}/reencrypt`, options); const json = await response.json(); - const sigKey = u8vec_to_public_sig_key(fromHexString(userAddress)); const client = default_client_for_centralized_kms(); const pubKey = u8vec_to_cryptobox_pk(fromHexString(publicKey)); const privKey = u8vec_to_cryptobox_sk(fromHexString(privateKey));