Skip to content

Commit bdcdba5

Browse files
committed
WIP: Include blinded paths
1 parent c73c111 commit bdcdba5

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

lightning/src/blinded_path/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub(crate) mod utils;
1515

1616
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
1717

18+
use core::ops::Deref;
1819
use crate::ln::msgs::DecodeError;
1920
use crate::offers::invoice::BlindedPayInfo;
2021
use crate::sign::EntropySource;
@@ -61,9 +62,9 @@ impl BlindedPath {
6162
///
6263
/// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
6364
// TODO: make all payloads the same size with padding + add dummy hops
64-
pub fn new_for_message<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification>
65-
(node_pks: &[PublicKey], entropy_source: &ES, secp_ctx: &Secp256k1<T>) -> Result<Self, ()>
66-
{
65+
pub fn new_for_message<ES: Deref, T: secp256k1::Signing + secp256k1::Verification>(
66+
node_pks: &[PublicKey], entropy_source: ES, secp_ctx: &Secp256k1<T>
67+
) -> Result<Self, ()> where ES::Target: EntropySource {
6768
if node_pks.len() < 2 { return Err(()) }
6869
let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
6970
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");

lightning/src/ln/channelmanager.rs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use bitcoin::secp256k1::{SecretKey,PublicKey};
3030
use bitcoin::secp256k1::Secp256k1;
3131
use bitcoin::{LockTime, secp256k1, Sequence};
3232

33+
use crate::blinded_path::BlindedPath;
3334
use crate::chain;
3435
use crate::chain::{Confirm, ChannelMonitorUpdateStatus, Watch, BestBlock};
3536
use crate::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator, LowerBoundedFeeEstimator};
@@ -6708,6 +6709,10 @@ where
67086709
/// Creates an [`OfferBuilder`] such that the [`Offer`] it builds is recognized by the
67096710
/// [`ChannelManager`] when handling [`InvoiceRequest`] messages for the offer.
67106711
///
6712+
/// Uses [`Router::find_partial_paths`] to construct a [`BlindedPath`] for the offer. If one is
6713+
/// found, also uses a derived signing pubkey for recipient privacy. Otherwise, uses the node id
6714+
/// as the signing pubkey.
6715+
///
67116716
/// [`Offer`]: crate::offers::offer::Offer
67126717
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
67136718
pub fn create_offer_builder(
@@ -6717,16 +6722,26 @@ where
67176722
let expanded_key = &self.inbound_payment_key;
67186723
let entropy = &*self.entropy_source;
67196724
let secp_ctx = &self.secp_ctx;
6725+
let builder = OfferBuilder::deriving_signing_pubkey(
6726+
description, node_id, expanded_key, entropy, secp_ctx
6727+
);
67206728

6721-
// TODO: Set blinded paths
6722-
OfferBuilder::deriving_signing_pubkey(description, node_id, expanded_key, entropy, secp_ctx)
6729+
match self.create_blinded_paths(1) {
6730+
Ok(paths) if !paths.is_empty() => builder.path(paths.into_iter().next().unwrap()),
6731+
// TODO: check if node is public?
6732+
Ok(_) | Err(_) => builder,
6733+
}
67236734
}
67246735

67256736
/// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
67266737
/// [`ChannelManager`] when handling [`Bolt12Invoice`] messages for the refund.
67276738
///
67286739
/// The provided `payment_id` is used to ensure that only one invoice is paid for the refund.
67296740
///
6741+
/// Uses [`Router::find_partial_paths`] to construct a [`BlindedPath`] for the refund. If one is
6742+
/// found, also uses a derived payer id for sender privacy. Otherwise, uses the node id as the
6743+
/// payer id.
6744+
///
67306745
/// [`Refund`]: crate::offers::refund::Refund
67316746
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
67326747
pub fn create_refund_builder(
@@ -6737,10 +6752,14 @@ where
67376752
let entropy = &*self.entropy_source;
67386753
let secp_ctx = &self.secp_ctx;
67396754

6740-
// TODO: Set blinded paths
67416755
let builder = RefundBuilder::deriving_payer_id(
67426756
description, node_id, expanded_key, entropy, secp_ctx, amount_msats, payment_id
67436757
)?;
6758+
let builder = match self.create_blinded_paths(1) {
6759+
Ok(paths) if !paths.is_empty() => builder.path(paths.into_iter().next().unwrap()),
6760+
// TODO: check if node is public?
6761+
Ok(_) | Err(_) => builder,
6762+
};
67446763
self.pending_outbound_payments
67456764
.add_new_awaiting_invoice(payment_id, retry_strategy)
67466765
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
@@ -6873,6 +6892,23 @@ where
68736892
inbound_payment::get_payment_preimage(payment_hash, payment_secret, &self.inbound_payment_key)
68746893
}
68756894

6895+
///
6896+
fn create_blinded_paths(&self, count: usize) -> Result<Vec<BlindedPath>, ()> {
6897+
let last_hops = self.per_peer_state.read().unwrap().iter()
6898+
.filter(|(_, peer)| peer.lock().unwrap().latest_features.supports_route_blinding())
6899+
.map(|(node_id, _)| *node_id)
6900+
.collect::<Vec<_>>();
6901+
let entropy_source = self.entropy_source.deref();
6902+
let secp_ctx = &self.secp_ctx;
6903+
6904+
self.router
6905+
.find_partial_paths(self.get_our_node_id(), last_hops.as_slice())?
6906+
.into_iter()
6907+
.map(|node_pks| BlindedPath::new_for_message(&node_pks[..], entropy_source, secp_ctx))
6908+
.take(count)
6909+
.collect()
6910+
}
6911+
68766912
/// Gets a fake short channel id for use in receiving [phantom node payments]. These fake scids
68776913
/// are used when constructing the phantom invoice's route hints.
68786914
///

lightning/src/routing/router.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub trait Router {
9090
&self, payer: &PublicKey, route_params: &RouteParameters,
9191
first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: InFlightHtlcs
9292
) -> Result<Route, LightningError>;
93+
9394
/// Finds a [`Route`] for a payment between the given `payer` and a payee.
9495
///
9596
/// The `payee` and the payment's value are given in [`RouteParameters::payment_params`]
@@ -104,6 +105,17 @@ pub trait Router {
104105
) -> Result<Route, LightningError> {
105106
self.find_route(payer, route_params, first_hops, inflight_htlcs)
106107
}
108+
109+
/// Finds partial paths to the `recipient` node for creating `BlindedPath`s. The nodes in
110+
/// `peers` are assumed to be direct peers with the `recipient`.
111+
///
112+
/// The default implementation returns two-node paths for each node in `peers` with the
113+
/// `recipient` node.
114+
fn find_partial_paths(
115+
&self, recipient: PublicKey, peers: &[PublicKey]
116+
) -> Result<Vec<Vec<PublicKey>>, ()> {
117+
Ok(peers.iter().map(|hop| vec![*hop, recipient]).collect())
118+
}
107119
}
108120

109121
/// [`ScoreLookUp`] implementation that factors in in-flight HTLC liquidity.

0 commit comments

Comments
 (0)