]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Make advance_path_by_one an associated method.
authorValentine Wallace <vwallace@protonmail.com>
Thu, 15 Aug 2024 22:18:51 +0000 (18:18 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Fri, 16 Aug 2024 14:30:42 +0000 (10:30 -0400)
lightning/src/blinded_path/message.rs
lightning/src/blinded_path/payment.rs
lightning/src/ln/outbound_payment.rs
lightning/src/onion_message/messenger.rs

index acc4ac3886617aac42d5940d39aca7b1dd12da34..93d36d621c976fe1bb3546802a56c294a8218da5 100644 (file)
@@ -138,6 +138,50 @@ impl BlindedMessagePath {
                &self.0.blinded_hops
        }
 
+       /// Advance the blinded onion message path by one hop, making the second hop into the new
+       /// introduction node.
+       ///
+       /// Will only modify `self` when returning `Ok`.
+       pub fn advance_path_by_one<NS: Deref, NL: Deref, T>(
+               &mut self, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
+       ) -> Result<(), ()>
+       where
+               NS::Target: NodeSigner,
+               NL::Target: NodeIdLookUp,
+               T: secp256k1::Signing + secp256k1::Verification,
+       {
+               let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &self.0.blinding_point, None)?;
+               let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
+               let encrypted_control_tlvs = &self.0.blinded_hops.get(0).ok_or(())?.encrypted_payload;
+               let mut s = Cursor::new(encrypted_control_tlvs);
+               let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
+               match ChaChaPolyReadAdapter::read(&mut reader, rho) {
+                       Ok(ChaChaPolyReadAdapter {
+                               readable: ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override })
+                       }) => {
+                               let next_node_id = match next_hop {
+                                       NextMessageHop::NodeId(pubkey) => pubkey,
+                                       NextMessageHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid) {
+                                               Some(pubkey) => pubkey,
+                                               None => return Err(()),
+                                       },
+                               };
+                               let mut new_blinding_point = match next_blinding_override {
+                                       Some(blinding_point) => blinding_point,
+                                       None => {
+                                               onion_utils::next_hop_pubkey(secp_ctx, self.0.blinding_point,
+                                                       control_tlvs_ss.as_ref()).map_err(|_| ())?
+                                       }
+                               };
+                               mem::swap(&mut self.0.blinding_point, &mut new_blinding_point);
+                               self.0.introduction_node = IntroductionNode::NodeId(next_node_id);
+                               self.0.blinded_hops.remove(0);
+                               Ok(())
+                       },
+                       _ => Err(())
+               }
+       }
+
        pub(crate) fn introduction_node_mut(&mut self) -> &mut IntroductionNode {
                &mut self.0.introduction_node
        }
@@ -345,46 +389,3 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
        utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
 }
 
-/// Advance the blinded onion message path by one hop, making the second hop into the new
-/// introduction node.
-///
-/// Will only modify `path` when returning `Ok`.
-pub fn advance_path_by_one<NS: Deref, NL: Deref, T>(
-       path: &mut BlindedMessagePath, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
-) -> Result<(), ()>
-where
-       NS::Target: NodeSigner,
-       NL::Target: NodeIdLookUp,
-       T: secp256k1::Signing + secp256k1::Verification,
-{
-       let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.0.blinding_point, None)?;
-       let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
-       let encrypted_control_tlvs = &path.0.blinded_hops.get(0).ok_or(())?.encrypted_payload;
-       let mut s = Cursor::new(encrypted_control_tlvs);
-       let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
-       match ChaChaPolyReadAdapter::read(&mut reader, rho) {
-               Ok(ChaChaPolyReadAdapter {
-                       readable: ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override })
-               }) => {
-                       let next_node_id = match next_hop {
-                               NextMessageHop::NodeId(pubkey) => pubkey,
-                               NextMessageHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid) {
-                                       Some(pubkey) => pubkey,
-                                       None => return Err(()),
-                               },
-                       };
-                       let mut new_blinding_point = match next_blinding_override {
-                               Some(blinding_point) => blinding_point,
-                               None => {
-                                       onion_utils::next_hop_pubkey(secp_ctx, path.0.blinding_point,
-                                               control_tlvs_ss.as_ref()).map_err(|_| ())?
-                               }
-                       };
-                       mem::swap(&mut path.0.blinding_point, &mut new_blinding_point);
-                       path.0.introduction_node = IntroductionNode::NodeId(next_node_id);
-                       path.0.blinded_hops.remove(0);
-                       Ok(())
-               },
-               _ => Err(())
-       }
-}
index 56fb87d300b5809f1cee38f696951ceebdd865a8..765e0b91f05590651c9e32d3f324047c6d689f0b 100644 (file)
@@ -121,6 +121,43 @@ impl BlindedPaymentPath {
                &self.0.blinded_hops
        }
 
