Skip to content

Commit

Permalink
Merge pull request #51 from zama-ai/feature/allow-mocked-init
Browse files Browse the repository at this point in the history
Feature/allow mocked init
  • Loading branch information
immortal-tofu authored Feb 9, 2024
2 parents 1456a21 + 66de8e5 commit e6a8eb2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/sdk/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
27 changes: 23 additions & 4 deletions src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export type ExportedContractKeypairs = {

export type FhevmInstanceParams = {
chainId: number;
publicKey: string;
publicKey?: string;
keypairs?: ExportedContractKeypairs;
};

Expand All @@ -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 = {};

Expand Down Expand Up @@ -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);
},

Expand Down

0 comments on commit e6a8eb2

Please sign in to comment.