Skip to content

Commit

Permalink
Merge pull request #70 from zama-ai/fhevmjs-coproc-fixes
Browse files Browse the repository at this point in the history
Fhevmjs fixes to work with local node
  • Loading branch information
immortal-tofu authored Jun 17, 2024
2 parents 40cd964 + 0213255 commit fc7c6e1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/ethCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export const fetchJSONRPC = async (url: string, options: RequestInit) => {
if (data && data.result) {
// The result is usually prefixed with '0x' and is in hex format
const hexResult = data.result;
if (typeof hexResult == 'object') {
return hexResult;
}
const decodedBytes = decodeAbiBytes(hexResult);
return `0x${toHexString(decodedBytes)}`;
} else {
Expand Down
54 changes: 54 additions & 0 deletions src/sdk/encrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
} from 'node-tfhe';
import { createTfheKeypair } from '../tfhe';
import { createEncryptedInput } from './encrypt';
import { fetchJSONRPC } from '../ethCall';
import { fromHexString } from '../utils';

describe('encrypt', () => {
let clientKey: TfheClientKey;
Expand Down Expand Up @@ -187,3 +189,55 @@ describe('encrypt', () => {
);
});
});

// describe('encryptWithCoprocessor', () => {
// let publicKey: TfheCompactPublicKey;
// const coprocessorNode = 'http://127.0.0.1:8745';

// beforeAll(async () => {
// const pkeyOptions = {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify({
// method: "eth_getPublicFhevmKey",
// params: [],
// id: 1,
// jsonrpc: "2.0",
// }),
// };
// const pkeyRes = await fetchJSONRPC(
// coprocessorNode,
// pkeyOptions,
// );

// publicKey = TfheCompactPublicKey.deserialize(fromHexString(pkeyRes.publicKey));
// });

// it('encrypt with coprocessor', async () => {
// // encrypted inputs, we pass coprocessor node url
// const input = createEncryptedInput(publicKey, coprocessorNode)(
// '0x8ba1f109551bd432803012645ac136ddd64dba72',
// '0xa5e1defb98EFe38EBb2D958CEe052410247F4c80',
// );
// // add inputs
// input.addBool(BigInt(0));
// input.add4(2);
// input.add8(BigInt(43));
// input.add16(BigInt(87));
// input.add32(BigInt(2339389323));
// input.add64(BigInt(23393893233));
// //input.add128(BigInt(233938932390)); // 128 bits not supported yet in coprocessor
// input.addAddress('0xa5e1defb98EFe38EBb2D958CEe052410247F4c80');

// // send to the coprocessor
// const res = await input.send();
// // receive handlesList, callerAddress, contractAddress and EIP712 signature
// expect(res.handlesList).toBeDefined();
// expect(res.handlesList.length).toBe(7);
// expect(res.callerAddress).toBe('0xa5e1defb98EFe38EBb2D958CEe052410247F4c80');
// expect(res.contractAddress).toBe('0x8ba1f109551bD432803012645Ac136ddd64DBA72');
// expect(res.signature).toBeDefined();
// });
// });
14 changes: 7 additions & 7 deletions src/sdk/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,16 @@ export const createEncryptedInput =
const ciphertext = encrypted.serialize();

const data = new Uint8Array(1 + bits.length + ciphertext.length);
data.set([data.length], 0);
data.set(bits, 1);
data.set(ciphertext, data.length + 1);
data.set([bits.length], 0);
bits.forEach((value, index) => {
data.set([ENCRYPTION_TYPES[value] & 0xff], 1 + index);
});
data.set(ciphertext, bits.length + 1);

const payload = {
jsonrpc: '2.0',
method: 'eth_addUserCiphertext',
params: [toHexString(data), contractAddress, callerAddress],
params: ['0x' + toHexString(data), contractAddress, callerAddress],
id: 1,
};

Expand All @@ -221,9 +223,7 @@ export const createEncryptedInput =
},
body: JSON.stringify(payload),
};
const response = await fetchJSONRPC(coprocessorUrl, options);
if (!response) throw new Error('Invalid input');
return JSON.parse(response);
return await fetchJSONRPC(coprocessorUrl, options);
},
};
};

0 comments on commit fc7c6e1

Please sign in to comment.