From 015e7b8b906f6de310bcd5927fdba274af0555de Mon Sep 17 00:00:00 2001 From: lemonknowsall Date: Tue, 21 Oct 2025 23:18:14 -0700 Subject: [PATCH] add mneumonic phrase for cashu mode and rename MintModal file --- src/components/wallet/CashuWallet.tsx | 52 +++++++- src/components/wallet/UTXOSelector.tsx | 120 ++++++++++++++++++ src/components/wallet/WalletHeader.tsx | 63 ++++++++- .../{MintModals.tsx => WalletModals.tsx} | 72 ++++++++++- 4 files changed, 303 insertions(+), 4 deletions(-) create mode 100644 src/components/wallet/UTXOSelector.tsx rename src/components/wallet/{MintModals.tsx => WalletModals.tsx} (74%) diff --git a/src/components/wallet/CashuWallet.tsx b/src/components/wallet/CashuWallet.tsx index c45f0c8..c254349 100644 --- a/src/components/wallet/CashuWallet.tsx +++ b/src/components/wallet/CashuWallet.tsx @@ -12,11 +12,11 @@ import { useWalletTransactions } from '@/hooks/cashu/useWalletTransactions'; import { useHistoryManager } from '@/hooks/cashu/useHistoryManager'; import { useNsecAccess } from '@/hooks/useNsecAccess'; import { useBitcoinBalance } from '@/hooks/useBitcoinBalance'; -import { nostrPubkeyToBitcoinAddress, npubToBitcoinAddress, fetchUTXOs, getFeeRates, createBitcoinTransaction, broadcastTransaction } from '@/lib/bitcoin'; +import { nostrPubkeyToBitcoinAddress, npubToBitcoinAddress, fetchUTXOs, getFeeRates, createBitcoinTransaction, broadcastTransaction, UTXO } from '@/lib/bitcoin'; import { QRModal } from '@/components/ui/qr-modal'; import { WalletHeader } from './WalletHeader'; import { TransactionCard } from './PaymentCard'; -import { WalletModals } from './MintModals'; +import { WalletModals } from './WalletModals'; import { Drawer, DrawerContent, DrawerHeader, DrawerTitle } from '@/components/ui/drawer'; import { TransactionHistory } from './TransactionHistory'; import { ScrollArea } from '@/components/ui/scroll-area'; @@ -36,6 +36,7 @@ export function CashuWallet() { error, balances, mints, + mnemonic, getEncodedToken, refreshMints, } = useCashu(); @@ -169,6 +170,12 @@ export function CashuWallet() { const [pendingBitcoinRecipient, setPendingBitcoinRecipient] = useState(null); const [showBitcoinAddressQR, setShowBitcoinAddressQR] = useState(false); const [bitcoinAddressQRCode, setBitcoinAddressQRCode] = useState(''); + const [showSeedPhraseInfo, setShowSeedPhraseInfo] = useState(false); + + // UTXO state + const [utxos, setUtxos] = useState([]); + const [utxoDropdownOpen, setUtxoDropdownOpen] = useState(false); + const [isLoadingUtxos, setIsLoadingUtxos] = useState(false); // Handle drawer close with state reset const handleSendDrawerClose = (open: boolean) => { @@ -296,6 +303,26 @@ export function CashuWallet() { } }; + // Fetch UTXOs for Bitcoin mode + const fetchBitcoinUtxos = async () => { + if (!bitcoinAddress || mode !== 'bitcoin') return; + + setIsLoadingUtxos(true); + try { + const fetchedUtxos = await fetchUTXOs(bitcoinAddress); + setUtxos(fetchedUtxos); + } catch (error) { + console.error('Error fetching UTXOs:', error); + toast({ + variant: "destructive", + title: "UTXO Error", + description: "Failed to fetch Bitcoin UTXOs", + }); + } finally { + setIsLoadingUtxos(false); + } + }; + // ============================================================================ // COMPUTED VALUES // ============================================================================ @@ -389,6 +416,19 @@ export function CashuWallet() { }; }, [isNpubCashLoggedIn, coco, isInitialized, syncQuotesWithWallet, toast]); + // ============================================================================ + // EFFECTS - UTXO Fetching + // ============================================================================ + + useEffect(() => { + if (mode === 'bitcoin' && bitcoinAddress) { + fetchBitcoinUtxos(); + } else { + // Clear UTXOs when not in Bitcoin mode + setUtxos([]); + } + }, [mode, bitcoinAddress]); + // ============================================================================ // EFFECTS - Cleanup // ============================================================================ @@ -420,6 +460,7 @@ export function CashuWallet() { setMode={setMode} userPubkey={user?.pubkey} npubCashUsername={user?.pubkey ? (npubCashUserInfo?.name || nip19.npubEncode(user.pubkey)) : undefined} + mnemonic={mnemonic} onCopyToClipboard={copyToClipboard} onShowLightningAddressQR={(qr) => { setLightningAddressQRCode(qr); @@ -429,6 +470,7 @@ export function CashuWallet() { setBitcoinAddressQRCode(qr); setShowBitcoinAddressQR(true); }} + onShowSeedPhraseInfo={() => setShowSeedPhraseInfo(true)} generateQRCode={generateQRCode} // MintSelector props activeMintUrl={activeMintUrl} @@ -442,6 +484,10 @@ export function CashuWallet() { getCleanMintLabel={getCleanMintLabel} dropdownOpen={mintDropdownOpen} setDropdownOpen={setMintDropdownOpen} + // UTXOSelector props + utxos={utxos} + utxoDropdownOpen={utxoDropdownOpen} + setUtxoDropdownOpen={setUtxoDropdownOpen} /> @@ -504,6 +550,8 @@ export function CashuWallet() { sentToken={sentToken} setSentToken={setSentToken} sentTokenQRCode={sentTokenQRCode} + showSeedPhraseInfo={showSeedPhraseInfo} + setShowSeedPhraseInfo={setShowSeedPhraseInfo} /> {/* Bottom Drawer Modals */} diff --git a/src/components/wallet/UTXOSelector.tsx b/src/components/wallet/UTXOSelector.tsx new file mode 100644 index 0000000..188d4a8 --- /dev/null +++ b/src/components/wallet/UTXOSelector.tsx @@ -0,0 +1,120 @@ +import { Button } from '@/components/ui/button'; +import { ChevronDown, Wallet } from 'lucide-react'; +import { useState, useEffect } from 'react'; +import { exchangeRateService } from '@/lib/exchangeRateService'; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuTrigger, +} from '@/components/ui/dropdown-menu'; +import { UTXO } from '@/lib/bitcoin'; + +interface UTXOSelectorProps { + utxos: UTXO[]; + showSats: boolean; + bitcoinAddress: string; + dropdownOpen: boolean; + setDropdownOpen: (open: boolean) => void; +} + +export function UTXOSelector({ + utxos, + showSats, + bitcoinAddress, + dropdownOpen, + setDropdownOpen, +}: UTXOSelectorProps) { + const [btcUsdRate, setBtcUsdRate] = useState(null); + + useEffect(() => { + exchangeRateService.getBtcUsdRate() + .then(rate => setBtcUsdRate(rate)) + .catch(() => setBtcUsdRate(null)); + }, []); + + const formatBalance = (sats: number): string => { + if (showSats) { + return `₿${sats.toLocaleString()}`; + } else if (btcUsdRate) { + const btcAmount = sats / 100000000; + const usd = btcAmount * btcUsdRate; + return `$${usd.toFixed(2)}`; + } else { + return `₿${sats.toLocaleString()}`; + } + }; + + const totalBalance = utxos.reduce((sum, utxo) => sum + utxo.value, 0); + + const formatUTXOId = (txid: string, vout: number): string => { + return `${txid.slice(0, 8)}...${txid.slice(-8)}:${vout}`; + }; + + return ( +
+ + + + + +
+ {utxos.length === 0 ? ( + + No UTXOs available + + ) : ( + utxos.map((utxo, index) => ( + +
+
+
+ {formatUTXOId(utxo.txid, utxo.vout)} +
+
+ {utxo.status.confirmed ? 'Confirmed' : 'Unconfirmed'} +
+
+
+
+ + {formatBalance(utxo.value)} + +
+
+ )) + )} +
+
+
+ +
+ +
+
+ ); +} diff --git a/src/components/wallet/WalletHeader.tsx b/src/components/wallet/WalletHeader.tsx index 945739e..17caf4f 100644 --- a/src/components/wallet/WalletHeader.tsx +++ b/src/components/wallet/WalletHeader.tsx @@ -1,10 +1,12 @@ import { useState, useEffect } from 'react'; -import { Zap, Coins, Copy, Check, QrCode, Link } from 'lucide-react'; +import { Zap, Coins, Copy, Check, QrCode, Link, Key } from 'lucide-react'; import { exchangeRateService } from '@/lib/exchangeRateService'; import { useToast } from '@/hooks/useToast'; import { LoginIcon } from '@/components/auth/LoginIcon'; import { MintSelector } from './MintSelector'; +import { UTXOSelector } from './UTXOSelector'; import { nostrPubkeyToBitcoinAddress } from '@/lib/bitcoin'; +import { UTXO } from '@/lib/bitcoin'; interface CashuMint { url: string; @@ -20,9 +22,11 @@ interface WalletHeaderProps { setMode: (mode: 'tokens' | 'lightning' | 'bitcoin') => void; userPubkey?: string; npubCashUsername?: string; + mnemonic?: string | null; onCopyToClipboard: (text: string) => Promise; onShowLightningAddressQR: (qrCode: string) => void; onShowBitcoinAddressQR: (qrCode: string) => void; + onShowSeedPhraseInfo: () => void; generateQRCode: (text: string) => Promise; // MintSelector props activeMintUrl: string | null; @@ -36,6 +40,10 @@ interface WalletHeaderProps { getCleanMintLabel: (mint: { name?: string; url: string }) => string; dropdownOpen: boolean; setDropdownOpen: (open: boolean) => void; + // UTXOSelector props + utxos: UTXO[]; + utxoDropdownOpen: boolean; + setUtxoDropdownOpen: (open: boolean) => void; } // Helper functions to prevent syntax errors in complex expressions @@ -102,9 +110,11 @@ export function WalletHeader({ setMode, userPubkey, npubCashUsername, + mnemonic, onCopyToClipboard, onShowLightningAddressQR, onShowBitcoinAddressQR, + onShowSeedPhraseInfo, generateQRCode, // MintSelector props activeMintUrl, @@ -118,9 +128,14 @@ export function WalletHeader({ getCleanMintLabel, dropdownOpen, setDropdownOpen, + // UTXOSelector props + utxos, + utxoDropdownOpen, + setUtxoDropdownOpen, }: WalletHeaderProps) { const [lightningAddressCopied, setLightningAddressCopied] = useState(false); const [bitcoinAddressCopied, setBitcoinAddressCopied] = useState(false); + const [mnemonicCopied, setMnemonicCopied] = useState(false); const [btcUsdRate, setBtcUsdRate] = useState(null); const { toast } = useToast(); @@ -233,6 +248,39 @@ export function WalletHeader({ )} + {/* Mnemonic Seed Phrase Row */} + {mode === 'tokens' && mnemonic && ( +
+ + +
+ )} + {/* Bitcoin Address Row */} {userPubkey && mode === 'bitcoin' && (() => { const bitcoinAddress = nostrPubkeyToBitcoinAddress(userPubkey); @@ -271,6 +319,19 @@ export function WalletHeader({ ) : null; })()} + + {/* UTXO Selector Row */} + {mode === 'bitcoin' && ( +
+ +
+ )} {/* Mint Selector Row */} {mode !== 'bitcoin' && ( diff --git a/src/components/wallet/MintModals.tsx b/src/components/wallet/WalletModals.tsx similarity index 74% rename from src/components/wallet/MintModals.tsx rename to src/components/wallet/WalletModals.tsx index da7dde8..dbdbbdb 100644 --- a/src/components/wallet/MintModals.tsx +++ b/src/components/wallet/WalletModals.tsx @@ -1,7 +1,7 @@ import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; -import { X, Coins, AlertTriangle, RefreshCw } from 'lucide-react'; +import { X, Coins, AlertTriangle, RefreshCw, Key } from 'lucide-react'; import { QRModal } from '@/components/ui/qr-modal'; import { nip19 } from 'nostr-tools'; import { useToast } from '@/hooks/useToast'; @@ -59,6 +59,10 @@ interface WalletModalProps { sentToken: string | null; setSentToken: (token: string | null) => void; sentTokenQRCode: string; + + // Seed Phrase Info Modal + showSeedPhraseInfo: boolean; + setShowSeedPhraseInfo: (show: boolean) => void; } export function WalletModals({ @@ -82,6 +86,8 @@ export function WalletModals({ sentToken, setSentToken, sentTokenQRCode, + showSeedPhraseInfo, + setShowSeedPhraseInfo, }: WalletModalProps) { const { toast } = useToast(); return ( @@ -260,6 +266,70 @@ export function WalletModals({ icon="qr" /> )} + + {/* Seed Phrase Info Modal */} + {showSeedPhraseInfo && ( +
setShowSeedPhraseInfo(false)} + > +
e.stopPropagation()} + > +
+ +
+ +

What is a Seed Phrase?

+
+

+ Your seed phrase is a secure way to backup and restore your wallet +

+ +
+
+

🔐 Recovery Key

+

+ Your seed phrase is a 12-word recovery key that allows you to restore your wallet + on any device. It's generated using industry-standard BIP39 wordlists. +

+
+ +
+

💾 Backup Important

+

+ Write down your seed phrase and store it securely offline. Anyone with access + to your seed phrase can control your wallet and funds. +

+
+ +
+

🛡️ Security Tips

+
    +
  • • Never share your seed phrase with anyone
  • +
  • • Store it offline in a secure location
  • +
  • • Don't take screenshots or store it digitally
  • +
  • • Use it only to restore your wallet
  • +
+
+ +
+

+ Note: The seed phrase shown above is masked for privacy. + Click the copy button to access the actual phrase for backup purposes. +

+
+
+
+
+
+ )} ); }