Skip to content

Commit

Permalink
[DRAFT] MS2 (#11)
Browse files Browse the repository at this point in the history
* add the draft for `Web3ZkSync`

* add a very early draft for `ZKSyncWallet`

* apply lint:fx

* add very early draft for AdapterL1 and AdapterL2

* fix

* Adapters, zk l2, zk l1

* add 1 unit test for Web3ZkSyncL2

* fix build

* fix a test

* added test file and a method with a TODO

* Fix tests. implement Wallet

* tiny fix and apply lint:fix

* deposit + withdraw

* add wallet methods

* few code fixes to wallet2.test.ts and apply lint:fix

* fix build

* apply lint:fix

* populate transaction, sign, send refactor

* fix withdraw and deposit

* fix tests

* apply lint:fix

* add gasLimit

* fixes

* fixes and tests

* update fixtures

* fix wallet. tests

* feat: enhance plugin context (#20)

* enhance plugin context

* enhance plugin contracts init

* enhance plugin contracts init and add a test

* ensure the correct provider used inside the plugin

* revert a draft change

* tiny to fix provider usage at plugin

* link to a web3.js issue

* pre deploy state

* fix tests

* fix

* add env

* test

* secret to env

* test

* test

* use envs

* test

* test

* test

* test var

* test

* test

* fix

* move web3 to dependencies

* fix web3 version compatibility

* apply yarn lint:fix

* fix version

* v1.0.0-alpha.0

* ignore file

---------

Co-authored-by: Oleksii Kosynskyi <oleksii.kosynskyi@gmail.com>
  • Loading branch information
Muhammad-Altabba and avkos authored Jul 10, 2024
1 parent 51e36d2 commit 12db0da
Show file tree
Hide file tree
Showing 31 changed files with 6,465 additions and 643 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
test:
strategy:
matrix:
node: [18, 20]
node: [20]
name: Install and test
runs-on: ubuntu-latest
steps:
Expand All @@ -17,4 +17,5 @@ jobs:
node-version: ${{ matrix.node }}
cache: yarn
- run: yarn install
- run: yarn test
- run: yarn build
- run: export PRIVATE_KEY=${{secrets.PRIVATE_KEY}} && yarn test
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
.prettierrc.json
.prettierignore
*.log
.idea

coverage/
benchmark/
Expand Down
96 changes: 48 additions & 48 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
{
"name": "web3-plugin-zksync",
"version": "0.1.5",
"description": "web3.js plugin for ZkSync",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"homepage": "https://github.com/web3/web3-plugin-zksync#readme",
"bugs": {
"url": "https://github.com/web3/web3-plugin-zksync/issues"
},
"scripts": {
"lint": "eslint '{src,test}/**/*.ts'",
"lint:fix": "eslint '{src,test}/**/*.ts' --fix",
"build": "tsc --project tsconfig.build.json",
"test": "jest --config=./test/jest.config.js"
},
"contributors": [
"ChainSafe <info@chainsafe.io>"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/web3/web3-plugin-zksync.git"
},
"dependencies": {
"ethereum-cryptography": "^2.1.3",
"hardhat": "^2.19.4",
"web3-core": "^4.5.0",
"web3-eth-abi": "^4.2.2",
"web3-eth-accounts": "^4.1.2",
"web3-eth-contract": "^4.5.0",
"web3-types": "^1.7.0",
"web3-utils": "^4.3.0"
},
"devDependencies": {
"@chainsafe/eslint-config": "^2.1.1",
"@types/jest": "^29.5.11",
"@types/node": "^20.11.10",
"eslint": "8.56.0",
"jest": "^29.7.0",
"jest-extended": "^4.0.2",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typescript": "^5.3.3",
"web3": "^4.10.0"
},
"peerDependencies": {
"web3": ">= 4.0.3"
}
"name": "web3-plugin-zksync",
"version": "1.0.0-alpha.0",
"description": "web3.js plugin for ZkSync",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"homepage": "https://github.com/web3/web3-plugin-zksync#readme",
"bugs": {
"url": "https://github.com/web3/web3-plugin-zksync/issues"
},
"scripts": {
"lint": "eslint '{src,test}/**/*.ts'",
"lint:fix": "eslint '{src,test}/**/*.ts' --fix",
"build": "tsc --project tsconfig.build.json",
"test": "jest --config=./test/jest.config.js"
},
"contributors": [
"ChainSafe <info@chainsafe.io>"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/web3/web3-plugin-zksync.git"
},
"dependencies": {
"ethereum-cryptography": "^2.1.3",
"hardhat": "^2.19.4",
"web3": "4.10.1-dev.1436228.0",
"web3-core": "4.5.1-dev.1436228.0",
"web3-eth-abi": "^4.2.2",
"web3-eth-accounts": "^4.1.2",
"web3-eth-contract": "4.5.1-dev.1436228.0",
"web3-types": "1.7.1-dev.1436228.0",
"web3-utils": "4.3.1-dev.1436228.0"
},
"devDependencies": {
"@chainsafe/eslint-config": "^2.1.1",
"@types/jest": "^29.5.11",
"@types/node": "^20.11.10",
"eslint": "8.56.0",
"jest": "^29.7.0",
"jest-extended": "^4.0.2",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"peerDependencies": {
"web3": ">= 4.0.3"
}
}
70 changes: 54 additions & 16 deletions src/Eip712.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Bytes, Eip712TypedData, Numbers } from 'web3-types';
import * as web3Abi from 'web3-eth-abi';
import * as web3Utils from 'web3-utils';
import type * as web3Accounts from 'web3-eth-accounts';
import { BaseTransaction, toUint8Array } from 'web3-eth-accounts';
import { BaseTransaction, bigIntToUint8Array, toUint8Array } from 'web3-eth-accounts';
import { RLP } from '@ethereumjs/rlp';
import type { Address } from 'web3';
import {
Expand Down Expand Up @@ -62,27 +62,37 @@ function arrayToPaymasterParams(arr: Uint8Array): PaymasterParams | undefined {

export class EIP712 {
static getSignInput(transaction: Eip712TxData): Eip712SignedInput {
const maxFeePerGas = toHex(transaction.maxFeePerGas || transaction.gasPrice || 0n);
const maxPriorityFeePerGas = toHex(transaction.maxPriorityFeePerGas || maxFeePerGas);
const gasPerPubdataByteLimit = toHex(
transaction.customData?.gasPerPubdata || DEFAULT_GAS_PER_PUBDATA_LIMIT,
);
const maxFeePerGas = toBigInt(transaction.maxFeePerGas || transaction.gasPrice || 0n);
const maxPriorityFeePerGas = toBigInt(transaction.maxPriorityFeePerGas || maxFeePerGas);
const gasPerPubdataByteLimit =
transaction.customData?.gasPerPubdata || DEFAULT_GAS_PER_PUBDATA_LIMIT;
return {
txType: transaction.type || EIP712_TX_TYPE,
from: transaction.from ? toHex(transaction.from) : undefined,
to: transaction.to ? toHex(transaction.to) : undefined,
gasLimit: transaction.gasLimit ? toBigInt(transaction.gasLimit) : 0,
from: transaction.from
? typeof transaction.from === 'string'
? transaction.from
: toHex(transaction.from)
: undefined,
to: transaction.to
? typeof transaction.to === 'string'
? transaction.to
: toHex(transaction.to)
: undefined,
gasLimit: transaction.gasLimit ? toBigInt(transaction.gasLimit) : 0n,
gasPerPubdataByteLimit: gasPerPubdataByteLimit,
customData: transaction.customData,
maxFeePerGas,
maxPriorityFeePerGas,
paymaster: transaction.customData?.paymasterParams?.paymaster || ZERO_ADDRESS,
nonce: transaction.nonce ? toBigInt(transaction.nonce) : 0,
value: transaction.value ? toHex(transaction.value) : '0x0',
value: transaction.value ? toBigInt(transaction.value) : 0n,
data: transaction.data ? toHex(transaction.data) : '0x',
factoryDeps:
transaction.customData?.factoryDeps?.map((dep: Bytes) => hashBytecode(dep)) || [],
paymasterInput: transaction.customData?.paymasterParams?.paymasterInput || '0x',
customData:
transaction.customData && Object.keys(transaction.customData).length > 0
? transaction.customData
: undefined,
};
}

Expand Down Expand Up @@ -241,14 +251,17 @@ export class EIP712 {
const maxFeePerGas = toHex(transaction.maxFeePerGas || transaction.gasPrice || 0);
const maxPriorityFeePerGas = toHex(transaction.maxPriorityFeePerGas || maxFeePerGas);

const nonce = toHex(transaction.nonce || 0);
let gasLimitBytes = new Uint8Array();
if (transaction.gasLimit && toHex(transaction.gasLimit) !== '0x0') {
gasLimitBytes = toBytes(transaction.gasLimit);
}

const nonce = toBigInt(transaction.nonce || 0);
const fields: Array<Uint8Array | Uint8Array[] | string | number | string[]> = [
nonce === '0x0' ? new Uint8Array() : toBytes(nonce),
nonce === 0n ? new Uint8Array() : bigIntToUint8Array(nonce),
maxPriorityFeePerGas === '0x0' ? new Uint8Array() : toBytes(maxPriorityFeePerGas),
maxFeePerGas === '0x0' ? new Uint8Array() : toBytes(maxFeePerGas),
toHex(transaction.gasLimit || 0) === '0x0'
? new Uint8Array()
: toBytes(transaction.gasLimit!),
gasLimitBytes,
transaction.to ? web3Utils.toChecksumAddress(toHex(transaction.to)) : '0x',
toHex(transaction.value || 0) === '0x0'
? new Uint8Array()
Expand Down Expand Up @@ -322,6 +335,31 @@ export class EIP712Signer {
return new EIP712Transaction(tx).sign(toBytes(this.web3Account.privateKey)).getSignature();
}

/**
* Hashes the transaction request using EIP712.
*
* @param transaction The transaction request that needs to be hashed.
* @returns A hash (digest) of the transaction request.
*
* @throws {Error} If `transaction.chainId` is not set.
*/
static getSignedDigest(transaction: Eip712TxData): Bytes {
if (!transaction.chainId) {
throw Error("Transaction chainId isn't set!");
}

return EIP712.txHash(transaction);

// const domain = {
// name: 'zkSync',
// version: '2',
// chainId: transaction.chainId,
// };
// TODO: Implement replacement of the following line
// @ts-ignore
// return ethers.TypedDataEncoder.hash(domain, EIP712_TYPES, EIP712.getSignInput(transaction));
}

/**
* Returns zkSync Era EIP712 domain.
*/
Expand Down
Loading

0 comments on commit 12db0da

Please sign in to comment.