Skip to content

Commit 973e64f

Browse files
Add isSupportedScope
1 parent e492e23 commit 973e64f

File tree

4 files changed

+81
-32
lines changed

4 files changed

+81
-32
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"branches": 93.06,
3-
"functions": 96.59,
4-
"lines": 98.08,
5-
"statements": 97.8
2+
"branches": 93.26,
3+
"functions": 96.79,
4+
"lines": 98.14,
5+
"statements": 97.87
66
}

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

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,7 @@ describe('MultichainRouter', () => {
297297
);
298298

299299
expect(
300-
messenger.call('MultichainRouter:getSupportedMethods', {
301-
scope: SOLANA_CAIP2,
302-
}),
300+
messenger.call('MultichainRouter:getSupportedMethods', SOLANA_CAIP2),
303301
).toStrictEqual(['signAndSendTransaction', 'getVersion']);
304302
});
305303

@@ -329,9 +327,7 @@ describe('MultichainRouter', () => {
329327
);
330328

331329
expect(
332-
messenger.call('MultichainRouter:getSupportedMethods', {
333-
scope: SOLANA_CAIP2,
334-
}),
330+
messenger.call('MultichainRouter:getSupportedMethods', SOLANA_CAIP2),
335331
).toStrictEqual(['signAndSendTransaction']);
336332
});
337333

@@ -361,15 +357,13 @@ describe('MultichainRouter', () => {
361357
);
362358

363359
expect(
364-
messenger.call('MultichainRouter:getSupportedMethods', {
365-
scope: SOLANA_CAIP2,
366-
}),
360+
messenger.call('MultichainRouter:getSupportedMethods', SOLANA_CAIP2),
367361
).toStrictEqual(['getVersion']);
368362
});
369363
});
370364

