From ab6bf376b19e2a1a20a109893dc52f64345e151d Mon Sep 17 00:00:00 2001 From: jiexi Date: Wed, 5 Jun 2024 08:36:55 -0700 Subject: [PATCH] fix: `SelectedNetworkController` permission state change handler (#4368) ## Explanation Fixes a bug with `SelectedNetworkController` where it incorrectly sets the networkClientId for a newly permitted domain when the useRequestQueue flag is set to false. ## References See: https://github.com/MetaMask/metamask-extension/pull/25046 ## Changelog ### `@metamask/selected-network-controller` - **FIXED**: No longer sets the networkClientId for a newly permitted domain unless the `useRequestQueuePreference` flag is true ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've highlighted breaking changes using the "BREAKING" category above as appropriate --------- Co-authored-by: Alex Donesky --- .../src/SelectedNetworkController.ts | 6 +++- .../tests/SelectedNetworkController.test.ts | 29 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/selected-network-controller/src/SelectedNetworkController.ts b/packages/selected-network-controller/src/SelectedNetworkController.ts index b7cd143452..2d611554fd 100644 --- a/packages/selected-network-controller/src/SelectedNetworkController.ts +++ b/packages/selected-network-controller/src/SelectedNetworkController.ts @@ -172,7 +172,11 @@ export class SelectedNetworkController extends BaseController< path[0] === 'subjects' && path[1] !== undefined; if (isChangingSubject && typeof path[1] === 'string') { const domain = path[1]; - if (op === 'add' && this.state.domains[domain] === undefined) { + if ( + op === 'add' && + this.state.domains[domain] === undefined && + this.#useRequestQueuePreference + ) { this.setNetworkClientIdForDomain( domain, this.messagingSystem.call('NetworkController:getState') diff --git a/packages/selected-network-controller/tests/SelectedNetworkController.test.ts b/packages/selected-network-controller/tests/SelectedNetworkController.test.ts index 315329a11f..0ca5a3b99d 100644 --- a/packages/selected-network-controller/tests/SelectedNetworkController.test.ts +++ b/packages/selected-network-controller/tests/SelectedNetworkController.test.ts @@ -617,8 +617,10 @@ describe('SelectedNetworkController', () => { }); describe('When a permission is added or removed', () => { - it('should add new domain to domains list on permission add', async () => { - const { controller, messenger } = setup(); + it('should add new domain to domains list on permission add if #useRequestQueuePreference is true', async () => { + const { controller, messenger } = setup({ + useRequestQueuePreference: true, + }); const mockPermission = { parentCapability: 'eth_accounts', id: 'example.com', @@ -638,6 +640,29 @@ describe('SelectedNetworkController', () => { expect(domains['example.com']).toBeDefined(); }); + it('should not add new domain to domains list on permission add if #useRequestQueuePreference is false', async () => { + const { controller, messenger } = setup({ + useRequestQueuePreference: false, + }); + const mockPermission = { + parentCapability: 'eth_accounts', + id: 'example.com', + date: Date.now(), + caveats: [{ type: 'restrictToAccounts', value: ['0x...'] }], + }; + + messenger.publish('PermissionController:stateChange', { subjects: {} }, [ + { + op: 'add', + path: ['subjects', 'example.com', 'permissions'], + value: mockPermission, + }, + ]); + + const { domains } = controller.state; + expect(domains['example.com']).toBeUndefined(); + }); + describe('on permission removal', () => { it('should remove domain from domains list', async () => { const { controller, messenger } = setup({