Skip to content

Commit 4c22614

Browse files
f break BlindedPaymentTlv variants out into structs
1 parent 32d8084 commit 4c22614

File tree

3 files changed

+68
-50
lines changed

3 files changed

+68
-50
lines changed

lightning/src/blinded_path/mod.rs

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

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

18-
use crate::blinded_path::payment::BlindedPaymentTlvs;
18+
use crate::blinded_path::payment::{ForwardTlvs, ReceiveTlvs};
1919
use crate::sign::EntropySource;
2020
use crate::ln::msgs::DecodeError;
2121
use crate::util::ser::{Readable, Writeable, Writer};
@@ -82,16 +82,18 @@ impl BlindedPath {
8282
/// Errors if `path` is empty or a node id in `path` is invalid.
8383
// TODO: make all payloads the same size with padding + add dummy hops
8484
pub fn new_for_payment<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification>(
85-
path: &[(PublicKey, BlindedPaymentTlvs)], entropy_source: &ES, secp_ctx: &Secp256k1<T>
85+
intermediate_nodes: &[(PublicKey, ForwardTlvs)], payee_node_id: PublicKey,
86+
payee_tlvs: ReceiveTlvs, entropy_source: &ES, secp_ctx: &Secp256k1<T>
8687
) -> Result<Self, ()> {
87-
if path.len() < 1 { return Err(()) }
8888
let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
8989
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
9090

9191
Ok(BlindedPath {
92-
introduction_node_id: path[0].0,
92+
introduction_node_id: intermediate_nodes.first().map_or(payee_node_id, |n| n.0),
9393
blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
94-
blinded_hops: payment::blinded_hops(secp_ctx, path, &blinding_secret).map_err(|_| ())?,
94+
blinded_hops: payment::blinded_hops(
95+
secp_ctx, intermediate_nodes, payee_node_id, payee_tlvs, &blinding_secret
96+
).map_err(|_| ())?,
9597
})
9698
}
9799
}

lightning/src/blinded_path/payment.rs

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,43 @@ use crate::ln::msgs::DecodeError;
1313
use crate::prelude::*;
1414
use crate::util::ser::{Readable, Writeable, Writer};
1515

16+
/// Data to construct a [`BlindedHop`] for forwarding a payment.
17+
pub struct ForwardTlvs {
18+
/// The short channel id this payment is being forwarded over.
19+
short_channel_id: u64,
20+
/// Payment parameters for relaying over this channel.
21+
payment_relay: PaymentRelay,
22+
/// Payment constraints when relaying over this channel.
23+
payment_constraints: PaymentConstraints,
24+
/// Supported and required features when relaying a payment onion containing this object's
25+
/// corresponding [`BlindedHop`].
26+
///
27+
/// [`BlindedHop`]: crate::blinded_path::BlindedHop
28+
features: BlindedHopFeatures,
29+
}
30+
31+
/// Data to construct a [`BlindedHop`] for receiving a payment.
32+
pub struct ReceiveTlvs {
33+
/// Used to authenticate the sender of a payment to the receiver and tie MPP HTLCs together.
34+
payment_secret: PaymentSecret,
35+
/// Constraints for the receiver of this payment.
36+
payment_constraints: PaymentConstraints,
37+
}
38+
1639
/// Data to construct a [`BlindedHop`] for sending a payment over.
1740
///
1841
/// [`BlindedHop`]: crate::blinded_path::BlindedHop
19-
pub enum BlindedPaymentTlvs {
42+
pub(crate) enum BlindedPaymentTlvs {
2043
/// This blinded payment data is to be forwarded.
21-
Forward {
22-
/// The short channel id this payment is being forwarded over.
23-
short_channel_id: u64,
24-
/// Payment parameters for relaying over this channel.
25-
payment_relay: PaymentRelay,
26-
/// Payment constraints when relaying over this channel.
27-
payment_constraints: PaymentConstraints,
28-
/// Supported and required features when relaying a payment onion containing this object's
29-
/// corresponding [`BlindedHop`].
30-
///
31-
/// [`BlindedHop`]: crate::blinded_path::BlindedHop
32-
features: BlindedHopFeatures,
33-
},
44+
Forward(ForwardTlvs),
3445
/// This blinded payment data is to be received.
35-
Receive {
36-
/// Used to authenticate the sender of a payment to the receiver and tie MPP HTLCs together.
37-
payment_secret: PaymentSecret,
38-
/// Constraints for the receiver of this payment.
39-
payment_constraints: PaymentConstraints,
40-
},
46+
Receive(ReceiveTlvs),
47+
}
48+
49+
// Used to include forward and receive TLVs in the same iterator for encoding.
50+
enum BlindedPaymentTlvsRef<'a> {
51+
Forward(&'a ForwardTlvs),
52+
Receive(&'a ReceiveTlvs),
4153
}
4254

4355
/// Parameters for relaying over a given [`BlindedHop`].
@@ -72,24 +84,24 @@ pub struct PaymentConstraints {
7284
pub htlc_minimum_msat: u64,
7385
}
7486

75-
impl Writeable for BlindedPaymentTlvs {
87+
impl_writeable_tlv_based!(ForwardTlvs, {
88+
(2, short_channel_id, required),
89+
(10, payment_relay, required),
90+
(12, payment_constraints, required),
91+
(14, features, required),
92+
});
93+
94+
impl_writeable_tlv_based!(ReceiveTlvs, {
95+
(6, payment_secret, required),
96+
(12, payment_constraints, required),
97+
});
98+
99+
impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {
76100
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
77101
// TODO: write padding
78102
match self {
79-
Self::Forward { short_channel_id, payment_relay, payment_constraints, features } => {
80-
encode_tlv_stream!(w, {
81-
(2, short_channel_id, required),
82-
(10, payment_relay, required),
83-
(12, payment_constraints, required),
84-
(14, features, required),
85-
});
86-
},
87-
Self::Receive { payment_secret, payment_constraints } => {
88-
encode_tlv_stream!(w, {
89-
(6, payment_secret, required),
90-
(12, payment_constraints, required),
91-
});
92-
}
103+
Self::Forward(tlvs) => tlvs.write(w)?,
104+
Self::Receive(tlvs) => tlvs.write(w)?,
93105
}
94106
Ok(())
95107
}
@@ -107,28 +119,32 @@ impl Readable for BlindedPaymentTlvs {
107119
});
108120
if let Some(short_channel_id) = scid {
109121
if payment_secret.is_some() { return Err(DecodeError::InvalidValue) }
110-
Ok(BlindedPaymentTlvs::Forward {
122+
Ok(BlindedPaymentTlvs::Forward(ForwardTlvs {
111123
short_channel_id,
112124
payment_relay: payment_relay.ok_or(DecodeError::InvalidValue)?,
113125
payment_constraints: payment_constraints.0.unwrap(),
114126
features: features.ok_or(DecodeError::InvalidValue)?,
115-
})
127+
}))
116128
} else {
117129
if payment_relay.is_some() | features.is_some() { return Err(DecodeError::InvalidValue) }
118-
Ok(BlindedPaymentTlvs::Receive {
130+
Ok(BlindedPaymentTlvs::Receive(ReceiveTlvs {
119131
payment_secret: payment_secret.ok_or(DecodeError::InvalidValue)?,
120132
payment_constraints: payment_constraints.0.unwrap(),
121-
})
133+
}))
122134
}
123135
}
124136
}
125137

