From 3e65e625be96fb2a7722ea33636a7a6cb82040fe Mon Sep 17 00:00:00 2001 From: Fred Walker Date: Thu, 2 Mar 2023 14:34:13 -0500 Subject: [PATCH] Move phantom route hint selection into its own function --- lightning-invoice/src/utils.rs | 45 ++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index 90d5c626b..a4b419881 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -200,6 +200,35 @@ where invoice = invoice.amount_milli_satoshis(amt); } + for route_hint in select_phantom_hints(amt_msat, phantom_route_hints, logger) { + invoice = invoice.private_route(route_hint); + } + + let raw_invoice = match invoice.build_raw() { + Ok(inv) => inv, + Err(e) => return Err(SignOrCreationError::CreationError(e)) + }; + let hrp_str = raw_invoice.hrp.to_string(); + let hrp_bytes = hrp_str.as_bytes(); + let data_without_signature = raw_invoice.data.to_base32(); + let signed_raw_invoice = raw_invoice.sign(|_| node_signer.sign_invoice(hrp_bytes, &data_without_signature, Recipient::PhantomNode)); + match signed_raw_invoice { + Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()), + Err(e) => Err(SignOrCreationError::SignError(e)) + } +} + +/// Utility to select route hints for phantom invoices. +/// See [`PhantomKeysManager`] for more information on phantom node payments. +/// +/// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager +fn select_phantom_hints(amt_msat: Option, phantom_route_hints: Vec, + logger: L) -> Vec +where + L::Target: Logger, +{ + let mut phantom_hints: Vec = Vec::new(); + for PhantomRouteHints { channels, phantom_scid, real_node_pubkey } in phantom_route_hints { log_trace!(logger, "Generating phantom route hints for node {}", log_pubkey!(real_node_pubkey)); @@ -223,22 +252,12 @@ where cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA, htlc_minimum_msat: None, htlc_maximum_msat: None,}); - invoice = invoice.private_route(route_hint.clone()); + + phantom_hints.push(route_hint.clone()); } } - let raw_invoice = match invoice.build_raw() { - Ok(inv) => inv, - Err(e) => return Err(SignOrCreationError::CreationError(e)) - }; - let hrp_str = raw_invoice.hrp.to_string(); - let hrp_bytes = hrp_str.as_bytes(); - let data_without_signature = raw_invoice.data.to_base32(); - let signed_raw_invoice = raw_invoice.sign(|_| node_signer.sign_invoice(hrp_bytes, &data_without_signature, Recipient::PhantomNode)); - match signed_raw_invoice { - Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()), - Err(e) => Err(SignOrCreationError::SignError(e)) - } + phantom_hints } #[cfg(feature = "std")] -- 2.39.5