refactor!: Add consolidated legacy method middleware#8583
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 <[email protected]>
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 <[email protected]>
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
|
@cursor review |
|
@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.
| const middleware = createMethodMiddleware({ | ||
| handlers: { callAction: messengerHandler }, | ||
| messenger: rootMessenger, | ||
| hooks: {}, |
There was a problem hiding this comment.
I'm unsure exactly why but the inference does not work as well as in the v2 version, for example here if you add extraneous hooks you do not get a type error.
There was a problem hiding this comment.
I wonder why we aren't using this package in the clients 🤔
There was a problem hiding this comment.
Great question. Didn't realize that. @jiexi do you know the status of this package?
There was a problem hiding this comment.
The package is used in mobile, but its integration was overlooked in the extension. It can be added there once the other changes in this PR have been integrated.
Type the `hooks` property as `Record<string, never>` (rather than
collapsing to `unknown` via `UnionToIntersection<never>`) when no
handler declares hooks. This forces callers to spell out `hooks: {}`
and prevents TypeScript from co-resolving the shared `Handlers` generic
in a way that also relaxes the `messenger` requirement.
Co-Authored-By: Claude Opus 4.7 <[email protected]>
| | readonly HandlerActions<Handlers[keyof Handlers]>['type'][] | ||
| | undefined, |
There was a problem hiding this comment.
Ahh i see. Because it's too early to type this in AnyMethodHandler itself. fun
|
nit with the naming of the handler type. For example This is awkward to me because on first read, |
Rename the per-method handler constants to `<methodName>Handler` (e.g. `getPermissions` → `getPermissionsHandler`, `walletCreateSession` → `walletCreateSessionHandler`) and export their corresponding type aliases. Drop the per-file `Record<methodName, handler>` wrappers; the consolidated `methodHandlers` map is now built directly in each package's index, using computed property keys from a `MethodNames` const object (or the existing enum, in `eip1193-permission-middleware`) to satisfy the naming-convention lint rule. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
@metamaskbot publish-preview |

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/[email protected].References
JsonRpcEngineV2#6327Checklist
Note
High Risk
Breaking export and typing changes across multiple published packages plus new middleware dispatch/validation logic could impact downstream integrations and runtime hook wiring.
Overview
Adds a deprecated legacy
createMethodMiddlewareexport to@metamask/json-rpc-enginethat dispatches JSON-RPC requests byreq.method, enforces exact hook sets (missing/extraneous), and optionally delegates a per-handlerMessengerwhenactionNamesare declared; shared utilities (selectHooks,assertExpectedHooks,createHandlerMessenger) are centralized inv2/utilsand v2 middleware is updated to support no-messenger/no-hooks handlers.Introduces breaking API reshapes in
@metamask/eip1193-permission-middlewareand@metamask/multichain-api-middlewareby replacing per-method exports with a singlemethodHandlersmap keyed by method name, updating handlers to the newMethodHandlertyping, and adding construction tests viacreateMethodMiddleware.Removes
@metamask/permission-controllerpermitted RPC method modules/exports and related types, and tightens a couple of behaviors:wallet_invokeMethodnow returnsinvalidParamswhenparamsisn’t an object, andwallet_createSessionmakestrackSessionCreatedEventa required hook (nullable to disable).Reviewed by Cursor Bugbot for commit 846bfc4. Bugbot is set up for automated code reviews on this repo. Configure here.