Cashu: recover the wallet's ecash from the seed words after a reinstall (NUT-09/13 restore)
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
- Domain
- backend, documentation, mobile-dev, testing
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 thatdocs/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, outsideIDENTITY_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.mdalready 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 returnsRestored { 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
-
Capability: NUT-09.
MintCapabilitieschecks only NUT-07/11/12 and thesatkeyset today, and cdk'srestore()does not check NUT-09 either — it just callspost_restore. Addnut09_restoreas an optional capability (not part ofis_usable()): the recovery action is offered only when the mint advertises it, andCashuRestoreUnsupportedcomes from that check, before any request. -
Rust (
cashu/wallet.rs,api/cashu.rs):cashu_restore(mint_url) -> CashuRestoreResult— connect to that mint for the loaded identity, runrestore(), return:recovered_sats: the balance after minus the balance before, notRestored.unspent. cdk upserts restored proofs (INSERT … ON CONFLICT(y) DO UPDATE), so restoring on top of a non-empty store never double-counts, butunspentalso includes the proofs that were already there — reporting it as "recovered" would be wrong on a live wallet;pending_sats(Restored.pending) andspent_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 withCashuUnsupportedOnWeb. -
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_urlsoffered as suggestions, or any URL pasted or scanned. Restoring at several mints is several calls. -
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. -
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:484qualified until this ships, then pointed at it.
- a phase in
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 withPreMintSecrets::with_conditions(orwith_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 iscounter = counter + :count(cdk-sql-common/src/wallet/mod.rs:1007-1013). On a fresh store (counter 0) that lands exactly onhighest + 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.
- fund a wallet, drop its store, open a fresh store with the same seed,
- 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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from MostroP2P/app
-
documentation priority: low
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
Maintainers usually reply within 1 day
-
area: ui bug priority: medium
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
area: ui bug priority: high
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
Bug: Button "Share logs" does nothingPossibly taken @Luquitasjeffrey claimed this 8 days ago. Open
Difficulty 2/5 Half a day Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
area: rust-core bug priority: low
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
Similar issues
-
Python example: list a model's scenes and their camerasPossibly taken @rudraparmar76 claimed this today. Opendocumentation good first issue python
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
iamahsanmehmood/openskp#796 · 1 comment ·
Maintainers usually reply within 1 day
-
needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Plural rendering: first matching part wins, but gen-l10n lets the last one win (=1 shadows one)Openbug help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
crowdin/flutter-sdk#97 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
MrSpideyNihal/lockfeed#16 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
InlitX/GymMane#529 · 1 comment ·
Maintainers usually reply within 1 day