Skip to content

Commit

Permalink
Use KeyringController
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Nov 15, 2024
1 parent 352bd40 commit 6cf774a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
SOLANA_CAIP2,
MOCK_SOLANA_ACCOUNTS,
MOCK_BTC_ACCOUNTS,
getMockSnapKeyring,
} from '../test-utils';
import { MultichainRoutingController } from './MultichainRoutingController';

Expand All @@ -24,18 +23,20 @@ describe('MultichainRoutingController', () => {
/* eslint-disable-next-line no-new */
new MultichainRoutingController({
messenger,
getSnapKeyring: getMockSnapKeyring(
jest.fn().mockResolvedValue({
txid: '53de51e2fa75c3cfa51132865f7d430138b1cd92a8f5267ec836ec565b422969',
}),
),
});

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

rootMessenger.registerActionHandler(
'KeyringController:submitNonEvmRequest',
async () => ({
txid: '53de51e2fa75c3cfa51132865f7d430138b1cd92a8f5267ec836ec565b422969',
}),
);

rootMessenger.registerActionHandler(
'SnapController:handleRequest',
async ({ handler }) => {
Expand Down Expand Up @@ -74,16 +75,20 @@ describe('MultichainRoutingController', () => {
/* eslint-disable-next-line no-new */
new MultichainRoutingController({
messenger,
getSnapKeyring: getMockSnapKeyring(
jest.fn().mockResolvedValue({ signature: '0x' }),
),
});

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

rootMessenger.registerActionHandler(
'KeyringController:submitNonEvmRequest',
async () => ({
signature: '0x',
}),
);

rootMessenger.registerActionHandler(
'PermissionController:getPermissions',
() => MOCK_SOLANA_SNAP_PERMISSIONS,
Expand Down Expand Up @@ -125,7 +130,6 @@ describe('MultichainRoutingController', () => {
/* eslint-disable-next-line no-new */
new MultichainRoutingController({
messenger,
getSnapKeyring: getMockSnapKeyring(),
});

rootMessenger.registerActionHandler(
Expand Down Expand Up @@ -175,7 +179,6 @@ describe('MultichainRoutingController', () => {
/* eslint-disable-next-line no-new */
new MultichainRoutingController({
messenger,
getSnapKeyring: getMockSnapKeyring(),
});

rootMessenger.registerActionHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ export type AccountsControllerListMultichainAccountsAction = {
handler: (chainId?: CaipChainId) => InternalAccount[];
};

export type KeyringControllerSubmitNonEvmRequestAction = {
type: `KeyringController:submitNonEvmRequest`;
handler: (args: {
address: string;
method: string;
params?: Json[] | Record<string, Json>;
chainId: CaipChainId;
}) => Promise<Json>;
};

export type MultichainRoutingControllerActions =
| MultichainRoutingControllerGetStateAction
| MultichainRoutingControllerHandleRequestAction;
Expand All @@ -66,7 +76,8 @@ export type MultichainRoutingControllerAllowedActions =
| GetAllSnaps
| HandleSnapRequest
| GetPermissions
| AccountsControllerListMultichainAccountsAction;
| AccountsControllerListMultichainAccountsAction
| KeyringControllerSubmitNonEvmRequestAction;

export type MultichainRoutingControllerEvents =
MultichainRoutingControllerStateChangeEvent;
Expand All @@ -81,19 +92,9 @@ export type MultichainRoutingControllerMessenger =
MultichainRoutingControllerEvents['type']
>;

export type SnapKeyring = {
submitNonEvmRequest: (args: {
address: string;
method: string;
params?: Json[] | Record<string, Json>;
chainId: CaipChainId;
}) => Promise<Json>;
};

export type MultichainRoutingControllerArgs = {
messenger: MultichainRoutingControllerMessenger;
state?: MultichainRoutingControllerState;
getSnapKeyring: () => Promise<SnapKeyring>;
};

export type MultichainRoutingControllerState = EmptyObject;
Expand All @@ -110,13 +111,7 @@ export class MultichainRoutingController extends BaseController<
MultichainRoutingControllerState,
MultichainRoutingControllerMessenger
> {
#getSnapKeyring: () => Promise<SnapKeyring>;

constructor({
messenger,
state,
getSnapKeyring,
}: MultichainRoutingControllerArgs) {
constructor({ messenger, state }: MultichainRoutingControllerArgs) {
super({
messenger,
metadata: {},
Expand All @@ -126,8 +121,6 @@ export class MultichainRoutingController extends BaseController<
},
});

this.#getSnapKeyring = getSnapKeyring;

this.messagingSystem.registerActionHandler(
`${controllerName}:handleRequest`,
async (...args) => this.handleRequest(...args),
Expand Down Expand Up @@ -258,13 +251,16 @@ export class MultichainRoutingController extends BaseController<
request,
);
if (accountSnap) {
const keyring = await this.#getSnapKeyring();
return keyring.submitNonEvmRequest({
address: accountSnap.address,
method,
params,
chainId: scope,
});
// TODO: Decide on API for this.
return this.messagingSystem.call(
'KeyringController:submitNonEvmRequest',
{
address: accountSnap.address,
method,
params,
chainId: scope,
},
);
}

// If the RPC request cannot be serviced by an account Snap,
Expand Down
1 change: 1 addition & 0 deletions packages/snaps-controllers/src/test-utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,7 @@ export const getRestrictedMultichainRoutingControllerMessenger = (
'SnapController:getAll',
'SnapController:handleRequest',
'AccountsController:listMultichainAccounts',
'KeyringController:submitNonEvmRequest',
],
});

Expand Down
7 changes: 0 additions & 7 deletions packages/snaps-controllers/src/test-utils/multichain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,3 @@ export const MOCK_SOLANA_SNAP_PERMISSIONS: Record<
parentCapability: SnapEndowments.Protocol,
},
};

export const getMockSnapKeyring = (implementation = jest.fn()) => {
return async () =>
Promise.resolve({
submitNonEvmRequest: implementation,
});
};

0 comments on commit 6cf774a

Please sign in to comment.