Cashu: recover the wallet's ecash from the seed words after a reinstall (NUT-09/13 restore)

Open
#854 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
dart, rust, sqlite

Research direction

Start with cashu/wallet.rs, api/cashu.rs, lib/src/rust/api/cashu.dart, and the capability checks, then trace the wallet screen and import walkthrough. Run the listed Rust unit and ignored integration tests, with explicit coverage for NUT-09 gating, mint selection, counters, and repeated restores. Done means reinstalling or using a second device with the same 12 words and mint URL rebuilds the balance through the user-facing recovery action, with documented unsupported cases.

Written by the indexing model from the issue text.

Description

Summary

The embedded Cashu wallet derives every proof from the identity's BIP-39 seed (NUT-13 deterministic secrets), and the code says that makes the ecash "recoverable from the words the user already backed up" (cashu/wallet.rs:86, crypto/keys.rs:76, api/identity.rs:461). It is not, today: nothing in the app ever calls cdk's restore() — no restore(, restore_with_opts or NUT13Options anywhere in rust/src or lib/, no recovery function in the bridge (lib/src/rust/api/cashu.dart), no recovery action on the wallet screen. A user who reinstalls the app or moves to a new phone imports the same words and finds a zero balance — the sats are still at the mint, signed for outputs only their seed can rebuild, and there is no button to rebuild them.

What exists today

  • Seed: open_wallet (api/cashu.rs:79) → identity::current_bip39_seed() (identity.rs:466) → crypto/keys.rs::derive_bip39_seed — Mnemonic::to_seed(""), the same 64 bytes the Nostr identity and trade keys come from (NIP-06). An nsec-imported identity has no seed (CashuNoMnemonic, identity.rs:475).
  • Per-identity proof store (cashu-<pubkey>.sqlite), kept when an identity is deleted. Re-importing the words on the same device reopens that file, so the balance comes back. That is not a restore: it only works while the file still exists — which is also all that docs/cashu/README.md:484 ("importing its words again restores the balance") can mean today.
  • sweep_spent_proofs (NUT-07) forgets proofs the mint reports spent. It recovers nothing (its own doc comment says so, cashu/wallet.rs:391-410).
  • The wallet's mint (cashu_wallet_mint_url, db/mod.rs:102) is a device preference in the app database, outside IDENTITY_SCOPED_PREFIXES: it survives an identity change but not a reinstall.

What is lost on reinstall today: the whole balance, even with the words backed up.

Why

  • Ecash is a bearer asset. docs/cashu/README.md already makes wallet backup/restore release-blocking (C10, :776) and calls it "a release requirement, not Wave-4 polish" (:40).
  • The building block is already in the pinned cdk (0.18.1): Wallet::restore() = restore_with_opts(NUT13Options::default()), batches of 100, stop after 3 consecutive empty batches. Per keyset — rotated ones included (self.keysets(Default::default())) — it rebuilds blinded messages from the seed (PreMintSecrets::restore_batch), asks the mint which ones it signed (post_restore, NUT-09), unblinds them, checks their state (check_proofs_spent, NUT-07), stores the unspent and pending proofs, and returns Restored { spent, unspent, pending }.
  • The words are the one backup we ask users to keep. Without a restore, the seed-derived wallet is a promise the app does not yet keep.

Proposal

  1. Capability: NUT-09. MintCapabilities checks only NUT-07/11/12 and the sat keyset today, and cdk's restore() does not check NUT-09 either — it just calls post_restore. Add nut09_restore as an optional capability (not part of is_usable()): the recovery action is offered only when the mint advertises it, and CashuRestoreUnsupported comes from that check, before any request.

  2. Rust (cashu/wallet.rs, api/cashu.rs): cashu_restore(mint_url) -> CashuRestoreResult — connect to that mint for the loaded identity, run restore(), return:

    • recovered_sats: the balance after minus the balance before, not Restored.unspent. cdk upserts restored proofs (INSERT … ON CONFLICT(y) DO UPDATE), so restoring on top of a non-empty store never double-counts, but unspent also includes the proofs that were already there — reporting it as "recovered" would be wrong on a live wallet;
    • pending_sats (Restored.pending) and spent_sats (Restored.spent), for the explanation.

    Under lifecycle_lock, like every other wallet lifecycle change. Errors as stable markers (CashuRestoreUnsupported, CashuMintUnreachable, CashuRestoreFailed); the wasm stub fails with CashuUnsupportedOnWeb.

  3. Which mint. The seed does not say where the sats are. The restore needs a mint URL: the wallet's mint if one is set, otherwise the user picks one — the active node's [cashu] mint_urls offered as suggestions, or any URL pasted or scanned. Restoring at several mints is several calls.

  4. Dart: an explicit "Recover ecash from your words" action on the wallet screen, plus a prompt after importing an identity from words (Account import and walkthrough restore) — never an automatic scan at every start, since a restore contacts the mint and links all of the identity's outputs in one session. l10n in all locales; DESIGN_SYSTEM.md.

  5. Spec and comments, in the same change:

    • a phase in docs/cashu/README.md, inside C10's release-blocking group;
    • C10's backup line (:776) asks for wallet backup "integrated with the existing encrypted-backup flow (export_encrypted_backup)", but that flow only encrypts the mnemonic (identity.rs:1041-1067). With NUT-13 the words are the wallet backup, apart from the cases listed below; the line should say so;
    • the "recoverable from the words" comments (cashu/wallet.rs:86, crypto/keys.rs:76, identity.rs:461) and README :484 qualified until this ships, then pointed at it.

What a restore cannot bring back (to state in the spec and the UI)

  • A Lightning mint quote paid but not yet minted (#390, receive side): nothing has been signed, so there is nothing for NUT-13 to find. Only the local store knows the quote.
  • Escrow tokens. Confirmed in cdk (wallet/swap/mod.rs:233-285): outputs with spending conditions are built with PreMintSecrets::with_conditions (or with_p2bk), which use random secrets, not seed-derived ones; only the swap's change comes from the seed (PreMintSecrets::from_seed). So the change of an escrow lock comes back with a restore, and the escrow token itself does not — it is recovered through the 2-of-3 / refund paths from the token stored with the trade.
  • Exported tokens nobody redeemed yet are found (they are unspent and derived from the seed) and come back into the wallet. That is correct for the user, but whoever later tries to redeem the token will be refused — the UI should say so before restoring.

Known behaviour to design around

  • Counters move by addition. After a restore cdk calls increment_keyset_counter(keyset, highest + 1), which in SQL is counter = counter + :count (cdk-sql-common/src/wallet/mod.rs:1007-1013). On a fresh store (counter 0) that lands exactly on highest + 1. On a live wallet the counter jumps further (C + highest + 1): safe — no secret is ever reused — but indices are skipped. Since a later restore always scans from 0 and stops after 3 empty batches (~300 indices), a jump wider than that would leave everything signed after it out of reach of any future restore. Options: restore only into an empty store, or set the counter instead of adding to it. Either way, covered by a test.
  • Stale keysets (cdk#2702 / #799): not yet checked against a mint.

Tests

  • Integration (#[ignore], local nutshell, FakeWallet backend):
    • fund a wallet, drop its store, open a fresh store with the same seed, restore() → same balance; spend afterwards → no "already signed" error;
    • restore on top of a live store → balance unchanged, recovered_sats = 0;
    • restore twice, spend in between, restore into a fresh store → same balance (the counter jump above);
    • a token exported before the restore is reported spent once redeemed elsewhere;
    • an escrow lock's change comes back, its locked token does not.
  • Unit: marker mapping; the NUT-09 gate (action hidden, mint not refused); the mint-choice rules.

Acceptance

Uninstall (or use a second device) → import the same 12 words → wallet → "Recover ecash" → pick the mint → the balance reappears.

Related

  • #390 — Lightning mint/melt (its receive side adds the one case a restore cannot cover).
  • #142 — restoring sessions and trades from the mnemonic after a reinstall.
  • #799 — stale keyset at connect (cdk#2702).
Dominant language
Dart
Stars
11
Forks
9
Avg merge
2d 1h
Merged PRs (30d)
16

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from MostroP2P/app

All issues in MostroP2P/app

Similar issues

More Dart issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.