Require payment secrets when building and reading invoices
[rust-lightning] / lightning-invoice / src / utils.rs
index 3b97a8bb81a8c97d7058cf2027b8a06620cbe453..df2bbfd8f12459381d3296dc44499cffa2030d07 100644 (file)
@@ -1,5 +1,5 @@
 //! Convenient utilities to create an invoice.
-use {Currency, Invoice, InvoiceBuilder, SignOrCreationError, RawInvoice};
+use {Currency, DEFAULT_EXPIRY_TIME, Invoice, InvoiceBuilder, SignOrCreationError, RawInvoice};
 use bech32::ToBase32;
 use bitcoin_hashes::Hash;
 use lightning::chain;
@@ -7,8 +7,9 @@ use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
 use lightning::chain::keysinterface::{Sign, KeysInterface};
 use lightning::ln::channelmanager::{ChannelManager, MIN_FINAL_CLTV_EXPIRY};
 use lightning::routing::network_graph::RoutingFees;
-use lightning::routing::router::RouteHintHop;
+use lightning::routing::router::{RouteHint, RouteHintHop};
 use lightning::util::logger::Logger;
+use std::convert::TryInto;
 use std::ops::Deref;
 
 /// Utility to construct an invoice. Generally, unless you want to do something like a custom
@@ -35,12 +36,12 @@ where
                        Some(id) => id,
                        None => continue,
                };
-               let forwarding_info = match channel.counterparty_forwarding_info {
+               let forwarding_info = match channel.counterparty.forwarding_info {
                        Some(info) => info,
                        None => continue,
                };
-               route_hints.push(vec![RouteHintHop {
-                       src_node_id: channel.remote_network_id,
+               route_hints.push(RouteHint(vec![RouteHintHop {
+                       src_node_id: channel.counterparty.node_id,
                        short_channel_id,
                        fees: RoutingFees {
                                base_msat: forwarding_info.fee_base_msat,
@@ -49,12 +50,12 @@ where
                        cltv_expiry_delta: forwarding_info.cltv_expiry_delta,
                        htlc_minimum_msat: None,
                        htlc_maximum_msat: None,
-               }]);
+               }]));
        }
 
        let (payment_hash, payment_secret) = channelmanager.create_inbound_payment(
                amt_msat,
-               7200, // default invoice expiry is 2 hours
+               DEFAULT_EXPIRY_TIME.try_into().unwrap(),
                0,
        );
        let our_node_pubkey = channelmanager.get_our_node_id();
@@ -67,10 +68,10 @@ where
                .basic_mpp()
                .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into());
        if let Some(amt) = amt_msat {
-               invoice = invoice.amount_pico_btc(amt * 10);
+               invoice = invoice.amount_milli_satoshis(amt);
        }
-       for hint in route_hints.drain(..) {
-               invoice = invoice.route(hint);
+       for hint in route_hints {
+               invoice = invoice.private_route(hint);
        }
 
        let raw_invoice = match invoice.build_raw() {
@@ -111,14 +112,9 @@ mod test {
                assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
                assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string())));
 
-               let mut route_hints = invoice.routes().clone();
-               let mut last_hops = Vec::new();
-               for hint in route_hints.drain(..) {
-                       last_hops.push(hint[hint.len() - 1].clone());
-               }
                let amt_msat = invoice.amount_pico_btc().unwrap() / 10;
-
                let first_hops = nodes[0].node.list_usable_channels();
+               let last_hops = invoice.route_hints();
                let network_graph = nodes[0].net_graph_msg_handler.network_graph.read().unwrap();
                let logger = test_utils::TestLogger::new();
                let route = router::get_route(
@@ -127,7 +123,7 @@ mod test {
                        &invoice.recover_payee_pub_key(),
                        Some(invoice.features().unwrap().clone()),
                        Some(&first_hops.iter().collect::<Vec<_>>()),
-                       &last_hops.iter().collect::<Vec<_>>(),
+                       &last_hops,
                        amt_msat,
                        invoice.min_final_cltv_expiry() as u32,
                        &logger,
@@ -136,7 +132,7 @@ mod test {
                let payment_event = {
                        let mut payment_hash = PaymentHash([0; 32]);
                        payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
-                       nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().unwrap().clone())).unwrap();
+                       nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone())).unwrap();
                        let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
                        assert_eq!(added_monitors.len(), 1);
                        added_monitors.clear();