From: Matt Corallo Date: Sun, 27 Mar 2022 18:25:55 +0000 (+0000) Subject: Print our peers' aliases in `listchannels` X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=refs%2Fheads%2F2022-03-node-alias;p=ldk-sample Print our peers' aliases in `listchannels` --- diff --git a/src/cli.rs b/src/cli.rs index 88f8542..d6ffa5d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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 { pub(crate) async fn poll_for_user_input( invoice_payer: Arc>, peer_manager: Arc, channel_manager: Arc, keys_manager: Arc, - inbound_payments: PaymentInfoStorage, outbound_payments: PaymentInfoStorage, - ldk_data_dir: String, network: Network, + network_graph: Arc, 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 /.ldk/logs"); @@ -309,7 +310,7 @@ pub(crate) async fn poll_for_user_input( 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) { println!("\t}},"); } -fn list_channels(channel_manager: &Arc) { +/// 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, network_graph: &Arc) { print!("["); for chan_info in channel_manager.list_channels() { println!(""); @@ -408,10 +422,21 @@ fn list_channels(channel_manager: &Arc) { 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); } diff --git a/src/main.rs b/src/main.rs index e962db8..ad48efd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(),