Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix bin #85

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
};
Loading