Skip to content

Commit 9ee4285

Browse files
Move blinded message path util into message submodule
1 parent a3dbf0d commit 9ee4285

File tree

3 files changed

+55
-52
lines changed

3 files changed

+55
-52
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
2-
use crate::blinded_path::BlindedHop;
1+
use bitcoin::hashes::{Hash, HashEngine};
2+
use bitcoin::hashes::sha256::Hash as Sha256;
3+
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
4+
5+
use crate::blinded_path::{BlindedHop, BlindedPath};
36
use crate::blinded_path::utils;
47
use crate::io;
8+
use crate::io::Cursor;
9+
use crate::ln::onion_utils;
10+
use crate::onion_message::ControlTlvs;
511
use crate::prelude::*;
6-
use crate::util::ser::{Writeable, Writer};
12+
use crate::sign::{NodeSigner, Recipient};
13+
use crate::util::chacha20poly1305rfc::ChaChaPolyReadAdapter;
14+
use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Writeable, Writer};
15+
16+
use core::mem;
17+
use core::ops::Deref;
718

819
/// TLVs to encode in an intermediate onion message packet's hop data. When provided in a blinded
920
/// route, they are encoded into [`BlindedHop::encrypted_payload`].
@@ -77,3 +88,38 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
7788

7889
Ok(blinded_hops)
7990
}
91+
92+
// Advance the blinded onion message path by one hop, so make the second hop into the new
93+
// introduction node.
94+
pub(crate) fn advance_path_by_one<NS: Deref, T: secp256k1::Signing + secp256k1::Verification>(
95+
path: &mut BlindedPath, node_signer: &NS, secp_ctx: &Secp256k1<T>
96+
) -> Result<(), ()> where NS::Target: NodeSigner {
97+
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.blinding_point, None)?;
98+
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
99+
let encrypted_control_tlvs = path.blinded_hops.remove(0).encrypted_payload;
100+
let mut s = Cursor::new(&encrypted_control_tlvs);
101+
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
102+
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
103+
Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(ForwardTlvs {
104+
mut next_node_id, next_blinding_override,
105+
})}) => {
106+
let mut new_blinding_point = match next_blinding_override {
107+
Some(blinding_point) => blinding_point,
108+
None => {
109+
let blinding_factor = {
110+
let mut sha = Sha256::engine();
111+
sha.input(&path.blinding_point.serialize()[..]);
112+
sha.input(control_tlvs_ss.as_ref());
113+
Sha256::from_engine(sha).into_inner()
114+
};
115+
path.blinding_point.mul_tweak(secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap())
116+
.map_err(|_| ())?
117+
}
118+
};
119+
mem::swap(&mut path.blinding_point, &mut new_blinding_point);
120+
mem::swap(&mut path.introduction_node_id, &mut next_node_id);
121+
Ok(())
122+
},
123+
_ => Err(())
124+
}
125+
}

lightning/src/blinded_path/mod.rs

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,13 @@
1212
pub(crate) mod message;
1313
pub(crate) mod utils;
1414

15-
use bitcoin::hashes::{Hash, HashEngine};
16-
use bitcoin::hashes::sha256::Hash as Sha256;
17-
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
15+
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
1816

19-
use crate::sign::{EntropySource, NodeSigner, Recipient};
20-
use crate::onion_message::ControlTlvs;
17+
use crate::sign::EntropySource;
2118
use crate::ln::msgs::DecodeError;
22-
use crate::ln::onion_utils;
23-
use crate::util::chacha20poly1305rfc::ChaChaPolyReadAdapter;
24-
use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, Writeable, Writer};
19+
use crate::util::ser::{Readable, Writeable, Writer};
2520

26-
use core::mem;
27-
use core::ops::Deref;
28-
use crate::io::{self, Cursor};
21+
use crate::io;
2922
use crate::prelude::*;
3023

3124
/// Onion messages and payments can be sent and received to blinded paths, which serve to hide the
@@ -79,42 +72,6 @@ impl BlindedPath {
7972
blinded_hops: message::blinded_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?,
8073
})
8174
}
82-
83-
// Advance the blinded onion message path by one hop, so make the second hop into the new
84-
// introduction node.
85-
pub(super) fn advance_message_path_by_one<NS: Deref, T: secp256k1::Signing + secp256k1::Verification>
86-
(&mut self, node_signer: &NS, secp_ctx: &Secp256k1<T>) -> Result<(), ()>
87-
where NS::Target: NodeSigner
88-
{
89-
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &self.blinding_point, None)?;
90-
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
91-
let encrypted_control_tlvs = self.blinded_hops.remove(0).encrypted_payload;
92-
let mut s = Cursor::new(&encrypted_control_tlvs);
93-
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
94-
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
95-
Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(message::ForwardTlvs {
96-
mut next_node_id, next_blinding_override,
97-
})}) => {
98-
let mut new_blinding_point = match next_blinding_override {
99-
Some(blinding_point) => blinding_point,
100-
None => {
101-
let blinding_factor = {
102-
let mut sha = Sha256::engine();
103-
sha.input(&self.blinding_point.serialize()[..]);
104-
sha.input(control_tlvs_ss.as_ref());
105-
Sha256::from_engine(sha).into_inner()
106-
};
107-
self.blinding_point.mul_tweak(secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap())
108-
.map_err(|_| ())?
109-
}
110-
};
111-
mem::swap(&mut self.blinding_point, &mut new_blinding_point);
112-
mem::swap(&mut self.introduction_node_id, &mut next_node_id);
113-
Ok(())
114-
},
115-
_ => Err(())
116-
}
117-
}
11875
}
11976

12077
impl Writeable for BlindedPath {

lightning/src/onion_message/messenger.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
1616
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
1717

1818
use crate::blinded_path::BlindedPath;
19-
use crate::blinded_path::message::{ForwardTlvs, ReceiveTlvs};
19+
use crate::blinded_path::message::{advance_path_by_one, ForwardTlvs, ReceiveTlvs};
2020
use crate::blinded_path::utils;
2121
use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient};
2222
use crate::events::OnionMessageProvider;
@@ -299,7 +299,7 @@ where
299299
let our_node_id = self.node_signer.get_node_id(Recipient::Node)
300300
.map_err(|()| SendError::GetNodeIdFailed)?;
301301
if blinded_path.introduction_node_id == our_node_id {
302-
blinded_path.advance_message_path_by_one(&self.node_signer, &self.secp_ctx)
302+
advance_path_by_one(blinded_path, &self.node_signer, &self.secp_ctx)
303303
.map_err(|()| SendError::BlindedPathAdvanceFailed)?;
304304
}
305305
}

0 commit comments

Comments
 (0)