]> git.bitcoin.ninja Git - ldk-sample/commitdiff
Print our peers' aliases in `listchannels` 2022-03-node-alias
authorMatt Corallo <git@bluematt.me>
Sun, 27 Mar 2022 18:25:55 +0000 (18:25 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 27 Mar 2022 20:29:55 +0000 (20:29 +0000)
src/cli.rs
src/main.rs

index 88f854265356274754a7ac7d396f1ee7ec4d7790..d6ffa5d96f2edf818bd7fcfee07bc9aa42413c8a 100644 (file)
@@ -11,6 +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 @@ pub(crate) fn parse_startup_args() -> Result<LdkUserInfo, ()> {
 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");
@@ -309,7 +310,7 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                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())
                                }
@@ -399,7 +400,20 @@ fn list_peers(peer_manager: Arc<PeerManager>) {
        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!("");
@@ -408,10 +422,21 @@ fn list_channels(channel_manager: &Arc<ChannelManager>) {
                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);
                }
index e962db83a2556c11f81a44f0f4fbc892660ea963..ad48efd4b1d9afcb45c3ad9d989411bc23871790 100644 (file)
@@ -699,10 +699,11 @@ async fn start_ldk() {
 
        // Start the CLI.
        cli::poll_for_user_input(
-               invoice_payer.clone(),
-               peer_manager.clone(),
-               channel_manager.clone(),
-               keys_manager.clone(),
+               Arc::clone(&invoice_payer),
+               Arc::clone(&peer_manager),
+               Arc::clone(&channel_manager),
+               Arc::clone(&keys_manager),
+               Arc::clone(&network_graph),
                inbound_payments,
                outbound_payments,
                ldk_data_dir.clone(),