From a5b7cf2c69f2ac05279335006b1b172e2f0cb8c9 Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Fri, 16 Jun 2023 13:22:53 -0400 Subject: [PATCH] Move blinded path util into blinded_path::utils This way it can be more easily reused for blinded payment paths. --- lightning/src/blinded_path/mod.rs | 16 ++++------------ lightning/src/blinded_path/utils.rs | 11 +++++++++++ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lightning/src/blinded_path/mod.rs b/lightning/src/blinded_path/mod.rs index c52df1651..2022e06c9 100644 --- a/lightning/src/blinded_path/mod.rs +++ b/lightning/src/blinded_path/mod.rs @@ -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( }; 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( 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(payload: P, encrypted_tlvs_ss: [u8; 32]) -> Vec { - 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(&self, w: &mut W) -> Result<(), io::Error> { self.introduction_node_id.write(w)?; diff --git a/lightning/src/blinded_path/utils.rs b/lightning/src/blinded_path/utils.rs index 1993ad932..c1ecff4b4 100644 --- a/lightning/src/blinded_path/utils.rs +++ b/lightning/src/blinded_path/utils.rs @@ -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(payload: P, encrypted_tlvs_ss: [u8; 32]) -> Vec { + 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 +} + -- 2.39.5