Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5014,7 +5014,7 @@ describe('SnapController', () => {
[MOCK_SNAP_ID]: {},
}),
).rejects.toThrow(
'A snap must request at least one of the following permissions: endowment:rpc, endowment:transaction-insight, endowment:cronjob, endowment:name-lookup, endowment:lifecycle-hooks, endowment:keyring, endowment:page-home, endowment:signature-insight.',
'A snap must request at least one of the following permissions: endowment:rpc, endowment:transaction-insight, endowment:cronjob, endowment:name-lookup, endowment:lifecycle-hooks, endowment:keyring, endowment:page-home, endowment:signature-insight, endowment:protocol.',
);

controller.destroy();
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-execution-environments/coverage.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"branches": 80,
"branches": 80.13,
"functions": 90.06,
"lines": 90.77,
"statements": 90.15
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,39 @@ describe('BaseSnapExecutor', () => {
});
});

it('supports onProtocolRequest export', async () => {
const CODE = `
module.exports.onProtocolRequest = ({ request }) => request.params[0];
`;

const executor = new TestSnapExecutor();
await executor.executeSnap(1, MOCK_SNAP_ID, CODE, []);

expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
id: 1,
result: 'OK',
});

await executor.writeCommand({
jsonrpc: '2.0',
id: 2,
method: 'snapRpc',
params: [
MOCK_SNAP_ID,
HandlerType.OnProtocolRequest,
MOCK_ORIGIN,
{ jsonrpc: '2.0', method: 'foo', params: ['bar'] },
],
});

expect(await executor.readCommand()).toStrictEqual({
id: 2,
jsonrpc: '2.0',
result: 'bar',
});
});

