Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: add KeyringInternalSnapClientV2 + missing v2 exports
  • Loading branch information
ccharly committed Dec 5, 2025
commit 4b72e8b47c48243ba2bc9f2dcd9e64dd4e7ed2a0
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,13 @@ import type {
KeyringResponseV1,
} from '@metamask/keyring-internal-api';
import { SubmitRequestResponseV1Struct } from '@metamask/keyring-internal-api';
import { KeyringClient, type Sender } from '@metamask/keyring-snap-client';
import { strictMask, type JsonRpcRequest } from '@metamask/keyring-utils';
import type { Messenger } from '@metamask/messenger';
import type { HandleSnapRequest } from '@metamask/snaps-controllers';
import { KeyringClient } from '@metamask/keyring-snap-client';
import { strictMask } from '@metamask/keyring-utils';
import type { SnapId } from '@metamask/snaps-sdk';
import type { HandlerType } from '@metamask/snaps-utils';
import type { Json } from '@metamask/utils';

// We only need to dispatch Snap request to the Snaps controller for now.
type AllowedActions = HandleSnapRequest;

/**
* A restricted-`Messenger` used by `KeyringInternalSnapClient` to dispatch
* internal Snap requests.
*/
export type KeyringInternalSnapClientMessenger = Messenger<
'KeyringInternalSnapClient',
AllowedActions
>;

/**
* Implementation of the `Sender` interface that can be used to send requests
* to a Snap through a `Messenger`.
*/
class SnapControllerMessengerSender implements Sender {
readonly #snapId: SnapId;

readonly #origin: string;

readonly #messenger: KeyringInternalSnapClientMessenger;

readonly #handler: HandlerType;

/**
* Create a new instance of `SnapControllerSender`.
*
* @param messenger - The `Messenger` instance used when dispatching controllers actions.
* @param snapId - The ID of the Snap to use.
* @param origin - The sender's origin.
* @param handler - The handler type.
*/
constructor(
messenger: KeyringInternalSnapClientMessenger,
snapId: SnapId,
origin: string,
handler: HandlerType,
) {
this.#messenger = messenger;
this.#snapId = snapId;
this.#origin = origin;
this.#handler = handler;
}

/**
* Send a request to the Snap and return the response.
*
* @param request - JSON-RPC request to send to the Snap.
* @returns A promise that resolves to the response of the request.
*/
async send(request: JsonRpcRequest): Promise<Json> {
return this.#messenger.call('SnapController:handleRequest', {
snapId: this.#snapId,
origin: this.#origin,
handler: this.#handler,
request,
}) as Promise<Json>;
}
}
import type { KeyringInternalSnapClientMessenger } from './KeyringInternalSnapClientMessenger';
import { SnapControllerMessengerSender } from './SnapControllerMessengerSender';

/**
* A `KeyringClient` that allows the communication with a Snap through a
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Implementation of the `Sender` interface that can be used to send requests
* to a Snap through a `Messenger`.
*/
export class SnapControllerMessengerSender implements Sender {
readonly #snapId: SnapId;

readonly #origin: string;

readonly #messenger: KeyringInternalSnapClientMessenger;

readonly #handler: HandlerType;

/**
* Create a new instance of `SnapControllerSender`.
*
* @param messenger - The `Messenger` instance used when dispatching controllers actions.
* @param snapId - The ID of the Snap to use.
* @param origin - The sender's origin.
* @param handler - The handler type.
*/
constructor(
messenger: KeyringInternalSnapClientMessenger,
snapId: SnapId,
origin: string,
handler: HandlerType,
) {
this.#messenger = messenger;
this.#snapId = snapId;
this.#origin = origin;
this.#handler = handler;
}

/**
* Send a request to the Snap and return the response.
*
* @param request - JSON-RPC request to send to the Snap.
* @returns A promise that resolves to the response of the request.
*/
async send(request: JsonRpcRequest): Promise<Json> {
return this.#messenger.call('SnapController:handleRequest', {
snapId: this.#snapId,
origin: this.#origin,
handler: this.#handler,
request,
}) as Promise<Json>;
}
}
1 change: 1 addition & 0 deletions packages/keyring-internal-snap-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './KeyringInternalSnapClient';
export * from './v2';
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { KeyringRpcV2Method, type KeyringAccount } from '@metamask/keyring-api';
import type { SnapId } from '@metamask/snaps-sdk';

import { KeyringInternalSnapClientV2 } from './KeyringInternalSnapClientV2';
import type { KeyringInternalSnapClientMessenger } from '../KeyringInternalSnapClientMessenger';

