Skip to content

Commit c138d62

Browse files
committed
WIP: Multi-hop blinded payment paths
1 parent 0992808 commit c138d62

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

lightning/src/ln/channelmanager.rs

+49-11
Original file line numberDiff line numberDiff line change
@@ -7652,9 +7652,7 @@ where
76527652

76537653
match self.create_inbound_payment(Some(amount_msats), relative_expiry, None) {
76547654
Ok((payment_hash, payment_secret)) => {
7655-
let payment_paths = vec![
7656-
self.create_one_hop_blinded_payment_path(payment_secret),
7657-
];
7655+
let payment_paths = self.create_blinded_payment_paths(amount_msats, payment_secret);
76587656
#[cfg(not(feature = "no-std"))]
76597657
let builder = refund.respond_using_derived_keys(
76607658
payment_paths, payment_hash, expanded_key, entropy
@@ -7831,6 +7829,18 @@ where
78317829
self.router.create_blinded_paths(recipient, peers, entropy_source, secp_ctx)
78327830
}
78337831

7832+
/// Creates multi-hop blinded payment paths for the given `amount_msats` by delegating to
7833+
/// [`Router::create_blinded_payment_paths`]. If the router returns an error or no paths,
7834+
/// creates a one-hop blinded payment path instead.
7835+
fn create_blinded_payment_paths(
7836+
&self, amount_msats: u64, payment_secret: PaymentSecret
7837+
) -> Vec<(BlindedPayInfo, BlindedPath)> {
7838+
self.create_multi_hop_blinded_payment_paths(amount_msats, payment_secret)
7839+
.ok()
7840+
.and_then(|paths| (!paths.is_empty()).then(|| paths))
7841+
.unwrap_or_else(|| vec![self.create_one_hop_blinded_payment_path(payment_secret)])
7842+
}
7843+
78347844
/// Creates a one-hop blinded payment path with [`ChannelManager::get_our_node_id`] as the
78357845
/// introduction node.
78367846
fn create_one_hop_blinded_payment_path(
@@ -7854,6 +7864,34 @@ where
78547864
).unwrap()
78557865
}
78567866

7867+
/// Creates multi-hop blinded payment paths for the given `amount_msats` by delegating to
7868+
/// [`Router::create_blinded_payment_paths`].
7869+
///
7870+
/// May return no paths if no peers [`support route blinding`] or whose channels don't have
7871+
/// enough inbound liquidity.
7872+
///
7873+
/// [`support route blinding`]: crate::ln::features::InitFeatures::supports_route_blinding
7874+
fn create_multi_hop_blinded_payment_paths(
7875+
&self, amount_msats: u64, payment_secret: PaymentSecret
7876+
) -> Result<Vec<(BlindedPayInfo, BlindedPath)>, ()> {
7877+
let entropy_source = self.entropy_source.deref();
7878+
let secp_ctx = &self.secp_ctx;
7879+
7880+
let first_hops = self.list_usable_channels();
7881+
let payee_node_id = self.get_our_node_id();
7882+
let max_cltv_expiry = self.best_block.read().unwrap().height() + LATENCY_GRACE_PERIOD_BLOCKS;
7883+
let payee_tlvs = ReceiveTlvs {
7884+
payment_secret,
7885+
payment_constraints: PaymentConstraints {
7886+
max_cltv_expiry,
7887+
htlc_minimum_msat: 1,
7888+
},
7889+
};
7890+
self.router.create_blinded_payment_paths(
7891+
payee_node_id, first_hops, payee_tlvs, amount_msats, entropy_source, secp_ctx
7892+
)
7893+
}
7894+
78577895
/// Gets a fake short channel id for use in receiving [phantom node payments]. These fake scids
78587896
/// are used when constructing the phantom invoice's route hints.
78597897
///
@@ -9091,7 +9129,7 @@ where
90919129
let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats(
90929130
&invoice_request
90939131
) {
9094-
Ok(amount_msats) => Some(amount_msats),
9132+
Ok(amount_msats) => amount_msats,
90959133
Err(error) => return Some(OffersMessage::InvoiceError(error.into())),
90969134
};
90979135
let invoice_request = match invoice_request.verify(expanded_key, secp_ctx) {
@@ -9103,11 +9141,11 @@ where
91039141
};
91049142
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
91059143

9106-
match self.create_inbound_payment(amount_msats, relative_expiry, None) {
9144+
match self.create_inbound_payment(Some(amount_msats), relative_expiry, None) {
91079145
Ok((payment_hash, payment_secret)) if invoice_request.keys.is_some() => {
9108-
let payment_paths = vec![
9109-
self.create_one_hop_blinded_payment_path(payment_secret),
9110-
];
9146+
let payment_paths = self.create_blinded_payment_paths(
9147+
amount_msats, payment_secret
9148+
);
91119149
#[cfg(not(feature = "no-std"))]
91129150
let builder = invoice_request.respond_using_derived_keys(
91139151
payment_paths, payment_hash
@@ -9126,9 +9164,9 @@ where
91269164
}
91279165
},
91289166
Ok((payment_hash, payment_secret)) => {
9129-
let payment_paths = vec![
9130-
self.create_one_hop_blinded_payment_path(payment_secret),
9131-
];
9167+
let payment_paths = self.create_blinded_payment_paths(
9168+
amount_msats, payment_secret
9169+
);
91329170
#[cfg(not(feature = "no-std"))]
91339171
let builder = invoice_request.respond_with(payment_paths, payment_hash);
91349172
#[cfg(feature = "no-std")]

0 commit comments

Comments
 (0)