From 932c7898d41c460a35cdad6abc478ee03962dd33 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Fri, 8 Dec 2023 15:54:21 -0600 Subject: [PATCH] WIP: Multi-hop blinded payment paths --- lightning/src/ln/channelmanager.rs | 60 ++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index ccf0543272b..d65da7a5938 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -7652,9 +7652,7 @@ where match self.create_inbound_payment(Some(amount_msats), relative_expiry, None) { Ok((payment_hash, payment_secret)) => { - let payment_paths = vec![ - self.create_one_hop_blinded_payment_path(payment_secret), - ]; + let payment_paths = self.create_blinded_payment_paths(amount_msats, payment_secret); #[cfg(not(feature = "no-std"))] let builder = refund.respond_using_derived_keys( payment_paths, payment_hash, expanded_key, entropy @@ -7831,6 +7829,18 @@ where self.router.create_blinded_paths(recipient, peers, entropy_source, secp_ctx) } + /// Creates multi-hop blinded payment paths for the given `amount_msats` by delegating to + /// [`Router::create_blinded_payment_paths`]. If the router returns an error or no paths, + /// creates a one-hop blinded payment path instead. + fn create_blinded_payment_paths( + &self, amount_msats: u64, payment_secret: PaymentSecret + ) -> Vec<(BlindedPayInfo, BlindedPath)> { + self.create_multi_hop_blinded_payment_paths(amount_msats, payment_secret) + .ok() + .and_then(|paths| (!paths.is_empty()).then(|| paths)) + .unwrap_or_else(|| vec![self.create_one_hop_blinded_payment_path(payment_secret)]) + } + /// Creates a one-hop blinded payment path with [`ChannelManager::get_our_node_id`] as the /// introduction node. fn create_one_hop_blinded_payment_path( @@ -7854,6 +7864,34 @@ where ).unwrap() } + /// Creates multi-hop blinded payment paths for the given `amount_msats` by delegating to + /// [`Router::create_blinded_payment_paths`]. + /// + /// May return no paths if no peers [`support route blinding`] or whose channels don't have + /// enough inbound liquidity. + /// + /// [`support route blinding`]: crate::ln::features::InitFeatures::supports_route_blinding + fn create_multi_hop_blinded_payment_paths( + &self, amount_msats: u64, payment_secret: PaymentSecret + ) -> Result, ()> { + let entropy_source = self.entropy_source.deref(); + let secp_ctx = &self.secp_ctx; + + let first_hops = self.list_usable_channels(); + let payee_node_id = self.get_our_node_id(); + let max_cltv_expiry = self.best_block.read().unwrap().height() + LATENCY_GRACE_PERIOD_BLOCKS; + let payee_tlvs = ReceiveTlvs { + payment_secret, + payment_constraints: PaymentConstraints { + max_cltv_expiry, + htlc_minimum_msat: 1, + }, + }; + self.router.create_blinded_payment_paths( + payee_node_id, first_hops, payee_tlvs, amount_msats, entropy_source, secp_ctx + ) + } + /// Gets a fake short channel id for use in receiving [phantom node payments]. These fake scids /// are used when constructing the phantom invoice's route hints. /// @@ -9091,7 +9129,7 @@ where let amount_msats = match InvoiceBuilder::::amount_msats( &invoice_request ) { - Ok(amount_msats) => Some(amount_msats), + Ok(amount_msats) => amount_msats, Err(error) => return Some(OffersMessage::InvoiceError(error.into())), }; let invoice_request = match invoice_request.verify(expanded_key, secp_ctx) { @@ -9103,11 +9141,11 @@ where }; let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32; - match self.create_inbound_payment(amount_msats, relative_expiry, None) { + match self.create_inbound_payment(Some(amount_msats), relative_expiry, None) { Ok((payment_hash, payment_secret)) if invoice_request.keys.is_some() => { - let payment_paths = vec![ - self.create_one_hop_blinded_payment_path(payment_secret), - ]; + let payment_paths = self.create_blinded_payment_paths( + amount_msats, payment_secret + ); #[cfg(not(feature = "no-std"))] let builder = invoice_request.respond_using_derived_keys( payment_paths, payment_hash @@ -9126,9 +9164,9 @@ where } }, Ok((payment_hash, payment_secret)) => { - let payment_paths = vec![ - self.create_one_hop_blinded_payment_path(payment_secret), - ]; + let payment_paths = self.create_blinded_payment_paths( + amount_msats, payment_secret + ); #[cfg(not(feature = "no-std"))] let builder = invoice_request.respond_with(payment_paths, payment_hash); #[cfg(feature = "no-std")]