126-
/// Construct blinded payment hops for the given `unblinded_path`.
138+
/// Construct blinded payment hops for the given `intermediate_nodes` and payee info.
127139
pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
128-
secp_ctx: &Secp256k1<T>, unblinded_path: &[(PublicKey, BlindedPaymentTlvs)], session_priv: &SecretKey
140+
secp_ctx: &Secp256k1<T>, intermediate_nodes: &[(PublicKey, ForwardTlvs)],
141+
payee_node_id: PublicKey, payee_tlvs: ReceiveTlvs, session_priv: &SecretKey
129142
) -> Result<Vec<BlindedHop>, secp256k1::Error> {
130-
utils::construct_blinded_hops(secp_ctx, unblinded_path.iter().map(|(pk, _)| pk),
131-
unblinded_path.iter().map(|(_, tlvs)| tlvs), session_priv)
143+
let pks = intermediate_nodes.iter().map(|(pk, _)| pk)
144+
.chain(core::iter::once(&payee_node_id));
145+
let tlvs = intermediate_nodes.iter().map(|(_, tlvs)| BlindedPaymentTlvsRef::Forward(tlvs))
146+
.chain(core::iter::once(BlindedPaymentTlvsRef::Receive(&payee_tlvs)));
147+
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
132148
}
133149

134150
impl_writeable_msg!(PaymentRelay, {

lightning/src/blinded_path/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ pub(super) fn construct_blinded_hops<'a, T, I1, I2>(
111111
) -> Result<Vec<BlindedHop>, secp256k1::Error>
112112
where
113113
T: secp256k1::Signing + secp256k1::Verification,
114-
I1: ExactSizeIterator<Item=&'a PublicKey>,
114+
I1: Iterator<Item=&'a PublicKey>,
115115
I2: Iterator,
116116
I2::Item: Writeable
117117
{
118-
let mut blinded_hops = Vec::with_capacity(unblinded_pks.len());
118+
let mut blinded_hops = Vec::with_capacity(unblinded_pks.size_hint().0);
119119
let mut curr_hop_idx = 0;
120120
construct_keys_callback(
121121
secp_ctx, unblinded_pks, None, session_priv,

0 commit comments

Comments
 (0)