Skip to content

Mint fallback#53

Draft
9qeklajc wants to merge 9 commits into
mainfrom
mint-fallback
Draft

Mint fallback#53
9qeklajc wants to merge 9 commits into
mainfrom
mint-fallback

Conversation

@9qeklajc

Copy link
Copy Markdown

No description provided.

9qeklajc and others added 3 commits July 15, 2026 01:37
When routstr.otrta.me rejects a Cashu token with 'mint_unreachable',
the mint fallback handler calls _spendToken with a fallback mint URL
(e.g. cubabitcoin.org). However, _spendToken's internal
_selectCandidateMints puts the highest-balance mint first regardless
of the preferredMintUrl parameter, so the fallback token was still
minted from the same unreachable mint (minibits).

Pass excludeMints: [initialMintUrl] to _spendToken so the failed
mint is excluded from candidate selection, forcing the fallback to
use the requested alternative mint.
…to-pay

When the local wallet runs out of proofs, the fallback chain now:

1. Retries across ALL configured mints (createProviderToken patched —
   previously only topUp retried on 'Not enough proofs')
2. Creates a routstr-core Lightning invoice (POST /lightning/invoice)
   → uses 'topup' purpose when an existing API key is available
   → pays via NWC (payBolt11) if connected, then retries
   → otherwise surfaces invoice for manual payment + polls until paid/expired
3. Falls back to local wallet Lightning invoice + NWC funding

Hardening (15 new tests):
- SSRF protection: rejects non-HTTPS provider URLs (except localhost)
- Amount validation: clamps to [1, 1_000_000] sats
- bolt11 validation: must start with 'lnbc'
- invoice_id validation: must be non-empty string
- Fetch timeout: AbortController (10s) on invoice creation
- NWC exception handling: caught, not propagated
- Double-install idempotency: patch markers prevent re-patching
- Concurrent calls: no shared state corruption
- API key safety: never logged in error messages
- Poller lifecycle: stops after 84 attempts (~7min) or on expired status

New wallet adapter method:
- payBolt11(bolt11): pays externally-created invoices via NWC

Verified end-to-end:
- 50-request stress test: 44/50 success, 6 fallback triggers (fugu-ultra)
- Routstr-core topup: 64,781 → 114,340 msats (balance increased)
- Daemon stable post-stress (142MB RSS, immediate recovery)
- 48/48 tests pass (33 existing + 15 hardening)
- Remove duplicate const controller/timeout declarations introduced during
  sibling-agent parallel edits
- Reduce fetch timeout from 10s to 3s so the fallback chain stays snappy
  and tests don't time out the 5s bun:test budget
.pi-subagents/, data/, ROUTSTRD_OTRTA_FIX.md, kill-orphan-cocod.sh are
local-only debug/state files that accumulated during incident response
and shouldn't be tracked.
routstr-core's /lightning/invoice endpoint requires integer sats
but options.amount can be a float (e.g. 64.79) derived from msat
costs. Without Math.ceil the fallback crashed with HTTP 422:
'Input should be a valid integer, got a number with a fractional part'

Fix: Math.ceil() in both topUpAmount calculations + defense-in-depth
Math.ceil() inside createInvoiceViaRoutstrCore itself.
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.

2 participants