Skip to content
Open
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
45 changes: 29 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,11 @@ import {
endowmentCaveatMappers,
endowmentPermissionBuilders,
} from './endowments';
import type { 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 +66,29 @@ 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,
// @ts-expect-error TypeScript is not convinced methodHooks and actionNames exist
{ targetName, specificationBuilder, methodHooks, actionNames },
Comment on lines +76 to +77
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this doesn't seem ideal 😅. I can take a look tomorrow.

) => {
if (!excludedPermissions.includes(targetName)) {
specifications[targetName] = specificationBuilder({
// @ts-expect-error The selectHooks type is wonky
methodHooks: selectHooks(hooks, methodHooks),
// @ts-expect-error Messenger type cannot be narrowed correctly
messenger: createRestrictedMethodMessenger({
namespace: targetName,
rootMessenger: messenger,
actionNames,
}),
});
}
return specifications;
},
Comment on lines +74 to +92
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions welcome for how to satisfy TypeScript here 🫠

{},
);
Loading
Loading