diff --git a/lightning/src/blinded_path/message.rs b/lightning/src/blinded_path/message.rs index 51494e1b21e..a47ea8242f0 100644 --- a/lightning/src/blinded_path/message.rs +++ b/lightning/src/blinded_path/message.rs @@ -14,23 +14,23 @@ use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey}; #[allow(unused_imports)] use crate::prelude::*; -use bitcoin::hashes::hmac::Hmac; -use bitcoin::hashes::sha256::Hash as Sha256; -use crate::blinded_path::{BlindedHop, BlindedPath, Direction, IntroductionNode, NodeIdLookUp}; use crate::blinded_path::utils; +use crate::blinded_path::{BlindedHop, BlindedPath, Direction, IntroductionNode, NodeIdLookUp}; +use crate::crypto::streams::ChaChaPolyReadAdapter; use crate::io; use crate::io::Cursor; use crate::ln::channelmanager::PaymentId; use crate::ln::msgs::DecodeError; use crate::ln::onion_utils; -use crate::types::payment::PaymentHash; use crate::offers::nonce::Nonce; use crate::onion_message::packet::ControlTlvs; use crate::routing::gossip::{NodeId, ReadOnlyNetworkGraph}; use crate::sign::{EntropySource, NodeSigner, Recipient}; -use crate::crypto::streams::ChaChaPolyReadAdapter; +use crate::types::payment::PaymentHash; use crate::util::scid_utils; use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, Writeable, Writer}; +use bitcoin::hashes::hmac::Hmac; +use bitcoin::hashes::sha256::Hash as Sha256; use core::mem; use core::ops::Deref; @@ -55,8 +55,12 @@ impl Readable for BlindedMessagePath { impl BlindedMessagePath { /// Create a one-hop blinded path for a message. pub fn one_hop( - recipient_node_id: PublicKey, context: MessageContext, entropy_source: ES, secp_ctx: &Secp256k1 - ) -> Result where ES::Target: EntropySource { + recipient_node_id: PublicKey, context: MessageContext, entropy_source: ES, + secp_ctx: &Secp256k1, + ) -> Result + where + ES::Target: EntropySource, + { Self::new(&[], recipient_node_id, context, entropy_source, secp_ctx) } @@ -68,20 +72,28 @@ impl BlindedMessagePath { pub fn new( intermediate_nodes: &[MessageForwardNode], recipient_node_id: PublicKey, context: MessageContext, entropy_source: ES, secp_ctx: &Secp256k1, - ) -> Result where ES::Target: EntropySource { + ) -> Result + where + ES::Target: EntropySource, + { let introduction_node = IntroductionNode::NodeId( - intermediate_nodes.first().map_or(recipient_node_id, |n| n.node_id) + intermediate_nodes.first().map_or(recipient_node_id, |n| n.node_id), ); let blinding_secret_bytes = entropy_source.get_secure_random_bytes(); - let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted"); + let blinding_secret = + SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted"); Ok(Self(BlindedPath { introduction_node, blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret), blinded_hops: blinded_hops( - secp_ctx, intermediate_nodes, recipient_node_id, - context, &blinding_secret, - ).map_err(|_| ())?, + secp_ctx, + intermediate_nodes, + recipient_node_id, + context, + &blinding_secret, + ) + .map_err(|_| ())?, })) } @@ -97,9 +109,9 @@ impl BlindedMessagePath { if let Some(node_info) = network_graph.node(&node_id) { if let Some((scid, channel_info)) = node_info .channels - .iter() - .filter_map(|scid| network_graph.channel(*scid).map(|info| (*scid, info))) - .min_by_key(|(scid, _)| scid_utils::block_from_scid(*scid)) + .iter() + .filter_map(|scid| network_graph.channel(*scid).map(|info| (*scid, info))) + .min_by_key(|(scid, _)| scid_utils::block_from_scid(*scid)) { let direction = if node_id == channel_info.node_one { Direction::NodeOne @@ -117,7 +129,7 @@ impl BlindedMessagePath { /// Returns the introduction [`NodeId`] of the blinded path, if it is publicly reachable (i.e., /// it is found in the network graph). pub fn public_introduction_node_id<'a>( - &self, network_graph: &'a ReadOnlyNetworkGraph + &self, network_graph: &'a ReadOnlyNetworkGraph, ) -> Option<&'a NodeId> { self.0.public_introduction_node_id(network_graph) } @@ -144,7 +156,7 @@ impl BlindedMessagePath { /// /// 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 + &mut self, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1, ) -> Result<(), ()> where NS::Target: NodeSigner, @@ -158,28 +170,31 @@ impl BlindedMessagePath { 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 }) + 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) { + 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(|_| ())? - } + 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(()) + _ => Err(()), } } @@ -189,7 +204,7 @@ impl BlindedMessagePath { #[cfg(test)] pub fn from_raw( - introduction_node_id: PublicKey, blinding_point: PublicKey, blinded_hops: Vec + introduction_node_id: PublicKey, blinding_point: PublicKey, blinded_hops: Vec, ) -> Self { Self(BlindedPath { introduction_node: IntroductionNode::NodeId(introduction_node_id), @@ -241,7 +256,7 @@ pub(crate) struct ReceiveTlvs { /// If `context` is `Some`, it is used to identify the blinded path that this onion message is /// sending to. This is useful for receivers to check that said blinded path is being used in /// the right context. - pub context: Option + pub context: Option, } impl Writeable for ForwardTlvs { @@ -480,20 +495,24 @@ pub(super) fn blinded_hops( secp_ctx: &Secp256k1, intermediate_nodes: &[MessageForwardNode], recipient_node_id: PublicKey, context: MessageContext, session_priv: &SecretKey, ) -> Result, secp256k1::Error> { - let pks = intermediate_nodes.iter().map(|node| node.node_id) + let pks = intermediate_nodes + .iter() + .map(|node| node.node_id) .chain(core::iter::once(recipient_node_id)); - let tlvs = pks.clone() + 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), }) - .map(|next_hop| ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None })) - .chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs{ context: Some(context) }))); + .map(|next_hop| { + ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None }) + }) + .chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { context: Some(context) }))); let path = pks.zip(tlvs); utils::construct_blinded_hops(secp_ctx, path, session_priv) } - diff --git a/lightning/src/blinded_path/mod.rs b/lightning/src/blinded_path/mod.rs index 168bc7174d6..2f9b1b9a411 100644 --- a/lightning/src/blinded_path/mod.rs +++ b/lightning/src/blinded_path/mod.rs @@ -9,8 +9,8 @@ //! Creating blinded paths and related utilities live here. -pub mod payment; pub mod message; +pub mod payment; pub(crate) mod utils; use bitcoin::secp256k1::PublicKey; @@ -90,7 +90,9 @@ impl NodeIdLookUp for EmptyNodeIdLookUp { impl Deref for EmptyNodeIdLookUp { type Target = EmptyNodeIdLookUp; - fn deref(&self) -> &Self { self } + fn deref(&self) -> &Self { + self + } } /// An encrypted payload and node id corresponding to a hop in a payment or onion message path, to @@ -108,7 +110,7 @@ pub struct BlindedHop { impl BlindedPath { pub(super) fn public_introduction_node_id<'a>( - &self, network_graph: &'a ReadOnlyNetworkGraph + &self, network_graph: &'a ReadOnlyNetworkGraph, ) -> Option<&'a NodeId> { match &self.introduction_node { IntroductionNode::NodeId(pubkey) => { @@ -116,12 +118,10 @@ impl BlindedPath { network_graph.nodes().get_key_value(&node_id).map(|(key, _)| key) }, IntroductionNode::DirectedShortChannelId(direction, scid) => { - network_graph - .channel(*scid) - .map(|c| match direction { - Direction::NodeOne => &c.node_one, - Direction::NodeTwo => &c.node_two, - }) + network_graph.channel(*scid).map(|c| match direction { + Direction::NodeOne => &c.node_one, + Direction::NodeTwo => &c.node_two, + }) }, } } @@ -155,7 +155,7 @@ impl Readable for BlindedPath { let introduction_node = match first_byte { 0 => IntroductionNode::DirectedShortChannelId(Direction::NodeOne, Readable::read(r)?), 1 => IntroductionNode::DirectedShortChannelId(Direction::NodeTwo, Readable::read(r)?), - 2|3 => { + 2 | 3 => { let mut bytes = [0; 33]; bytes[0] = first_byte; r.read_exact(&mut bytes[1..])?; @@ -165,16 +165,14 @@ impl Readable for BlindedPath { }; let blinding_point = Readable::read(r)?; let num_hops: u8 = Readable::read(r)?; - if num_hops == 0 { return Err(DecodeError::InvalidValue) } + if num_hops == 0 { + return Err(DecodeError::InvalidValue); + } let mut blinded_hops: Vec = Vec::with_capacity(num_hops.into()); for _ in 0..num_hops { blinded_hops.push(Readable::read(r)?); } - Ok(BlindedPath { - introduction_node, - blinding_point, - blinded_hops, - }) + Ok(BlindedPath { introduction_node, blinding_point, blinded_hops }) } } diff --git a/lightning/src/blinded_path/payment.rs b/lightning/src/blinded_path/payment.rs index cf5af7d784e..7302ab5ed1d 100644 --- a/lightning/src/blinded_path/payment.rs +++ b/lightning/src/blinded_path/payment.rs @@ -13,15 +13,13 @@ use bitcoin::hashes::hmac::Hmac; use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey}; -use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NodeIdLookUp}; use crate::blinded_path::utils; +use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NodeIdLookUp}; use crate::crypto::streams::ChaChaPolyReadAdapter; use crate::io; use crate::io::Cursor; -use crate::types::payment::PaymentSecret; use crate::ln::channel_state::CounterpartyForwardingInfo; use crate::ln::channelmanager::Verification; -use crate::types::features::BlindedHopFeatures; use crate::ln::inbound_payment::ExpandedKey; use crate::ln::msgs::DecodeError; use crate::ln::onion_utils; @@ -30,7 +28,12 @@ use crate::offers::nonce::Nonce; use crate::offers::offer::OfferId; use crate::routing::gossip::{NodeId, ReadOnlyNetworkGraph}; use crate::sign::{EntropySource, NodeSigner, Recipient}; -use crate::util::ser::{FixedLengthReader, LengthReadableArgs, HighZeroBytesDroppedBigSize, Readable, WithoutLength, Writeable, Writer}; +use crate::types::features::BlindedHopFeatures; +use crate::types::payment::PaymentSecret; +use crate::util::ser::{ + FixedLengthReader, HighZeroBytesDroppedBigSize, LengthReadableArgs, Readable, WithoutLength, + Writeable, Writer, +}; use core::mem; use core::ops::Deref; @@ -89,14 +92,22 @@ impl BlindedPaymentPath { /// Create a one-hop blinded path for a payment. pub fn one_hop( payee_node_id: PublicKey, payee_tlvs: ReceiveTlvs, min_final_cltv_expiry_delta: u16, - entropy_source: ES, secp_ctx: &Secp256k1 - ) -> Result where ES::Target: EntropySource { + entropy_source: ES, secp_ctx: &Secp256k1, + ) -> Result + where + ES::Target: EntropySource, + { // This value is not considered in pathfinding for 1-hop blinded paths, because it's intended to // be in relation to a specific channel. let htlc_maximum_msat = u64::max_value(); Self::new( - &[], payee_node_id, payee_tlvs, htlc_maximum_msat, min_final_cltv_expiry_delta, - entropy_source, secp_ctx + &[], + payee_node_id, + payee_tlvs, + htlc_maximum_msat, + min_final_cltv_expiry_delta, + entropy_source, + secp_ctx, ) } @@ -111,32 +122,44 @@ impl BlindedPaymentPath { intermediate_nodes: &[PaymentForwardNode], payee_node_id: PublicKey, payee_tlvs: ReceiveTlvs, htlc_maximum_msat: u64, min_final_cltv_expiry_delta: u16, entropy_source: ES, secp_ctx: &Secp256k1, - ) -> Result where ES::Target: EntropySource { + ) -> Result + where + ES::Target: EntropySource, + { let introduction_node = IntroductionNode::NodeId( - intermediate_nodes.first().map_or(payee_node_id, |n| n.node_id) + intermediate_nodes.first().map_or(payee_node_id, |n| n.node_id), ); let blinding_secret_bytes = entropy_source.get_secure_random_bytes(); - let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted"); + let blinding_secret = + SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted"); let blinded_payinfo = compute_payinfo( - intermediate_nodes, &payee_tlvs.tlvs, htlc_maximum_msat, min_final_cltv_expiry_delta + intermediate_nodes, + &payee_tlvs.tlvs, + htlc_maximum_msat, + min_final_cltv_expiry_delta, )?; Ok(Self { inner_path: BlindedPath { introduction_node, blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret), blinded_hops: blinded_hops( - secp_ctx, intermediate_nodes, payee_node_id, payee_tlvs, &blinding_secret - ).map_err(|_| ())?, + secp_ctx, + intermediate_nodes, + payee_node_id, + payee_tlvs, + &blinding_secret, + ) + .map_err(|_| ())?, }, - payinfo: blinded_payinfo + payinfo: blinded_payinfo, }) } /// Returns the introduction [`NodeId`] of the blinded path, if it is publicly reachable (i.e., /// it is found in the network graph). pub fn public_introduction_node_id<'a>( - &self, network_graph: &'a ReadOnlyNetworkGraph + &self, network_graph: &'a ReadOnlyNetworkGraph, ) -> Option<&'a NodeId> { self.inner_path.public_introduction_node_id(network_graph) } @@ -163,35 +186,40 @@ impl BlindedPaymentPath { /// /// 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 + &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.inner_path.blinding_point, None)?; + let control_tlvs_ss = + node_signer.ecdh(Recipient::Node, &self.inner_path.blinding_point, None)?; let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes()); - let encrypted_control_tlvs = &self.inner_path.blinded_hops.get(0).ok_or(())?.encrypted_payload; + let encrypted_control_tlvs = + &self.inner_path.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, .. }) + 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.inner_path.blinding_point, control_tlvs_ss.as_ref() - ).map_err(|_| ())?; + secp_ctx, + self.inner_path.blinding_point, + control_tlvs_ss.as_ref(), + ) + .map_err(|_| ())?; mem::swap(&mut self.inner_path.blinding_point, &mut new_blinding_point); self.inner_path.introduction_node = IntroductionNode::NodeId(next_node_id); self.inner_path.blinded_hops.remove(0); Ok(()) }, - _ => Err(()) + _ => Err(()), } } @@ -206,7 +234,7 @@ impl BlindedPaymentPath { #[cfg(any(test, fuzzing))] pub fn from_raw( introduction_node_id: PublicKey, blinding_point: PublicKey, blinded_hops: Vec, - payinfo: BlindedPayInfo + payinfo: BlindedPayInfo, ) -> Self { Self { inner_path: BlindedPath { @@ -214,7 +242,7 @@ impl BlindedPaymentPath { blinding_point, blinded_hops, }, - payinfo + payinfo, } } @@ -406,7 +434,9 @@ impl TryFrom for PaymentRelay { fn try_from(info: CounterpartyForwardingInfo) -> Result { let CounterpartyForwardingInfo { - fee_base_msat, fee_proportional_millionths, cltv_expiry_delta + fee_base_msat, + fee_proportional_millionths, + cltv_expiry_delta, } = info; // Avoid exposing esoteric CLTV expiry deltas @@ -424,9 +454,11 @@ impl TryFrom for PaymentRelay { impl Writeable for ForwardTlvs { fn write(&self, w: &mut W) -> Result<(), io::Error> { - let features_opt = - if self.features == BlindedHopFeatures::empty() { None } - else { Some(WithoutLength(&self.features)) }; + let features_opt = if self.features == BlindedHopFeatures::empty() { + None + } else { + Some(WithoutLength(&self.features)) + }; encode_tlv_stream!(w, { (2, self.short_channel_id, required), (10, self.payment_relay, required), @@ -488,7 +520,7 @@ impl Readable for BlindedPaymentTlvs { if let Some(short_channel_id) = scid { if payment_secret.is_some() { - return Err(DecodeError::InvalidValue) + return Err(DecodeError::InvalidValue); } Ok(BlindedPaymentTlvs::Forward(ForwardTlvs { short_channel_id, @@ -498,7 +530,9 @@ impl Readable for BlindedPaymentTlvs { features: features.unwrap_or_else(BlindedHopFeatures::empty), })) } else { - if payment_relay.is_some() || features.is_some() { return Err(DecodeError::InvalidValue) } + if payment_relay.is_some() || features.is_some() { + return Err(DecodeError::InvalidValue); + } Ok(BlindedPaymentTlvs::Receive(ReceiveTlvs { tlvs: UnauthenticatedReceiveTlvs { payment_secret: payment_secret.ok_or(DecodeError::InvalidValue)?, @@ -513,12 +547,14 @@ impl Readable for BlindedPaymentTlvs { /// Construct blinded payment hops for the given `intermediate_nodes` and payee info. pub(super) fn blinded_hops( - secp_ctx: &Secp256k1, intermediate_nodes: &[PaymentForwardNode], - payee_node_id: PublicKey, payee_tlvs: ReceiveTlvs, session_priv: &SecretKey, + secp_ctx: &Secp256k1, intermediate_nodes: &[PaymentForwardNode], 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 tlvs = intermediate_nodes.iter().map(|node| BlindedPaymentTlvsRef::Forward(&node.tlvs)) + 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))); let path = pks.zip(tlvs); @@ -527,7 +563,9 @@ pub(super) fn blinded_hops( } /// `None` if underflow occurs. -pub(crate) fn amt_to_forward_msat(inbound_amt_msat: u64, payment_relay: &PaymentRelay) -> Option { +pub(crate) fn amt_to_forward_msat( + inbound_amt_msat: u64, payment_relay: &PaymentRelay, +) -> Option { let inbound_amt = inbound_amt_msat as u128; let base = payment_relay.fee_base_msat as u128; let prop = payment_relay.fee_proportional_millionths as u128; @@ -557,26 +595,31 @@ pub(super) fn compute_payinfo( for tlvs in intermediate_nodes.iter().rev().map(|n| &n.tlvs) { // In the future, we'll want to take the intersection of all supported features for the // `BlindedPayInfo`, but there are no features in that context right now. - if tlvs.features.requires_unknown_bits_from(&BlindedHopFeatures::empty()) { return Err(()) } + if tlvs.features.requires_unknown_bits_from(&BlindedHopFeatures::empty()) { + return Err(()); + } let next_base_fee = tlvs.payment_relay.fee_base_msat as u64; let next_prop_mil = tlvs.payment_relay.fee_proportional_millionths as u64; // Use integer arithmetic to compute `ceil(a/b)` as `(a+b-1)/b` // ((curr_base_fee * (1_000_000 + next_prop_mil)) / 1_000_000) + next_base_fee - curr_base_fee = curr_base_fee.checked_mul(1_000_000 + next_prop_mil) + curr_base_fee = curr_base_fee + .checked_mul(1_000_000 + next_prop_mil) .and_then(|f| f.checked_add(1_000_000 - 1)) .map(|f| f / 1_000_000) .and_then(|f| f.checked_add(next_base_fee)) .ok_or(())?; // ceil(((curr_prop_mil + 1_000_000) * (next_prop_mil + 1_000_000)) / 1_000_000) - 1_000_000 - curr_prop_mil = curr_prop_mil.checked_add(1_000_000) + curr_prop_mil = curr_prop_mil + .checked_add(1_000_000) .and_then(|f1| next_prop_mil.checked_add(1_000_000).and_then(|f2| f2.checked_mul(f1))) .and_then(|f| f.checked_add(1_000_000 - 1)) .map(|f| f / 1_000_000) .and_then(|f| f.checked_sub(1_000_000)) .ok_or(())?; - cltv_expiry_delta = cltv_expiry_delta.checked_add(tlvs.payment_relay.cltv_expiry_delta).ok_or(())?; + cltv_expiry_delta = + cltv_expiry_delta.checked_add(tlvs.payment_relay.cltv_expiry_delta).ok_or(())?; } let mut htlc_minimum_msat: u64 = 1; @@ -587,18 +630,22 @@ pub(super) fn compute_payinfo( // in the amount that this node receives and contribute towards reaching its min. htlc_minimum_msat = amt_to_forward_msat( core::cmp::max(node.tlvs.payment_constraints.htlc_minimum_msat, htlc_minimum_msat), - &node.tlvs.payment_relay - ).unwrap_or(1); // If underflow occurs, we definitely reached this node's min + &node.tlvs.payment_relay, + ) + .unwrap_or(1); // If underflow occurs, we definitely reached this node's min htlc_maximum_msat = amt_to_forward_msat( - core::cmp::min(node.htlc_maximum_msat, htlc_maximum_msat), &node.tlvs.payment_relay - ).ok_or(())?; // If underflow occurs, we cannot send to this hop without exceeding their max + core::cmp::min(node.htlc_maximum_msat, htlc_maximum_msat), + &node.tlvs.payment_relay, + ) + .ok_or(())?; // If underflow occurs, we cannot send to this hop without exceeding their max } - htlc_minimum_msat = core::cmp::max( - payee_tlvs.payment_constraints.htlc_minimum_msat, htlc_minimum_msat - ); + htlc_minimum_msat = + core::cmp::max(payee_tlvs.payment_constraints.htlc_minimum_msat, htlc_minimum_msat); htlc_maximum_msat = core::cmp::min(payee_htlc_maximum_msat, htlc_maximum_msat); - if htlc_maximum_msat < htlc_minimum_msat { return Err(()) } + if htlc_maximum_msat < htlc_minimum_msat { + return Err(()); + } Ok(BlindedPayInfo { fee_base_msat: u32::try_from(curr_base_fee).map_err(|_| ())?, fee_proportional_millionths: u32::try_from(curr_prop_mil).map_err(|_| ())?, @@ -677,62 +724,67 @@ impl_writeable_tlv_based!(Bolt12RefundContext, {}); #[cfg(test)] mod tests { - use bitcoin::secp256k1::PublicKey; - use crate::blinded_path::payment::{Bolt12RefundContext, PaymentForwardNode, ForwardTlvs, PaymentConstraints, PaymentContext, PaymentRelay, UnauthenticatedReceiveTlvs}; - use crate::types::payment::PaymentSecret; - use crate::types::features::BlindedHopFeatures; + use crate::blinded_path::payment::{ + Bolt12RefundContext, ForwardTlvs, PaymentConstraints, PaymentContext, PaymentForwardNode, + PaymentRelay, UnauthenticatedReceiveTlvs, + }; use crate::ln::functional_test_utils::TEST_FINAL_CLTV; + use crate::types::features::BlindedHopFeatures; + use crate::types::payment::PaymentSecret; + use bitcoin::secp256k1::PublicKey; #[test] fn compute_payinfo() { // Taken from the spec example for aggregating blinded payment info. See // https://github.com/lightning/bolts/blob/master/proposals/route-blinding.md#blinded-payments let dummy_pk = PublicKey::from_slice(&[2; 33]).unwrap(); - let intermediate_nodes = vec![PaymentForwardNode { - node_id: dummy_pk, - tlvs: ForwardTlvs { - short_channel_id: 0, - payment_relay: PaymentRelay { - cltv_expiry_delta: 144, - fee_proportional_millionths: 500, - fee_base_msat: 100, + let intermediate_nodes = vec![ + PaymentForwardNode { + node_id: dummy_pk, + tlvs: ForwardTlvs { + short_channel_id: 0, + payment_relay: PaymentRelay { + cltv_expiry_delta: 144, + fee_proportional_millionths: 500, + fee_base_msat: 100, + }, + payment_constraints: PaymentConstraints { + max_cltv_expiry: 0, + htlc_minimum_msat: 100, + }, + next_blinding_override: None, + features: BlindedHopFeatures::empty(), }, - payment_constraints: PaymentConstraints { - max_cltv_expiry: 0, - htlc_minimum_msat: 100, - }, - next_blinding_override: None, - features: BlindedHopFeatures::empty(), + htlc_maximum_msat: u64::max_value(), }, - htlc_maximum_msat: u64::max_value(), - }, PaymentForwardNode { - node_id: dummy_pk, - tlvs: ForwardTlvs { - short_channel_id: 0, - payment_relay: PaymentRelay { - cltv_expiry_delta: 144, - fee_proportional_millionths: 500, - fee_base_msat: 100, - }, - payment_constraints: PaymentConstraints { - max_cltv_expiry: 0, - htlc_minimum_msat: 1_000, + PaymentForwardNode { + node_id: dummy_pk, + tlvs: ForwardTlvs { + short_channel_id: 0, + payment_relay: PaymentRelay { + cltv_expiry_delta: 144, + fee_proportional_millionths: 500, + fee_base_msat: 100, + }, + payment_constraints: PaymentConstraints { + max_cltv_expiry: 0, + htlc_minimum_msat: 1_000, + }, + next_blinding_override: None, + features: BlindedHopFeatures::empty(), }, - next_blinding_override: None, - features: BlindedHopFeatures::empty(), + htlc_maximum_msat: u64::max_value(), }, - htlc_maximum_msat: u64::max_value(), - }]; + ]; let recv_tlvs = UnauthenticatedReceiveTlvs { payment_secret: PaymentSecret([0; 32]), - payment_constraints: PaymentConstraints { - max_cltv_expiry: 0, - htlc_minimum_msat: 1, - }, + payment_constraints: PaymentConstraints { max_cltv_expiry: 0, htlc_minimum_msat: 1 }, payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}), }; let htlc_maximum_msat = 100_000; - let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat, 12).unwrap(); + let blinded_payinfo = + super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat, 12) + .unwrap(); assert_eq!(blinded_payinfo.fee_base_msat, 201); assert_eq!(blinded_payinfo.fee_proportional_millionths, 1001); assert_eq!(blinded_payinfo.cltv_expiry_delta, 300); @@ -744,13 +796,11 @@ mod tests { fn compute_payinfo_1_hop() { let recv_tlvs = UnauthenticatedReceiveTlvs { payment_secret: PaymentSecret([0; 32]), - payment_constraints: PaymentConstraints { - max_cltv_expiry: 0, - htlc_minimum_msat: 1, - }, + payment_constraints: PaymentConstraints { max_cltv_expiry: 0, htlc_minimum_msat: 1 }, payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}), }; - let blinded_payinfo = super::compute_payinfo(&[], &recv_tlvs, 4242, TEST_FINAL_CLTV as u16).unwrap(); + let blinded_payinfo = + super::compute_payinfo(&[], &recv_tlvs, 4242, TEST_FINAL_CLTV as u16).unwrap(); assert_eq!(blinded_payinfo.fee_base_msat, 0); assert_eq!(blinded_payinfo.fee_proportional_millionths, 0); assert_eq!(blinded_payinfo.cltv_expiry_delta, TEST_FINAL_CLTV as u16); @@ -763,51 +813,57 @@ mod tests { // If no hops charge fees, the htlc_minimum_msat should just be the maximum htlc_minimum_msat // along the path. let dummy_pk = PublicKey::from_slice(&[2; 33]).unwrap(); - let intermediate_nodes = vec![PaymentForwardNode { - node_id: dummy_pk, - tlvs: ForwardTlvs { - short_channel_id: 0, - payment_relay: PaymentRelay { - cltv_expiry_delta: 0, - fee_proportional_millionths: 0, - fee_base_msat: 0, - }, - payment_constraints: PaymentConstraints { - max_cltv_expiry: 0, - htlc_minimum_msat: 1, + let intermediate_nodes = vec![ + PaymentForwardNode { + node_id: dummy_pk, + tlvs: ForwardTlvs { + short_channel_id: 0, + payment_relay: PaymentRelay { + cltv_expiry_delta: 0, + fee_proportional_millionths: 0, + fee_base_msat: 0, + }, + payment_constraints: PaymentConstraints { + max_cltv_expiry: 0, + htlc_minimum_msat: 1, + }, + next_blinding_override: None, + features: BlindedHopFeatures::empty(), }, - next_blinding_override: None, - features: BlindedHopFeatures::empty(), + htlc_maximum_msat: u64::max_value(), }, - htlc_maximum_msat: u64::max_value() - }, PaymentForwardNode { - node_id: dummy_pk, - tlvs: ForwardTlvs { - short_channel_id: 0, - payment_relay: PaymentRelay { - cltv_expiry_delta: 0, - fee_proportional_millionths: 0, - fee_base_msat: 0, + PaymentForwardNode { + node_id: dummy_pk, + tlvs: ForwardTlvs { + short_channel_id: 0, + payment_relay: PaymentRelay { + cltv_expiry_delta: 0, + fee_proportional_millionths: 0, + fee_base_msat: 0, + }, + payment_constraints: PaymentConstraints { + max_cltv_expiry: 0, + htlc_minimum_msat: 2_000, + }, + next_blinding_override: None, + features: BlindedHopFeatures::empty(), }, - payment_constraints: PaymentConstraints { - max_cltv_expiry: 0, - htlc_minimum_msat: 2_000, - }, - next_blinding_override: None, - features: BlindedHopFeatures::empty(), + htlc_maximum_msat: u64::max_value(), }, - htlc_maximum_msat: u64::max_value() - }]; + ]; let recv_tlvs = UnauthenticatedReceiveTlvs { payment_secret: PaymentSecret([0; 32]), - payment_constraints: PaymentConstraints { - max_cltv_expiry: 0, - htlc_minimum_msat: 3, - }, + payment_constraints: PaymentConstraints { max_cltv_expiry: 0, htlc_minimum_msat: 3 }, payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}), }; let htlc_maximum_msat = 100_000; - let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat, TEST_FINAL_CLTV as u16).unwrap(); + let blinded_payinfo = super::compute_payinfo( + &intermediate_nodes[..], + &recv_tlvs, + htlc_maximum_msat, + TEST_FINAL_CLTV as u16, + ) + .unwrap(); assert_eq!(blinded_payinfo.htlc_minimum_msat, 2_000); } @@ -816,54 +872,66 @@ mod tests { // Create a path with varying fees and htlc_mins, and make sure htlc_minimum_msat ends up as the // max (htlc_min - following_fees) along the path. let dummy_pk = PublicKey::from_slice(&[2; 33]).unwrap(); - let intermediate_nodes = vec![PaymentForwardNode { - node_id: dummy_pk, - tlvs: ForwardTlvs { - short_channel_id: 0, - payment_relay: PaymentRelay { - cltv_expiry_delta: 0, - fee_proportional_millionths: 500, - fee_base_msat: 1_000, - }, - payment_constraints: PaymentConstraints { - max_cltv_expiry: 0, - htlc_minimum_msat: 5_000, + let intermediate_nodes = vec![ + PaymentForwardNode { + node_id: dummy_pk, + tlvs: ForwardTlvs { + short_channel_id: 0, + payment_relay: PaymentRelay { + cltv_expiry_delta: 0, + fee_proportional_millionths: 500, + fee_base_msat: 1_000, + }, + payment_constraints: PaymentConstraints { + max_cltv_expiry: 0, + htlc_minimum_msat: 5_000, + }, + next_blinding_override: None, + features: BlindedHopFeatures::empty(), }, - next_blinding_override: None, - features: BlindedHopFeatures::empty(), + htlc_maximum_msat: u64::max_value(), }, - htlc_maximum_msat: u64::max_value() - }, PaymentForwardNode { - node_id: dummy_pk, - tlvs: ForwardTlvs { - short_channel_id: 0, - payment_relay: PaymentRelay { - cltv_expiry_delta: 0, - fee_proportional_millionths: 500, - fee_base_msat: 200, - }, - payment_constraints: PaymentConstraints { - max_cltv_expiry: 0, - htlc_minimum_msat: 2_000, + PaymentForwardNode { + node_id: dummy_pk, + tlvs: ForwardTlvs { + short_channel_id: 0, + payment_relay: PaymentRelay { + cltv_expiry_delta: 0, + fee_proportional_millionths: 500, + fee_base_msat: 200, + }, + payment_constraints: PaymentConstraints { + max_cltv_expiry: 0, + htlc_minimum_msat: 2_000, + }, + next_blinding_override: None, + features: BlindedHopFeatures::empty(), }, - next_blinding_override: None, - features: BlindedHopFeatures::empty(), + htlc_maximum_msat: u64::max_value(), }, - htlc_maximum_msat: u64::max_value() - }]; + ]; let recv_tlvs = UnauthenticatedReceiveTlvs { payment_secret: PaymentSecret([0; 32]), - payment_constraints: PaymentConstraints { - max_cltv_expiry: 0, - htlc_minimum_msat: 1, - }, + payment_constraints: PaymentConstraints { max_cltv_expiry: 0, htlc_minimum_msat: 1 }, payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}), }; let htlc_minimum_msat = 3798; - assert!(super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_minimum_msat - 1, TEST_FINAL_CLTV as u16).is_err()); + assert!(super::compute_payinfo( + &intermediate_nodes[..], + &recv_tlvs, + htlc_minimum_msat - 1, + TEST_FINAL_CLTV as u16 + ) + .is_err()); let htlc_maximum_msat = htlc_minimum_msat + 1; - let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat, TEST_FINAL_CLTV as u16).unwrap(); + let blinded_payinfo = super::compute_payinfo( + &intermediate_nodes[..], + &recv_tlvs, + htlc_maximum_msat, + TEST_FINAL_CLTV as u16, + ) + .unwrap(); assert_eq!(blinded_payinfo.htlc_minimum_msat, htlc_minimum_msat); assert_eq!(blinded_payinfo.htlc_maximum_msat, htlc_maximum_msat); } @@ -873,51 +941,57 @@ mod tests { // Create a path with varying fees and `htlc_maximum_msat`s, and make sure the aggregated max // htlc ends up as the min (htlc_max - following_fees) along the path. let dummy_pk = PublicKey::from_slice(&[2; 33]).unwrap(); - let intermediate_nodes = vec![PaymentForwardNode { - node_id: dummy_pk, - tlvs: ForwardTlvs { - short_channel_id: 0, - payment_relay: PaymentRelay { - cltv_expiry_delta: 0, - fee_proportional_millionths: 500, - fee_base_msat: 1_000, - }, - payment_constraints: PaymentConstraints { - max_cltv_expiry: 0, - htlc_minimum_msat: 1, + let intermediate_nodes = vec![ + PaymentForwardNode { + node_id: dummy_pk, + tlvs: ForwardTlvs { + short_channel_id: 0, + payment_relay: PaymentRelay { + cltv_expiry_delta: 0, + fee_proportional_millionths: 500, + fee_base_msat: 1_000, + }, + payment_constraints: PaymentConstraints { + max_cltv_expiry: 0, + htlc_minimum_msat: 1, + }, + next_blinding_override: None, + features: BlindedHopFeatures::empty(), }, - next_blinding_override: None, - features: BlindedHopFeatures::empty(), + htlc_maximum_msat: 5_000, }, - htlc_maximum_msat: 5_000, - }, PaymentForwardNode { - node_id: dummy_pk, - tlvs: ForwardTlvs { - short_channel_id: 0, - payment_relay: PaymentRelay { - cltv_expiry_delta: 0, - fee_proportional_millionths: 500, - fee_base_msat: 1, - }, - payment_constraints: PaymentConstraints { - max_cltv_expiry: 0, - htlc_minimum_msat: 1, + PaymentForwardNode { + node_id: dummy_pk, + tlvs: ForwardTlvs { + short_channel_id: 0, + payment_relay: PaymentRelay { + cltv_expiry_delta: 0, + fee_proportional_millionths: 500, + fee_base_msat: 1, + }, + payment_constraints: PaymentConstraints { + max_cltv_expiry: 0, + htlc_minimum_msat: 1, + }, + next_blinding_override: None, + features: BlindedHopFeatures::empty(), }, - next_blinding_override: None, - features: BlindedHopFeatures::empty(), + htlc_maximum_msat: 10_000, }, - htlc_maximum_msat: 10_000 - }]; + ]; let recv_tlvs = UnauthenticatedReceiveTlvs { payment_secret: PaymentSecret([0; 32]), - payment_constraints: PaymentConstraints { - max_cltv_expiry: 0, - htlc_minimum_msat: 1, - }, + payment_constraints: PaymentConstraints { max_cltv_expiry: 0, htlc_minimum_msat: 1 }, payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}), }; - let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, 10_000, TEST_FINAL_CLTV as u16).unwrap(); + let blinded_payinfo = super::compute_payinfo( + &intermediate_nodes[..], + &recv_tlvs, + 10_000, + TEST_FINAL_CLTV as u16, + ) + .unwrap(); assert_eq!(blinded_payinfo.htlc_maximum_msat, 3997); } } diff --git a/lightning/src/blinded_path/utils.rs b/lightning/src/blinded_path/utils.rs index 7472f8f393f..25000e84650 100644 --- a/lightning/src/blinded_path/utils.rs +++ b/lightning/src/blinded_path/utils.rs @@ -9,18 +9,18 @@ //! Onion message utility methods live here. -use bitcoin::hashes::{Hash, HashEngine}; use bitcoin::hashes::hmac::{Hmac, HmacEngine}; use bitcoin::hashes::sha256::Hash as Sha256; -use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey, Scalar}; +use bitcoin::hashes::{Hash, HashEngine}; use bitcoin::secp256k1::ecdh::SharedSecret; +use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey}; -use super::{BlindedHop, BlindedPath}; use super::message::BlindedMessagePath; +use super::{BlindedHop, BlindedPath}; +use crate::crypto::streams::ChaChaPolyWriteAdapter; use crate::ln::msgs::DecodeError; use crate::ln::onion_utils; use crate::onion_message::messenger::Destination; -use crate::crypto::streams::ChaChaPolyWriteAdapter; use crate::util::ser::{Readable, Writeable}; use crate::io; @@ -32,60 +32,80 @@ use crate::prelude::*; // TODO: DRY with onion_utils::construct_onion_keys_callback macro_rules! build_keys_helper { - ($session_priv: ident, $secp_ctx: ident, $callback: ident) => -{ - 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(); - let mut onion_packet_pubkey = msg_blinding_point.clone(); - - macro_rules! build_keys { - ($hop: expr, $blinded: expr, $encrypted_payload: expr) => {{ - let pk = *$hop.borrow(); - let encrypted_data_ss = SharedSecret::new(&pk, &msg_blinding_point_priv); - - let blinded_hop_pk = if $blinded { pk } else { - let hop_pk_blinding_factor = { - let mut hmac = HmacEngine::::new(b"blinded_node_id"); - hmac.input(encrypted_data_ss.as_ref()); - Hmac::from_engine(hmac).to_byte_array() + ($session_priv: ident, $secp_ctx: ident, $callback: ident) => { + 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(); + let mut onion_packet_pubkey = msg_blinding_point.clone(); + + macro_rules! build_keys { + ($hop: expr, $blinded: expr, $encrypted_payload: expr) => {{ + let pk = *$hop.borrow(); + let encrypted_data_ss = SharedSecret::new(&pk, &msg_blinding_point_priv); + + let blinded_hop_pk = if $blinded { + pk + } else { + let hop_pk_blinding_factor = { + let mut hmac = HmacEngine::::new(b"blinded_node_id"); + hmac.input(encrypted_data_ss.as_ref()); + Hmac::from_engine(hmac).to_byte_array() + }; + pk.mul_tweak( + $secp_ctx, + &Scalar::from_be_bytes(hop_pk_blinding_factor).unwrap(), + )? }; - pk.mul_tweak($secp_ctx, &Scalar::from_be_bytes(hop_pk_blinding_factor).unwrap())? - }; - let onion_packet_ss = SharedSecret::new(&blinded_hop_pk, &onion_packet_pubkey_priv); - - let rho = onion_utils::gen_rho_from_shared_secret(encrypted_data_ss.as_ref()); - let unblinded_hop_opt = if $blinded { None } else { Some($hop) }; - $callback(blinded_hop_pk, onion_packet_ss, onion_packet_pubkey, rho, unblinded_hop_opt, $encrypted_payload); - (encrypted_data_ss, onion_packet_ss) - }} - } - - macro_rules! build_keys_in_loop { - ($pk: expr, $blinded: expr, $encrypted_payload: expr) => { - let (encrypted_data_ss, onion_packet_ss) = build_keys!($pk, $blinded, $encrypted_payload); + let onion_packet_ss = SharedSecret::new(&blinded_hop_pk, &onion_packet_pubkey_priv); + + let rho = onion_utils::gen_rho_from_shared_secret(encrypted_data_ss.as_ref()); + let unblinded_hop_opt = if $blinded { None } else { Some($hop) }; + $callback( + blinded_hop_pk, + onion_packet_ss, + onion_packet_pubkey, + rho, + unblinded_hop_opt, + $encrypted_payload, + ); + (encrypted_data_ss, onion_packet_ss) + }}; + } - let msg_blinding_point_blinding_factor = { - let mut sha = Sha256::engine(); - sha.input(&msg_blinding_point.serialize()[..]); - sha.input(encrypted_data_ss.as_ref()); - Sha256::from_engine(sha).to_byte_array() - }; + macro_rules! build_keys_in_loop { + ($pk: expr, $blinded: expr, $encrypted_payload: expr) => { + let (encrypted_data_ss, onion_packet_ss) = + build_keys!($pk, $blinded, $encrypted_payload); - msg_blinding_point_priv = msg_blinding_point_priv.mul_tweak(&Scalar::from_be_bytes(msg_blinding_point_blinding_factor).unwrap())?; - msg_blinding_point = PublicKey::from_secret_key($secp_ctx, &msg_blinding_point_priv); + let msg_blinding_point_blinding_factor = { + let mut sha = Sha256::engine(); + sha.input(&msg_blinding_point.serialize()[..]); + sha.input(encrypted_data_ss.as_ref()); + Sha256::from_engine(sha).to_byte_array() + }; - let onion_packet_pubkey_blinding_factor = { - let mut sha = Sha256::engine(); - sha.input(&onion_packet_pubkey.serialize()[..]); - sha.input(onion_packet_ss.as_ref()); - Sha256::from_engine(sha).to_byte_array() + msg_blinding_point_priv = msg_blinding_point_priv.mul_tweak( + &Scalar::from_be_bytes(msg_blinding_point_blinding_factor).unwrap(), + )?; + msg_blinding_point = + PublicKey::from_secret_key($secp_ctx, &msg_blinding_point_priv); + + let onion_packet_pubkey_blinding_factor = { + let mut sha = Sha256::engine(); + sha.input(&onion_packet_pubkey.serialize()[..]); + sha.input(onion_packet_ss.as_ref()); + Sha256::from_engine(sha).to_byte_array() + }; + onion_packet_pubkey_priv = onion_packet_pubkey_priv.mul_tweak( + &Scalar::from_be_bytes(onion_packet_pubkey_blinding_factor).unwrap(), + )?; + onion_packet_pubkey = + PublicKey::from_secret_key($secp_ctx, &onion_packet_pubkey_priv); }; - onion_packet_pubkey_priv = onion_packet_pubkey_priv.mul_tweak(&Scalar::from_be_bytes(onion_packet_pubkey_blinding_factor).unwrap())?; - onion_packet_pubkey = PublicKey::from_secret_key($secp_ctx, &onion_packet_pubkey_priv); - }; - } -}} + } + }; +} #[inline] pub(crate) fn construct_keys_for_onion_message<'a, T, I, F>( @@ -94,7 +114,7 @@ pub(crate) fn construct_keys_for_onion_message<'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); @@ -122,7 +142,7 @@ pub(super) fn construct_keys_for_blinded_path<'a, T, I, F, H>( where T: secp256k1::Signing + secp256k1::Verification, H: Borrow, - I: Iterator, + I: Iterator, F: FnMut(PublicKey, SharedSecret, PublicKey, [u8; 32], Option, Option>), { build_keys_helper!(session_priv, secp_ctx, callback); @@ -134,8 +154,8 @@ where } struct PublicKeyWithTlvs { - pubkey: PublicKey, - tlvs: W, + pubkey: PublicKey, + tlvs: W, } impl Borrow for PublicKeyWithTlvs { @@ -149,18 +169,24 @@ pub(crate) fn construct_blinded_hops<'a, T, I, W>( ) -> Result, secp256k1::Error> where T: secp256k1::Signing + secp256k1::Verification, - I: Iterator, - W: Writeable + I: Iterator, + W: Writeable, { let mut blinded_hops = Vec::with_capacity(unblinded_path.size_hint().0); construct_keys_for_blinded_path( - secp_ctx, unblinded_path.map(|(pubkey, tlvs)| PublicKeyWithTlvs { pubkey, tlvs }), session_priv, + secp_ctx, + unblinded_path.map(|(pubkey, tlvs)| PublicKeyWithTlvs { pubkey, tlvs }), + session_priv, |blinded_node_id, _, _, encrypted_payload_rho, unblinded_hop_data, _| { blinded_hops.push(BlindedHop { blinded_node_id, - encrypted_payload: encrypt_payload(unblinded_hop_data.unwrap().tlvs, encrypted_payload_rho), + encrypted_payload: encrypt_payload( + unblinded_hop_data.unwrap().tlvs, + encrypted_payload_rho, + ), }); - })?; + }, + )?; Ok(blinded_hops) } @@ -179,7 +205,9 @@ impl Readable for Padding { fn read(reader: &mut R) -> Result { loop { let mut buf = [0; 8192]; - if reader.read(&mut buf[..])? == 0 { break; } + if reader.read(&mut buf[..])? == 0 { + break; + } } Ok(Self {}) } diff --git a/rustfmt_excluded_files b/rustfmt_excluded_files index 973ecff3392..838cfda5aa9 100644 --- a/rustfmt_excluded_files +++ b/rustfmt_excluded_files @@ -1,7 +1,3 @@ -lightning/src/blinded_path/message.rs -lightning/src/blinded_path/mod.rs -lightning/src/blinded_path/payment.rs -lightning/src/blinded_path/utils.rs lightning/src/chain/chaininterface.rs lightning/src/chain/chainmonitor.rs lightning/src/chain/channelmonitor.rs