Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ describe('MultichainRoutingService', () => {
withSnapKeyring,
});

rootMessenger.registerActionHandler(
'SnapController:getRunnableSnaps',
() => {
return [getTruncatedSnap()];
},
);

rootMessenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => MOCK_BTC_ACCOUNTS,
Expand Down Expand Up @@ -90,6 +97,13 @@ describe('MultichainRoutingService', () => {
withSnapKeyring,
});

rootMessenger.registerActionHandler(
'SnapController:getRunnableSnaps',
() => {
return [getTruncatedSnap()];
},
);

rootMessenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => MOCK_SOLANA_ACCOUNTS,
Expand Down Expand Up @@ -142,6 +156,13 @@ describe('MultichainRoutingService', () => {
withSnapKeyring,
});

rootMessenger.registerActionHandler(
'SnapController:getRunnableSnaps',
() => {
return [getTruncatedSnap()];
},
);

rootMessenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => MOCK_SOLANA_ACCOUNTS,
Expand Down Expand Up @@ -231,7 +252,7 @@ describe('MultichainRoutingService', () => {
});

expect(messenger.call).toHaveBeenNthCalledWith(
5,
6,
'SnapController:handleRequest',
{
snapId: MOCK_SNAP_ID,
Expand Down Expand Up @@ -302,6 +323,13 @@ describe('MultichainRoutingService', () => {
withSnapKeyring,
});

rootMessenger.registerActionHandler(
'SnapController:getRunnableSnaps',
() => {
return [getTruncatedSnap()];
},
);

rootMessenger.registerActionHandler(
'SnapController:handleRequest',
async ({ handler }) => {
Expand Down Expand Up @@ -352,6 +380,13 @@ describe('MultichainRoutingService', () => {
withSnapKeyring,
});

rootMessenger.registerActionHandler(
'SnapController:getRunnableSnaps',
() => {
return [getTruncatedSnap()];
},
);

rootMessenger.registerActionHandler(
'SnapController:handleRequest',
async ({ handler }) => {
Expand Down Expand Up @@ -405,6 +440,13 @@ describe('MultichainRoutingService', () => {
withSnapKeyring,
});

rootMessenger.registerActionHandler(
'SnapController:getRunnableSnaps',
() => {
return [getTruncatedSnap()];
},
);

rootMessenger.registerActionHandler(
'SnapController:handleRequest',
async ({ handler }) => {
Expand Down Expand Up @@ -577,6 +619,13 @@ describe('MultichainRoutingService', () => {
() => MOCK_SOLANA_ACCOUNTS,
);

rootMessenger.registerActionHandler(
'SnapController:getRunnableSnaps',
() => {
return [getTruncatedSnap()];
},
);

expect(
messenger.call(
'MultichainRoutingService:getSupportedAccounts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,7 @@ export class MultichainRoutingService {
scope: CaipChainId,
request: JsonRpcRequest,
) {
const accounts = this.#messenger
.call('AccountsController:listMultichainAccounts', scope)
.filter(
(
account: InternalAccount,
): account is InternalAccount & {
metadata: Required<InternalAccount['metadata']>;
} =>
Boolean(account.metadata.snap?.enabled) &&
account.methods.includes(request.method),
);
const accounts = this.#getSupportedAccountsMetadata(scope, request.method);

// If no accounts can service the request, return null.
if (accounts.length === 0) {
Expand Down Expand Up @@ -349,12 +339,31 @@ export class MultichainRoutingService {
* Get a list of metadata for supported accounts for a given scope from the client.
*
* @param scope - The CAIP-2 scope.
* @param method - An optional method that the account should support.
* @returns A list of metadata for the supported accounts.
*/
#getSupportedAccountsMetadata(scope: CaipChainId): InternalAccount[] {
#getSupportedAccountsMetadata(
scope: CaipChainId,
method?: string,
): (InternalAccount & {
metadata: Required<InternalAccount['metadata']>;
})[] {
const runnableSnaps = this.#messenger
.call('SnapController:getRunnableSnaps')
.map((snap) => snap.id);

return this.#messenger
.call('AccountsController:listMultichainAccounts', scope)
.filter((account: InternalAccount) => account.metadata.snap?.enabled);
.filter(
(
account: InternalAccount,
): account is InternalAccount & {
metadata: Required<InternalAccount['metadata']>;
} =>
account.metadata.snap?.id !== undefined &&
runnableSnaps.includes(account.metadata.snap?.id) &&
(method === undefined || account.methods.includes(method)),
);
}

/**
Expand Down Expand Up @@ -395,10 +404,10 @@ export class MultichainRoutingService {
* @returns True if the router can service the scope, otherwise false.
*/
isSupportedScope(scope: CaipChainId): boolean {
const hasAccountSnap = this.#messenger
.call('AccountsController:listMultichainAccounts', scope)
.some((account: InternalAccount) => account.metadata.snap?.enabled);
// We currently assume here that if one Snap exists that service the scope, we can service the scope generally.
return hasAccountSnap || this.#getProtocolSnaps(scope).length > 0;
return (
this.#getSupportedAccountsMetadata(scope).length > 0 ||
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is slightly slower now since we do not use .some. We could duplicate the code to make it faster

this.#getProtocolSnaps(scope).length > 0
);
}
}
4 changes: 0 additions & 4 deletions packages/snaps-controllers/src/test-utils/multichain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export const MOCK_BTC_ACCOUNTS = [
lastSelected: 1729154128902,
name: 'Bitcoin Account',
snap: {
enabled: true,
id: MOCK_SNAP_ID,
name: 'Bitcoin',
},
},
methods: ['sendBitcoin'],
Expand All @@ -50,9 +48,7 @@ export const MOCK_SOLANA_ACCOUNTS = [
lastSelected: 1729154128902,
name: 'Solana Account',
snap: {
enabled: true,
id: MOCK_SNAP_ID,
name: 'Solana',
},
},
methods: ['signAndSendTransaction'],
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-utils/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type InternalAccount = {
scopes: CaipChainId[];
metadata: {
name: string;
snap?: { id: SnapId; enabled: boolean; name: string };
snap?: { id: SnapId };
};
};

Expand Down
Loading