Skip to content

refactor(tron-wallet-snap): assert supported networks at handler boundaries instead of casting - #402

Closed
ulissesferreira wants to merge 7 commits into
mainfrom
WPN-2226-guarantee-local-type-consistency-for-networks
Closed

ulissesferreira wants to merge 7 commits into
mainfrom
WPN-2226-guarantee-local-type-consistency-for-networks

Conversation

@ulissesferreira

Copy link
Copy Markdown
Contributor

Explanation

The Tron Snap handled scopes and networks coming from untrusted boundaries (RPC request params, the Keyring API) with forced casts (chainId as Network, scope as Network), silently accepting values the Snap does not control and indexing Networks[...] with them.

This PR replaces those casts with runtime assertions at the handler boundary, matching how other request fields are validated:

  • Adds isNetwork (type guard) and assertNetwork (throws InvalidParamsError) next to Network in constants.
  • ClientRequestHandler: the four chainId as Network casts (onAmountInput, confirmSend, claimUnstakedTrx, claimTrxStakingRewards) now use assertNetwork(chainId).
  • KeyringHandler.resolveAccountAddress: the extension-provided scope is asserted instead of cast.

Follow-up work (out of scope): collapse the remaining internally-consistent casts (SendService, StakingService, asset mappers) and the config/manifest-derived casts by flowing Network down from these boundaries.

Ticket: WPN-2217

References

Based on the review thread in #388. This PR is intended to serve as the base that #388 builds on. Supersedes #394 (branch rename).

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

- Assert supported networks where data enters the snap: manifest scopes,
  AssetsController assets, Token API filtering and TrackTransaction
  background event params.
- Carry the validated `Network` type instead of re-casting: sign
  renderers take `TronWalletKeyringRequest`, send uses `asset.network`,
  staking and zero-balance assets use `getAssetNetwork`.
- Add `TronKeyringAccount` with `scopes: Network[]` for stored accounts;
  it remains assignable to the emitted `KeyringAccount`.
- Build CAIP asset types from `Network` instead of `TrxScope`.
- Add a lint rule banning casts to `Network` or `TrxScope` in the Tron snap.
Per review, keep the root ESLint config untouched: the no-tron-network-casts
rule block and the shared no-enums selector extraction are dropped.
…ues(Network)

The config structs validate the clients' baseUrls as
record(NetworkStruct, UrlStruct), so keys are guaranteed to be supported
networks. Iterating Object.values(Network) keeps that typing end to end,
removing the Object.entries key-widening that previously forced a runtime
isSupportedNetwork guard (and, before this PR, an 'as Network' cast).
@ulissesferreira

Copy link
Copy Markdown
Contributor Author

Superseded by #403 — rebased on latest main with the scope reduced to the type-consistency changes only.

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.

1 participant