From 39bb2a469e428f9d57925e53969e5ff7b1aa079c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Fri, 9 Feb 2024 14:56:37 +0100 Subject: [PATCH 1/3] feat: allow init without public key --- src/sdk/index.ts | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/sdk/index.ts b/src/sdk/index.ts index 91d6dd4..0d3ba7d 100644 --- a/src/sdk/index.ts +++ b/src/sdk/index.ts @@ -69,10 +69,13 @@ export const createInstance = async ( await sodium.ready; const { chainId, publicKey, keypairs } = params; if (typeof chainId !== 'number') throw new Error('chainId must be a number'); - if (typeof publicKey !== 'string') + if (publicKey && typeof publicKey !== 'string') throw new Error('publicKey must be a string'); - const buff = fromHexString(publicKey); - const tfheCompactPublicKey = TfheCompactPublicKey.deserialize(buff); + let tfheCompactPublicKey: TfheCompactPublicKey; + if (publicKey) { + const buff = fromHexString(publicKey); + tfheCompactPublicKey = TfheCompactPublicKey.deserialize(buff); + } let contractKeypairs: ContractKeypairs = {}; @@ -107,23 +110,39 @@ export const createInstance = async ( encrypt8(value) { if (value == null) throw new Error('Missing value'); if (typeof value !== 'number') throw new Error('Value must be a number'); + if (!tfheCompactPublicKey) + throw new Error( + 'Your instance has been created without the public blockchain key', + ); return encrypt8(value, tfheCompactPublicKey); }, encrypt16(value) { if (value == null) throw new Error('Missing value'); if (typeof value !== 'number') throw new Error('Value must be a number'); + if (!tfheCompactPublicKey) + throw new Error( + 'Your instance has been created without the public blockchain key', + ); return encrypt16(value, tfheCompactPublicKey); }, encrypt32(value) { if (value == null) throw new Error('Missing value'); if (typeof value !== 'number') throw new Error('Value must be a number'); + if (!tfheCompactPublicKey) + throw new Error( + 'Your instance has been created without the public blockchain key', + ); return encrypt32(value, tfheCompactPublicKey); }, encrypt64(value) { if (value == null) throw new Error('Missing value'); if (typeof value !== 'number') throw new Error('Value must be a number'); + if (!tfheCompactPublicKey) + throw new Error( + 'Your instance has been created without the public blockchain key', + ); return encrypt64(value, tfheCompactPublicKey); }, From 8cd97d9f9cfe8a3c07eb69f73e29342003b2f339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Fri, 9 Feb 2024 15:58:26 +0100 Subject: [PATCH 2/3] fix: make public key optionnal --- src/sdk/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdk/index.ts b/src/sdk/index.ts index 0d3ba7d..1d165ac 100644 --- a/src/sdk/index.ts +++ b/src/sdk/index.ts @@ -59,7 +59,7 @@ export type ExportedContractKeypairs = { export type FhevmInstanceParams = { chainId: number; - publicKey: string; + publicKey?: string; keypairs?: ExportedContractKeypairs; }; From 66de8e5a32b10b80103688c8205724442659e616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Fri, 9 Feb 2024 16:05:42 +0100 Subject: [PATCH 3/3] fix: add more test regarding mocks --- src/sdk/index.test.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/sdk/index.test.ts b/src/sdk/index.test.ts index 5e590d9..17d57bc 100644 --- a/src/sdk/index.test.ts +++ b/src/sdk/index.test.ts @@ -26,6 +26,34 @@ describe('token', () => { expect(instance.hasKeypair).toBeDefined(); }); + it('creates an instance for mock', async () => { + const instance = await createInstance({ + chainId: 1234, + }); + expect(instance.encrypt8).toBeDefined(); + expect(instance.encrypt16).toBeDefined(); + expect(instance.encrypt32).toBeDefined(); + expect(instance.encrypt64).toBeDefined(); + expect(instance.generatePublicKey).toBeDefined(); + expect(instance.decrypt).toBeDefined(); + expect(instance.serializeKeypairs).toBeDefined(); + expect(instance.getPublicKey).toBeDefined(); + expect(instance.hasKeypair).toBeDefined(); + + expect(() => instance.encrypt8(2)).toThrow( + 'Your instance has been created without the public blockchain key', + ); + expect(() => instance.encrypt16(2)).toThrow( + 'Your instance has been created without the public blockchain key', + ); + expect(() => instance.encrypt32(2)).toThrow( + 'Your instance has been created without the public blockchain key', + ); + expect(() => instance.encrypt64(2)).toThrow( + 'Your instance has been created without the public blockchain key', + ); + }); + it('fails to create an instance', async () => { await expect( createInstance({