From 0078f75f946e48fd082b1c93321d5ef47a76379a Mon Sep 17 00:00:00 2001 From: "shakespeare.diy" Date: Fri, 24 Jul 2026 16:53:40 +0700 Subject: [PATCH] Fix wallet invoice failure (CORS-blocked mint) + history crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Root cause: mint.minibits.cash sends no Access-Control-Allow-Origin header, so browsers block it (NetworkError → "Failed to create invoice / Failed to fetch mint"). Default to CORS-friendly mint.coinos.io instead. - useLightningOperations: detect CORS/network failures on receive & send, show actionable "Can't reach this mint — switch mints" message instead of a dead end. - MintPrivacyManager: reorder recommended mints by browser reachability; flag Minibits as mobile-app-only (not web-reachable). - useHistoryManager: fix `mintQuoteRepository.getAll is not a function` crash by probing for whichever list method the installed coco-cashu version exposes (getAll/getAllQuotes/list/...), failing gracefully. Co-authored-by: shakespeare.diy --- src/components/wallet/MintPrivacyManager.tsx | 17 +++-- src/hooks/wallet/useCashu.ts | 8 ++- src/hooks/wallet/useHistoryManager.ts | 70 ++++++++++++-------- src/hooks/wallet/useLightningOperations.ts | 29 +++++--- 4 files changed, 81 insertions(+), 43 deletions(-) diff --git a/src/components/wallet/MintPrivacyManager.tsx b/src/components/wallet/MintPrivacyManager.tsx index 9955396..6d5cf6e 100644 --- a/src/components/wallet/MintPrivacyManager.tsx +++ b/src/components/wallet/MintPrivacyManager.tsx @@ -43,23 +43,26 @@ interface MintPrivacyManagerProps { className?: string; } -// Recommended mints (no stablecoin mints) +// Recommended mints (no stablecoin mints). +// Ordered by browser reachability — web wallets can only use mints that send +// CORS headers. Minibits works great in mobile apps but does NOT allow browser +// requests, so it's flagged and listed last. const RECOMMENDED_MINTS = [ - { - url: 'https://mint.minibits.cash/Bitcoin', - name: 'Minibits', - description: 'Popular mobile-friendly mint', - }, { url: 'https://mint.coinos.io', name: 'Coinos', - description: 'Web-based mint from Canada', + description: 'Web-friendly mint (recommended for browsers)', }, { url: 'https://legend.lnbits.com/cashu/api/v1/AptDNABNBXv8gpuywhx6NV', name: 'LNbits Legend', description: 'Community mint on LNbits', }, + { + url: 'https://mint.minibits.cash/Bitcoin', + name: 'Minibits', + description: '⚠ Mobile-app only — not reachable from web browsers', + }, ]; export function MintPrivacyManager({ diff --git a/src/hooks/wallet/useCashu.ts b/src/hooks/wallet/useCashu.ts index a1fe64e..01aa3f7 100644 --- a/src/hooks/wallet/useCashu.ts +++ b/src/hooks/wallet/useCashu.ts @@ -166,9 +166,13 @@ export function useCashu(): UseCashuReturn { console.warn('[Cashu] Failed to enable some coco services:', err); } - // Add default mint if no mints exist + // Add default mint if no mints exist. + // NOTE: the mint MUST send permissive CORS headers, otherwise the browser + // blocks every request with a NetworkError ("Failed to fetch mint"). + // mint.minibits.cash does NOT set Access-Control-Allow-Origin, so it can't + // be used directly from a browser — we default to a CORS-enabled mint. try { - const DEFAULT_MINT_URL = 'https://mint.minibits.cash/Bitcoin'; + const DEFAULT_MINT_URL = 'https://mint.coinos.io'; const existingMints = await coco.mint.getAllTrustedMints(); const hasDefaultMint = existingMints.some((m: Mint) => m.mintUrl.toLowerCase() === DEFAULT_MINT_URL.toLowerCase() diff --git a/src/hooks/wallet/useHistoryManager.ts b/src/hooks/wallet/useHistoryManager.ts index ec1b6fd..64d852f 100644 --- a/src/hooks/wallet/useHistoryManager.ts +++ b/src/hooks/wallet/useHistoryManager.ts @@ -34,35 +34,53 @@ export function useHistoryManager({ try { const allHistory: HistoryEntry[] = []; + // The coco-cashu repository API has shifted method names across versions + // (e.g. `getAll`, `getAllQuotes`, `list`). Probe for whichever exists so a + // version bump can't crash the whole history view. + const listFrom = async (repo: unknown): Promise>> => { + if (!repo || typeof repo !== 'object') return []; + const r = repo as Record; + const candidates = ['getAll', 'getAllQuotes', 'list', 'getQuotes', 'all']; + for (const name of candidates) { + const fn = r[name]; + if (typeof fn === 'function') { + try { + const res = await (fn as () => Promise).call(repo); + if (Array.isArray(res)) return res as Array>; + } catch { + // try the next candidate + } + } + } + return []; + }; + + const repos = repositories as Record; + + const toEntry = ( + quote: Record, + type: 'mint' | 'melt', + ): HistoryEntry => { + const state = quote.state as string | undefined; + return { + id: String(quote.quoteId ?? quote.quote ?? quote.id ?? ''), + type, + amount: Number(quote.amount ?? 0), + mintUrl: String(quote.mintUrl ?? ''), + status: state === 'paid' ? 'completed' : state === 'pending' ? 'pending' : 'failed', + timestamp: Number(quote.createdAt ?? Date.now()), + quoteId: quote.quoteId ? String(quote.quoteId) : undefined, + invoice: quote.request ? String(quote.request) : undefined, + }; + }; + // Fetch mint quotes (receive operations) - const mintQuotes = await (repositories as any).mintQuoteRepository?.getAll() || []; - for (const quote of mintQuotes) { - allHistory.push({ - id: quote.quoteId, - type: 'mint', - amount: quote.amount, - mintUrl: quote.mintUrl, - status: quote.state === 'paid' ? 'completed' : quote.state === 'pending' ? 'pending' : 'failed', - timestamp: quote.createdAt || Date.now(), - quoteId: quote.quoteId, - invoice: quote.request, - }); - } + const mintQuotes = await listFrom(repos.mintQuoteRepository); + for (const quote of mintQuotes) allHistory.push(toEntry(quote, 'mint')); // Fetch melt quotes (send operations) - const meltQuotes = await (repositories as any).meltQuoteRepository?.getAll() || []; - for (const quote of meltQuotes) { - allHistory.push({ - id: quote.quoteId, - type: 'melt', - amount: quote.amount, - mintUrl: quote.mintUrl, - status: quote.state === 'paid' ? 'completed' : quote.state === 'pending' ? 'pending' : 'failed', - timestamp: quote.createdAt || Date.now(), - quoteId: quote.quoteId, - invoice: quote.request, - }); - } + const meltQuotes = await listFrom(repos.meltQuoteRepository); + for (const quote of meltQuotes) allHistory.push(toEntry(quote, 'melt')); // Sort by timestamp (newest first) allHistory.sort((a, b) => b.timestamp - a.timestamp); diff --git a/src/hooks/wallet/useLightningOperations.ts b/src/hooks/wallet/useLightningOperations.ts index 6d96cfd..6c5962d 100644 --- a/src/hooks/wallet/useLightningOperations.ts +++ b/src/hooks/wallet/useLightningOperations.ts @@ -90,10 +90,18 @@ export function useLightningOperations({ mintQuoteUnsubscribeRef.current = unsubscribe; } catch (err) { - toast({ - variant: 'destructive', - title: 'Failed to create invoice', - description: err instanceof Error ? err.message : 'Unknown error' + const raw = err instanceof Error ? err.message : 'Unknown error'; + // A CORS-blocked or unreachable mint surfaces as a NetworkError / + // "Failed to fetch mint". Give the user something actionable instead of a + // dead end — the fix is to switch to a browser-reachable mint. + const looksUnreachable = + /failed to fetch|networkerror|load failed|cors|fetch mint/i.test(raw); + toast({ + variant: 'destructive', + title: looksUnreachable ? "Can't reach this mint" : 'Failed to create invoice', + description: looksUnreachable + ? `${activeMintUrl?.replace(/^https?:\/\//, '')} can't be reached from your browser (it may not allow web requests). Switch to a different mint in wallet settings and try again.` + : raw, }); } }, [coco, quoteAmount, activeMintUrl, generateQRCode, toast]); @@ -118,10 +126,15 @@ export function useLightningOperations({ setLightningInvoice(''); } catch (err) { - toast({ - variant: 'destructive', - title: 'Payment failed', - description: err instanceof Error ? err.message : 'Unknown error' + const raw = err instanceof Error ? err.message : 'Unknown error'; + const looksUnreachable = + /failed to fetch|networkerror|load failed|cors|fetch mint/i.test(raw); + toast({ + variant: 'destructive', + title: looksUnreachable ? "Can't reach this mint" : 'Payment failed', + description: looksUnreachable + ? `${activeMintUrl?.replace(/^https?:\/\//, '')} can't be reached from your browser. Switch to a different mint in wallet settings and try again.` + : raw, }); } finally { setIsProcessingPayment(false);