-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.rs
More file actions
788 lines (719 loc) · 22.7 KB
/
main.rs
File metadata and controls
788 lines (719 loc) · 22.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use colored::Colorize;
use rustyline::DefaultEditor;
use rustyline::error::ReadlineError;
use orange_sdk::bitcoin_payment_instructions::amount::Amount;
use orange_sdk::{
CashuConfig, ChainSource, CurrencyUnit, Event, ExtraConfig, LoggerType, Mnemonic, PaymentInfo,
Seed, SparkWalletConfig, StorageConfig, Tunables, Wallet, WalletConfig, bitcoin::Network,
};
use rand::RngCore;
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::signal;
const NETWORK: Network = Network::Bitcoin; // Supports Bitcoin and Regtest
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
/// Use Cashu wallet instead of Spark
#[arg(long)]
cashu: bool,
/// Cashu mint URL (requires --cashu)
#[arg(long, requires = "cashu")]
mint_url: String,
/// npub.cash URL for lightning address support (requires --cashu)
#[arg(long, requires = "cashu")]
npubcash_url: Option<String>,
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
/// Get wallet balance
Balance,
/// Send a payment
Send {
/// Destination address or invoice
destination: String,
/// Amount in sats (optional)
amount: Option<u64>,
},
/// Receive a payment
Receive {
/// Amount in sats (optional)
amount: Option<u64>,
},
/// Show wallet status
Status,
/// List recent transactions
Transactions,
/// List our lightning Channels
Channels,
/// Estimate the fee for a payment
EstimateFee {
/// Destination address or invoice
destination: String,
/// Amount in sats (optional)
amount: Option<u64>,
},
/// Get the current lightning address
GetLightningAddress,
/// Register a lightning address
RegisterLightningAddress {
/// The lightning address name to register
name: String,
},
/// Clear the screen
Clear,
/// Exit the application
Exit,
}
struct WalletState {
wallet: Wallet,
shutdown: Arc<AtomicBool>,
}
fn get_config(network: Network, cli: &Cli) -> Result<WalletConfig> {
let storage_path = format!("./wallet_data/{network}");
// Generate or load seed
let seed = generate_or_load_seed(&storage_path)?;
let extra_config = if cli.cashu {
ExtraConfig::Cashu(CashuConfig {
mint_url: cli.mint_url.clone(),
unit: CurrencyUnit::Sat,
npubcash_url: cli.npubcash_url.clone(),
})
} else {
ExtraConfig::Spark(SparkWalletConfig::default())
};
match network {
Network::Regtest => {
let lsp_address = "185.150.162.100:3551"
.parse()
.map_err(|_| anyhow::anyhow!("Failed to parse LSP address"))?;
let lsp_pubkey = "02a88abd44b3cfc9c0eb7cd93f232dc473de4f66bcea0ee518be70c3b804c90201"
.parse()
.context("Failed to parse LSP public key")?;
Ok(WalletConfig {
storage_config: StorageConfig::LocalSQLite(storage_path.to_string()),
logger_type: LoggerType::File {
path: PathBuf::from(format!("{storage_path}/wallet.log")),
},
chain_source: ChainSource::Electrum(
"tcp://spark-regtest.benthecarman.com:50001".to_string(),
),
lsp: (lsp_address, lsp_pubkey, None),
scorer_url: None,
rgs_url: None,
network,
seed,
tunables: Tunables::default(),
extra_config,
})
},
Network::Bitcoin => {
// Matt's LSP config for demo
let lsp_address = "69.59.18.144:9735"
.parse()
.map_err(|_| anyhow::anyhow!("Failed to parse LSP address"))?;
let lsp_pubkey = "021deaa26ce6bb7cc63bd30e83a2bba1c0368269fa3bb9b616a24f40d941ac7d32"
.parse()
.context("Failed to parse LSP public key")?;
let lsp_token = Some("DeveloperTestingOnly".to_string());
Ok(WalletConfig {
storage_config: StorageConfig::LocalSQLite(storage_path.to_string()),
logger_type: LoggerType::File {
path: PathBuf::from(format!("{storage_path}/wallet.log")),
},
chain_source: ChainSource::Esplora {
url: "https://blockstream.info/api".to_string(),
username: None,
password: None,
},
lsp: (lsp_address, lsp_pubkey, lsp_token),
scorer_url: None,
rgs_url: None,
network,
seed,
tunables: Tunables::default(),
extra_config,
})
},
_ => Err(anyhow::anyhow!("Unsupported network: {network:?}")),
}
}
impl WalletState {
async fn new(cli: &Cli) -> Result<Self> {
let shutdown = Arc::new(AtomicBool::new(false));
let config = get_config(NETWORK, cli)
.with_context(|| format!("Failed to get wallet config for network: {NETWORK:?}"))?;
println!("{} Initializing wallet...", "⚡".bright_yellow());
match Wallet::new(config).await {
Ok(wallet) => {
println!("{} Wallet initialized successfully!", "✅".bright_green());
println!("Network: {}", NETWORK.to_string().bright_cyan());
let w = wallet.clone();
tokio::spawn(async move {
let event = w.next_event_async().await;
match event {
Event::PaymentSuccessful { payment_id, .. } => {
println!("{} Payment successful: {}", "✅".bright_green(), payment_id);
},
Event::PaymentFailed { payment_id, .. } => {
println!("{} Payment failed: {}", "❌".bright_red(), payment_id);
},
Event::PaymentReceived { payment_id, amount_msat, .. } => {
println!(
"{} Payment received: {} ({} msat)",
"📥".bright_green(),
payment_id,
amount_msat
);
},
Event::OnchainPaymentReceived { txid, amount_sat, .. } => {
println!(
"{} On-chain payment received: {} ({} sats)",
"📥".bright_green(),
txid,
amount_sat
);
},
Event::ChannelOpened { funding_txo, .. } => {
println!("{} Channel opened: {funding_txo}", "🔓".bright_green());
},
Event::ChannelClosed { reason, .. } => {
println!(
"{} Channel closed: {}",
"🔒".bright_red(),
reason
.map(|r| r.to_string())
.unwrap_or_else(|| "Unknown reason".to_string())
);
},
Event::RebalanceInitiated { amount_msat, .. } => {
println!(
"{} Rebalance initiated: {} msat",
"🔄".bright_yellow(),
amount_msat
);
},
Event::RebalanceSuccessful { amount_msat, fee_msat, .. } => {
println!(
"{} Rebalance successful: {} msat (fee: {} msat)",
"✅".bright_green(),
amount_msat,
fee_msat
);
},
Event::SplicePending { new_funding_txo, .. } => {
println!(
"{} Splice pending: {}",
"🔄".bright_yellow(),
new_funding_txo
);
},
}
w.event_handled().unwrap();
});
Ok(WalletState { wallet, shutdown })
},
Err(e) => Err(anyhow::anyhow!("Failed to initialize wallet: {:?}", e)),
}
}
fn wallet(&self) -> &Wallet {
&self.wallet
}
fn is_shutdown_requested(&self) -> bool {
self.shutdown.load(Ordering::Relaxed)
}
}
#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<()> {
let cli = Cli::parse();
println!("{}", "🟠 Orange CLI Wallet".bright_yellow().bold());
println!("{}", "Type 'help' for available commands or 'exit' to quit".dimmed());
println!();
// Initialize wallet once at startup
let mut state = WalletState::new(&cli).await?;
// Set up signal handling for graceful shutdown
let shutdown_state = state.shutdown.clone();
let shutdown_wallet = state.wallet.clone();
tokio::task::spawn(async move {
if let Ok(()) = signal::ctrl_c().await {
println!("\n{} Shutdown signal received, stopping wallet...", "⏹️".bright_yellow());
shutdown_state.store(true, Ordering::Relaxed);
shutdown_wallet.stop().await;
println!("{} Goodbye!", "👋".bright_green());
std::process::exit(0);
}
});
// If a command was provided via command line, execute it and start interactive mode
if let Some(command) = cli.command {
execute_command(command, &mut state).await?;
println!();
}
// Start interactive mode
start_interactive_mode(state).await
}
async fn start_interactive_mode(mut state: WalletState) -> Result<()> {
let mut rl = DefaultEditor::new().context("Failed to create readline editor")?;
loop {
// Check if shutdown was requested by signal handler
if state.is_shutdown_requested() {
break;
}
let prompt = format!("{} ", "orange>".bright_green().bold());
let readline = rl.readline(&prompt);
match readline {
Ok(line) => {
let line = line.trim();
if line.is_empty() {
continue;
}
rl.add_history_entry(line).ok();
match parse_command(line) {
Ok(command) => {
if let Err(e) = execute_command(command, &mut state).await {
println!("{} {}", "Error:".bright_red().bold(), e);
}
},
Err(e) => {
let error_msg = e.to_string();
if !error_msg.is_empty() {
println!("{} {}", "Parse error:".bright_red().bold(), e);
println!(
"{} Type 'help' for available commands",
"Hint:".bright_yellow().bold()
);
}
},
}
},
Err(ReadlineError::Interrupted) => {
println!("\n{} Stopping wallet...", "⏹️".bright_yellow());
state.wallet().stop().await;
println!("{} Goodbye!", "👋".bright_green());
break;
},
Err(ReadlineError::Eof) => {
println!("\n{} Stopping wallet...", "⏹️".bright_yellow());
state.wallet().stop().await;
println!("{} Goodbye!", "👋".bright_green());
break;
},
Err(err) => {
println!("{} {:?}", "Error:".bright_red().bold(), err);
break;
},
}
}
Ok(())
}
fn parse_command(input: &str) -> Result<Commands> {
let parts: Vec<&str> = input.split_whitespace().collect();
if parts.is_empty() {
return Err(anyhow::anyhow!("Empty command"));
}
match parts[0].to_lowercase().as_str() {
"balance" | "bal" => Ok(Commands::Balance),
"send" | "pay" => {
if parts.len() < 2 {
return Err(anyhow::anyhow!("Usage: send <destination> <amount>"));
}
let destination = parts[1].to_string();
let amount = if parts.len() > 2 {
Some(parts[2].parse::<u64>().context("Amount must be a valid number")?)
} else {
None
};
Ok(Commands::Send { destination, amount })
},
"receive" | "recv" => {
let amount = if parts.len() > 1 {
Some(parts[1].parse::<u64>().context("Amount must be a valid number")?)
} else {
None
};
Ok(Commands::Receive { amount })
},
"status" => Ok(Commands::Status),
"transactions" | "txs" | "tx" => Ok(Commands::Transactions),
"channels" | "chan" => Ok(Commands::Channels),
"estimate-fee" | "estimatefee" | "fee" => {
if parts.len() < 2 {
return Err(anyhow::anyhow!("Usage: estimate-fee <destination> [amount]"));
}
let destination = parts[1].to_string();
let amount = if parts.len() > 2 {
Some(parts[2].parse::<u64>().context("Amount must be a valid number")?)
} else {
None
};
Ok(Commands::EstimateFee { destination, amount })
},
"get-lightning-address" | "get-ln-addr" | "ln-addr" => Ok(Commands::GetLightningAddress),
"register-lightning-address" | "register-ln-addr" => {
if parts.len() < 2 {
return Err(anyhow::anyhow!("Usage: register-lightning-address <name>"));
}
let name = parts[1].to_string();
Ok(Commands::RegisterLightningAddress { name })
},
"clear" | "cls" => Ok(Commands::Clear),
"exit" | "quit" | "q" => Ok(Commands::Exit),
"help" => {
print_help();
Err(anyhow::anyhow!(""))
},
_ => Err(anyhow::anyhow!("Unknown command: {}", parts[0])),
}
}
async fn execute_command(command: Commands, state: &mut WalletState) -> Result<()> {
match command {
Commands::Balance => {
let wallet = state.wallet();
println!("{} Fetching balance...", "💰".bright_yellow());
match wallet.get_balance().await {
Ok(balance) => {
println!("Trusted balance: {} sats", balance.trusted.sats_rounding_up());
println!("LN balance: {} sats", balance.lightning.sats_rounding_up());
println!(
"Pending balance: {} sats",
balance.pending_balance.sats_rounding_up()
);
},
Err(e) => {
return Err(anyhow::anyhow!("Failed to get balance: {:?}", e));
},
}
},
Commands::Send { destination, amount } => {
let wallet = state.wallet();
println!("{} Sending payment...", "📤".bright_yellow());
println!("Destination: {}", destination.bright_cyan());
if let Some(amount) = amount {
println!("Amount: {} sats", amount.to_string().bright_green().bold());
}
let amount = amount
.map(|a| Amount::from_sats(a).map_err(|_| anyhow::anyhow!("Invalid amount")))
.transpose()?;
match wallet.parse_payment_instructions(&destination).await {
Ok(instructions) => match PaymentInfo::build(instructions, amount) {
Ok(payment_info) => match wallet.pay(&payment_info).await {
Ok(_) => {
println!("{} Payment initiated successfully!", "✅".bright_green());
},
Err(e) => {
return Err(anyhow::anyhow!("Failed to send payment: {e:?}"));
},
},
Err(_) => {
return Err(anyhow::anyhow!(
"Payment amount doesn't match instruction requirements"
));
},
},
Err(e) => {
return Err(anyhow::anyhow!("Failed to parse payment instructions: {e:?}"));
},
}
},
Commands::Receive { amount } => {
let wallet = state.wallet();
println!("{} Generating payment request...", "📥".bright_yellow());
let amount = amount
.map(Amount::from_sats)
.transpose()
.map_err(|_| anyhow::anyhow!("Invalid amount"))?;
match wallet.get_single_use_receive_uri(amount).await {
Ok(uri) => {
match amount {
Some(amt) => {
println!("Invoice for {} sats:", amt.sats_rounding_up());
},
None => {
println!("Invoice for any amount:");
},
}
println!("{}", uri.invoice.to_string().bright_cyan());
if let Some(ref address) = uri.address {
println!("On-chain address: {}", address.to_string().bright_cyan());
}
println!("Full URI: {}", uri.to_string().bright_cyan());
},
Err(e) => {
return Err(anyhow::anyhow!("Failed to generate receive URI: {:?}", e));
},
}
},
Commands::Status => {
let wallet = state.wallet();
println!("{} Wallet Status", "📊".bright_blue().bold());
println!("Node ID: {}", wallet.node_id().to_string().bright_cyan());
println!(
"LSP Connected: {}",
if wallet.is_connected_to_lsp() { "Yes".bright_green() } else { "No".bright_red() }
);
println!(
"Rebalance Enabled: {}",
if wallet.get_rebalance_enabled().await {
"Yes".bright_green()
} else {
"No".bright_red()
}
);
let tunables = wallet.get_tunables();
println!("Tunables:");
println!(
" Trusted balance limit: {} sats",
tunables.trusted_balance_limit.sats_rounding_up()
);
println!(" Rebalance minimum: {} sats", tunables.rebalance_min.sats_rounding_up());
println!(
" On-chain receive threshold: {} sats",
tunables.onchain_receive_threshold.sats_rounding_up()
);
},
Commands::Transactions => {
let wallet = state.wallet();
println!("{} Fetching transactions...", "📋".bright_yellow());
match wallet.list_transactions().await {
Ok(transactions) => {
if transactions.is_empty() {
println!("No transactions found.");
} else {
println!("Found {} transactions:", transactions.len());
for (i, tx) in transactions.iter().enumerate() {
let status_icon = match tx.status {
orange_sdk::TxStatus::Pending => "⏳",
orange_sdk::TxStatus::Completed => "✅",
orange_sdk::TxStatus::Failed => "❌",
};
let direction_icon = if tx.outbound { "📤" } else { "📥" };
println!(
"{} {} {} {} {} sats",
i + 1,
status_icon,
direction_icon,
format!("{:?}", tx.payment_type).bright_cyan(),
tx.amount
.map(|a| a.sats_rounding_up().to_string())
.unwrap_or_else(|| "?".to_string())
.bright_green()
);
}
}
},
Err(e) => {
return Err(anyhow::anyhow!("Failed to list transactions: {:?}", e));
},
}
},
Commands::Channels => {
let wallet = state.wallet();
let channels = wallet.channels();
if channels.is_empty() {
println!("{} No channels found.", "🔒".bright_yellow());
} else {
println!("{} Found {} channels:", "🔒".bright_green(), channels.len());
for channel in channels {
let status = if channel.is_usable {
"Usable".bright_green()
} else if channel.is_channel_ready {
"Ready".bright_yellow()
} else {
"Inactive".bright_red()
};
println!(
"{} Channel TXO: {}, Status: {}, Inbound Capacity: {} sats, Outbound Capacity: {} sats",
"🔗".bright_cyan(),
channel
.funding_txo
.map(|t| t.to_string())
.unwrap_or("Unknown".to_string())
.bright_cyan(),
status.bright_yellow(),
(channel.inbound_capacity_msat / 1_000).to_string().bright_green(),
(channel.outbound_capacity_msat / 1_000).to_string().bright_green(),
);
}
}
},
Commands::EstimateFee { destination, amount } => {
let wallet = state.wallet();
println!("{} Estimating fee...", "🧮".bright_yellow());
println!("Destination: {}", destination.bright_cyan());
if let Some(amount) = amount {
println!("Amount: {} sats", amount.to_string().bright_green().bold());
}
let _amount = amount
.map(|a| Amount::from_sats(a).map_err(|_| anyhow::anyhow!("Invalid amount")))
.transpose()?;
match wallet.parse_payment_instructions(&destination).await {
Ok(instructions) => {
let estimated_fee = wallet.estimate_fee(&instructions).await;
println!(
"{} Estimated fee: {} sats",
"✅".bright_green(),
estimated_fee.sats_rounding_up()
);
},
Err(e) => {
return Err(anyhow::anyhow!("Failed to parse payment instructions: {e:?}"));
},
}
},
Commands::GetLightningAddress => {
let wallet = state.wallet();
println!("{} Fetching lightning address...", "⚡".bright_yellow());
match wallet.get_lightning_address().await {
Ok(Some(address)) => {
println!(
"{} Lightning address: {}",
"⚡".bright_green(),
address.bright_cyan()
);
},
Ok(None) => {
println!("{} No lightning address registered yet.", "⚡".bright_yellow());
println!(
"{} Use 'register-lightning-address <name>' to register one",
"Hint:".bright_yellow().bold()
);
},
Err(e) => {
return Err(anyhow::anyhow!("Failed to get lightning address: {:?}", e));
},
}
},
Commands::RegisterLightningAddress { name } => {
let wallet = state.wallet();
println!(
"{} Registering lightning address: {}...",
"⚡".bright_yellow(),
name.bright_cyan()
);
match wallet.register_lightning_address(name.clone()).await {
Ok(()) => {
println!("{} Lightning address registered successfully!", "✅".bright_green());
// Fetch and display the full address
match wallet.get_lightning_address().await {
Ok(Some(address)) => {
println!(
"{} Your lightning address: {}",
"⚡".bright_green(),
address.bright_cyan()
);
},
_ => {},
}
},
Err(e) => {
return Err(anyhow::anyhow!("Failed to register lightning address: {:?}", e));
},
}
},
Commands::Clear => {
print!("\x1B[2J\x1B[1;1H");
std::io::stdout().flush().unwrap();
},
Commands::Exit => {
println!("{} Stopping wallet...", "⏹️".bright_yellow());
state.wallet().stop().await;
println!("{} Goodbye!", "👋".bright_green());
std::process::exit(0);
},
}
Ok(())
}
fn print_help() {
println!("{}", "Available Commands:".bright_blue().bold());
println!();
println!(" {}", "balance".bright_green().bold());
println!(" Show wallet balance (auto-initializes wallet if needed)");
println!();
println!(" {} <destination> <amount>", "send".bright_green().bold());
println!(" Send a payment to an address or invoice");
println!();
println!(" {} [amount]", "receive".bright_green().bold());
println!(" Generate a payment request (invoice)");
println!();
println!(" {}", "status".bright_green().bold());
println!(" Show wallet status information");
println!();
println!(" {}", "transactions".bright_green().bold());
println!(" List recent transactions");
println!();
println!(" {}", "channels".bright_green().bold());
println!(" List channels");
println!();
println!(" {} <destination> [amount]", "estimate-fee".bright_green().bold());
println!(" Estimate the fee for a payment");
println!();
println!(" {}", "get-lightning-address".bright_green().bold());
println!(" Get the current lightning address");
println!();
println!(" {} <name>", "register-lightning-address".bright_green().bold());
println!(" Register a lightning address");
println!();
println!(" {}", "clear".bright_green().bold());
println!(" Clear the terminal screen");
println!();
println!(" {}", "exit".bright_green().bold());
println!(" Exit the application");
println!();
println!("{}", "Examples:".bright_blue().bold());
println!(" balance");
println!(" send lnbc1... 10000");
println!(" receive 25000");
println!(" estimate-fee lnbc1... 10000");
println!(" events");
println!();
println!("{}", "Note:".bright_yellow().bold());
println!(" The wallet will be auto-initialized on first use with:");
println!(" - Network: {NETWORK}");
println!(" - Storage: ./wallet_data/{NETWORK}");
println!(" - Generated seed phrase (displayed on init)");
}
fn generate_or_load_seed(storage_path: &str) -> Result<Seed> {
let seed_file_path = format!("{}/seed.txt", storage_path);
// Try to load existing seed
if let Ok(seed_content) = fs::read_to_string(&seed_file_path) {
let mnemonic_str = seed_content.trim();
match Mnemonic::from_str(mnemonic_str) {
Ok(mnemonic) => {
println!("{} Loaded existing seed from {}", "🔑".bright_green(), seed_file_path);
return Ok(Seed::Mnemonic { mnemonic, passphrase: None });
},
Err(e) => {
println!(
"{} Warning: Failed to parse existing seed file: {}",
"⚠️".bright_yellow(),
e
);
println!("{} Generating new seed...", "🔄".bright_yellow());
},
}
}
// Generate new seed
let mut entropy = [0u8; 16]; // 128 bits for 12-word mnemonic
rand::thread_rng().fill_bytes(&mut entropy);
let mnemonic = Mnemonic::from_entropy(&entropy)?;
println!("{} Generated new seed: {}", "🔑".bright_yellow(), mnemonic);
// Create storage directory if it doesn't exist
fs::create_dir_all(storage_path)
.with_context(|| format!("Failed to create storage directory: {}", storage_path))?;
// Save seed to file
fs::write(&seed_file_path, mnemonic.to_string())
.with_context(|| format!("Failed to write seed to file: {}", seed_file_path))?;
println!("{seed_file_path} Seed saved to {}", "💾".bright_green());
println!(
"{} Keep this seed phrase safe - it's needed to recover your wallet!",
"⚠️".bright_red().bold()
);
Ok(Seed::Mnemonic { mnemonic, passphrase: None })
}