Skip to content

Commit 6cbd7c4

Browse files
Allow omitting jsonrpc and id in handleRequest (#1556)
* Slightly refactor handleRequest * Fix tests and coverage * Update shasums
1 parent 2fce138 commit 6cbd7c4

File tree

13 files changed

+28
-63
lines changed

13 files changed

+28
-63
lines changed

packages/examples/packages/bip32/snap.manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "cOJYxa17Mn160j/YH8887WOpGFVl0N0c2TK4WBfpdh0=",
10+
"shasum": "eZsL+yuk4ZRUs5mKlFUgQ/cOSxBR3LgjnuYV4pnu/mY=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/bip44/snap.manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "/tbydO725MRM49ZgIJ/B9mKSysfVRxDL8JulEYLcvz4=",
10+
"shasum": "uX4m5soXlVN7UYHzLL/Ushqc8HdD3HDi0feLkl7vOOo=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/dialogs/snap.manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "VGoLdBnY9aOihxKIC243QpxbSd7N54D2JtSUwCwj/Go=",
10+
"shasum": "x7nzQYgkmWOAQ46WfBl/RE7R6QbLdg+3Cem52I/AgrI=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/get-entropy/snap.manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "8LTCy3FTdNiSavFDKD/vtBC2dIDHF1KnO+oWGp/frdc=",
10+
"shasum": "HgZvE/n41ozPudgvCEmFdGc30cW50H/PZkrzCp4gFNU=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/invoke-snap/packages/core-signer/snap.manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "FiljNwk76RZIi5odcK4IzZ6CJh9qA6OZo4iZTSFh40A=",
10+
"shasum": "Nr7S8uNAt9uLcvQCeyRiwfa0YLjaeJkDIuccHUiVv4k=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/manage-state/snap.manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "G8MTnTgczLaqj0/5pSeyW9lqakzHfBd5MW8ltGwLm/4=",
10+
"shasum": "fBYptDM+etAMdZ0RLcTrDxp5utOdqVnNA9ct3sT369c=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/notifications/snap.manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "8ip4xc7sMbpic8lNDVYRUEhPK0QHR2SLPyDXCuf62m4=",
10+
"shasum": "/lLHUZ/SWHARUnhQ3KaAcyg7dEY2JLpr0SX+rWJBbHU=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/rpc-methods/jest.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ module.exports = deepmerge(baseConfig, {
1010
],
1111
coverageThreshold: {
1212
global: {
13-
branches: 86.08,
13+
branches: 85.96,
1414
functions: 100,
15-
lines: 97.75,
16-
statements: 96.39,
15+
lines: 97.95,
16+
statements: 96.57,
1717
},
1818
},
1919
});

packages/rpc-methods/src/restricted/invokeSnap.test.ts

-20
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ describe('implementation', () => {
110110
handler: 'onRpcRequest',
111111
origin: MOCK_ORIGIN,
112112
request: {
113-
jsonrpc: '2.0',
114-
id: expect.any(String),
115113
method: 'hello',
116114
params: {},
117115
},
@@ -139,24 +137,6 @@ describe('implementation', () => {
139137

140138
expect(hooks.handleSnapRpcRequest).not.toHaveBeenCalled();
141139
});
142-
143-
it('throws if request is not valid', async () => {
144-
const hooks = getMockHooks();
145-
hooks.getSnap.mockImplementation(getTruncatedSnap);
146-
const implementation = getInvokeSnapImplementation(hooks);
147-
await expect(
148-
implementation({
149-
context: { origin: MOCK_ORIGIN },
150-
method: WALLET_SNAP_PERMISSION_KEY,
151-
params: {},
152-
}),
153-
).rejects.toThrow(
154-
'Must specify a valid JSON-RPC request object as single parameter.',
155-
);
156-
157-
expect(hooks.getSnap).toHaveBeenCalledTimes(0);
158-
expect(hooks.handleSnapRpcRequest).not.toHaveBeenCalled();
159-
});
160140
});
161141

