Skip to content

Commit

Permalink
Multi-hop blinded payment paths in ChannelManager
Browse files Browse the repository at this point in the history
When constructing blinded payment paths for Bolt12Invoice, delegate to
Router::create_blinded_payment_paths which may produce multi-hop blinded
paths. Fallback to one-hop blinded paths if the Router fails or returns
no paths.
  • Loading branch information
jkczyz committed Dec 14, 2023
1 parent 3619a3b commit fad9dbd
Showing 1 changed file with 49 additions and 11 deletions.
60 changes: 49 additions & 11 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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<Vec<(BlindedPayInfo, BlindedPath)>, ()> {
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.
///
Expand Down Expand Up @@ -9091,7 +9129,7 @@ where
let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::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) {
Expand All @@ -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
Expand All @@ -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")]
Expand Down

0 comments on commit fad9dbd

Please sign in to comment.