Skip to content

Commit

Permalink
Merge pull request #76 from zama-ai/kms/update
Browse files Browse the repository at this point in the history
chore: update kms wasm
  • Loading branch information
immortal-tofu authored Jun 26, 2024
2 parents f804765 + d4bad5b commit e16fc2d
Show file tree
Hide file tree
Showing 16 changed files with 3,218 additions and 1,842 deletions.
50 changes: 23 additions & 27 deletions bin/fhevm.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
#!/usr/bin/env node
'use strict';

import { JsonRpcProvider, AbiCoder } from 'ethers';
import { program } from 'commander';
import { toHexString, prependHttps, throwError } from './utils.js';
import { createInstance } from '../lib/node.cjs';
import { createInstance, getChainIdFromNetwork } from '../lib/node.cjs';

const allowedBits = [8, 16, 32, 64];

const FHE_LIB_ADDRESS = '0x000000000000000000000000000000000000005d';

let _instance;
const getInstance = async (provider) => {

const getInstance = async (networkUrl) => {
if (_instance) return _instance;

// 1. Get chain id
let network;
try {
network = await provider.getNetwork();
} catch (e) {
throwError('Network is unreachable');
}
const chainId = +network.chainId.toString();
try {
const ret = await provider.call({
to: FHE_LIB_ADDRESS,
// first four bytes of keccak256('fhePubKey(bytes1)') + 1 byte for library
data: '0xd9d47bb001',
});
const decoded = AbiCoder.defaultAbiCoder().decode(['bytes'], ret);
const publicKey = decoded[0];
_instance = await createInstance({ chainId, publicKey });
_instance = await createInstance({ networkUrl });
} catch (e) {
return throwError(
"This network doesn't seem to use fhEVM or use an incompatible version.",
Expand All @@ -41,15 +26,26 @@ const getInstance = async (provider) => {

program
.command('encrypt')
.argument('<bits>', 'number of bits (4, 8, 16, 32, 64)')
.argument('<value>', 'integer to encrypt')
.action(async (bits, value, options) => {
if (!allowedBits.includes(+bits)) throwError('Invalid number of bits');
.argument('[bits]', 'numbers of bits for values eg: [1,64]')
.argument('[values]', 'integers to encrypt eg: [1,39320]')
.action(async (bitsArr, valuesArr, options) => {
const host = prependHttps(options.node);
const provider = new JsonRpcProvider(host);
const instance = await getInstance(provider);
const result = instance[`encrypt${bits}`](parseInt(value, 10));
console.log(`0x${toHexString(result)}`);
const instance = await getInstance(host);
const encryptedInput = instance.createEncryptedInput();
bitsArr.forEach((bits, i) => {
if (!allowedBits.includes(+bits)) throwError('Invalid number of bits');
const suffix = bits === 1 ? 'Bool' : bits === '160' ? 'Address' : bits;
encryptedInput[`add${suffix}`](parseInt(values[i], 10));
});
const result = await encryptedInput.encrypt();

console.log('Input proof:');
console.log(`0x${toHexString(result.inputProof)}`);
console.log('Handles:');
result.handles.forEach((handle, i) => {
console.log(`Handle ${i}`);
console.log(`0x${toHexString(handle)}`);
});
})
.requiredOption('-n, --node <url>', 'url of the blockchain');

Expand Down
6 changes: 3 additions & 3 deletions jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ module.exports = {
transformIgnorePatterns: ['/node_modules/'],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
branches: 70,
functions: 70,
lines: 70,
},
},
};
112 changes: 99 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fhevmjs",
"version": "0.5.0-12",
"version": "0.5.0-13",
"description": "fhEVM SDK for blockchain using TFHE",
"main": "lib/node.js",
"types": "lib/node/node.d.ts",
Expand Down Expand Up @@ -45,12 +45,12 @@
"@types/keccak": "^3.0.4",
"bigint-buffer": "^1.1.5",
"commander": "^11.0.0",
"crypto-js": "^4.1.1",
"ethers": "^6.6.4",
"node-fetch": "^2.7.0",
"node-tfhe": "^0.6.3",
"sha3": "^2.1.4",
"tfhe": "^0.6.3",
"url": "^0.11.3",
"web3-validator": "^2.0.6"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit e16fc2d

Please sign in to comment.