Skip to content

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

Closed
ulissesferreira wants to merge 7 commits into
mainfrom
WPN-2217-remove-scope-network-forced-casts
Closed

ulissesferreira wants to merge 7 commits into
mainfrom
WPN-2217-remove-scope-network-forced-casts

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.

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

@ulissesferreira
ulissesferreira force-pushed the WPN-2217-remove-scope-network-forced-casts branch 2 times, most recently from 0ddf3d2 to 995dd3d Compare September 30, 2026 15:51
@ulissesferreira
ulissesferreira force-pushed the WPN-2217-remove-scope-network-forced-casts branch from 995dd3d to 08849c2 Compare September 30, 2026 15:56
@ulissesferreira
ulissesferreira added this pull request to stack #395 September 30, 2026 16:06
@ulissesferreira
ulissesferreira force-pushed the WPN-2217-remove-scope-network-forced-casts branch 6 times, most recently from 090e695 to 7a86d64 Compare October 1, 2026 14:31
@ulissesferreira
ulissesferreira removed this pull request from stack #395 October 1, 2026 15:17
- 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
ulissesferreira force-pushed the WPN-2217-remove-scope-network-forced-casts branch from 7a86d64 to 33731fa Compare October 1, 2026 15:27
@sonarqubecloud

sonarqubecloud Bot commented Oct 1, 2026

Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When working locally on each Snap we should have narrowed types for certain locally stored things that we know are chain specific. Keyring accounts are a good example.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous code's direct access to baseUrl was nice but doesn't play well with typescript because network always gets casted as string.

@ulissesferreira ulissesferreira Oct 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous code's direct access to baseUrl was nice but doesn't play well with typescript because network always gets casted as string.

@ulissesferreira
ulissesferreira force-pushed the WPN-2217-remove-scope-network-forced-casts branch from 98a9bbe to 746f36f Compare October 1, 2026 16:49
@ulissesferreira
ulissesferreira deleted the WPN-2217-remove-scope-network-forced-casts branch October 1, 2026 16:49
@ulissesferreira

ulissesferreira commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Superseded by #402 — the branch was renamed to WPN-2226-guarantee-local-type-consistency-for-networks, which closed this PR. The same work continues there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant