refactor!: Add consolidated legacy method middleware#8583
Open
refactor!: Add consolidated legacy method middleware#8583
Conversation
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>
This comment was marked as resolved.
This comment was marked as resolved.
…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>
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as 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
Member
Author
|
@cursor review |
Member
Author
|
@metamaskbot publish-preview |
There was a problem hiding this comment.
✅ 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.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Explanation
Consolidates the bespoke
makeMethodMiddlewareMakerimplementations from theMetaMask extension and mobile clients into a single legacy
createMethodMiddlewareexported from
@metamask/json-rpc-engine, and removes the unused permittedmethod handlers from
@metamask/permission-controller.@metamask/json-rpc-enginecreateMethodMiddlewarethat builds aJsonRpcEnginemiddlewarefrom a record of handlers keyed by method name. Hooks are validated against the
union of
hookNamesdeclared by the handlers (rejecting both missing andextraneous hooks), and incoming requests are dispatched by
method.actionNamesand receive a per-handler delegatedMessengeras the sixth argument toimplementation, mirroring the v2createMethodMiddleware.messenger delegation utilities (
assertExpectedHooks,selectHooks,createHandlerMessenger).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)getPermissionsHandler,requestPermissionsHandler,revokePermissionsHandler) into the newMethodHandlerformat and consolidates them into a singlemethodHandlersexport keyed by method name. The individual handler exports are removed.
@metamask/multichain-api-middleware(BREAKING)walletCreateSession,walletGetSession,walletInvokeMethod,walletRevokeSession) into the newMethodHandlerformat and consolidates them into a single
methodHandlersexport keyed bymethod name. The individual handler exports are removed.
@metamask/permission-controller(BREAKING)getPermissions,requestPermissions,revokePermissions) and the relatedpermissionRpcMethodsandPermittedHandlerExportexports. These were unusedin practice. Replacement types are available in
@metamask/json-rpc-engine@10.3.0.References
JsonRpcEngineV2#6327Checklist
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
createMethodMiddlewareexport to@metamask/json-rpc-enginethat dispatches requests byreq.method, validates hooks (missing/extraneous), optionally delegates a per-handler messenger via declaredactionNames, and normalizes thrown non-Errorvalues into JSON-RPC internal errors.Introduces breaking API shape changes in
@metamask/eip1193-permission-middlewareand@metamask/multichain-api-middlewareby consolidating per-method exports into a singlemethodHandlersrecord keyed by method name, updating handlers to the newMethodHandlerformat, and adding tests that construct middleware from these handlers;wallet_createSessionnow requires atrackSessionCreatedEventhook (nullable).Removes unused permitted RPC method handler modules/types from
@metamask/permission-controller(includingpermissionRpcMethodsandPermittedHandlerExport) 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.