From a841e6b9e1599b6dd7b67ca7f6dc2b85645e64b3 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Thu, 19 Oct 2023 15:50:19 -0500 Subject: [PATCH] Onion message routing to immediate peers. DefaultMessageRouter always fails. Update it so that it can route to a directly connected peer. This is needed for an Offers minimum viable product. --- lightning/src/ln/channelmanager.rs | 23 +++++++++++++++++++++++ lightning/src/onion_message/messenger.rs | 17 ++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 13b0a815e..e71d7124a 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -7325,6 +7325,11 @@ where /// the node must be announced. Otherwise, there is no way to find a path to the introduction /// node in order to send the [`InvoiceRequest`]. /// + /// # Limitations + /// + /// Requires a direct connection to the introduction node in the responding [`InvoiceRequest`]'s + /// reply path. + /// /// [`Offer`]: crate::offers::offer::Offer /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest pub fn create_offer_builder( @@ -7365,6 +7370,11 @@ where /// node must be announced. Otherwise, there is no way to find a path to the introduction node /// in order to send the [`Bolt12Invoice`]. /// + /// # Limitations + /// + /// Requires a direct connection to an introduction node in the responding + /// [`Bolt12Invoice::payment_paths`]. + /// /// # Errors /// /// Errors if a duplicate `payment_id` is provided given the caveats in the aforementioned link @@ -7372,6 +7382,7 @@ where /// /// [`Refund`]: crate::offers::refund::Refund /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice + /// [`Bolt12Invoice::payment_paths`]: crate::offers::invoice::Bolt12Invoice::payment_paths pub fn create_refund_builder( &self, description: String, amount_msats: u64, absolute_expiry: Duration, payment_id: PaymentId, retry_strategy: Retry, max_total_routing_fee_msat: Option @@ -7428,6 +7439,12 @@ where /// node must be announced. Otherwise, there is no way to find a path to the introduction node /// in order to send the [`Bolt12Invoice`]. /// + /// # Limitations + /// + /// Requires a direct connection to an introduction node in [`Offer::paths`] or to + /// [`Offer::signing_pubkey`], if empty. A similar restriction applies to the responding + /// [`Bolt12Invoice::payment_paths`]. + /// /// # Errors /// /// Errors if a duplicate `payment_id` is provided given the caveats in the aforementioned link @@ -7438,6 +7455,7 @@ where /// [`InvoiceRequest::payer_note`]: crate::offers::invoice_request::InvoiceRequest::payer_note /// [`InvoiceRequestBuilder`]: crate::offers::invoice_request::InvoiceRequestBuilder /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice + /// [`Bolt12Invoice::payment_paths`]: crate::offers::invoice::Bolt12Invoice::payment_paths /// [Avoiding Duplicate Payments]: #avoiding-duplicate-payments pub fn pay_for_offer( &self, offer: &Offer, quantity: Option, amount_msats: Option, @@ -7507,6 +7525,11 @@ where /// [`BlindedPath`] containing the [`PaymentSecret`] needed to reconstruct the corresponding /// [`PaymentPreimage`]. /// + /// # Limitations + /// + /// Requires a direct connection to an introduction node in [`Refund::paths`] or to + /// [`Refund::payer_id`], if empty. + /// /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice pub fn request_refund_payment(&self, refund: &Refund) -> Result<(), Bolt12SemanticError> { let expanded_key = &self.inbound_payment_key; diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index ba343ec6e..723e105d0 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -177,14 +177,18 @@ pub trait MessageRouter { ) -> Result; } -/// A [`MessageRouter`] that always fails. +/// A [`MessageRouter`] that can only route to a directly connected [`Destination`]. pub struct DefaultMessageRouter; impl MessageRouter for DefaultMessageRouter { fn find_path( - &self, _sender: PublicKey, _peers: Vec, _destination: Destination + &self, _sender: PublicKey, peers: Vec, destination: Destination ) -> Result { - Err(()) + if peers.contains(&destination.first_node()) { + Ok(OnionMessagePath { intermediate_nodes: vec![], destination }) + } else { + Err(()) + } } } @@ -214,6 +218,13 @@ impl Destination { Destination::BlindedPath(BlindedPath { blinded_hops, .. }) => blinded_hops.len(), } } + + fn first_node(&self) -> PublicKey { + match self { + Destination::Node(node_id) => *node_id, + Destination::BlindedPath(BlindedPath { introduction_node_id: node_id, .. }) => *node_id, + } + } } /// Errors that may occur when [sending an onion message]. -- 2.39.5