Skip to content

Commit 381cc64

Browse files
Move some blinded path message code into message submodule.
We'll similarly separate blinded path payments code into its own module.
1 parent a5b7cf2 commit 381cc64

File tree

4 files changed

+90
-78
lines changed

4 files changed

+90
-78
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
2+
use crate::blinded_path::BlindedHop;
3+
use crate::blinded_path::utils;
4+
use crate::io;
5+
use crate::prelude::*;
6+
use crate::util::ser::{Writeable, Writer};
7+
8+
/// TLVs to encode in an intermediate onion message packet's hop data. When provided in a blinded
9+
/// route, they are encoded into [`BlindedHop::encrypted_payload`].
10+
pub(crate) struct ForwardTlvs {
11+
/// The node id of the next hop in the onion message's path.
12+
pub(crate) next_node_id: PublicKey,
13+
/// Senders to a blinded path use this value to concatenate the route they find to the
14+
/// introduction node with the blinded path.
15+
pub(crate) next_blinding_override: Option<PublicKey>,
16+
}
17+
18+
/// Similar to [`ForwardTlvs`], but these TLVs are for the final node.
19+
pub(crate) struct ReceiveTlvs {
20+
/// If `path_id` is `Some`, it is used to identify the blinded path that this onion message is
21+
/// sending to. This is useful for receivers to check that said blinded path is being used in
22+
/// the right context.
23+
pub(crate) path_id: Option<[u8; 32]>,
24+
}
25+
26+
impl Writeable for ForwardTlvs {
27+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
28+
// TODO: write padding
29+
encode_tlv_stream!(writer, {
30+
(4, self.next_node_id, required),
31+
(8, self.next_blinding_override, option)
32+
});
33+
Ok(())
34+
}
35+
}
36+
37+
impl Writeable for ReceiveTlvs {
38+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
39+
// TODO: write padding
40+
encode_tlv_stream!(writer, {
41+
(6, self.path_id, option),
42+
});
43+
Ok(())
44+
}
45+
}
46+
47+
/// Construct blinded onion message hops for the given `unblinded_path`.
48+
pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
49+
secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], session_priv: &SecretKey
50+
) -> Result<Vec<BlindedHop>, secp256k1::Error> {
51+
let mut blinded_hops = Vec::with_capacity(unblinded_path.len());
52+
53+
let mut prev_ss_and_blinded_node_id = None;
54+
utils::construct_keys_callback(secp_ctx, unblinded_path, None, session_priv, |blinded_node_id, _, _, encrypted_payload_ss, unblinded_pk, _| {
55+
if let Some((prev_ss, prev_blinded_node_id)) = prev_ss_and_blinded_node_id {
56+
if let Some(pk) = unblinded_pk {
57+
let payload = ForwardTlvs {
58+
next_node_id: pk,
59+
next_blinding_override: None,
60+
};
61+
blinded_hops.push(BlindedHop {
62+
blinded_node_id: prev_blinded_node_id,
63+
encrypted_payload: utils::encrypt_payload(payload, prev_ss),
64+
});
65+
} else { debug_assert!(false); }
66+
}
67+
prev_ss_and_blinded_node_id = Some((encrypted_payload_ss, blinded_node_id));
68+
})?;
69+
70+
if let Some((final_ss, final_blinded_node_id)) = prev_ss_and_blinded_node_id {
71+
let final_payload = ReceiveTlvs { path_id: None };
72+
blinded_hops.push(BlindedHop {
73+
blinded_node_id: final_blinded_node_id,
74+
encrypted_payload: utils::encrypt_payload(final_payload, final_ss),
75+
});
76+
} else { debug_assert!(false) }
77+
78+
Ok(blinded_hops)
79+
}

lightning/src/blinded_path/mod.rs

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
//! Creating blinded paths and related utilities live here.
1111
12+
pub(crate) mod message;
1213
pub(crate) mod utils;
1314

1415
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
@@ -73,7 +74,7 @@ impl BlindedPath {
7374
Ok(BlindedPath {
7475
introduction_node_id,
7576
blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
76-
blinded_hops: blinded_message_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?,
77+
blinded_hops: message::blinded_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?,
7778
})
7879
}
7980

