Skip to content

Commit 5aaaf72

Browse files
committed
refactor: migrate {Authentication,UserStorage}Controller to @metamask/messenger
1 parent e5f7fa0 commit 5aaaf72

File tree

9 files changed

+155
-92
lines changed

9 files changed

+155
-92
lines changed

packages/profile-sync-controller/CHANGELOG.md

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

1010
### Changed
1111

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`.
1214
- Implement deferred login pattern in `SRPJwtBearerAuth` to prevent race conditions during concurrent authentication attempts ([#6353](https://github.com/MetaMask/core/pull/6353))
1315
- Add `#deferredLogin` method that ensures only one login operation executes at a time using Promise map caching
1416
- Bump `@metamask/base-controller` from `^8.1.0` to `^8.3.0` ([#6355](https://github.com/MetaMask/core/pull/6355), [#6465](https://github.com/MetaMask/core/pull/6465))

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.3.0",
104+
"@metamask/messenger": "^0.2.0",
104105
"@metamask/snaps-sdk": "^9.0.0",
105106
"@metamask/snaps-utils": "^11.0.0",
106107
"@noble/ciphers": "^1.3.0",

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

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import { Messenger } from '@metamask/base-controller';
1+
import {
2+
Messenger,
3+
MOCK_ANY_NAMESPACE,
4+
type MessengerActions,
5+
type MessengerEvents,
6+
type MockAnyNamespace,
7+
} from '@metamask/messenger';
28

39
import AuthenticationController from './AuthenticationController';
410
import type {
5-
AllowedActions,
6-
AllowedEvents,
11+
AuthenticationControllerMessenger,
712
AuthenticationControllerState,
813
} from './AuthenticationController';
914
import {
@@ -538,23 +543,52 @@ describe('AuthenticationController', () => {
538543
});
539544
});
540545

546+
type AllAuthenticationControllerActions =
547+
MessengerActions<AuthenticationControllerMessenger>;
548+
549+
type AllAuthenticationControllerEvents =
550+
MessengerEvents<AuthenticationControllerMessenger>;
551+
552+
type RootMessenger = Messenger<
553+
MockAnyNamespace,
554+
AllAuthenticationControllerActions,
555+
AllAuthenticationControllerEvents
556+
>;
557+
558+
/**
559+
* Constructs the root messenger.
560+
*
561+
* @returns A root messenger.
562+
*/
563+
function getRootMessenger(): RootMessenger {
564+
return new Messenger({ namespace: MOCK_ANY_NAMESPACE });
565+
}
566+
567+
const controllerName = 'AuthenticationController';
568+
541569
/**
542570
* Jest Test Utility - create Auth Messenger
543571
*
544572
* @returns Auth Messenger
545573
*/
546574
function createAuthenticationMessenger() {
547-
const baseMessenger = new Messenger<AllowedActions, AllowedEvents>();
548-
const messenger = baseMessenger.getRestricted({
549-
name: 'AuthenticationController',
550-
allowedActions: [
551-
'KeyringController:getState',
552-
'SnapController:handleRequest',
553-
],
554-
allowedEvents: ['KeyringController:lock', 'KeyringController:unlock'],
575+
const rootMessenger = getRootMessenger();
576+
const messenger = new Messenger<
577+
typeof controllerName,
578+
AllAuthenticationControllerActions,
579+
AllAuthenticationControllerEvents,
580+
RootMessenger
581+
>({
582+
namespace: controllerName,
583+
parent: rootMessenger,
584+
});
585+
rootMessenger.delegate({
586+
messenger,
587+
actions: ['KeyringController:getState', 'SnapController:handleRequest'],
588+
events: ['KeyringController:lock', 'KeyringController:unlock'],
555589
});
556590

557-
return { messenger, baseMessenger };
591+
return { messenger, baseMessenger: rootMessenger };
558592
}
559593

560594
/**
@@ -583,6 +617,12 @@ function createMockAuthenticationMessenger() {
583617
mockCall.mockImplementation((...args) => {
584618
const [actionType, params] = args;
585619
if (actionType === 'SnapController:handleRequest') {
620+
if (typeof params === 'string') {
621+
throw new Error(
622+
`MOCK_FAIL - unsupported SnapController:handleRequest call: ${params}`,
623+
);
624+
}
625+
586626
if (params?.request.method === 'getPublicKey') {
587627
return mockSnapGetPublicKey();
588628
}

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

Lines changed: 23 additions & 31 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

1515
import {
@@ -98,21 +98,15 @@ export type AuthenticationControllerStateChangeEvent =
9898
export type Events = AuthenticationControllerStateChangeEvent;
9999

100100
// Allowed Actions
101-
export type AllowedActions =
102-
| HandleSnapRequest
103-
| KeyringControllerGetStateAction;
101+
type AllowedActions = HandleSnapRequest | KeyringControllerGetStateAction;
104102

105-
export type AllowedEvents =
106-
| KeyringControllerLockEvent
107-
| KeyringControllerUnlockEvent;
103+
type AllowedEvents = KeyringControllerLockEvent | KeyringControllerUnlockEvent;
108104

109105
// Messenger
110-
export type AuthenticationControllerMessenger = RestrictedMessenger<
106+
export type AuthenticationControllerMessenger = Messenger<
111107
typeof controllerName,
112108
Actions | AllowedActions,
113-
Events | AllowedEvents,
114-
AllowedActions['type'],
115-
AllowedEvents['type']
109+
Events | AllowedEvents
116110
>;
117111

118112
/**
@@ -136,16 +130,14 @@ export default class AuthenticationController extends BaseController<
136130

137131
readonly #keyringController = {
138132
setupLockedStateSubscriptions: () => {
139-
const { isUnlocked } = this.messagingSystem.call(
140-
'KeyringController:getState',
141-
);
133+
const { isUnlocked } = this.messenger.call('KeyringController:getState');
142134
this.#isUnlocked = isUnlocked;
143135

144-
this.messagingSystem.subscribe('KeyringController:unlock', () => {
136+
this.messenger.subscribe('KeyringController:unlock', () => {
145137
this.#isUnlocked = true;
146138
});
147139

148-
this.messagingSystem.subscribe('KeyringController:lock', () => {
140+
this.messenger.subscribe('KeyringController:lock', () => {
149141
this.#isUnlocked = false;
150142
});
151143
},
@@ -212,32 +204,32 @@ export default class AuthenticationController extends BaseController<
212204
* actions.
213205
*/
214206
#registerMessageHandlers(): void {
215-
this.messagingSystem.registerActionHandler(
207+
this.messenger.registerActionHandler(
216208
'AuthenticationController:getBearerToken',
217209
this.getBearerToken.bind(this),
218210
);
219211

220-
this.messagingSystem.registerActionHandler(
212+
this.messenger.registerActionHandler(
221213
'AuthenticationController:getSessionProfile',
222214
this.getSessionProfile.bind(this),
223215
);
224216

225-
this.messagingSystem.registerActionHandler(
217+
this.messenger.registerActionHandler(
226218
'AuthenticationController:isSignedIn',
227219
this.isSignedIn.bind(this),
228220
);
229221

230-
this.messagingSystem.registerActionHandler(
222+
this.messenger.registerActionHandler(
231223
'AuthenticationController:performSignIn',
232224
this.performSignIn.bind(this),
233225
);
234226

235-
this.messagingSystem.registerActionHandler(
227+
this.messenger.registerActionHandler(
236228
'AuthenticationController:performSignOut',
237229
this.performSignOut.bind(this),
238230
);
239231

240-
this.messagingSystem.registerActionHandler(
232+
this.messenger.registerActionHandler(
241233
'AuthenticationController:getUserProfileLineage',
242234
this.getUserProfileLineage.bind(this),
243235
);
@@ -361,7 +353,7 @@ export default class AuthenticationController extends BaseController<
361353
async #snapGetPublicKey(entropySourceId?: string): Promise<string> {
362354
this.#assertIsUnlocked('#snapGetPublicKey');
363355

364-
const result = (await this.messagingSystem.call(
356+
const result = (await this.messenger.call(
365357
'SnapController:handleRequest',
366358
createSnapPublicKeyRequest(entropySourceId),
367359
)) as string;
@@ -377,7 +369,7 @@ export default class AuthenticationController extends BaseController<
377369
async #snapGetAllPublicKeys(): Promise<[string, string][]> {
378370
this.#assertIsUnlocked('#snapGetAllPublicKeys');
379371

380-
const result = (await this.messagingSystem.call(
372+
const result = (await this.messenger.call(
381373
'SnapController:handleRequest',
382374
createSnapAllPublicKeysRequest(),
383375
)) as [string, string][];
@@ -407,7 +399,7 @@ export default class AuthenticationController extends BaseController<
407399

408400
this.#assertIsUnlocked('#snapSignMessage');
409401

410-
const result = (await this.messagingSystem.call(
402+
const result = (await this.messenger.call(
411403
'SnapController:handleRequest',
412404
createSnapSignMessageRequest(message, entropySourceId),
413405
)) as string;

0 commit comments

Comments
 (0)