Update to LDK 0.0.111 and add command for sending an empty onion message
[ldk-sample] / src / cli.rs
index 5863ed0144430dda1fbe18231b7cbe70b0a988b6..f50d3f9fa175aca2e18b49b43d9e93386829a80f 100644 (file)
@@ -1,8 +1,8 @@
 use crate::disk;
 use crate::hex_utils;
 use crate::{
-       ChannelManager, HTLCStatus, InvoicePayer, MillisatAmount, NetworkGraph, PaymentInfo,
-       PaymentInfoStorage, PeerManager,
+       ChannelManager, HTLCStatus, InvoicePayer, MillisatAmount, NetworkGraph, OnionMessenger,
+       PaymentInfo, PaymentInfoStorage, PeerManager,
 };
 use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::hashes::Hash;
@@ -11,6 +11,7 @@ use bitcoin::secp256k1::PublicKey;
 use lightning::chain::keysinterface::{KeysInterface, KeysManager, Recipient};
 use lightning::ln::msgs::NetAddress;
 use lightning::ln::{PaymentHash, PaymentPreimage};
+use lightning::onion_message::Destination;
 use lightning::routing::gossip::NodeId;
 use lightning::util::config::{ChannelHandshakeConfig, ChannelHandshakeLimits, UserConfig};
 use lightning::util::events::EventHandler;
@@ -142,8 +143,9 @@ 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>,
-       network_graph: Arc<NetworkGraph>, inbound_payments: PaymentInfoStorage,
-       outbound_payments: PaymentInfoStorage, ldk_data_dir: String, network: Network,
+       network_graph: Arc<NetworkGraph>, onion_messenger: Arc<OnionMessenger>,
+       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");
@@ -416,6 +418,48 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                )
                                        );
                                }
+                               "sendonionmessage" => {
+                                       let path_pks_str = words.next();
+                                       if path_pks_str.is_none() {
+                                               println!(
+                                                       "ERROR: sendonionmessage requires at least one node id for the path"
+                                               );
+                                               continue;
+                                       }
+                                       let mut node_pks = Vec::new();
+                                       let mut errored = false;
+                                       for pk_str in path_pks_str.unwrap().split(",") {
+                                               let node_pubkey_vec = match hex_utils::to_vec(pk_str) {
+                                                       Some(peer_pubkey_vec) => peer_pubkey_vec,
+                                                       None => {
+                                                               println!("ERROR: couldn't parse peer_pubkey");
+                                                               errored = true;
+                                                               break;
+                                                       }
+                                               };
+                                               let node_pubkey = match PublicKey::from_slice(&node_pubkey_vec) {
+                                                       Ok(peer_pubkey) => peer_pubkey,
+                                                       Err(_) => {
+                                                               println!("ERROR: couldn't parse peer_pubkey");
+                                                               errored = true;
+                                                               break;
+                                                       }
+                                               };
+                                               node_pks.push(node_pubkey);
+                                       }
+                                       if errored {
+                                               continue;
+                                       }
+                                       let destination_pk = node_pks.pop().unwrap();
+                                       match onion_messenger.send_onion_message(
+                                               &node_pks,
+                                               Destination::Node(destination_pk),
+                                               None,
+                                       ) {
+                                               Ok(()) => println!("SUCCESS: forwarded onion message to first hop"),
+                                               Err(e) => println!("ERROR: failed to send onion message: {:?}", e),
+                                       }
+                               }
                                _ => println!("Unknown command. See `\"help\" for available commands."),
                        }
                }
@@ -435,6 +479,7 @@ fn help() {
        println!("nodeinfo");
        println!("listpeers");
        println!("signmessage <message>");
+       println!("sendonionmessage <node_id_1,node_id_2,..,destination_node_id>");
 }
 
 fn node_info(channel_manager: &Arc<ChannelManager>, peer_manager: &Arc<PeerManager>) {