Skip to content

fix(toolkit): [PM-20017] — use checked_add for wallet seed increment#1081

Open
m2ux wants to merge 4 commits intomainfrom
fix/PM-20017-checked-seed-increment
Open

fix(toolkit): [PM-20017] — use checked_add for wallet seed increment#1081
m2ux wants to merge 4 commits intomainfrom
fix/PM-20017-checked-seed-increment

Conversation

@m2ux
Copy link
Copy Markdown
Contributor

@m2ux m2ux commented Mar 26, 2026

Summary

Replace panic-on-overflow with explicit error return in wallet seed increment, addressing Least Authority audit finding AL (PM-20017).

🎫 Ticket 📐 Engineering


Motivation

The wallet seed increment function uses checked_add(1).expect() which panics on arithmetic overflow instead of returning an error. While overflow is physically impossible with the current 64-bit seed value space (u128 counter), the Least Authority audit correctly identified this as a code quality issue — panicking on arithmetic operations violates defensive programming principles.


Changes

  • Wallet module (ledger/helpers) — Changed increment_seed return type from String to Result<String, &'static str>, replacing .expect() with .ok_or()?
  • Batches builder (util/toolkit) — Updated compute_batches_seeds to propagate Result; retained expect() in build_txs_from where overflow is physically impossible and trait constraints apply
  • Builder module (util/toolkit) — Updated relevant_wallet_seeds to return Result
  • Transaction generator (util/toolkit) — Added DynamicError mapping at the build_txs boundary
  • Tests — 4 new unit tests covering normal increment, overflow, width preservation, and from-zero cases

📌 Submission Checklist

  • Changes are backward-compatible (or flagged if breaking)
  • Pull request description explains why the change is needed
  • Self-reviewed the diff
  • I have included a change file, or skipped for this reason: included audit-checked-seed-increment.md
  • If the changes introduce a new feature, I have bumped the node minor version
  • Update documentation (if relevant)
  • No new todos introduced

🔱 Fork Strategy

  • Node Runtime Update
  • Node Client Update
  • Other
  • N/A

🗹 TODO before merging

  • Ready for review

@m2ux m2ux marked this pull request as ready for review March 26, 2026 17:14
@m2ux m2ux requested a review from a team as a code owner March 26, 2026 17:14
m2ux added 3 commits March 26, 2026 17:18
Addresses Least Authority audit Issue AL — unchecked addition in
wallet seed increment that could silently wrap on overflow.
Change Wallet::increment_seed return type from String to
Result<String, &'static str>. Replace .expect("wallet seed overflow")
with .ok_or("wallet seed overflow")? to return an explicit error on
overflow instead of panicking.

Propagate Result through the caller chain:
- compute_batches_seeds() returns Result<Vec<WalletSeed>, &'static str>
- Builder::relevant_wallet_seeds() returns Result<Vec<WalletSeed>, &'static str>
- TransactionGenerator::build_txs() maps to DynamicError

In BatchesBuilder::build_txs_from(), retain expect() on the Result
since overflow is physically impossible (64-bit seed space starting
from 2, batch sizes of ~hundreds) and the BuildTxs trait constrains
the error type to JoinError.

Add unit tests: normal increment, overflow error, width preservation,
zero increment.

Addresses Least Authority audit Issue AL.
@m2ux m2ux force-pushed the fix/PM-20017-checked-seed-increment branch from eb5ed35 to 4788683 Compare March 26, 2026 17:18
@m2ux m2ux self-assigned this Mar 26, 2026
@m2ux m2ux changed the title fix(toolkit): PM-20017 — use checked_add for wallet seed increment fix(toolkit): [PM-20017] — use checked_add for wallet seed increment Mar 30, 2026
#toolkit
# Replace unchecked addition in wallet seed increment with checked_add

Replace unchecked `+` operator in wallet seed increment with `checked_add` to
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I didn't see a change in this area. Maybe the change description needs a revision.

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.

2 participants