Skip to content

Commit ab160d2

Browse files
Add more tests
1 parent c59564f commit ab160d2

File tree

8 files changed

+224
-10
lines changed

8 files changed

+224
-10
lines changed

packages/examples/packages/browserify-plugin/snap.manifest.json

Lines changed: 1 addition & 1 deletion
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": "ssGPi4fxyZSvKOMXbVdOa/vnTx0qrq6WZWCUV91dLqo=",
10+
"shasum": "OSiIEtXrriLywqI5Ri/27eUOis8/bkC0K9YNYXX27yc=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/browserify/snap.manifest.json

Lines changed: 1 addition & 1 deletion
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": "+342Ghzfo9UpTxJgqIPOieHvqRTnf4h00s8r0DpbozY=",
10+
"shasum": "cRFWr8JAZoT/HDjmUcJe+1gNOtSQzikgwKw+WxrPjig=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"branches": 92.96,
3-
"functions": 96.56,
4-
"lines": 98.05,
5-
"statements": 97.77
2+
"branches": 93.16,
3+
"functions": 96.63,
4+
"lines": 98.1,
5+
"statements": 97.82
66
}

packages/snaps-controllers/src/multichain/MultichainRoutingController.test.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,97 @@ describe('MultichainRoutingController', () => {
198198
}),
199199
).rejects.toThrow('The method does not exist / is not available');
200200
});
201+
202+
it('throws if address resolution fails', async () => {
203+
const rootMessenger = getRootMultichainRoutingControllerMessenger();
204+
const messenger =
205+
getRestrictedMultichainRoutingControllerMessenger(rootMessenger);
206+
207+
/* eslint-disable-next-line no-new */
208+
new MultichainRoutingController({
209+
messenger,
210+
});
211+
212+
rootMessenger.registerActionHandler(
213+
'AccountsController:listMultichainAccounts',
214+
() => MOCK_SOLANA_ACCOUNTS,
215+
);
216+
217+
rootMessenger.registerActionHandler(
218+
'PermissionController:getPermissions',
219+
() => MOCK_SOLANA_SNAP_PERMISSIONS,
220+
);
221+
222+
rootMessenger.registerActionHandler(
223+
'SnapController:handleRequest',
224+
async ({ handler }) => {
225+
if (handler === HandlerType.OnKeyringRequest) {
226+
// Simulate the Snap returning a bogus address
227+
return { address: 'foo' };
228+
}
229+
throw new Error('Unmocked request');
230+
},
231+
);
232+
233+
await expect(
234+
messenger.call('MultichainRoutingController:handleRequest', {
235+
connectedAddresses: SOLANA_CONNECTED_ACCOUNTS,
236+
scope: SOLANA_CAIP2,
237+
request: {
238+
method: 'signAndSendTransaction',
239+
params: {
240+
message: 'foo',
241+
},
242+
},
243+
}),
244+
).rejects.toThrow('Internal JSON-RPC error');
245+
});
246+
247+
it('throws if address resolution returns an address that isnt available', async () => {
248+
const rootMessenger = getRootMultichainRoutingControllerMessenger();
249+
const messenger =
250+
getRestrictedMultichainRoutingControllerMessenger(rootMessenger);
251+
252+
/* eslint-disable-next-line no-new */
253+
new MultichainRoutingController({
254+
messenger,
255+
});
256+
257+
rootMessenger.registerActionHandler(
258+
'AccountsController:listMultichainAccounts',
259+
() => MOCK_SOLANA_ACCOUNTS,
260+
);
261+
262+
rootMessenger.registerActionHandler(
263+
'PermissionController:getPermissions',
264+
() => MOCK_SOLANA_SNAP_PERMISSIONS,
265+
);
266+
267+
rootMessenger.registerActionHandler(
268+
'SnapController:handleRequest',
269+
async ({ handler }) => {
270+
if (handler === HandlerType.OnKeyringRequest) {
271+
// Simulate the Snap returning an unconnected address
272+
return {
273+
address:
274+
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKa',
275+
};
276+
}
277+
throw new Error('Unmocked request');
278+
},
279+
);
280+
281+
await expect(
282+
messenger.call('MultichainRoutingController:handleRequest', {
283+
connectedAddresses: SOLANA_CONNECTED_ACCOUNTS,
284+
scope: SOLANA_CAIP2,
285+
request: {
286+
method: 'signAndSendTransaction',
287+
params: {
288+
message: 'foo',
289+
},
290+
},
291+
}),
292+
).rejects.toThrow('Invalid method parameter(s)');
293+
});
201294
});

packages/snaps-execution-environments/src/common/BaseSnapExecutor.test.browser.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,48 @@ describe('BaseSnapExecutor', () => {
16001600
});
16011601
});
16021602

