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

Move snap_manageAccounts to a gated permitted method #2869

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions packages/snaps-rpc-methods/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ module.exports = deepmerge(baseConfig, {
],
coverageThreshold: {
global: {
branches: 92.85,
functions: 97.23,
lines: 97.8,
statements: 97.31,
branches: 92.88,
functions: 97.22,
lines: 97.81,
statements: 97.32,
},
},
});
9 changes: 0 additions & 9 deletions packages/snaps-rpc-methods/src/permissions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,6 @@ describe('buildSnapRestrictedMethodSpecifications', () => {
],
"targetName": "snap_getPreferences",
},
"snap_manageAccounts": {
"allowedCaveats": null,
"methodImplementation": [Function],
"permissionType": "RestrictedMethod",
"subjectTypes": [
"snap",
],
"targetName": "snap_manageAccounts",
},
"snap_manageState": {
"allowedCaveats": null,
"methodImplementation": [Function],
Expand Down
2 changes: 2 additions & 0 deletions packages/snaps-rpc-methods/src/permitted/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getInterfaceStateHandler } from './getInterfaceState';
import { getSnapsHandler } from './getSnaps';
import { invokeKeyringHandler } from './invokeKeyring';
import { invokeSnapSugarHandler } from './invokeSnapSugar';
import { manageAccountsHandler } from './manageAccounts';
import { requestSnapsHandler } from './requestSnaps';
import { resolveInterfaceHandler } from './resolveInterface';
import { updateInterfaceHandler } from './updateInterface';
Expand All @@ -27,6 +28,7 @@ export const methodHandlers = {
snap_resolveInterface: resolveInterfaceHandler,
snap_getCurrencyRate: getCurrencyRateHandler,
snap_experimentalProviderRequest: providerRequestHandler,
snap_manageAccounts: manageAccountsHandler,
};
/* eslint-enable @typescript-eslint/naming-convention */

Expand Down
4 changes: 3 additions & 1 deletion packages/snaps-rpc-methods/src/permitted/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { GetClientStatusHooks } from './getClientStatus';
import type { GetCurrencyRateMethodHooks } from './getCurrencyRate';
import type { GetInterfaceStateMethodHooks } from './getInterfaceState';
import type { GetSnapsHooks } from './getSnaps';
import type { ManageAccountsMethodHooks } from './manageAccounts';
import type { RequestSnapsHooks } from './requestSnaps';
import type { ResolveInterfaceMethodHooks } from './resolveInterface';
import type { UpdateInterfaceMethodHooks } from './updateInterface';
Expand All @@ -18,7 +19,8 @@ export type PermittedRpcMethodHooks = GetAllSnapsHooks &
GetInterfaceStateMethodHooks &
ResolveInterfaceMethodHooks &
GetCurrencyRateMethodHooks &
ProviderRequestMethodHooks;
ProviderRequestMethodHooks &
ManageAccountsMethodHooks;

export * from './handlers';
export * from './middleware';
208 changes: 208 additions & 0 deletions packages/snaps-rpc-methods/src/permitted/manageAccounts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import { rpcErrors } from '@metamask/rpc-errors';
import type { ManageAccountsResult } from '@metamask/snaps-sdk';
import type {
JsonRpcFailure,
JsonRpcRequest,
PendingJsonRpcResponse,
} from '@metamask/utils';

import type { ManageAccountsParameters } from './manageAccounts';
import { manageAccountsHandler } from './manageAccounts';

describe('snap_manageAccounts', () => {
describe('manageAccountsHandler', () => {
it('has the expected shape', () => {
expect(manageAccountsHandler).toMatchObject({
methodNames: ['snap_manageAccounts'],
implementation: expect.any(Function),
hookNames: {
hasPermission: true,
handleKeyringSnapMessage: true,
},
});
});
});

describe('implementation', () => {
it('returns the result from the `handleKeyringSnapMessage` hook', async () => {
const { implementation } = manageAccountsHandler;

const hasPermission = jest.fn().mockReturnValue(true);
const handleKeyringSnapMessage = jest.fn().mockReturnValue('foo');

const hooks = {
hasPermission,
handleKeyringSnapMessage,
};

const engine = new JsonRpcEngine();

engine.push((request, response, next, end) => {
const result = implementation(
request as JsonRpcRequest<ManageAccountsParameters>,
response as PendingJsonRpcResponse<ManageAccountsResult>,
next,
end,
hooks,
);

result?.catch(end);
});

const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'snap_manageAccounts',
params: {
method: 'foo',
params: { bar: 'baz' },
},
});

expect(handleKeyringSnapMessage).toHaveBeenCalledWith({
method: 'foo',
params: { bar: 'baz' },
});

expect(response).toStrictEqual({ jsonrpc: '2.0', id: 1, result: 'foo' });
});

it('throws an error if the snap does not have permission', async () => {
const { implementation } = manageAccountsHandler;

const hasPermission = jest.fn().mockReturnValue(false);
const handleKeyringSnapMessage = jest.fn().mockReturnValue('foo');

const hooks = {
hasPermission,
handleKeyringSnapMessage,
};

const engine = new JsonRpcEngine();

engine.push((request, response, next, end) => {
const result = implementation(
request as JsonRpcRequest<ManageAccountsParameters>,
response as PendingJsonRpcResponse<ManageAccountsResult>,
next,
end,
hooks,
);

result?.catch(end);
});

const response = (await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'snap_manageAccounts',
params: {
method: 'foo',
params: { bar: 'baz' },
},
})) as JsonRpcFailure;

expect(response.error).toStrictEqual({
...rpcErrors.methodNotFound().serialize(),
stack: expect.any(String),
});
});

it('throws an error if the `handleKeyringSnapMessage` hook throws', async () => {
const { implementation } = manageAccountsHandler;

const hasPermission = jest.fn().mockReturnValue(true);
const handleKeyringSnapMessage = jest
.fn()
.mockRejectedValue(new Error('foo'));

const hooks = {
hasPermission,
handleKeyringSnapMessage,
};

const engine = new JsonRpcEngine();

engine.push((request, response, next, end) => {
const result = implementation(
request as JsonRpcRequest<ManageAccountsParameters>,
response as PendingJsonRpcResponse<ManageAccountsResult>,
next,
end,
hooks,
);

result?.catch(end);
});

const response = (await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'snap_manageAccounts',
params: {
method: 'foo',
params: { bar: 'baz' },
},
})) as JsonRpcFailure;

expect(response.error).toStrictEqual({
code: -32603,
message: 'foo',
data: {
cause: {
message: 'foo',
stack: expect.any(String),
},
},
});
});

it('throws on invalid params', async () => {
const { implementation } = manageAccountsHandler;

const hasPermission = jest.fn().mockReturnValue(true);
const handleKeyringSnapMessage = jest.fn().mockReturnValue('foo');

const hooks = {
hasPermission,
handleKeyringSnapMessage,
};

const engine = new JsonRpcEngine();

engine.push((request, response, next, end) => {
const result = implementation(
request as JsonRpcRequest<ManageAccountsParameters>,
response as PendingJsonRpcResponse<ManageAccountsResult>,
next,
end,
hooks,
);

result?.catch(end);
});

const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'snap_manageAccounts',
params: {
method: 'foo',
params: 42,
},
});

expect(response).toStrictEqual({
jsonrpc: '2.0',
id: 1,
error: {
code: -32602,
message:
'Invalid params: At path: params -- Expected the value to satisfy a union of `array | record`, but received: 42.',
stack: expect.any(String),
},
});
});
});
});
Loading
Loading