X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-sample;a=blobdiff_plain;f=src%2Fcli.rs;h=dc8bb407942e289ffaec47eb37b5922bfb2dd1f2;hp=8033cdada51642a48e64b5eee947da7d956c18fc;hb=0cbff7f763b02f0b2b535a723300165834cbfc56;hpb=432106edae85e3e5edc43e2a7c9053ac2d1e5532 diff --git a/src/cli.rs b/src/cli.rs index 8033cda..dc8bb40 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -4,25 +4,21 @@ use crate::{ ChannelManager, FilesystemLogger, HTLCStatus, MillisatAmount, PaymentInfo, PaymentInfoStorage, PeerManager, }; -use bitcoin::hashes::sha256::Hash as Sha256Hash; -use bitcoin::hashes::Hash; use bitcoin::network::constants::Network; -use bitcoin::secp256k1::key::{PublicKey, SecretKey}; -use bitcoin::secp256k1::Secp256k1; +use bitcoin::secp256k1::key::PublicKey; use lightning::chain; -use lightning::ln::channelmanager::{PaymentHash, PaymentPreimage, PaymentSecret}; +use lightning::chain::keysinterface::KeysManager; use lightning::ln::features::InvoiceFeatures; -use lightning::routing::network_graph::{NetGraphMsgHandler, RoutingFees}; +use lightning::ln::{PaymentHash, PaymentSecret}; +use lightning::routing::network_graph::NetGraphMsgHandler; use lightning::routing::router; -use lightning::routing::router::RouteHint; +use lightning::routing::router::RouteHintHop; use lightning::util::config::UserConfig; -use lightning_invoice::{Currency, Invoice, InvoiceBuilder, Route, RouteHop}; -use rand; -use rand::Rng; +use lightning_invoice::{utils, Currency, Invoice}; use std::env; use std::io; use std::io::{BufRead, Write}; -use std::net::{SocketAddr, TcpStream}; +use std::net::{SocketAddr, ToSocketAddrs}; use std::ops::Deref; use std::path::Path; use std::str::FromStr; @@ -41,7 +37,7 @@ pub(crate) struct LdkUserInfo { } pub(crate) fn parse_startup_args() -> Result { - if env::args().len() < 4 { + if env::args().len() < 3 { println!("ldk-tutorial-node requires 3 arguments: `cargo run :@: ldk_storage_directory_path [] [bitcoin-network]`"); return Err(()); } @@ -71,7 +67,7 @@ pub(crate) fn parse_startup_args() -> Result { let mut ldk_peer_port_set = true; let ldk_peer_listening_port: u16 = match env::args().skip(3).next().map(|p| p.parse()) { Some(Ok(p)) => p, - Some(Err(e)) => panic!(e), + Some(Err(e)) => panic!("{}", e), None => { ldk_peer_port_set = false; 9735 @@ -101,12 +97,15 @@ pub(crate) fn parse_startup_args() -> Result { pub(crate) async fn poll_for_user_input( peer_manager: Arc, channel_manager: Arc, - router: Arc, Arc>>, + keys_manager: Arc, + router: Arc, Arc>>, inbound_payments: PaymentInfoStorage, outbound_payments: PaymentInfoStorage, - node_privkey: SecretKey, event_notifier: mpsc::Sender<()>, ldk_data_dir: String, - logger: Arc, network: Network, + event_notifier: mpsc::Sender<()>, ldk_data_dir: String, logger: Arc, + network: Network, ) { - println!("LDK startup successful. To view available commands: \"help\".\nLDK logs are available at /.ldk/logs"); + println!("LDK startup successful. To view available commands: \"help\"."); + println!("LDK logs are available at /.ldk/logs"); + println!("Local Node ID is {}.", channel_manager.get_our_node_id()); let stdin = io::stdin(); print!("> "); io::stdout().flush().unwrap(); // Without flushing, the `>` doesn't print @@ -152,6 +151,7 @@ pub(crate) async fn poll_for_user_input( peer_manager.clone(), event_notifier.clone(), ) + .await .is_err() { print!("> "); @@ -195,16 +195,20 @@ pub(crate) async fn poll_for_user_input( continue; } - let invoice_res = 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 = match Invoice::from_str(invoice_str.unwrap()) { + Ok(inv) => inv, + Err(e) => { + println!("ERROR: invalid invoice: {:?}", e); + print!("> "); + io::stdout().flush().unwrap(); + continue; + } + }; + let mut route_hints = invoice.routes().clone(); + let mut last_hops = Vec::new(); + for hint in route_hints.drain(..) { + last_hops.push(hint[hint.len() - 1].clone()); } - let invoice = invoice_res.unwrap(); - let route_hints: Vec = - invoice.routes().iter().map(|&route| route.clone()).collect(); let amt_pico_btc = invoice.amount_pico_btc(); if amt_pico_btc.is_none() { @@ -216,7 +220,7 @@ pub(crate) async fn poll_for_user_input( 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 final_cltv = invoice.min_final_cltv_expiry() as u32; let mut payment_hash = PaymentHash([0; 32]); payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]); @@ -230,54 +234,19 @@ pub(crate) async fn poll_for_user_input( 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), + let invoice_features = match invoice.features() { + Some(feat) => Some(feat.clone()), + None => None, }; + send_payment( payee_pubkey, amt_msat, final_cltv, payment_hash, payment_secret, - invoice_features_opt, - route_hints, + invoice_features, + last_hops, router.clone(), channel_manager.clone(), outbound_payments.clone(), @@ -303,8 +272,8 @@ pub(crate) async fn poll_for_user_input( get_invoice( amt_msat.unwrap(), inbound_payments.clone(), - node_privkey.clone(), channel_manager.clone(), + keys_manager.clone(), network, ); } @@ -332,6 +301,7 @@ pub(crate) async fn poll_for_user_input( peer_manager.clone(), event_notifier.clone(), ) + .await .is_ok() { println!("SUCCESS: connected to peer {}", pubkey); @@ -379,6 +349,8 @@ pub(crate) async fn poll_for_user_input( channel_id.copy_from_slice(&channel_id_vec.unwrap()); force_close_channel(channel_id, channel_manager.clone()); } + "nodeinfo" => node_info(channel_manager.clone(), peer_manager.clone()), + "listpeers" => list_peers(peer_manager.clone()), _ => println!("Unknown command. See `\"help\" for available commands."), } } @@ -398,6 +370,23 @@ fn help() { println!("forceclosechannel "); } +fn node_info(channel_manager: Arc, peer_manager: Arc) { + println!("\t{{"); + println!("\t\t node_pubkey: {}", channel_manager.get_our_node_id()); + println!("\t\t num_channels: {}", channel_manager.list_channels().len()); + println!("\t\t num_usable_channels: {}", channel_manager.list_usable_channels().len()); + println!("\t\t num_peers: {}", peer_manager.get_peer_node_ids().len()); + println!("\t}},"); +} + +fn list_peers(peer_manager: Arc) { + println!("\t{{"); + for pubkey in peer_manager.get_peer_node_ids() { + println!("\t\t pubkey: {}", pubkey); + } + println!("\t}},"); +} + fn list_channels(channel_manager: Arc) { print!("["); for chan_info in channel_manager.list_channels() { @@ -417,7 +406,7 @@ fn list_channels(channel_manager: Arc) { } println!("\t\tpending_open: {},", pending_channel); println!("\t\tchannel_value_satoshis: {},", chan_info.channel_value_satoshis); - println!("\t\tchannel_can_send_payments: {},", chan_info.is_live); + println!("\t\tchannel_can_send_payments: {},", chan_info.is_usable); println!("\t}},"); } println!("]"); @@ -465,7 +454,7 @@ fn list_payments(inbound_payments: PaymentInfoStorage, outbound_payments: Paymen println!("]"); } -pub(crate) fn connect_peer_if_necessary( +pub(crate) async fn connect_peer_if_necessary( pubkey: PublicKey, peer_addr: SocketAddr, peer_manager: Arc, event_notifier: mpsc::Sender<()>, ) -> Result<(), ()> { @@ -474,24 +463,36 @@ pub(crate) fn connect_peer_if_necessary( return Ok(()); } } - match TcpStream::connect_timeout(&peer_addr, Duration::from_secs(10)) { - Ok(stream) => { - let peer_mgr = peer_manager.clone(); - let event_ntfns = event_notifier.clone(); - tokio::spawn(async move { - lightning_net_tokio::setup_outbound(peer_mgr, event_ntfns, pubkey, stream).await; - }); + match lightning_net_tokio::connect_outbound( + Arc::clone(&peer_manager), + event_notifier, + pubkey, + peer_addr, + ) + .await + { + Some(conn_closed_fut) => { + let mut closed_fut_box = Box::pin(conn_closed_fut); let mut peer_connected = false; while !peer_connected { + match futures::poll!(&mut closed_fut_box) { + std::task::Poll::Ready(_) => { + println!("ERROR: Peer disconnected before we finished the handshake"); + return Err(()); + } + std::task::Poll::Pending => {} + } for node_pubkey in peer_manager.get_peer_node_ids() { if node_pubkey == pubkey { peer_connected = true; } } + // Avoid blocking the tokio context by sleeping a bit + tokio::time::sleep(Duration::from_millis(10)).await; } } - Err(e) => { - println!("ERROR: failed to connect to peer: {:?}", e); + None => { + println!("ERROR: failed to connect to peer"); return Err(()); } } @@ -523,8 +524,8 @@ fn open_channel( fn send_payment( payee: PublicKey, amt_msat: u64, final_cltv: u32, payment_hash: PaymentHash, payment_secret: Option, payee_features: Option, - mut route_hints: Vec, - router: Arc, Arc>>, + route_hints: Vec, + router: Arc, Arc>>, channel_manager: Arc, payment_storage: PaymentInfoStorage, logger: Arc, ) { @@ -532,29 +533,13 @@ fn send_payment( let first_hops = channel_manager.list_usable_channels(); let payer_pubkey = channel_manager.get_our_node_id(); - let mut hints: Vec = Vec::new(); - for route in route_hints.drain(..) { - let route_hops = route.into_inner(); - let last_hop = &route_hops[route_hops.len() - 1]; - hints.push(RouteHint { - src_node_id: last_hop.pubkey, - short_channel_id: u64::from_be_bytes(last_hop.short_channel_id), - fees: RoutingFees { - base_msat: last_hop.fee_base_msat, - proportional_millionths: last_hop.fee_proportional_millionths, - }, - cltv_expiry_delta: last_hop.cltv_expiry_delta, - htlc_minimum_msat: None, - htlc_maximum_msat: None, - }) - } let route = router::get_route( &payer_pubkey, &network_graph, &payee, payee_features, Some(&first_hops.iter().collect::>()), - &hints.iter().collect::>(), + &route_hints.iter().collect::>(), amt_msat, final_cltv, logger, @@ -587,71 +572,43 @@ fn send_payment( } fn get_invoice( - amt_msat: u64, payment_storage: PaymentInfoStorage, our_node_privkey: SecretKey, - channel_manager: Arc, network: Network, + amt_msat: u64, payment_storage: PaymentInfoStorage, channel_manager: Arc, + keys_manager: Arc, network: Network, ) { let mut payments = payment_storage.lock().unwrap(); - let secp_ctx = Secp256k1::new(); - - let mut preimage = [0; 32]; - rand::thread_rng().fill_bytes(&mut preimage); - let payment_hash = Sha256Hash::hash(&preimage); - - let our_node_pubkey = channel_manager.get_our_node_id(); - let mut invoice = InvoiceBuilder::new(match network { + let currency = match network { Network::Bitcoin => Currency::Bitcoin, Network::Testnet => Currency::BitcoinTestnet, Network::Regtest => Currency::Regtest, - Network::Signet => panic!("Signet invoices not supported"), - }) - .payment_hash(payment_hash) - .description("rust-lightning-bitcoinrpc invoice".to_string()) - .amount_pico_btc(amt_msat * 10) - .current_timestamp() - .payee_pub_key(our_node_pubkey); - - // Add route hints to the invoice. - let our_channels = channel_manager.list_usable_channels(); - let mut min_final_cltv_expiry = 9; - for channel in our_channels { - let short_channel_id = match channel.short_channel_id { - Some(id) => id.to_be_bytes(), - None => continue, - }; - let forwarding_info = match channel.counterparty_forwarding_info { - Some(info) => info, - None => continue, - }; - if forwarding_info.cltv_expiry_delta > min_final_cltv_expiry { - min_final_cltv_expiry = forwarding_info.cltv_expiry_delta; + Network::Signet => panic!("Signet unsupported"), + }; + let invoice = match utils::create_invoice_from_channelmanager( + &channel_manager, + keys_manager, + currency, + Some(amt_msat), + "ldk-tutorial-node".to_string(), + ) { + Ok(inv) => { + println!("SUCCESS: generated invoice: {}", inv); + inv } - invoice = invoice.route(vec![RouteHop { - pubkey: channel.remote_network_id, - short_channel_id, - fee_base_msat: forwarding_info.fee_base_msat, - fee_proportional_millionths: forwarding_info.fee_proportional_millionths, - cltv_expiry_delta: forwarding_info.cltv_expiry_delta, - }]); - } - invoice = invoice.min_final_cltv_expiry(min_final_cltv_expiry.into()); - - // Sign the invoice. - let invoice = - invoice.build_signed(|msg_hash| secp_ctx.sign_recoverable(msg_hash, &our_node_privkey)); - - match invoice { - Ok(invoice) => println!("SUCCESS: generated invoice: {}", invoice), - Err(e) => println!("ERROR: failed to create invoice: {:?}", e), - } + Err(e) => { + println!("ERROR: failed to create invoice: {:?}", e); + return; + } + }; + let mut payment_hash = PaymentHash([0; 32]); + payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]); payments.insert( - PaymentHash(payment_hash.into_inner()), + payment_hash, PaymentInfo { - preimage: Some(PaymentPreimage(preimage)), + preimage: None, // We can't add payment secrets to invoices until we support features in invoices. // Otherwise lnd errors with "destination hop doesn't understand payment addresses" // (for context, lnd calls payment secrets "payment addresses"). - secret: None, + secret: Some(invoice.payment_secret().unwrap().clone()), status: HTLCStatus::Pending, amt_msat: MillisatAmount(Some(amt_msat)), }, @@ -681,13 +638,12 @@ pub(crate) fn parse_peer_info( if peer_addr_str.is_none() || peer_addr_str.is_none() { return Err(std::io::Error::new( std::io::ErrorKind::Other, - "ERROR: incorrectly formatted peer - info. Should be formatted as: `pubkey@host:port`", + "ERROR: incorrectly formatted peer info. Should be formatted as: `pubkey@host:port`", )); } - let peer_addr: Result = peer_addr_str.unwrap().parse(); - if peer_addr.is_err() { + let peer_addr = peer_addr_str.unwrap().to_socket_addrs().map(|mut r| r.next()); + if peer_addr.is_err() || peer_addr.as_ref().unwrap().is_none() { return Err(std::io::Error::new( std::io::ErrorKind::Other, "ERROR: couldn't parse pubkey@host:port into a socket address", @@ -702,5 +658,5 @@ pub(crate) fn parse_peer_info( )); } - Ok((pubkey.unwrap(), peer_addr.unwrap())) + Ok((pubkey.unwrap(), peer_addr.unwrap().unwrap())) }