Skip to content

Commit

Permalink
Allow omitting jsonrpc and id in handleRequest (#1556)
Browse files Browse the repository at this point in the history
* Slightly refactor handleRequest

* Fix tests and coverage

* Update shasums
  • Loading branch information
FrederikBolding committed Jun 30, 2023
1 parent 2fce138 commit 6cbd7c4
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 63 deletions.
2 changes: 1 addition & 1 deletion packages/examples/packages/bip32/snap.manifest.json
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "cOJYxa17Mn160j/YH8887WOpGFVl0N0c2TK4WBfpdh0=",
"shasum": "eZsL+yuk4ZRUs5mKlFUgQ/cOSxBR3LgjnuYV4pnu/mY=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/bip44/snap.manifest.json
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "/tbydO725MRM49ZgIJ/B9mKSysfVRxDL8JulEYLcvz4=",
"shasum": "uX4m5soXlVN7UYHzLL/Ushqc8HdD3HDi0feLkl7vOOo=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/dialogs/snap.manifest.json
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "VGoLdBnY9aOihxKIC243QpxbSd7N54D2JtSUwCwj/Go=",
"shasum": "x7nzQYgkmWOAQ46WfBl/RE7R6QbLdg+3Cem52I/AgrI=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/get-entropy/snap.manifest.json
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "8LTCy3FTdNiSavFDKD/vtBC2dIDHF1KnO+oWGp/frdc=",
"shasum": "HgZvE/n41ozPudgvCEmFdGc30cW50H/PZkrzCp4gFNU=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "FiljNwk76RZIi5odcK4IzZ6CJh9qA6OZo4iZTSFh40A=",
"shasum": "Nr7S8uNAt9uLcvQCeyRiwfa0YLjaeJkDIuccHUiVv4k=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/manage-state/snap.manifest.json
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "G8MTnTgczLaqj0/5pSeyW9lqakzHfBd5MW8ltGwLm/4=",
"shasum": "fBYptDM+etAMdZ0RLcTrDxp5utOdqVnNA9ct3sT369c=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "8ip4xc7sMbpic8lNDVYRUEhPK0QHR2SLPyDXCuf62m4=",
"shasum": "/lLHUZ/SWHARUnhQ3KaAcyg7dEY2JLpr0SX+rWJBbHU=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc-methods/jest.config.js
Expand Up @@ -10,10 +10,10 @@ module.exports = deepmerge(baseConfig, {
],
coverageThreshold: {
global: {
branches: 86.08,
branches: 85.96,
functions: 100,
lines: 97.75,
statements: 96.39,
lines: 97.95,
statements: 96.57,
},
},
});
20 changes: 0 additions & 20 deletions packages/rpc-methods/src/restricted/invokeSnap.test.ts
Expand Up @@ -110,8 +110,6 @@ describe('implementation', () => {
handler: 'onRpcRequest',
origin: MOCK_ORIGIN,
request: {
jsonrpc: '2.0',
id: expect.any(String),
method: 'hello',
params: {},
},
Expand Down Expand Up @@ -139,24 +137,6 @@ describe('implementation', () => {

expect(hooks.handleSnapRpcRequest).not.toHaveBeenCalled();
});

it('throws if request is not valid', async () => {
const hooks = getMockHooks();
hooks.getSnap.mockImplementation(getTruncatedSnap);
const implementation = getInvokeSnapImplementation(hooks);
await expect(
implementation({
context: { origin: MOCK_ORIGIN },
method: WALLET_SNAP_PERMISSION_KEY,
params: {},
}),
).rejects.toThrow(
'Must specify a valid JSON-RPC request object as single parameter.',
);

expect(hooks.getSnap).toHaveBeenCalledTimes(0);
expect(hooks.handleSnapRpcRequest).not.toHaveBeenCalled();
});
});

