Skip to content

Commit 934263b

Browse files
Add getSupportedAccounts
1 parent 6426041 commit 934263b

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,40 @@ describe('MultichainRouter', () => {
367367
).toStrictEqual(['getVersion']);
368368
});
369369
});
370+
371+
describe('getSupportedAccounts', () => {
372+
it('returns a set of both protocol and account Snap methods', async () => {
373+
const rootMessenger = getRootMultichainRouterMessenger();
374+
const messenger = getRestrictedMultichainRouterMessenger(rootMessenger);
375+
const withSnapKeyring = getMockWithSnapKeyring();
376+
377+
/* eslint-disable-next-line no-new */
378+
new MultichainRouter({
379+
messenger,
380+
withSnapKeyring,
381+
});
382+
383+
rootMessenger.registerActionHandler('SnapController:getAll', () => {
384+
return [getTruncatedSnap()];
385+
});
386+
387+
rootMessenger.registerActionHandler(
388+
'AccountsController:listMultichainAccounts',
389+
() => MOCK_SOLANA_ACCOUNTS,
390+
);
391+
392+
rootMessenger.registerActionHandler(
393+
'PermissionController:getPermissions',
394+
() => MOCK_SOLANA_SNAP_PERMISSIONS,
395+
);
396+
397+
expect(
398+
messenger.call('MultichainRouter:getSupportedAccounts', {
399+
scope: SOLANA_CAIP2,
400+
}),
401+
).toStrictEqual([
402+
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
403+
]);
404+
});
405+
});
370406
});

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export type MultichainRouterGetSupportedMethodsAction = {
2828
handler: MultichainRouter['getSupportedMethods'];
2929
};
3030

31+
export type MultichainRouterGetSupportedAccountsAction = {
32+
type: `${typeof name}:getSupportedAccounts`;
33+
handler: MultichainRouter['getSupportedAccounts'];
34+
};
35+
3136
// Since the AccountsController depends on snaps-controllers we manually type this
3237
type InternalAccount = {
3338
id: string;
@@ -62,7 +67,8 @@ export type AccountsControllerListMultichainAccountsAction = {
6267

6368
export type MultichainRouterActions =
6469
| MultichainRouterHandleRequestAction
65-
| MultichainRouterGetSupportedMethodsAction;
70+
| MultichainRouterGetSupportedMethodsAction
71+
| MultichainRouterGetSupportedAccountsAction;
6672

6773
export type MultichainRouterAllowedActions =
6874
| GetAllSnaps
@@ -110,6 +116,11 @@ export class MultichainRouter {
110116
`${name}:getSupportedMethods`,
111117
(...args) => this.getSupportedMethods(...args),
112118
);
119+
120+
this.#messenger.registerActionHandler(
121+
`${name}:getSupportedAccounts`,
122+
(...args) => this.getSupportedAccounts(...args),
123+
);
113124
}
114125

115126
async #resolveRequestAddress(
@@ -300,4 +311,18 @@ export class MultichainRouter {
300311

301312
return Array.from(new Set([...accountMethods, ...protocolMethods]));
302313
}
314+
315+
/**
316+
* Get a list of supported accounts for a given scope.
317+
*
318+
* @param options - An options bag.
319+
* @param options.scope - The CAIP-2 scope.
320+
* @returns A list of CAIP-10 addresses.
321+
*/
322+
getSupportedAccounts({ scope }: { scope: CaipChainId }): string[] {
323+
return this.#messenger
324+
.call('AccountsController:listMultichainAccounts', scope)
325+
.filter((account: InternalAccount) => account.metadata.snap?.enabled)
326+
.map((account) => `${scope}:${account.address}`);
327+
}
303328
}

0 commit comments

Comments
 (0)