From 2cf7859159884e8420294d28821b628be96e82d8 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 28 Aug 2024 13:22:29 -0500 Subject: [PATCH] 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(); + }); });