From e5625a72a01be7b20b55dede1e8fa77e9166affe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Tue, 20 Feb 2024 00:59:50 +0100 Subject: [PATCH] fix: enhance tests --- src/sdk/encrypt.test.ts | 22 ++++++++++++- src/sdk/index.test.ts | 55 +++++++++++-------------------- src/sdk/index.ts | 73 ----------------------------------------- 3 files changed, 40 insertions(+), 110 deletions(-) diff --git a/src/sdk/encrypt.test.ts b/src/sdk/encrypt.test.ts index b1e0feb..fb5aa22 100644 --- a/src/sdk/encrypt.test.ts +++ b/src/sdk/encrypt.test.ts @@ -105,7 +105,17 @@ describe('encrypt8', () => { }); }); - it('encrypt/decrypt 32bits', async () => { + it('encrypt/decrypt 0 64bits', async () => { + const buffer = encrypt64(0, publicKey); + const compactList = CompactFheUint64List.deserialize(buffer); + let encryptedList = compactList.expand(); + encryptedList.forEach((v: FheUint64) => { + const decrypted = v.decrypt(clientKey); + expect(decrypted.toString()).toBe('0'); + }); + }); + + it('encrypt/decrypt 64bits', async () => { const buffer = encrypt64(3021094839202949, publicKey); const compactList = CompactFheUint64List.deserialize(buffer); let encryptedList = compactList.expand(); @@ -114,4 +124,14 @@ describe('encrypt8', () => { expect(decrypted.toString()).toBe('3021094839202949'); }); }); + + it('encrypt/decrypt 64bits', async () => { + const buffer = encrypt64(BigInt('18446744073709551615'), publicKey); + const compactList = CompactFheUint64List.deserialize(buffer); + let encryptedList = compactList.expand(); + encryptedList.forEach((v: FheUint64) => { + const decrypted = v.decrypt(clientKey); + expect(decrypted.toString()).toBe('18446744073709551615'); + }); + }); }); diff --git a/src/sdk/index.test.ts b/src/sdk/index.test.ts index 17d57bc..30a2526 100644 --- a/src/sdk/index.test.ts +++ b/src/sdk/index.test.ts @@ -3,7 +3,7 @@ import { createInstance } from './index'; import { createTfhePublicKey } from '../tfhe'; import { fromHexString, toHexString, numberToBytes } from '../utils'; -describe('token', () => { +describe('index', () => { let tfhePublicKey: string; beforeAll(async () => { @@ -100,10 +100,13 @@ describe('token', () => { chainId: 1234, publicKey: tfhePublicKey, }); + expect(instance.encrypt64(BigInt(34))).toBeTruthy(); + expect(() => instance.encrypt4(undefined as any)).toThrow('Missing value'); expect(() => instance.encrypt8(undefined as any)).toThrow('Missing value'); expect(() => instance.encrypt16(undefined as any)).toThrow('Missing value'); expect(() => instance.encrypt32(undefined as any)).toThrow('Missing value'); + expect(() => instance.encrypt64(undefined as any)).toThrow('Missing value'); expect(() => instance.encrypt8('wrong value' as any)).toThrow( 'Value must be a number', @@ -114,22 +117,22 @@ describe('token', () => { expect(() => instance.encrypt32('wrong value' as any)).toThrow( 'Value must be a number', ); - }); + expect(() => instance.encrypt64('wrong value' as any)).toThrow( + 'Value must be a number', + ); - it('controls generateToken', async () => { - const instance = await createInstance({ - chainId: 1234, - publicKey: tfhePublicKey, - }); - expect(() => instance.generateToken(undefined as any)).toThrow( - 'Missing contract address', + expect(() => instance.encrypt4(BigInt(34) as any)).toThrow( + 'Value must be a number', ); - expect(() => instance.generateToken({ verifyingContract: '' })).toThrow( - 'Missing contract address', + expect(() => instance.encrypt8(BigInt(34) as any)).toThrow( + 'Value must be a number', + ); + expect(() => instance.encrypt16(BigInt(34) as any)).toThrow( + 'Value must be a number', + ); + expect(() => instance.encrypt32(BigInt(34) as any)).toThrow( + 'Value must be a number', ); - expect(() => - instance.generateToken({ verifyingContract: '0x847473829d' }), - ).toThrow('Invalid contract address'); }); it('controls generatePublicKey', async () => { @@ -148,7 +151,7 @@ describe('token', () => { ).toThrow('Invalid contract address'); }); - it('save generated token', async () => { + it('save generated public key', async () => { const instance = await createInstance({ chainId: 1234, publicKey: tfhePublicKey, @@ -168,26 +171,6 @@ describe('token', () => { expect(kp!.publicKey).toBe(publicKey); }); - it('save generated token (deprecated)', async () => { - const instance = await createInstance({ - chainId: 1234, - publicKey: tfhePublicKey, - }); - - const contractAddress = '0x1c786b8ca49D932AFaDCEc00827352B503edf16c'; - - const { token, publicKey } = instance.generateToken({ - verifyingContract: contractAddress, - }); - - instance.setTokenSignature(contractAddress, 'signnnn'); - - expect(instance.hasKeypair(contractAddress)).toBeTruthy(); - - const kp = instance.getTokenSignature(contractAddress); - expect(kp!.publicKey).toBe(publicKey); - }); - it("don't export keys without signature", async () => { const instance = await createInstance({ chainId: 1234, @@ -218,7 +201,7 @@ describe('token', () => { verifyingContract: contractAddress, }); - instance.setTokenSignature(contractAddress, 'signnnn'); + instance.setSignature(contractAddress, 'signnnn'); const kp = instance.getPublicKey(contractAddress); expect(kp!.publicKey).toBe(publicKey); diff --git a/src/sdk/index.ts b/src/sdk/index.ts index 5059b49..7afd1fc 100644 --- a/src/sdk/index.ts +++ b/src/sdk/index.ts @@ -16,14 +16,6 @@ export type FhevmInstance = { encrypt16: (value: number) => Uint8Array; encrypt32: (value: number) => Uint8Array; encrypt64: (value: number | bigint) => Uint8Array; - generateToken: ( - options: GeneratePublicKeyParams & { - force?: boolean; - }, - ) => { - publicKey: Uint8Array; - token: EIP712; - }; generatePublicKey: ( options: GeneratePublicKeyParams & { force?: boolean; @@ -32,10 +24,6 @@ export type FhevmInstance = { publicKey: Uint8Array; eip712: EIP712; }; - setTokenSignature: (contractAddress: string, signature: string) => void; - getTokenSignature: ( - contractAddress: string, - ) => { publicKey: Uint8Array; signature: string } | null; setSignature: (contractAddress: string, signature: string) => void; getPublicKey: ( contractAddress: string, @@ -157,36 +145,6 @@ export const createInstance = async ( return encrypt64(value, tfheCompactPublicKey); }, - /** - * @deprecated Since version 0.3.0. Will be deleted in version 0.4.0. Use generatePublicKey instead. - */ - generateToken(options) { - console.warn( - 'generateToken is deprecated. Use generatePublicKey instead', - ); - if (!options || !options.verifyingContract) - throw new Error('Missing contract address'); - if (!isAddress(options.verifyingContract)) - throw new Error('Invalid contract address'); - let kp; - if (!options.force && contractKeypairs[options.verifyingContract]) { - kp = contractKeypairs[options.verifyingContract]; - } - const { eip712, keypair } = generatePublicKey({ - verifyingContract: options.verifyingContract, - name: options.name, - version: options.version, - chainId, - keypair: kp, - }); - contractKeypairs[options.verifyingContract] = { - privateKey: keypair.privateKey, - publicKey: keypair.publicKey, - signature: null, - }; - return { token: eip712, publicKey: keypair.publicKey }; - }, - // Reencryption generatePublicKey(options) { if (!options || !options.verifyingContract) @@ -212,21 +170,6 @@ export const createInstance = async ( return { eip712, publicKey: keypair.publicKey }; }, - /** - * @deprecated Since version 0.3.0. Will be deleted in version 0.4.0. Use generatePublicKey instead. - */ - setTokenSignature(contractAddress: string, signature: string) { - console.warn( - 'setTokenSignature is deprecated. Use generatePublicKey instead', - ); - if ( - contractKeypairs[contractAddress] && - contractKeypairs[contractAddress].privateKey - ) { - contractKeypairs[contractAddress].signature = signature; - } - }, - setSignature(contractAddress: string, signature: string) { if ( contractKeypairs[contractAddress] && @@ -236,22 +179,6 @@ export const createInstance = async ( } }, - /** - * @deprecated Since version 0.3.0. Will be deleted in version 0.4.0. Use generatePublicKey instead. - */ - getTokenSignature(contractAddress: string): TokenSignature | null { - console.warn( - 'setTokenSignature is deprecated. Use generatePublicKey instead', - ); - if (hasKeypair(contractAddress)) { - return { - publicKey: contractKeypairs[contractAddress].publicKey, - signature: contractKeypairs[contractAddress].signature!, - }; - } - return null; - }, - getPublicKey(contractAddress: string): TokenSignature | null { if (hasKeypair(contractAddress)) { return {