From 68dd259a1c787f0812382eab9dcbd4d63a3e72d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20Mi=C3=B1o?= Date: Tue, 5 Mar 2024 14:50:41 -0300 Subject: [PATCH] more tests --- src/handleProviderRequest.test.ts | 79 +++++++++++++++++++++++++++++-- src/handleProviderRequest.ts | 44 +++++++++-------- src/references/messengers.ts | 2 +- 3 files changed, 100 insertions(+), 25 deletions(-) diff --git a/src/handleProviderRequest.test.ts b/src/handleProviderRequest.test.ts index fcaf875..286006a 100644 --- a/src/handleProviderRequest.test.ts +++ b/src/handleProviderRequest.test.ts @@ -5,7 +5,7 @@ import { ProviderRequestPayload, RequestResponse, } from './references/messengers'; -import { Address, isHex } from 'viem'; +import { Address, isHex, toHex } from 'viem'; import { mainnet, optimism } from 'viem/chains'; import { StaticJsonRpcProvider } from '@ethersproject/providers'; @@ -114,12 +114,21 @@ describe('handleProviderRequest', () => { SIGN_SIGNATURE + 'eth_signTypedData_v4' + payload.params[0], ); } + case 'wallet_addEthereumChain': { + return Promise.resolve(true); + } + case 'wallet_switchEthereumChain': { + return Promise.resolve(true); + } + case 'wallet_watchAsset': { + return Promise.resolve(true); + } default: { return Promise.resolve({}); } } }); - const onAddEthereumChainMock = vi.fn(() => ({ chainAlreadyAdded: true })); + const onAddEthereumChainMock = vi.fn(() => ({ chainAlreadyAdded: false })); const onSwitchEthereumChainNotSupportedMock = vi.fn(() => null); const onSwitchEthereumChainSupportedMock = vi.fn(() => null); @@ -458,9 +467,73 @@ describe('handleProviderRequest', () => { }, { id: 1 }, ); - console.log(response.result); expect(response.result).toBe( SIGN_SIGNATURE + 'eth_signTypedData_v4' + TYPED_MESSAGE, ); }); + + it('should call wallet_addEthereumChain correctly', async () => { + const response = await transport.send( + { + id: 1, + method: 'wallet_addEthereumChain', + params: [ + { + blockExplorerUrls: [mainnet.blockExplorers.default.url], + chainId: toHex(mainnet.id), + chainName: mainnet.network, + nativeCurrency: mainnet.nativeCurrency, + rpcUrls: [mainnet.rpcUrls.default.http], + }, + RAINBOWWALLET_ETH_ADDRESS, + ], + meta: { + sender: { url: 'https://dapp1.com' }, + topic: 'providerRequest', + }, + }, + { id: 1 }, + ); + expect(response.result).toBeNull(); + }); + + it('should call wallet_switchEthereumChain correctly', async () => { + const response = await transport.send( + { + id: 1, + method: 'wallet_switchEthereumChain', + params: [{ chainId: toHex(mainnet.id) }], + meta: { + sender: { url: 'https://dapp1.com' }, + topic: 'providerRequest', + }, + }, + { id: 1 }, + ); + expect(response.result).toBeNull(); + }); + + it('should call wallet_watchAsset correctly', async () => { + const response = await transport.send( + { + id: 1, + method: 'wallet_watchAsset', + params: { + type: 'ERC20', + options: { + address: '0xb60e8dd61c5d32be8058bb8eb970870f07233155', + symbol: 'FOO', + decimals: 18, + image: 'https://foo.io/token-image.svg', + }, + }, + meta: { + sender: { url: 'https://dapp1.com' }, + topic: 'providerRequest', + }, + }, + { id: 1 }, + ); + expect(response.result).toBeTruthy(); + }); }); diff --git a/src/handleProviderRequest.ts b/src/handleProviderRequest.ts index 9cfb462..e9bef5d 100644 --- a/src/handleProviderRequest.ts +++ b/src/handleProviderRequest.ts @@ -105,16 +105,16 @@ export const handleProviderRequest = ({ break; } case 'eth_getBalance': { + const p = params as Array; const provider = getProvider({ chainId: activeSession?.chainId }); - const balance = await provider.getBalance(params?.[0] as string); + const balance = await provider.getBalance(p?.[0] as string); response = toHex(balance); break; } case 'eth_getTransactionByHash': { + const p = params as Array; const provider = getProvider({ chainId: activeSession?.chainId }); - const transaction = await provider.getTransaction( - params?.[0] as string, - ); + const transaction = await provider.getTransaction(p?.[0] as string); const normalizedTransaction = normalizeTransactionResponsePayload(transaction); const { @@ -137,15 +137,15 @@ export const handleProviderRequest = ({ break; } case 'eth_call': { + const p = params as Array; const provider = getProvider({ chainId: activeSession?.chainId }); - response = await provider.call(params?.[0] as TransactionRequest); + response = await provider.call(p?.[0] as TransactionRequest); break; } case 'eth_estimateGas': { + const p = params as Array; const provider = getProvider({ chainId: activeSession?.chainId }); - const gas = await provider.estimateGas( - params?.[0] as TransactionRequest, - ); + const gas = await provider.estimateGas(p?.[0] as TransactionRequest); response = toHex(gas); break; } @@ -156,11 +156,9 @@ export const handleProviderRequest = ({ break; } case 'eth_getCode': { + const p = params as Array; const provider = getProvider({ chainId: activeSession?.chainId }); - response = await provider.getCode( - params?.[0] as string, - params?.[1] as string, - ); + response = await provider.getCode(p?.[0] as string, p?.[1] as string); break; } case 'eth_sendTransaction': @@ -170,11 +168,12 @@ export const handleProviderRequest = ({ case 'eth_signTypedData_v3': case 'eth_signTypedData_v4': { // If we need to validate the input before showing the UI, it should go here. + const p = params as Array; if (method === 'eth_signTypedData_v4') { // we don't trust the params order - let dataParam = params?.[1]; - if (!isAddress(params?.[0] as Address)) { - dataParam = params?.[0]; + let dataParam = p?.[1]; + if (!isAddress(p?.[0] as Address)) { + dataParam = p?.[0]; } const data = @@ -201,7 +200,8 @@ export const handleProviderRequest = ({ break; } case 'wallet_addEthereumChain': { - const proposedChain = params?.[0] as AddEthereumChainProposedChain; + const p = params as Array; + const proposedChain = p?.[0] as AddEthereumChainProposedChain; const proposedChainId = Number(proposedChain.chainId); const featureFlags = getFeatureFlags(); if (!featureFlags.custom_rpc) { @@ -285,7 +285,8 @@ export const handleProviderRequest = ({ break; } case 'wallet_switchEthereumChain': { - const proposedChain = params?.[0] as AddEthereumChainProposedChain; + const p = params as Array; + const proposedChain = p?.[0] as AddEthereumChainProposedChain; const supportedChainId = isSupportedChain?.( Number(proposedChain.chainId), ); @@ -321,7 +322,6 @@ export const handleProviderRequest = ({ decimals?: number; }; }; - if (type !== 'ERC20') { throw new Error('Method supported only for ERC20'); } @@ -369,12 +369,14 @@ export const handleProviderRequest = ({ response = [address?.toLowerCase()]; break; } - case 'personal_ecRecover': + case 'personal_ecRecover': { + const p = params as Array; response = recoverPersonalSignature({ - data: params?.[0] as string, - signature: params?.[1] as string, + data: p?.[0] as string, + signature: p?.[1] as string, }); break; + } default: { try { if (method?.substring(0, 7) === 'wallet_') { diff --git a/src/references/messengers.ts b/src/references/messengers.ts index c00633a..a629e0f 100644 --- a/src/references/messengers.ts +++ b/src/references/messengers.ts @@ -3,7 +3,7 @@ import { RPCMethod } from './ethereum'; export type RequestArguments = { id?: number; method: RPCMethod; - params?: Array; + params?: Array | object; }; export type RequestResponse =