+       /// Advance the blinded onion payment path by one hop, making the second hop into the new
+       /// introduction node.
+       ///
+       /// Will only modify `self` when returning `Ok`.
+       pub fn advance_path_by_one<NS: Deref, NL: Deref, T>(
+               &mut self, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
+       ) -> Result<(), ()>
+       where
+               NS::Target: NodeSigner,
+               NL::Target: NodeIdLookUp,
+               T: secp256k1::Signing + secp256k1::Verification,
+       {
+               let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &self.0.blinding_point, None)?;
+               let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
+               let encrypted_control_tlvs = &self.0.blinded_hops.get(0).ok_or(())?.encrypted_payload;
+               let mut s = Cursor::new(encrypted_control_tlvs);
+               let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
+               match ChaChaPolyReadAdapter::read(&mut reader, rho) {
+                       Ok(ChaChaPolyReadAdapter {
+                               readable: BlindedPaymentTlvs::Forward(ForwardTlvs { short_channel_id, .. })
+                       }) => {
+                               let next_node_id = match node_id_lookup.next_node_id(short_channel_id) {
+                                       Some(node_id) => node_id,
+                                       None => return Err(()),
+                               };
+                               let mut new_blinding_point = onion_utils::next_hop_pubkey(
+                                       secp_ctx, self.0.blinding_point, control_tlvs_ss.as_ref()
+                               ).map_err(|_| ())?;
+                               mem::swap(&mut self.0.blinding_point, &mut new_blinding_point);
+                               self.0.introduction_node = IntroductionNode::NodeId(next_node_id);
+                               self.0.blinded_hops.remove(0);
+                               Ok(())
+                       },
+                       _ => Err(())
+               }
+       }
+
        #[cfg(any(test, fuzzing))]
        pub fn from_raw(
                introduction_node_id: PublicKey, blinding_point: PublicKey, blinded_hops: Vec<BlindedHop>
@@ -383,43 +420,6 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
        utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
 }
 
-/// Advance the blinded onion payment path by one hop, making the second hop into the new
-/// introduction node.
-///
-/// Will only modify `path` when returning `Ok`.
-pub fn advance_path_by_one<NS: Deref, NL: Deref, T>(
-       path: &mut BlindedPaymentPath, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
-) -> Result<(), ()>
-where
-       NS::Target: NodeSigner,
-       NL::Target: NodeIdLookUp,
-       T: secp256k1::Signing + secp256k1::Verification,
-{
-       let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.0.blinding_point, None)?;
-       let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
-       let encrypted_control_tlvs = &path.0.blinded_hops.get(0).ok_or(())?.encrypted_payload;
-       let mut s = Cursor::new(encrypted_control_tlvs);
-       let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
-       match ChaChaPolyReadAdapter::read(&mut reader, rho) {
-               Ok(ChaChaPolyReadAdapter {
-                       readable: BlindedPaymentTlvs::Forward(ForwardTlvs { short_channel_id, .. })
-               }) => {
-                       let next_node_id = match node_id_lookup.next_node_id(short_channel_id) {
-                               Some(node_id) => node_id,
-                               None => return Err(()),
-                       };
-                       let mut new_blinding_point = onion_utils::next_hop_pubkey(
-                               secp_ctx, path.0.blinding_point, control_tlvs_ss.as_ref()
-                       ).map_err(|_| ())?;
-                       mem::swap(&mut path.0.blinding_point, &mut new_blinding_point);
-                       path.0.introduction_node = IntroductionNode::NodeId(next_node_id);
-                       path.0.blinded_hops.remove(0);
-                       Ok(())
-               },
-               _ => Err(())
-       }
-}
-
 /// `None` if underflow occurs.
 pub(crate) fn amt_to_forward_msat(inbound_amt_msat: u64, payment_relay: &PaymentRelay) -> Option<u64> {
        let inbound_amt = inbound_amt_msat as u128;
index 3971386600dfaeff4d210a590895d43272b1f4c4..aff2367779b7d17920c41d01f0f0d56c8f382def 100644 (file)
@@ -14,7 +14,6 @@ use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
 
 use crate::blinded_path::{IntroductionNode, NodeIdLookUp};
-use crate::blinded_path::payment::advance_path_by_one;
 use crate::events::{self, PaymentFailureReason};
 use crate::ln::types::{PaymentHash, PaymentPreimage, PaymentSecret};
 use crate::ln::channel_state::ChannelDetails;
@@ -845,7 +844,7 @@ impl OutboundPayments {
                                        },
                                };
                                if introduction_node_id == our_node_id {
-                                       let _ = advance_path_by_one(path, node_signer, node_id_lookup, secp_ctx);
+                                       let _ = path.advance_path_by_one(node_signer, node_id_lookup, secp_ctx);
                                }
                        }
                }
index 8dba54d810e29d02e62a69c5e605cbb7e0405ec3..a5c81bb364efbd4e4260d4a7e1e8d6d66722a734 100644 (file)
@@ -16,7 +16,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
 
 use crate::blinded_path::{IntroductionNode, NodeIdLookUp};
-use crate::blinded_path::message::{advance_path_by_one, BlindedMessagePath, ForwardNode, ForwardTlvs, MessageContext, NextMessageHop, ReceiveTlvs};
+use crate::blinded_path::message::{BlindedMessagePath, ForwardNode, ForwardTlvs, MessageContext, NextMessageHop, ReceiveTlvs};
 use crate::blinded_path::utils;
 use crate::events::{Event, EventHandler, EventsProvider, ReplayEvent};
 use crate::sign::{EntropySource, NodeSigner, Recipient};
@@ -901,7 +901,7 @@ where
                                },
                        };
                        if introduction_node_id == our_node_id {
-                               advance_path_by_one(blinded_path, node_signer, node_id_lookup, &secp_ctx)
+                               blinded_path.advance_path_by_one(node_signer, node_id_lookup, &secp_ctx)
                                        .map_err(|()| SendError::BlindedPathAdvanceFailed)?;
                        }
                }