X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fcli.rs;h=7d865158e6e7e786a93b52ff99a513d766f50b08;hb=8984205acf1d328837d0f33798e89e479aea1882;hp=5863ed0144430dda1fbe18231b7cbe70b0a988b6;hpb=64d7bdce16ae5a6d83cc290551b42574db177193;p=ldk-sample diff --git a/src/cli.rs b/src/cli.rs index 5863ed0..7d86515 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; @@ -18,7 +19,7 @@ use lightning_invoice::payment::PaymentError; use lightning_invoice::{utils, Currency, Invoice}; use std::env; use std::io; -use std::io::{BufRead, Write}; +use std::io::Write; use std::net::{IpAddr, SocketAddr, ToSocketAddrs}; use std::ops::Deref; use std::path::Path; @@ -142,21 +143,21 @@ 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"); println!("Local Node ID is {}.", channel_manager.get_our_node_id()); - let stdin = io::stdin(); - let mut line_reader = stdin.lock().lines(); loop { print!("> "); io::stdout().flush().unwrap(); // Without flushing, the `>` doesn't print - let line = match line_reader.next() { - Some(l) => l.unwrap(), - None => break, - }; + let mut line = String::new(); + if let Err(e) = io::stdin().read_line(&mut line) { + break println!("ERROR: {e:#}"); + } + let mut words = line.split_whitespace(); if let Some(word) = words.next() { match word { @@ -416,6 +417,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 +478,7 @@ fn help() { println!("nodeinfo"); println!("listpeers"); println!("signmessage "); + println!("sendonionmessage "); } fn node_info(channel_manager: &Arc, peer_manager: &Arc) {