Skip to content

Commit acb28b9

Browse files
committed
WIP: Include blinded paths
1 parent 213ee79 commit acb28b9

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
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

+39-3
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};
@@ -6353,6 +6354,10 @@ where
63536354
/// Creates an [`OfferBuilder`] such that the [`Offer`] it builds is recognized by the
63546355
/// [`ChannelManager`] when handling [`InvoiceRequest`] messages for the offer.
63556356
///
6357+
/// Uses [`Router::find_partial_paths`] to construct a [`BlindedPath`] for the offer. If one is
6358+
/// found, also uses a derived signing pubkey for recipient privacy. Otherwise, uses the node id
6359+
/// as the signing pubkey.
6360+
///
63566361
/// [`Offer`]: crate::offers::offer::Offer
63576362
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
63586363
pub fn create_offer_builder(
@@ -6362,16 +6367,26 @@ where
63626367
let expanded_key = &self.inbound_payment_key;
63636368
let entropy = &*self.entropy_source;
63646369
let secp_ctx = &self.secp_ctx;
6370+
let builder = OfferBuilder::deriving_signing_pubkey(
6371+
description, node_id, expanded_key, entropy, secp_ctx
6372+
);
63656373

6366-
// TODO: Set blinded paths
6367-
OfferBuilder::deriving_signing_pubkey(description, node_id, expanded_key, entropy, secp_ctx)
6374+
match self.create_blinded_paths(1) {
6375+
Ok(paths) if !paths.is_empty() => builder.path(paths.into_iter().next().unwrap()),
6376+
// TODO: check if node is public?
6377+
Ok(_) | Err(_) => builder,
6378+
}
63686379
}
63696380

63706381
/// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
63716382
/// [`ChannelManager`] when handling [`Bolt12Invoice`] messages for the refund.
63726383
///
63736384
/// The provided `payment_id` is used to ensure that only one invoice is paid for the refund.
63746385
///
6386+
/// Uses [`Router::find_partial_paths`] to construct a [`BlindedPath`] for the refund. If one is
6387+
/// found, also uses a derived payer id for sender privacy. Otherwise, uses the node id as the
6388+
/// payer id.
6389+
///
63756390
/// [`Refund`]: crate::offers::refund::Refund
63766391
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
63776392
pub fn create_refund_builder(
@@ -6382,10 +6397,14 @@ where
63826397
let entropy = &*self.entropy_source;
63836398
let secp_ctx = &self.secp_ctx;
63846399

6385-
// TODO: Set blinded paths
63866400
let builder = RefundBuilder::deriving_payer_id(
63876401
description, node_id, expanded_key, entropy, secp_ctx, amount_msats, payment_id
63886402
)?;
6403+
let builder = match self.create_blinded_paths(1) {
6404+
Ok(paths) if !paths.is_empty() => builder.path(paths.into_iter().next().unwrap()),
6405+
// TODO: check if node is public?
6406+
Ok(_) | Err(_) => builder,
6407+
};
63896408
self.pending_outbound_payments
63906409
.add_new_awaiting_invoice(payment_id)
63916410
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
@@ -6518,6 +6537,23 @@ where
65186537
inbound_payment::get_payment_preimage(payment_hash, payment_secret, &self.inbound_payment_key)
65196538
}
65206539

6540+
///
6541+
fn create_blinded_paths(&self, count: usize) -> Result<Vec<BlindedPath>, ()> {
6542+
let last_hops = self.per_peer_state.read().unwrap().iter()
6543+
.filter(|(_, peer)| peer.lock().unwrap().latest_features.supports_route_blinding())
6544+
.map(|(node_id, _)| *node_id)
6545+
.collect::<Vec<_>>();
6546+
let entropy_source = self.entropy_source.deref();
6547+
let secp_ctx = &self.secp_ctx;
6548+
6549+
self.router
6550+
.find_partial_paths(self.get_our_node_id(), last_hops.as_slice())?
6551+
.into_iter()
6552+
.map(|node_pks| BlindedPath::new_for_message(&node_pks[..], entropy_source, secp_ctx))
6553+
.take(count)
6554+
.collect()
6555+
}
6556+
65216557
/// Gets a fake short channel id for use in receiving [phantom node payments]. These fake scids
65226558
/// are used when constructing the phantom invoice's route hints.
65236559
///

lightning/src/routing/router.rs

+12
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
/// [`Score`] implementation that factors in in-flight HTLC liquidity.

0 commit comments

Comments
 (0)