1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00

Ensure chainId comparison in switchEthereumChain handler is case insensitive (#20149)

This commit is contained in:
Dan J Miller 2023-07-24 16:34:51 -02:30 committed by GitHub
parent d5dbdfa912
commit 0ea765ebf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 129 additions and 1 deletions

View File

@ -116,7 +116,7 @@ async function switchEthereumChainHandler(
if (
Object.values(BUILT_IN_INFURA_NETWORKS)
.map(({ chainId: id }) => id)
.includes(chainId)
.includes(_chainId)
) {
await setProviderType(approvedRequestData.type);
} else {

View File

@ -0,0 +1,128 @@
import {
CHAIN_IDS,
NETWORK_TYPES,
} from '../../../../../shared/constants/network';
import switchEthereumChain from './switch-ethereum-chain';
const NON_INFURA_CHAIN_ID = '0x123456789';
const mockRequestUserApproval = ({ requestData }) => {
return Promise.resolve(requestData);
};
const MOCK_MAINNET_CONFIGURATION = {
id: 123,
chainId: CHAIN_IDS.MAINNET,
type: NETWORK_TYPES.MAINNET,
};
const MOCK_LINEA_MAINNET_CONFIGURATION = {
id: 123,
chainId: CHAIN_IDS.LINEA_MAINNET,
type: NETWORK_TYPES.LINEA_MAINNET,
};
describe('switchEthereumChainHandler', () => {
it('should call setProviderType when switching to a built in infura network', async () => {
const mockSetProviderType = jest.fn();
const mockSetActiveNetwork = jest.fn();
const switchEthereumChainHandler = switchEthereumChain.implementation;
await switchEthereumChainHandler(
{
origin: 'example.com',
params: [{ chainId: CHAIN_IDS.MAINNET }],
},
{},
jest.fn(),
jest.fn(),
{
getCurrentChainId: () => NON_INFURA_CHAIN_ID,
findNetworkConfigurationBy: () => MOCK_MAINNET_CONFIGURATION,
setProviderType: mockSetProviderType,
setActiveNetwork: mockSetActiveNetwork,
requestUserApproval: mockRequestUserApproval,
},
);
expect(mockSetProviderType).toHaveBeenCalledTimes(1);
expect(mockSetProviderType).toHaveBeenCalledWith(
MOCK_MAINNET_CONFIGURATION.type,
);
});
it('should call setProviderType when switching to a built in infura network, when chainId from request is lower case', async () => {
const mockSetProviderType = jest.fn();
const mockSetActiveNetwork = jest.fn();
const switchEthereumChainHandler = switchEthereumChain.implementation;
await switchEthereumChainHandler(
{
origin: 'example.com',
params: [{ chainId: CHAIN_IDS.LINEA_MAINNET.toLowerCase() }],
},
{},
jest.fn(),
jest.fn(),
{
getCurrentChainId: () => NON_INFURA_CHAIN_ID,
findNetworkConfigurationBy: () => MOCK_LINEA_MAINNET_CONFIGURATION,
setProviderType: mockSetProviderType,
setActiveNetwork: mockSetActiveNetwork,
requestUserApproval: mockRequestUserApproval,
},
);
expect(mockSetProviderType).toHaveBeenCalledTimes(1);
expect(mockSetProviderType).toHaveBeenCalledWith(
MOCK_LINEA_MAINNET_CONFIGURATION.type,
);
});
it('should call setProviderType when switching to a built in infura network, when chainId from request is upper case', async () => {
const mockSetProviderType = jest.fn();
const mockSetActiveNetwork = jest.fn();
const switchEthereumChainHandler = switchEthereumChain.implementation;
await switchEthereumChainHandler(
{
origin: 'example.com',
params: [{ chainId: CHAIN_IDS.LINEA_MAINNET.toUpperCase() }],
},
{},
jest.fn(),
jest.fn(),
{
getCurrentChainId: () => NON_INFURA_CHAIN_ID,
findNetworkConfigurationBy: () => MOCK_LINEA_MAINNET_CONFIGURATION,
setProviderType: mockSetProviderType,
setActiveNetwork: mockSetActiveNetwork,
requestUserApproval: mockRequestUserApproval,
},
);
expect(mockSetProviderType).toHaveBeenCalledTimes(1);
expect(mockSetProviderType).toHaveBeenCalledWith(
MOCK_LINEA_MAINNET_CONFIGURATION.type,
);
});
it('should call setActiveNetwork when switching to a custom network', async () => {
const mockSetProviderType = jest.fn();
const mockSetActiveNetwork = jest.fn();
const switchEthereumChainHandler = switchEthereumChain.implementation;
await switchEthereumChainHandler(
{
origin: 'example.com',
params: [{ chainId: NON_INFURA_CHAIN_ID }],
},
{},
jest.fn(),
jest.fn(),
{
getCurrentChainId: () => CHAIN_IDS.MAINNET,
findNetworkConfigurationBy: () => MOCK_MAINNET_CONFIGURATION,
setProviderType: mockSetProviderType,
setActiveNetwork: mockSetActiveNetwork,
requestUserApproval: mockRequestUserApproval,
},
);
expect(mockSetActiveNetwork).toHaveBeenCalledTimes(1);
expect(mockSetActiveNetwork).toHaveBeenCalledWith(
MOCK_MAINNET_CONFIGURATION.id,
);
});
});