From 97ea6989f2ffe859614cc6c7e0a756e2c1efddb7 Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Tue, 13 Sep 2022 17:07:48 -0400 Subject: [PATCH] Update to LDK 0.0.111 and add command for sending an empty onion message --- Cargo.lock | 59 ++++++++++++++++++++++++++++++----------------------- Cargo.toml | 16 +++++++-------- src/cli.rs | 53 +++++++++++++++++++++++++++++++++++++++++++---- src/main.rs | 15 +++++++++++--- 4 files changed, 102 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f07b3f..f85bf81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,13 +20,19 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf9ff0bbfd639f15c74af777d81383cf53efb7c93613f6cab67c6c11e05bbf8b" +[[package]] +name = "bech32" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445" + [[package]] name = "bitcoin" -version = "0.28.1" +version = "0.29.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05bba324e6baf655b882df672453dbbc527bc938cadd27750ae510aaccc3a66a" +checksum = "9cb36de3b18ad25f396f9168302e36fb7e1e8923298ab3127da252d288d5af9d" dependencies = [ - "bech32", + "bech32 0.9.1", "bitcoin_hashes", "secp256k1", ] @@ -37,14 +43,14 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "754eb4c7f35c031f33c95cc257b4c4192a5c9d3de637d3ee78ab052a3f35da57" dependencies = [ - "bech32", + "bech32 0.8.1", ] [[package]] name = "bitcoin_hashes" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "006cc91e1a1d99819bc5b8214be3555c1f0611b169f527a1fdc54ed1f2b745b0" +checksum = "90064b8dee6815a6470d60bad07bbbaee885c0e12d04177138fa3291a01b7bc4" [[package]] name = "bytes" @@ -204,7 +210,7 @@ name = "ldk-tutorial-node" version = "0.1.0" dependencies = [ "base64", - "bech32", + "bech32 0.8.1", "bitcoin", "bitcoin-bech32", "chrono", @@ -230,18 +236,18 @@ checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" [[package]] name = "lightning" -version = "0.0.110" +version = "0.0.111" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dce6da860338d5a9ddc3fd42432465310cfab93b342bbd23b41b7c1f7c509d3" +checksum = "d86c207bcbca1c9dfb3668b81e2ad35b81370e583f2f8c5ee642a9f07925b05e" dependencies = [ "bitcoin", ] [[package]] name = "lightning-background-processor" -version = "0.0.110" +version = "0.0.111" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8de9d0de42bb933ffb8d33c6b0a75302f08b35126bfc74398ba01ad0c201f8d" +checksum = "0dfef23242b3656fa54a050b5cc4f1f7938d27cbf940c6c9e5b9a08f198f8d86" dependencies = [ "bitcoin", "lightning", @@ -250,9 +256,9 @@ dependencies = [ [[package]] name = "lightning-block-sync" -version = "0.0.110" +version = "0.0.111" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cee1ad5df24c87e5d09a655af9d36e12c193961c908b71fd1a5fa39f01cfa0d" +checksum = "1d1bb816be90f9b182627822ba3ed0b68cbf29315228fe54e4436fe5ad12df25" dependencies = [ "bitcoin", "chunked_transfer", @@ -264,11 +270,11 @@ dependencies = [ [[package]] name = "lightning-invoice" -version = "0.18.0" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32aa02b7fd0bd95e40b6ca8d9d9232b162d5e23b41bd2bc42abe9e9c78d34d72" +checksum = "023d5bd83191e13281b877526efb503a96aaae7ef97c7ba6359ba73f9c1bee11" dependencies = [ - "bech32", + "bech32 0.9.1", "bitcoin_hashes", "lightning", "num-traits", @@ -277,9 +283,9 @@ dependencies = [ [[package]] name = "lightning-net-tokio" -version = "0.0.110" +version = "0.0.111" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce57d093fbc643835bc64c0501b52a3531d2511dcb1237d0495d68fea3adc47d" +checksum = "1ce3c24e8e3d2c34ec503d3a0f76c07b0f4815705737a6a739058638c774580a" dependencies = [ "bitcoin", "lightning", @@ -288,9 +294,9 @@ dependencies = [ [[package]] name = "lightning-persister" -version = "0.0.110" +version = "0.0.111" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9abe8137de42431ed344f64821c12cf02f934c7869f5f8febaa1b3cb851e1fc1" +checksum = "235c5419f4f1b699176f51709317c88651c6bcfe3104be87294e1b0ba11a16af" dependencies = [ "bitcoin", "libc", @@ -300,9 +306,9 @@ dependencies = [ [[package]] name = "lightning-rapid-gossip-sync" -version = "0.0.110" +version = "0.0.111" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "391732631b14f7a1d9dc84dc3f644484d9b73190a31087b3856505cf0525bea0" +checksum = "ff55ab0918a12e5eecf511a0ae924340361dd64213139af689da51c0759f59b8" dependencies = [ "bitcoin", "lightning", @@ -458,18 +464,19 @@ checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695" [[package]] name = "secp256k1" -version = "0.22.1" +version = "0.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26947345339603ae8395f68e2f3d85a6b0a8ddfe6315818e80b8504415099db0" +checksum = "b7649a0b3ffb32636e60c7ce0d70511eda9c52c658cd0634e194d5a19943aeff" dependencies = [ + "bitcoin_hashes", "secp256k1-sys", ] [[package]] name = "secp256k1-sys" -version = "0.5.2" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "152e20a0fd0519390fc43ab404663af8a0b794273d2a91d60ad4a39f13ffe110" +checksum = "7058dc8eaf3f2810d7828680320acda0b25a288f6d288e19278e249bbf74226b" dependencies = [ "cc", ] diff --git a/Cargo.toml b/Cargo.toml index 475ea55..0f20050 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,16 +8,16 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -lightning = { version = "0.0.110", features = ["max_level_trace"] } -lightning-block-sync = { version = "0.0.110", features = [ "rpc-client" ] } -lightning-invoice = { version = "0.18" } -lightning-net-tokio = { version = "0.0.110" } -lightning-persister = { version = "0.0.110" } -lightning-background-processor = { version = "0.0.110" } -lightning-rapid-gossip-sync = { version = "0.0.110" } +lightning = { version = "0.0.111", features = ["max_level_trace"] } +lightning-block-sync = { version = "0.0.111", features = [ "rpc-client" ] } +lightning-invoice = { version = "0.19" } +lightning-net-tokio = { version = "0.0.111" } +lightning-persister = { version = "0.0.111" } +lightning-background-processor = { version = "0.0.111" } +lightning-rapid-gossip-sync = { version = "0.0.111" } base64 = "0.13.0" -bitcoin = "0.28.1" +bitcoin = "0.29.0" bitcoin-bech32 = "0.12" bech32 = "0.8" hex = "0.3" diff --git a/src/cli.rs b/src/cli.rs index 5863ed0..f50d3f9 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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 { pub(crate) async fn poll_for_user_input( invoice_payer: Arc>, peer_manager: Arc, channel_manager: Arc, keys_manager: Arc, - network_graph: Arc, inbound_payments: PaymentInfoStorage, - outbound_payments: PaymentInfoStorage, ldk_data_dir: String, network: Network, + network_graph: Arc, onion_messenger: 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"); @@ -416,6 +418,48 @@ pub(crate) async fn poll_for_user_input( ) ); } + "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 "); + println!("sendonionmessage "); } fn node_info(channel_manager: &Arc, peer_manager: &Arc) { diff --git a/src/main.rs b/src/main.rs index 1fddf8b..9c85b70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ use lightning::ln::channelmanager::{ }; use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler, SimpleArcPeerManager}; use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret}; +use lightning::onion_message::SimpleArcOnionMessenger; use lightning::routing::gossip; use lightning::routing::gossip::{NodeId, P2PGossipSync}; use lightning::routing::scoring::ProbabilisticScorer; @@ -112,6 +113,8 @@ type Router = DefaultRouter, Arc>; pub(crate) type NetworkGraph = gossip::NetworkGraph>; +type OnionMessenger = SimpleArcOnionMessenger; + async fn handle_ldk_events( channel_manager: &Arc, bitcoind_client: &BitcoindClient, network_graph: &NetworkGraph, keys_manager: &KeysManager, @@ -554,18 +557,23 @@ async fn start_ldk() { // Step 12: Initialize the PeerManager let channel_manager: Arc = Arc::new(channel_manager); + let onion_messenger: Arc = + Arc::new(OnionMessenger::new(keys_manager.clone(), logger.clone())); let mut ephemeral_bytes = [0; 32]; + let current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs(); rand::thread_rng().fill_bytes(&mut ephemeral_bytes); let lightning_msg_handler = MessageHandler { chan_handler: channel_manager.clone(), route_handler: gossip_sync.clone(), + onion_message_handler: onion_messenger.clone(), }; let peer_manager: Arc = Arc::new(PeerManager::new( lightning_msg_handler, keys_manager.get_node_secret(Recipient::Node).unwrap(), + current_time, &ephemeral_bytes, logger.clone(), - Arc::new(IgnoringMessageHandler {}), + IgnoringMessageHandler {}, )); // ## Running LDK @@ -721,14 +729,14 @@ async fn start_ldk() { // some public channels, and is only useful if we have public listen address(es) to announce. // In a production environment, this should occur only after the announcement of new channels // to avoid churn in the global network graph. - let chan_manager = Arc::clone(&channel_manager); + let peer_man = Arc::clone(&peer_manager); let network = args.network; if !args.ldk_announced_listen_addr.is_empty() { tokio::spawn(async move { let mut interval = tokio::time::interval(Duration::from_secs(60)); loop { interval.tick().await; - chan_manager.broadcast_node_announcement( + peer_man.broadcast_node_announcement( [0; 3], args.ldk_announced_node_name, args.ldk_announced_listen_addr.clone(), @@ -744,6 +752,7 @@ async fn start_ldk() { Arc::clone(&channel_manager), Arc::clone(&keys_manager), Arc::clone(&network_graph), + Arc::clone(&onion_messenger), inbound_payments, outbound_payments, ldk_data_dir.clone(), -- 2.39.5