Update to LDK 0.0.121
[ldk-sample] / src / cli.rs
index 99a43784cb69ad55b2aabd9598f6c47d6db3a9be..e3487421373c74ad6ae862fefe8f53119551edff 100644 (file)
@@ -1,31 +1,33 @@
-use crate::disk;
+use crate::disk::{self, INBOUND_PAYMENTS_FNAME, OUTBOUND_PAYMENTS_FNAME};
 use crate::hex_utils;
 use crate::{
-       ChannelManager, HTLCStatus, InvoicePayer, MillisatAmount, NetworkGraph, OnionMessenger,
-       PaymentInfo, PaymentInfoStorage, PeerManager,
+       ChannelManager, HTLCStatus, InboundPaymentInfoStorage, MillisatAmount, NetworkGraph,
+       OnionMessenger, OutboundPaymentInfoStorage, PaymentInfo, PeerManager,
 };
 use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::hashes::Hash;
 use bitcoin::network::constants::Network;
 use bitcoin::secp256k1::PublicKey;
-use lightning::chain::keysinterface::{KeysInterface, KeysManager, Recipient};
-use lightning::ln::msgs::{DecodeError, NetAddress};
-use lightning::ln::{PaymentHash, PaymentPreimage};
-use lightning::onion_message::{CustomOnionMessageContents, Destination, OnionMessageContents};
+use lightning::ln::channelmanager::{PaymentId, RecipientOnionFields, Retry};
+use lightning::ln::msgs::SocketAddress;
+use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage};
+use lightning::onion_message::messenger::Destination;
+use lightning::onion_message::packet::OnionMessageContents;
 use lightning::routing::gossip::NodeId;
+use lightning::routing::router::{PaymentParameters, RouteParameters};
+use lightning::sign::{EntropySource, KeysManager};
 use lightning::util::config::{ChannelHandshakeConfig, ChannelHandshakeLimits, UserConfig};
-use lightning::util::events::EventHandler;
-use lightning::util::ser::{MaybeReadableArgs, Writeable, Writer};
-use lightning_invoice::payment::PaymentError;
-use lightning_invoice::{utils, Currency, Invoice};
+use lightning::util::persist::KVStore;
+use lightning::util::ser::{Writeable, Writer};
+use lightning_invoice::{utils, Bolt11Invoice, Currency};
+use lightning_persister::fs_store::FilesystemStore;
 use std::env;
 use std::io;
 use std::io::Write;
-use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
-use std::ops::Deref;
+use std::net::{SocketAddr, ToSocketAddrs};
 use std::path::Path;
 use std::str::FromStr;
