Skip to content

ENG-631: reclaim mint-settled spent card slots on the top-up load tap - #759

Merged
islandbitcoin merged 7 commits into
mainfrom
jabariennis/eng-631-reclaim-spent-slots
Oct 9, 2026
Merged

islandbitcoin merged 7 commits into
mainfrom
jabariennis/eng-631-reclaim-spent-slots

Conversation

@islandbitcoin

Copy link
Copy Markdown
Contributor

Problem

A Cashu card's spent slots stay occupied for good: loadTopUp only ever wrote into empty slots and never sent CLEAR_SPENT, so a card that had been spent down to 0 empty slots could never take another top-up, even once the mint had settled every one of those spends. The card screen showed "loaded · free" only, so the holder could not see where the slots had gone either.

A spent slot is owed until the mint settles it: a burn whose signature never left the card (a POS tap cut short) can be recovered only from its slot, and the applet's CLEAR_SPENT is all-or-nothing. So the clear can only run once the mint vouches for every spent slot.

Fix (flash-mobile, ENG-631)

Card reads harvest spent slots. readCashuCard now reads every occupied slot (readCardSlots) and carries the spent ones on CashuCardInfo.spentSlots; getUnspentByKeyset is unchanged. The Flashcard context re-reads the slots after an op that moved the balance or the spent count, so a clear shows at once (app/utils/cashu-card.ts, app/contexts/Flashcard.tsx).

Mint check before the tap, verified in-session. reclaimVerdicts asks the mint (NUT-07) about each proof-shaped spent slot's Y before the tap, like the existing proofStatesForLoad. Safe because SPENT is terminal: a stale verdict can only withhold a clear. Inside the tap, reclaimSpentSlots re-reads GET_SLOT_STATUS/GET_PROOF and sends B0 31 00 00 01 only when every spent slot is mint-SPENT or a CLEAR_SPENT remnant (isProofShaped false: the applet zeroes keyset → amount → nonce → C before marking the slot empty, D14). One UNSPENT or PENDING slot blocks the whole clear and is counted as settling. A mint that cannot be asked costs no tap: no slot is freed.

CLEAR_SPENT runs in exactly one place: the top-up load tap, between VERIFY_PIN and loadTopUp (useCardTopUp.load). loadTopUp itself still never clears; its resumed-load inventory now also ignores a torn spent slot whose nonce survived but whose data is not a proof, and lets the mint decide whether the record's proof is written again.

Reclaimable slots are room. prepareTopUp takes card.reclaimable (never inflating empty), the amount step counts it, and a card at 0 empty slots can top up again. If the in-session re-check skips the clear, loadTopUp refuses with slots as before, the record stays minted, and the refusal says how many spent slots are still settling. The done step says how many slots were freed.

Copy. Card screen: {unspent} loaded · {spent} spent · {free} free of {max} plus a note under the row when spent > 0. Top-up: topUpSlotsReclaim, topUpReclaimed, topUpSlotsSettling (yarn update-translations run).

Cross-checked against the getfoxy/card fork's spec/APDU.md: CLEAR_SPENT is unchanged there (0x31, Le 01, D14 write order); its recent applet changes (sign-before-burn, daily limit, change tap) touch the spend path, not this.

How it was tested

  • yarn test: 164 suites, 1969 tests, all green.
  • yarn tsc:check, yarn check:translations: clean.
  • eslint on every changed file: 0 errors (whole-repo count unchanged from main: 1187 pre-existing errors); prettier --check (pinned 2.8.8) and typos on the changed files: clean.
  • New/updated specs:
    • __tests__/utils/cashu-card.spec.ts: CLEAR_SPENT bytes, 1-byte parse, wrong length, PIN refusal; isProofShaped table (zero/non-pow2/over-range amount, off-curve and zeroed C, zero nonce, zero keyset); readCardSlots reads every occupied slot and reports disagreeing status bytes as neither; readKeysetSplit runs with unspent = 0, spent > 0; a read sends no SPEND/LOAD/CLEAR.
    • __tests__/utils/cashu-card-topup.spec.ts (fake signing mint + fake card): all SPENT ⇒ 14, 13…, 31, count returned, slots empty; one UNSPENT / one PENDING / nonce missing from verdicts / verdicts undefined ⇒ no 0x31 and settling; remnant clears without a verdict; no spent slots ⇒ no GET_PROOF, no 0x31; a short freed count is logged; full card (32 settled spends) reclaimed then loaded in one session (14, 13×32, 31, 14, 30×6); full card with one unsettled spend refuses with slots, record stays minted; reclaimVerdicts and reclaimPlan tables; reclaimable counted in prepareTopUp room (and against holds); torn spent slot keeping its nonce is written again when the mint says UNSPENT and not when SPENT.
    • __tests__/hooks/use-card-top-up.spec.tsx: order mint → tap → verify → reclaim → load; no spent slots ⇒ no verdict call; verdict failure ⇒ tap still runs with verdicts: undefined; a refused load keeps its reason and carries the reclaim; proofStatesForLoad failure still aborts before any mint call about spent slots; reclaimPlanFor.
    • __tests__/screens/flashcard-v2-topup.spec.tsx: unsettled spent slots are not room; 0-empty card with all-SPENT slots tops up (reclaimable: 32 reaches prepareTopUp); no mint call on a card with no spent slots or on a resume; mint failure leaves them out; done step shows freed / settling notes; slots refusal appends the settling note.
    • __tests__/screens/flashcard-v2.spec.tsx: 3 loaded · 5 spent · 24 free of 32 and the spent note.
    • Context specs: a tap reads the seven spent slots (14, 13×8), never CLEAR_SPENT; an op that frees spent slots re-reads them and keeps the per-unit figures; an op that changes nothing reads no slots.

