Skip to content

Commit 6de2b6d

Browse files
authored
refactor: migrate AddressBookController to @metamask/messenger (#6383)
## 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 the `AddressBookController` class to the new `@metamask/messenger` comm system, 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
1 parent 5cca126 commit 6de2b6d

File tree

8 files changed

+62
-35
lines changed

8 files changed

+62
-35
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ linkStyle default opacity:0.5
160160
accounts_controller --> network_controller;
161161
address_book_controller --> base_controller;
162162
address_book_controller --> controller_utils;
163+
address_book_controller --> messenger;
163164
announcement_controller --> base_controller;
164165
app_metadata_controller --> base_controller;
165166
approval_controller --> base_controller;

packages/address-book-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` ([#6383](https://github.com/MetaMask/core/pull/6383))
13+
- Previously, `AddressBookController` accepted a `RestrictedMessenger` instance from `@metamask/base-controller`.
14+
- **BREAKING:** Metadata property `anonymous` renamed to `includeInDebugSnapshot` ([#6383](https://github.com/MetaMask/core/pull/6383))
15+
1016
## [6.2.1]
1117

1218
### Changed

packages/address-book-controller/package.json

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

packages/address-book-controller/src/AddressBookController.test.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,69 @@
1-
import { Messenger, deriveStateFromMetadata } from '@metamask/base-controller';
1+
import { deriveStateFromMetadata } from '@metamask/base-controller/next';
22
import { toHex } from '@metamask/controller-utils';
3+
import {
4+
Messenger,
5+
MOCK_ANY_NAMESPACE,
6+
type MessengerActions,
7+
type MessengerEvents,
8+
type MockAnyNamespace,
9+
} from '@metamask/messenger';
310
import type { Hex } from '@metamask/utils';
411

512
import type {
6-
AddressBookControllerActions,
7-
AddressBookControllerEvents,
8-
AddressBookControllerContactUpdatedEvent,
913
AddressBookControllerContactDeletedEvent,
14+
AddressBookControllerMessenger,
1015
} from './AddressBookController';
1116
import {
1217
AddressBookController,
1318
AddressType,
1419
controllerName,
1520
} from './AddressBookController';
1621

22+
type AllActions = MessengerActions<AddressBookControllerMessenger>;
23+
24+
type AllEvents = MessengerEvents<AddressBookControllerMessenger>;
25+
26+
type RootMessenger = Messenger<MockAnyNamespace, AllActions, AllEvents>;
27+
28+
/**
29+
* Creates a new root messenger instance for testing.
30+
*
31+
* @returns A new Messenger instance.
32+
*/
33+
function getRootMessenger(): RootMessenger {
34+
return new Messenger({ namespace: MOCK_ANY_NAMESPACE });
35+
}
36+
1737
/**
1838
* Helper function to create test fixtures
1939
*
2040
* @returns Test fixtures including messenger, controller, and event listeners
2141
*/
2242
function arrangeMocks() {
23-
const messenger = new Messenger<
24-
AddressBookControllerActions,
25-
AddressBookControllerEvents
26-
>();
27-
const restrictedMessenger = messenger.getRestricted({
28-
name: controllerName,
29-
allowedActions: [],
30-
allowedEvents: [],
43+
const rootMessenger = getRootMessenger();
44+
const addressBookControllerMessenger = new Messenger<
45+
typeof controllerName,
46+
AllActions,
47+
AllEvents,
48+
typeof rootMessenger
49+
>({
50+
namespace: controllerName,
51+
parent: rootMessenger,
3152
});
3253
const controller = new AddressBookController({
33-
messenger: restrictedMessenger,
54+
messenger: addressBookControllerMessenger,
3455
});
3556

3657
// Set up mock event listeners
3758
const contactUpdatedListener = jest.fn();
3859
const contactDeletedListener = jest.fn();
3960

4061
// Subscribe to events
41-
messenger.subscribe(
42-
'AddressBookController:contactUpdated' as AddressBookControllerContactUpdatedEvent['type'],
62+
rootMessenger.subscribe(
63+
'AddressBookController:contactUpdated',
4364
contactUpdatedListener,
4465
);
45-
messenger.subscribe(
66+
rootMessenger.subscribe(
4667
'AddressBookController:contactDeleted' as AddressBookControllerContactDeletedEvent['type'],
4768
contactDeletedListener,
4869
);
@@ -627,7 +648,7 @@ describe('AddressBookController', () => {
627648
deriveStateFromMetadata(
628649
controller.state,
629650
controller.metadata,
630-
'anonymous',
651+
'includeInDebugSnapshot',
631652
),
632653
).toMatchInlineSnapshot(`Object {}`);
633654
});

packages/address-book-controller/src/AddressBookController.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import type {
22
ControllerGetStateAction,
33
ControllerStateChangeEvent,
4-
RestrictedMessenger,
5-
} from '@metamask/base-controller';
6-
import { BaseController } from '@metamask/base-controller';
4+
} from '@metamask/base-controller/next';
5+
import { BaseController } from '@metamask/base-controller/next';
76
import {
87
normalizeEnsName,
98
isValidHexAddress,
109
isSafeDynamicKey,
1110
toChecksumHexAddress,
1211
toHex,
1312
} from '@metamask/controller-utils';
13+
import type { Messenger } from '@metamask/messenger';
1414
import type { Hex } from '@metamask/utils';
1515

1616
/**
@@ -150,7 +150,7 @@ const addressBookControllerMetadata = {
150150
addressBook: {
151151
includeInStateLogs: true,
152152
persist: true,
153-
anonymous: false,
153+
includeInDebugSnapshot: false,
154154
usedInUi: true,
155155
},
156156
};
@@ -170,12 +170,10 @@ export const getDefaultAddressBookControllerState =
170170
/**
171171
* The messenger of the {@link AddressBookController} for communication.
172172
*/
173-
export type AddressBookControllerMessenger = RestrictedMessenger<
173+
export type AddressBookControllerMessenger = Messenger<
174174
typeof controllerName,
175175
AddressBookControllerActions,
176-
AddressBookControllerEvents,
177-
never,
178-
never
176+
AddressBookControllerEvents
179177
>;
180178

181179
/**
@@ -275,7 +273,7 @@ export class AddressBookController extends BaseController<
275273
// These entries with chainId='*' are the wallet's own accounts (internal MetaMask accounts),
276274
// not user-created contacts. They don't need to trigger sync events.
277275
if (String(chainId) !== WALLET_ACCOUNTS_CHAIN_ID) {
278-
this.messagingSystem.publish(
276+
this.messenger.publish(
279277
'AddressBookController:contactDeleted',
280278
deletedEntry,
281279
);
@@ -335,10 +333,7 @@ export class AddressBookController extends BaseController<
335333
// These entries with chainId='*' are the wallet's own accounts (internal MetaMask accounts),
336334
// not user-created contacts. They don't need to trigger sync events.
337335
if (String(chainId) !== WALLET_ACCOUNTS_CHAIN_ID) {
338-
this.messagingSystem.publish(
339-
'AddressBookController:contactUpdated',
340-
entry,
341-
);
336+
this.messenger.publish('AddressBookController:contactUpdated', entry);
342337
}
343338

344339
return true;
@@ -348,15 +343,15 @@ export class AddressBookController extends BaseController<
348343
* Registers message handlers for the AddressBookController.
349344
*/
350345
#registerMessageHandlers() {
351-
this.messagingSystem.registerActionHandler(
346+
this.messenger.registerActionHandler(
352347
`${controllerName}:list`,
353348
this.list.bind(this),
354349
);
355-
this.messagingSystem.registerActionHandler(
350+
this.messenger.registerActionHandler(
356351
`${controllerName}:set`,
357352
this.set.bind(this),
358353
);
359-
this.messagingSystem.registerActionHandler(
354+
this.messenger.registerActionHandler(
360355
`${controllerName}:delete`,
361356
this.delete.bind(this),
362357
);

packages/address-book-controller/tsconfig.build.json

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

packages/address-book-controller/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
},
66
"references": [
77
{ "path": "../base-controller" },
8-
{ "path": "../controller-utils" }
8+
{ "path": "../controller-utils" },
9+
{ "path": "../messenger" }
910
],
1011
"include": ["../../types", "./src"]
1112
}

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,7 @@ __metadata:
26682668
"@metamask/auto-changelog": "npm:^3.4.4"
26692669
"@metamask/base-controller": "npm:^8.4.2"
26702670
"@metamask/controller-utils": "npm:^11.14.1"
2671+
"@metamask/messenger": "npm:^0.3.0"
26712672
"@metamask/utils": "npm:^11.8.1"
26722673
"@types/jest": "npm:^27.4.1"
26732674
deepmerge: "npm:^4.2.2"

0 commit comments

Comments
 (0)