Skip to content

Make wallet persistence refuse stale and silent overwrites - #14

Open
sqhell wants to merge 3 commits into
red-dev-inc:mainfrom
sqhell:fix/wallet-store-epochs
Open

sqhell wants to merge 3 commits into
red-dev-inc:mainfrom
sqhell:fix/wallet-store-epochs

Conversation

@sqhell

@sqhell sqhell commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

What

  • All wallet storage goes through one module, src/storage/walletStore.ts, with one rule: a save lands only if the instance saving descends from what is stored right now, or it is an explicit resync or recovery allowed to win.
  • Versioned record with metadata; epoch is checked and written in one IndexedDB transaction; a legacy byte array counts as epoch 0 and converts on first save.
  • The epoch is bound to the WebWallet instance through a registry, so a stale closure, old hook or other tab is rejected by construction; a superseded instance can never write again.
  • Full Resync persists the fresh wallet as a forced write, with its birthday, before it becomes visible, then retires the old instance.
  • Stored bytes that fail to decode are backed up and left in place; the home page offers an explicit "Discard unreadable wallet data and recover from MetaMask" action.
  • A rejected writer reloads from storage; tabs learn of saves through a BroadcastChannel; one tab per origin holds a Web Locks writer lock and runs the periodic rescan.
  • The blob's own account birthday is authoritative; the snap value is repaired downward, never raised.
  • Tests under fake-indexeddb for the store, the hook ordering and the home page guard.

Why

Twice in one day a correct wallet was replaced in IndexedDB by a worse one: a second tab kept saving its old copy over a completed Full Resync (every tab wrote the single wallet key unconditionally, last writer won), and stored bytes that failed to load were replaced by auto-recovery with an account-only wallet built from the chain tip. Stacked on the resync-dialog PR because it rewrites the same resync flow.

Stack

Position 10 of 15 in the WebZjs release order, to be merged in this order into main. Builds on #8 (fix/resync-dialog). GitHub shows the whole stack against main until those merge; the commits of this PR alone are here: sqhell/WebZjs@fix/resync-dialog...fix/wallet-store-epochs

  1. Make rustfmt and clippy pass on main #20 chore/rust-checks
  2. Reuse one web worker for every sync instead of spawning per call #6 fix/reuse-sync-worker
  3. Accept Trailers-Only gRPC responses from lightwalletd #7 fix/grpc-web-trailers-only
  4. Keep the Full Resync form usable during background syncs #8 fix/resync-dialog
  5. Send again from the transaction history, with a caution for new recipients #9 feat/send-again
  6. Surface PCZT parse and signer errors instead of trapping; readable transfer errors #10 fix/pczt-parse-error
  7. Sign the change-paired Orchard spends NU6.3 bundles require #11 fix/sign-change-paired-spends
  8. Stop a full resync from an old birthday exhausting the wasm heap #12 fix/historic-sync-oom
  9. Point to Full Resync from the end of the transaction history #13 feat/history-resync-hint
  10. Make wallet persistence refuse stale and silent overwrites #14 fix/wallet-store-epochs (this PR)
  11. Spend and count the Ironwood (NU6.3) pool #15 feat/ironwood-pool
  12. Classify history by what the wallet spent, not only by what it received #16 fix/history-from-spends
  13. Wallet stats card on the account summary #17 feat/wallet-stats
  14. Bounded sync steps, instance ids, close and worker poisoning #18 feat/resumable-sync
  15. Resumable, checkpointed full resync driven by sync steps #19 feat/resync-resume

Rust pins reference sqhell/zcash_client_memory and sqhell/librustzcash by revision so CI builds exactly what was tested; once the zcash_client_memory stack (red-dev-inc/zcash_client_memory #16 to #21) merges, the memory pin goes back to red-dev-inc/zcash_client_memory feat/snap-nu63. The integration branch test/all-fixes on the fork merges all fourteen and is the build that was tested end to end.

Tests

cd packages/web-wallet && yarn test

Rust-Checks has failed on main since the NU6.3 toolchain bump: rustfmt
differs in nine files, and clippy on nightly-2025-06-15 rejects ten
uninlined format arguments in webzjs-keys and one map iteration in
webzjs-requests under -D warnings. Apply rustfmt and clippy's own
suggestions; no behaviour changes.
…yncs

The resync form was rendered only while no sync was running, and fullResync
refused with "Sync already in progress" if a periodic rescan happened to be in
flight. With a 35 s rescan interval that made the dialog vanish mid-typing and
reject Confirm at random.

Syncs are now serialised on the JS side instead of refused: a small
module-level scheduler owns `syncInProgress`, periodic rescans skip their turn
while a sync runs, and a full resync waits behind an in-flight rescan. This
also closes the race where a rescan that had passed its guard could clear
`syncInProgress` while a full resync was still running. (The wasm wallet runs
syncs on one serial worker in a separate change; this one stands alone.)

The form stays mounted during background syncs. While the resync waits for a
running sync it can still be cancelled, which drops it from the queue; once it
starts, Confirm and Cancel are disabled until it settles, and a card mounted
while a resync is in flight shows that instead of offering another. The
birthday is validated against the chain height inline instead of an alert.

A custom birthday is written to the snap only once the resync succeeds, and
before the fresh wallet is swapped into state, so a failed resync never leaves
a birthday the old wallet never used (a birthday above the tip previously broke
auto-recovery with "block is not in the main chain"), and a failed snap write
leaves state, the snap and IndexedDB describing the same wallet.
…writes

Twice in one day a correct wallet was replaced in IndexedDB by a worse one.
A second tab kept saving its old copy over a completed Full Resync, because
every tab wrote the single `wallet` key unconditionally and the last writer
won. Earlier, stored bytes that failed to load were replaced by auto-recovery
with an account-only wallet built from the chain tip.

All wallet storage now goes through one module, src/storage/walletStore.ts,
which enforces one rule: a save lands only if the instance saving descends
from what is stored right now, or it is an explicit resync or recovery that
is allowed to win.

- The stored value is a versioned record with metadata; `epoch` increments on
  every write and is checked and written in one IndexedDB transaction. A
  legacy byte array counts as epoch 0 and is converted on its first save.
- The epoch is bound to the WebWallet instance through a registry, not to
  React state, so a stale closure, an old hook or another tab is rejected
  by construction. A superseded instance can never write again.
- Full Resync persists the fresh wallet as a forced write, with its birthday,
  before it becomes visible, then retires the old instance. Snap state is
  updated afterwards and repaired later if that fails.
- Stored bytes that fail to decode are backed up and left in place. The empty
  wallet built instead is never registered, so no save, including
  auto-recovery, can replace them. The home page offers an explicit
  "Discard unreadable wallet data and recover from MetaMask" action.
- A rejected writer reloads the wallet from storage instead of fighting;
  tabs learn of saves through a BroadcastChannel and reload when visible.
  One tab per origin holds a Web Locks writer lock and runs the periodic
  rescan; the others follow. Both are for freshness and efficiency only,
  the epoch check keeps every configuration safe.
- The blob's own account birthday, read straight from the stored bytes, is
  authoritative. The snap value is repaired downward when it is later, never
  raised.

Tests: the store module under fake-indexeddb (stale write rejected with
storage untouched, forced resync wins, superseded instance blocked, legacy
migration, unreadable bytes quarantined, concurrent saves serialized, and a
two-tab simulation), the hook ordering (persist before swap, birthday
precedence, reload on stale write, recovery guard) and the home page guard.

This branch has not been deployed

No deployments
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