Skip to content

Commit

Permalink
fix: enhance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
immortal-tofu committed Feb 19, 2024
1 parent 56b9ec3 commit e5625a7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 110 deletions.
22 changes: 21 additions & 1 deletion src/sdk/encrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
});
});
});
55 changes: 19 additions & 36 deletions src/sdk/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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',
Expand All @@ -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 () => {
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
73 changes: 0 additions & 73 deletions src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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] &&
Expand All @@ -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 {
Expand Down

0 comments on commit e5625a7

Please sign in to comment.