Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement MultichainRouter and onProtocolRequest (SIP-26) #2875

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
8 changes: 4 additions & 4 deletions packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 92.96,
"functions": 96.71,
"lines": 98.01,
"statements": 97.71
"branches": 93.17,
"functions": 96.85,
"lines": 98.07,
"statements": 97.78
}
370 changes: 370 additions & 0 deletions packages/snaps-controllers/src/multichain/MultichainRouter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,370 @@
import { HandlerType } from '@metamask/snaps-utils';
import { getTruncatedSnap } from '@metamask/snaps-utils/test-utils';

import {
getRootMultichainRouterMessenger,
getRestrictedMultichainRouterMessenger,
BTC_CAIP2,
BTC_CONNECTED_ACCOUNTS,
MOCK_SOLANA_SNAP_PERMISSIONS,
SOLANA_CONNECTED_ACCOUNTS,
SOLANA_CAIP2,
MOCK_SOLANA_ACCOUNTS,
MOCK_BTC_ACCOUNTS,
getMockWithSnapKeyring,
} from '../test-utils';
import { MultichainRouter } from './MultichainRouter';

describe('MultichainRouter', () => {
describe('handleRequest', () => {
it('can route signing requests to account Snaps without address resolution', async () => {
const rootMessenger = getRootMultichainRouterMessenger();
const messenger = getRestrictedMultichainRouterMessenger(rootMessenger);
const withSnapKeyring = getMockWithSnapKeyring({
submitRequest: jest.fn().mockResolvedValue({
txid: '53de51e2fa75c3cfa51132865f7d430138b1cd92a8f5267ec836ec565b422969',
}),
});

/* eslint-disable-next-line no-new */
new MultichainRouter({
messenger,
withSnapKeyring,
});

rootMessenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => MOCK_BTC_ACCOUNTS,
);

rootMessenger.registerActionHandler(
'SnapController:handleRequest',
async ({ handler }) => {
if (handler === HandlerType.OnKeyringRequest) {
return null;
}
throw new Error('Unmocked request');
},
);

const result = await messenger.call('MultichainRouter:handleRequest', {
connectedAddresses: BTC_CONNECTED_ACCOUNTS,
scope: BTC_CAIP2,
request: {
method: 'btc_sendmany',
params: {
message: 'foo',
},
},
});

expect(result).toStrictEqual({
txid: '53de51e2fa75c3cfa51132865f7d430138b1cd92a8f5267ec836ec565b422969',
});
});

it('can route signing requests to account Snaps using address resolution', async () => {
const rootMessenger = getRootMultichainRouterMessenger();
const messenger = getRestrictedMultichainRouterMessenger(rootMessenger);
const withSnapKeyring = getMockWithSnapKeyring({
submitRequest: jest.fn().mockResolvedValue({
signature: '0x',
}),
});

/* eslint-disable-next-line no-new */
new MultichainRouter({
messenger,
withSnapKeyring,
});

rootMessenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => MOCK_SOLANA_ACCOUNTS,
);

rootMessenger.registerActionHandler(
'PermissionController:getPermissions',
() => MOCK_SOLANA_SNAP_PERMISSIONS,
);

rootMessenger.registerActionHandler(
'SnapController:handleRequest',
async ({ handler }) => {
if (handler === HandlerType.OnKeyringRequest) {
return { address: SOLANA_CONNECTED_ACCOUNTS[0] };
}
throw new Error('Unmocked request');
},
);

const result = await messenger.call('MultichainRouter:handleRequest', {
connectedAddresses: SOLANA_CONNECTED_ACCOUNTS,
scope: SOLANA_CAIP2,
request: {
method: 'signAndSendTransaction',
params: {
message: 'foo',
},
},
});

expect(result).toStrictEqual({ signature: '0x' });
});

it('can route protocol requests to protocol Snaps', async () => {
const rootMessenger = getRootMultichainRouterMessenger();
const messenger = getRestrictedMultichainRouterMessenger(rootMessenger);
const withSnapKeyring = getMockWithSnapKeyring();

/* eslint-disable-next-line no-new */
new MultichainRouter({
messenger,
withSnapKeyring,
});

rootMessenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => [],
);

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

rootMessenger.registerActionHandler(
'PermissionController:getPermissions',
() => MOCK_SOLANA_SNAP_PERMISSIONS,
);

rootMessenger.registerActionHandler(
'SnapController:handleRequest',
async () => ({
'feature-set': 2891131721,
'solana-core': '1.16.7',
}),
);

const result = await messenger.call('MultichainRouter:handleRequest', {
connectedAddresses: [],
scope: SOLANA_CAIP2,
request: {
method: 'getVersion',
},
});

expect(result).toStrictEqual({
'feature-set': 2891131721,
'solana-core': '1.16.7',
});
});

it('throws if no suitable Snaps are found', async () => {
const rootMessenger = getRootMultichainRouterMessenger();
const messenger = getRestrictedMultichainRouterMessenger(rootMessenger);
const withSnapKeyring = getMockWithSnapKeyring();

/* eslint-disable-next-line no-new */
new MultichainRouter({
messenger,
withSnapKeyring,
});

rootMessenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => [],
);

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

await expect(
messenger.call('MultichainRouter:handleRequest', {
connectedAddresses: [],
scope: SOLANA_CAIP2,
request: {
method: 'getVersion',
},
}),
).rejects.toThrow('The method does not exist / is not available');
});

