From 6654ed4bfbfef1f49c83c1d27f2c38f189007797 Mon Sep 17 00:00:00 2001 From: Lucas Santos Date: Wed, 27 Nov 2024 18:33:05 -0300 Subject: [PATCH 1/4] feat: add support for L2 networks [zksync] --- packages/sysweb3-keyring/package.json | 3 +- packages/sysweb3-keyring/src/providers.ts | 39 ++++++++++++++++++- .../src/transactions/ethereum.ts | 18 ++++++--- packages/sysweb3-keyring/src/types.ts | 5 ++- packages/sysweb3-keyring/yarn.lock | 9 ++++- 5 files changed, 64 insertions(+), 10 deletions(-) diff --git a/packages/sysweb3-keyring/package.json b/packages/sysweb3-keyring/package.json index 7c660b8..cba9005 100644 --- a/packages/sysweb3-keyring/package.json +++ b/packages/sysweb3-keyring/package.json @@ -56,7 +56,8 @@ "satoshi-bitcoin": "^1.0.5", "syscoinjs-lib": "^1.0.218", "syscointx-js": "^1.0.106", - "web3": "^1.7.1" + "web3": "^1.7.1", + "zksync-ethers": "5" }, "devDependencies": { "@types/crypto-js": "^4.1.3", diff --git a/packages/sysweb3-keyring/src/providers.ts b/packages/sysweb3-keyring/src/providers.ts index f65e64e..cd7e368 100644 --- a/packages/sysweb3-keyring/src/providers.ts +++ b/packages/sysweb3-keyring/src/providers.ts @@ -3,10 +3,12 @@ import { deepCopy } from '@ethersproject/properties'; import { fetchJson } from '@ethersproject/web'; import { BigNumber, ethers, logger } from 'ethers'; import { ConnectionInfo, Logger, shallowCopy } from 'ethers/lib/utils'; +import { Provider } from 'zksync-ethers'; import { handleStatusCodeError } from './errorUtils'; import { checkError } from './utils'; -export class CustomJsonRpcProvider extends ethers.providers.JsonRpcProvider { + +class BaseProvider extends ethers.providers.JsonRpcProvider { private timeoutCounter = 0; private isPossibleGetChainId = true; private cooldownTime = 120 * 1000; @@ -356,3 +358,38 @@ export class CustomJsonRpcProvider extends ethers.providers.JsonRpcProvider { return promise; } } + +export class CustomJsonRpcProvider extends BaseProvider { + constructor( + signal: AbortSignal, + url?: ConnectionInfo | string, + network?: Networkish + ) { + super(signal, url, network); + } +} + +export class CustomL2JsonRpcProvider extends Provider { + private baseProvider: BaseProvider; + + constructor( + signal: AbortSignal, + url?: ConnectionInfo | string, + network?: ethers.providers.Networkish + ) { + super(url, network); + this.baseProvider = new BaseProvider(signal, url, network); + } + + perform(method: string, params: any) { + return this.baseProvider.perform(method, params); + } + + send(method: string, params: any[]) { + return this.baseProvider.send(method, params); + } + + sendBatch(method: string, params: any[]) { + return this.baseProvider.sendBatch(method, params); + } +} diff --git a/packages/sysweb3-keyring/src/transactions/ethereum.ts b/packages/sysweb3-keyring/src/transactions/ethereum.ts index a590146..967ef80 100644 --- a/packages/sysweb3-keyring/src/transactions/ethereum.ts +++ b/packages/sysweb3-keyring/src/transactions/ethereum.ts @@ -24,9 +24,10 @@ import { BigNumber, ethers } from 'ethers'; import { Deferrable } from 'ethers/lib/utils'; import floor from 'lodash/floor'; import omit from 'lodash/omit'; +import { Provider } from 'zksync-ethers'; import { LedgerKeyring } from '../ledger'; -import { CustomJsonRpcProvider } from '../providers'; +import { CustomJsonRpcProvider, CustomL2JsonRpcProvider } from '../providers'; import { SyscoinHDSigner } from '../signers'; import { TrezorKeyring } from '../trezor'; import { @@ -48,8 +49,8 @@ import { } from '@pollum-io/sysweb3-utils'; export class EthereumTransactions implements IEthereumTransactions { - public web3Provider: CustomJsonRpcProvider; - public contentScriptWeb3Provider: CustomJsonRpcProvider; + public web3Provider: CustomJsonRpcProvider | Provider; + public contentScriptWeb3Provider: CustomJsonRpcProvider | Provider; public trezorSigner: TrezorKeyring; public ledgerSigner: LedgerKeyring; private getNetwork: () => INetwork; @@ -1775,11 +1776,18 @@ export class EthereumTransactions implements IEthereumTransactions { public setWeb3Provider(network: INetwork) { this.abortController.abort(); this.abortController = new AbortController(); - this.web3Provider = new CustomJsonRpcProvider( + const L2Networks = [324, 300]; + const isL2Network = L2Networks.includes(network.chainId); + + const CurrentProvider = isL2Network + ? CustomL2JsonRpcProvider + : CustomJsonRpcProvider; + + this.web3Provider = new CurrentProvider( this.abortController.signal, network.url ); - this.contentScriptWeb3Provider = new CustomJsonRpcProvider( + this.contentScriptWeb3Provider = new CurrentProvider( this.abortController.signal, network.url ); diff --git a/packages/sysweb3-keyring/src/types.ts b/packages/sysweb3-keyring/src/types.ts index 6814596..3bb0b07 100644 --- a/packages/sysweb3-keyring/src/types.ts +++ b/packages/sysweb3-keyring/src/types.ts @@ -8,6 +8,7 @@ import { SignedTransaction, TransactionConfig, } from 'web3-core'; +import { Provider } from 'zksync-ethers'; import { LedgerKeyring } from './ledger'; import { TrezorKeyring } from './trezor'; @@ -134,8 +135,8 @@ export interface IEthereumTransactions { ) => Promise; setWeb3Provider: (network: INetwork) => void; importAccount: (mnemonicOrPrivKey: string) => ethers.Wallet; - web3Provider: CustomJsonRpcProvider; - contentScriptWeb3Provider: CustomJsonRpcProvider; + web3Provider: CustomJsonRpcProvider | Provider; + contentScriptWeb3Provider: CustomJsonRpcProvider | Provider; } export interface ISyscoinTransactions { diff --git a/packages/sysweb3-keyring/yarn.lock b/packages/sysweb3-keyring/yarn.lock index 6df59c3..2fbeca2 100644 --- a/packages/sysweb3-keyring/yarn.lock +++ b/packages/sysweb3-keyring/yarn.lock @@ -2760,7 +2760,7 @@ ethereumjs-wallet@^1.0.2: utf8 "^3.0.0" uuid "^8.3.2" -ethers@5.7.2, ethers@^5.6.4, ethers@^5.6.9: +ethers@5.7.2, ethers@^5.6.4, ethers@^5.6.9, ethers@~5.7.0: version "5.7.2" resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.7.2.tgz#3a7deeabbb8c030d4126b24f84e525466145872e" integrity sha512-wswUsmWo1aOK8rR7DIKiWSw9DbLWe6x98Jrn8wcTflTVvaXhAMaB5zGAXy0GYQEQp9iO1iSHWVyARQm11zUtyg== @@ -5349,3 +5349,10 @@ yallist@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72" integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A== + +zksync-ethers@5: + version "5.10.0" + resolved "https://registry.yarnpkg.com/zksync-ethers/-/zksync-ethers-5.10.0.tgz#3caf25478539a3fd6e170c13555cec6a1dae32e2" + integrity sha512-OAjTGAHF9wbdkRGkj7XZuF/a1Sk/FVbwH4pmLjAKlR7mJ7sQtQhBhrPU2dCc67xLaNvEESPfwil19ES5wooYFg== + dependencies: + ethers "~5.7.0" From 6d2bf3a47b9a9cdc068663dda679ceb14aaba4ca Mon Sep 17 00:00:00 2001 From: Lucas Santos Date: Thu, 28 Nov 2024 09:45:30 -0300 Subject: [PATCH 2/4] chore: use CustomL2JsonRpcProvider as type --- packages/sysweb3-keyring/src/transactions/ethereum.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/sysweb3-keyring/src/transactions/ethereum.ts b/packages/sysweb3-keyring/src/transactions/ethereum.ts index 967ef80..1ba2c9a 100644 --- a/packages/sysweb3-keyring/src/transactions/ethereum.ts +++ b/packages/sysweb3-keyring/src/transactions/ethereum.ts @@ -24,7 +24,6 @@ import { BigNumber, ethers } from 'ethers'; import { Deferrable } from 'ethers/lib/utils'; import floor from 'lodash/floor'; import omit from 'lodash/omit'; -import { Provider } from 'zksync-ethers'; import { LedgerKeyring } from '../ledger'; import { CustomJsonRpcProvider, CustomL2JsonRpcProvider } from '../providers'; @@ -49,8 +48,10 @@ import { } from '@pollum-io/sysweb3-utils'; export class EthereumTransactions implements IEthereumTransactions { - public web3Provider: CustomJsonRpcProvider | Provider; - public contentScriptWeb3Provider: CustomJsonRpcProvider | Provider; + public web3Provider: CustomJsonRpcProvider | CustomL2JsonRpcProvider; + public contentScriptWeb3Provider: + | CustomJsonRpcProvider + | CustomL2JsonRpcProvider; public trezorSigner: TrezorKeyring; public ledgerSigner: LedgerKeyring; private getNetwork: () => INetwork; From 21a48475af43fd585c3145a3c7c71ac507cdc96c Mon Sep 17 00:00:00 2001 From: Lucas Santos Date: Thu, 28 Nov 2024 09:47:35 -0300 Subject: [PATCH 3/4] chore: adjust types --- packages/sysweb3-keyring/src/types.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/sysweb3-keyring/src/types.ts b/packages/sysweb3-keyring/src/types.ts index 3bb0b07..a1be967 100644 --- a/packages/sysweb3-keyring/src/types.ts +++ b/packages/sysweb3-keyring/src/types.ts @@ -1,14 +1,13 @@ import { TransactionResponse } from '@ethersproject/abstract-provider'; import { TypedData, TypedMessage } from 'eth-sig-util'; import { ethers, BigNumber, BigNumberish } from 'ethers'; -import { CustomJsonRpcProvider } from 'providers'; +import { CustomJsonRpcProvider, CustomL2JsonRpcProvider } from 'providers'; import { EncryptedKeystoreV3Json, Sign, SignedTransaction, TransactionConfig, } from 'web3-core'; -import { Provider } from 'zksync-ethers'; import { LedgerKeyring } from './ledger'; import { TrezorKeyring } from './trezor'; @@ -135,8 +134,8 @@ export interface IEthereumTransactions { ) => Promise; setWeb3Provider: (network: INetwork) => void; importAccount: (mnemonicOrPrivKey: string) => ethers.Wallet; - web3Provider: CustomJsonRpcProvider | Provider; - contentScriptWeb3Provider: CustomJsonRpcProvider | Provider; + web3Provider: CustomJsonRpcProvider | CustomL2JsonRpcProvider; + contentScriptWeb3Provider: CustomJsonRpcProvider | CustomL2JsonRpcProvider; } export interface ISyscoinTransactions { From dde8ad5c3d559e1e866cb03a7366d68839270280 Mon Sep 17 00:00:00 2001 From: Lucas Santos Date: Mon, 9 Dec 2024 08:18:34 -0300 Subject: [PATCH 4/4] chore: publish and update package version --- packages/sysweb3-keyring/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sysweb3-keyring/package.json b/packages/sysweb3-keyring/package.json index cba9005..36d4f9c 100644 --- a/packages/sysweb3-keyring/package.json +++ b/packages/sysweb3-keyring/package.json @@ -1,6 +1,6 @@ { "name": "@pollum-io/sysweb3-keyring", - "version": "1.0.485", + "version": "1.0.486", "description": "Keyring Manager for UTXO and Web3 Wallets", "main": "cjs/index.js", "types": "types/index.d.ts",