Skip to content

feat!: add KeyringRequest.origin #273

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

Merged
merged 22 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2fa0ee7
feat: add KeyringRequest.origin
ccharly Apr 2, 2025
93954ce
feat!: add new RestrictedKeyringClient
ccharly Apr 4, 2025
03a1594
refactor: rename Restricted -> Public
ccharly Apr 4, 2025
2dd007a
refactor: make KeyringPublicClient a true class
ccharly Apr 4, 2025
35694bc
test: re-use tests for the new KeyringPublicClient
ccharly Apr 4, 2025
2d7389c
chore: (wip) add new KeyringVersion + versionned internal clients
ccharly May 20, 2025
22f2b9b
refactor: make KeyringClient.send protected + KeyringInternalSnapClie…
ccharly May 21, 2025
5279b8b
chore: lint
ccharly May 22, 2025
6ea5a18
refactor: move KeyringVersion internally into keyring-snap-bridge
ccharly May 22, 2025
795b48f
chore: bumps snaps-* + use new action from snaps-controllers package
ccharly May 22, 2025
82d3699
chore: yarn.lock
ccharly May 22, 2025
a324a79
chore: lint
ccharly May 22, 2025
ad9f8f1
feat: add safeguard against empty and non-defined origins
ccharly May 22, 2025
842b9e4
refactor: add submitRequestV1 to KeyringInternalSnapClient
ccharly May 23, 2025
c335bed
chore: update readme
ccharly May 23, 2025
cf843c1
chore: fix keyring-snap-sdk's keyring-api dep
ccharly May 26, 2025
7ef17ae
Revert "chore: fix keyring-snap-sdk's keyring-api dep"
ccharly May 26, 2025
03fdc7b
chore: typo
ccharly May 26, 2025
2352190
fix: use array instead of objects for platform version mapping
ccharly May 26, 2025
747bf47
chore: comment
ccharly May 26, 2025
2a0c476
Merge branch 'main' into feat/keyring-request-origin
ccharly May 26, 2025
6bbc1a6
Merge branch 'main' into feat/keyring-request-origin
ccharly May 26, 2025
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ linkStyle default opacity:0.5
keyring_internal_api --> keyring_api;
keyring_internal_api --> keyring_utils;
keyring_internal_snap_client --> keyring_api;
keyring_internal_snap_client --> keyring_internal_api;
keyring_internal_snap_client --> keyring_snap_client;
keyring_internal_snap_client --> keyring_utils;
eth_snap_keyring --> keyring_api;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"test:types": "yarn foreach test:types"
},
"resolutions": {
"@metamask/snaps-sdk@^6.24.0": "npm:@metamask/snaps-sdk@^7.0.0",
"@types/node": "^20.12.12",
"@types/web": "^0.0.69",
"@typescript/lib-dom": "npm:@types/web@^0.0.69",
Expand Down
21 changes: 21 additions & 0 deletions packages/keyring-api/src/api/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('KeyringRequest', () => {
id: '47d782ac-15c8-4c81-8bfe-759ae1be4a3e',
scope: 'eip155:1',
account: 'd6311e3c-a4ec-43fa-b341-592ffefd9797',
origin: 'metamask',
request: {
method: 'eth_personalSign',
params: {
Expand All @@ -24,6 +25,7 @@ describe('KeyringRequest', () => {
id: '47d782ac-15c8-4c81-8bfe-759ae1be4a3e',
scope: 'eip155:1',
account: 'd6311e3c-a4ec-43fa-b341-592ffefd9797',
origin: 'metamask',
request: {
method: 'eth_somethingElseWithNoParameters',
},
Expand All @@ -36,6 +38,7 @@ describe('KeyringRequest', () => {
request: {
scope: 'eip155:1',
account: 'd6311e3c-a4ec-43fa-b341-592ffefd9797',
origin: 'metamask',
request: {
method: 'eth_personalSign',
params: {
Expand All @@ -50,6 +53,7 @@ describe('KeyringRequest', () => {
request: {
id: '47d782ac-15c8-4c81-8bfe-759ae1be4a3e',
account: 'd6311e3c-a4ec-43fa-b341-592ffefd9797',
origin: 'metamask',
request: {
method: 'eth_personalSign',
params: {
Expand All @@ -64,6 +68,22 @@ describe('KeyringRequest', () => {
request: {
id: '47d782ac-15c8-4c81-8bfe-759ae1be4a3e',
scope: 'eip155:1',
origin: 'metamask',
request: {
method: 'eth_personalSign',
params: {
data: '0x00...',
},
},
},
expected: false,
},
// Missing origin
{
request: {
id: '47d782ac-15c8-4c81-8bfe-759ae1be4a3e',
scope: 'eip155:1',
account: 'd6311e3c-a4ec-43fa-b341-592ffefd9797',
request: {
method: 'eth_personalSign',
params: {
Expand All @@ -79,6 +99,7 @@ describe('KeyringRequest', () => {
id: '47d782ac-15c8-4c81-8bfe-759ae1be4a3e',
scope: 'eip155:1',
account: 'd6311e3c-a4ec-43fa-b341-592ffefd9797',
origin: 'metamask',
},
expected: false,
},
Expand Down
5 changes: 5 additions & 0 deletions packages/keyring-api/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export const KeyringRequestStruct = object({
*/
account: UuidStruct,

/**
* Origin of the sender.
*/
origin: string(),

/**
* Inner request sent by the client application.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/keyring-internal-api/src/compatibility/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './v1';
23 changes: 23 additions & 0 deletions packages/keyring-internal-api/src/compatibility/v1.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { EthMethod, EthScope } from '@metamask/keyring-api';

import { toKeyringRequestV1 } from './v1';

describe('v1', () => {
describe('toKeyringRequestV1', () => {
const request = {
id: 'mock-request-id',
scope: EthScope.Mainnet,
account: '55583f38-d81b-48f8-8494-fc543c2b5c95',
origin: 'test',
request: {
method: EthMethod.PersonalSign,
params: {},
},
};
const { origin, ...requestV1 } = request;

it('converts a keyring request to a keyring request v1', () => {
expect(toKeyringRequestV1(request)).toStrictEqual(requestV1);
});
});
});
34 changes: 34 additions & 0 deletions packages/keyring-internal-api/src/compatibility/v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { KeyringRequest } from '@metamask/keyring-api';
import {
KeyringRequestStruct,
KeyringResponseStruct,
} from '@metamask/keyring-api';
import { omit, type Infer } from '@metamask/superstruct';

/**
* Keyring request (v1).
*/
export const KeyringRequestV1Struct = omit(KeyringRequestStruct, ['origin']);

export type KeyringRequestV1 = Infer<typeof KeyringRequestV1Struct>;

/**
* Response to a call to `submitRequest` (v1).
*/
export const KeyringResponseV1Struct = KeyringResponseStruct;

export type KeyringResponseV1 = Infer<typeof KeyringResponseV1Struct>;

export const SubmitRequestResponseV1Struct = KeyringResponseV1Struct;

/**
* Converts a keyring request to a keyring request v1.
*
* @param request - A keyring request.
* @returns A keyring request v1.
*/
export function toKeyringRequestV1(request: KeyringRequest): KeyringRequestV1 {
const { origin, ...requestV1 } = request;

return requestV1;
}
2 changes: 2 additions & 0 deletions packages/keyring-internal-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './compatibility';
export type * from './eth';
export * from './types';
export * from './versions';
12 changes: 12 additions & 0 deletions packages/keyring-internal-api/src/versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export enum KeyringVersion {
/** Default. */
V1 = 'v1',

/**
* Introduction of `KeyringRequest.origin`.
*
* Snap will now receive the `origin` as part of a `KeyringRequest` when `submitRequest` is invoked.
* We also expect Snaps to display this `origin` in their confirmation screens (if any).
*/
V2 = 'v2',
}
7 changes: 4 additions & 3 deletions packages/keyring-internal-snap-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@
"dependencies": {
"@metamask/base-controller": "^7.1.1",
"@metamask/keyring-api": "workspace:^",
"@metamask/keyring-internal-api": "workspace:^",
"@metamask/keyring-snap-client": "workspace:^",
"@metamask/keyring-utils": "workspace:^"
},
"devDependencies": {
"@lavamoat/allow-scripts": "^3.2.1",
"@lavamoat/preinstall-always-fail": "^2.1.0",
"@metamask/auto-changelog": "^3.4.4",
"@metamask/snaps-controllers": "^9.18.0",
"@metamask/snaps-sdk": "^6.16.0",
"@metamask/snaps-utils": "^8.9.1",
"@metamask/snaps-controllers": "^12.2.0",
"@metamask/snaps-sdk": "^7.0.0",
"@metamask/snaps-utils": "^9.3.0",
"@metamask/utils": "^11.1.0",
"@ts-bridge/cli": "^0.6.3",
"@types/jest": "^29.5.12",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import type { KeyringAccount } from '@metamask/keyring-api';
import {
EthMethod,
EthScope,
type KeyringAccount,
} from '@metamask/keyring-api';
import type { KeyringRequestV1 } from '@metamask/keyring-internal-api';
import type { SnapId } from '@metamask/snaps-sdk';

import {
KeyringInternalSnapClient,
type KeyringInternalSnapClientMessenger,
} from './KeyringInternalSnapClient';

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

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

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

const messenger = {
call: jest.fn(),
Expand Down Expand Up @@ -81,4 +86,44 @@ describe('KeyringInternalSnapClient', () => {
);
});
});

describe('submitRequestV1', () => {
const keyringRequest: KeyringRequestV1 = {
id: 'mock-request-id',
scope: EthScope.Mainnet,
account: MOCK_ACCOUNT.id,
request: {
method: EthMethod.PersonalSign,
params: {},
},
};
const request = {
snapId,
origin: 'metamask',
handler: 'onKeyringRequest',
request: {
id: expect.any(String),
jsonrpc: '2.0',
method: 'keyring_submitRequest',
params: keyringRequest,
},
};

it('calls the submitRequestV1 method', async () => {
const client = new KeyringInternalSnapClient({
messenger: messenger as unknown as KeyringInternalSnapClientMessenger,
snapId,
});

messenger.call.mockResolvedValue({
pending: false,
result: null,
});
await client.submitRequestV1(keyringRequest);
expect(messenger.call).toHaveBeenCalledWith(
'SnapController:handleRequest',
request,
);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { RestrictedMessenger } from '@metamask/base-controller';
import { KeyringRpcMethod } from '@metamask/keyring-api';
import type {
KeyringRequestV1,
KeyringResponseV1,
} from '@metamask/keyring-internal-api';
import { SubmitRequestResponseV1Struct } from '@metamask/keyring-internal-api';
import { KeyringClient, type Sender } from '@metamask/keyring-snap-client';
import type { JsonRpcRequest } from '@metamask/keyring-utils';
import { strictMask, type JsonRpcRequest } from '@metamask/keyring-utils';
import type { HandleSnapRequest } from '@metamask/snaps-controllers';
import type { SnapId } from '@metamask/snaps-sdk';
import type { HandlerType } from '@metamask/snaps-utils';
Expand Down Expand Up @@ -121,4 +127,20 @@ export class KeyringInternalSnapClient extends KeyringClient {
snapId,
});
}

/**
* Submit a keyring request v1 (with no `origin`).
*
* @param request - Keyring request.
* @returns Keyring request's response.
*/
async submitRequestV1(request: KeyringRequestV1): Promise<KeyringResponseV1> {
return strictMask(
await this.send({
method: KeyringRpcMethod.SubmitRequest,
params: request,
}),
SubmitRequestResponseV1Struct,
);
}
}
3 changes: 3 additions & 0 deletions packages/keyring-internal-snap-client/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
{
"path": "../keyring-api/tsconfig.build.json"
},
{
"path": "../keyring-internal-api/tsconfig.build.json"
},
{
"path": "../keyring-snap-client/tsconfig.build.json"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/keyring-snap-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
"@lavamoat/allow-scripts": "^3.2.1",
"@lavamoat/preinstall-always-fail": "^2.1.0",
"@metamask/auto-changelog": "^3.4.4",
"@metamask/snaps-controllers": "^9.18.0",
"@metamask/snaps-sdk": "^6.16.0",
"@metamask/snaps-utils": "^8.9.1",
"@metamask/snaps-controllers": "^12.2.0",
"@metamask/snaps-sdk": "^7.0.0",
"@metamask/snaps-utils": "^9.3.0",
"@ts-bridge/cli": "^0.6.3",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.12",
Expand Down
Loading
Loading