diff --git a/src/hooks/wallets/web3.ts b/src/hooks/wallets/web3.ts index 2bb3b649d0..ae31d511c3 100644 --- a/src/hooks/wallets/web3.ts +++ b/src/hooks/wallets/web3.ts @@ -29,7 +29,7 @@ export const getRpcServiceUrl = (rpcUri: RpcUri): string => { export const createWeb3ReadOnly = (chain: ChainInfo, customRpc?: string): JsonRpcProvider | undefined => { const url = customRpc || getRpcServiceUrl(chain.rpcUri) if (!url) return - return new JsonRpcProvider(url, undefined, { + return new JsonRpcProvider(url, Number(chain.chainId), { staticNetwork: true, batchMaxCount: BATCH_MAX_COUNT, }) diff --git a/src/services/private-key-module/icon.ts b/src/services/private-key-module/icon.ts index 85f3e6826a..69c435dfb8 100644 --- a/src/services/private-key-module/icon.ts +++ b/src/services/private-key-module/icon.ts @@ -1,5 +1,5 @@ -const icon = ` - +const icon = ` + ` export default icon diff --git a/src/services/private-key-module/index.ts b/src/services/private-key-module/index.ts index 9bd04ae7c0..9ca734c6d8 100644 --- a/src/services/private-key-module/index.ts +++ b/src/services/private-key-module/index.ts @@ -3,8 +3,9 @@ import type { ChainInfo } from '@safe-global/safe-gateway-typescript-sdk' import { type WalletInit, createEIP1193Provider } from '@web3-onboard/common' import { getRpcServiceUrl } from '@/hooks/wallets/web3' import pkPopupStore from './pk-popup-store' +import { numberToHex } from '@/utils/hex' -export const PRIVATE_KEY_MODULE_LABEL = 'Private Key' +export const PRIVATE_KEY_MODULE_LABEL = 'Private key' async function getPrivateKey() { const savedKey = pkPopupStore.getStore()?.privateKey @@ -23,7 +24,13 @@ async function getPrivateKey() { }) } +let currentChainId = '' +let currentRpcUri = '' + const PrivateKeyModule = (chainId: ChainInfo['chainId'], rpcUri: ChainInfo['rpcUri']): WalletInit => { + currentChainId = chainId + currentRpcUri = getRpcServiceUrl(rpcUri) + return () => { return { label: PRIVATE_KEY_MODULE_LABEL, @@ -34,20 +41,41 @@ const PrivateKeyModule = (chainId: ChainInfo['chainId'], rpcUri: ChainInfo['rpcU throw new Error('You rejected the connection') } - const provider = new JsonRpcProvider(getRpcServiceUrl(rpcUri)) - const wallet = new Wallet(privateKey, provider) + let provider: JsonRpcProvider + let wallet: Wallet + let lastChainId = '' + const chainChangedListeners = new Set<(chainId: string) => void>() + + const updateProvider = () => { + console.log('[Private key signer] Updating provider to chainId', currentChainId, currentRpcUri) + provider?.destroy() + provider = new JsonRpcProvider(currentRpcUri, Number(currentChainId), { staticNetwork: true }) + wallet = new Wallet(privateKey, provider) + lastChainId = currentChainId + chainChangedListeners.forEach((listener) => listener(numberToHex(Number(currentChainId)))) + } + + updateProvider() return { provider: createEIP1193Provider( { - ...wallet.provider, on: (event: string, listener: (...args: any[]) => void) => { if (event === 'accountsChanged') { } else if (event === 'chainChanged') { + chainChangedListeners.add(listener) } else { provider.on(event, listener) } }, + + request: async (request: { method: string; params: any[] }) => { + if (currentChainId !== lastChainId) { + updateProvider() + } + return provider.send(request.method, request.params) + }, + disconnect: () => { pkPopupStore.setStore({ isOpen: false, @@ -56,7 +84,7 @@ const PrivateKeyModule = (chainId: ChainInfo['chainId'], rpcUri: ChainInfo['rpcU }, }, { - eth_chainId: async () => chainId, + eth_chainId: async () => currentChainId, // @ts-ignore eth_getCode: async ({ params }) => provider.getCode(params[0], params[1]), @@ -80,6 +108,12 @@ const PrivateKeyModule = (chainId: ChainInfo['chainId'], rpcUri: ChainInfo['rpcU const signedMessage = await wallet.signTypedData(params[1].domain, params[1].data, params[1].value) return signedMessage }, + + // @ts-ignore + wallet_switchEthereumChain: async ({ params }) => { + console.log('[Private key signer] Switching chain', params) + updateProvider() + }, }, ), }