add mneumonic phrase for cashu mode and rename MintModal file

This commit is contained in:
lemonknowsall committed 2025-10-21 23:18:14 -07:00
1 parent 3b8c5da1fd
commit 015e7b8b90
4 files changed
+303 -4

No files matched your search

+50 -2
View File
@@ -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<string | null>(null);
const [showBitcoinAddressQR, setShowBitcoinAddressQR] = useState(false);
const [bitcoinAddressQRCode, setBitcoinAddressQRCode] = useState('');
const [showSeedPhraseInfo, setShowSeedPhraseInfo] = useState(false);
// UTXO state
const [utxos, setUtxos] = useState<UTXO[]>([]);
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 */}
+120
View File
@@ -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<number | null>(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 (
<div className="flex gap-2 items-center min-w-0">
<DropdownMenu open={dropdownOpen} onOpenChange={setDropdownOpen}>
<DropdownMenuTrigger asChild>
<Button
variant="outline"
className="justify-between rounded-full bg-muted hover:bg-muted/80 border text-base flex-1 h-[40px] px-3 min-w-0"
disabled={utxos.length === 0}
>
<div className="flex items-center justify-between w-full min-w-0">
<div className="flex items-center gap-2 min-w-0 flex-1">
<span className="truncate font-medium min-w-0 flex-1">
{utxos.length === 0 ? 'No UTXOs' : `${utxos.length} UTXO${utxos.length !== 1 ? 's' : ''}`}
</span>
</div>
<span className="text-muted-foreground font-medium flex-shrink-0 ml-2">
{formatBalance(totalBalance)}
</span>
</div>
<ChevronDown className="h-5 w-5 ml-2 flex-shrink-0" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-[var(--radix-dropdown-menu-trigger-width)] bg-muted border shadow-none max-h-[200px] overflow-hidden">
<div className="max-h-[120px] overflow-y-auto">
{utxos.length === 0 ? (
<DropdownMenuItem disabled className="flex items-center justify-center p-3 text-base text-muted-foreground">
No UTXOs available
</DropdownMenuItem>
) : (
utxos.map((utxo, index) => (
<DropdownMenuItem
key={`utxo-${utxo.txid}-${utxo.vout}`}
className="flex items-center justify-between p-3 text-base"
>
<div className="flex items-center gap-3 flex-1 min-w-0">
<div className="flex-1 min-w-0">
<div className="font-medium truncate text-sm">
{formatUTXOId(utxo.txid, utxo.vout)}
</div>
<div className="text-xs text-muted-foreground">
{utxo.status.confirmed ? 'Confirmed' : 'Unconfirmed'}
</div>
</div>
</div>
<div className="flex items-center gap-2 flex-shrink-0">
<span className="px-3 py-1 text-sm font-medium text-muted-foreground">
{formatBalance(utxo.value)}
</span>
</div>
</DropdownMenuItem>
))
)}
</div>
</DropdownMenuContent>
</DropdownMenu>
<div className="flex flex-col gap-1">
<button
className="flex items-center justify-center px-2 py-1.5 hover:bg-muted rounded-md transition-colors h-[40px] w-[42px] flex-shrink-0"
title="Bitcoin wallet"
>
<Wallet className="h-4 w-4" />
</button>
</div>
</div>
);
}
+62 -1
View File
@@ -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<void>;
onShowLightningAddressQR: (qrCode: string) => void;
onShowBitcoinAddressQR: (qrCode: string) => void;
onShowSeedPhraseInfo: () => void;
generateQRCode: (text: string) => Promise<string>;
// 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<number | null>(null);
const { toast } = useToast();
@@ -233,6 +248,39 @@ export function WalletHeader({
</div>
)}
{/* Mnemonic Seed Phrase Row */}
{mode === 'tokens' && mnemonic && (
<div className="flex gap-2 items-center min-w-[300px]">
<button
onClick={async () => {
await onCopyToClipboard(mnemonic);
setMnemonicCopied(true);
setTimeout(() => setMnemonicCopied(false), 2000);
}}
className="flex items-center gap-2 px-3 py-1.5 bg-muted hover:bg-muted/80 rounded-md border text-base font-mono transition-colors flex-1 h-[40px] min-w-0"
title="Click to copy seed phrase"
>
<div className="min-w-0 flex-1 flex items-center">
<span className="truncate min-w-0 flex-1">
{mnemonic.split(' ').map(() => '****').join(' ')}
</span>
</div>
{mnemonicCopied ? (
<Check className="h-4 w-4 text-green-600 flex-shrink-0" />
) : (
<Copy className="h-4 w-4 text-muted-foreground flex-shrink-0" />
)}
</button>
<button
onClick={onShowSeedPhraseInfo}
className="flex items-center justify-center px-2 py-1.5 hover:bg-muted rounded-md transition-colors h-[40px] w-[42px] flex-shrink-0"
title="What is a seed phrase?"
>
<Key className="h-4 w-4" />
</button>
</div>
)}
{/* Bitcoin Address Row */}
{userPubkey && mode === 'bitcoin' && (() => {
const bitcoinAddress = nostrPubkeyToBitcoinAddress(userPubkey);
@@ -271,6 +319,19 @@ export function WalletHeader({
</div>
) : null;
})()}
{/* UTXO Selector Row */}
{mode === 'bitcoin' && (
<div className="min-w-[300px]">
<UTXOSelector
utxos={utxos}
showSats={showSats}
bitcoinAddress={userPubkey ? nostrPubkeyToBitcoinAddress(userPubkey) : ''}
dropdownOpen={utxoDropdownOpen}
setDropdownOpen={setUtxoDropdownOpen}
/>
</div>
)}
{/* Mint Selector Row */}
{mode !== 'bitcoin' && (
@@ -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 && (
<div
className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4"
onClick={() => setShowSeedPhraseInfo(false)}
>
<div
className="w-full max-w-md border-0 shadow-lg bg-background rounded-lg"
onClick={(e) => e.stopPropagation()}
>
<div className="relative p-6">
<button
onClick={() => setShowSeedPhraseInfo(false)}
className="absolute top-4 right-4 p-2 hover:bg-muted rounded-md transition-colors"
>
<X className="h-4 w-4" />
</button>
<div className="flex items-center mb-4 pr-8">
<Key className="h-5 w-5 mr-2" />
<h2 className="text-lg font-semibold">What is a Seed Phrase?</h2>
</div>
<p className="text-muted-foreground mb-6">
Your seed phrase is a secure way to backup and restore your wallet
</p>
<div className="space-y-4 text-sm">
<div>
<h4 className="font-semibold mb-2">🔐 Recovery Key</h4>
<p className="text-muted-foreground">
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.
</p>
</div>
<div>
<h4 className="font-semibold mb-2">💾 Backup Important</h4>
<p className="text-muted-foreground">
Write down your seed phrase and store it securely offline. Anyone with access
to your seed phrase can control your wallet and funds.
</p>
</div>
<div>
<h4 className="font-semibold mb-2">🛡️ Security Tips</h4>
<ul className="text-muted-foreground space-y-1 ml-4">
<li>• Never share your seed phrase with anyone</li>
<li>• Store it offline in a secure location</li>
<li>• Don't take screenshots or store it digitally</li>
<li>• Use it only to restore your wallet</li>
</ul>
</div>
<div className="bg-muted p-3 rounded-md">
<p className="text-xs text-muted-foreground">
<strong>Note:</strong> The seed phrase shown above is masked for privacy.
Click the copy button to access the actual phrase for backup purposes.
</p>
</div>
</div>
</div>
</div>
</div>
)}
</>
);
}