1603+
it('supports onProtocolRequest export', async () => {
1604+
const CODE = `
1605+
module.exports.onProtocolRequest = ({ origin, scope, request }) => ({ origin, scope, request })
1606+
`;
1607+
1608+
const executor = new TestSnapExecutor();
1609+
await executor.executeSnap(1, MOCK_SNAP_ID, CODE, []);
1610+
1611+
expect(await executor.readCommand()).toStrictEqual({
1612+
jsonrpc: '2.0',
1613+
id: 1,
1614+
result: 'OK',
1615+
});
1616+
1617+
const params = {
1618+
scope: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
1619+
request: {
1620+
jsonrpc: '2.0',
1621+
id: 'foo',
1622+
method: 'getVersion',
1623+
},
1624+
};
1625+
1626+
await executor.writeCommand({
1627+
jsonrpc: '2.0',
1628+
id: 2,
1629+
method: 'snapRpc',
1630+
params: [
1631+
MOCK_SNAP_ID,
1632+
HandlerType.OnProtocolRequest,
1633+
MOCK_ORIGIN,
1634+
{ jsonrpc: '2.0', method: '', params },
1635+
],
1636+
});
1637+
1638+
expect(await executor.readCommand()).toStrictEqual({
1639+
id: 2,
1640+
jsonrpc: '2.0',
1641+
result: { origin: MOCK_ORIGIN, ...params },
1642+
});
1643+
});
1644+
16031645
describe('lifecycle hooks', () => {
16041646
const LIFECYCLE_HOOKS = [HandlerType.OnInstall, HandlerType.OnUpdate];
16051647

packages/snaps-execution-environments/src/common/validation.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { UserInputEventType } from '@metamask/snaps-sdk';
22

33
import {
44
assertIsOnNameLookupRequestArguments,
5+
assertIsOnProtocolRequestArguments,
56
assertIsOnSignatureRequestArguments,
67
assertIsOnTransactionRequestArguments,
78
assertIsOnUserInputRequestArguments,
@@ -232,3 +233,68 @@ describe('assertIsOnUserInputRequestArguments', () => {
232233
);
233234
});
234235
});
236+
237+
describe('assertIsOnProtocolRequestArguments', () => {
238+
it.each([
239+
{
240+
scope: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
241+
request: {
242+
jsonrpc: '2.0',
243+
id: 'foo',
244+
method: 'getVersion',
245+
},
246+
},
247+
{
248+
scope: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
249+
request: {
250+
jsonrpc: '2.0',
251+
id: 'foo',
252+
method: 'getVersion',
253+
params: {
254+
foo: 'bar',
255+
},
256+
},
257+
},
258+
])('does not throw for a valid protocol request param object', (value) => {
259+
expect(() => assertIsOnProtocolRequestArguments(value)).not.toThrow();
260+
});
261+
262+
it.each([
263+
true,
264+
false,
265+
null,
266+
undefined,
267+
0,
268+
1,
269+
'',
270+
'foo',
271+
[],
272+
{},
273+
{ request: { foo: 'bar' } },
274+
{
275+
request: {
276+
jsonrpc: '2.0',
277+
id: 'foo',
278+
method: 'getVersion',
279+
params: {
280+
foo: 'bar',
281+
},
282+
},
283+
},
284+
{
285+
scope: 'foo',
286+
request: {
287+
jsonrpc: '2.0',
288+
id: 'foo',
289+
method: 'getVersion',
290+
},
291+
},
292+
])(
293+
'throws if the value is not a valid protocol request params object',
294+
(value) => {
295+
expect(() => assertIsOnProtocolRequestArguments(value as any)).toThrow(
296+
'Invalid request params:',
297+
);
298+
},
299+
);
300+
});

packages/snaps-rpc-methods/jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ module.exports = deepmerge(baseConfig, {
1010
],
1111
coverageThreshold: {
1212
global: {
13-
branches: 93.97,
14-
functions: 98.05,
15-
lines: 98.67,
16-
statements: 98.25,
13+
branches: 94.17,
14+
functions: 98.59,
15+
lines: 98.78,
16+
statements: 98.37,
1717
},
1818
},
1919
});

packages/snaps-simulation/src/methods/specifications.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ describe('getPermissionSpecifications', () => {
133133
],
134134
"targetName": "endowment:page-settings",
135135
},
136+
"endowment:protocol": {
137+
"allowedCaveats": [
138+
"protocolSnapScopes",
139+
"maxRequestTime",
140+
],
141+
"endowmentGetter": [Function],
142+
"permissionType": "Endowment",
143+
"subjectTypes": [
144+
"snap",
145+
],
146+
"targetName": "endowment:protocol",
147+
"validator": [Function],
148+
},
136149
"endowment:rpc": {
137150
"allowedCaveats": [
138151
"rpcOrigin",

0 commit comments

Comments
 (0)