Skip to content

Commit a3dbf0d

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 4450e42 commit a3dbf0d

File tree

4 files changed

+90
-78
lines changed

4 files changed

+90
-78
lines changed
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::hashes::{Hash, HashEngine};
@@ -75,7 +76,7 @@ impl BlindedPath {
7576
Ok(BlindedPath {
7677
introduction_node_id,
7778
blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
78-
blinded_hops: blinded_message_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?,
79+
blinded_hops: message::blinded_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?,
7980
})
8081
}
8182

@@ -91,7 +92,7 @@ impl BlindedPath {
9192
let mut s = Cursor::new(&encrypted_control_tlvs);
9293
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
9394
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
94-
Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(ForwardTlvs {
95+
Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(message::ForwardTlvs {
9596
mut next_node_id, next_blinding_override,
9697
})}) => {
9798
let mut new_blinding_point = match next_blinding_override {
@@ -116,40 +117,6 @@ impl BlindedPath {
116117
}
117118
}
118119

119-
/// Construct blinded onion message hops for the given `unblinded_path`.
120-
fn blinded_message_hops<T: secp256k1::Signing + secp256k1::Verification>(
121-
secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], session_priv: &SecretKey
122-
) -> Result<Vec<BlindedHop>, secp256k1::Error> {
123-
let mut blinded_hops = Vec::with_capacity(unblinded_path.len());
124-
125-
let mut prev_ss_and_blinded_node_id = None;
126-
utils::construct_keys_callback(secp_ctx, unblinded_path, None, session_priv, |blinded_node_id, _, _, encrypted_payload_ss, unblinded_pk, _| {
127-
if let Some((prev_ss, prev_blinded_node_id)) = prev_ss_and_blinded_node_id {
128-
if let Some(pk) = unblinded_pk {
129-
let payload = ForwardTlvs {
130-
next_node_id: pk,
131-
next_blinding_override: None,
132-
};
133-
blinded_hops.push(BlindedHop {
134-
blinded_node_id: prev_blinded_node_id,
135-
encrypted_payload: utils::encrypt_payload(payload, prev_ss),
136-
});
137-
} else { debug_assert!(false); }
138-
}
139-
prev_ss_and_blinded_node_id = Some((encrypted_payload_ss, blinded_node_id));
140-
})?;
141-
142-
if let Some((final_ss, final_blinded_node_id)) = prev_ss_and_blinded_node_id {
143-
let final_payload = ReceiveTlvs { path_id: None };
144-
blinded_hops.push(BlindedHop {
145-
blinded_node_id: final_blinded_node_id,
146-
encrypted_payload: utils::encrypt_payload(final_payload, final_ss),
147-
});
148-
} else { debug_assert!(false) }
149-
150-
Ok(blinded_hops)
151-
}
152-
153120
impl Writeable for BlindedPath {
154121
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
155122
self.introduction_node_id.write(w)?;
@@ -185,41 +152,3 @@ impl_writeable!(BlindedHop, {
185152
encrypted_payload
186153
});
187154

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

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)