diff --git a/package.json b/package.json index 9a6a62b..c149c07 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fhevmjs", - "version": "0.5.0-6", + "version": "0.5.0-7", "description": "fhEVM SDK for blockchain using TFHE", "main": "lib/node/index.js", "types": "lib/node/node.d.ts", diff --git a/src/node.ts b/src/node.ts index ceeed48..efa8079 100644 --- a/src/node.ts +++ b/src/node.ts @@ -10,4 +10,4 @@ if (!global.fetch) { export * from './sdk'; export * from './tfhe'; -export { clientKeyDecryptor } from './utils'; +export { clientKeyDecryptor, getCiphertextCallParams } from './utils'; diff --git a/src/sdk/index.test.ts b/src/sdk/index.test.ts index ca811c8..842dae4 100644 --- a/src/sdk/index.test.ts +++ b/src/sdk/index.test.ts @@ -1,7 +1,6 @@ import sodium from 'libsodium-wrappers'; import { createInstance } from './index'; import { createTfhePublicKey } from '../tfhe'; -import { fromHexString, bigIntToBytes } from '../utils'; describe('index', () => { let tfhePublicKey: string; @@ -20,6 +19,7 @@ describe('index', () => { expect(instance.createEIP712).toBeDefined(); expect(instance.generateKeypair).toBeDefined(); expect(instance.createEncryptedInput).toBeDefined(); + expect(instance.getPublicKey()).toBe(tfhePublicKey); }); it('creates an instance for mock', async () => { diff --git a/src/sdk/index.ts b/src/sdk/index.ts index b97bcfd..633eb4c 100644 --- a/src/sdk/index.ts +++ b/src/sdk/index.ts @@ -12,7 +12,11 @@ import { createEncryptedInput } from './encrypt'; import { generateKeypair, createEIP712, EIP712 } from './keypair'; import { reencryptRequest } from './reencrypt'; -export { getPublicKeyCallParams } from './network'; +export { + getPublicKeyCallParams, + getPublicKeyFromCoprocessor, + getPublicKeyFromNetwork, +} from './network'; type FhevmInstanceConfig = { chainId: number; @@ -41,6 +45,7 @@ export type FhevmInstance = { contractAddress: string, userAddress: string, ) => Promise; + getPublicKey: () => string | null; }; export const createInstance = async ( @@ -57,7 +62,8 @@ export const createInstance = async ( if (typeof chainId !== 'number') throw new Error('chainId must be a number.'); if (coprocessorUrl && !publicKey) { - publicKey = await getPublicKeyFromCoprocessor(coprocessorUrl); + const data = await getPublicKeyFromCoprocessor(coprocessorUrl); + publicKey = data.publicKey; } else if (networkUrl && !publicKey) { publicKey = await getPublicKeyFromNetwork(networkUrl); } @@ -86,5 +92,6 @@ export const createInstance = async ( generateKeypair, createEIP712: createEIP712(chainId), reencrypt: reencryptRequest(reencryptionUrl), + getPublicKey: () => publicKey || null, }; }; diff --git a/src/sdk/network.test.ts b/src/sdk/network.test.ts index fc1c78c..de77b7a 100644 --- a/src/sdk/network.test.ts +++ b/src/sdk/network.test.ts @@ -1,7 +1,11 @@ import { TfheCompactPublicKey, TfheClientKey } from 'node-tfhe'; import { createTfheKeypair } from '../tfhe'; import { createEncryptedInput } from './encrypt'; -import { getPublicKeyCallParams, getPublicKeyFromNetwork } from './network'; +import { + getPublicKeyCallParams, + getPublicKeyFromCoprocessor, + getPublicKeyFromNetwork, +} from './network'; import { fromHexString } from '../utils'; describe('network', () => { @@ -18,7 +22,7 @@ describe('network', () => { }); it('get public key params', async () => { - const params = await getPublicKeyCallParams(); + const params = getPublicKeyCallParams(); expect(params.to).toBe('0x000000000000000000000000000000000000005d'); expect(params.data).toBe('0xd9d47bb001'); }); diff --git a/src/utils.test.ts b/src/utils.test.ts index 79e2a1b..ca8d2eb 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -4,6 +4,7 @@ import { bytesToHex, clientKeyDecryptor, fromHexString, + getCiphertextCallParams, toHexString, } from './utils'; import { createTfheKeypair } from './tfhe'; @@ -138,4 +139,12 @@ describe('decrypt', () => { const v = await d.decryptAddress(toHexString(c)); expect(v).toBe('0x8ba1f109551bd432803012645ac136ddd64dba72'); }); + + it('returns ciphertext call params', async () => { + const params = getCiphertextCallParams(BigInt(23)); + expect(params.data).toBe( + '0xff627e770000000000000000000000000000000000000000000000000000000000000017', + ); + expect(params.to).toBe('0x000000000000000000000000000000000000005d'); + }); }); diff --git a/src/utils.ts b/src/utils.ts index 304d249..229b027 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -68,3 +68,12 @@ export const clientKeyDecryptor = (clientKeySer: Uint8Array) => { }, }; }; + +export const getCiphertextCallParams = (handle: bigint) => { + let hex = handle.toString(16); + hex = hex.padStart(64, '0'); + return { + to: '0x000000000000000000000000000000000000005d', + data: '0xff627e77' + hex, + }; +};