Skip to content

Commit 7d6e178

Browse files
refactor: migrate {Authentication,UserStorage}Controller to @metamask/messenger (#6533)
## 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 `AuthenticationController`, `UserStorageController` 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 `AuthenticationController` and `UserStorageController` to `@metamask/messenger`, renames state metadata `anonymous` -> `includeInDebugSnapshot`, and updates tests, configs, and deps. > > - **Profile Sync Controller**: > - Migrate `AuthenticationController` and `UserStorageController` from `@metamask/base-controller` messenger to `@metamask/messenger` (`messagingSystem` -> `messenger`; new handler/subscribe/call usage; updated types). > - Update imports to `@metamask/base-controller/next`. > - Rename state metadata key `anonymous` to `includeInDebugSnapshot` and adjust tests accordingly. > - Revise tests/mocks to construct root/child messengers and delegate permissions; adapt mock handlers (e.g., `SnapController:handleRequest`). > - **Dependencies/Config**: > - Add `@metamask/messenger` dependency and TS project references; update `yarn.lock`. > - Update README dependency graph to include `@metamask/messenger` and new edges. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit b09e552. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Michele Esposito <michele@esposito.codes>
1 parent caccf12 commit 7d6e178

File tree

11 files changed

+168
-99
lines changed

11 files changed

+168
-99
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ linkStyle default opacity:0.5
320320
preferences_controller --> messenger;
321321
preferences_controller --> keyring_controller;
322322
profile_sync_controller --> base_controller;
323+
profile_sync_controller --> messenger;
323324
profile_sync_controller --> address_book_controller;
324325
profile_sync_controller --> keyring_controller;
325326
rate_limit_controller --> base_controller;

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

1218
### Changed

packages/profile-sync-controller/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
},
102102
"dependencies": {
103103
"@metamask/base-controller": "^8.4.2",
104+
"@metamask/messenger": "^0.3.0",
104105
"@metamask/snaps-sdk": "^9.0.0",
105106
"@metamask/snaps-utils": "^11.0.0",
106107
"@metamask/utils": "^11.8.1",

packages/profile-sync-controller/src/controllers/authentication/AuthenticationController.test.ts

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { Messenger, deriveStateFromMetadata } from '@metamask/base-controller';
1+
import { deriveStateFromMetadata } from '@metamask/base-controller/next';
2+
import {
3+
Messenger,
4+
MOCK_ANY_NAMESPACE,
5+
type MessengerActions,
6+
type MessengerEvents,
7+
type MockAnyNamespace,
8+
} from '@metamask/messenger';
29

310
import AuthenticationController from './AuthenticationController';
411
import type {
5-
AllowedActions,
6-
AllowedEvents,
12+
AuthenticationControllerMessenger,
713
AuthenticationControllerState,
814
} from './AuthenticationController';
915
import {
@@ -560,7 +566,7 @@ describe('metadata', () => {
560566
deriveStateFromMetadata(
561567
controller.state,
562568
controller.metadata,
563-
'anonymous',
569+
'includeInDebugSnapshot',
564570
),
565571
).toMatchInlineSnapshot(`
566572
Object {
@@ -726,23 +732,52 @@ describe('metadata', () => {
726732
});
727733
});
728734

735+
type AllAuthenticationControllerActions =
736+
MessengerActions<AuthenticationControllerMessenger>;
737+
738+
type AllAuthenticationControllerEvents =
739+
MessengerEvents<AuthenticationControllerMessenger>;
740+
741+
type RootMessenger = Messenger<
742+
MockAnyNamespace,
743+
AllAuthenticationControllerActions,
744+
AllAuthenticationControllerEvents
745+
>;
746+
747+
/**
748+
* Constructs the root messenger.
749+
*
750+
* @returns A root messenger.
751+
*/
752+
function getRootMessenger(): RootMessenger {
753+
return new Messenger({ namespace: MOCK_ANY_NAMESPACE });
754+
}
755+
756+
const controllerName = 'AuthenticationController';
757+
729758
/**
730759
* Jest Test Utility - create Auth Messenger
731760
*
732761
* @returns Auth Messenger
733762
*/
734763
function createAuthenticationMessenger() {
735-
const baseMessenger = new Messenger<AllowedActions, AllowedEvents>();
736-
const messenger = baseMessenger.getRestricted({
737-
name: 'AuthenticationController',
738-
allowedActions: [
739-
'KeyringController:getState',
740-
'SnapController:handleRequest',
741-
],
742-
allowedEvents: ['KeyringController:lock', 'KeyringController:unlock'],
764+
const rootMessenger = getRootMessenger();
765+
const messenger = new Messenger<
766+
typeof controllerName,
767+
AllAuthenticationControllerActions,
768+
AllAuthenticationControllerEvents,
769+
RootMessenger
770+
>({
771+
namespace: controllerName,
772+
parent: rootMessenger,
773+
});
774+
rootMessenger.delegate({
775+
messenger,
776+
actions: ['KeyringController:getState', 'SnapController:handleRequest'],
777+
events: ['KeyringController:lock', 'KeyringController:unlock'],
743778
});
744779

745-
return { messenger, baseMessenger };
780+
return { messenger, baseMessenger: rootMessenger };
746781
}
747782

748783
/**
@@ -771,6 +806,12 @@ function createMockAuthenticationMessenger() {
771806
mockCall.mockImplementation((...args) => {
772807
const [actionType, params] = args;
773808
if (actionType === 'SnapController:handleRequest') {
809+
if (typeof params === 'string') {
810+
throw new Error(
811+
`MOCK_FAIL - unsupported SnapController:handleRequest call: ${params}`,
812+
);
813+
}
814+
774815
if (params?.request.method === 'getPublicKey') {
775816
return mockSnapGetPublicKey();
776817
}

packages/profile-sync-controller/src/controllers/authentication/AuthenticationController.ts

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import type {
2-
ControllerGetStateAction,
3-
ControllerStateChangeEvent,
4-
RestrictedMessenger,
5-
StateMetadata,
6-
} from '@metamask/base-controller';
7-
import { BaseController } from '@metamask/base-controller';
1+
import {
2+
BaseController,
3+
type ControllerGetStateAction,
4+
type ControllerStateChangeEvent,
5+
type StateMetadata,
6+
} from '@metamask/base-controller/next';
87
import type {
98
KeyringControllerGetStateAction,
109
KeyringControllerLockEvent,
1110
KeyringControllerUnlockEvent,
1211
} from '@metamask/keyring-controller';
12+
import type { Messenger } from '@metamask/messenger';
1313
import type { HandleSnapRequest } from '@metamask/snaps-controllers';
1414
import type { Json } from '@metamask/utils';
1515

@@ -46,7 +46,7 @@ const metadata: StateMetadata<AuthenticationControllerState> = {
4646
isSignedIn: {
4747
includeInStateLogs: true,
4848
persist: true,
49-
anonymous: true,
49+
includeInDebugSnapshot: true,
5050
usedInUi: true,
5151
},
5252
srpSessionData: {
@@ -74,7 +74,7 @@ const metadata: StateMetadata<AuthenticationControllerState> = {
7474
);
7575
},
7676
persist: true,
77-
anonymous: false,
77+
includeInDebugSnapshot: false,
7878
usedInUi: true,
7979
},
8080
};
@@ -125,21 +125,15 @@ export type AuthenticationControllerStateChangeEvent =
125125
export type Events = AuthenticationControllerStateChangeEvent;
126126

127127
// Allowed Actions
128-
export type AllowedActions =
129-
| HandleSnapRequest
130-
| KeyringControllerGetStateAction;
128+
type AllowedActions = HandleSnapRequest | KeyringControllerGetStateAction;
131129

132-
export type AllowedEvents =
133-
| KeyringControllerLockEvent
134-
| KeyringControllerUnlockEvent;
130+
type AllowedEvents = KeyringControllerLockEvent | KeyringControllerUnlockEvent;
135131

136132
// Messenger
137-
export type AuthenticationControllerMessenger = RestrictedMessenger<
133+
export type AuthenticationControllerMessenger = Messenger<
138134
typeof controllerName,
139135
Actions | AllowedActions,
140-
Events | AllowedEvents,
141-
AllowedActions['type'],
142-
AllowedEvents['type']
136+
Events | AllowedEvents
143137
>;
144138

145139
/**
@@ -163,16 +157,14 @@ export default class AuthenticationController extends BaseController<
163157

164158
readonly #keyringController = {
165159
setupLockedStateSubscriptions: () => {
166-
const { isUnlocked } = this.messagingSystem.call(
167-
'KeyringController:getState',
168-
);
160+
const { isUnlocked } = this.messenger.call('KeyringController:getState');
169161
this.#isUnlocked = isUnlocked;
170162

171-
this.messagingSystem.subscribe('KeyringController:unlock', () => {
163+
this.messenger.subscribe('KeyringController:unlock', () => {
172164
this.#isUnlocked = true;
173165
});
174166

175-
this.messagingSystem.subscribe('KeyringController:lock', () => {
167+
this.messenger.subscribe('KeyringController:lock', () => {
176168
this.#isUnlocked = false;
177169
});
178170
},
@@ -239,32 +231,32 @@ export default class AuthenticationController extends BaseController<
239231
* actions.
240232
*/
241233
#registerMessageHandlers(): void {
242-
this.messagingSystem.registerActionHandler(
234+
this.messenger.registerActionHandler(
243235
'AuthenticationController:getBearerToken',
244236
this.getBearerToken.bind(this),
245237
);
246238

247-
this.messagingSystem.registerActionHandler(
239+
this.messenger.registerActionHandler(
248240
'AuthenticationController:getSessionProfile',
249241
this.getSessionProfile.bind(this),
250242
);
251243

252-
this.messagingSystem.registerActionHandler(
244+
this.messenger.registerActionHandler(
253245
'AuthenticationController:isSignedIn',
254246
this.isSignedIn.bind(this),
255247
);
256248

257-
this.messagingSystem.registerActionHandler(
249+
this.messenger.registerActionHandler(
258250
'AuthenticationController:performSignIn',
259251
this.performSignIn.bind(this),
260252
);
261253

262-
this.messagingSystem.registerActionHandler(
254+
this.messenger.registerActionHandler(
263255
'AuthenticationController:performSignOut',
264256
this.performSignOut.bind(this),
265257
);
266258

267-
this.messagingSystem.registerActionHandler(
259+
this.messenger.registerActionHandler(
268260
'AuthenticationController:getUserProfileLineage',
269261
this.getUserProfileLineage.bind(this),
270262
);
@@ -388,7 +380,7 @@ export default class AuthenticationController extends BaseController<
388380
async #snapGetPublicKey(entropySourceId?: string): Promise<string> {
389381
this.#assertIsUnlocked('#snapGetPublicKey');
390382

391-
const result = (await this.messagingSystem.call(
383+
const result = (await this.messenger.call(
392384
'SnapController:handleRequest',
393385
createSnapPublicKeyRequest(entropySourceId),
394386
)) as string;
@@ -404,7 +396,7 @@ export default class AuthenticationController extends BaseController<
404396
async #snapGetAllPublicKeys(): Promise<[string, string][]> {
405397
this.#assertIsUnlocked('#snapGetAllPublicKeys');
406398

407-
const result = (await this.messagingSystem.call(
399+
const result = (await this.messenger.call(
408400
'SnapController:handleRequest',
409401
createSnapAllPublicKeysRequest(),
410402
)) as [string, string][];
@@ -434,7 +426,7 @@ export default class AuthenticationController extends BaseController<
434426

435427
this.#assertIsUnlocked('#snapSignMessage');
436428

437-
const result = (await this.messagingSystem.call(
429+
const result = (await this.messenger.call(
438430
'SnapController:handleRequest',
439431
createSnapSignMessageRequest(message, entropySourceId),
440432
)) as string;

packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deriveStateFromMetadata } from '@metamask/base-controller';
1+
import { deriveStateFromMetadata } from '@metamask/base-controller/next';
22
import type nock from 'nock';
33

44
import { mockUserStorageMessenger } from './__fixtures__/mockMessenger';
@@ -766,7 +766,7 @@ describe('metadata', () => {
766766
deriveStateFromMetadata(
767767
controller.state,
768768
controller.metadata,
769-
'anonymous',
769+
'includeInDebugSnapshot',
770770
),
771771
).toMatchInlineSnapshot(`
772772
Object {

0 commit comments

Comments
 (0)