Move blinded message path util into message submodule
authorValentine Wallace <vwallace@protonmail.com>
Fri, 16 Jun 2023 17:59:31 +0000 (13:59 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Tue, 22 Aug 2023 17:26:11 +0000 (13:26 -0400)
lightning/src/blinded_path/message.rs
lightning/src/blinded_path/mod.rs
lightning/src/onion_message/messenger.rs

index 024fb451d00a78d1fe42a0524b042cfe3f42b9ec..2549673b00f1a390306f2e9ca7ae891450a35274 100644 (file)
@@ -1,9 +1,18 @@
 use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
-use crate::blinded_path::BlindedHop;
+
+use crate::blinded_path::{BlindedHop, BlindedPath};
 use crate::blinded_path::utils;
 use crate::io;
+use crate::io::Cursor;
+use crate::ln::onion_utils;
+use crate::onion_message::ControlTlvs;
 use crate::prelude::*;
-use crate::util::ser::{Writeable, Writer};
+use crate::sign::{NodeSigner, Recipient};
+use crate::util::chacha20poly1305rfc::ChaChaPolyReadAdapter;
+use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Writeable, Writer};
+
+use core::mem;
+use core::ops::Deref;
 
 /// TLVs to encode in an intermediate onion message packet's hop data. When provided in a blinded
 /// route, they are encoded into [`BlindedHop::encrypted_payload`].
@@ -77,3 +86,32 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
 
        Ok(blinded_hops)
 }
+
+// Advance the blinded onion message path by one hop, so make the second hop into the new
+// introduction node.
+pub(crate) fn advance_path_by_one<NS: Deref, T: secp256k1::Signing + secp256k1::Verification>(
+       path: &mut BlindedPath, node_signer: &NS, secp_ctx: &Secp256k1<T>
+) -> Result<(), ()> where NS::Target: NodeSigner {
+       let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.blinding_point, None)?;
+       let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
+       let encrypted_control_tlvs = path.blinded_hops.remove(0).encrypted_payload;
+       let mut s = Cursor::new(&encrypted_control_tlvs);
+       let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
+       match ChaChaPolyReadAdapter::read(&mut reader, rho) {
+               Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(ForwardTlvs {
+                       mut next_node_id, next_blinding_override,
+               })}) => {
+                       let mut new_blinding_point = match next_blinding_override {
+                               Some(blinding_point) => blinding_point,
+                               None => {
+                                       onion_utils::next_hop_pubkey(secp_ctx, path.blinding_point,
+                                               control_tlvs_ss.as_ref()).map_err(|_| ())?
+                               }
+                       };
+                       mem::swap(&mut path.blinding_point, &mut new_blinding_point);
+                       mem::swap(&mut path.introduction_node_id, &mut next_node_id);
+                       Ok(())
+               },
+               _ => Err(())
+       }
+}
index 3ad96869f70d4db5255b7b3d3ddd37aa4fb5dbe3..6225f99869b9364c6fadd0f75f437bcafc95536d 100644 (file)
@@ -14,16 +14,11 @@ pub(crate) mod utils;
 
 use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
 
-use crate::sign::{EntropySource, NodeSigner, Recipient};
-use crate::onion_message::ControlTlvs;
+use crate::sign::EntropySource;
 use crate::ln::msgs::DecodeError;
-use crate::ln::onion_utils;
-use crate::util::chacha20poly1305rfc::ChaChaPolyReadAdapter;
-use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, Writeable, Writer};
+use crate::util::ser::{Readable, Writeable, Writer};
 
-use core::mem;
-use core::ops::Deref;
-use crate::io::{self, Cursor};
+use crate::io;
 use crate::prelude::*;
 
 /// Onion messages and payments can be sent and received to blinded paths, which serve to hide the
@@ -77,36 +72,6 @@ impl BlindedPath {
                        blinded_hops: message::blinded_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?,
                })
        }
-
-       // Advance the blinded onion message path by one hop, so make the second hop into the new
-       // introduction node.
-       pub(super) fn advance_message_path_by_one<NS: Deref, T: secp256k1::Signing + secp256k1::Verification>
-               (&mut self, node_signer: &NS, secp_ctx: &Secp256k1<T>) -> Result<(), ()>
-               where NS::Target: NodeSigner
-       {
-               let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &self.blinding_point, None)?;
-               let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
-               let encrypted_control_tlvs = self.blinded_hops.remove(0).encrypted_payload;
-               let mut s = Cursor::new(&encrypted_control_tlvs);
-               let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
-               match ChaChaPolyReadAdapter::read(&mut reader, rho) {
-                       Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(message::ForwardTlvs {
-                               mut next_node_id, next_blinding_override,
-                       })}) => {
-                               let mut new_blinding_point = match next_blinding_override {
-                                       Some(blinding_point) => blinding_point,
-                                       None => {
-                                               onion_utils::next_hop_pubkey(secp_ctx, self.blinding_point,
-                                                       control_tlvs_ss.as_ref()).map_err(|_| ())?
-                                       }
-                               };
-                               mem::swap(&mut self.blinding_point, &mut new_blinding_point);
-                               mem::swap(&mut self.introduction_node_id, &mut next_node_id);
-                               Ok(())
-                       },
-                       _ => Err(())
-               }
-       }
 }
 
 impl Writeable for BlindedPath {
index b6cdbbd7ca777eaf337bc0654031d81c94f45c10..09f207e36aa1fadbd4a2e133b50cecc9dc0dcae7 100644 (file)
@@ -16,7 +16,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
 
 use crate::blinded_path::BlindedPath;
-use crate::blinded_path::message::{ForwardTlvs, ReceiveTlvs};
+use crate::blinded_path::message::{advance_path_by_one, ForwardTlvs, ReceiveTlvs};
 use crate::blinded_path::utils;
 use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient};
 use crate::events::OnionMessageProvider;
@@ -299,7 +299,7 @@ where
                                let our_node_id = self.node_signer.get_node_id(Recipient::Node)
                                        .map_err(|()| SendError::GetNodeIdFailed)?;
                                if blinded_path.introduction_node_id == our_node_id {
-                                       blinded_path.advance_message_path_by_one(&self.node_signer, &self.secp_ctx)
+                                       advance_path_by_one(blinded_path, &self.node_signer, &self.secp_ctx)
                                                .map_err(|()| SendError::BlindedPathAdvanceFailed)?;
                                }
                        }