Skip to content

Commit

Permalink
Merge pull request #87 from zama-ai/feat/add-support-eip
Browse files Browse the repository at this point in the history
Feat/add support eip
  • Loading branch information
immortal-tofu authored Jun 30, 2024
2 parents 6f54534 + 9f368dd commit 19fd3b8
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 25 deletions.
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 @@ type FhevmInstanceConfig = {
chainId?: number;
publicKey?: string;
gatewayUrl?: string;
network?: Eip1193Provider;
networkUrl?: string;
coprocessorUrl?: string;
};
Expand Down Expand Up @@ -51,7 +55,7 @@ export type FhevmInstance = {
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 @@ export const createInstance = async (
throw new Error('chainId must be a number.');
} else if (networkUrl) {
chainId = await getChainIdFromNetwork(networkUrl);
} else if (network) {
chainId = await getChainIdFromEip1193(network);
} else {
throw new Error(
"You didn't provide the chainId nor the network url to get it.",
Expand All @@ -83,10 +89,8 @@ export const createInstance = async (
publicKey = data.publicKey;
} else if (networkUrl && !publicKey) {
publicKey = await getPublicKeyFromNetwork(networkUrl);
}

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

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

0 comments on commit 19fd3b8

Please sign in to comment.