it('throws if address resolution fails', async () => {
const rootMessenger = getRootMultichainRouterMessenger();
const messenger = getRestrictedMultichainRouterMessenger(rootMessenger);
const withSnapKeyring = getMockWithSnapKeyring({
// Simulate the Snap returning a bogus address
resolveAccountAddress: jest.fn().mockResolvedValue({ address: 'foo' }),
});

/* eslint-disable-next-line no-new */
new MultichainRouter({
messenger,
withSnapKeyring,
});

rootMessenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => MOCK_SOLANA_ACCOUNTS,
);

rootMessenger.registerActionHandler(
'PermissionController:getPermissions',
() => MOCK_SOLANA_SNAP_PERMISSIONS,
);

await expect(
messenger.call('MultichainRouter:handleRequest', {
connectedAddresses: SOLANA_CONNECTED_ACCOUNTS,
scope: SOLANA_CAIP2,
request: {
method: 'signAndSendTransaction',
params: {
message: 'foo',
},
},
}),
).rejects.toThrow('Internal JSON-RPC error');
});

it('throws if address resolution returns an address that isnt available', async () => {
const rootMessenger = getRootMultichainRouterMessenger();
const messenger = getRestrictedMultichainRouterMessenger(rootMessenger);
const withSnapKeyring = getMockWithSnapKeyring({
// Simulate the Snap returning an unconnected address
resolveAccountAddress: jest.fn().mockResolvedValue({
address:
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKa',
}),
});

/* eslint-disable-next-line no-new */
new MultichainRouter({
messenger,
withSnapKeyring,
});

rootMessenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => MOCK_SOLANA_ACCOUNTS,
);

rootMessenger.registerActionHandler(
'PermissionController:getPermissions',
() => MOCK_SOLANA_SNAP_PERMISSIONS,
);

await expect(
messenger.call('MultichainRouter:handleRequest', {
connectedAddresses: SOLANA_CONNECTED_ACCOUNTS,
scope: SOLANA_CAIP2,
request: {
method: 'signAndSendTransaction',
params: {
message: 'foo',
},
},
}),
).rejects.toThrow('Invalid method parameter(s)');
});
});

describe('getSupportedMethods', () => {
it('returns a set of both protocol and account Snap methods', async () => {
const rootMessenger = getRootMultichainRouterMessenger();
const messenger = getRestrictedMultichainRouterMessenger(rootMessenger);
const withSnapKeyring = getMockWithSnapKeyring();

/* eslint-disable-next-line no-new */
new MultichainRouter({
messenger,
withSnapKeyring,
});

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

rootMessenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => MOCK_SOLANA_ACCOUNTS,
);

rootMessenger.registerActionHandler(
'PermissionController:getPermissions',
() => MOCK_SOLANA_SNAP_PERMISSIONS,
);

expect(
messenger.call('MultichainRouter:getSupportedMethods', {
scope: SOLANA_CAIP2,
}),
).toStrictEqual(['signAndSendTransaction', 'getVersion']);
});

it('handles lack of protocol Snaps', async () => {
const rootMessenger = getRootMultichainRouterMessenger();
const messenger = getRestrictedMultichainRouterMessenger(rootMessenger);
const withSnapKeyring = getMockWithSnapKeyring();

/* eslint-disable-next-line no-new */
new MultichainRouter({
messenger,
withSnapKeyring,
});

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

rootMessenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => MOCK_SOLANA_ACCOUNTS,
);

rootMessenger.registerActionHandler(
'PermissionController:getPermissions',
() => ({}),
);

expect(
messenger.call('MultichainRouter:getSupportedMethods', {
scope: SOLANA_CAIP2,
}),
).toStrictEqual(['signAndSendTransaction']);
});

it('handles lack of account Snaps', async () => {
const rootMessenger = getRootMultichainRouterMessenger();
const messenger = getRestrictedMultichainRouterMessenger(rootMessenger);
const withSnapKeyring = getMockWithSnapKeyring();

/* eslint-disable-next-line no-new */
new MultichainRouter({
messenger,
withSnapKeyring,
});

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

rootMessenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => [],
);

rootMessenger.registerActionHandler(
'PermissionController:getPermissions',
() => MOCK_SOLANA_SNAP_PERMISSIONS,
);

expect(
messenger.call('MultichainRouter:getSupportedMethods', {
scope: SOLANA_CAIP2,
}),
).toStrictEqual(['getVersion']);
});
});
});
Loading
Loading