Skip to content

Commit 2ea0719

Browse files
committed
refactor: migrate MultichainAccountService to @metamask/messenger
1 parent b469c1f commit 2ea0719

File tree

8 files changed

+68
-78
lines changed

8 files changed

+68
-78
lines changed

packages/multichain-account-service/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Add `multichainAccountGroupCreated` event emitted from wallet level when new groups are created.
1414
- Add `multichainAccountGroupUpdated` event emitted from wallet level when groups are synchronized.
1515

16+
### Changed
17+
18+
- **BREAKING:** Use new `Messenger` from `@metamask/messenger` ([#6544](https://github.com/MetaMask/core/pull/6544))
19+
- Previously, `MultichainAccountService` accepted a `RestrictedMessenger` instance from `@metamask/base-controller`.
20+
1621
## [0.7.0]
1722

1823
### Added

packages/multichain-account-service/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@metamask/keyring-internal-api": "^8.1.0",
5454
"@metamask/keyring-snap-client": "^7.0.0",
5555
"@metamask/keyring-utils": "^3.1.0",
56+
"@metamask/messenger": "^0.2.0",
5657
"@metamask/snaps-sdk": "^9.0.0",
5758
"@metamask/snaps-utils": "^11.0.0",
5859
"@metamask/superstruct": "^3.1.0",

packages/multichain-account-service/src/MultichainAccountService.test.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-disable jsdoc/require-jsdoc */
22

3-
import type { Messenger } from '@metamask/base-controller';
43
import type { KeyringAccount } from '@metamask/keyring-api';
54
import { EthAccountType, SolAccountType } from '@metamask/keyring-api';
65
import { KeyringTypes, type KeyringObject } from '@metamask/keyring-controller';
@@ -27,14 +26,9 @@ import {
2726
makeMockAccountProvider,
2827
mockAsInternalAccount,
2928
setupAccountProvider,
29+
type RootMessenger,
3030
} from './tests';
31-
import type {
32-
AllowedActions,
33-
AllowedEvents,
34-
MultichainAccountServiceActions,
35-
MultichainAccountServiceEvents,
36-
MultichainAccountServiceMessenger,
37-
} from './types';
31+
import type { MultichainAccountServiceMessenger } from './types';
3832

3933
// Mock providers.
4034
jest.mock('./providers/EvmAccountProvider', () => {
@@ -84,18 +78,12 @@ function setup({
8478
keyrings = [MOCK_HD_KEYRING_1, MOCK_HD_KEYRING_2],
8579
accounts,
8680
}: {
87-
messenger?: Messenger<
88-
MultichainAccountServiceActions | AllowedActions,
89-
MultichainAccountServiceEvents | AllowedEvents
90-
>;
81+
messenger?: RootMessenger;
9182
keyrings?: KeyringObject[];
9283
accounts?: KeyringAccount[];
9384
} = {}): {
9485
service: MultichainAccountService;
95-
messenger: Messenger<
96-
MultichainAccountServiceActions | AllowedActions,
97-
MultichainAccountServiceEvents | AllowedEvents
98-
>;
86+
messenger: RootMessenger;
9987
mocks: Mocks;
10088
} {
10189
const mocks: Mocks = {

packages/multichain-account-service/src/providers/EvmAccountProvider.test.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Messenger } from '@metamask/base-controller';
21
import { type KeyringMetadata } from '@metamask/keyring-controller';
32
import type {
43
EthKeyring,
@@ -17,13 +16,8 @@ import {
1716
MOCK_HD_ACCOUNT_2,
1817
MOCK_HD_KEYRING_1,
1918
MockAccountBuilder,
19+
type RootMessenger,
2020
} from '../tests';
21-
import type {
22-
AllowedActions,
23-
AllowedEvents,
24-
MultichainAccountServiceActions,
25-
MultichainAccountServiceEvents,
26-
} from '../types';
2721

2822
class MockEthKeyring implements EthKeyring {
2923
readonly type = 'MockEthKeyring';
@@ -90,17 +84,11 @@ function setup({
9084
messenger = getRootMessenger(),
9185
accounts = [],
9286
}: {
93-
messenger?: Messenger<
94-
MultichainAccountServiceActions | AllowedActions,
95-
MultichainAccountServiceEvents | AllowedEvents
96-
>;
87+
messenger?: RootMessenger;
9788
accounts?: InternalAccount[];
9889
} = {}): {
9990
provider: EvmAccountProvider;
100-
messenger: Messenger<
101-
MultichainAccountServiceActions | AllowedActions,
102-
MultichainAccountServiceEvents | AllowedEvents
103-
>;
91+
messenger: RootMessenger;
10492
keyring: MockEthKeyring;
10593
mocks: {
10694
getAccountByAddress: jest.Mock;

packages/multichain-account-service/src/providers/SolAccountProvider.test.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { isBip44Account } from '@metamask/account-api';
2-
import type { Messenger } from '@metamask/base-controller';
32
import type { SnapKeyring } from '@metamask/eth-snap-keyring';
43
import type { KeyringMetadata } from '@metamask/keyring-controller';
54
import type {
@@ -17,13 +16,8 @@ import {
1716
MOCK_SOL_ACCOUNT_1,
1817
MOCK_SOL_DISCOVERED_ACCOUNT_1,
1918
MockAccountBuilder,
19+
type RootMessenger,
2020
} from '../tests';
21-
import type {
22-
AllowedActions,
23-
AllowedEvents,
24-
MultichainAccountServiceActions,
25-
MultichainAccountServiceEvents,
26-
} from '../types';
2721

2822
class MockSolanaKeyring {
2923
readonly type = 'MockSolanaKeyring';
@@ -94,17 +88,11 @@ function setup({
9488
messenger = getRootMessenger(),
9589
accounts = [],
9690
}: {
97-
messenger?: Messenger<
98-
MultichainAccountServiceActions | AllowedActions,
99-
MultichainAccountServiceEvents | AllowedEvents
100-
>;
91+
messenger?: RootMessenger;
10192
accounts?: InternalAccount[];
10293
} = {}): {
10394
provider: AccountProviderWrapper;
104-
messenger: Messenger<
105-
MultichainAccountServiceActions | AllowedActions,
106-
MultichainAccountServiceEvents | AllowedEvents
107-
>;
95+
messenger: RootMessenger;
10896
keyring: MockSolanaKeyring;
10997
mocks: {
11098
handleRequest: jest.Mock;
Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,57 @@
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

3-
import type {
4-
AllowedActions,
5-
AllowedEvents,
6-
MultichainAccountServiceActions,
7-
MultichainAccountServiceEvents,
8-
MultichainAccountServiceMessenger,
9-
} from '../types';
9+
import type { MultichainAccountServiceMessenger } from '../types';
10+
11+
type AllMultichainAccountServiceActions =
12+
MessengerActions<MultichainAccountServiceMessenger>;
13+
14+
type AllMultichainAccountServiceEvents =
15+
MessengerEvents<MultichainAccountServiceMessenger>;
16+
17+
export type RootMessenger = Messenger<
18+
MockAnyNamespace,
19+
AllMultichainAccountServiceActions,
20+
AllMultichainAccountServiceEvents
21+
>;
1022

1123
/**
12-
* Creates a new root messenger instance for testing.
24+
* Creates and returns a root messenger for testing
1325
*
14-
* @returns A new Messenger instance.
26+
* @returns A messenger instance
1527
*/
16-
export function getRootMessenger() {
17-
return new Messenger<
18-
MultichainAccountServiceActions | AllowedActions,
19-
MultichainAccountServiceEvents | AllowedEvents
20-
>();
28+
export function getRootMessenger(): RootMessenger {
29+
return new Messenger({
30+
namespace: MOCK_ANY_NAMESPACE,
31+
});
2132
}
2233

2334
/**
2435
* Retrieves a restricted messenger for the MultichainAccountService.
2536
*
26-
* @param messenger - The root messenger instance. Defaults to a new Messenger created by getRootMessenger().
37+
* @param rootMessenger - The root messenger instance. Defaults to a new Messenger created by getRootMessenger().
2738
* @returns The restricted messenger for the MultichainAccountService.
2839
*/
2940
export function getMultichainAccountServiceMessenger(
30-
messenger: ReturnType<typeof getRootMessenger>,
41+
rootMessenger: RootMessenger,
3142
): MultichainAccountServiceMessenger {
32-
return messenger.getRestricted({
33-
name: 'MultichainAccountService',
34-
allowedEvents: [
35-
'KeyringController:stateChange',
36-
'AccountsController:accountAdded',
37-
'AccountsController:accountRemoved',
38-
],
39-
allowedActions: [
43+
const messenger = new Messenger<
44+
'MultichainAccountService',
45+
AllMultichainAccountServiceActions,
46+
AllMultichainAccountServiceEvents,
47+
RootMessenger
48+
>({
49+
namespace: 'MultichainAccountService',
50+
parent: rootMessenger,
51+
});
52+
rootMessenger.delegate({
53+
messenger,
54+
actions: [
4055
'AccountsController:getAccount',
4156
'AccountsController:getAccountByAddress',
4257
'AccountsController:listMultichainAccounts',
@@ -46,5 +61,11 @@ export function getMultichainAccountServiceMessenger(
4661
'NetworkController:findNetworkClientIdByChainId',
4762
'NetworkController:getNetworkClientById',
4863
],
64+
events: [
65+
'KeyringController:stateChange',
66+
'AccountsController:accountAdded',
67+
'AccountsController:accountRemoved',
68+
],
4969
});
70+
return messenger;
5071
}

packages/multichain-account-service/src/types.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import type {
99
AccountsControllerGetAccountByAddressAction,
1010
AccountsControllerListMultichainAccountsAction,
1111
} from '@metamask/accounts-controller';
12-
import type { RestrictedMessenger } from '@metamask/base-controller';
1312
import type { KeyringAccount } from '@metamask/keyring-api';
1413
import type {
1514
KeyringControllerGetStateAction,
1615
KeyringControllerStateChangeEvent,
1716
KeyringControllerWithKeyringAction,
1817
} from '@metamask/keyring-controller';
18+
import type { Messenger } from '@metamask/messenger';
1919
import type {
2020
NetworkControllerFindNetworkClientIdByChainIdAction,
2121
NetworkControllerGetNetworkClientByIdAction,
@@ -115,7 +115,7 @@ export type MultichainAccountServiceEvents =
115115
* All actions registered by other modules that {@link MultichainAccountService}
116116
* calls.
117117
*/
118-
export type AllowedActions =
118+
type AllowedActions =
119119
| AccountsControllerListMultichainAccountsAction
120120
| AccountsControllerGetAccountAction
121121
| AccountsControllerGetAccountByAddressAction
@@ -129,7 +129,7 @@ export type AllowedActions =
129129
* All events published by other modules that {@link MultichainAccountService}
130130
* subscribes to.
131131
*/
132-
export type AllowedEvents =
132+
type AllowedEvents =
133133
| KeyringControllerStateChangeEvent
134134
| AccountsControllerAccountAddedEvent
135135
| AccountsControllerAccountRemovedEvent;
@@ -138,10 +138,8 @@ export type AllowedEvents =
138138
* The messenger restricted to actions and events that
139139
* {@link MultichainAccountService} needs to access.
140140
*/
141-
export type MultichainAccountServiceMessenger = RestrictedMessenger<
141+
export type MultichainAccountServiceMessenger = Messenger<
142142
'MultichainAccountService',
143143
MultichainAccountServiceActions | AllowedActions,
144-
MultichainAccountServiceEvents | AllowedEvents,
145-
AllowedActions['type'],
146-
AllowedEvents['type']
144+
MultichainAccountServiceEvents | AllowedEvents
147145
>;

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3829,6 +3829,7 @@ __metadata:
38293829
"@metamask/keyring-internal-api": "npm:^8.1.0"
38303830
"@metamask/keyring-snap-client": "npm:^7.0.0"
38313831
"@metamask/keyring-utils": "npm:^3.1.0"
3832+
"@metamask/messenger": "npm:^0.2.0"
38323833
"@metamask/providers": "npm:^22.1.0"
38333834
"@metamask/snaps-controllers": "npm:^14.0.1"
38343835
"@metamask/snaps-sdk": "npm:^9.0.0"

0 commit comments

Comments
 (0)