From: Valentine Wallace Date: Thu, 15 Aug 2024 22:18:51 +0000 (-0400) Subject: Make advance_path_by_one an associated method. X-Git-Tag: v0.0.124-beta~10^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=b204a6a11d662e58e0584856e65b412be2072ebb;p=rust-lightning Make advance_path_by_one an associated method. --- diff --git a/lightning/src/blinded_path/message.rs b/lightning/src/blinded_path/message.rs index acc4ac388..93d36d621 100644 --- a/lightning/src/blinded_path/message.rs +++ b/lightning/src/blinded_path/message.rs @@ -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( + &mut self, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1 + ) -> 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( 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( - path: &mut BlindedMessagePath, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1 -) -> 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(()) - } -} diff --git a/lightning/src/blinded_path/payment.rs b/lightning/src/blinded_path/payment.rs index 56fb87d30..765e0b91f 100644 --- a/lightning/src/blinded_path/payment.rs +++ b/lightning/src/blinded_path/payment.rs @@ -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( + &mut self, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1 + ) -> 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 @@ -383,43 +420,6 @@ pub(super) fn blinded_hops( 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( - path: &mut BlindedPaymentPath, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1 -) -> 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 { let inbound_amt = inbound_amt_msat as u128; diff --git a/lightning/src/ln/outbound_payment.rs b/lightning/src/ln/outbound_payment.rs index 397138660..aff236777 100644 --- a/lightning/src/ln/outbound_payment.rs +++ b/lightning/src/ln/outbound_payment.rs @@ -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); } } } diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 8dba54d81..a5c81bb36 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -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)?; } }