X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-invoice%2Fsrc%2Futils.rs;h=e09fd836224b80a339564b94d2239bca29ec9347;hb=e53344663ce58ba0bd04b14f2fce26577de4defb;hp=62820cfe11007027ecc2ec7802a5a4946767e1c6;hpb=aeeafed7d531618edc54effbdeed5e35ee3eaa16;p=rust-lightning diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index 62820cfe..e09fd836 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -1,6 +1,6 @@ //! Convenient utilities to create an invoice. -use {CreationError, Currency, DEFAULT_EXPIRY_TIME, Invoice, InvoiceBuilder, SignOrCreationError}; +use {CreationError, Currency, Invoice, InvoiceBuilder, SignOrCreationError}; use payment::{Payer, Router}; use crate::{prelude::*, Description, InvoiceDescription, Sha256}; @@ -13,13 +13,13 @@ use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret}; use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, PaymentId, PaymentSendFailure, MIN_FINAL_CLTV_EXPIRY}; #[cfg(feature = "std")] use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA}; +use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey}; use lightning::ln::msgs::LightningError; -use lightning::routing::scoring::Score; -use lightning::routing::network_graph::{NetworkGraph, RoutingFees}; +use lightning::routing::gossip::{NetworkGraph, RoutingFees}; use lightning::routing::router::{Route, RouteHint, RouteHintHop, RouteParameters, find_route}; +use lightning::routing::scoring::Score; use lightning::util::logger::Logger; -use secp256k1::key::PublicKey; -use core::convert::TryInto; +use secp256k1::PublicKey; use core::ops::Deref; use core::time::Duration; use sync::Mutex; @@ -38,9 +38,12 @@ use sync::Mutex; /// may be too long for QR code scanning. To fix this, `PhantomRouteHints::channels` may be pared /// down /// -/// `payment_hash` and `payment_secret` come from [`ChannelManager::create_inbound_payment`] or -/// [`ChannelManager::create_inbound_payment_for_hash`]. These values can be retrieved from any -/// participating node. +/// `payment_hash` can be specified if you have a specific need for a custom payment hash (see the difference +/// between [`ChannelManager::create_inbound_payment`] and [`ChannelManager::create_inbound_payment_for_hash`]). +/// If `None` is provided for `payment_hash`, then one will be created. +/// +/// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for +/// in excess of the current time. /// /// Note that the provided `keys_manager`'s `KeysInterface` implementation must support phantom /// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this @@ -48,15 +51,17 @@ use sync::Mutex; /// /// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager /// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints +/// [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment +/// [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash /// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels pub fn create_phantom_invoice( - amt_msat: Option, description: String, payment_hash: PaymentHash, payment_secret: PaymentSecret, + amt_msat: Option, payment_hash: Option, description: String, invoice_expiry_delta_secs: u32, phantom_route_hints: Vec, keys_manager: K, network: Currency, ) -> Result> where K::Target: KeysInterface { let description = Description::new(description).map_err(SignOrCreationError::CreationError)?; let description = InvoiceDescription::Direct(&description,); _create_phantom_invoice::( - amt_msat, description, payment_hash, payment_secret, phantom_route_hints, keys_manager, network, + amt_msat, payment_hash, description, invoice_expiry_delta_secs, phantom_route_hints, keys_manager, network, ) } @@ -76,9 +81,12 @@ pub fn create_phantom_invoice( /// /// `description_hash` is a SHA-256 hash of the description text /// -/// `payment_hash` and `payment_secret` come from [`ChannelManager::create_inbound_payment`] or -/// [`ChannelManager::create_inbound_payment_for_hash`]. These values can be retrieved from any -/// participating node. +/// `payment_hash` can be specified if you have a specific need for a custom payment hash (see the difference +/// between [`ChannelManager::create_inbound_payment`] and [`ChannelManager::create_inbound_payment_for_hash`]). +/// If `None` is provided for `payment_hash`, then one will be created. +/// +/// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for +/// in excess of the current time. /// /// Note that the provided `keys_manager`'s `KeysInterface` implementation must support phantom /// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this @@ -86,28 +94,28 @@ pub fn create_phantom_invoice( /// /// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager /// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints +/// [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment +/// [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash /// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels pub fn create_phantom_invoice_with_description_hash( - amt_msat: Option, description_hash: Sha256, payment_hash: PaymentHash, - payment_secret: PaymentSecret, phantom_route_hints: Vec, - keys_manager: K, network: Currency, + amt_msat: Option, payment_hash: Option, invoice_expiry_delta_secs: u32, + description_hash: Sha256, phantom_route_hints: Vec, keys_manager: K, network: Currency, ) -> Result> where K::Target: KeysInterface { - _create_phantom_invoice::( - amt_msat, - InvoiceDescription::Hash(&description_hash), - payment_hash, payment_secret, phantom_route_hints, keys_manager, network, + amt_msat, payment_hash, InvoiceDescription::Hash(&description_hash), + invoice_expiry_delta_secs, phantom_route_hints, keys_manager, network, ) } #[cfg(feature = "std")] fn _create_phantom_invoice( - amt_msat: Option, description: InvoiceDescription, payment_hash: PaymentHash, - payment_secret: PaymentSecret, phantom_route_hints: Vec, - keys_manager: K, network: Currency, + amt_msat: Option, payment_hash: Option, description: InvoiceDescription, + invoice_expiry_delta_secs: u32, phantom_route_hints: Vec, keys_manager: K, network: Currency, ) -> Result> where K::Target: KeysInterface { + use std::time::{SystemTime, UNIX_EPOCH}; + if phantom_route_hints.len() == 0 { return Err(SignOrCreationError::CreationError( CreationError::MissingRouteHints, @@ -120,11 +128,41 @@ fn _create_phantom_invoice( InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0), }; + // If we ever see performance here being too slow then we should probably take this ExpandedKey as a parameter instead. + let keys = ExpandedKey::new(&keys_manager.get_inbound_payment_key_material()); + let (payment_hash, payment_secret) = if let Some(payment_hash) = payment_hash { + let payment_secret = create_from_hash( + &keys, + amt_msat, + payment_hash, + invoice_expiry_delta_secs, + SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Time must be > 1970") + .as_secs(), + ) + .map_err(|_| SignOrCreationError::CreationError(CreationError::InvalidAmount))?; + (payment_hash, payment_secret) + } else { + create( + &keys, + amt_msat, + invoice_expiry_delta_secs, + &keys_manager, + SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Time must be > 1970") + .as_secs(), + ) + .map_err(|_| SignOrCreationError::CreationError(CreationError::InvalidAmount))? + }; + let mut invoice = invoice .current_timestamp() .payment_hash(Hash::from_slice(&payment_hash.0).unwrap()) .payment_secret(payment_secret) - .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into()); + .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into()) + .expiry_time(Duration::from_secs(invoice_expiry_delta_secs.into())); if let Some(amt) = amt_msat { invoice = invoice.amount_milli_satoshis(amt); } @@ -174,9 +212,12 @@ fn _create_phantom_invoice( /// method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user /// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify /// that the payment secret is valid when the invoice is paid. +/// +/// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for +/// in excess of the current time. pub fn create_invoice_from_channelmanager( channelmanager: &ChannelManager, keys_manager: K, network: Currency, - amt_msat: Option, description: String + amt_msat: Option, description: String, invoice_expiry_delta_secs: u32 ) -> Result> where M::Target: chain::Watch, @@ -189,7 +230,8 @@ where let duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) .expect("for the foreseeable future this shouldn't happen"); create_invoice_from_channelmanager_and_duration_since_epoch( - channelmanager, keys_manager, network, amt_msat, description, duration + channelmanager, keys_manager, network, amt_msat, description, duration, + invoice_expiry_delta_secs ) } @@ -200,9 +242,12 @@ where /// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify /// that the payment secret is valid when the invoice is paid. /// Use this variant if you want to pass the `description_hash` to the invoice. +/// +/// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for +/// in excess of the current time. pub fn create_invoice_from_channelmanager_with_description_hash( channelmanager: &ChannelManager, keys_manager: K, network: Currency, - amt_msat: Option, description_hash: Sha256, + amt_msat: Option, description_hash: Sha256, invoice_expiry_delta_secs: u32 ) -> Result> where M::Target: chain::Watch, @@ -218,7 +263,8 @@ where .expect("for the foreseeable future this shouldn't happen"); create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch( - channelmanager, keys_manager, network, amt_msat, description_hash, duration, + channelmanager, keys_manager, network, amt_msat, + description_hash, duration, invoice_expiry_delta_secs ) } @@ -228,6 +274,7 @@ where pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch( channelmanager: &ChannelManager, keys_manager: K, network: Currency, amt_msat: Option, description_hash: Sha256, duration_since_epoch: Duration, + invoice_expiry_delta_secs: u32 ) -> Result> where M::Target: chain::Watch, @@ -239,7 +286,7 @@ where _create_invoice_from_channelmanager_and_duration_since_epoch( channelmanager, keys_manager, network, amt_msat, InvoiceDescription::Hash(&description_hash), - duration_since_epoch, + duration_since_epoch, invoice_expiry_delta_secs ) } @@ -249,6 +296,7 @@ where pub fn create_invoice_from_channelmanager_and_duration_since_epoch( channelmanager: &ChannelManager, keys_manager: K, network: Currency, amt_msat: Option, description: String, duration_since_epoch: Duration, + invoice_expiry_delta_secs: u32 ) -> Result> where M::Target: chain::Watch, @@ -262,13 +310,14 @@ where InvoiceDescription::Direct( &Description::new(description).map_err(SignOrCreationError::CreationError)?, ), - duration_since_epoch, + duration_since_epoch, invoice_expiry_delta_secs ) } fn _create_invoice_from_channelmanager_and_duration_since_epoch( channelmanager: &ChannelManager, keys_manager: K, network: Currency, amt_msat: Option, description: InvoiceDescription, duration_since_epoch: Duration, + invoice_expiry_delta_secs: u32 ) -> Result> where M::Target: chain::Watch, @@ -282,7 +331,7 @@ where // `create_inbound_payment` only returns an error if the amount is greater than the total bitcoin // supply. let (payment_hash, payment_secret) = channelmanager - .create_inbound_payment(amt_msat, DEFAULT_EXPIRY_TIME.try_into().unwrap()) + .create_inbound_payment(amt_msat, invoice_expiry_delta_secs) .map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?; let our_node_pubkey = channelmanager.get_our_node_id(); @@ -299,7 +348,8 @@ where .payment_hash(Hash::from_slice(&payment_hash.0).unwrap()) .payment_secret(payment_secret) .basic_mpp() - .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into()); + .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into()) + .expiry_time(Duration::from_secs(invoice_expiry_delta_secs.into())); if let Some(amt) = amt_msat { invoice = invoice.amount_milli_satoshis(amt); } @@ -374,8 +424,8 @@ fn filter_channels(channels: Vec, min_inbound_capacity_msat: Opt proportional_millionths: forwarding_info.fee_proportional_millionths, }, cltv_expiry_delta: forwarding_info.cltv_expiry_delta, - htlc_minimum_msat: None, - htlc_maximum_msat: None,}]) + htlc_minimum_msat: channel.inbound_htlc_minimum_msat, + htlc_maximum_msat: channel.inbound_htlc_maximum_msat,}]) }; // If all channels are private, return the route hint for the highest inbound capacity channel // per counterparty node. If channels with an higher inbound capacity than the @@ -390,13 +440,13 @@ fn filter_channels(channels: Vec, min_inbound_capacity_msat: Opt } /// A [`Router`] implemented using [`find_route`]. -pub struct DefaultRouter, L: Deref> where L::Target: Logger { +pub struct DefaultRouter>, L: Deref> where L::Target: Logger { network_graph: G, logger: L, random_seed_bytes: Mutex<[u8; 32]>, } -impl, L: Deref> DefaultRouter where L::Target: Logger { +impl>, L: Deref> DefaultRouter where L::Target: Logger { /// Creates a new router using the given [`NetworkGraph`], a [`Logger`], and a randomness source /// `random_seed_bytes`. pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32]) -> Self { @@ -405,18 +455,19 @@ impl, L: Deref> DefaultRouter where L::Tar } } -impl, L: Deref, S: Score> Router for DefaultRouter +impl>, L: Deref, S: Score> Router for DefaultRouter where L::Target: Logger { fn find_route( &self, payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash, first_hops: Option<&[&ChannelDetails]>, scorer: &S ) -> Result { + let network_graph = self.network_graph.read_only(); let random_seed_bytes = { let mut locked_random_seed_bytes = self.random_seed_bytes.lock().unwrap(); *locked_random_seed_bytes = sha256::Hash::hash(&*locked_random_seed_bytes).into_inner(); *locked_random_seed_bytes }; - find_route(payer, params, &*self.network_graph, first_hops, &*self.logger, scorer, &random_seed_bytes) + find_route(payer, params, &network_graph, first_hops, &*self.logger, scorer, &random_seed_bytes) } } @@ -488,19 +539,24 @@ mod test { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let nodes = create_network(2, &node_cfgs, &node_chanmgrs); create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known()); + let non_default_invoice_expiry_secs = 4200; let invoice = create_invoice_from_channelmanager_and_duration_since_epoch( &nodes[1].node, nodes[1].keys_manager, Currency::BitcoinTestnet, Some(10_000), "test".to_string(), - Duration::from_secs(1234567)).unwrap(); + Duration::from_secs(1234567), non_default_invoice_expiry_secs).unwrap(); assert_eq!(invoice.amount_pico_btc(), Some(100_000)); assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64); assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string()))); + assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into())); // Invoice SCIDs should always use inbound SCID aliases over the real channel ID, if one is // available. + let chan = &nodes[1].node.list_usable_channels()[0]; assert_eq!(invoice.route_hints().len(), 1); assert_eq!(invoice.route_hints()[0].0.len(), 1); - assert_eq!(invoice.route_hints()[0].0[0].short_channel_id, - nodes[1].node.list_usable_channels()[0].inbound_scid_alias.unwrap()); + assert_eq!(invoice.route_hints()[0].0[0].short_channel_id, chan.inbound_scid_alias.unwrap()); + + assert_eq!(invoice.route_hints()[0].0[0].htlc_minimum_msat, chan.inbound_htlc_minimum_msat); + assert_eq!(invoice.route_hints()[0].0[0].htlc_maximum_msat, chan.inbound_htlc_maximum_msat); let payment_params = PaymentParameters::from_node_id(invoice.recover_payee_pub_key()) .with_features(invoice.features().unwrap().clone()) @@ -511,12 +567,12 @@ mod test { final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32, }; let first_hops = nodes[0].node.list_usable_channels(); - let network_graph = node_cfgs[0].network_graph; + let network_graph = &node_cfgs[0].network_graph; let logger = test_utils::TestLogger::new(); let scorer = test_utils::TestScorer::with_penalty(0); let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes(); let route = find_route( - &nodes[0].node.get_our_node_id(), &route_params, network_graph, + &nodes[0].node.get_our_node_id(), &route_params, &network_graph.read_only(), Some(&first_hops.iter().collect::>()), &logger, &scorer, &random_seed_bytes ).unwrap(); @@ -551,7 +607,7 @@ mod test { let description_hash = crate::Sha256(Hash::hash("Testing description_hash".as_bytes())); let invoice = ::utils::create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch( &nodes[1].node, nodes[1].keys_manager, Currency::BitcoinTestnet, Some(10_000), - description_hash, Duration::from_secs(1234567), + description_hash, Duration::from_secs(1234567), 3600 ).unwrap(); assert_eq!(invoice.amount_pico_btc(), Some(100_000)); assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64); @@ -603,7 +659,7 @@ mod test { // `msgs::ChannelUpdate` is never handled for the node(s). As the `msgs::ChannelUpdate` // is never handled, the `channel.counterparty.forwarding_info` is never assigned. let mut private_chan_cfg = UserConfig::default(); - private_chan_cfg.channel_options.announced_channel = false; + private_chan_cfg.channel_handshake_config.announced_channel = false; let temporary_channel_id = nodes[2].node.create_channel(nodes[0].node.get_our_node_id(), 1_000_000, 500_000_000, 42, Some(private_chan_cfg)).unwrap(); let open_channel = get_event_msg!(nodes[2], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id()); nodes[0].node.handle_open_channel(&nodes[2].node.get_our_node_id(), InitFeatures::known(), &open_channel); @@ -617,10 +673,10 @@ mod test { connect_blocks(&nodes[2], CHAN_CONFIRM_DEPTH - 1); confirm_transaction_at(&nodes[0], &tx, conf_height); connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1); - let as_funding_locked = get_event_msg!(nodes[2], MessageSendEvent::SendFundingLocked, nodes[0].node.get_our_node_id()); - nodes[2].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[2].node.get_our_node_id())); + let as_channel_ready = get_event_msg!(nodes[2], MessageSendEvent::SendChannelReady, nodes[0].node.get_our_node_id()); + nodes[2].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[2].node.get_our_node_id())); get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id()); - nodes[0].node.handle_funding_locked(&nodes[2].node.get_our_node_id(), &as_funding_locked); + nodes[0].node.handle_channel_ready(&nodes[2].node.get_our_node_id(), &as_channel_ready); get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id()); // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the second @@ -711,7 +767,7 @@ mod test { ) { let invoice = create_invoice_from_channelmanager_and_duration_since_epoch( &invoice_node.node, invoice_node.keys_manager, Currency::BitcoinTestnet, invoice_amt, "test".to_string(), - Duration::from_secs(1234567)).unwrap(); + Duration::from_secs(1234567), 3600).unwrap(); let hints = invoice.private_routes(); for hint in hints { @@ -747,27 +803,35 @@ mod test { nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_0_2.0); let payment_amt = 10_000; - let (payment_preimage, payment_hash, payment_secret) = { - if user_generated_pmt_hash { - let payment_preimage = PaymentPreimage([1; 32]); - let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner()); - let payment_secret = nodes[1].node.create_inbound_payment_for_hash(payment_hash, Some(payment_amt), 3600).unwrap(); - (payment_preimage, payment_hash, payment_secret) - } else { - let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(payment_amt), 3600).unwrap(); - let payment_preimage = nodes[1].node.get_payment_preimage(payment_hash, payment_secret).unwrap(); - (payment_preimage, payment_hash, payment_secret) - } - }; let route_hints = vec![ nodes[1].node.get_phantom_route_hints(), nodes[2].node.get_phantom_route_hints(), ]; - let invoice = ::utils::create_phantom_invoice::(Some(payment_amt), "test".to_string(), payment_hash, payment_secret, route_hints, &nodes[1].keys_manager, Currency::BitcoinTestnet).unwrap(); + + let user_payment_preimage = PaymentPreimage([1; 32]); + let payment_hash = if user_generated_pmt_hash { + Some(PaymentHash(Sha256::hash(&user_payment_preimage.0[..]).into_inner())) + } else { + None + }; + let non_default_invoice_expiry_secs = 4200; + + let invoice = + ::utils::create_phantom_invoice::( + Some(payment_amt), payment_hash, "test".to_string(), non_default_invoice_expiry_secs, + route_hints, &nodes[1].keys_manager, Currency::BitcoinTestnet + ).unwrap(); + let (payment_hash, payment_secret) = (PaymentHash(invoice.payment_hash().into_inner()), *invoice.payment_secret()); + let payment_preimage = if user_generated_pmt_hash { + user_payment_preimage + } else { + nodes[1].node.get_payment_preimage(payment_hash, payment_secret).unwrap() + }; assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64); assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string()))); assert_eq!(invoice.route_hints().len(), 2); + assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into())); assert!(!invoice.features().unwrap().supports_basic_mpp()); let payment_params = PaymentParameters::from_node_id(invoice.recover_payee_pub_key()) @@ -779,12 +843,12 @@ mod test { final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32, }; let first_hops = nodes[0].node.list_usable_channels(); - let network_graph = node_cfgs[0].network_graph; + let network_graph = &node_cfgs[0].network_graph; let logger = test_utils::TestLogger::new(); let scorer = test_utils::TestScorer::with_penalty(0); let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes(); let route = find_route( - &nodes[0].node.get_our_node_id(), ¶ms, network_graph, + &nodes[0].node.get_our_node_id(), ¶ms, &network_graph.read_only(), Some(&first_hops.iter().collect::>()), &logger, &scorer, &random_seed_bytes ).unwrap(); let (payment_event, fwd_idx) = { @@ -839,6 +903,40 @@ mod test { } } + #[test] + #[cfg(feature = "std")] + fn test_multi_node_hints_has_htlc_min_max_values() { + let mut chanmon_cfgs = create_chanmon_cfgs(3); + let seed_1 = [42 as u8; 32]; + let seed_2 = [43 as u8; 32]; + let cross_node_seed = [44 as u8; 32]; + chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed); + chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed); + let node_cfgs = create_node_cfgs(3, &chanmon_cfgs); + let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]); + let nodes = create_network(3, &node_cfgs, &node_chanmgrs); + + create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known()); + create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known()); + + let payment_amt = 20_000; + let (payment_hash, _payment_secret) = nodes[1].node.create_inbound_payment(Some(payment_amt), 3600).unwrap(); + let route_hints = vec![ + nodes[1].node.get_phantom_route_hints(), + nodes[2].node.get_phantom_route_hints(), + ]; + + let invoice = ::utils::create_phantom_invoice::(Some(payment_amt), Some(payment_hash), "test".to_string(), 3600, route_hints, &nodes[1].keys_manager, Currency::BitcoinTestnet).unwrap(); + + let chan_0_1 = &nodes[1].node.list_usable_channels()[0]; + assert_eq!(invoice.route_hints()[0].0[0].htlc_minimum_msat, chan_0_1.inbound_htlc_minimum_msat); + assert_eq!(invoice.route_hints()[0].0[0].htlc_maximum_msat, chan_0_1.inbound_htlc_maximum_msat); + + let chan_0_2 = &nodes[2].node.list_usable_channels()[0]; + assert_eq!(invoice.route_hints()[1].0[0].htlc_minimum_msat, chan_0_2.inbound_htlc_minimum_msat); + assert_eq!(invoice.route_hints()[1].0[0].htlc_maximum_msat, chan_0_2.inbound_htlc_maximum_msat); + } + #[test] #[cfg(feature = "std")] fn create_phantom_invoice_with_description_hash() { @@ -848,17 +946,23 @@ mod test { let nodes = create_network(3, &node_cfgs, &node_chanmgrs); let payment_amt = 20_000; - let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(payment_amt), 3600).unwrap(); let route_hints = vec![ nodes[1].node.get_phantom_route_hints(), nodes[2].node.get_phantom_route_hints(), ]; let description_hash = crate::Sha256(Hash::hash("Description hash phantom invoice".as_bytes())); - let invoice = ::utils::create_phantom_invoice_with_description_hash::(Some(payment_amt), description_hash, payment_hash, payment_secret, route_hints, &nodes[1].keys_manager, Currency::BitcoinTestnet).unwrap(); - + let non_default_invoice_expiry_secs = 4200; + let invoice = ::utils::create_phantom_invoice_with_description_hash::< + EnforcingSigner, &test_utils::TestKeysInterface, + >( + Some(payment_amt), None, non_default_invoice_expiry_secs, description_hash, + route_hints, &nodes[1].keys_manager, Currency::BitcoinTestnet + ) + .unwrap(); assert_eq!(invoice.amount_pico_btc(), Some(200_000)); assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64); + assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into())); assert_eq!(invoice.description(), InvoiceDescription::Hash(&crate::Sha256(Sha256::hash("Description hash phantom invoice".as_bytes())))); } @@ -942,7 +1046,7 @@ mod test { // `msgs::ChannelUpdate` is never handled for the node(s). As the `msgs::ChannelUpdate` // is never handled, the `channel.counterparty.forwarding_info` is never assigned. let mut private_chan_cfg = UserConfig::default(); - private_chan_cfg.channel_options.announced_channel = false; + private_chan_cfg.channel_handshake_config.announced_channel = false; let temporary_channel_id = nodes[1].node.create_channel(nodes[3].node.get_our_node_id(), 1_000_000, 500_000_000, 42, Some(private_chan_cfg)).unwrap(); let open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[3].node.get_our_node_id()); nodes[3].node.handle_open_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &open_channel); @@ -956,10 +1060,10 @@ mod test { connect_blocks(&nodes[1], CHAN_CONFIRM_DEPTH - 1); confirm_transaction_at(&nodes[3], &tx, conf_height); connect_blocks(&nodes[3], CHAN_CONFIRM_DEPTH - 1); - let as_funding_locked = get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[3].node.get_our_node_id()); - nodes[1].node.handle_funding_locked(&nodes[3].node.get_our_node_id(), &get_event_msg!(nodes[3], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id())); + let as_channel_ready = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, nodes[3].node.get_our_node_id()); + nodes[1].node.handle_channel_ready(&nodes[3].node.get_our_node_id(), &get_event_msg!(nodes[3], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id())); get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[3].node.get_our_node_id()); - nodes[3].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &as_funding_locked); + nodes[3].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &as_channel_ready); get_event_msg!(nodes[3], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id()); // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the third @@ -1158,7 +1262,6 @@ mod test { mut chan_ids_to_match: HashSet, nodes_contains_public_channels: bool ){ - let (payment_hash, payment_secret) = invoice_node.node.create_inbound_payment(invoice_amt, 3600).unwrap(); let phantom_route_hints = network_multi_nodes.iter() .map(|node| node.node.get_phantom_route_hints()) .collect::>(); @@ -1166,7 +1269,7 @@ mod test { .map(|route_hint| route_hint.phantom_scid) .collect::>(); - let invoice = ::utils::create_phantom_invoice::(invoice_amt, "test".to_string(), payment_hash, payment_secret, phantom_route_hints, &invoice_node.keys_manager, Currency::BitcoinTestnet).unwrap(); + let invoice = ::utils::create_phantom_invoice::(invoice_amt, None, "test".to_string(), 3600, phantom_route_hints, &invoice_node.keys_manager, Currency::BitcoinTestnet).unwrap(); let invoice_hints = invoice.private_routes();