diff --git a/src/components/CashuWallet.tsx b/src/components/CashuWallet.tsx index 9e39edf..6f8f1ca 100644 --- a/src/components/CashuWallet.tsx +++ b/src/components/CashuWallet.tsx @@ -31,6 +31,7 @@ export function CashuWallet() { createMeltQuote, payMeltQuote, redeemMintQuote, + addMintQuote, refreshBalances, refreshMints, checkAndRedeemPendingQuotes, @@ -505,7 +506,6 @@ export function CashuWallet() { mint_url: quote.mintUrl, amount: quote.amount, state: quote.state, - request: quote.request?.substring(0, 50) + '...' }); // Add mint if not already added @@ -515,37 +515,15 @@ export function CashuWallet() { await addMint(quote.mintUrl); } - // Call the mint's API directly since this is an external quote - console.log('🔄 Redeeming npubx.cash quote via mint API:', quote.quoteId, 'from mint:', quote.mintUrl); - console.log('🔄 Lightning invoice:', quote.request); + // Use coco-cashu's addMintQuote API instead of calling mint directly + console.log('🔄 Adding mint quote to coco-cashu:', quote.quoteId); + await addMintQuote(quote.mintUrl, quote.quoteId); - // Try the /mint endpoint for quote redemption - const response = await fetch(`${quote.mintUrl}/mint`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - quote: quote.quoteId, // Use the quote ID to redeem - }), - }); - - if (!response.ok) { - const errorData = await response.json().catch(() => ({ message: 'Failed to redeem quote' })); - throw new Error(errorData.message || `Mint API error: ${response.status}`); - } - - const data = await response.json(); - console.log('✅ Mint API response:', data); - - if (data.paid && data.proofs) { - // We received tokens, now we need to add them to our wallet - console.log('🔄 Adding received tokens to wallet...'); - await receive(JSON.stringify(data.proofs)); - console.log('✅ Successfully claimed tokens from npubx.cash quote'); - } else { - throw new Error('No tokens received from mint'); - } + console.log('✅ Quote added to coco-cashu successfully'); + console.log('💡 Coco will automatically redeem when mint processor runs'); + + // Trigger the mint processor to check and redeem the quote + await checkAndRedeemPendingQuotes(); // Refresh quotes and balances await fetchQuotes(); @@ -557,7 +535,6 @@ export function CashuWallet() { alert(`Failed to claim: ${err instanceof Error ? err.message : 'Unknown error'}`); } }; - const handleShowDenominations = async (mintUrl: string) => { try { console.log('🔍 Getting token denominations for mint:', mintUrl); diff --git a/src/hooks/useCashu.ts b/src/hooks/useCashu.ts index 772f034..550b663 100644 --- a/src/hooks/useCashu.ts +++ b/src/hooks/useCashu.ts @@ -85,6 +85,9 @@ export interface CashuActions { refreshMints: () => Promise; checkAndRedeemPendingQuotes: () => Promise; clearError: () => void; + + // Add mint quote (for external quotes like from npubx.cash) + addMintQuote: (mintUrl: string, quoteId: string) => Promise; } export type UseCashuReturn = CashuState & CashuActions; @@ -581,6 +584,42 @@ export function useCashu(): UseCashuReturn { } }, [refreshBalances]); + // Add this implementation in the useCashu function + const addMintQuote = useCallback(async (mintUrl: string, quoteId: string) => { + if (!managerRef.current || !repositoriesRef.current) throw new Error('Wallet not initialized'); + + try { + setError(null); + console.log('📝 Adding mint quote to repository:', { mintUrl, quoteId }); + + // Access the mint quote repository + const mintQuoteRepo = (repositoriesRef.current as { + mintQuoteRepository?: { + addMintQuote: (quote: { quote: string; mintUrl: string; state: string }) => Promise + } + })?.mintQuoteRepository; + + if (!mintQuoteRepo) { + throw new Error('Mint quote repository not available'); + } + + // Add the quote to the repository with UNPAID state + // The mint processor will check and redeem it + await mintQuoteRepo.addMintQuote({ + quote: quoteId, + mintUrl: mintUrl, + state: 'UNPAID', // Mark as UNPAID so coco will check it + }); + + console.log('✅ Mint quote added to repository successfully'); + } catch (err) { + const errorMessage = err instanceof Error ? err.message : 'Failed to add mint quote'; + console.error('❌ Error adding mint quote:', err); + setError(errorMessage); + throw err; + } + }, []); + const createMeltQuote = useCallback(async (mintUrl: string, invoice: string): Promise => { if (!managerRef.current) throw new Error('Wallet not initialized'); @@ -692,5 +731,6 @@ export function useCashu(): UseCashuReturn { refreshMints, checkAndRedeemPendingQuotes, clearError, + addMintQuote, }; }