Skip to content

fix(bitcoin-wallet-snap): widen address scan and sync coverage - #201

Open
jeremytsng wants to merge 6 commits into
mainfrom
investigate/btc-snap-balance-display
Open

fix(bitcoin-wallet-snap): widen address scan and sync coverage#201
jeremytsng wants to merge 6 commits into
mainfrom
investigate/btc-snap-balance-display

Conversation

@jeremytsng

@jeremytsng jeremytsng commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Explanation

Two support cases (0.09 BTC and 6.9 BTC) reported bridged funds as missing. In both cases the funds sat on a change address the snap itself created during a partner PSBT fill. The coins were derived from the user's seed and were always spendable, but the snap's address coverage gave no guarantee the wallet would show them:

  • A full scan runs only once, at account creation, with stopGap: 5 — a quarter of the BIP44 gap limit of 20. Any coin outside that window is never found again.
  • When fillPsbt drains change to one of our own scripts, nothing guarantees that script is revealed and persisted before broadcast.

On-chain evidence shows the first case's change coin was in fact tracked by the snap (a later wallet transaction coin-selected it together with the user's own address), which points at the client's balance display path for that report — tracked separately. This PR closes the snap-side coverage class with minimal infra impact:

  1. Reveal at fill time. When a PSBT template output pays one of our own scripts, fillPsbt reveals the derivation index for that script and persists it before signing. If we sign it, we watch it. This prevents new cases at the source, at near-zero recurring cost.
  2. Split stopGap into { discovery: 5, scan: 20 }. Account discovery keeps the cheap probe; a real account scan uses the BIP44 gap limit.
  3. One-time repair. The first regular sync after this update schedules a single full scan per existing account (rescanV1 state marker), so wallets that already drifted are repaired once — no recurring scan jobs.
  4. Telemetry. A Scan Discovered Missed Transactions tracking event fires when the repair scan finds a transaction the wallet did not know about. This is the acceptance metric: its rate should go to zero.

Deliberately out of scope, pending an infra discussion on Esplora load: recurring sync for unselected accounts and scheduled rescans. A follow-up PR will propose those with cadences sized by the infra owners.

References

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

Bridged change landing on wallet scripts could stay invisible: full scans
ran once at account creation with a stop gap of 5, routine sync covered
only revealed scripts of selected accounts, and filling a partner PSBT
did not guarantee the drained change script was revealed and persisted.

- Split chain stopGap into { discovery: 5, scan: 20 }; account discovery
  keeps the cheap probe, real scans use the BIP44 gap limit
- Reveal and persist own scripts drained to by fillPsbt before signing
- Add synchronizeAllAccounts (PT10M) and fullScanAccounts (PT6H) cron
  jobs so unselected accounts and unrevealed scripts are still covered
- Emit a tracking event when a full scan finds transactions routine sync
  missed
@jeremytsng
jeremytsng requested a review from a team as a code owner August 26, 2026 13:21
@jeremytsng
jeremytsng deployed to default-branch August 26, 2026 13:21 — with GitHub Actions Active
…ime repair

Recurring all-account sync and full-scan cron jobs need an infra
discussion on Esplora load. Replace them with a single repair pass:
the first sync after this update schedules one full scan per existing
account, gated by a rescanV1 state marker. The missed-transactions
tracking event now fires only on these repair scans, keeping the
metric free of expected account-creation discoveries.
@jeremytsng
jeremytsng marked this pull request as draft August 26, 2026 18:57
@jeremytsng
jeremytsng marked this pull request as ready for review August 26, 2026 18:57
@jeremytsng
jeremytsng deployed to default-branch August 26, 2026 18:57 — with GitHub Actions Active
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.

[bitcoin-wallet-snap] bug: wallet balance drops to zero during pending transactions (unconfirmed change UTXOs not displayed)

1 participant