Move blinded path util into blinded_path::utils
authorValentine Wallace <vwallace@protonmail.com>
Fri, 16 Jun 2023 17:22:53 +0000 (13:22 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Tue, 22 Aug 2023 17:18:42 +0000 (13:18 -0400)
This way it can be more easily reused for blinded payment paths.

lightning/src/blinded_path/mod.rs
lightning/src/blinded_path/utils.rs

index c52df1651fe3a5b3a8aaf137113881a31478147f..2022e06c99a10c80aa294ed6dbefcf58fcd972a8 100644 (file)
@@ -17,8 +17,8 @@ use crate::sign::{EntropySource, NodeSigner, Recipient};
 use crate::onion_message::ControlTlvs;
 use crate::ln::msgs::DecodeError;
 use crate::ln::onion_utils;
-use crate::util::chacha20poly1305rfc::{ChaChaPolyReadAdapter, ChaChaPolyWriteAdapter};
-use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, VecWriter, Writeable, Writer};
+use crate::util::chacha20poly1305rfc::ChaChaPolyReadAdapter;
+use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, Writeable, Writer};
 
 use core::mem;
 use core::ops::Deref;
@@ -124,7 +124,7 @@ fn blinded_message_hops<T: secp256k1::Signing + secp256k1::Verification>(
                                };
                                blinded_hops.push(BlindedHop {
                                        blinded_node_id: prev_blinded_node_id,
-                                       encrypted_payload: encrypt_payload(payload, prev_ss),
+                                       encrypted_payload: utils::encrypt_payload(payload, prev_ss),
                                });
                        } else { debug_assert!(false); }
                }
@@ -135,21 +135,13 @@ fn blinded_message_hops<T: secp256k1::Signing + secp256k1::Verification>(
                let final_payload = ReceiveTlvs { path_id: None };
                blinded_hops.push(BlindedHop {
                        blinded_node_id: final_blinded_node_id,
-                       encrypted_payload: encrypt_payload(final_payload, final_ss),
+                       encrypted_payload: utils::encrypt_payload(final_payload, final_ss),
                });
        } else { debug_assert!(false) }
 
        Ok(blinded_hops)
 }
 
-/// Encrypt TLV payload to be used as a [`BlindedHop::encrypted_payload`].
-fn encrypt_payload<P: Writeable>(payload: P, encrypted_tlvs_ss: [u8; 32]) -> Vec<u8> {
-       let mut writer = VecWriter(Vec::new());
-       let write_adapter = ChaChaPolyWriteAdapter::new(encrypted_tlvs_ss, &payload);
-       write_adapter.write(&mut writer).expect("In-memory writes cannot fail");
-       writer.0
-}
-
 impl Writeable for BlindedPath {
        fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
                self.introduction_node_id.write(w)?;
index 1993ad932267db8f2d2c8a98f999f2726530b833..c1ecff4b4af19155211fba89920b3852e8b22be1 100644 (file)
@@ -18,6 +18,8 @@ use bitcoin::secp256k1::ecdh::SharedSecret;
 use super::BlindedPath;
 use crate::ln::onion_utils;
 use crate::onion_message::Destination;
+use crate::util::chacha20poly1305rfc::ChaChaPolyWriteAdapter;
+use crate::util::ser::{VecWriter, Writeable};
 
 use crate::prelude::*;
 
@@ -96,3 +98,12 @@ pub(crate) fn construct_keys_callback<T: secp256k1::Signing + secp256k1::Verific
        }
        Ok(())
 }
+
+/// Encrypt TLV payload to be used as a [`crate::blinded_path::BlindedHop::encrypted_payload`].
+pub(super) fn encrypt_payload<P: Writeable>(payload: P, encrypted_tlvs_ss: [u8; 32]) -> Vec<u8> {
+       let mut writer = VecWriter(Vec::new());
+       let write_adapter = ChaChaPolyWriteAdapter::new(encrypted_tlvs_ss, &payload);
+       write_adapter.write(&mut writer).expect("In-memory writes cannot fail");
+       writer.0
+}
+