371365
describe('getSupportedAccounts', () => {
372-
it('returns a set of both protocol and account Snap methods', async () => {
366+
it('returns a set of accounts for the requested scope', async () => {
373367
const rootMessenger = getRootMultichainRouterMessenger();
374368
const messenger = getRestrictedMultichainRouterMessenger(rootMessenger);
375369
const withSnapKeyring = getMockWithSnapKeyring();
@@ -380,27 +374,60 @@ describe('MultichainRouter', () => {
380374
withSnapKeyring,
381375
});
382376

383-
rootMessenger.registerActionHandler('SnapController:getAll', () => {
384-
return [getTruncatedSnap()];
377+
rootMessenger.registerActionHandler(
378+
'AccountsController:listMultichainAccounts',
379+
() => MOCK_SOLANA_ACCOUNTS,
380+
);
381+
382+
expect(
383+
messenger.call('MultichainRouter:getSupportedAccounts', SOLANA_CAIP2),
384+
).toStrictEqual([
385+
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
386+
]);
387+
});
388+
});
389+
390+
describe('isSupportedScope', () => {
391+
it('returns true if an account Snap exists', async () => {
392+
const rootMessenger = getRootMultichainRouterMessenger();
393+
const messenger = getRestrictedMultichainRouterMessenger(rootMessenger);
394+
const withSnapKeyring = getMockWithSnapKeyring();
395+
396+
/* eslint-disable-next-line no-new */
397+
new MultichainRouter({
398+
messenger,
399+
withSnapKeyring,
385400
});
386401

387402
rootMessenger.registerActionHandler(
388403
'AccountsController:listMultichainAccounts',
389404
() => MOCK_SOLANA_ACCOUNTS,
390405
);
391406

407+
expect(
408+
messenger.call('MultichainRouter:isSupportedScope', SOLANA_CAIP2),
409+
).toBe(true);
410+
});
411+
412+
it('returns false if no account Snap is found', async () => {
413+
const rootMessenger = getRootMultichainRouterMessenger();
414+
const messenger = getRestrictedMultichainRouterMessenger(rootMessenger);
415+
const withSnapKeyring = getMockWithSnapKeyring();
416+
417+
/* eslint-disable-next-line no-new */
418+
new MultichainRouter({
419+
messenger,
420+
withSnapKeyring,
421+
});
422+
392423
rootMessenger.registerActionHandler(
393-
'PermissionController:getPermissions',
394-
() => MOCK_SOLANA_SNAP_PERMISSIONS,
424+
'AccountsController:listMultichainAccounts',
425+
() => [],
395426
);
396427

397428
expect(
398-
messenger.call('MultichainRouter:getSupportedAccounts', {
399-
scope: SOLANA_CAIP2,
400-
}),
401-
).toStrictEqual([
402-
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
403-
]);
429+
messenger.call('MultichainRouter:isSupportedScope', SOLANA_CAIP2),
430+
).toBe(false);
404431
});
405432
});
406433
});

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export type MultichainRouterGetSupportedAccountsAction = {
3333
handler: MultichainRouter['getSupportedAccounts'];
3434
};
3535

36+
export type MultichainRouterIsSupportedScopeAction = {
37+
type: `${typeof name}:isSupportedScope`;
38+
handler: MultichainRouter['isSupportedScope'];
39+
};
40+
3641
// Since the AccountsController depends on snaps-controllers we manually type this
3742
type InternalAccount = {
3843
id: string;
@@ -68,7 +73,8 @@ export type AccountsControllerListMultichainAccountsAction = {
6873
export type MultichainRouterActions =
6974
| MultichainRouterHandleRequestAction
7075
| MultichainRouterGetSupportedMethodsAction
71-
| MultichainRouterGetSupportedAccountsAction;
76+
| MultichainRouterGetSupportedAccountsAction
77+
| MultichainRouterIsSupportedScopeAction;
7278

7379
export type MultichainRouterAllowedActions =
7480
| GetAllSnaps
@@ -121,6 +127,11 @@ export class MultichainRouter {
121127
`${name}:getSupportedAccounts`,
122128
(...args) => this.getSupportedAccounts(...args),
123129
);
130+
131+
this.#messenger.registerActionHandler(
132+
`${name}:isSupportedScope`,
133+
(...args) => this.isSupportedScope(...args),
134+
);
124135
}
125136

126137
async #resolveRequestAddress(
@@ -294,11 +305,10 @@ export class MultichainRouter {
294305
* Get a list of supported methods for a given scope.
295306
* This combines both protocol and account Snaps supported methods.
296307
*
297-
* @param options - An options bag.
298-
* @param options.scope - The CAIP-2 scope.
308+
* @param scope - The CAIP-2 scope.
299309
* @returns A list of supported methods.
300310
*/
301-
getSupportedMethods({ scope }: { scope: CaipChainId }): string[] {
311+
getSupportedMethods(scope: CaipChainId): string[] {
302312
const accountMethods = this.#messenger
303313
.call('AccountsController:listMultichainAccounts', scope)
304314
.filter((account: InternalAccount) => account.metadata.snap?.enabled)
@@ -314,14 +324,26 @@ export class MultichainRouter {
314324
/**
315325
* Get a list of supported accounts for a given scope.
316326
*
317-
* @param options - An options bag.
318-
* @param options.scope - The CAIP-2 scope.
327+
* @param scope - The CAIP-2 scope.
319328
* @returns A list of CAIP-10 addresses.
320329
*/
321-
getSupportedAccounts({ scope }: { scope: CaipChainId }): string[] {
330+
getSupportedAccounts(scope: CaipChainId): string[] {
322331
return this.#messenger
323332
.call('AccountsController:listMultichainAccounts', scope)
324333
.filter((account: InternalAccount) => account.metadata.snap?.enabled)
325334
.map((account) => `${scope}:${account.address}`);
326335
}
336+
337+
/**
338+
* Determine whether a given CAIP-2 scope is supported by the router.
339+
*
340+
* @param scope - The CAIP-2 scope.
341+
* @returns True if the router can service the scope, otherwise false.
342+
*/
343+
isSupportedScope(scope: CaipChainId): boolean {
344+
// We currently assume here that if one Snap exists that service the scope, we can service the scope generally.
345+
return this.#messenger
346+
.call('AccountsController:listMultichainAccounts', scope)
347+
.some((account: InternalAccount) => account.metadata.snap?.enabled);
348+
}
327349
}

packages/snaps-rpc-methods/src/endowments/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ export const handlerEndowments: Record<HandlerType, string | null> = {
125125
[HandlerType.OnHomePage]: homePageEndowmentBuilder.targetName,
126126
[HandlerType.OnSettingsPage]: settingsPageEndowmentBuilder.targetName,
127127
[HandlerType.OnSignature]: signatureInsightEndowmentBuilder.targetName,
128-
[HandlerType.OnProtocolRequest]: protocolEndowmentBuilder.targetName,
129128
[HandlerType.OnUserInput]: null,
130129
[HandlerType.OnAssetsLookup]: assetsEndowmentBuilder.targetName,
131130
[HandlerType.OnAssetsConversion]: assetsEndowmentBuilder.targetName,
131+
[HandlerType.OnProtocolRequest]: protocolEndowmentBuilder.targetName,
132132
};
133133

134134
export * from './enum';

0 commit comments

Comments
 (0)