Adapt to the lightning_invoice crate moving to Rust-Lightning
[ldk-sample] / src / cli.rs
1 use crate::disk;
2 use crate::hex_utils;
3 use crate::{
4         ChannelManager, FilesystemLogger, HTLCStatus, MillisatAmount, PaymentInfo, PaymentInfoStorage,
5         PeerManager,
6 };
7 use bitcoin::hashes::sha256::Hash as Sha256Hash;
8 use bitcoin::hashes::Hash;
9 use bitcoin::network::constants::Network;
10 use bitcoin::secp256k1::key::{PublicKey, SecretKey};
11 use bitcoin::secp256k1::Secp256k1;
12 use lightning::chain;
13 use lightning::ln::channelmanager::{PaymentHash, PaymentPreimage, PaymentSecret};
14 use lightning::ln::features::InvoiceFeatures;
15 use lightning::routing::network_graph::{NetGraphMsgHandler, RoutingFees};
16 use lightning::routing::router;
17 use lightning::routing::router::RouteHint;
18 use lightning::util::config::UserConfig;
19 use lightning_invoice::{Currency, Invoice, InvoiceBuilder, Route, RouteHop};
20 use rand;
21 use rand::Rng;
22 use std::env;
23 use std::io;
24 use std::io::{BufRead, Write};
25 use std::net::{SocketAddr, TcpStream};
26 use std::ops::Deref;
27 use std::path::Path;
28 use std::str::FromStr;
29 use std::sync::Arc;
30 use std::time::Duration;
31 use tokio::sync::mpsc;
32
33 pub(crate) struct LdkUserInfo {
34         pub(crate) bitcoind_rpc_username: String,
35         pub(crate) bitcoind_rpc_password: String,
36         pub(crate) bitcoind_rpc_port: u16,
37         pub(crate) bitcoind_rpc_host: String,
38         pub(crate) ldk_storage_dir_path: String,
39         pub(crate) ldk_peer_listening_port: u16,
40         pub(crate) network: Network,
41 }
42
43 pub(crate) fn parse_startup_args() -> Result<LdkUserInfo, ()> {
44         if env::args().len() < 4 {
45                 println!("ldk-tutorial-node requires 3 arguments: `cargo run <bitcoind-rpc-username>:<bitcoind-rpc-password>@<bitcoind-rpc-host>:<bitcoind-rpc-port> ldk_storage_directory_path [<ldk-incoming-peer-listening-port>] [bitcoin-network]`");
46                 return Err(());
47         }
48         let bitcoind_rpc_info = env::args().skip(1).next().unwrap();
49         let bitcoind_rpc_info_parts: Vec<&str> = bitcoind_rpc_info.split("@").collect();
50         if bitcoind_rpc_info_parts.len() != 2 {
51                 println!("ERROR: bad bitcoind RPC URL provided");
52                 return Err(());
53         }
54         let rpc_user_and_password: Vec<&str> = bitcoind_rpc_info_parts[0].split(":").collect();
55         if rpc_user_and_password.len() != 2 {
56                 println!("ERROR: bad bitcoind RPC username/password combo provided");
57                 return Err(());
58         }
59         let bitcoind_rpc_username = rpc_user_and_password[0].to_string();
60         let bitcoind_rpc_password = rpc_user_and_password[1].to_string();
61         let bitcoind_rpc_path: Vec<&str> = bitcoind_rpc_info_parts[1].split(":").collect();
62         if bitcoind_rpc_path.len() != 2 {
63                 println!("ERROR: bad bitcoind RPC path provided");
64                 return Err(());
65         }
66         let bitcoind_rpc_host = bitcoind_rpc_path[0].to_string();
67         let bitcoind_rpc_port = bitcoind_rpc_path[1].parse::<u16>().unwrap();
68
69         let ldk_storage_dir_path = env::args().skip(2).next().unwrap();
70
71         let mut ldk_peer_port_set = true;
72         let ldk_peer_listening_port: u16 = match env::args().skip(3).next().map(|p| p.parse()) {
73                 Some(Ok(p)) => p,
74                 Some(Err(e)) => panic!(e),
75                 None => {
76                         ldk_peer_port_set = false;
77                         9735
78                 }
79         };
80
81         let arg_idx = match ldk_peer_port_set {
82                 true => 4,
83                 false => 3,
84         };
85         let network: Network = match env::args().skip(arg_idx).next().as_ref().map(String::as_str) {
86                 Some("testnet") => Network::Testnet,
87                 Some("regtest") => Network::Regtest,
88                 Some(_) => panic!("Unsupported network provided. Options are: `regtest`, `testnet`"),
89                 None => Network::Testnet,
90         };
91         Ok(LdkUserInfo {
92                 bitcoind_rpc_username,
93                 bitcoind_rpc_password,
94                 bitcoind_rpc_host,
95                 bitcoind_rpc_port,
96                 ldk_storage_dir_path,
97                 ldk_peer_listening_port,
98                 network,
99         })
100 }
101
102 pub(crate) async fn poll_for_user_input(
103         peer_manager: Arc<PeerManager>, channel_manager: Arc<ChannelManager>,
104         router: Arc<NetGraphMsgHandler<Arc<dyn chain::Access>, Arc<FilesystemLogger>>>,
105         inbound_payments: PaymentInfoStorage, outbound_payments: PaymentInfoStorage,
106         node_privkey: SecretKey, event_notifier: mpsc::Sender<()>, ldk_data_dir: String,
107         logger: Arc<FilesystemLogger>, network: Network,
108 ) {
109         println!("LDK startup successful. To view available commands: \"help\".\nLDK logs are available at <your-supplied-ldk-data-dir-path>/.ldk/logs");
110         let stdin = io::stdin();
111         print!("> ");
112         io::stdout().flush().unwrap(); // Without flushing, the `>` doesn't print
113         for line in stdin.lock().lines() {
114                 let _ = event_notifier.try_send(());
115                 let line = line.unwrap();
116                 let mut words = line.split_whitespace();
117                 if let Some(word) = words.next() {
118                         match word {
119                                 "help" => help(),
120                                 "openchannel" => {
121                                         let peer_pubkey_and_ip_addr = words.next();
122                                         let channel_value_sat = words.next();
123                                         if peer_pubkey_and_ip_addr.is_none() || channel_value_sat.is_none() {
124                                                 println!("ERROR: openchannel has 2 required arguments: `openchannel pubkey@host:port channel_amt_satoshis` [--public]");
125                                                 print!("> ");
126                                                 io::stdout().flush().unwrap();
127                                                 continue;
128                                         }
129                                         let peer_pubkey_and_ip_addr = peer_pubkey_and_ip_addr.unwrap();
130                                         let (pubkey, peer_addr) =
131                                                 match parse_peer_info(peer_pubkey_and_ip_addr.to_string()) {
132                                                         Ok(info) => info,
133                                                         Err(e) => {
134                                                                 println!("{:?}", e.into_inner().unwrap());
135                                                                 print!("> ");
136                                                                 io::stdout().flush().unwrap();
137                                                                 continue;
138                                                         }
139                                                 };
140
141                                         let chan_amt_sat: Result<u64, _> = channel_value_sat.unwrap().parse();
142                                         if chan_amt_sat.is_err() {
143                                                 println!("ERROR: channel amount must be a number");
144                                                 print!("> ");
145                                                 io::stdout().flush().unwrap();
146                                                 continue;
147                                         }
148
149                                         if connect_peer_if_necessary(
150                                                 pubkey,
151                                                 peer_addr,
152                                                 peer_manager.clone(),
153                                                 event_notifier.clone(),
154                                         )
155                                         .is_err()
156                                         {
157                                                 print!("> ");
158                                                 io::stdout().flush().unwrap();
159                                                 continue;
160                                         };
161
162                                         let announce_channel = match words.next() {
163                                                 Some("--public") | Some("--public=true") => true,
164                                                 Some("--public=false") => false,
165                                                 Some(_) => {
166                                                         println!("ERROR: invalid `--public` command format. Valid formats: `--public`, `--public=true` `--public=false`");
167                                                         print!("> ");
168                                                         io::stdout().flush().unwrap();
169                                                         continue;
170                                                 }
171                                                 None => false,
172                                         };
173
174                                         if open_channel(
175                                                 pubkey,
176                                                 chan_amt_sat.unwrap(),
177                                                 announce_channel,
178                                                 channel_manager.clone(),
179                                         )
180                                         .is_ok()
181                                         {
182                                                 let peer_data_path = format!("{}/channel_peer_data", ldk_data_dir.clone());
183                                                 let _ = disk::persist_channel_peer(
184                                                         Path::new(&peer_data_path),
185                                                         peer_pubkey_and_ip_addr,
186                                                 );
187                                         }
188                                 }
189                                 "sendpayment" => {
190                                         let invoice_str = words.next();
191                                         if invoice_str.is_none() {
192                                                 println!("ERROR: sendpayment requires an invoice: `sendpayment <invoice>`");
193                                                 print!("> ");
194                                                 io::stdout().flush().unwrap();
195                                                 continue;
196                                         }
197
198                                         let invoice_res = Invoice::from_str(invoice_str.unwrap());
199                                         if invoice_res.is_err() {
200                                                 println!("ERROR: invalid invoice: {:?}", invoice_res.unwrap_err());
201                                                 print!("> ");
202                                                 io::stdout().flush().unwrap();
203                                                 continue;
204                                         }
205                                         let invoice = invoice_res.unwrap();
206                                         let route_hints: Vec<Route> =
207                                                 invoice.routes().iter().map(|&route| route.clone()).collect();
208
209                                         let amt_pico_btc = invoice.amount_pico_btc();
210                                         if amt_pico_btc.is_none() {
211                                                 println!("ERROR: invalid invoice: must contain amount to pay");
212                                                 print!("> ");
213                                                 io::stdout().flush().unwrap();
214                                                 continue;
215                                         }
216                                         let amt_msat = amt_pico_btc.unwrap() / 10;
217
218                                         let payee_pubkey = invoice.recover_payee_pub_key();
219                                         let final_cltv = *invoice.min_final_cltv_expiry().unwrap_or(&9) as u32;
220
221                                         let mut payment_hash = PaymentHash([0; 32]);
222                                         payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
223
224                                         let payment_secret = match invoice.payment_secret() {
225                                                 Some(secret) => {
226                                                         let mut payment_secret = PaymentSecret([0; 32]);
227                                                         payment_secret.0.copy_from_slice(&secret.0);
228                                                         Some(payment_secret)
229                                                 }
230                                                 None => None,
231                                         };
232
233                                         // rust-lightning-invoice doesn't currently support features, so we parse features
234                                         // manually from the invoice.
235                                         let mut invoice_features = InvoiceFeatures::empty();
236                                         for field in &invoice.into_signed_raw().raw_invoice().data.tagged_fields {
237                                                 match field {
238                                                         lightning_invoice::RawTaggedField::UnknownSemantics(vec) => {
239                                                                 if vec[0] == bech32::u5::try_from_u8(5).unwrap() {
240                                                                         if vec.len() >= 6 && vec[5].to_u8() & 0b10000 != 0 {
241                                                                                 invoice_features =
242                                                                                         invoice_features.set_variable_length_onion_optional();
243                                                                         }
244                                                                         if vec.len() >= 6 && vec[5].to_u8() & 0b01000 != 0 {
245                                                                                 invoice_features =
246                                                                                         invoice_features.set_variable_length_onion_required();
247                                                                         }
248                                                                         if vec.len() >= 4 && vec[3].to_u8() & 0b00001 != 0 {
249                                                                                 invoice_features =
250                                                                                         invoice_features.set_payment_secret_optional();
251                                                                         }
252                                                                         if vec.len() >= 5 && vec[4].to_u8() & 0b10000 != 0 {
253                                                                                 invoice_features =
254                                                                                         invoice_features.set_payment_secret_required();
255                                                                         }
256                                                                         if vec.len() >= 4 && vec[3].to_u8() & 0b00100 != 0 {
257                                                                                 invoice_features =
258                                                                                         invoice_features.set_basic_mpp_optional();
259                                                                         }
260                                                                         if vec.len() >= 4 && vec[3].to_u8() & 0b00010 != 0 {
261                                                                                 invoice_features =
262                                                                                         invoice_features.set_basic_mpp_required();
263                                                                         }
264                                                                 }
265                                                         }
266                                                         _ => {}
267                                                 }
268                                         }
269                                         let invoice_features_opt = match invoice_features == InvoiceFeatures::empty() {
270                                                 true => None,
271                                                 false => Some(invoice_features),
272                                         };
273                                         send_payment(
274                                                 payee_pubkey,
275                                                 amt_msat,
276                                                 final_cltv,
277                                                 payment_hash,
278                                                 payment_secret,
279                                                 invoice_features_opt,
280                                                 route_hints,
281                                                 router.clone(),
282                                                 channel_manager.clone(),
283                                                 outbound_payments.clone(),
284                                                 logger.clone(),
285                                         );
286                                 }
287                                 "getinvoice" => {
288                                         let amt_str = words.next();
289                                         if amt_str.is_none() {
290                                                 println!("ERROR: getinvoice requires an amount in millisatoshis");
291                                                 print!("> ");
292                                                 io::stdout().flush().unwrap();
293                                                 continue;
294                                         }
295
296                                         let amt_msat: Result<u64, _> = amt_str.unwrap().parse();
297                                         if amt_msat.is_err() {
298                                                 println!("ERROR: getinvoice provided payment amount was not a number");
299                                                 print!("> ");
300                                                 io::stdout().flush().unwrap();
301                                                 continue;
302                                         }
303                                         get_invoice(
304                                                 amt_msat.unwrap(),
305                                                 inbound_payments.clone(),
306                                                 node_privkey.clone(),
307                                                 channel_manager.clone(),
308                                                 network,
309                                         );
310                                 }
311                                 "connectpeer" => {
312                                         let peer_pubkey_and_ip_addr = words.next();
313                                         if peer_pubkey_and_ip_addr.is_none() {
314                                                 println!("ERROR: connectpeer requires peer connection info: `connectpeer pubkey@host:port`");
315                                                 print!("> ");
316                                                 io::stdout().flush().unwrap();
317                                                 continue;
318                                         }
319                                         let (pubkey, peer_addr) =
320                                                 match parse_peer_info(peer_pubkey_and_ip_addr.unwrap().to_string()) {
321                                                         Ok(info) => info,
322                                                         Err(e) => {
323                                                                 println!("{:?}", e.into_inner().unwrap());
324                                                                 print!("> ");
325                                                                 io::stdout().flush().unwrap();
326                                                                 continue;
327                                                         }
328                                                 };
329                                         if connect_peer_if_necessary(
330                                                 pubkey,
331                                                 peer_addr,
332                                                 peer_manager.clone(),
333                                                 event_notifier.clone(),
334                                         )
335                                         .is_ok()
336                                         {
337                                                 println!("SUCCESS: connected to peer {}", pubkey);
338                                         }
339                                 }
340                                 "listchannels" => list_channels(channel_manager.clone()),
341                                 "listpayments" => {
342                                         list_payments(inbound_payments.clone(), outbound_payments.clone())
343                                 }
344                                 "closechannel" => {
345                                         let channel_id_str = words.next();
346                                         if channel_id_str.is_none() {
347                                                 println!("ERROR: closechannel requires a channel ID: `closechannel <channel_id>`");
348                                                 print!("> ");
349                                                 io::stdout().flush().unwrap();
350                                                 continue;
351                                         }
352                                         let channel_id_vec = hex_utils::to_vec(channel_id_str.unwrap());
353                                         if channel_id_vec.is_none() {
354                                                 println!("ERROR: couldn't parse channel_id as hex");
355                                                 print!("> ");
356                                                 io::stdout().flush().unwrap();
357                                                 continue;
358                                         }
359                                         let mut channel_id = [0; 32];
360                                         channel_id.copy_from_slice(&channel_id_vec.unwrap());
361                                         close_channel(channel_id, channel_manager.clone());
362                                 }
363                                 "forceclosechannel" => {
364                                         let channel_id_str = words.next();
365                                         if channel_id_str.is_none() {
366                                                 println!("ERROR: forceclosechannel requires a channel ID: `forceclosechannel <channel_id>`");
367                                                 print!("> ");
368                                                 io::stdout().flush().unwrap();
369                                                 continue;
370                                         }
371                                         let channel_id_vec = hex_utils::to_vec(channel_id_str.unwrap());
372                                         if channel_id_vec.is_none() {
373                                                 println!("ERROR: couldn't parse channel_id as hex");
374                                                 print!("> ");
375                                                 io::stdout().flush().unwrap();
376                                                 continue;
377                                         }
378                                         let mut channel_id = [0; 32];
379                                         channel_id.copy_from_slice(&channel_id_vec.unwrap());
380                                         force_close_channel(channel_id, channel_manager.clone());
381                                 }
382                                 _ => println!("Unknown command. See `\"help\" for available commands."),
383                         }
384                 }
385                 print!("> ");
386                 io::stdout().flush().unwrap();
387         }
388 }
389
390 fn help() {
391         println!("openchannel pubkey@host:port <channel_amt_satoshis>");
392         println!("sendpayment <invoice>");
393         println!("getinvoice <amt_in_millisatoshis>");
394         println!("connectpeer pubkey@host:port");
395         println!("listchannels");
396         println!("listpayments");
397         println!("closechannel <channel_id>");
398         println!("forceclosechannel <channel_id>");
399 }
400
401 fn list_channels(channel_manager: Arc<ChannelManager>) {
402         print!("[");
403         for chan_info in channel_manager.list_channels() {
404                 println!("");
405                 println!("\t{{");
406                 println!("\t\tchannel_id: {},", hex_utils::hex_str(&chan_info.channel_id[..]));
407                 println!(
408                         "\t\tpeer_pubkey: {},",
409                         hex_utils::hex_str(&chan_info.remote_network_id.serialize())
410                 );
411                 let mut pending_channel = false;
412                 match chan_info.short_channel_id {
413                         Some(id) => println!("\t\tshort_channel_id: {},", id),
414                         None => {
415                                 pending_channel = true;
416                         }
417                 }
418                 println!("\t\tpending_open: {},", pending_channel);
419                 println!("\t\tchannel_value_satoshis: {},", chan_info.channel_value_satoshis);
420                 println!("\t\tchannel_can_send_payments: {},", chan_info.is_live);
421                 println!("\t}},");
422         }
423         println!("]");
424 }
425
426 fn list_payments(inbound_payments: PaymentInfoStorage, outbound_payments: PaymentInfoStorage) {
427         let inbound = inbound_payments.lock().unwrap();
428         let outbound = outbound_payments.lock().unwrap();
429         print!("[");
430         for (payment_hash, payment_info) in inbound.deref() {
431                 println!("");
432                 println!("\t{{");
433                 println!("\t\tamount_millisatoshis: {},", payment_info.amt_msat);
434                 println!("\t\tpayment_hash: {},", hex_utils::hex_str(&payment_hash.0));
435                 println!("\t\thtlc_direction: inbound,");
436                 println!(
437                         "\t\thtlc_status: {},",
438                         match payment_info.status {
439                                 HTLCStatus::Pending => "pending",
440                                 HTLCStatus::Succeeded => "succeeded",
441                                 HTLCStatus::Failed => "failed",
442                         }
443                 );
444
445                 println!("\t}},");
446         }
447
448         for (payment_hash, payment_info) in outbound.deref() {
449                 println!("");
450                 println!("\t{{");
451                 println!("\t\tamount_millisatoshis: {},", payment_info.amt_msat);
452                 println!("\t\tpayment_hash: {},", hex_utils::hex_str(&payment_hash.0));
453                 println!("\t\thtlc_direction: outbound,");
454                 println!(
455                         "\t\thtlc_status: {},",
456                         match payment_info.status {
457                                 HTLCStatus::Pending => "pending",
458                                 HTLCStatus::Succeeded => "succeeded",
459                                 HTLCStatus::Failed => "failed",
460                         }
461                 );
462
463                 println!("\t}},");
464         }
465         println!("]");
466 }
467
468 pub(crate) fn connect_peer_if_necessary(
469         pubkey: PublicKey, peer_addr: SocketAddr, peer_manager: Arc<PeerManager>,
470         event_notifier: mpsc::Sender<()>,
471 ) -> Result<(), ()> {
472         for node_pubkey in peer_manager.get_peer_node_ids() {
473                 if node_pubkey == pubkey {
474                         return Ok(());
475                 }
476         }
477         match TcpStream::connect_timeout(&peer_addr, Duration::from_secs(10)) {
478                 Ok(stream) => {
479                         let peer_mgr = peer_manager.clone();
480                         let event_ntfns = event_notifier.clone();
481                         tokio::spawn(async move {
482                                 lightning_net_tokio::setup_outbound(peer_mgr, event_ntfns, pubkey, stream).await;
483                         });
484                         let mut peer_connected = false;
485                         while !peer_connected {
486                                 for node_pubkey in peer_manager.get_peer_node_ids() {
487                                         if node_pubkey == pubkey {
488                                                 peer_connected = true;
489                                         }
490                                 }
491                         }
492                 }
493                 Err(e) => {
494                         println!("ERROR: failed to connect to peer: {:?}", e);
495                         return Err(());
496                 }
497         }
498         Ok(())
499 }
500
501 fn open_channel(
502         peer_pubkey: PublicKey, channel_amt_sat: u64, announce_channel: bool,
503         channel_manager: Arc<ChannelManager>,
504 ) -> Result<(), ()> {
505         let mut config = UserConfig::default();
506         if announce_channel {
507                 config.channel_options.announced_channel = true;
508         }
509         // lnd's max to_self_delay is 2016, so we want to be compatible.
510         config.peer_channel_config_limits.their_to_self_delay = 2016;
511         match channel_manager.create_channel(peer_pubkey, channel_amt_sat, 0, 0, None) {
512                 Ok(_) => {
513                         println!("EVENT: initiated channel with peer {}. ", peer_pubkey);
514                         return Ok(());
515                 }
516                 Err(e) => {
517                         println!("ERROR: failed to open channel: {:?}", e);
518                         return Err(());
519                 }
520         }
521 }
522
523 fn send_payment(
524         payee: PublicKey, amt_msat: u64, final_cltv: u32, payment_hash: PaymentHash,
525         payment_secret: Option<PaymentSecret>, payee_features: Option<InvoiceFeatures>,
526         mut route_hints: Vec<Route>,
527         router: Arc<NetGraphMsgHandler<Arc<dyn chain::Access>, Arc<FilesystemLogger>>>,
528         channel_manager: Arc<ChannelManager>, payment_storage: PaymentInfoStorage,
529         logger: Arc<FilesystemLogger>,
530 ) {
531         let network_graph = router.network_graph.read().unwrap();
532         let first_hops = channel_manager.list_usable_channels();
533         let payer_pubkey = channel_manager.get_our_node_id();
534
535         let mut hints: Vec<RouteHint> = Vec::new();
536         for route in route_hints.drain(..) {
537                 let route_hops = route.into_inner();
538                 let last_hop = &route_hops[route_hops.len() - 1];
539                 hints.push(RouteHint {
540                         src_node_id: last_hop.pubkey,
541                         short_channel_id: u64::from_be_bytes(last_hop.short_channel_id),
542                         fees: RoutingFees {
543                                 base_msat: last_hop.fee_base_msat,
544                                 proportional_millionths: last_hop.fee_proportional_millionths,
545                         },
546                         cltv_expiry_delta: last_hop.cltv_expiry_delta,
547                         htlc_minimum_msat: None,
548                         htlc_maximum_msat: None,
549                 })
550         }
551         let route = router::get_route(
552                 &payer_pubkey,
553                 &network_graph,
554                 &payee,
555                 payee_features,
556                 Some(&first_hops.iter().collect::<Vec<_>>()),
557                 &hints.iter().collect::<Vec<_>>(),
558                 amt_msat,
559                 final_cltv,
560                 logger,
561         );
562         if let Err(e) = route {
563                 println!("ERROR: failed to find route: {}", e.err);
564                 return;
565         }
566         let status = match channel_manager.send_payment(&route.unwrap(), payment_hash, &payment_secret)
567         {
568                 Ok(()) => {
569                         println!("EVENT: initiated sending {} msats to {}", amt_msat, payee);
570                         HTLCStatus::Pending
571                 }
572                 Err(e) => {
573                         println!("ERROR: failed to send payment: {:?}", e);
574                         HTLCStatus::Failed
575                 }
576         };
577         let mut payments = payment_storage.lock().unwrap();
578         payments.insert(
579                 payment_hash,
580                 PaymentInfo {
581                         preimage: None,
582                         secret: payment_secret,
583                         status,
584                         amt_msat: MillisatAmount(Some(amt_msat)),
585                 },
586         );
587 }
588
589 fn get_invoice(
590         amt_msat: u64, payment_storage: PaymentInfoStorage, our_node_privkey: SecretKey,
591         channel_manager: Arc<ChannelManager>, network: Network,
592 ) {
593         let mut payments = payment_storage.lock().unwrap();
594         let secp_ctx = Secp256k1::new();
595
596         let mut preimage = [0; 32];
597         rand::thread_rng().fill_bytes(&mut preimage);
598         let payment_hash = Sha256Hash::hash(&preimage);
599
600         let our_node_pubkey = channel_manager.get_our_node_id();
601         let mut invoice = InvoiceBuilder::new(match network {
602                 Network::Bitcoin => Currency::Bitcoin,
603                 Network::Testnet => Currency::BitcoinTestnet,
604                 Network::Regtest => Currency::Regtest,
605                 Network::Signet => panic!("Signet invoices not supported"),
606         })
607         .payment_hash(payment_hash)
608         .description("rust-lightning-bitcoinrpc invoice".to_string())
609         .amount_pico_btc(amt_msat * 10)
610         .current_timestamp()
611         .payee_pub_key(our_node_pubkey);
612
613         // Add route hints to the invoice.
614         let our_channels = channel_manager.list_usable_channels();
615         let mut min_final_cltv_expiry = 9;
616         for channel in our_channels {
617                 let short_channel_id = match channel.short_channel_id {
618                         Some(id) => id.to_be_bytes(),
619                         None => continue,
620                 };
621                 let forwarding_info = match channel.counterparty_forwarding_info {
622                         Some(info) => info,
623                         None => continue,
624                 };
625                 if forwarding_info.cltv_expiry_delta > min_final_cltv_expiry {
626                         min_final_cltv_expiry = forwarding_info.cltv_expiry_delta;
627                 }
628                 invoice = invoice.route(vec![RouteHop {
629                         pubkey: channel.remote_network_id,
630                         short_channel_id,
631                         fee_base_msat: forwarding_info.fee_base_msat,
632                         fee_proportional_millionths: forwarding_info.fee_proportional_millionths,
633                         cltv_expiry_delta: forwarding_info.cltv_expiry_delta,
634                 }]);
635         }
636         invoice = invoice.min_final_cltv_expiry(min_final_cltv_expiry.into());
637
638         // Sign the invoice.
639         let invoice =
640                 invoice.build_signed(|msg_hash| secp_ctx.sign_recoverable(msg_hash, &our_node_privkey));
641
642         match invoice {
643                 Ok(invoice) => println!("SUCCESS: generated invoice: {}", invoice),
644                 Err(e) => println!("ERROR: failed to create invoice: {:?}", e),
645         }
646
647         payments.insert(
648                 PaymentHash(payment_hash.into_inner()),
649                 PaymentInfo {
650                         preimage: Some(PaymentPreimage(preimage)),
651                         // We can't add payment secrets to invoices until we support features in invoices.
652                         // Otherwise lnd errors with "destination hop doesn't understand payment addresses"
653                         // (for context, lnd calls payment secrets "payment addresses").
654                         secret: None,
655                         status: HTLCStatus::Pending,
656                         amt_msat: MillisatAmount(Some(amt_msat)),
657                 },
658         );
659 }
660
661 fn close_channel(channel_id: [u8; 32], channel_manager: Arc<ChannelManager>) {
662         match channel_manager.close_channel(&channel_id) {
663                 Ok(()) => println!("EVENT: initiating channel close"),
664                 Err(e) => println!("ERROR: failed to close channel: {:?}", e),
665         }
666 }
667
668 fn force_close_channel(channel_id: [u8; 32], channel_manager: Arc<ChannelManager>) {
669         match channel_manager.force_close_channel(&channel_id) {
670                 Ok(()) => println!("EVENT: initiating channel force-close"),
671                 Err(e) => println!("ERROR: failed to force-close channel: {:?}", e),
672         }
673 }
674
675 pub(crate) fn parse_peer_info(
676         peer_pubkey_and_ip_addr: String,
677 ) -> Result<(PublicKey, SocketAddr), std::io::Error> {
678         let mut pubkey_and_addr = peer_pubkey_and_ip_addr.split("@");
679         let pubkey = pubkey_and_addr.next();
680         let peer_addr_str = pubkey_and_addr.next();
681         if peer_addr_str.is_none() || peer_addr_str.is_none() {
682                 return Err(std::io::Error::new(
683                         std::io::ErrorKind::Other,
684                         "ERROR: incorrectly formatted peer
685         info. Should be formatted as: `pubkey@host:port`",
686                 ));
687         }
688
689         let peer_addr: Result<SocketAddr, _> = peer_addr_str.unwrap().parse();
690         if peer_addr.is_err() {
691                 return Err(std::io::Error::new(
692                         std::io::ErrorKind::Other,
693                         "ERROR: couldn't parse pubkey@host:port into a socket address",
694                 ));
695         }
696
697         let pubkey = hex_utils::to_compressed_pubkey(pubkey.unwrap());
698         if pubkey.is_none() {
699                 return Err(std::io::Error::new(
700                         std::io::ErrorKind::Other,
701                         "ERROR: unable to parse given pubkey for node",
702                 ));
703         }
704
705         Ok((pubkey.unwrap(), peer_addr.unwrap()))
706 }