Fix wallet invoice failure (CORS-blocked mint) + history crash
- 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 <assistant@shakespeare.diy>
This commit is contained in:
parent
40e4a27565
commit
0078f75f94
4 changed files with 81 additions and 43 deletions
|
|
@ -43,23 +43,26 @@ interface MintPrivacyManagerProps {
|
||||||
className?: string;
|
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 = [
|
const RECOMMENDED_MINTS = [
|
||||||
{
|
|
||||||
url: 'https://mint.minibits.cash/Bitcoin',
|
|
||||||
name: 'Minibits',
|
|
||||||
description: 'Popular mobile-friendly mint',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
url: 'https://mint.coinos.io',
|
url: 'https://mint.coinos.io',
|
||||||
name: 'Coinos',
|
name: 'Coinos',
|
||||||
description: 'Web-based mint from Canada',
|
description: 'Web-friendly mint (recommended for browsers)',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
url: 'https://legend.lnbits.com/cashu/api/v1/AptDNABNBXv8gpuywhx6NV',
|
url: 'https://legend.lnbits.com/cashu/api/v1/AptDNABNBXv8gpuywhx6NV',
|
||||||
name: 'LNbits Legend',
|
name: 'LNbits Legend',
|
||||||
description: 'Community mint on LNbits',
|
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({
|
export function MintPrivacyManager({
|
||||||
|
|
|
||||||
|
|
@ -166,9 +166,13 @@ export function useCashu(): UseCashuReturn {
|
||||||
console.warn('[Cashu] Failed to enable some coco services:', err);
|
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 {
|
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 existingMints = await coco.mint.getAllTrustedMints();
|
||||||
const hasDefaultMint = existingMints.some((m: Mint) =>
|
const hasDefaultMint = existingMints.some((m: Mint) =>
|
||||||
m.mintUrl.toLowerCase() === DEFAULT_MINT_URL.toLowerCase()
|
m.mintUrl.toLowerCase() === DEFAULT_MINT_URL.toLowerCase()
|
||||||
|
|
|
||||||
|
|
@ -34,35 +34,53 @@ export function useHistoryManager({
|
||||||
try {
|
try {
|
||||||
const allHistory: HistoryEntry[] = [];
|
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<Array<Record<string, unknown>>> => {
|
||||||
|
if (!repo || typeof repo !== 'object') return [];
|
||||||
|
const r = repo as Record<string, unknown>;
|
||||||
|
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<unknown>).call(repo);
|
||||||
|
if (Array.isArray(res)) return res as Array<Record<string, unknown>>;
|
||||||
|
} catch {
|
||||||
|
// try the next candidate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
};
|
||||||
|
|
||||||
|
const repos = repositories as Record<string, unknown>;
|
||||||
|
|
||||||
|
const toEntry = (
|
||||||
|
quote: Record<string, unknown>,
|
||||||
|
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)
|
// Fetch mint quotes (receive operations)
|
||||||
const mintQuotes = await (repositories as any).mintQuoteRepository?.getAll() || [];
|
const mintQuotes = await listFrom(repos.mintQuoteRepository);
|
||||||
for (const quote of mintQuotes) {
|
for (const quote of mintQuotes) allHistory.push(toEntry(quote, 'mint'));
|
||||||
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,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch melt quotes (send operations)
|
// Fetch melt quotes (send operations)
|
||||||
const meltQuotes = await (repositories as any).meltQuoteRepository?.getAll() || [];
|
const meltQuotes = await listFrom(repos.meltQuoteRepository);
|
||||||
for (const quote of meltQuotes) {
|
for (const quote of meltQuotes) allHistory.push(toEntry(quote, 'melt'));
|
||||||
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,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort by timestamp (newest first)
|
// Sort by timestamp (newest first)
|
||||||
allHistory.sort((a, b) => b.timestamp - a.timestamp);
|
allHistory.sort((a, b) => b.timestamp - a.timestamp);
|
||||||
|
|
|
||||||
|
|
@ -90,10 +90,18 @@ export function useLightningOperations({
|
||||||
|
|
||||||
mintQuoteUnsubscribeRef.current = unsubscribe;
|
mintQuoteUnsubscribeRef.current = unsubscribe;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
toast({
|
const raw = err instanceof Error ? err.message : 'Unknown error';
|
||||||
variant: 'destructive',
|
// A CORS-blocked or unreachable mint surfaces as a NetworkError /
|
||||||
title: 'Failed to create invoice',
|
// "Failed to fetch mint". Give the user something actionable instead of a
|
||||||
description: err instanceof Error ? err.message : 'Unknown error'
|
// 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]);
|
}, [coco, quoteAmount, activeMintUrl, generateQRCode, toast]);
|
||||||
|
|
@ -118,10 +126,15 @@ export function useLightningOperations({
|
||||||
|
|
||||||
setLightningInvoice('');
|
setLightningInvoice('');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
toast({
|
const raw = err instanceof Error ? err.message : 'Unknown error';
|
||||||
variant: 'destructive',
|
const looksUnreachable =
|
||||||
title: 'Payment failed',
|
/failed to fetch|networkerror|load failed|cors|fetch mint/i.test(raw);
|
||||||
description: err instanceof Error ? err.message : 'Unknown error'
|
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 {
|
} finally {
|
||||||
setIsProcessingPayment(false);
|
setIsProcessingPayment(false);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue