Skip to content

refactor(snaps-rpc-methods)!: Use messenger for restricted method implementations#3968

Merged
FrederikBolding merged 21 commits intomainfrom
fb/restricted-methods-messenger
May 8, 2026
Merged

refactor(snaps-rpc-methods)!: Use messenger for restricted method implementations#3968
FrederikBolding merged 21 commits intomainfrom
fb/restricted-methods-messenger

Conversation

@FrederikBolding
Copy link
Copy Markdown
Member

@FrederikBolding FrederikBolding commented Apr 24, 2026

Migrate the restricted method implementations to use the messenger where applicable.

https://consensyssoftware.atlassian.net/browse/WPC-995


Note

Medium Risk
Refactors multiple restricted RPC methods to use messenger-based controller actions instead of direct hook functions, which could subtly change runtime behavior and error handling across permissions, dialogs/notifications, and keyring-derived entropy. Broad test updates mitigate risk but the surface area is large and touches keyring/approval/state controllers.

Overview
Migrates restricted RPC methods to messenger-based actions. snap_dialog, snap_notify, wallet_snap, snap_manageState, and entropy/key derivation methods now accept a Messenger and call controller actions (e.g., approvals, snap interface, snap state, keyring access, rate limiting) instead of invoking dedicated hook functions.

Centralizes entropy-source access via new getMnemonic/getMnemonicSeed utilities (backed by KeyringController:withKeyring) and adds shared action type definitions in src/types.ts. Permission specification building is updated to create per-method restricted messengers using declared actionNames, and tests are rewritten accordingly (with new negative cases for invalid keyrings/entropy sources). Coverage thresholds are adjusted slightly, and snaps-simulation removes the obsolete get-mnemonic-seed and interface hook implementations/tests.

Reviewed by Cursor Bugbot for commit de25c13. Bugbot is set up for automated code reviews on this repo. Configure here.

@FrederikBolding FrederikBolding force-pushed the fb/restricted-methods-messenger branch from e80f1b8 to bd01104 Compare May 4, 2026 09:43
@FrederikBolding FrederikBolding changed the title refactor(snaps-rpc-methods): Use messenger for method implementations refactor(snaps-rpc-methods): Use messenger for restricted method implementations May 4, 2026
Comment thread packages/snaps-rpc-methods/src/restricted/dialog.ts Outdated
Comment thread packages/snaps-rpc-methods/src/restricted/dialog.ts Outdated
Comment on lines +153 to +158
actionNames: [
'ApprovalController:addRequest',
'SnapInterfaceController:createInterface',
'SnapInterfaceController:getInterface',
'SnapInterfaceController:setInterfaceDisplayed',
],
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.

Maybe to match the UI messenger:

Suggested change
actionNames: [
'ApprovalController:addRequest',
'SnapInterfaceController:createInterface',
'SnapInterfaceController:getInterface',
'SnapInterfaceController:setInterfaceDisplayed',
],
capabilities: {
actions: [
'ApprovalController:addRequest',
'SnapInterfaceController:createInterface',
'SnapInterfaceController:getInterface',
'SnapInterfaceController:setInterfaceDisplayed',
],
}

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.

createRestrictedMethodMessenger uses the actionNames naming, that's why I chose to name it like that for the specifications. Do you think it is cleaner to do: actionNames: capabilities.actions?

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.

No strong opinion, actionNames is fine with me.

@FrederikBolding FrederikBolding changed the title refactor(snaps-rpc-methods): Use messenger for restricted method implementations refactor(snaps-rpc-methods)!: Use messenger for restricted method implementations May 5, 2026
Comment thread packages/snaps-rpc-methods/src/permissions.ts
@FrederikBolding FrederikBolding force-pushed the fb/restricted-methods-messenger branch from 37fbc73 to 5aa93a9 Compare May 5, 2026 09:34
@FrederikBolding FrederikBolding marked this pull request as ready for review May 5, 2026 09:34
@FrederikBolding FrederikBolding requested a review from a team as a code owner May 5, 2026 09:34
@FrederikBolding FrederikBolding requested a review from Mrtenz May 5, 2026 09:34
Comment thread packages/snaps-rpc-methods/src/restricted/index.ts
@codecov
Copy link
Copy Markdown

codecov Bot commented May 5, 2026

Codecov Report

❌ Patch coverage is 99.07407% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 98.58%. Comparing base (dcb5bf9) to head (de25c13).

Files with missing lines Patch % Lines
...ackages/snaps-rpc-methods/src/restricted/notify.ts 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3968      +/-   ##
==========================================
+ Coverage   98.56%   98.58%   +0.02%     
==========================================
  Files         429      427       -2     
  Lines       12365    12399      +34     
  Branches     1944     1948       +4     
==========================================
+ Hits        12187    12223      +36     
+ Misses        178      176       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment on lines +269 to +274
const { content, message, title, footerLink } =
validatedParams as NotificationArgs & {
content?: string;
title?: string;
footerLink?: { href: string; text: string };
};
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.

This mess is copied from the extension, suggestions for improvements welcome 🫠

Comment thread packages/snaps-rpc-methods/src/utils.ts
Comment thread packages/snaps-rpc-methods/src/utils.ts Outdated
Comment thread packages/snaps-rpc-methods/src/utils.ts
getUnlockPromise,
getClientCryptography,
}: GetBip32EntropyMethodHooks) {
methodHooks: { getUnlockPromise, getClientCryptography },
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.

Wondering if we should add an action to the Snap controller for this, so we can fully remove the hooks?

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.

The problem with these hooks is that they are client-specific. There is no platform-agnostic handler for getUnlockPromise that can also show the popup when needed on extension.

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.

Ah, good point. That may be something to implement in the wallet service for each client, but right now this is fine.

| ManageStateMessengerActions
| NotifyMessengerActions;

export type RestrictedMethodMessenger = Messenger<
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.

Does this mean we create a messenger that has access to all the actions used by these methods? Should it be scoped to individual methods?

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.

It is narrowed to the individual methods when building the specifications for the PermissionController here: https://github.com/MetaMask/snaps/pull/3968/files#diff-610e0a55f8b1fdc2b25489c558d3338b11c4c91e5dae790f891de1e97d6109bdR84-R88

Comment thread packages/snaps-rpc-methods/src/restricted/notify.ts
Comment thread packages/snaps-rpc-methods/src/permissions.ts Outdated
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.

I don't love that we have to copy (and maintain) a bunch of types. We should definitely reconsider a messenger types package for each controller.

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.

Me neither.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit de25c13. Configure here.

Comment thread packages/snaps-rpc-methods/src/restricted/dialog.ts
@FrederikBolding FrederikBolding added this pull request to the merge queue May 8, 2026
Merged via the queue into main with commit 73b3131 May 8, 2026
129 checks passed
@FrederikBolding FrederikBolding deleted the fb/restricted-methods-messenger branch May 8, 2026 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants