Skip to content
Merged
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
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: 96.68,
functions: 99.2,
lines: 99.06,
statements: 98.78,
branches: 97.28,
functions: 98.84,
lines: 99.14,
statements: 98.81,
},
},
});
8 changes: 7 additions & 1 deletion packages/snaps-rpc-methods/src/permissions.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Messenger } from '@metamask/messenger';

import {
buildSnapEndowmentSpecifications,
buildSnapRestrictedMethodSpecifications,
Expand Down Expand Up @@ -193,7 +195,11 @@ describe('buildSnapEndowmentSpecifications', () => {

describe('buildSnapRestrictedMethodSpecifications', () => {
it('returns the expected object', () => {
const specifications = buildSnapRestrictedMethodSpecifications([], {});
const specifications = buildSnapRestrictedMethodSpecifications(
[],
{},
new Messenger({ namespace: 'SnapsRestrictedMethods' }),
);
expect(specifications).toMatchInlineSnapshot(`
{
"snap_dialog": {
Expand Down
47 changes: 31 additions & 16 deletions packages/snaps-rpc-methods/src/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type {
PermissionConstraint,
PermissionSpecificationConstraint,
import { selectHooks } from '@metamask/json-rpc-engine/v2';
import {
createRestrictedMethodMessenger,
type PermissionConstraint,
type PermissionSpecificationConstraint,
} from '@metamask/permission-controller';
import type { SnapPermissions } from '@metamask/snaps-utils';
import { hasProperty } from '@metamask/utils';
Expand All @@ -9,11 +11,14 @@ import {
endowmentCaveatMappers,
endowmentPermissionBuilders,
} from './endowments';
import type {
RestrictedMethodActions,
RestrictedMethodMessenger,
} from './restricted';
import {
caveatMappers,
restrictedMethodPermissionBuilders,
} from './restricted';
import { selectHooks } from './utils';

/**
* Map initial permissions as defined in a Snap manifest to something that can
Expand Down Expand Up @@ -64,18 +69,28 @@ export const buildSnapEndowmentSpecifications = (
export const buildSnapRestrictedMethodSpecifications = (
excludedPermissions: string[],
hooks: Record<string, unknown>,
messenger: RestrictedMethodMessenger,
) =>
Object.values(restrictedMethodPermissionBuilders).reduce<
Record<string, PermissionSpecificationConstraint>
>((specifications, { targetName, specificationBuilder, methodHooks }) => {
if (!excludedPermissions.includes(targetName)) {
specifications[targetName] = specificationBuilder({
// @ts-expect-error The selectHooks type is wonky
methodHooks: selectHooks<typeof hooks, keyof typeof methodHooks>(
hooks,
methodHooks,
) as Pick<typeof hooks, keyof typeof methodHooks>,
});
}
return specifications;
}, {});
>(
(
specifications,
{ targetName, specificationBuilder, methodHooks, actionNames },
) => {
if (!excludedPermissions.includes(targetName)) {
specifications[targetName] = specificationBuilder({
methodHooks: selectHooks(hooks, methodHooks),
messenger: createRestrictedMethodMessenger({
namespace: targetName,
rootMessenger: messenger,
actionNames: actionNames as readonly [
RestrictedMethodActions['type'],
],
}),
});
}
return specifications;
},
Comment thread
FrederikBolding marked this conversation as resolved.
{},
);
Loading
Loading