-pub(crate) fn poll_for_user_input(peer_manager: Arc<PeerManager>, channel_manager:
- Arc<ChannelManager>, router: Arc<NetGraphMsgHandler<Arc<dyn
- chain::Access>, Arc<FilesystemLogger>>>, payment_storage:
- PaymentInfoStorage, node_privkey: SecretKey, event_notifier:
- mpsc::Sender<()>, ldk_data_dir: String, logger: Arc<FilesystemLogger>,
- runtime_handle: Handle) {
- println!("LDK startup successful. To view available commands: \"help\".\nLDK logs are available in the `logs` folder of <your-supplied-ldk-data-dir-path>/.ldk/logs");
- let stdin = io::stdin();
- print!("> "); io::stdout().flush().unwrap(); // Without flushing, the `>` doesn't print
- for line in stdin.lock().lines() {
- let _ = event_notifier.try_send(());
- let line = line.unwrap();
- let mut words = line.split_whitespace();
- if let Some(word) = words.next() {
- match word {
- "help" => help(),
- "openchannel" => {
- let peer_pubkey_and_ip_addr = words.next();
- let channel_value_sat = words.next();
- if peer_pubkey_and_ip_addr.is_none() || channel_value_sat.is_none() {
- println!("ERROR: openchannel takes 2 arguments: `openchannel pubkey@host:port channel_amt_satoshis`");
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
- let peer_pubkey_and_ip_addr = peer_pubkey_and_ip_addr.unwrap();
- let (pubkey, peer_addr) = match parse_peer_info(peer_pubkey_and_ip_addr.to_string()) {
- Ok(info) => info,
- Err(e) => {
- println!("{:?}", e.into_inner().unwrap());
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
- };
-
- let chan_amt_sat: Result<u64, _> = channel_value_sat.unwrap().parse();
- if chan_amt_sat.is_err() {
- println!("ERROR: channel amount must be a number");
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
-
- if connect_peer_if_necessary(pubkey, peer_addr, peer_manager.clone(),
- event_notifier.clone(),
- runtime_handle.clone()).is_err() {
- print!("> "); io::stdout().flush().unwrap(); continue;
- };
-
- if open_channel(pubkey, chan_amt_sat.unwrap(), channel_manager.clone()).is_ok() {
- let peer_data_path = format!("{}/channel_peer_data", ldk_data_dir.clone());
- let _ = disk::persist_channel_peer(Path::new(&peer_data_path), peer_pubkey_and_ip_addr);
- }
- },
- "sendpayment" => {
- let invoice_str = words.next();
- if invoice_str.is_none() {
- println!("ERROR: sendpayment requires an invoice: `sendpayment <invoice>`");
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
-
- let invoice_res = lightning_invoice::Invoice::from_str(invoice_str.unwrap());
- if invoice_res.is_err() {
- println!("ERROR: invalid invoice: {:?}", invoice_res.unwrap_err());
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
- let invoice = invoice_res.unwrap();
-
- let amt_pico_btc = invoice.amount_pico_btc();
- if amt_pico_btc.is_none () {
- println!("ERROR: invalid invoice: must contain amount to pay");
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
- let amt_msat = amt_pico_btc.unwrap() / 10;
-
- let payee_pubkey = invoice.recover_payee_pub_key();
- let final_cltv = *invoice.min_final_cltv_expiry().unwrap_or(&9) as u32;
-
- let mut payment_hash = PaymentHash([0; 32]);
- payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
-
-
- let payment_secret = match invoice.payment_secret() {
- Some(secret) => {
- let mut payment_secret = PaymentSecret([0; 32]);
- payment_secret.0.copy_from_slice(&secret.0);
- Some(payment_secret)
- },
- None => None
- };
-
- let mut invoice_features = InvoiceFeatures::empty();
- for field in &invoice.into_signed_raw().raw_invoice().data.tagged_fields {
- match field {
- lightning_invoice::RawTaggedField::UnknownSemantics(vec) => {
- if vec[0] == bech32::u5::try_from_u8(5).unwrap() {
- if vec.len() >= 6 && vec[5].to_u8() & 0b10000 != 0 {
- invoice_features.set_supports_var_onion_optin();
- }
- if vec.len() >= 6 && vec[5].to_u8() & 0b01000 != 0 {
- invoice_features.set_requires_var_onion_optin();
- }
- if vec.len() >= 4 && vec[3].to_u8() & 0b00001 != 0 {
- invoice_features.set_supports_payment_secret();
- }
- if vec.len() >= 5 && vec[4].to_u8() & 0b10000 != 0 {
- invoice_features.set_requires_payment_secret();
- }
- if vec.len() >= 4 && vec[3].to_u8() & 0b00100 != 0 {
- invoice_features.set_supports_basic_mpp();
- }
- if vec.len() >= 4 && vec[3].to_u8() & 0b00010 != 0 {
- invoice_features.set_requires_basic_mpp();
- }
- }
- },
- _ => {}
- }
- }
- let invoice_features_opt = match invoice_features == InvoiceFeatures::empty() {
- true => None,
- false => Some(invoice_features)
- };
- send_payment(payee_pubkey, amt_msat, final_cltv, payment_hash,
- payment_secret, invoice_features_opt, router.clone(),
- channel_manager.clone(), payment_storage.clone(),
- logger.clone());
- },
- "getinvoice" => {
- let amt_str = words.next();
- if amt_str.is_none() {
- println!("ERROR: getinvoice requires an amount in satoshis");
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
-
- let amt_sat: Result<u64, _> = amt_str.unwrap().parse();
- if amt_sat.is_err() {
- println!("ERROR: getinvoice provided payment amount was not a number");
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
- get_invoice(amt_sat.unwrap(), payment_storage.clone(), node_privkey.clone(),
- channel_manager.clone(), router.clone());
- },
- "connectpeer" => {
- let peer_pubkey_and_ip_addr = words.next();
- if peer_pubkey_and_ip_addr.is_none() {
- println!("ERROR: connectpeer requires peer connection info: `connectpeer pubkey@host:port`");
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
- let (pubkey, peer_addr) = match parse_peer_info(peer_pubkey_and_ip_addr.unwrap().to_string()) {
- Ok(info) => info,
- Err(e) => {
- println!("{:?}", e.into_inner().unwrap());
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
- };
- if connect_peer_if_necessary(pubkey, peer_addr, peer_manager.clone(),
- event_notifier.clone(), runtime_handle.clone()).is_ok() {
- println!("SUCCESS: connected to peer {}", pubkey);
- }
-
- },
- "listchannels" => list_channels(channel_manager.clone()),
- "listpayments" => list_payments(payment_storage.clone()),
- "closechannel" => {
- let channel_id_str = words.next();
- if channel_id_str.is_none() {
- println!("ERROR: closechannel requires a channel ID: `closechannel <channel_id>`");
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
- let channel_id_vec = hex_utils::to_vec(channel_id_str.unwrap());
- if channel_id_vec.is_none() {
- println!("ERROR: couldn't parse channel_id as hex");
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
- let mut channel_id = [0; 32];
- channel_id.copy_from_slice(&channel_id_vec.unwrap());
- close_channel(channel_id, channel_manager.clone());
- },
- "forceclosechannel" => {
- let channel_id_str = words.next();
- if channel_id_str.is_none() {
- println!("ERROR: forceclosechannel requires a channel ID: `forceclosechannel <channel_id>`");
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
- let channel_id_vec = hex_utils::to_vec(channel_id_str.unwrap());
- if channel_id_vec.is_none() {
- println!("ERROR: couldn't parse channel_id as hex");
- print!("> "); io::stdout().flush().unwrap(); continue;
- }
- let mut channel_id = [0; 32];
- channel_id.copy_from_slice(&channel_id_vec.unwrap());
- force_close_channel(channel_id, channel_manager.clone());
- }
- _ => println!("Unknown command. See `\"help\" for available commands.")
- }
- }
- print!("> "); io::stdout().flush().unwrap();
-
- }
+pub(crate) fn poll_for_user_input(
+ peer_manager: Arc<PeerManager>, channel_manager: Arc<ChannelManager>,
+ router: Arc<NetGraphMsgHandler<Arc<dyn chain::Access>, Arc<FilesystemLogger>>>,
+ payment_storage: PaymentInfoStorage, node_privkey: SecretKey, event_notifier: mpsc::Sender<()>,
+ ldk_data_dir: String, logger: Arc<FilesystemLogger>, runtime_handle: Handle, network: Network,
+) {
+ println!("LDK startup successful. To view available commands: \"help\".\nLDK logs are available at <your-supplied-ldk-data-dir-path>/.ldk/logs");
+ let stdin = io::stdin();
+ print!("> ");
+ io::stdout().flush().unwrap(); // Without flushing, the `>` doesn't print
+ for line in stdin.lock().lines() {
+ let _ = event_notifier.try_send(());
+ let line = line.unwrap();
+ let mut words = line.split_whitespace();
+ if let Some(word) = words.next() {
+ match word {
+ "help" => help(),
+ "openchannel" => {
+ let peer_pubkey_and_ip_addr = words.next();
+ let channel_value_sat = words.next();
+ if peer_pubkey_and_ip_addr.is_none() || channel_value_sat.is_none() {
+ println!("ERROR: openchannel has 2 required arguments: `openchannel pubkey@host:port channel_amt_satoshis` [--public]");
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+ let peer_pubkey_and_ip_addr = peer_pubkey_and_ip_addr.unwrap();
+ let (pubkey, peer_addr) =
+ match parse_peer_info(peer_pubkey_and_ip_addr.to_string()) {
+ Ok(info) => info,
+ Err(e) => {
+ println!("{:?}", e.into_inner().unwrap());
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+ };
+
+ let chan_amt_sat: Result<u64, _> = channel_value_sat.unwrap().parse();
+ if chan_amt_sat.is_err() {
+ println!("ERROR: channel amount must be a number");
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+
+ if connect_peer_if_necessary(
+ pubkey,
+ peer_addr,
+ peer_manager.clone(),
+ event_notifier.clone(),
+ runtime_handle.clone(),
+ )
+ .is_err()
+ {
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ };
+
+ // let private_channel = match words.next().as_ref().map(String::as_str) {
+ let announce_channel = match words.next() {
+ Some("--public") | Some("--public=true") | Some("--public true") => true,
+ Some("--public=false") | Some("--public false") => false,
+ Some(_) => {
+ println!("ERROR: invalid `--public` command format");
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+ None => false,
+ };
+
+ if open_channel(
+ pubkey,
+ chan_amt_sat.unwrap(),
+ announce_channel,
+ channel_manager.clone(),
+ )
+ .is_ok()
+ {
+ let peer_data_path = format!("{}/channel_peer_data", ldk_data_dir.clone());
+ let _ = disk::persist_channel_peer(
+ Path::new(&peer_data_path),
+ peer_pubkey_and_ip_addr,
+ );
+ }
+ }
+ "sendpayment" => {
+ let invoice_str = words.next();
+ if invoice_str.is_none() {
+ println!("ERROR: sendpayment requires an invoice: `sendpayment <invoice>`");
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+
+ let invoice_res = lightning_invoice::Invoice::from_str(invoice_str.unwrap());
+ if invoice_res.is_err() {
+ println!("ERROR: invalid invoice: {:?}", invoice_res.unwrap_err());
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+ let invoice = invoice_res.unwrap();
+
+ let amt_pico_btc = invoice.amount_pico_btc();
+ if amt_pico_btc.is_none() {
+ println!("ERROR: invalid invoice: must contain amount to pay");
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+ let amt_msat = amt_pico_btc.unwrap() / 10;
+
+ let payee_pubkey = invoice.recover_payee_pub_key();
+ let final_cltv = *invoice.min_final_cltv_expiry().unwrap_or(&9) as u32;
+
+ let mut payment_hash = PaymentHash([0; 32]);
+ payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
+
+ let payment_secret = match invoice.payment_secret() {
+ Some(secret) => {
+ let mut payment_secret = PaymentSecret([0; 32]);
+ payment_secret.0.copy_from_slice(&secret.0);
+ Some(payment_secret)
+ }
+ None => None,
+ };
+
+ // rust-lightning-invoice doesn't currently support features, so we parse features
+ // manually from the invoice.
+ let mut invoice_features = InvoiceFeatures::empty();
+ for field in &invoice.into_signed_raw().raw_invoice().data.tagged_fields {
+ match field {
+ lightning_invoice::RawTaggedField::UnknownSemantics(vec) => {
+ if vec[0] == bech32::u5::try_from_u8(5).unwrap() {
+ if vec.len() >= 6 && vec[5].to_u8() & 0b10000 != 0 {
+ invoice_features =
+ invoice_features.set_variable_length_onion_optional();
+ }
+ if vec.len() >= 6 && vec[5].to_u8() & 0b01000 != 0 {
+ invoice_features =
+ invoice_features.set_variable_length_onion_required();
+ }
+ if vec.len() >= 4 && vec[3].to_u8() & 0b00001 != 0 {
+ invoice_features =
+ invoice_features.set_payment_secret_optional();
+ }
+ if vec.len() >= 5 && vec[4].to_u8() & 0b10000 != 0 {
+ invoice_features =
+ invoice_features.set_payment_secret_required();
+ }
+ if vec.len() >= 4 && vec[3].to_u8() & 0b00100 != 0 {
+ invoice_features =
+ invoice_features.set_basic_mpp_optional();
+ }
+ if vec.len() >= 4 && vec[3].to_u8() & 0b00010 != 0 {
+ invoice_features =
+ invoice_features.set_basic_mpp_required();
+ }
+ }
+ }
+ _ => {}
+ }
+ }
+ let invoice_features_opt = match invoice_features == InvoiceFeatures::empty() {
+ true => None,
+ false => Some(invoice_features),
+ };
+ send_payment(
+ payee_pubkey,
+ amt_msat,
+ final_cltv,
+ payment_hash,
+ payment_secret,
+ invoice_features_opt,
+ router.clone(),
+ channel_manager.clone(),
+ payment_storage.clone(),
+ logger.clone(),
+ );
+ }
+ "getinvoice" => {
+ let amt_str = words.next();
+ if amt_str.is_none() {
+ println!("ERROR: getinvoice requires an amount in satoshis");
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+
+ let amt_sat: Result<u64, _> = amt_str.unwrap().parse();
+ if amt_sat.is_err() {
+ println!("ERROR: getinvoice provided payment amount was not a number");
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+ get_invoice(
+ amt_sat.unwrap(),
+ payment_storage.clone(),
+ node_privkey.clone(),
+ channel_manager.clone(),
+ network,
+ );
+ }
+ "connectpeer" => {
+ let peer_pubkey_and_ip_addr = words.next();
+ if peer_pubkey_and_ip_addr.is_none() {
+ println!("ERROR: connectpeer requires peer connection info: `connectpeer pubkey@host:port`");
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+ let (pubkey, peer_addr) =
+ match parse_peer_info(peer_pubkey_and_ip_addr.unwrap().to_string()) {
+ Ok(info) => info,
+ Err(e) => {
+ println!("{:?}", e.into_inner().unwrap());
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+ };
+ if connect_peer_if_necessary(
+ pubkey,
+ peer_addr,
+ peer_manager.clone(),
+ event_notifier.clone(),
+ runtime_handle.clone(),
+ )
+ .is_ok()
+ {
+ println!("SUCCESS: connected to peer {}", pubkey);
+ }
+ }
+ "listchannels" => list_channels(channel_manager.clone()),
+ "listpayments" => list_payments(payment_storage.clone()),
+ "closechannel" => {
+ let channel_id_str = words.next();
+ if channel_id_str.is_none() {
+ println!("ERROR: closechannel requires a channel ID: `closechannel <channel_id>`");
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+ let channel_id_vec = hex_utils::to_vec(channel_id_str.unwrap());
+ if channel_id_vec.is_none() {
+ println!("ERROR: couldn't parse channel_id as hex");
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+ let mut channel_id = [0; 32];
+ channel_id.copy_from_slice(&channel_id_vec.unwrap());
+ close_channel(channel_id, channel_manager.clone());
+ }
+ "forceclosechannel" => {
+ let channel_id_str = words.next();
+ if channel_id_str.is_none() {
+ println!("ERROR: forceclosechannel requires a channel ID: `forceclosechannel <channel_id>`");
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+ let channel_id_vec = hex_utils::to_vec(channel_id_str.unwrap());
+ if channel_id_vec.is_none() {
+ println!("ERROR: couldn't parse channel_id as hex");
+ print!("> ");
+ io::stdout().flush().unwrap();
+ continue;
+ }
+ let mut channel_id = [0; 32];
+ channel_id.copy_from_slice(&channel_id_vec.unwrap());
+ force_close_channel(channel_id, channel_manager.clone());
+ }
+ _ => println!("Unknown command. See `\"help\" for available commands."),
+ }
+ }
+ print!("> ");
+ io::stdout().flush().unwrap();
+ }