Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanmino committed Mar 6, 2024
1 parent b40400c commit 68dd259
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 25 deletions.
79 changes: 76 additions & 3 deletions src/handleProviderRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
});
});
44 changes: 23 additions & 21 deletions src/handleProviderRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ export const handleProviderRequest = ({
break;
}
case 'eth_getBalance': {
const p = params as Array<unknown>;
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<unknown>;
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 {
Expand All @@ -137,15 +137,15 @@ export const handleProviderRequest = ({
break;
}
case 'eth_call': {
const p = params as Array<unknown>;
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<unknown>;
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;
}
Expand All @@ -156,11 +156,9 @@ export const handleProviderRequest = ({
break;
}
case 'eth_getCode': {
const p = params as Array<unknown>;
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':
Expand All @@ -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<unknown>;
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 =
Expand All @@ -201,7 +200,8 @@ export const handleProviderRequest = ({
break;
}
case 'wallet_addEthereumChain': {
const proposedChain = params?.[0] as AddEthereumChainProposedChain;
const p = params as Array<unknown>;
const proposedChain = p?.[0] as AddEthereumChainProposedChain;
const proposedChainId = Number(proposedChain.chainId);
const featureFlags = getFeatureFlags();
if (!featureFlags.custom_rpc) {
Expand Down Expand Up @@ -285,7 +285,8 @@ export const handleProviderRequest = ({
break;
}
case 'wallet_switchEthereumChain': {
const proposedChain = params?.[0] as AddEthereumChainProposedChain;
const p = params as Array<unknown>;
const proposedChain = p?.[0] as AddEthereumChainProposedChain;
const supportedChainId = isSupportedChain?.(
Number(proposedChain.chainId),
);
Expand Down Expand Up @@ -321,7 +322,6 @@ export const handleProviderRequest = ({
decimals?: number;
};
};

if (type !== 'ERC20') {
throw new Error('Method supported only for ERC20');
}
Expand Down Expand Up @@ -369,12 +369,14 @@ export const handleProviderRequest = ({
response = [address?.toLowerCase()];
break;
}
case 'personal_ecRecover':
case 'personal_ecRecover': {
const p = params as Array<unknown>;
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_') {
Expand Down
2 changes: 1 addition & 1 deletion src/references/messengers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RPCMethod } from './ethereum';
export type RequestArguments = {
id?: number;
method: RPCMethod;
params?: Array<unknown>;
params?: Array<unknown> | object;
};

export type RequestResponse =
Expand Down

0 comments on commit 68dd259

Please sign in to comment.