From 4922548bf2d925fb30d123174a0276c1cd21b300 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Thu, 15 Aug 2024 17:56:17 -0500 Subject: [PATCH] Use PublicKey values in construct_keys_callback Instead of accepting iterators yielding PublicKey by reference in utils::construct_keys_callback, take iterators yielding values since these implement Copy and need to be copied anyway. This will help avoid a situation when padding where both a reference and mutable reference are needed. --- lightning/src/blinded_path/message.rs | 6 +++--- lightning/src/blinded_path/payment.rs | 4 ++-- lightning/src/blinded_path/utils.rs | 6 +++--- lightning/src/onion_message/messenger.rs | 7 ++++--- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lightning/src/blinded_path/message.rs b/lightning/src/blinded_path/message.rs index 93d36d621..d4a118e1c 100644 --- a/lightning/src/blinded_path/message.rs +++ b/lightning/src/blinded_path/message.rs @@ -374,14 +374,14 @@ pub(super) fn blinded_hops( secp_ctx: &Secp256k1, intermediate_nodes: &[ForwardNode], recipient_node_id: PublicKey, context: MessageContext, session_priv: &SecretKey ) -> Result, secp256k1::Error> { - let pks = intermediate_nodes.iter().map(|node| &node.node_id) - .chain(core::iter::once(&recipient_node_id)); + let pks = intermediate_nodes.iter().map(|node| node.node_id) + .chain(core::iter::once(recipient_node_id)); let tlvs = pks.clone() .skip(1) // The first node's TLVs contains the next node's pubkey .zip(intermediate_nodes.iter().map(|node| node.short_channel_id)) .map(|(pubkey, scid)| match scid { Some(scid) => NextMessageHop::ShortChannelId(scid), - None => NextMessageHop::NodeId(*pubkey), + None => NextMessageHop::NodeId(pubkey), }) .map(|next_hop| ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None })) .chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs{ context: Some(context) }))); diff --git a/lightning/src/blinded_path/payment.rs b/lightning/src/blinded_path/payment.rs index e128e6675..5a143d5e6 100644 --- a/lightning/src/blinded_path/payment.rs +++ b/lightning/src/blinded_path/payment.rs @@ -462,8 +462,8 @@ pub(super) fn blinded_hops( secp_ctx: &Secp256k1, intermediate_nodes: &[ForwardNode], payee_node_id: PublicKey, payee_tlvs: ReceiveTlvs, session_priv: &SecretKey ) -> Result, secp256k1::Error> { - let pks = intermediate_nodes.iter().map(|node| &node.node_id) - .chain(core::iter::once(&payee_node_id)); + let pks = intermediate_nodes.iter().map(|node| node.node_id) + .chain(core::iter::once(payee_node_id)); let tlvs = intermediate_nodes.iter().map(|node| BlindedPaymentTlvsRef::Forward(&node.tlvs)) .chain(core::iter::once(BlindedPaymentTlvsRef::Receive(&payee_tlvs))); utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv) diff --git a/lightning/src/blinded_path/utils.rs b/lightning/src/blinded_path/utils.rs index c9aa1a403..f96df7014 100644 --- a/lightning/src/blinded_path/utils.rs +++ b/lightning/src/blinded_path/utils.rs @@ -91,13 +91,13 @@ pub(crate) fn construct_keys_callback<'a, T, I, F>( ) -> Result<(), secp256k1::Error> where T: secp256k1::Signing + secp256k1::Verification, - I: Iterator, + I: Iterator, F: FnMut(PublicKey, SharedSecret, PublicKey, [u8; 32], Option, Option>), { build_keys_helper!(session_priv, secp_ctx, callback); for pk in unblinded_path { - build_keys_in_loop!(*pk, false, None); + build_keys_in_loop!(pk, false, None); } if let Some(dest) = destination { match dest { @@ -120,7 +120,7 @@ pub(crate) fn construct_blinded_hops<'a, T, I1, I2>( ) -> Result, secp256k1::Error> where T: secp256k1::Signing + secp256k1::Verification, - I1: Iterator, + I1: Iterator, I2: Iterator, I2::Item: Writeable { diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 89aa4dbce..4f862acfa 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -925,7 +925,7 @@ where } }; let (packet_payloads, packet_keys) = packet_payloads_and_keys( - &secp_ctx, &intermediate_nodes, destination, contents, reply_path, &blinding_secret + &secp_ctx, intermediate_nodes, destination, contents, reply_path, &blinding_secret )?; let prng_seed = entropy_source.get_secure_random_bytes(); @@ -1784,7 +1784,7 @@ pub type SimpleRefOnionMessenger< /// Construct onion packet payloads and keys for sending an onion message along the given /// `unblinded_path` to the given `destination`. fn packet_payloads_and_keys( - secp_ctx: &Secp256k1, unblinded_path: &[PublicKey], destination: Destination, message: T, + secp_ctx: &Secp256k1, unblinded_path: Vec, destination: Destination, message: T, mut reply_path: Option, session_priv: &SecretKey ) -> Result<(Vec<(Payload, [u8; 32])>, Vec), SendError> { let num_hops = unblinded_path.len() + destination.num_hops(); @@ -1809,7 +1809,8 @@ fn packet_payloads_and_keys