162142
describe('handleSnapInstall', () => {

packages/rpc-methods/src/restricted/invokeSnap.ts

+2-16
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ import {
1515
RequestedSnapPermissions,
1616
InstallSnapsResult,
1717
} from '@metamask/snaps-utils';
18-
import { isJsonRpcRequest, Json, NonEmptyArray } from '@metamask/utils';
18+
import { Json, NonEmptyArray } from '@metamask/utils';
1919
import { ethErrors } from 'eth-rpc-errors';
20-
import { nanoid } from 'nanoid';
2120

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

@@ -171,19 +170,6 @@ export function getInvokeSnapImplementation({
171170

172171
const { snapId, request } = params as InvokeSnapParams;
173172

174-
const rpcRequest = {
175-
jsonrpc: '2.0',
176-
id: nanoid(),
177-
...request,
178-
};
179-
180-
if (!isJsonRpcRequest(rpcRequest)) {
181-
throw ethErrors.rpc.invalidParams({
182-
message:
183-
'Must specify a valid JSON-RPC request object as single parameter.',
184-
});
185-
}
186-
187173
if (!getSnap(snapId)) {
188174
throw ethErrors.rpc.invalidRequest({
189175
message: `The snap "${snapId}" is not installed. Please install it first, before invoking the snap.`,
@@ -195,7 +181,7 @@ export function getInvokeSnapImplementation({
195181
return (await handleSnapRpcRequest({
196182
snapId,
197183
origin,
198-
request: rpcRequest,
184+
request,
199185
handler: HandlerType.OnRpcRequest,
200186
})) as Json;
201187
};
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"branches": 88.55,
2+
"branches": 88.73,
33
"functions": 95.97,
4-
"lines": 96.74,
5-
"statements": 96.39
4+
"lines": 96.8,
5+
"statements": 96.45
66
}

packages/snaps-controllers/src/snaps/SnapController.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1659,8 +1659,8 @@ describe('SnapController', () => {
16591659
}),
16601660
).rejects.toThrow(
16611661
ethErrors.rpc.invalidRequest({
1662-
message: 'Invalid "jsonrpc" property. Must be "2.0" if provided.',
1663-
data: 'kaplar',
1662+
message:
1663+
'Invalid JSON-RPC request: At path: jsonrpc -- Expected the literal `"2.0"`, but received: "kaplar".',
16641664
}),
16651665
);
16661666

packages/snaps-controllers/src/snaps/SnapController.ts

+11-12
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
} from '@metamask/snaps-utils';
6161
import {
6262
assert,
63+
assertIsJsonRpcRequest,
6364
Duration,
6465
gtRange,
6566
gtVersion,
@@ -2420,8 +2421,16 @@ export class SnapController extends BaseController<
24202421
snapId,
24212422
origin,
24222423
handler: handlerType,
2423-
request,
2424+
request: rawRequest,
24242425
}: SnapRpcHookArgs & { snapId: ValidatedSnapId }): Promise<unknown> {
2426+
const request = {
2427+
jsonrpc: '2.0',
2428+
id: nanoid(),
2429+
...rawRequest,
2430+
};
2431+
2432+
assertIsJsonRpcRequest(request);
2433+
24252434
const permissionName = handlerEndowments[handlerType];
24262435
const hasPermission = this.messagingSystem.call(
24272436
'PermissionController:hasPermission',
@@ -2526,23 +2535,13 @@ export class SnapController extends BaseController<
25262535
}
25272536
}
25282537

2529-
let _request = request;
2530-
if (!hasProperty(request, 'jsonrpc')) {
2531-
_request = { ...(request as Record<string, unknown>), jsonrpc: '2.0' };
2532-
} else if (request.jsonrpc !== '2.0') {
2533-
throw ethErrors.rpc.invalidRequest({
2534-
message: 'Invalid "jsonrpc" property. Must be "2.0" if provided.',
2535-
data: request.jsonrpc,
2536-
});
2537-
}
2538-
25392538
const timer = new Timer(this.maxRequestTime);
25402539
this.#recordSnapRpcRequestStart(snapId, request.id, timer);
25412540

25422541
const handleRpcRequestPromise = this.messagingSystem.call(
25432542
'ExecutionService:handleRpcRequest',
25442543
snapId,
2545-
{ origin, handler: handlerType, request: _request },
2544+
{ origin, handler: handlerType, request },
25462545
);
25472546

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

0 commit comments

Comments
 (0)