Skip to content

Commit 4fbdc63

Browse files
authored
refactor: migrate DelegationController to @metamask/messenger (#6459)
## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> This PR migrates `DelegationController` to the new `@metamask/messenger` message bus, as opposed to the one exported from `@metamask/base-controller`. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> * Related to #5626 ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Migrates `DelegationController` to `@metamask/messenger`, updates controller/test imports to `@metamask/base-controller/next`, and renames metadata `anonymous` to `includeInDebugSnapshot`. > > - **DelegationController**: > - Switch to `@metamask/messenger` (`messenger.call`, controller-specific messenger) and update types to `Messenger`. > - Import `BaseController`/`StateMetadata` from `@metamask/base-controller/next`. > - Rename metadata key `anonymous` -> `includeInDebugSnapshot`. > - Replace internal calls from `messagingSystem.call` to `messenger.call`. > - **Tests**: > - Rework messenger mocks to use `@metamask/messenger` with namespacing/delegation. > - Update `deriveStateFromMetadata` import and metadata key usage. > - **Package/Config**: > - Add dependency `@metamask/messenger` and TS project references. > - Update changelog with BREAKING changes (new messenger, metadata key rename). > - **Docs**: > - Update README dependency graph (add `delegation_controller --> messenger`, add `eip_7702_internal_rpc_middleware --> controller_utils`). > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit a3979fb. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent cfdd5e7 commit 4fbdc63

File tree

9 files changed

+60
-30
lines changed

9 files changed

+60
-30
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ linkStyle default opacity:0.5
215215
core_backend --> accounts_controller;
216216
core_backend --> keyring_controller;
217217
delegation_controller --> base_controller;
218+
delegation_controller --> messenger;
218219
delegation_controller --> accounts_controller;
219220
delegation_controller --> keyring_controller;
220221
earn_controller --> base_controller;

packages/delegation-controller/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- **BREAKING:** Use new `Messenger` from `@metamask/messenger` ([#6459](https://github.com/MetaMask/core/pull/6459))
13+
- Previously, `DelegationController` accepted a `RestrictedMessenger` instance from `@metamask/base-controller`.
14+
- **BREAKING:** Metadata property `anonymous` renamed to `includeInDebugSnapshot` ([#6459](https://github.com/MetaMask/core/pull/6459))
15+
1016
## [0.8.1]
1117

1218
### Changed

packages/delegation-controller/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"dependencies": {
5050
"@metamask/base-controller": "^8.4.2",
51+
"@metamask/messenger": "^0.3.0",
5152
"@metamask/utils": "^11.8.1"
5253
},
5354
"devDependencies": {

packages/delegation-controller/src/DelegationController.test.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
1-
import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';
2-
import { deriveStateFromMetadata, Messenger } from '@metamask/base-controller';
1+
import { deriveStateFromMetadata } from '@metamask/base-controller/next';
2+
import { SignTypedDataVersion } from '@metamask/keyring-controller';
33
import {
4-
type KeyringControllerSignTypedMessageAction,
5-
SignTypedDataVersion,
6-
} from '@metamask/keyring-controller';
4+
Messenger,
5+
MOCK_ANY_NAMESPACE,
6+
type MessengerActions,
7+
type MessengerEvents,
8+
type MockAnyNamespace,
9+
} from '@metamask/messenger';
710
import { hexToNumber } from '@metamask/utils';
811

912
import { ROOT_AUTHORITY } from './constants';
1013
import { controllerName, DelegationController } from './DelegationController';
1114
import type {
1215
Address,
1316
Delegation,
14-
DelegationControllerEvents,
17+
DelegationControllerMessenger,
1518
DelegationControllerState,
1619
DelegationEntry,
1720
DeleGatorEnvironment,
1821
Hex,
1922
} from './types';
2023
import { toDelegationStruct } from './utils';
2124

25+
type AllDelegationControllerActions =
26+
MessengerActions<DelegationControllerMessenger>;
27+
28+
type AllDelegationControllerEvents =
29+
MessengerEvents<DelegationControllerMessenger>;
30+
31+
type RootMessenger = Messenger<
32+
MockAnyNamespace,
33+
AllDelegationControllerActions,
34+
AllDelegationControllerEvents
35+
>;
36+
2237
const FROM_MOCK = '0x2234567890123456789012345678901234567890' as Address;
2338
const SIGNATURE_HASH_MOCK = '0x123ABC';
2439

@@ -60,11 +75,9 @@ class TestDelegationController extends DelegationController {
6075
* @returns The mock messenger instance plus individual mock functions for each action.
6176
*/
6277
function createMessengerMock() {
63-
const messenger = new Messenger<
64-
| KeyringControllerSignTypedMessageAction
65-
| AccountsControllerGetSelectedAccountAction,
66-
DelegationControllerEvents
67-
>();
78+
const messenger: RootMessenger = new Messenger({
79+
namespace: MOCK_ANY_NAMESPACE,
80+
});
6881

6982
const accountsControllerGetSelectedAccountMock = jest.fn();
7083
const keyringControllerSignTypedMessageMock = jest.fn();
@@ -84,19 +97,27 @@ function createMessengerMock() {
8497
keyringControllerSignTypedMessageMock,
8598
);
8699

87-
const restrictedMessenger = messenger.getRestricted({
88-
name: `${controllerName}`,
89-
allowedActions: [
100+
const delegationControllerMessenger = new Messenger<
101+
'DelegationController',
102+
AllDelegationControllerActions,
103+
AllDelegationControllerEvents,
104+
RootMessenger
105+
>({
106+
namespace: controllerName,
107+
parent: messenger,
108+
});
109+
messenger.delegate({
110+
messenger: delegationControllerMessenger,
111+
actions: [
90112
'AccountsController:getSelectedAccount',
91113
'KeyringController:signTypedMessage',
92114
],
93-
allowedEvents: [],
94115
});
95116

96117
return {
97118
accountsControllerGetSelectedAccountMock,
98119
keyringControllerSignTypedMessageMock,
99-
messenger: restrictedMessenger,
120+
messenger: delegationControllerMessenger,
100121
};
101122
}
102123

@@ -679,7 +700,7 @@ describe(`${controllerName}`, () => {
679700
deriveStateFromMetadata(
680701
controller.state,
681702
controller.metadata,
682-
'anonymous',
703+
'includeInDebugSnapshot',
683704
),
684705
).toMatchInlineSnapshot(`Object {}`);
685706
});

packages/delegation-controller/src/DelegationController.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { StateMetadata } from '@metamask/base-controller';
2-
import { BaseController } from '@metamask/base-controller';
1+
import type { StateMetadata } from '@metamask/base-controller/next';
2+
import { BaseController } from '@metamask/base-controller/next';
33
import { SignTypedDataVersion } from '@metamask/keyring-controller';
44
import { hexToNumber } from '@metamask/utils';
55

@@ -23,7 +23,7 @@ const delegationControllerMetadata = {
2323
delegations: {
2424
includeInStateLogs: false,
2525
persist: true,
26-
anonymous: false,
26+
includeInDebugSnapshot: false,
2727
usedInUi: false,
2828
},
2929
} satisfies StateMetadata<DelegationControllerState>;
@@ -115,7 +115,7 @@ export class DelegationController extends BaseController<
115115

116116
// TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.
117117
// Waiting on confirmations team to implement this.
118-
const signature: string = await this.messagingSystem.call(
118+
const signature: string = await this.messenger.call(
119119
'KeyringController:signTypedMessage',
120120
data,
121121
SignTypedDataVersion.V4,
@@ -154,7 +154,7 @@ export class DelegationController extends BaseController<
154154
* @returns A list of delegation entries that match the filter.
155155
*/
156156
list(filter?: DelegationFilter) {
157-
const account = this.messagingSystem.call(
157+
const account = this.messenger.call(
158158
'AccountsController:getSelectedAccount',
159159
);
160160
const requester = account.address as Address;

packages/delegation-controller/src/types.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accou
22
import type {
33
ControllerGetStateAction,
44
ControllerStateChangeEvent,
5-
RestrictedMessenger,
6-
} from '@metamask/base-controller';
5+
} from '@metamask/base-controller/next';
76
import type { KeyringControllerSignTypedMessageAction } from '@metamask/keyring-controller';
7+
import type { Messenger } from '@metamask/messenger';
88

99
import type {
1010
controllerName,
@@ -149,10 +149,8 @@ type AllowedActions =
149149

150150
type AllowedEvents = never;
151151

152-
export type DelegationControllerMessenger = RestrictedMessenger<
152+
export type DelegationControllerMessenger = Messenger<
153153
typeof controllerName,
154154
DelegationControllerActions | AllowedActions,
155-
DelegationControllerEvents | AllowedEvents,
156-
AllowedActions['type'],
157-
AllowedEvents['type']
155+
DelegationControllerEvents | AllowedEvents
158156
>;

packages/delegation-controller/tsconfig.build.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"references": [
99
{ "path": "../base-controller/tsconfig.build.json" },
1010
{ "path": "../keyring-controller/tsconfig.build.json" },
11-
{ "path": "../accounts-controller/tsconfig.build.json" }
11+
{ "path": "../accounts-controller/tsconfig.build.json" },
12+
{ "path": "../messenger/tsconfig.build.json" }
1213
],
1314
"include": ["../../types", "./src"]
1415
}

packages/delegation-controller/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"references": [
77
{ "path": "../base-controller" },
88
{ "path": "../keyring-controller" },
9-
{ "path": "../accounts-controller" }
9+
{ "path": "../accounts-controller" },
10+
{ "path": "../messenger" }
1011
],
1112
"include": ["../../types", "./src"]
1213
}

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3211,6 +3211,7 @@ __metadata:
32113211
"@metamask/auto-changelog": "npm:^3.4.4"
32123212
"@metamask/base-controller": "npm:^8.4.2"
32133213
"@metamask/keyring-controller": "npm:^23.2.0"
3214+
"@metamask/messenger": "npm:^0.3.0"
32143215
"@metamask/utils": "npm:^11.8.1"
32153216
"@ts-bridge/cli": "npm:^0.6.1"
32163217
"@types/jest": "npm:^27.4.1"

0 commit comments

Comments
 (0)