Skip to content

Commit 45181c9

Browse files
committed
WIP: Include blinded paths
1 parent a32c5cb commit 45181c9

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

lightning/src/blinded_path/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ impl BlindedPath {
6464
///
6565
/// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
6666
// TODO: make all payloads the same size with padding + add dummy hops
67-
pub fn new_for_message<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification>
68-
(node_pks: &[PublicKey], entropy_source: &ES, secp_ctx: &Secp256k1<T>) -> Result<Self, ()>
69-
{
67+
pub fn new_for_message<ES: Deref, T: secp256k1::Signing + secp256k1::Verification>(
68+
node_pks: &[PublicKey], entropy_source: ES, secp_ctx: &Secp256k1<T>
69+
) -> Result<Self, ()> where ES::Target: EntropySource {
7070
if node_pks.len() < 2 { return Err(()) }
7171
let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
7272
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");

lightning/src/ln/channelmanager.rs

+42-6
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};
@@ -5741,6 +5742,10 @@ where
57415742
/// Creates an [`OfferBuilder`] such that the [`Offer`] it builds is recognized by the
57425743
/// [`OnionMessenger`] when handling [`InvoiceRequest`] messages for the offer.
57435744
///
5745+
/// Uses [`Router::find_partial_paths`] to construct a [`BlindedPath`] for the offer. If one is
5746+
/// found, also uses a derived signing pubkey for recipient privacy. Otherwise, uses the node id
5747+
/// as the signing pubkey.
5748+
///
57445749
/// [`Offer`]: crate::offers::offer::Offer
57455750
/// [`OnionMessenger`]: crate::onion_message::OnionMessenger
57465751
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
@@ -5751,14 +5756,24 @@ where
57515756
let expanded_key = &self.inbound_payment_key;
57525757
let entropy = &*self.entropy_source;
57535758
let secp_ctx = &self.secp_ctx;
5759+
let builder = OfferBuilder::deriving_signing_pubkey(
5760+
description, node_id, expanded_key, entropy, secp_ctx
5761+
);
57545762

5755-
// TODO: Set blinded paths
5756-
OfferBuilder::deriving_signing_pubkey(description, node_id, expanded_key, entropy, secp_ctx)
5763+
match self.create_blinded_paths(1) {
5764+
Ok(paths) if !paths.is_empty() => builder.path(paths.into_iter().next().unwrap()),
5765+
// TODO: check if node is public?
5766+
Ok(_) | Err(_) => builder,
5767+
}
57575768
}
57585769

57595770
/// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
57605771
/// [`OnionMessenger`] when handling [`Invoice`] messages for the refund.
57615772
///
5773+
/// Uses [`Router::find_partial_paths`] to construct a [`BlindedPath`] for the refund. If one is
5774+
/// found, also uses a derived payer id for sender privacy. Otherwise, uses the node id as the
5775+
/// payer id.
5776+
///
57625777
/// [`Refund`]: crate::offers::refund::Refund
57635778
/// [`OnionMessenger`]: crate::onion_message::OnionMessenger
57645779
/// [`Invoice`]: crate::offers::invoice::Invoice
@@ -5769,11 +5784,15 @@ where
57695784
let expanded_key = &self.inbound_payment_key;
57705785
let entropy = &*self.entropy_source;
57715786
let secp_ctx = &self.secp_ctx;
5772-
5773-
// TODO: Set blinded paths
5774-
RefundBuilder::deriving_payer_id(
5787+
let builder = RefundBuilder::deriving_payer_id(
57755788
description, node_id, expanded_key, entropy, secp_ctx, amount_msats
5776-
)
5789+
)?;
5790+
5791+
match self.create_blinded_paths(1) {
5792+
Ok(paths) if !paths.is_empty() => Ok(builder.path(paths.into_iter().next().unwrap())),
5793+
// TODO: check if node is public?
5794+
Ok(_) | Err(_) => Ok(builder),
5795+
}
57775796
}
57785797

57795798
/// Creates an [`InvoiceRequestBuilder`] such that the [`InvoiceRequest`] it builds is
@@ -5923,6 +5942,23 @@ where
59235942
inbound_payment::get_payment_preimage(payment_hash, payment_secret, &self.inbound_payment_key)
59245943
}
59255944

5945+
///
5946+
fn create_blinded_paths(&self, count: usize) -> Result<Vec<BlindedPath>, ()> {
5947+
let last_hops = self.per_peer_state.read().unwrap().iter()
5948+
.filter(|(_, peer)| peer.lock().unwrap().latest_features.supports_route_blinding())
5949+
.map(|(node_id, _)| *node_id)
5950+
.collect::<Vec<_>>();
5951+
let entropy_source = self.entropy_source.deref();
5952+
let secp_ctx = &self.secp_ctx;
5953+
5954+
self.router
5955+
.find_partial_paths(self.get_our_node_id(), last_hops.as_slice())?
5956+
.into_iter()
5957+
.map(|node_pks| BlindedPath::new_for_message(&node_pks[..], entropy_source, secp_ctx))
5958+
.take(count)
5959+
.collect()
5960+
}
5961+
59265962
/// Gets a fake short channel id for use in receiving [phantom node payments]. These fake scids
59275963
/// are used when constructing the phantom invoice's route hints.
59285964
///

lightning/src/routing/router.rs

+12
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub trait Router {
8787
&self, payer: &PublicKey, route_params: &RouteParameters,
8888
first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: &InFlightHtlcs
8989
) -> Result<Route, LightningError>;
90+
9091
/// Finds a [`Route`] between `payer` and `payee` for a payment with the given values. Includes
9192
/// `PaymentHash` and `PaymentId` to be able to correlate the request with a specific payment.
9293
fn find_route_with_id(
@@ -96,6 +97,17 @@ pub trait Router {
9697
) -> Result<Route, LightningError> {
9798
self.find_route(payer, route_params, first_hops, inflight_htlcs)
9899
}
100+
101+
/// Finds partial paths to the `recipient` node for creating `BlindedPath`s. The nodes in
102+
/// `peers` are assumed to be direct peers with the `recipient`.
103+
///
104+
/// The default implementation returns two-node paths for each node in `peers` with the
105+
/// `recipient` node.
106+
fn find_partial_paths(
107+
&self, recipient: PublicKey, peers: &[PublicKey]
108+
) -> Result<Vec<Vec<PublicKey>>, ()> {
109+
Ok(peers.iter().map(|hop| vec![*hop, recipient]).collect())
110+
}
99111
}
100112

101113
/// [`Score`] implementation that factors in in-flight HTLC liquidity.

0 commit comments

Comments
 (0)