refactor(wallet-cli): Parameterise RpcHandler with struct-validated dispatch#8846
Draft
sirtimid wants to merge 1 commit into
Draft
refactor(wallet-cli): Parameterise RpcHandler with struct-validated dispatch#8846sirtimid wants to merge 1 commit into
sirtimid wants to merge 1 commit into
Conversation
…ispatch Adds a `defineHandler(paramsStruct, run)` helper plus a generic `RpcHandler<TParams, TResult>` / `RpcHandlerDefinition` type so each daemon RPC method owns its params struct. `rpc-socket-server` now validates params via `superstruct.validate` once per request and returns `-32602 invalidParams` on shape mismatch, so handler bodies can trust their input. Rewrites `getStatus` and `call` against the new shape and replaces the `wallet.messenger.call as any` cast with a single labelled `RpcDispatcher` bind. Tests updated; coverage stays at 100%. Closes #8777. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
Resolves #8777.
RpcHandler<TParams, TResult>,RpcHandlerDefinition<TParams, TResult>({ paramsStruct, run }), and adefineHandler(paramsStruct, run)helper indaemon/types.ts.RpcHandlerMapnow maps method names to definitions.rpc-socket-server.tsvalidatesparamsagainst the handler's struct viasuperstruct.validatebefore dispatch and returns-32602 invalidParamson shape mismatch. Each handler body can now trust the shape of its argument.daemon-entry.ts:getStatususesliteral(null).calluses adefined struct that enforces a non-empty[string, ...Json[]]tuple, dropping the ad-hocArray.isArray / typeofchecks from the handler body.wallet.messenger.call as anycast with a single typedRpcDispatcher = messenger.call.bind(messenger)escape hatch.@metamask/superstruct ^3.1.0towallet-cli(already used across the monorepo at this version).Branched off
rekm/wallet-cli; merge target intentionally kept on the feature branch rather thanmain. No changelog entry (folds into the package's existing0.0.0initial-release bullet).Test plan
yarn workspace @metamask/wallet-cli run test— 248 tests pass, 100% line/branch/function/statement coverage on the touched files.yarn lint:eslint packages/wallet-cli/src— clean.yarn constraints— clean.yarn workspace @metamask/wallet-cli run changelog:validate— clean.-32602 invalidParamspath and verifiesrunis not invoked when the struct rejects.callhandler cover both the rejection cases (null, empty, non-string head, non-array) and the accepting case.🤖 Generated with Claude Code