describe('handleSnapInstall', () => {
Expand Down
18 changes: 2 additions & 16 deletions packages/rpc-methods/src/restricted/invokeSnap.ts
Expand Up @@ -15,9 +15,8 @@ import {
RequestedSnapPermissions,
InstallSnapsResult,
} from '@metamask/snaps-utils';
import { isJsonRpcRequest, Json, NonEmptyArray } from '@metamask/utils';
import { Json, NonEmptyArray } from '@metamask/utils';
import { ethErrors } from 'eth-rpc-errors';
import { nanoid } from 'nanoid';

import { MethodHooksObject } from '../utils';

Expand Down Expand Up @@ -171,19 +170,6 @@ export function getInvokeSnapImplementation({

const { snapId, request } = params as InvokeSnapParams;

const rpcRequest = {
jsonrpc: '2.0',
id: nanoid(),
...request,
};

if (!isJsonRpcRequest(rpcRequest)) {
throw ethErrors.rpc.invalidParams({
message:
'Must specify a valid JSON-RPC request object as single parameter.',
});
}

if (!getSnap(snapId)) {
throw ethErrors.rpc.invalidRequest({
message: `The snap "${snapId}" is not installed. Please install it first, before invoking the snap.`,
Expand All @@ -195,7 +181,7 @@ export function getInvokeSnapImplementation({
return (await handleSnapRpcRequest({
snapId,
origin,
request: rpcRequest,
request,
handler: HandlerType.OnRpcRequest,
})) as Json;
};
Expand Down
6 changes: 3 additions & 3 deletions packages/snaps-controllers/coverage.json
@@ -1,6 +1,6 @@
{
"branches": 88.55,
"branches": 88.73,
"functions": 95.97,
"lines": 96.74,
"statements": 96.39
"lines": 96.8,
"statements": 96.45
}
4 changes: 2 additions & 2 deletions packages/snaps-controllers/src/snaps/SnapController.test.ts
Expand Up @@ -1659,8 +1659,8 @@ describe('SnapController', () => {
}),
).rejects.toThrow(
ethErrors.rpc.invalidRequest({
message: 'Invalid "jsonrpc" property. Must be "2.0" if provided.',
data: 'kaplar',
message:
'Invalid JSON-RPC request: At path: jsonrpc -- Expected the literal `"2.0"`, but received: "kaplar".',
}),
);

Expand Down
23 changes: 11 additions & 12 deletions packages/snaps-controllers/src/snaps/SnapController.ts
Expand Up @@ -60,6 +60,7 @@ import {
} from '@metamask/snaps-utils';
import {
assert,
assertIsJsonRpcRequest,
Duration,
gtRange,
gtVersion,
Expand Down Expand Up @@ -2420,8 +2421,16 @@ export class SnapController extends BaseController<
snapId,
origin,
handler: handlerType,
request,
request: rawRequest,
}: SnapRpcHookArgs & { snapId: ValidatedSnapId }): Promise<unknown> {
const request = {
jsonrpc: '2.0',
id: nanoid(),
...rawRequest,
};

assertIsJsonRpcRequest(request);

const permissionName = handlerEndowments[handlerType];
const hasPermission = this.messagingSystem.call(
'PermissionController:hasPermission',
Expand Down Expand Up @@ -2526,23 +2535,13 @@ export class SnapController extends BaseController<
}
}

let _request = request;
if (!hasProperty(request, 'jsonrpc')) {
_request = { ...(request as Record<string, unknown>), jsonrpc: '2.0' };
} else if (request.jsonrpc !== '2.0') {
throw ethErrors.rpc.invalidRequest({
message: 'Invalid "jsonrpc" property. Must be "2.0" if provided.',
data: request.jsonrpc,
});
}

const timer = new Timer(this.maxRequestTime);
this.#recordSnapRpcRequestStart(snapId, request.id, timer);

const handleRpcRequestPromise = this.messagingSystem.call(
'ExecutionService:handleRpcRequest',
snapId,
{ origin, handler: handlerType, request: _request },
{ origin, handler: handlerType, request },
);

// This will either get the result or reject due to the timeout.
Expand Down

0 comments on commit 6cbd7c4

Please sign in to comment.