Merge pull request #55 from TheBlueMatt/2022-03-node-alias
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Tue, 29 Mar 2022 23:13:30 +0000 (23:13 +0000)
committerGitHub <noreply@github.com>
Tue, 29 Mar 2022 23:13:30 +0000 (23:13 +0000)
1  2 
src/cli.rs

diff --combined src/cli.rs
index 3d28fd113f2c5eb9c432b20dcd1b5f88598c24ad,d6ffa5d96f2edf818bd7fcfee07bc9aa42413c8a..681714f17936093dc218c4411df1bce0b27b5b97
@@@ -11,6 -11,7 +11,7 @@@ use bitcoin::secp256k1::key::PublicKey
  use lightning::chain::keysinterface::{KeysInterface, KeysManager, Recipient};
  use lightning::ln::msgs::NetAddress;
  use lightning::ln::{PaymentHash, PaymentPreimage};
+ use lightning::routing::network_graph::{NetworkGraph, NodeId};
  use lightning::util::config::{ChannelConfig, ChannelHandshakeLimits, UserConfig};
  use lightning::util::events::EventHandler;
  use lightning_invoice::payment::PaymentError;
@@@ -141,8 -142,8 +142,8 @@@ pub(crate) fn parse_startup_args() -> R
  pub(crate) async fn poll_for_user_input<E: EventHandler>(
        invoice_payer: Arc<InvoicePayer<E>>, peer_manager: Arc<PeerManager>,
        channel_manager: Arc<ChannelManager>, keys_manager: Arc<KeysManager>,
-       inbound_payments: PaymentInfoStorage, outbound_payments: PaymentInfoStorage,
-       ldk_data_dir: String, network: Network,
+       network_graph: Arc<NetworkGraph>, inbound_payments: PaymentInfoStorage,
+       outbound_payments: PaymentInfoStorage, ldk_data_dir: String, network: Network,
  ) {
        println!("LDK startup successful. To view available commands: \"help\".");
        println!("LDK logs are available at <your-supplied-ldk-data-dir-path>/.ldk/logs");
                                                println!("SUCCESS: connected to peer {}", pubkey);
                                        }
                                }
-                               "listchannels" => list_channels(&channel_manager),
+                               "listchannels" => list_channels(&channel_manager, &network_graph),
                                "listpayments" => {
                                        list_payments(inbound_payments.clone(), outbound_payments.clone())
                                }
                                                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");
 +                                      if channel_id_vec.is_none() || channel_id_vec.as_ref().unwrap().len() != 32 {
 +                                              println!("ERROR: couldn't parse channel_id");
                                                continue;
                                        }
                                        let mut channel_id = [0; 32];
                                                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");
 +                                      if channel_id_vec.is_none() || channel_id_vec.as_ref().unwrap().len() != 32 {
 +                                              println!("ERROR: couldn't parse channel_id");
                                                continue;
                                        }
                                        let mut channel_id = [0; 32];
@@@ -399,7 -400,20 +400,20 @@@ fn list_peers(peer_manager: Arc<PeerMan
        println!("\t}},");
  }
  
- fn list_channels(channel_manager: &Arc<ChannelManager>) {
+ /// Takes some untrusted bytes and returns a sanitized string that is safe to print
+ fn sanitize_string(bytes: &[u8]) -> String {
+       let mut ret = String::with_capacity(bytes.len());
+       // We should really support some sane subset of UTF-8 here, but limiting to printable ASCII
+       // instead makes this trivial.
+       for b in bytes {
+               if *b >= 0x20 && *b <= 0x7e {
+                       ret.push(*b as char);
+               }
+       }
+       ret
+ }
+ fn list_channels(channel_manager: &Arc<ChannelManager>, network_graph: &Arc<NetworkGraph>) {
        print!("[");
        for chan_info in channel_manager.list_channels() {
                println!("");
                if let Some(funding_txo) = chan_info.funding_txo {
                        println!("\t\tfunding_txid: {},", funding_txo.txid);
                }
                println!(
                        "\t\tpeer_pubkey: {},",
                        hex_utils::hex_str(&chan_info.counterparty.node_id.serialize())
                );
+               if let Some(node_info) = network_graph
+                       .read_only()
+                       .nodes()
+                       .get(&NodeId::from_pubkey(&chan_info.counterparty.node_id))
+               {
+                       if let Some(announcement) = &node_info.announcement_info {
+                               println!("\t\tpeer_alias: {}", sanitize_string(&announcement.alias));
+                       }
+               }
                if let Some(id) = chan_info.short_channel_id {
                        println!("\t\tshort_channel_id: {},", id);
                }