-use std::sync::Arc;
+use std::sync::{Arc, Mutex};
 use std::time::Duration;
 
 pub(crate) struct LdkUserInfo {
@@ -35,141 +37,35 @@ pub(crate) struct LdkUserInfo {
        pub(crate) bitcoind_rpc_host: String,
        pub(crate) ldk_storage_dir_path: String,
        pub(crate) ldk_peer_listening_port: u16,
-       pub(crate) ldk_announced_listen_addr: Vec<NetAddress>,
+       pub(crate) ldk_announced_listen_addr: Vec<SocketAddress>,
        pub(crate) ldk_announced_node_name: [u8; 32],
        pub(crate) network: Network,
 }
 
-pub(crate) fn parse_startup_args() -> Result<LdkUserInfo, ()> {
-       if env::args().len() < 3 {
-               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] [announced-node-name announced-listen-addr*]`");
-               return Err(());
-       }
-       let bitcoind_rpc_info = env::args().skip(1).next().unwrap();
-       let bitcoind_rpc_info_parts: Vec<&str> = bitcoind_rpc_info.rsplitn(2, "@").collect();
-       if bitcoind_rpc_info_parts.len() != 2 {
-               println!("ERROR: bad bitcoind RPC URL provided");
-               return Err(());
-       }
-       let rpc_user_and_password: Vec<&str> = bitcoind_rpc_info_parts[1].split(":").collect();
-       if rpc_user_and_password.len() != 2 {
-               println!("ERROR: bad bitcoind RPC username/password combo provided");
-               return Err(());
-       }
-       let bitcoind_rpc_username = rpc_user_and_password[0].to_string();
-       let bitcoind_rpc_password = rpc_user_and_password[1].to_string();
-       let bitcoind_rpc_path: Vec<&str> = bitcoind_rpc_info_parts[0].split(":").collect();
-       if bitcoind_rpc_path.len() != 2 {
-               println!("ERROR: bad bitcoind RPC path provided");
-               return Err(());
-       }
-       let bitcoind_rpc_host = bitcoind_rpc_path[0].to_string();
-       let bitcoind_rpc_port = bitcoind_rpc_path[1].parse::<u16>().unwrap();
-
-       let ldk_storage_dir_path = env::args().skip(2).next().unwrap();
-
-       let mut ldk_peer_port_set = true;
-       let ldk_peer_listening_port: u16 = match env::args().skip(3).next().map(|p| p.parse()) {
-               Some(Ok(p)) => p,
-               Some(Err(_)) => {
-                       ldk_peer_port_set = false;
-                       9735
-               }
-               None => {
-                       ldk_peer_port_set = false;
-                       9735
-               }
-       };
-
-       let mut arg_idx = match ldk_peer_port_set {
-               true => 4,
-               false => 3,
-       };
-       let network: Network = match env::args().skip(arg_idx).next().as_ref().map(String::as_str) {
-               Some("testnet") => Network::Testnet,
-               Some("regtest") => Network::Regtest,
-               Some("signet") => Network::Signet,
-               Some(net) => {
-                       panic!("Unsupported network provided. Options are: `regtest`, `testnet`, and `signet`. Got {}", net);
-               }
-               None => Network::Testnet,
-       };
-
-       let ldk_announced_node_name = match env::args().skip(arg_idx + 1).next().as_ref() {
-               Some(s) => {
-                       if s.len() > 32 {
-                               panic!("Node Alias can not be longer than 32 bytes");
-                       }
-                       arg_idx += 1;
-                       let mut bytes = [0; 32];
-                       bytes[..s.len()].copy_from_slice(s.as_bytes());
-                       bytes
-               }
-               None => [0; 32],
-       };
-
-       let mut ldk_announced_listen_addr = Vec::new();
-       loop {
-               match env::args().skip(arg_idx + 1).next().as_ref() {
-                       Some(s) => match IpAddr::from_str(s) {
-                               Ok(IpAddr::V4(a)) => {
-                                       ldk_announced_listen_addr
-                                               .push(NetAddress::IPv4 { addr: a.octets(), port: ldk_peer_listening_port });
-                                       arg_idx += 1;
-                               }
-                               Ok(IpAddr::V6(a)) => {
-                                       ldk_announced_listen_addr
-                                               .push(NetAddress::IPv6 { addr: a.octets(), port: ldk_peer_listening_port });
-                                       arg_idx += 1;
-                               }
-                               Err(_) => panic!("Failed to parse announced-listen-addr into an IP address"),
-                       },
-                       None => break,
-               }
-       }
-
-       Ok(LdkUserInfo {
-               bitcoind_rpc_username,
-               bitcoind_rpc_password,
-               bitcoind_rpc_host,
-               bitcoind_rpc_port,
-               ldk_storage_dir_path,
-               ldk_peer_listening_port,
-               ldk_announced_listen_addr,
-               ldk_announced_node_name,
-               network,
-       })
-}
-
+#[derive(Debug)]
 struct UserOnionMessageContents {
        tlv_type: u64,
        data: Vec<u8>,
 }
 
-impl CustomOnionMessageContents for UserOnionMessageContents {
+impl OnionMessageContents for UserOnionMessageContents {
        fn tlv_type(&self) -> u64 {
                self.tlv_type
        }
 }
-impl MaybeReadableArgs<u64> for UserOnionMessageContents {
-       fn read<R: std::io::Read>(_r: &mut R, _args: u64) -> Result<Option<Self>, DecodeError> {
-               // UserOnionMessageContents is only ever passed to `send_onion_message`, never to an
-               // `OnionMessageHandler`, thus it does not need to implement the read side here.
-               unreachable!();
-       }
-}
+
 impl Writeable for UserOnionMessageContents {
        fn write<W: Writer>(&self, w: &mut W) -> Result<(), std::io::Error> {
                w.write_all(&self.data)
        }
 }
 
-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>, onion_messenger: Arc<OnionMessenger>,
-       inbound_payments: PaymentInfoStorage, outbound_payments: PaymentInfoStorage,
-       ldk_data_dir: String, network: Network, logger: Arc<disk::FilesystemLogger>,
+pub(crate) fn poll_for_user_input(
+       peer_manager: Arc<PeerManager>, channel_manager: Arc<ChannelManager>,
+       keys_manager: Arc<KeysManager>, network_graph: Arc<NetworkGraph>,
+       onion_messenger: Arc<OnionMessenger>, inbound_payments: Arc<Mutex<InboundPaymentInfoStorage>>,
+       outbound_payments: Arc<Mutex<OutboundPaymentInfoStorage>>, ldk_data_dir: String,
+       network: Network, logger: Arc<disk::FilesystemLogger>, fs_store: Arc<FilesystemStore>,
 ) {
        println!(
                "LDK startup successful. Enter \"help\" to view available commands. Press Ctrl-D to quit."
@@ -197,7 +93,7 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                        let peer_pubkey_and_ip_addr = words.next();
                                        let channel_value_sat = words.next();
                                        if peer_pubkey_and_ip_addr.is_none() || channel_value_sat.is_none() {
-                                               println!("ERROR: openchannel has 2 required arguments: `openchannel pubkey@host:port channel_amt_satoshis` [--public]");
+                                               println!("ERROR: openchannel has 2 required arguments: `openchannel pubkey@host:port channel_amt_satoshis` [--public] [--with-anchors]");
                                                continue;
                                        }
                                        let peer_pubkey_and_ip_addr = peer_pubkey_and_ip_addr.unwrap();
@@ -216,27 +112,36 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                continue;
                                        }
 
-                                       if connect_peer_if_necessary(pubkey, peer_addr, peer_manager.clone())
-                                               .await
+                                       if tokio::runtime::Handle::current()
+                                               .block_on(connect_peer_if_necessary(
+                                                       pubkey,
+                                                       peer_addr,
+                                                       peer_manager.clone(),
+                                               ))
                                                .is_err()
                                        {
                                                continue;
                                        };
 
-                                       let announce_channel = match words.next() {
-                                               Some("--public") | Some("--public=true") => true,
-                                               Some("--public=false") => false,
-                                               Some(_) => {
-                                                       println!("ERROR: invalid `--public` command format. Valid formats: `--public`, `--public=true` `--public=false`");
-                                                       continue;
+                                       let (mut announce_channel, mut with_anchors) = (false, false);
+                                       while let Some(word) = words.next() {
+                                               match word {
+                                                       "--public" | "--public=true" => announce_channel = true,
+                                                       "--public=false" => announce_channel = false,
+                                                       "--with-anchors" | "--with-anchors=true" => with_anchors = true,
+                                                       "--with-anchors=false" => with_anchors = false,
+                                                       _ => {
+                                                               println!("ERROR: invalid boolean flag format. Valid formats: `--option`, `--option=true` `--option=false`");
+                                                               continue;
+                                                       }
                                                }
-                                               None => false,
-                                       };
+                                       }
 
                                        if open_channel(
                                                pubkey,
                                                chan_amt_sat.unwrap(),
                                                announce_channel,
+                                               with_anchors,
                                                channel_manager.clone(),
                                        )
                                        .is_ok()
@@ -255,7 +160,7 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                continue;
                                        }
 
-                                       let invoice = match Invoice::from_str(invoice_str.unwrap()) {
+                                       let invoice = match Bolt11Invoice::from_str(invoice_str.unwrap()) {
                                                Ok(inv) => inv,
                                                Err(e) => {
                                                        println!("ERROR: invalid invoice: {:?}", e);
@@ -263,7 +168,12 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                }
                                        };
 
-                                       send_payment(&*invoice_payer, &invoice, outbound_payments.clone());
+                                       send_payment(
+                                               &channel_manager,
+                                               &invoice,
+                                               &mut outbound_payments.lock().unwrap(),
+                                               Arc::clone(&fs_store),
+                                       );
                                }
                                "keysend" => {
                                        let dest_pubkey = match words.next() {
@@ -294,11 +204,12 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                }
                                        };
                                        keysend(
-                                               &*invoice_payer,
+                                               &channel_manager,
                                                dest_pubkey,
                                                amt_msat,
                                                &*keys_manager,
-                                               outbound_payments.clone(),
+                                               &mut outbound_payments.lock().unwrap(),
+                                               Arc::clone(&fs_store),
                                        );
                                }
                                "getinvoice" => {
@@ -326,15 +237,19 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                continue;
                                        }
 
+                                       let mut inbound_payments = inbound_payments.lock().unwrap();
                                        get_invoice(
                                                amt_msat.unwrap(),
-                                               Arc::clone(&inbound_payments),
-                                               &*channel_manager,
+                                               &mut inbound_payments,
+                                               &channel_manager,
                                                Arc::clone(&keys_manager),
                                                network,
                                                expiry_secs.unwrap(),
                                                Arc::clone(&logger),
                                        );
+                                       fs_store
+                                               .write("", "", INBOUND_PAYMENTS_FNAME, &inbound_payments.encode())
+                                               .unwrap();
                                }
                                "connectpeer" => {
                                        let peer_pubkey_and_ip_addr = words.next();
@@ -350,17 +265,48 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                                continue;
                                                        }
                                                };
-                                       if connect_peer_if_necessary(pubkey, peer_addr, peer_manager.clone())
-                                               .await
+                                       if tokio::runtime::Handle::current()
+                                               .block_on(connect_peer_if_necessary(
+                                                       pubkey,
+                                                       peer_addr,
+                                                       peer_manager.clone(),
+                                               ))
                                                .is_ok()
                                        {
                                                println!("SUCCESS: connected to peer {}", pubkey);
                                        }
                                }
-                               "listchannels" => list_channels(&channel_manager, &network_graph),
-                               "listpayments" => {
-                                       list_payments(inbound_payments.clone(), outbound_payments.clone())
+                               "disconnectpeer" => {
+                                       let peer_pubkey = words.next();
+                                       if peer_pubkey.is_none() {
+                                               println!("ERROR: disconnectpeer requires peer public key: `disconnectpeer <peer_pubkey>`");
+                                               continue;
+                                       }
+
+                                       let peer_pubkey =
+                                               match bitcoin::secp256k1::PublicKey::from_str(peer_pubkey.unwrap()) {
+                                                       Ok(pubkey) => pubkey,
+                                                       Err(e) => {
+                                                               println!("ERROR: {}", e.to_string());
+                                                               continue;
+                                                       }
+                                               };
+
+                                       if do_disconnect_peer(
+                                               peer_pubkey,
+                                               peer_manager.clone(),
+                                               channel_manager.clone(),
+                                       )
+                                       .is_ok()
+                                       {
+                                               println!("SUCCESS: disconnected from peer {}", peer_pubkey);
+                                       }
                                }
+                               "listchannels" => list_channels(&channel_manager, &network_graph),
+                               "listpayments" => list_payments(
+                                       &inbound_payments.lock().unwrap(),
+                                       &outbound_payments.lock().unwrap(),
+                               ),
                                "closechannel" => {
                                        let channel_id_str = words.next();
                                        if channel_id_str.is_none() {
@@ -437,15 +383,15 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                "listpeers" => list_peers(peer_manager.clone()),
                                "signmessage" => {
                                        const MSG_STARTPOS: usize = "signmessage".len() + 1;
-                                       if line.as_bytes().len() <= MSG_STARTPOS {
+                                       if line.trim().as_bytes().len() <= MSG_STARTPOS {
                                                println!("ERROR: signmsg requires a message");
                                                continue;
                                        }
                                        println!(
                                                "{:?}",
                                                lightning::util::message_signing::sign(
-                                                       &line.as_bytes()[MSG_STARTPOS..],
-                                                       &keys_manager.get_node_secret(Recipient::Node).unwrap()
+                                                       &line.trim().as_bytes()[MSG_STARTPOS..],
+                                                       &keys_manager.get_node_secret_key()
                                                )
                                        );
                                }
@@ -457,7 +403,7 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                );
                                                continue;
                                        }
-                                       let mut node_pks = Vec::new();
+                                       let mut intermediate_nodes = 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) {
@@ -476,7 +422,7 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                                break;
                                                        }
                                                };
-                                               node_pks.push(node_pubkey);
+                                               intermediate_nodes.push(node_pubkey);
                                        }
                                        if errored {
                                                continue;
@@ -495,14 +441,15 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                        continue;
                                                }
                                        };
-                                       let destination_pk = node_pks.pop().unwrap();
+                                       let destination = Destination::Node(intermediate_nodes.pop().unwrap());
                                        match onion_messenger.send_onion_message(
-                                               &node_pks,
-                                               Destination::Node(destination_pk),
-                                               OnionMessageContents::Custom(UserOnionMessageContents { tlv_type, data }),
+                                               UserOnionMessageContents { tlv_type, data },
+                                               destination,
                                                None,
                                        ) {
-                                               Ok(()) => println!("SUCCESS: forwarded onion message to first hop"),
+                                               Ok(success) => {
+                                                       println!("SUCCESS: forwarded onion message to first hop {:?}", success)
+                                               }
                                                Err(e) => println!("ERROR: failed to send onion message: {:?}", e),
                                        }
                                }
@@ -524,12 +471,13 @@ fn help() {
        println!("  help\tShows a list of commands.");
        println!("  quit\tClose the application.");
        println!("\n  Channels:");
-       println!("      openchannel pubkey@host:port <amt_satoshis> [--public]");
+       println!("      openchannel pubkey@host:port <amt_satoshis> [--public] [--with-anchors]");
        println!("      closechannel <channel_id> <peer_pubkey>");
        println!("      forceclosechannel <channel_id> <peer_pubkey>");
        println!("      listchannels");
        println!("\n  Peers:");
        println!("      connectpeer pubkey@host:port");
+       println!("      disconnectpeer <peer_pubkey>");
        println!("      listpeers");
        println!("\n  Payments:");
        println!("      sendpayment <invoice>");
@@ -559,7 +507,7 @@ fn node_info(channel_manager: &Arc<ChannelManager>, peer_manager: &Arc<PeerManag
 
 fn list_peers(peer_manager: Arc<PeerManager>) {
        println!("\t{{");
-       for pubkey in peer_manager.get_peer_node_ids() {
+       for (pubkey, _) in peer_manager.get_peer_node_ids() {
                println!("\t\t pubkey: {}", pubkey);
        }
        println!("\t}},");
@@ -570,7 +518,7 @@ fn list_channels(channel_manager: &Arc<ChannelManager>, network_graph: &Arc<Netw
        for chan_info in channel_manager.list_channels() {
                println!("");
                println!("\t{{");
-               println!("\t\tchannel_id: {},", hex_utils::hex_str(&chan_info.channel_id[..]));
+               println!("\t\tchannel_id: {},", chan_info.channel_id);
                if let Some(funding_txo) = chan_info.funding_txo {
                        println!("\t\tfunding_txid: {},", funding_txo.txid);
                }
@@ -594,7 +542,7 @@ fn list_channels(channel_manager: &Arc<ChannelManager>, network_graph: &Arc<Netw
                }
                println!("\t\tis_channel_ready: {},", chan_info.is_channel_ready);
                println!("\t\tchannel_value_satoshis: {},", chan_info.channel_value_satoshis);
-               println!("\t\tlocal_balance_msat: {},", chan_info.balance_msat);
+               println!("\t\toutbound_capacity_msat: {},", chan_info.outbound_capacity_msat);
                if chan_info.is_usable {
                        println!("\t\tavailable_balance_for_send_msat: {},", chan_info.outbound_capacity_msat);
                        println!("\t\tavailable_balance_for_recv_msat: {},", chan_info.inbound_capacity_msat);
@@ -606,15 +554,15 @@ fn list_channels(channel_manager: &Arc<ChannelManager>, network_graph: &Arc<Netw
        println!("]");
 }
 
-fn list_payments(inbound_payments: PaymentInfoStorage, outbound_payments: PaymentInfoStorage) {
-       let inbound = inbound_payments.lock().unwrap();
-       let outbound = outbound_payments.lock().unwrap();
+fn list_payments(
+       inbound_payments: &InboundPaymentInfoStorage, outbound_payments: &OutboundPaymentInfoStorage,
+) {
        print!("[");
-       for (payment_hash, payment_info) in inbound.deref() {
+       for (payment_hash, payment_info) in &inbound_payments.payments {
                println!("");
                println!("\t{{");
                println!("\t\tamount_millisatoshis: {},", payment_info.amt_msat);
-               println!("\t\tpayment_hash: {},", hex_utils::hex_str(&payment_hash.0));
+               println!("\t\tpayment_hash: {},", payment_hash);
                println!("\t\thtlc_direction: inbound,");
                println!(
                        "\t\thtlc_status: {},",
@@ -628,11 +576,11 @@ fn list_payments(inbound_payments: PaymentInfoStorage, outbound_payments: Paymen
                println!("\t}},");
        }
 
-       for (payment_hash, payment_info) in outbound.deref() {
+       for (payment_hash, payment_info) in &outbound_payments.payments {
                println!("");
                println!("\t{{");
                println!("\t\tamount_millisatoshis: {},", payment_info.amt_msat);
-               println!("\t\tpayment_hash: {},", hex_utils::hex_str(&payment_hash.0));
+               println!("\t\tpayment_hash: {},", payment_hash);
                println!("\t\thtlc_direction: outbound,");
                println!(
                        "\t\thtlc_status: {},",
@@ -651,7 +599,7 @@ fn list_payments(inbound_payments: PaymentInfoStorage, outbound_payments: Paymen
 pub(crate) async fn connect_peer_if_necessary(
        pubkey: PublicKey, peer_addr: SocketAddr, peer_manager: Arc<PeerManager>,
 ) -> Result<(), ()> {
-       for node_pubkey in peer_manager.get_peer_node_ids() {
+       for (node_pubkey, _) in peer_manager.get_peer_node_ids() {
                if node_pubkey == pubkey {
                        return Ok(());
                }
@@ -671,16 +619,12 @@ pub(crate) async fn do_connect_peer(
                Some(connection_closed_future) => {
                        let mut connection_closed_future = Box::pin(connection_closed_future);
                        loop {
-                               match futures::poll!(&mut connection_closed_future) {
-                                       std::task::Poll::Ready(_) => {
-                                               return Err(());
-                                       }
-                                       std::task::Poll::Pending => {}
-                               }
-                               // Avoid blocking the tokio context by sleeping a bit
-                               match peer_manager.get_peer_node_ids().iter().find(|id| **id == pubkey) {
-                                       Some(_) => return Ok(()),
-                                       None => tokio::time::sleep(Duration::from_millis(10)).await,
+                               tokio::select! {
+                                       _ = &mut connection_closed_future => return Err(()),
+                                       _ = tokio::time::sleep(Duration::from_millis(10)) => {},
+                               };
+                               if peer_manager.get_peer_node_ids().iter().find(|(id, _)| *id == pubkey).is_some() {
+                                       return Ok(());
                                }
                        }
                }
@@ -688,8 +632,31 @@ pub(crate) async fn do_connect_peer(
        }
 }
 
+fn do_disconnect_peer(
+       pubkey: bitcoin::secp256k1::PublicKey, peer_manager: Arc<PeerManager>,
+       channel_manager: Arc<ChannelManager>,
+) -> Result<(), ()> {
+       //check for open channels with peer
+       for channel in channel_manager.list_channels() {
+               if channel.counterparty.node_id == pubkey {
+                       println!("Error: Node has an active channel with this peer, close any channels first");
+                       return Err(());
+               }
+       }
+
+       //check the pubkey matches a valid connected peer
+       let peers = peer_manager.get_peer_node_ids();
+       if !peers.iter().any(|(pk, _)| &pubkey == pk) {
+               println!("Error: Could not find peer {}", pubkey);
+               return Err(());
+       }
+
+       peer_manager.disconnect_by_node_id(pubkey);
+       Ok(())
+}
+
 fn open_channel(
-       peer_pubkey: PublicKey, channel_amt_sat: u64, announced_channel: bool,
+       peer_pubkey: PublicKey, channel_amt_sat: u64, announced_channel: bool, with_anchors: bool,
        channel_manager: Arc<ChannelManager>,
 ) -> Result<(), ()> {
        let config = UserConfig {
@@ -700,12 +667,13 @@ fn open_channel(
                },
                channel_handshake_config: ChannelHandshakeConfig {
                        announced_channel,
+                       negotiate_anchors_zero_fee_htlc_tx: with_anchors,
                        ..Default::default()
                },
                ..Default::default()
        };
 
-       match channel_manager.create_channel(peer_pubkey, channel_amt_sat, 0, 0, Some(config)) {
+       match channel_manager.create_channel(peer_pubkey, channel_amt_sat, 0, 0, None, Some(config)) {
                Ok(_) => {
                        println!("EVENT: initiated channel with peer {}. ", peer_pubkey);
                        return Ok(());
@@ -717,105 +685,133 @@ fn open_channel(
        }
 }
 
-fn send_payment<E: EventHandler>(
-       invoice_payer: &InvoicePayer<E>, invoice: &Invoice, payment_storage: PaymentInfoStorage,
+fn send_payment(
+       channel_manager: &ChannelManager, invoice: &Bolt11Invoice,
+       outbound_payments: &mut OutboundPaymentInfoStorage, fs_store: Arc<FilesystemStore>,
 ) {
-       let status = match invoice_payer.pay_invoice(invoice) {
-               Ok(_payment_id) => {
-                       let payee_pubkey = invoice.recover_payee_pub_key();
-                       let amt_msat = invoice.amount_milli_satoshis().unwrap();
-                       println!("EVENT: initiated sending {} msats to {}", amt_msat, payee_pubkey);
-                       print!("> ");
-                       HTLCStatus::Pending
-               }
-               Err(PaymentError::Invoice(e)) => {
-                       println!("ERROR: invalid invoice: {}", e);
-                       print!("> ");
-                       return;
-               }
-               Err(PaymentError::Routing(e)) => {
-                       println!("ERROR: failed to find route: {}", e.err);
-                       print!("> ");
-                       return;
-               }
-               Err(PaymentError::Sending(e)) => {
-                       println!("ERROR: failed to send payment: {:?}", e);
-                       print!("> ");
-                       HTLCStatus::Failed
-               }
-       };
-       let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
-       let payment_secret = Some(invoice.payment_secret().clone());
-
-       let mut payments = payment_storage.lock().unwrap();
-       payments.insert(
-               payment_hash,
+       let payment_id = PaymentId((*invoice.payment_hash()).to_byte_array());
+       let payment_hash = PaymentHash((*invoice.payment_hash()).to_byte_array());
+       let payment_secret = Some(*invoice.payment_secret());
+       outbound_payments.payments.insert(
+               payment_id,
                PaymentInfo {
                        preimage: None,
                        secret: payment_secret,
-                       status,
+                       status: HTLCStatus::Pending,
                        amt_msat: MillisatAmount(invoice.amount_milli_satoshis()),
                },
        );
-}
-
-fn keysend<E: EventHandler, K: KeysInterface>(
-       invoice_payer: &InvoicePayer<E>, payee_pubkey: PublicKey, amt_msat: u64, keys: &K,
-       payment_storage: PaymentInfoStorage,
-) {
-       let payment_preimage = keys.get_secure_random_bytes();
-
-       let status = match invoice_payer.pay_pubkey(
-               payee_pubkey,
-               PaymentPreimage(payment_preimage),
-               amt_msat,
-               40,
-       ) {
-               Ok(_payment_id) => {
-                       println!("EVENT: initiated sending {} msats to {}", amt_msat, payee_pubkey);
-                       print!("> ");
-                       HTLCStatus::Pending
+       fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap();
+
+       let mut recipient_onion = RecipientOnionFields::secret_only(*invoice.payment_secret());
+       recipient_onion.payment_metadata = invoice.payment_metadata().map(|v| v.clone());
+       let mut payment_params = match PaymentParameters::from_node_id(
+               invoice.recover_payee_pub_key(),
+               invoice.min_final_cltv_expiry_delta() as u32,
+       )
+       .with_expiry_time(
+               invoice.duration_since_epoch().as_secs().saturating_add(invoice.expiry_time().as_secs()),
+       )
+       .with_route_hints(invoice.route_hints())
+       {
+               Err(e) => {
+                       println!("ERROR: Could not process invoice to prepare payment parameters, {:?}, invoice: {:?}", e, invoice);
+                       return;
                }
-               Err(PaymentError::Invoice(e)) => {
-                       println!("ERROR: invalid payee: {}", e);
-                       print!("> ");
+               Ok(p) => p,
+       };
+       if let Some(features) = invoice.features() {
+               payment_params = match payment_params.with_bolt11_features(features.clone()) {
+                       Err(e) => {
+                               println!("ERROR: Could not build BOLT11 payment parameters! {:?}", e);
+                               return;
+                       }
+                       Ok(p) => p,
+               };
+       }
+       let invoice_amount = match invoice.amount_milli_satoshis() {
+               None => {
+                       println!("ERROR: An invoice with an amount is expected; {:?}", invoice);
                        return;
                }
-               Err(PaymentError::Routing(e)) => {
-                       println!("ERROR: failed to find route: {}", e.err);
+               Some(a) => a,
+       };
+       let route_params =
+               RouteParameters::from_payment_params_and_value(payment_params, invoice_amount);
+
+       match channel_manager.send_payment(
+               payment_hash,
+               recipient_onion,
+               payment_id,
+               route_params,
+               Retry::Timeout(Duration::from_secs(10)),
+       ) {
+               Ok(_) => {
+                       let payee_pubkey = invoice.recover_payee_pub_key();
+                       let amt_msat = invoice.amount_milli_satoshis().unwrap();
+                       println!("EVENT: initiated sending {} msats to {}", amt_msat, payee_pubkey);
                        print!("> ");
-                       return;
                }
-               Err(PaymentError::Sending(e)) => {
+               Err(e) => {
                        println!("ERROR: failed to send payment: {:?}", e);
                        print!("> ");
-                       HTLCStatus::Failed
+                       outbound_payments.payments.get_mut(&payment_id).unwrap().status = HTLCStatus::Failed;
+                       fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap();
                }
        };
+}
+
+fn keysend<E: EntropySource>(
+       channel_manager: &ChannelManager, payee_pubkey: PublicKey, amt_msat: u64, entropy_source: &E,
+       outbound_payments: &mut OutboundPaymentInfoStorage, fs_store: Arc<FilesystemStore>,
+) {
+       let payment_preimage = PaymentPreimage(entropy_source.get_secure_random_bytes());
+       let payment_id = PaymentId(Sha256::hash(&payment_preimage.0[..]).to_byte_array());
 
-       let mut payments = payment_storage.lock().unwrap();
-       payments.insert(
-               PaymentHash(Sha256::hash(&payment_preimage).into_inner()),
+       let route_params = RouteParameters::from_payment_params_and_value(
+               PaymentParameters::for_keysend(payee_pubkey, 40, false),
+               amt_msat,
+       );
+       outbound_payments.payments.insert(
+               payment_id,
                PaymentInfo {
                        preimage: None,
                        secret: None,
-                       status,
+                       status: HTLCStatus::Pending,
                        amt_msat: MillisatAmount(Some(amt_msat)),
                },
        );
+       fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap();
+       match channel_manager.send_spontaneous_payment_with_retry(
+               Some(payment_preimage),
+               RecipientOnionFields::spontaneous_empty(),
+               payment_id,
+               route_params,
+               Retry::Timeout(Duration::from_secs(10)),
+       ) {
+               Ok(_payment_hash) => {
+                       println!("EVENT: initiated sending {} msats to {}", amt_msat, payee_pubkey);
+                       print!("> ");
+               }
+               Err(e) => {
+                       println!("ERROR: failed to send payment: {:?}", e);
+                       print!("> ");
+                       outbound_payments.payments.get_mut(&payment_id).unwrap().status = HTLCStatus::Failed;
+                       fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap();
+               }
+       };
 }
 
 fn get_invoice(
-       amt_msat: u64, payment_storage: PaymentInfoStorage, channel_manager: &ChannelManager,
-       keys_manager: Arc<KeysManager>, network: Network, expiry_secs: u32,
-       logger: Arc<disk::FilesystemLogger>,
+       amt_msat: u64, inbound_payments: &mut InboundPaymentInfoStorage,
+       channel_manager: &ChannelManager, keys_manager: Arc<KeysManager>, network: Network,
+       expiry_secs: u32, logger: Arc<disk::FilesystemLogger>,
 ) {
-       let mut payments = payment_storage.lock().unwrap();
        let currency = match network {
                Network::Bitcoin => Currency::Bitcoin,
-               Network::Testnet => Currency::BitcoinTestnet,
                Network::Regtest => Currency::Regtest,
                Network::Signet => Currency::Signet,
+               Network::Testnet | _ => Currency::BitcoinTestnet,
        };
        let invoice = match utils::create_invoice_from_channelmanager(
                channel_manager,
@@ -825,6 +821,7 @@ fn get_invoice(
                Some(amt_msat),
                "ldk-tutorial-node".to_string(),
                expiry_secs,
+               None,
        ) {
                Ok(inv) => {
                        println!("SUCCESS: generated invoice: {}", inv);
@@ -836,8 +833,8 @@ fn get_invoice(
                }
        };
 
-       let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
-       payments.insert(
+       let payment_hash = PaymentHash(invoice.payment_hash().to_byte_array());
+       inbound_payments.payments.insert(
                payment_hash,
                PaymentInfo {
                        preimage: None,
@@ -851,7 +848,7 @@ fn get_invoice(
 fn close_channel(
        channel_id: [u8; 32], counterparty_node_id: PublicKey, channel_manager: Arc<ChannelManager>,
 ) {
-       match channel_manager.close_channel(&channel_id, &counterparty_node_id) {
+       match channel_manager.close_channel(&ChannelId(channel_id), &counterparty_node_id) {
                Ok(()) => println!("EVENT: initiating channel close"),
                Err(e) => println!("ERROR: failed to close channel: {:?}", e),
        }
@@ -860,7 +857,9 @@ fn close_channel(
 fn force_close_channel(
        channel_id: [u8; 32], counterparty_node_id: PublicKey, channel_manager: Arc<ChannelManager>,
 ) {
-       match channel_manager.force_close_broadcasting_latest_txn(&channel_id, &counterparty_node_id) {
+       match channel_manager
+               .force_close_broadcasting_latest_txn(&ChannelId(channel_id), &counterparty_node_id)
+       {
                Ok(()) => println!("EVENT: initiating channel force-close"),
                Err(e) => println!("ERROR: failed to force-close channel: {:?}", e),
        }