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

Feat/add support eip #87

Merged
merged 4 commits into from
Jun 30, 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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

strategy:
matrix:
node-version: [18.x]
node-version: [20.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
- run: npm ci
- run: npm test
- run: npm run build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publishprerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
- run: npm ci
- run: npm test
- run: npm run build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

strategy:
matrix:
node-version: [18.x]
node-version: [20.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
43 changes: 31 additions & 12 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fhevmjs",
"version": "0.5.1",
"version": "0.5.2",
"description": "fhEVM SDK for blockchain using TFHE",
"main": "lib/node.js",
"types": "lib/node/node.d.ts",
Expand All @@ -22,6 +22,9 @@
"require": "./lib/node.cjs"
}
},
"engines": {
"node": ">=20"
},
"scripts": {
"lint": "eslint src/",
"build": "npm run build:lib && npm run build:bundle",
Expand All @@ -45,7 +48,6 @@
"@types/keccak": "^3.0.4",
"bigint-buffer": "^1.1.5",
"commander": "^11.0.0",
"ethers": "^6.6.4",
"node-fetch": "^2.7.0",
"node-tfhe": "^0.6.3",
"sha3": "^2.1.4",
Expand All @@ -64,6 +66,7 @@
"@types/node-fetch": "^2.6.11",
"buffer": "^6.0.3",
"crypto-browserify": "^3.12.0",
"ethers": "^6.13.1",
"fetch-mock-jest": "^1.5.1",
"jest": "^29.5.0",
"jest-raw-loader": "^1.0.1",
Expand Down
14 changes: 9 additions & 5 deletions src/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { TfheCompactPublicKey } from 'node-tfhe';
import type { Eip1193Provider } from 'ethers';
import { URL } from 'url';
import { fromHexString } from '../utils';
import { ZKInput } from './encrypt';
import {
getPublicKeyFromNetwork,
getPublicKeyFromCoprocessor,
getChainIdFromNetwork,
getChainIdFromEip1193,
getPublicKeyFromEip1193,
} from './network';
import { createEncryptedInput } from './encrypt';
import { generateKeypair, createEIP712, EIP712 } from './keypair';
Expand All @@ -22,6 +25,7 @@
chainId?: number;
publicKey?: string;
gatewayUrl?: string;
network?: Eip1193Provider;
networkUrl?: string;
coprocessorUrl?: string;
};
Expand Down Expand Up @@ -51,7 +55,7 @@
export const createInstance = async (
config: FhevmInstanceConfig,
): Promise<FhevmInstance> => {
let { publicKey, networkUrl, gatewayUrl, coprocessorUrl } = config;
let { publicKey, networkUrl, network, gatewayUrl, coprocessorUrl } = config;

if (gatewayUrl) {
gatewayUrl = new URL(gatewayUrl).href;
Expand All @@ -72,6 +76,8 @@
throw new Error('chainId must be a number.');
} else if (networkUrl) {
chainId = await getChainIdFromNetwork(networkUrl);
} else if (network) {

Check warning on line 79 in src/sdk/index.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 79 in src/sdk/index.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
chainId = await getChainIdFromEip1193(network);

Check warning on line 80 in src/sdk/index.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 80 in src/sdk/index.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
} else {
throw new Error(
"You didn't provide the chainId nor the network url to get it.",
Expand All @@ -83,10 +89,8 @@
publicKey = data.publicKey;
} else if (networkUrl && !publicKey) {
publicKey = await getPublicKeyFromNetwork(networkUrl);
}

if (networkUrl && !chainId) {
chainId = await getChainIdFromNetwork(networkUrl);
} else if (network && !publicKey) {

Check warning on line 92 in src/sdk/index.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
publicKey = await getPublicKeyFromEip1193(network);

Check warning on line 93 in src/sdk/index.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 93 in src/sdk/index.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

if (publicKey && typeof publicKey !== 'string')
Expand Down
40 changes: 38 additions & 2 deletions src/sdk/network.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import { fetchJSONRPC } from '../ethCall';
import type { Eip1193Provider } from 'ethers';
import { decodeAbiBytes, fetchJSONRPC } from '../ethCall';
import { toHexString } from '../utils';

export const getPublicKeyCallParams = () => ({
to: '0x000000000000000000000000000000000000005d',
data: '0xd9d47bb001',
});

export const getChainIdFromEip1193 = async (ethereum: Eip1193Provider) => {
const payload = {
method: 'eth_chainId',
params: [],
};

let chainId;
try {
chainId = await ethereum.request(payload);
} catch (e) {
throw new Error('Impossible to fetch chain id (wrong network?)');
}
return Number(chainId);
};

export const getChainIdFromNetwork = async (url: string) => {
const payload = {
jsonrpc: '2.0',
Expand All @@ -24,11 +41,30 @@ export const getChainIdFromNetwork = async (url: string) => {
try {
chainId = await fetchJSONRPC(url, options);
} catch (e) {
throw new Error('Impossible to fetch chain id (wrong networkUrl?)');
throw new Error('Impossible to fetch chain id (wrong url?)');
}
return Number(chainId);
};

export const getPublicKeyFromEip1193 = async (ethereum: Eip1193Provider) => {
const payload = {
method: 'eth_call',
params: [getPublicKeyCallParams(), 'latest'],
};

let publicKey;
try {
const rawPubKey = await ethereum.request(payload);
const decodedBytes = decodeAbiBytes(rawPubKey);
publicKey = `0x${toHexString(decodedBytes)}`;
} catch (e) {
throw new Error(
'Impossible to fetch public key from network (wrong network?)',
);
}
return publicKey;
};

// Define the function to perform the eth_call
export const getPublicKeyFromNetwork = async (url: string) => {
// Create the JSON-RPC request payload
Expand Down
Loading