Skip to content

Commit

Permalink
Merge pull request #85 from zama-ai/feat/add-error-try-catch
Browse files Browse the repository at this point in the history
chore: fix bin
  • Loading branch information
immortal-tofu authored Jun 28, 2024
2 parents d3cd80e + e90d577 commit 25bbaa0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
29 changes: 19 additions & 10 deletions bin/fhevm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

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

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

const FHE_LIB_ADDRESS = '0x000000000000000000000000000000000000005d';

Expand All @@ -18,24 +18,33 @@ const getInstance = async (networkUrl) => {
_instance = await createInstance({ networkUrl });
} catch (e) {
return throwError(
"This network doesn't seem to use fhEVM or use an incompatible version.",
`This network (${networkUrl}) doesn't seem to use fhEVM or use an incompatible version.`,
);
}
return _instance;
};

program
.command('encrypt')
.argument('[bits]', 'numbers of bits for values eg: [1,64]')
.argument('[values]', 'integers to encrypt eg: [1,39320]')
.action(async (bitsArr, valuesArr, options) => {
.argument('<contractAddress>', 'address of the contract')
.argument('<userAddress>', 'address of the account')
.argument('<values:bits...>', 'values with number of bits eg: 1:1 3324242:64')
.action(async (contractAddress, userAddress, valuesArr, options) => {
const host = prependHttps(options.node);
const instance = await getInstance(host);
const encryptedInput = instance.createEncryptedInput();
bitsArr.forEach((bits, i) => {
const encryptedInput = instance.createEncryptedInput(
contractAddress,
userAddress,
);
valuesArr.forEach((str, i) => {
const [value, bits] = str.split(':');
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 suffix = +bits === 1 ? 'Bool' : bits === '160' ? 'Address' : bits;
try {
encryptedInput[`add${suffix}`](parseInt(value, 10));
} catch (e) {
return throwError(e.message);
}
});
const result = await encryptedInput.encrypt();

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

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-17",
"version": "0.5.0",
"description": "fhEVM SDK for blockchain using TFHE",
"main": "lib/node.js",
"types": "lib/node/node.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/ethCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export const fetchJSONRPC = async (url: string, options: RequestInit) => {
const decodedBytes = decodeAbiBytes(hexResult);
return `0x${toHexString(decodedBytes)}`;
} else {
console.error('No result from blockchain call');
throw new Error('No result from blockchain call');
}
} catch (error) {
console.error('Error performing eth_call:', error);
throw new Error('Error performing eth_call');
}
};

0 comments on commit 25bbaa0

Please sign in to comment.