From 7a0e42f454a5d85a00821c880a759e6a8de11745 Mon Sep 17 00:00:00 2001 From: Manuel Gellfart Date: Tue, 6 Feb 2024 16:14:08 +0100 Subject: [PATCH] fix: do not forward eth_chainId (#3208) - also fixes lint issues in test files --- .../safe-apps/AppFrame/useAppCommunicator.ts | 4 ++-- .../tx/SignOrExecuteForm/hooks.test.ts | 7 +++++++ src/hooks/wallets/useInitWeb3.ts | 7 +++---- src/hooks/wallets/web3.ts | 20 +++++++++++-------- .../tx/__tests__/encodeSignatures.test.ts | 3 +++ 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/components/safe-apps/AppFrame/useAppCommunicator.ts b/src/components/safe-apps/AppFrame/useAppCommunicator.ts index df36131719..e87a09472f 100644 --- a/src/components/safe-apps/AppFrame/useAppCommunicator.ts +++ b/src/components/safe-apps/AppFrame/useAppCommunicator.ts @@ -28,11 +28,11 @@ import type { Permission, PermissionRequest } from '@safe-global/safe-apps-sdk/d import type { SafeSettings } from '@safe-global/safe-apps-sdk' import AppCommunicator from '@/services/safe-apps/AppCommunicator' import { Errors, logError } from '@/services/exceptions' -import { createSafeAppsWeb3Provider } from '@/hooks/wallets/web3' import type { SafePermissionsRequest } from '@/hooks/safe-apps/permissions' import { SAFE_APPS_EVENTS, trackSafeAppEvent } from '@/services/analytics' import { useAppSelector } from '@/store' import { selectRpc } from '@/store/settingsSlice' +import { createSafeAppsWeb3Provider } from '@/hooks/wallets/web3' export enum CommunicatorMessages { REJECT_TRANSACTION_MESSAGE = 'Transaction was rejected', @@ -79,7 +79,7 @@ const useAppCommunicator = ( return } - return createSafeAppsWeb3Provider(chain.rpcUri, customRpc?.[chain.chainId]) + return createSafeAppsWeb3Provider(chain, customRpc?.[chain.chainId]) }, [chain, customRpc]) useEffect(() => { diff --git a/src/components/tx/SignOrExecuteForm/hooks.test.ts b/src/components/tx/SignOrExecuteForm/hooks.test.ts index b2ede6cbb6..d0bb6f7125 100644 --- a/src/components/tx/SignOrExecuteForm/hooks.test.ts +++ b/src/components/tx/SignOrExecuteForm/hooks.test.ts @@ -410,6 +410,7 @@ describe('SignOrExecute hooks', () => { data: '0x0001', staticPart: () => '', dynamicPart: () => '', + isContractSignature: false, }) const id = await executeTx({ gasPrice: 1 }, tx, '123', 'origin.com', true) @@ -442,6 +443,7 @@ describe('SignOrExecute hooks', () => { data: '0x0001', staticPart: () => '', dynamicPart: () => '', + isContractSignature: false, }) const proposeSpy = jest @@ -453,6 +455,7 @@ describe('SignOrExecute hooks', () => { data: '0x0001', staticPart: () => '', dynamicPart: () => '', + isContractSignature: false, }) return Promise.resolve(tx) }) @@ -492,6 +495,7 @@ describe('SignOrExecute hooks', () => { data: '0x0001', staticPart: () => '', dynamicPart: () => '', + isContractSignature: false, }) const proposeSpy = jest @@ -503,6 +507,7 @@ describe('SignOrExecute hooks', () => { data: '0x0001', staticPart: () => '', dynamicPart: () => '', + isContractSignature: false, }) return Promise.resolve(tx) }) @@ -535,6 +540,7 @@ describe('SignOrExecute hooks', () => { data: '0x0001', staticPart: () => '', dynamicPart: () => '', + isContractSignature: false, }) const { result } = renderHook(() => useAlreadySigned(tx)) expect(result.current).toEqual(true) @@ -554,6 +560,7 @@ describe('SignOrExecute hooks', () => { data: '0x0001', staticPart: () => '', dynamicPart: () => '', + isContractSignature: false, }) const { result } = renderHook(() => useAlreadySigned(tx)) expect(result.current).toEqual(false) diff --git a/src/hooks/wallets/useInitWeb3.ts b/src/hooks/wallets/useInitWeb3.ts index 1015214a39..e9e897ef35 100644 --- a/src/hooks/wallets/useInitWeb3.ts +++ b/src/hooks/wallets/useInitWeb3.ts @@ -9,7 +9,6 @@ import { selectRpc } from '@/store/settingsSlice' export const useInitWeb3 = () => { const chain = useCurrentChain() const chainId = chain?.chainId - const rpcUri = chain?.rpcUri const wallet = useWallet() const customRpc = useAppSelector(selectRpc) const customRpcUrl = chain ? customRpc?.[chain.chainId] : undefined @@ -24,11 +23,11 @@ export const useInitWeb3 = () => { }, [wallet, chainId]) useEffect(() => { - if (!rpcUri) { + if (!chain) { setWeb3ReadOnly(undefined) return } - const web3ReadOnly = createWeb3ReadOnly(rpcUri, customRpcUrl) + const web3ReadOnly = createWeb3ReadOnly(chain, customRpcUrl) setWeb3ReadOnly(web3ReadOnly) - }, [rpcUri, customRpcUrl]) + }, [chain, customRpcUrl]) } diff --git a/src/hooks/wallets/web3.ts b/src/hooks/wallets/web3.ts index 181b1875ef..3ac3a6792e 100644 --- a/src/hooks/wallets/web3.ts +++ b/src/hooks/wallets/web3.ts @@ -1,6 +1,6 @@ -import { RPC_AUTHENTICATION, type RpcUri } from '@safe-global/safe-gateway-typescript-sdk' +import { type ChainInfo, RPC_AUTHENTICATION, type RpcUri } from '@safe-global/safe-gateway-typescript-sdk' import { INFURA_TOKEN, SAFE_APPS_INFURA_TOKEN } from '@/config/constants' -import { JsonRpcProvider, BrowserProvider, type Eip1193Provider, type Provider } from 'ethers' +import { JsonRpcProvider, BrowserProvider, type Eip1193Provider, type Provider, Network } from 'ethers' import ExternalStore from '@/services/ExternalStore' import { EMPTY_DATA } from '@safe-global/protocol-kit/dist/src/utils/constants' @@ -20,20 +20,24 @@ export const getRpcServiceUrl = (rpcUri: RpcUri): string => { return formatRpcServiceUrl(rpcUri, INFURA_TOKEN) } -export const createWeb3ReadOnly = (rpcUri: RpcUri, customRpc?: string): JsonRpcProvider | undefined => { - const url = customRpc || getRpcServiceUrl(rpcUri) +export const createWeb3ReadOnly = (chain: ChainInfo, customRpc?: string): JsonRpcProvider | undefined => { + const url = customRpc || getRpcServiceUrl(chain.rpcUri) if (!url) return - return new JsonRpcProvider(url) + return new JsonRpcProvider(url, new Network(chain.chainName, chain.chainId), { + staticNetwork: true, + }) } export const createWeb3 = (walletProvider: Eip1193Provider): BrowserProvider => { return new BrowserProvider(walletProvider) } -export const createSafeAppsWeb3Provider = (safeAppsRpcUri: RpcUri, customRpc?: string): JsonRpcProvider | undefined => { - const url = customRpc || formatRpcServiceUrl(safeAppsRpcUri, SAFE_APPS_INFURA_TOKEN) +export const createSafeAppsWeb3Provider = (chain: ChainInfo, customRpc?: string): JsonRpcProvider | undefined => { + const url = customRpc || formatRpcServiceUrl(chain.rpcUri, SAFE_APPS_INFURA_TOKEN) if (!url) return - return new JsonRpcProvider(url) + return new JsonRpcProvider(url, new Network(chain.chainName, chain.chainId), { + staticNetwork: true, + }) } export const { setStore: setWeb3, useStore: useWeb3 } = new ExternalStore() diff --git a/src/services/tx/__tests__/encodeSignatures.test.ts b/src/services/tx/__tests__/encodeSignatures.test.ts index 28a7271806..c66c44bd41 100644 --- a/src/services/tx/__tests__/encodeSignatures.test.ts +++ b/src/services/tx/__tests__/encodeSignatures.test.ts @@ -32,6 +32,7 @@ describe('encodeSignatures', () => { data: '0xEEE', staticPart: () => '0xEEE', dynamicPart: () => '', + isContractSignature: false, }) safeTx.addSignature({ @@ -39,6 +40,7 @@ describe('encodeSignatures', () => { data: '0xAAA', staticPart: () => '0xAAA', dynamicPart: () => '', + isContractSignature: false, }) const owner = '0x123' @@ -57,6 +59,7 @@ describe('encodeSignatures', () => { data: '0xAAA', staticPart: () => '0xAAA', dynamicPart: () => '', + isContractSignature: false, }) const owner = '0x123'