Skip to content

refactor!: Add consolidated legacy method middleware#8583

Open
rekmarks wants to merge 17 commits intomainfrom
rekm/v2-permitted-handlers
Open

refactor!: Add consolidated legacy method middleware#8583
rekmarks wants to merge 17 commits intomainfrom
rekm/v2-permitted-handlers

Conversation

@rekmarks
Copy link
Copy Markdown
Member

@rekmarks rekmarks commented Apr 24, 2026

Explanation

Consolidates the bespoke makeMethodMiddlewareMaker implementations from the
MetaMask extension and mobile clients into a single legacy createMethodMiddleware
exported from @metamask/json-rpc-engine, and removes the unused permitted
method handlers from @metamask/permission-controller.

@metamask/json-rpc-engine

  • Adds a legacy createMethodMiddleware that builds a JsonRpcEngine middleware
    from a record of handlers keyed by method name. Hooks are validated against the
    union of hookNames declared by the handlers (rejecting both missing and
    extraneous hooks), and incoming requests are dispatched by method.
  • Handlers may declare actionNames and receive a per-handler delegated
    Messenger as the sixth argument to implementation, mirroring the v2
    createMethodMiddleware.
  • The legacy and v2 versions share their hook selection / validation and
    messenger delegation utilities (assertExpectedHooks, selectHooks,
    createHandlerMessenger).
  • The legacy export is deprecated in favor of the v2 createMethodMiddleware.
    It exists only to give the extension and mobile clients a consolidated
    migration target before they move to JsonRpcEngineV2.

@metamask/eip1193-permission-middleware (BREAKING)

  • Reshapes the per-method exports (getPermissionsHandler,
    requestPermissionsHandler, revokePermissionsHandler) into the new
    MethodHandler format and consolidates them into a single methodHandlers
    export keyed by method name. The individual handler exports are removed.

@metamask/multichain-api-middleware (BREAKING)

  • Reshapes the per-method exports (walletCreateSession, walletGetSession,
    walletInvokeMethod, walletRevokeSession) into the new MethodHandler
    format and consolidates them into a single methodHandlers export keyed by
    method name. The individual handler exports are removed.

@metamask/permission-controller (BREAKING)

  • Removes the permitted RPC method handler modules (getPermissions,
    requestPermissions, revokePermissions) and the related
    permissionRpcMethods and PermittedHandlerExport exports. These were unused
    in practice. Replacement types are available in
    @metamask/json-rpc-engine@10.3.0.

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Medium Risk
Introduces new JSON-RPC dispatch middleware and reshapes multiple public package exports (breaking), so downstream integrations may break and runtime hook validation/dispatch paths could affect request handling.

Overview
Adds a deprecated legacy createMethodMiddleware export to @metamask/json-rpc-engine that dispatches requests by req.method, validates hooks (missing/extraneous), optionally delegates a per-handler messenger via declared actionNames, and normalizes thrown non-Error values into JSON-RPC internal errors.

Introduces breaking API shape changes in @metamask/eip1193-permission-middleware and @metamask/multichain-api-middleware by consolidating per-method exports into a single methodHandlers record keyed by method name, updating handlers to the new MethodHandler format, and adding tests that construct middleware from these handlers; wallet_createSession now requires a trackSessionCreatedEvent hook (nullable).

Removes unused permitted RPC method handler modules/types from @metamask/permission-controller (including permissionRpcMethods and PermittedHandlerExport) and updates v2 JSON-RPC engine utilities to centralize hook selection/validation and messenger delegation; changelogs and lint suppressions are updated accordingly.

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

rekmarks and others added 3 commits April 24, 2026 13:32
Add `createMethodMiddlewareFactory` to the legacy (v1) surface,
consolidating the near-identical `makeMethodMiddlewareMaker`
implementations from metamask-extension and metamask-mobile. The new
export is deprecated in favor of the v2 `createMethodMiddleware`.

Extract `selectHooks` and `assertExpectedHooks` into a shared
`hookUtils.ts` module used by both v1 and v2. v2 now performs the same
strict missing/extraneous hook validation as v1 at middleware
construction time.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@rekmarks rekmarks changed the title refactor!: Consolidate legacy method middleware implementation refactor!: Add consolidated legacy method middleware Apr 24, 2026
@rekmarks

This comment was marked as resolved.

cursor[bot]

This comment was marked as resolved.

Comment thread packages/permission-controller/CHANGELOG.md Outdated
Comment thread packages/json-rpc-engine/src/v2/index.ts
Comment thread packages/json-rpc-engine/src/createMethodMiddleware.ts Outdated
Comment thread packages/json-rpc-engine/src/createMethodMiddleware.test.ts Outdated
rekmarks and others added 4 commits April 27, 2026 09:50
…thod middleware

Allow `Messenger<string, never>` in the no-actions branch of
`CreateMethodMiddlewareFactoryOptions` so callers may still pass a
no-op root messenger when handlers declare no actions.

Pre-select hooks per handler at curried-call time and store them on a
`ResolvedHandler` entry, mirroring v2's dispatch shape and removing
`selectHooks` from the per-request path.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@rekmarks

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

Comment thread packages/json-rpc-engine/src/createMethodMiddleware.ts Outdated
Comment thread packages/json-rpc-engine/src/createMethodMiddleware.test.ts Outdated
Comment thread packages/json-rpc-engine/src/createMethodMiddleware.test.ts Outdated
Comment thread packages/json-rpc-engine/src/createMethodMiddleware.ts Outdated
Overload createMethodMiddleware (v1 and v2) so messenger is optional when
handlers do not use messenger actions. Move actionNames/root validation
into createHandlerMessenger; add tests for no-messenger and missing-root
cases.

Made-with: Cursor
Add a fifth generic to MethodHandlerImplementation and MethodHandler so
callers can type non-standard JSON-RPC request fields (e.g. origin) without
widening JsonRpcEngine types. Extras are optionalized on the request
parameter so middleware can pass a plain JsonRpcRequest. Add a test and
changelog note.

Made-with: Cursor
@rekmarks rekmarks marked this pull request as ready for review April 28, 2026 21:21
@rekmarks rekmarks requested a review from a team as a code owner April 28, 2026 21:21
@rekmarks rekmarks requested a review from a team as a code owner April 28, 2026 21:21
@rekmarks
Copy link
Copy Markdown
Member Author

@cursor review

@rekmarks
Copy link
Copy Markdown
Member Author

@metamaskbot publish-preview

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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 45a82ea. Configure here.

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 86dd857. Configure here.

Comment thread packages/multichain-api-middleware/src/handlers/wallet-createSession.ts Outdated
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