it('supports onHomePage export', async () => {
const CODE = `
module.exports.onHomePage = () => ({ content: { type: 'panel', children: [] }});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export function getHandlerArguments(
}
case HandlerType.OnRpcRequest:
case HandlerType.OnKeyringRequest:
case HandlerType.OnProtocolRequest:
return { origin, request };

case HandlerType.OnCronjob:
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-rpc-methods/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = deepmerge(baseConfig, {
],
coverageThreshold: {
global: {
branches: 91.21,
branches: 90.95,
functions: 96.96,
lines: 97.51,
statements: 96.97,
Expand Down
1 change: 1 addition & 0 deletions packages/snaps-rpc-methods/src/endowments/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export enum SnapEndowments {
LifecycleHooks = 'endowment:lifecycle-hooks',
Keyring = 'endowment:keyring',
HomePage = 'endowment:page-home',
Protocol = 'endowment:protocol',
}
11 changes: 11 additions & 0 deletions packages/snaps-rpc-methods/src/endowments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
nameLookupEndowmentBuilder,
} from './name-lookup';
import { networkAccessEndowmentBuilder } from './network-access';
import { getProtocolCaveatMapper, protocolEndowmentBuilder } from './protocol';
import {
getRpcCaveatMapper,
rpcCaveatSpecifications,
Expand Down Expand Up @@ -55,6 +56,7 @@ export const endowmentPermissionBuilders = {
[nameLookupEndowmentBuilder.targetName]: nameLookupEndowmentBuilder,
[lifecycleHooksEndowmentBuilder.targetName]: lifecycleHooksEndowmentBuilder,
[keyringEndowmentBuilder.targetName]: keyringEndowmentBuilder,
[protocolEndowmentBuilder.targetName]: protocolEndowmentBuilder,
[homePageEndowmentBuilder.targetName]: homePageEndowmentBuilder,
[signatureInsightEndowmentBuilder.targetName]:
signatureInsightEndowmentBuilder,
Expand Down Expand Up @@ -88,6 +90,9 @@ export const endowmentCaveatMappers: Record<
[keyringEndowmentBuilder.targetName]: createMaxRequestTimeMapper(
getKeyringCaveatMapper,
),
[protocolEndowmentBuilder.targetName]: createMaxRequestTimeMapper(
getProtocolCaveatMapper,
),
[signatureInsightEndowmentBuilder.targetName]: createMaxRequestTimeMapper(
getSignatureInsightCaveatMapper,
),
Expand All @@ -106,6 +111,7 @@ export const handlerEndowments: Record<HandlerType, string | null> = {
[HandlerType.OnKeyringRequest]: keyringEndowmentBuilder.targetName,
[HandlerType.OnHomePage]: homePageEndowmentBuilder.targetName,
[HandlerType.OnSignature]: signatureInsightEndowmentBuilder.targetName,
[HandlerType.OnProtocolRequest]: protocolEndowmentBuilder.targetName,
[HandlerType.OnUserInput]: null,
};

Expand All @@ -117,3 +123,8 @@ export { getChainIdsCaveat, getLookupMatchersCaveat } from './name-lookup';
export { getKeyringCaveatOrigins } from './keyring';
export { getMaxRequestTimeCaveat } from './caveats';
export { getCronjobCaveatJobs } from './cronjob';
export {
getProtocolCaveatChainIds,
getProtocolCaveatOrigins,
getProtocolCaveatRpcMethods,
} from './protocol';
131 changes: 131 additions & 0 deletions packages/snaps-rpc-methods/src/endowments/protocol.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { PermissionType, SubjectType } from '@metamask/permission-controller';
import { SnapCaveatType } from '@metamask/snaps-utils';

import { SnapEndowments } from './enum';
import {
getProtocolCaveatChainIds,
getProtocolCaveatMapper,
getProtocolCaveatOrigins,
protocolEndowmentBuilder,
} from './protocol';

describe('endowment:protocol', () => {
it('builds the expected permission specification', () => {
const specification = protocolEndowmentBuilder.specificationBuilder({});
expect(specification).toStrictEqual({
permissionType: PermissionType.Endowment,
targetName: SnapEndowments.Protocol,
endowmentGetter: expect.any(Function),
allowedCaveats: [
SnapCaveatType.KeyringOrigin,
SnapCaveatType.ChainIds,
SnapCaveatType.MaxRequestTime,
],
subjectTypes: [SubjectType.Snap],
validator: expect.any(Function),
});

expect(specification.endowmentGetter()).toBeUndefined();
});

describe('validator', () => {
it('throws if the caveat is not a "keyringOrigin" and "chainIds"', () => {
const specification = protocolEndowmentBuilder.specificationBuilder({});

expect(() =>
specification.validator({
// @ts-expect-error Missing other required permission types.
caveats: undefined,
}),
).toThrow('Expected the following caveats: "keyringOrigin", "chainIds".');

expect(() =>
// @ts-expect-error Missing other required permission types.
specification.validator({
caveats: [{ type: 'foo', value: 'bar' }],
}),
).toThrow(
'Expected the following caveats: "keyringOrigin", "chainIds", "maxRequestTime", received "foo".',
);

expect(() =>
// @ts-expect-error Missing other required permission types.
specification.validator({
caveats: [
{ type: 'keyringOrigin', value: { allowedOrgins: ['foo.com'] } },
],
}),
).toThrow('Expected the following caveats: "keyringOrigin", "chainIds".');

expect(() =>
// @ts-expect-error Missing other required permission types.
specification.validator({
caveats: [
{ type: 'keyringOrigin', value: { allowedOrgins: ['foo.com'] } },
{ type: 'keyringOrigin', value: { allowedOrgins: ['bar.com'] } },
],
}),
).toThrow('Duplicate caveats are not allowed.');
});
});
});

describe('getProtocolCaveatMapper', () => {
it('maps a value to a caveat', () => {
expect(
getProtocolCaveatMapper({
allowedOrigins: ['foo.com'],
chains: ['bip122:000000000019d6689c085ae165831e93'],
}),
).toStrictEqual({
caveats: [
{
type: SnapCaveatType.ChainIds,
value: ['bip122:000000000019d6689c085ae165831e93'],
},
{
type: SnapCaveatType.KeyringOrigin,
value: { allowedOrigins: ['foo.com'] },
},
],
});
});

it('returns null if the input is null', () => {
expect(getProtocolCaveatMapper(null)).toStrictEqual({
caveats: null,
});
});
});

describe('getProtocolCaveatOrigins', () => {
it('returns the origins from the caveat', () => {
expect(
// @ts-expect-error Missing other required permission types.
getProtocolCaveatOrigins({
caveats: [
{
type: SnapCaveatType.KeyringOrigin,
value: { allowedOrigins: ['foo.com'] },
},
],
}),
).toStrictEqual({ allowedOrigins: ['foo.com'] });
});
});

describe('getProtocolCaveatChainIds', () => {
it('returns the chain ids from the caveat', () => {
expect(
// @ts-expect-error Missing other required permission types.
getProtocolCaveatChainIds({
caveats: [
{
type: SnapCaveatType.ChainIds,
value: ['bip122:000000000019d6689c085ae165831e93'],
},
],
}),
).toStrictEqual(['bip122:000000000019d6689c085ae165831e93']);
});
});
Loading