From 2cf7859159884e8420294d28821b628be96e82d8 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 28 Aug 2024 13:22:29 -0500 Subject: [PATCH 1/3] fix issue where wallet_addEthereumChain does not attach a result to the response object when the currently selected rpcUrl matches the request --- .../handlers/add-ethereum-chain.js | 1 + .../handlers/add-ethereum-chain.test.js | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js b/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js index c9e39c1b8579..756895aa735e 100644 --- a/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js +++ b/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js @@ -139,6 +139,7 @@ async function addEthereumChainHandler( currentChainIdForDomain === chainId && currentRpcUrl === firstValidRPCUrl ) { + res.result = null; return end(); } diff --git a/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.test.js b/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.test.js index 2380825c9e3c..041964628b64 100644 --- a/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.test.js +++ b/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.test.js @@ -3,6 +3,7 @@ import { CHAIN_IDS, NETWORK_TYPES, } from '../../../../../shared/constants/network'; +import * as chainUtils from './ethereum-chain-utils'; import addEthereumChain from './add-ethereum-chain'; const NON_INFURA_CHAIN_ID = '0x123456789'; @@ -541,4 +542,42 @@ describe('addEthereumChainHandler', () => { }), ); }); + + it('should add result set to null to response object if the requested rpcUrl (and chainId) is currently selected', async () => { + jest.spyOn(chainUtils, 'findExistingNetwork').mockReturnValue({ + chainId: '0x1', + rpcUrl: 'https://mainnet.infura.io/v3/', + ticker: 'ETH', + }); + + const mocks = makeMocks({ + permissionsFeatureFlagIsActive: false, + overrides: { + getCurrentChainIdForDomain: jest.fn().mockReturnValue('0x1'), + }, + }); + const res = {}; + await addEthereumChainHandler( + { + origin: 'example.com', + params: [ + { + chainId: CHAIN_IDS.MAINNET, + chainName: 'Ethereum Mainnet', + rpcUrls: ['https://mainnet.infura.io/v3/'], + nativeCurrency: { + symbol: 'ETH', + decimals: 18, + }, + blockExplorerUrls: ['https://etherscan.io'], + }, + ], + }, + res, + jest.fn(), + jest.fn(), + mocks, + ); + expect(res.result).toBeNull(); + }); }); From 4c9364078ea8975b1a02189a36559b78eceaf23e Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 28 Aug 2024 16:24:16 -0500 Subject: [PATCH 2/3] improve test --- .../handlers/add-ethereum-chain.js | 1 - .../handlers/add-ethereum-chain.test.js | 28 ++++++++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js b/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js index 756895aa735e..571688688611 100644 --- a/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js +++ b/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js @@ -134,7 +134,6 @@ async function addEthereumChainHandler( } else { networkClientId = existingNetwork.id ?? existingNetwork.type; const currentRpcUrl = getCurrentRpcUrl(); - if ( currentChainIdForDomain === chainId && currentRpcUrl === firstValidRPCUrl diff --git a/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.test.js b/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.test.js index 041964628b64..2122b614d419 100644 --- a/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.test.js +++ b/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.test.js @@ -3,7 +3,6 @@ import { CHAIN_IDS, NETWORK_TYPES, } from '../../../../../shared/constants/network'; -import * as chainUtils from './ethereum-chain-utils'; import addEthereumChain from './add-ethereum-chain'; const NON_INFURA_CHAIN_ID = '0x123456789'; @@ -543,33 +542,36 @@ describe('addEthereumChainHandler', () => { ); }); - it('should add result set to null to response object if the requested rpcUrl (and chainId) is currently selected', async () => { - jest.spyOn(chainUtils, 'findExistingNetwork').mockReturnValue({ - chainId: '0x1', - rpcUrl: 'https://mainnet.infura.io/v3/', - ticker: 'ETH', - }); + it.only('should add result set to null to response object if the requested rpcUrl (and chainId) is currently selected', async () => { + const CURRENT_RPC_CONFIG = createMockNonInfuraConfiguration(); const mocks = makeMocks({ permissionsFeatureFlagIsActive: false, overrides: { - getCurrentChainIdForDomain: jest.fn().mockReturnValue('0x1'), + getCurrentChainIdForDomain: jest + .fn() + .mockReturnValue(CURRENT_RPC_CONFIG.chainId), + findNetworkConfigurationBy: jest + .fn() + .mockReturnValue(CURRENT_RPC_CONFIG), + getCurrentRpcUrl: jest.fn().mockReturnValue(CURRENT_RPC_CONFIG.rpcUrl), }, }); const res = {}; + await addEthereumChainHandler( { origin: 'example.com', params: [ { - chainId: CHAIN_IDS.MAINNET, - chainName: 'Ethereum Mainnet', - rpcUrls: ['https://mainnet.infura.io/v3/'], + chainId: CURRENT_RPC_CONFIG.chainId, + chainName: 'Custom Network', + rpcUrls: [CURRENT_RPC_CONFIG.rpcUrl], nativeCurrency: { - symbol: 'ETH', + symbol: CURRENT_RPC_CONFIG.ticker, decimals: 18, }, - blockExplorerUrls: ['https://etherscan.io'], + blockExplorerUrls: ['https://custom.blockexplorer'], }, ], }, From c4c185b94482870f8729b1c28258f53366a4cc82 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 28 Aug 2024 16:47:48 -0500 Subject: [PATCH 3/3] unfocus test --- .../rpc-method-middleware/handlers/add-ethereum-chain.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.test.js b/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.test.js index 2122b614d419..db4b967fa468 100644 --- a/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.test.js +++ b/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.test.js @@ -542,7 +542,7 @@ describe('addEthereumChainHandler', () => { ); }); - it.only('should add result set to null to response object if the requested rpcUrl (and chainId) is currently selected', async () => { + it('should add result set to null to response object if the requested rpcUrl (and chainId) is currently selected', async () => { const CURRENT_RPC_CONFIG = createMockNonInfuraConfiguration(); const mocks = makeMocks({