]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Use PublicKey values in construct_keys_callback
authorJeffrey Czyz <jkczyz@gmail.com>
Thu, 15 Aug 2024 22:56:17 +0000 (17:56 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Tue, 20 Aug 2024 21:35:40 +0000 (16:35 -0500)
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
lightning/src/blinded_path/payment.rs
lightning/src/blinded_path/utils.rs
lightning/src/onion_message/messenger.rs

index 93d36d621c976fe1bb3546802a56c294a8218da5..d4a118e1c990b598781ceac1be6b9a1816f69e90 100644 (file)
@@ -374,14 +374,14 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
        secp_ctx: &Secp256k1<T>, intermediate_nodes: &[ForwardNode], recipient_node_id: PublicKey,
        context: MessageContext, session_priv: &SecretKey
 ) -> Result<Vec<BlindedHop>, 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) })));
index e128e6675205c3d908bae8114cb90b9b16048504..5a143d5e6dc2c4be5497c55e5f399462753882da 100644 (file)
@@ -462,8 +462,8 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
        secp_ctx: &Secp256k1<T>, intermediate_nodes: &[ForwardNode],
        payee_node_id: PublicKey, payee_tlvs: ReceiveTlvs, session_priv: &SecretKey
 ) -> Result<Vec<BlindedHop>, 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)
index c9aa1a4039a3c75b82563641c4d4c7ae90961470..f96df7014b2511fe5bae2088120a5a7dfa72e7c5 100644 (file)
@@ -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<Item=&'a PublicKey>,
+       I: Iterator<Item=PublicKey>,
        F: FnMut(PublicKey, SharedSecret, PublicKey, [u8; 32], Option<PublicKey>, Option<Vec<u8>>),
 {
        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<Vec<BlindedHop>, secp256k1::Error>
 where
        T: secp256k1::Signing + secp256k1::Verification,
-       I1: Iterator<Item=&'a PublicKey>,
+       I1: Iterator<Item=PublicKey>,
        I2: Iterator,
        I2::Item: Writeable
 {
index 89aa4dbce682b9d9c759ca8fe892835d250feba4..4f862acfae309f682d203adfe131cc9d65c50bbc 100644 (file)
@@ -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<T: OnionMessageContents, S: secp256k1::Signing + secp256k1::Verification>(
-       secp_ctx: &Secp256k1<S>, unblinded_path: &[PublicKey], destination: Destination, message: T,
+       secp_ctx: &Secp256k1<S>, unblinded_path: Vec<PublicKey>, destination: Destination, message: T,
        mut reply_path: Option<BlindedMessagePath>, session_priv: &SecretKey
 ) -> Result<(Vec<(Payload<T>, [u8; 32])>, Vec<onion_utils::OnionKeys>), SendError> {
        let num_hops = unblinded_path.len() + destination.num_hops();
@@ -1809,7 +1809,8 @@ fn packet_payloads_and_keys<T: OnionMessageContents, S: secp256k1::Signing + sec
        let mut blinded_path_idx = 0;
        let mut prev_control_tlvs_ss = None;
        let mut final_control_tlvs = None;
-       utils::construct_keys_callback(secp_ctx, unblinded_path.iter(), Some(destination), session_priv,
+       utils::construct_keys_callback(
+               secp_ctx, unblinded_path.into_iter(), Some(destination), session_priv,
                |_, onion_packet_ss, ephemeral_pubkey, control_tlvs_ss, unblinded_pk_opt, enc_payload_opt| {
                        if num_unblinded_hops != 0 && unblinded_path_idx < num_unblinded_hops {
                                if let Some(ss) = prev_control_tlvs_ss.take() {