X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fblinded_path%2Futils.rs;h=c62b4e6c2612cb52ee57321b747947d2100bd214;hb=b1abf32937db89ce9603636f2aefa3ff224c4fba;hp=a188274a5d2fccd0b2eeee67c77c16530c53e1aa;hpb=1b356619b3b178ef5a4f392056ed2fd5b7de2541;p=rust-lightning diff --git a/lightning/src/blinded_path/utils.rs b/lightning/src/blinded_path/utils.rs index a188274a..c62b4e6c 100644 --- a/lightning/src/blinded_path/utils.rs +++ b/lightning/src/blinded_path/utils.rs @@ -15,7 +15,7 @@ use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey, Scalar}; use bitcoin::secp256k1::ecdh::SharedSecret; -use super::BlindedPath; +use super::{BlindedHop, BlindedPath}; use crate::ln::msgs::DecodeError; use crate::ln::onion_utils; use crate::onion_message::Destination; @@ -27,11 +27,15 @@ use crate::prelude::*; // TODO: DRY with onion_utils::construct_onion_keys_callback #[inline] -pub(crate) fn construct_keys_callback, Option>)>( - secp_ctx: &Secp256k1, unblinded_path: &[PublicKey], destination: Option, - session_priv: &SecretKey, mut callback: FType -) -> Result<(), secp256k1::Error> { +pub(crate) fn construct_keys_callback<'a, T, I, F>( + secp_ctx: &Secp256k1, unblinded_path: I, destination: Option, + session_priv: &SecretKey, mut callback: F +) -> Result<(), secp256k1::Error> +where + T: secp256k1::Signing + secp256k1::Verification, + I: Iterator, + F: FnMut(PublicKey, SharedSecret, PublicKey, [u8; 32], Option, Option>), +{ let mut msg_blinding_point_priv = session_priv.clone(); let mut msg_blinding_point = PublicKey::from_secret_key(secp_ctx, &msg_blinding_point_priv); let mut onion_packet_pubkey_priv = msg_blinding_point_priv.clone(); @@ -101,10 +105,32 @@ pub(crate) fn construct_keys_callback( + secp_ctx: &Secp256k1, unblinded_pks: I1, mut unblinded_tlvs: I2, session_priv: &SecretKey +) -> Result, secp256k1::Error> +where + T: secp256k1::Signing + secp256k1::Verification, + I1: Iterator, + I2: Iterator, + I2::Item: Writeable +{ + let mut blinded_hops = Vec::with_capacity(unblinded_pks.size_hint().0); + construct_keys_callback( + secp_ctx, unblinded_pks, None, session_priv, + |blinded_node_id, _, _, encrypted_payload_rho, _, _| { + blinded_hops.push(BlindedHop { + blinded_node_id, + encrypted_payload: encrypt_payload(unblinded_tlvs.next().unwrap(), encrypted_payload_rho), + }); + })?; + Ok(blinded_hops) +} + /// Encrypt TLV payload to be used as a [`crate::blinded_path::BlindedHop::encrypted_payload`]. -pub(super) fn encrypt_payload(payload: P, encrypted_tlvs_ss: [u8; 32]) -> Vec { +fn encrypt_payload(payload: P, encrypted_tlvs_rho: [u8; 32]) -> Vec { let mut writer = VecWriter(Vec::new()); - let write_adapter = ChaChaPolyWriteAdapter::new(encrypted_tlvs_ss, &payload); + let write_adapter = ChaChaPolyWriteAdapter::new(encrypted_tlvs_rho, &payload); write_adapter.write(&mut writer).expect("In-memory writes cannot fail"); writer.0 }