@@ -89,7 +90,7 @@ impl BlindedPath {
8990
let mut s = Cursor::new(&encrypted_control_tlvs);
9091
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
9192
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
92-
Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(ForwardTlvs {
93+
Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(message::ForwardTlvs {
9394
mut next_node_id, next_blinding_override,
9495
})}) => {
9596
let mut new_blinding_point = match next_blinding_override {
@@ -108,40 +109,6 @@ impl BlindedPath {
108109
}
109110
}
110111

111-
/// Construct blinded onion message hops for the given `unblinded_path`.
112-
fn blinded_message_hops<T: secp256k1::Signing + secp256k1::Verification>(
113-
secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], session_priv: &SecretKey
114-
) -> Result<Vec<BlindedHop>, secp256k1::Error> {
115-
let mut blinded_hops = Vec::with_capacity(unblinded_path.len());
116-
117-
let mut prev_ss_and_blinded_node_id = None;
118-
utils::construct_keys_callback(secp_ctx, unblinded_path, None, session_priv, |blinded_node_id, _, _, encrypted_payload_ss, unblinded_pk, _| {
119-
if let Some((prev_ss, prev_blinded_node_id)) = prev_ss_and_blinded_node_id {
120-
if let Some(pk) = unblinded_pk {
121-
let payload = ForwardTlvs {
122-
next_node_id: pk,
123-
next_blinding_override: None,
124-
};
125-
blinded_hops.push(BlindedHop {
126-
blinded_node_id: prev_blinded_node_id,
127-
encrypted_payload: utils::encrypt_payload(payload, prev_ss),
128-
});
129-
} else { debug_assert!(false); }
130-
}
131-
prev_ss_and_blinded_node_id = Some((encrypted_payload_ss, blinded_node_id));
132-
})?;
133-
134-
if let Some((final_ss, final_blinded_node_id)) = prev_ss_and_blinded_node_id {
135-
let final_payload = ReceiveTlvs { path_id: None };
136-
blinded_hops.push(BlindedHop {
137-
blinded_node_id: final_blinded_node_id,
138-
encrypted_payload: utils::encrypt_payload(final_payload, final_ss),
139-
});
140-
} else { debug_assert!(false) }
141-
142-
Ok(blinded_hops)
143-
}
144-
145112
impl Writeable for BlindedPath {
146113
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
147114
self.introduction_node_id.write(w)?;
@@ -177,41 +144,3 @@ impl_writeable!(BlindedHop, {
177144
encrypted_payload
178145
});
179146

180-
/// TLVs to encode in an intermediate onion message packet's hop data. When provided in a blinded
181-
/// route, they are encoded into [`BlindedHop::encrypted_payload`].
182-
pub(crate) struct ForwardTlvs {
183-
/// The node id of the next hop in the onion message's path.
184-
pub(super) next_node_id: PublicKey,
185-
/// Senders to a blinded path use this value to concatenate the route they find to the
186-
/// introduction node with the blinded path.
187-
pub(super) next_blinding_override: Option<PublicKey>,
188-
}
189-
190-
/// Similar to [`ForwardTlvs`], but these TLVs are for the final node.
191-
pub(crate) struct ReceiveTlvs {
192-
/// If `path_id` is `Some`, it is used to identify the blinded path that this onion message is
193-
/// sending to. This is useful for receivers to check that said blinded path is being used in
194-
/// the right context.
195-
pub(super) path_id: Option<[u8; 32]>,
196-
}
197-
198-
impl Writeable for ForwardTlvs {
199-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
200-
// TODO: write padding
201-
encode_tlv_stream!(writer, {
202-
(4, self.next_node_id, required),
203-
(8, self.next_blinding_override, option)
204-
});
205-
Ok(())
206-
}
207-
}
208-
209-
impl Writeable for ReceiveTlvs {
210-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
211-
// TODO: write padding
212-
encode_tlv_stream!(writer, {
213-
(6, self.path_id, option),
214-
});
215-
Ok(())
216-
}
217-
}

lightning/src/onion_message/messenger.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use bitcoin::hashes::hmac::{Hmac, HmacEngine};
1515
use bitcoin::hashes::sha256::Hash as Sha256;
1616
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
1717

18-
use crate::blinded_path::{BlindedPath, ForwardTlvs, ReceiveTlvs, utils};
18+
use crate::blinded_path::BlindedPath;
19+
use crate::blinded_path::message::{ForwardTlvs, ReceiveTlvs};
20+
use crate::blinded_path::utils;
1921
use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient};
2022
use crate::events::OnionMessageProvider;
2123
use crate::ln::features::{InitFeatures, NodeFeatures};

lightning/src/onion_message/packet.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
use bitcoin::secp256k1::PublicKey;
1313
use bitcoin::secp256k1::ecdh::SharedSecret;
1414

15-
use crate::blinded_path::{BlindedPath, ForwardTlvs, ReceiveTlvs};
15+
use crate::blinded_path::BlindedPath;
16+
use crate::blinded_path::message::{ForwardTlvs, ReceiveTlvs};
1617
use crate::ln::msgs::DecodeError;
1718
use crate::ln::onion_utils;
1819
use super::messenger::CustomOnionMessageHandler;
@@ -151,15 +152,16 @@ pub(super) enum ForwardControlTlvs {
151152
Blinded(Vec<u8>),
152153
/// If we're constructing an onion message hop through an intermediate unblinded node, we'll need
153154
/// to construct the intermediate hop's control TLVs in their unblinded state to avoid encoding
154-
/// them into an intermediate Vec. See [`crate::blinded_path::ForwardTlvs`] for more info.
155+
/// them into an intermediate Vec. See [`crate::blinded_path::message::ForwardTlvs`] for more
156+
/// info.
155157
Unblinded(ForwardTlvs),
156158
}
157159

158160
/// Receive control TLVs in their blinded and unblinded form.
159161
pub(super) enum ReceiveControlTlvs {
160162
/// See [`ForwardControlTlvs::Blinded`].
161163
Blinded(Vec<u8>),
162-
/// See [`ForwardControlTlvs::Unblinded`] and [`crate::blinded_path::ReceiveTlvs`].
164+
/// See [`ForwardControlTlvs::Unblinded`] and [`crate::blinded_path::message::ReceiveTlvs`].
163165
Unblinded(ReceiveTlvs),
164166
}
165167

0 commit comments

Comments
 (0)