From 23736c088969a545977e729acad28daf49423fb1 Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Thu, 4 Sep 2025 12:55:32 +0200 Subject: [PATCH 1/4] migrate `DelegationController` messenger --- packages/delegation-controller/package.json | 1 + .../src/DelegationController.test.ts | 52 +++++++++++++------ .../src/DelegationController.ts | 8 +-- packages/delegation-controller/src/types.ts | 10 ++-- yarn.lock | 1 + 5 files changed, 46 insertions(+), 26 deletions(-) diff --git a/packages/delegation-controller/package.json b/packages/delegation-controller/package.json index 6968425777c..e9967127901 100644 --- a/packages/delegation-controller/package.json +++ b/packages/delegation-controller/package.json @@ -48,6 +48,7 @@ }, "dependencies": { "@metamask/base-controller": "^8.2.0", + "@metamask/messenger": "^0.1.0", "@metamask/utils": "^11.4.2" }, "devDependencies": { diff --git a/packages/delegation-controller/src/DelegationController.test.ts b/packages/delegation-controller/src/DelegationController.test.ts index c1420d70716..802884f7200 100644 --- a/packages/delegation-controller/src/DelegationController.test.ts +++ b/packages/delegation-controller/src/DelegationController.test.ts @@ -1,9 +1,11 @@ -import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller'; -import { Messenger } from '@metamask/base-controller'; +import { SignTypedDataVersion } from '@metamask/keyring-controller'; import { - type KeyringControllerSignTypedMessageAction, - SignTypedDataVersion, -} from '@metamask/keyring-controller'; + Messenger, + MOCK_ANY_NAMESPACE, + type MessengerActions, + type MessengerEvents, + type MockAnyNamespace, +} from '@metamask/messenger'; import { hexToNumber } from '@metamask/utils'; import { ROOT_AUTHORITY } from './constants'; @@ -11,7 +13,7 @@ import { controllerName, DelegationController } from './DelegationController'; import type { Address, Delegation, - DelegationControllerEvents, + DelegationControllerMessenger, DelegationControllerState, DelegationEntry, DeleGatorEnvironment, @@ -19,6 +21,18 @@ import type { } from './types'; import { toDelegationStruct } from './utils'; +type AllDelegationControllerActions = + MessengerActions; + +type AllDelegationControllerEvents = + MessengerEvents; + +type RootMessenger = Messenger< + MockAnyNamespace, + AllDelegationControllerActions, + AllDelegationControllerEvents +>; + const FROM_MOCK = '0x2234567890123456789012345678901234567890' as Address; const SIGNATURE_HASH_MOCK = '0x123ABC'; @@ -60,11 +74,9 @@ class TestDelegationController extends DelegationController { * @returns The mock messenger instance plus individual mock functions for each action. */ function createMessengerMock() { - const messenger = new Messenger< - | KeyringControllerSignTypedMessageAction - | AccountsControllerGetSelectedAccountAction, - DelegationControllerEvents - >(); + const messenger: RootMessenger = new Messenger({ + namespace: MOCK_ANY_NAMESPACE, + }); const accountsControllerGetSelectedAccountMock = jest.fn(); const keyringControllerSignTypedMessageMock = jest.fn(); @@ -84,19 +96,27 @@ function createMessengerMock() { keyringControllerSignTypedMessageMock, ); - const restrictedMessenger = messenger.getRestricted({ - name: `${controllerName}`, - allowedActions: [ + const delegationControllerMessenger = new Messenger< + 'DelegationController', + AllDelegationControllerActions, + AllDelegationControllerEvents, + RootMessenger + >({ + namespace: controllerName, + parent: messenger, + }); + messenger.delegate({ + messenger: delegationControllerMessenger, + actions: [ 'AccountsController:getSelectedAccount', 'KeyringController:signTypedMessage', ], - allowedEvents: [], }); return { accountsControllerGetSelectedAccountMock, keyringControllerSignTypedMessageMock, - messenger: restrictedMessenger, + messenger: delegationControllerMessenger, }; } diff --git a/packages/delegation-controller/src/DelegationController.ts b/packages/delegation-controller/src/DelegationController.ts index 805c12dd946..df6be61f564 100644 --- a/packages/delegation-controller/src/DelegationController.ts +++ b/packages/delegation-controller/src/DelegationController.ts @@ -1,5 +1,5 @@ -import type { StateMetadata } from '@metamask/base-controller'; -import { BaseController } from '@metamask/base-controller'; +import type { StateMetadata } from '@metamask/base-controller/next'; +import { BaseController } from '@metamask/base-controller/next'; import { SignTypedDataVersion } from '@metamask/keyring-controller'; import { hexToNumber } from '@metamask/utils'; @@ -113,7 +113,7 @@ export class DelegationController extends BaseController< // TODO:: Replace with `SignatureController:newUnsignedTypedMessage`. // Waiting on confirmations team to implement this. - const signature: string = await this.messagingSystem.call( + const signature: string = await this.messenger.call( 'KeyringController:signTypedMessage', data, SignTypedDataVersion.V4, @@ -152,7 +152,7 @@ export class DelegationController extends BaseController< * @returns A list of delegation entries that match the filter. */ list(filter?: DelegationFilter) { - const account = this.messagingSystem.call( + const account = this.messenger.call( 'AccountsController:getSelectedAccount', ); const requester = account.address as Address; diff --git a/packages/delegation-controller/src/types.ts b/packages/delegation-controller/src/types.ts index 20c73de1578..453a826be01 100644 --- a/packages/delegation-controller/src/types.ts +++ b/packages/delegation-controller/src/types.ts @@ -2,9 +2,9 @@ import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accou import type { ControllerGetStateAction, ControllerStateChangeEvent, - RestrictedMessenger, -} from '@metamask/base-controller'; +} from '@metamask/base-controller/next'; import type { KeyringControllerSignTypedMessageAction } from '@metamask/keyring-controller'; +import type { Messenger } from '@metamask/messenger'; import type { controllerName, @@ -149,10 +149,8 @@ type AllowedActions = type AllowedEvents = never; -export type DelegationControllerMessenger = RestrictedMessenger< +export type DelegationControllerMessenger = Messenger< typeof controllerName, DelegationControllerActions | AllowedActions, - DelegationControllerEvents | AllowedEvents, - AllowedActions['type'], - AllowedEvents['type'] + DelegationControllerEvents | AllowedEvents >; diff --git a/yarn.lock b/yarn.lock index 6ba685274fa..8ab643c21e9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3009,6 +3009,7 @@ __metadata: "@metamask/auto-changelog": "npm:^3.4.4" "@metamask/base-controller": "npm:^8.2.0" "@metamask/keyring-controller": "npm:^23.0.0" + "@metamask/messenger": "npm:^0.1.0" "@metamask/utils": "npm:^11.4.2" "@ts-bridge/cli": "npm:^0.6.1" "@types/jest": "npm:^27.4.1" From 2b596b6ad2938f05b7a8ffca2309692169cfa5aa Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Thu, 4 Sep 2025 12:42:02 +0200 Subject: [PATCH 2/4] update changelog --- packages/delegation-controller/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/delegation-controller/CHANGELOG.md b/packages/delegation-controller/CHANGELOG.md index 08f81ddb390..4057e3c7ea6 100644 --- a/packages/delegation-controller/CHANGELOG.md +++ b/packages/delegation-controller/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed ++- **BREAKING:** Use new `Messenger` from `@metamask/messenger` ([#6459](https://github.com/MetaMask/core/pull/6459)) ++ - Previously, `DelegationController` accepted a `RestrictedMessenger` instance from `@metamask/base-controller`. - Bump `@metamask/base-controller` from `^8.1.0` to `^8.2.0` ([#6355](https://github.com/MetaMask/core/pull/6355)) ## [0.7.0] From db25b2c7c3573555c6b671f211d7d973e2a6e2d1 Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Tue, 23 Sep 2025 15:33:56 +0200 Subject: [PATCH 3/4] update tsconfig files and README --- README.md | 2 ++ packages/delegation-controller/tsconfig.build.json | 3 ++- packages/delegation-controller/tsconfig.json | 3 ++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4eab983a1dc..836d609b2ea 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,7 @@ linkStyle default opacity:0.5 composable_controller --> base_controller; composable_controller --> json_rpc_engine; delegation_controller --> base_controller; + delegation_controller --> messenger; delegation_controller --> accounts_controller; delegation_controller --> keyring_controller; earn_controller --> base_controller; @@ -292,6 +293,7 @@ linkStyle default opacity:0.5 signature_controller --> controller_utils; signature_controller --> accounts_controller; signature_controller --> approval_controller; + signature_controller --> gator_permissions_controller; signature_controller --> keyring_controller; signature_controller --> logging_controller; signature_controller --> network_controller; diff --git a/packages/delegation-controller/tsconfig.build.json b/packages/delegation-controller/tsconfig.build.json index 573b24248e1..6f7018d977e 100644 --- a/packages/delegation-controller/tsconfig.build.json +++ b/packages/delegation-controller/tsconfig.build.json @@ -8,7 +8,8 @@ "references": [ { "path": "../base-controller/tsconfig.build.json" }, { "path": "../keyring-controller/tsconfig.build.json" }, - { "path": "../accounts-controller/tsconfig.build.json" } + { "path": "../accounts-controller/tsconfig.build.json" }, + { "path": "../messenger/tsconfig.build.json" } ], "include": ["../../types", "./src"] } diff --git a/packages/delegation-controller/tsconfig.json b/packages/delegation-controller/tsconfig.json index e766ef509b6..2808844b345 100644 --- a/packages/delegation-controller/tsconfig.json +++ b/packages/delegation-controller/tsconfig.json @@ -6,7 +6,8 @@ "references": [ { "path": "../base-controller" }, { "path": "../keyring-controller" }, - { "path": "../accounts-controller" } + { "path": "../accounts-controller" }, + { "path": "../messenger" } ], "include": ["../../types", "./src"] } From c0369df17cf7bbdf3570c3b86983a595dd2d2da2 Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Tue, 23 Sep 2025 15:34:56 +0200 Subject: [PATCH 4/4] rename `anonymous` to `includeInDebugSnapshot` --- packages/delegation-controller/src/DelegationController.test.ts | 2 +- packages/delegation-controller/src/DelegationController.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/delegation-controller/src/DelegationController.test.ts b/packages/delegation-controller/src/DelegationController.test.ts index 743ce196bbb..6c701639968 100644 --- a/packages/delegation-controller/src/DelegationController.test.ts +++ b/packages/delegation-controller/src/DelegationController.test.ts @@ -700,7 +700,7 @@ describe(`${controllerName}`, () => { deriveStateFromMetadata( controller.state, controller.metadata, - 'anonymous', + 'includeInDebugSnapshot', ), ).toMatchInlineSnapshot(`Object {}`); }); diff --git a/packages/delegation-controller/src/DelegationController.ts b/packages/delegation-controller/src/DelegationController.ts index 001ff3613d8..95a149ad3c7 100644 --- a/packages/delegation-controller/src/DelegationController.ts +++ b/packages/delegation-controller/src/DelegationController.ts @@ -23,7 +23,7 @@ const delegationControllerMetadata = { delegations: { includeInStateLogs: false, persist: true, - anonymous: false, + includeInDebugSnapshot: false, usedInUi: false, }, } satisfies StateMetadata;