Skip to content

Commit

Permalink
Merge pull request #71 from zama-ai/fix-cp-pk
Browse files Browse the repository at this point in the history
fix: fix the way we get pk from cp
  • Loading branch information
immortal-tofu authored Jun 17, 2024
2 parents f3af363 + c90f3c0 commit d8115b2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ if (!global.fetch) {

export * from './sdk';
export * from './tfhe';
export { clientKeyDecryptor } from './utils';
export { clientKeyDecryptor, getCiphertextCallParams } from './utils';
2 changes: 1 addition & 1 deletion src/sdk/index.test.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 () => {
Expand Down
11 changes: 9 additions & 2 deletions src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +45,7 @@ export type FhevmInstance = {
contractAddress: string,
userAddress: string,
) => Promise<bigint>;
getPublicKey: () => string | null;
};

export const createInstance = async (
Expand All @@ -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);
}
Expand Down Expand Up @@ -86,5 +92,6 @@ export const createInstance = async (
generateKeypair,
createEIP712: createEIP712(chainId),
reencrypt: reencryptRequest(reencryptionUrl),
getPublicKey: () => publicKey || null,
};
};
8 changes: 6 additions & 2 deletions src/sdk/network.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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');
});
Expand Down
9 changes: 9 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
bytesToHex,
clientKeyDecryptor,
fromHexString,
getCiphertextCallParams,
toHexString,
} from './utils';
import { createTfheKeypair } from './tfhe';
Expand Down Expand Up @@ -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');
});
});
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
};

0 comments on commit d8115b2

Please sign in to comment.