const MOCK_ACCOUNT: KeyringAccount = {
id: '13f94041-6ae6-451f-a0fe-afdd2fda18a7',
address: '0xE9A74AACd7df8112911ca93260fC5a046f8a64Ae',
options: {},
methods: [],
scopes: ['eip155:0'],
type: 'eip155:eoa',
};

describe('KeyringInternalSnapClientV2', () => {
const snapId = 'local:localhost:3000' as SnapId;

const accountsList: KeyringAccount[] = [MOCK_ACCOUNT];

const messenger = {
call: jest.fn(),
};

describe('getAccounts', () => {
const request = {
snapId,
origin: 'metamask',
handler: 'onKeyringRequest',
request: {
id: expect.any(String),
jsonrpc: '2.0',
method: KeyringRpcV2Method.GetAccounts,
},
};

it('calls the getAccounts method and return the result', async () => {
const client = new KeyringInternalSnapClientV2({
messenger: messenger as unknown as KeyringInternalSnapClientMessenger,
snapId,
});

messenger.call.mockResolvedValue(accountsList);
const accounts = await client.getAccounts();
expect(messenger.call).toHaveBeenCalledWith(
'SnapController:handleRequest',
request,
);
expect(accounts).toStrictEqual(accountsList);
});

it('calls the getAccounts method and return the result (withSnapId)', async () => {
const client = new KeyringInternalSnapClientV2({
messenger: messenger as unknown as KeyringInternalSnapClientMessenger,
});

messenger.call.mockResolvedValue(accountsList);
const accounts = await client.withSnapId(snapId).getAccounts();
expect(messenger.call).toHaveBeenCalledWith(
'SnapController:handleRequest',
request,
);
expect(accounts).toStrictEqual(accountsList);
});

it('calls the default snapId value ("undefined")', async () => {
const client = new KeyringInternalSnapClientV2({
messenger: messenger as unknown as KeyringInternalSnapClientMessenger,
});

messenger.call.mockResolvedValue(accountsList);
await client.getAccounts();
expect(messenger.call).toHaveBeenCalledWith(
'SnapController:handleRequest',
{
...request,
snapId: 'undefined',
},
);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { KeyringClientV2 } from '@metamask/keyring-snap-client';
import type { SnapId } from '@metamask/snaps-sdk';
import type { HandlerType } from '@metamask/snaps-utils';

import type { KeyringInternalSnapClientMessenger } from '../KeyringInternalSnapClientMessenger';
import { SnapControllerMessengerSender } from '../SnapControllerMessengerSender';

/**
* A `KeyringClient` that allows the communication with a Snap through a
* `Messenger`.
*/
export class KeyringInternalSnapClientV2 extends KeyringClientV2 {
readonly #messenger: KeyringInternalSnapClientMessenger;

/**
* Create a new instance of `KeyringInternalSnapClient`.
*
* The `handlerType` argument has a hard-coded default `string` value instead
* of a `HandlerType` value to prevent the `@metamask/snaps-utils` module
* from being required at runtime.
*
* @param args - Constructor arguments.
* @param args.messenger - The `KeyringInternalSnapClientMessenger` instance to use.
* @param args.snapId - The ID of the Snap to use (default: `'undefined'`).
* @param args.origin - The sender's origin (default: `'metamask'`).
* @param args.handler - The handler type (default: `'onKeyringRequest'`).
*/
constructor({
messenger,
snapId = 'undefined' as SnapId,
origin = 'metamask',
handler = 'onKeyringRequest' as HandlerType,
}: {
messenger: KeyringInternalSnapClientMessenger;
snapId?: SnapId;
origin?: string;
handler?: HandlerType;
}) {
super(
new SnapControllerMessengerSender(messenger, snapId, origin, handler),
);
this.#messenger = messenger;
}

/**
* Create a new instance of `KeyringInternalSnapClient` with the specified
* `snapId`.
*
* @param snapId - The ID of the Snap to use in the new instance.
* @returns A new instance of `KeyringInternalSnapClient` with the
* specified Snap ID.
*/
withSnapId(snapId: SnapId): KeyringInternalSnapClientV2 {
return new KeyringInternalSnapClientV2({
messenger: this.#messenger,
snapId,
});
}
}
1 change: 1 addition & 0 deletions packages/keyring-internal-snap-client/src/v2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './KeyringInternalSnapClientV2';
1 change: 1 addition & 0 deletions packages/keyring-snap-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './KeyringClient';
export * from './KeyringSnapRpcClient';
export * from './KeyringPublicClient';
export * from './v2';
1 change: 1 addition & 0 deletions packages/keyring-snap-client/src/v2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './KeyringClientV2';
Loading