Follow-ups

  • No standalone "Reclaim slots" action on the card screen yet: the clear runs only on a load tap.
  • Per-slot clearing (a partially torn slot that still passes the slot checks rebuilds a Y the mint never saw ⇒ UNSPENT ⇒ blocks every reclaim) needs applet support; logged, out of scope.
  • flash-pos part (section C of the plan: show spent count on the balance screen) is a separate PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WxkKbrg2Xohf3mQj7BLrs6

Dread and others added 3 commits October 9, 2026 14:34
…(ENG-631)

Add the CLEAR_SPENT (0x31) APDU and `isProofShaped`, the card file's slot
checks that tell a CLEAR_SPENT remnant from a proof. A card read now reads
every occupied slot (`readCardSlots`) and carries the spent slots on
`CashuCardInfo.spentSlots`, so the next top-up can ask the mint about them
before its load tap. The Flashcard context re-reads the slots after an op
that moved the balance or the spent count, so a clear shows at once.

`getUnspentByKeyset` still reads unspent slots only; nothing here sends
CLEAR_SPENT.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WxkKbrg2Xohf3mQj7BLrs6
…tap (ENG-631)

`reclaimVerdicts` asks the mint (NUT-07) about the card's proof-shaped spent
slots before the tap; `reclaimPlan` says what a reclaim will do with them;
`reclaimSpentSlots` re-reads the spent slots in the session and sends
CLEAR_SPENT only when every one is a remnant or mint-SPENT, otherwise sends
nothing and counts what is still settling. `prepareTopUp` counts
`card.reclaimable` as room; `loadTopUp` itself still never clears, and its
inventory no longer takes a torn spent slot's nonce as the record's proof.
`TopUpError` can carry the reclaim's result with a refusal.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WxkKbrg2Xohf3mQj7BLrs6
… (ENG-631)

The load tap now runs VERIFY_PIN, the reclaim, then the load in one session;
the mint is asked about the spent slots before the tap, and a mint that
cannot answer costs no tap. The amount step counts the slots the tap will
free as room (`reclaimable`), the done step says how many were freed, and a
no-room refusal says how many spent slots are still settling. The card
screen shows unspent, spent and free counts with a note on spent slots.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WxkKbrg2Xohf3mQj7BLrs6
@linear

linear Bot commented Oct 9, 2026

Copy link
Copy Markdown

ENG-631

Dread and others added 4 commits October 9, 2026 14:49
…drop the retry note from the done step (ENG-631)

Review fixes on #759.

- `reclaimSpentSlots` hands on what it read (`inventory`: the slot
  statuses and each spent slot's proof) when it sent no CLEAR_SPENT, and
  `loadTopUp` takes that in place of a read of its own. A first load in
  the same tap now sends no GET_SLOT_STATUS; a resumed load reads only
  the unspent slots, taking the spent ones from the reclaim. After a
  clear the read is stale and the load reads the card afresh, which
  after a full clear means the unspent slots alone. On a resumed load
  of a card with k spent and u unspent slots that is u GET_PROOFs
  before the first LOAD_PROOF instead of 1 + k + u.
- The done step's settling note no longer reuses `topUpSlotsSettling`,
  whose "Try again later." belongs to the `slots` refusal: a load that
  went through has nothing to try again. New `topUpSettlingNote`.

Tests: engine tests count the tap's APDUs for a first and a resumed load
with and without a clear, and assert the inventory handed on (or not);
the hook test checks the hand-off; the screen test asserts the new note
and that it carries no retry imperative.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WxkKbrg2Xohf3mQj7BLrs6
Mirror the five new FlashcardV2 keys (slotsSpentNote, topUpSlotsReclaim,
topUpReclaimed, topUpSlotsSettling, topUpSettlingNote) and the updated
slotSummary into all 23 locale files, the same way #746/#747/#755 did,
so check:translation-drift passes.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WxkKbrg2Xohf3mQj7BLrs6
…t field, and rethrow the engine's own refusal (ENG-631)

Review fixes on #759:

- The amount field waited for the unfinished-top-ups read but not for the
  reclaim plan. On a card with no empty slot and every slot spent, the room
  was 0 until the mint answered, so the field refused every amount ("try a
  smaller amount") and then changed its mind a round-trip later. The screen
  now tracks the plan settling (`reclaimRead`), set at once when there is
  nothing to ask about and in a `finally` otherwise, and the field waits on
  both reads.

- `useCardTopUp.load` built a fresh TopUpError to attach the reclaim, so a
  refused load's stack pointed at the catch block rather than the engine's
  own check. `TopUpError.reclaim` is now writable and the original error is
  rethrown with it set.

Tests: two screen tests hold the plan (resolve / reject) and assert no field
mounts until it settles; a hook test asserts the rejected error is the very
one the engine threw.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WxkKbrg2Xohf3mQj7BLrs6
… reclaim plan (ENG-631)

Review fix on #759:

- The amount field waits for the reclaim plan, and that plan is a mint
  round-trip over the phone's network with no timeout. On a card with
  spent slots the amount step showed the unit caption, "Paid from" and a
  disabled Continue around a blank gap for as long as the mint took to
  answer, or RN fetch took to fail. An ActivityIndicator now sits in the
  field's slot (`topup-amount-loading`) until both the unfinished-top-ups
  read and the plan have settled, matching the screen's own loading step.

- The held-plan, held-read and refused-mint screen tests assert the
  indicator is present before settle/refuse and gone after.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WxkKbrg2Xohf3mQj7BLrs6
@islandbitcoin
islandbitcoin merged commit 0b9054b into main Oct 9, 2026
10 checks passed
@islandbitcoin
islandbitcoin deleted the jabariennis/eng-631-reclaim-spent-slots branch October 9, 2026 23:48
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