Skip to content

Commit 6cf774a

Browse files
Use KeyringController
1 parent 352bd40 commit 6cf774a

File tree

4 files changed

+38
-45
lines changed

4 files changed

+38
-45
lines changed

packages/snaps-controllers/src/multichain/MultichainRoutingController.test.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
SOLANA_CAIP2,
1212
MOCK_SOLANA_ACCOUNTS,
1313
MOCK_BTC_ACCOUNTS,
14-
getMockSnapKeyring,
1514
} from '../test-utils';
1615
import { MultichainRoutingController } from './MultichainRoutingController';
1716

@@ -24,18 +23,20 @@ describe('MultichainRoutingController', () => {
2423
/* eslint-disable-next-line no-new */
2524
new MultichainRoutingController({
2625
messenger,
27-
getSnapKeyring: getMockSnapKeyring(
28-
jest.fn().mockResolvedValue({
29-
txid: '53de51e2fa75c3cfa51132865f7d430138b1cd92a8f5267ec836ec565b422969',
30-
}),
31-
),
3226
});
3327

3428
rootMessenger.registerActionHandler(
3529
'AccountsController:listMultichainAccounts',
3630
() => MOCK_BTC_ACCOUNTS,
3731
);
3832

33+
rootMessenger.registerActionHandler(
34+
'KeyringController:submitNonEvmRequest',
35+
async () => ({
36+
txid: '53de51e2fa75c3cfa51132865f7d430138b1cd92a8f5267ec836ec565b422969',
37+
}),
38+
);
39+
3940
rootMessenger.registerActionHandler(
4041
'SnapController:handleRequest',
4142
async ({ handler }) => {
@@ -74,16 +75,20 @@ describe('MultichainRoutingController', () => {
7475
/* eslint-disable-next-line no-new */
7576
new MultichainRoutingController({
7677
messenger,
77-
getSnapKeyring: getMockSnapKeyring(
78-
jest.fn().mockResolvedValue({ signature: '0x' }),
79-
),
8078
});
8179

8280
rootMessenger.registerActionHandler(
8381
'AccountsController:listMultichainAccounts',
8482
() => MOCK_SOLANA_ACCOUNTS,
8583
);
8684

85+
rootMessenger.registerActionHandler(
86+
'KeyringController:submitNonEvmRequest',
87+
async () => ({
88+
signature: '0x',
89+
}),
90+
);
91+
8792
rootMessenger.registerActionHandler(
8893
'PermissionController:getPermissions',
8994
() => MOCK_SOLANA_SNAP_PERMISSIONS,
@@ -125,7 +130,6 @@ describe('MultichainRoutingController', () => {
125130
/* eslint-disable-next-line no-new */
126131
new MultichainRoutingController({
127132
messenger,
128-
getSnapKeyring: getMockSnapKeyring(),
129133
});
130134

131135
rootMessenger.registerActionHandler(
@@ -175,7 +179,6 @@ describe('MultichainRoutingController', () => {
175179
/* eslint-disable-next-line no-new */
176180
new MultichainRoutingController({
177181
messenger,
178-
getSnapKeyring: getMockSnapKeyring(),
179182
});
180183

181184
rootMessenger.registerActionHandler(

packages/snaps-controllers/src/multichain/MultichainRoutingController.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ export type AccountsControllerListMultichainAccountsAction = {
5858
handler: (chainId?: CaipChainId) => InternalAccount[];
5959
};
6060

61+
export type KeyringControllerSubmitNonEvmRequestAction = {
62+
type: `KeyringController:submitNonEvmRequest`;
63+
handler: (args: {
64+
address: string;
65+
method: string;
66+
params?: Json[] | Record<string, Json>;
67+
chainId: CaipChainId;
68+
}) => Promise<Json>;
69+
};
70+
6171
export type MultichainRoutingControllerActions =
6272
| MultichainRoutingControllerGetStateAction
6373
| MultichainRoutingControllerHandleRequestAction;
@@ -66,7 +76,8 @@ export type MultichainRoutingControllerAllowedActions =
6676
| GetAllSnaps
6777
| HandleSnapRequest
6878
| GetPermissions
69-
| AccountsControllerListMultichainAccountsAction;
79+
| AccountsControllerListMultichainAccountsAction
80+
| KeyringControllerSubmitNonEvmRequestAction;
7081

7182
export type MultichainRoutingControllerEvents =
7283
MultichainRoutingControllerStateChangeEvent;
@@ -81,19 +92,9 @@ export type MultichainRoutingControllerMessenger =
8192
MultichainRoutingControllerEvents['type']
8293
>;
8394

84-
export type SnapKeyring = {
85-
submitNonEvmRequest: (args: {
86-
address: string;
87-
method: string;
88-
params?: Json[] | Record<string, Json>;
89-
chainId: CaipChainId;
90-
}) => Promise<Json>;
91-
};
92-
9395
export type MultichainRoutingControllerArgs = {
9496
messenger: MultichainRoutingControllerMessenger;
9597
state?: MultichainRoutingControllerState;
96-
getSnapKeyring: () => Promise<SnapKeyring>;
9798
};
9899

99100
export type MultichainRoutingControllerState = EmptyObject;
@@ -110,13 +111,7 @@ export class MultichainRoutingController extends BaseController<
110111
MultichainRoutingControllerState,
111112
MultichainRoutingControllerMessenger
112113
> {
113-
#getSnapKeyring: () => Promise<SnapKeyring>;
114-
115-
constructor({
116-
messenger,
117-
state,
118-
getSnapKeyring,
119-
}: MultichainRoutingControllerArgs) {
114+
constructor({ messenger, state }: MultichainRoutingControllerArgs) {
120115
super({
121116
messenger,
122117
metadata: {},
@@ -126,8 +121,6 @@ export class MultichainRoutingController extends BaseController<
126121
},
127122
});
128123

129-
this.#getSnapKeyring = getSnapKeyring;
130-
131124
this.messagingSystem.registerActionHandler(
132125
`${controllerName}:handleRequest`,
133126
async (...args) => this.handleRequest(...args),
@@ -258,13 +251,16 @@ export class MultichainRoutingController extends BaseController<
258251
request,
259252
);
260253
if (accountSnap) {
261-
const keyring = await this.#getSnapKeyring();
262-
return keyring.submitNonEvmRequest({
263-
address: accountSnap.address,
264-
method,
265-
params,
266-
chainId: scope,
267-
});
254+
// TODO: Decide on API for this.
255+
return this.messagingSystem.call(
256+
'KeyringController:submitNonEvmRequest',
257+
{
258+
address: accountSnap.address,
259+
method,
260+
params,
261+
chainId: scope,
262+
},
263+
);
268264
}
269265

270266
// If the RPC request cannot be serviced by an account Snap,

packages/snaps-controllers/src/test-utils/controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@ export const getRestrictedMultichainRoutingControllerMessenger = (
857857
'SnapController:getAll',
858858
'SnapController:handleRequest',
859859
'AccountsController:listMultichainAccounts',
860+
'KeyringController:submitNonEvmRequest',
860861
],
861862
});
862863

packages/snaps-controllers/src/test-utils/multichain.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,3 @@ export const MOCK_SOLANA_SNAP_PERMISSIONS: Record<
8787
parentCapability: SnapEndowments.Protocol,
8888
},
8989
};
90-
91-
export const getMockSnapKeyring = (implementation = jest.fn()) => {
92-
return async () =>
93-
Promise.resolve({
94-
submitNonEvmRequest: implementation,
95-
});
96-
};

0 commit comments

Comments
 (0)