X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fblinded_path%2Fmod.rs;h=2d3d085bddf35d51a5276c5c9c048506527ac45b;hb=0a6886ee38cfd906f984d7b8f444cb8621fce4fd;hp=3dc6b121b006b80f95f05bfb59f23a5ff0764b98;hpb=38690bf50a60dc7b7ca3fbf67299b9b701607721;p=rust-lightning diff --git a/lightning/src/blinded_path/mod.rs b/lightning/src/blinded_path/mod.rs index 3dc6b121..2d3d085b 100644 --- a/lightning/src/blinded_path/mod.rs +++ b/lightning/src/blinded_path/mod.rs @@ -10,16 +10,18 @@ //! Creating blinded paths and related utilities live here. pub mod payment; -pub(crate) mod message; +pub mod message; pub(crate) mod utils; use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey}; +use core::ops::Deref; use crate::ln::msgs::DecodeError; use crate::offers::invoice::BlindedPayInfo; use crate::routing::gossip::{NodeId, ReadOnlyNetworkGraph}; use crate::sign::EntropySource; use crate::util::ser::{Readable, Writeable, Writer}; +use crate::util::scid_utils; use crate::io; use crate::prelude::*; @@ -100,6 +102,11 @@ impl NodeIdLookUp for EmptyNodeIdLookUp { } } +impl Deref for EmptyNodeIdLookUp { + type Target = EmptyNodeIdLookUp; + fn deref(&self) -> &Self { self } +} + /// An encrypted payload and node id corresponding to a hop in a payment or onion message path, to /// be encoded in the sender's onion packet. These hops cannot be identified by outside observers /// and thus can be used to hide the identity of the recipient. @@ -115,10 +122,10 @@ pub struct BlindedHop { impl BlindedPath { /// Create a one-hop blinded path for a message. - pub fn one_hop_for_message( - recipient_node_id: PublicKey, entropy_source: &ES, secp_ctx: &Secp256k1 - ) -> Result { - Self::new_for_message(&[recipient_node_id], entropy_source, secp_ctx) + pub fn one_hop_for_message( + recipient_node_id: PublicKey, entropy_source: ES, secp_ctx: &Secp256k1 + ) -> Result where ES::Target: EntropySource { + Self::new_for_message(&[], recipient_node_id, entropy_source, secp_ctx) } /// Create a blinded path for an onion message, to be forwarded along `node_pks`. The last node @@ -126,26 +133,30 @@ impl BlindedPath { /// /// Errors if no hops are provided or if `node_pk`(s) are invalid. // TODO: make all payloads the same size with padding + add dummy hops - pub fn new_for_message( - node_pks: &[PublicKey], entropy_source: &ES, secp_ctx: &Secp256k1 - ) -> Result { - if node_pks.is_empty() { return Err(()) } + pub fn new_for_message( + intermediate_nodes: &[message::ForwardNode], recipient_node_id: PublicKey, + entropy_source: ES, secp_ctx: &Secp256k1 + ) -> Result where ES::Target: EntropySource { + let introduction_node = IntroductionNode::NodeId( + 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 introduction_node = IntroductionNode::NodeId(node_pks[0]); Ok(BlindedPath { introduction_node, blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret), - blinded_hops: message::blinded_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?, + blinded_hops: message::blinded_hops( + secp_ctx, intermediate_nodes, recipient_node_id, &blinding_secret, + ).map_err(|_| ())?, }) } /// Create a one-hop blinded path for a payment. - pub fn one_hop_for_payment( + pub fn one_hop_for_payment( payee_node_id: PublicKey, payee_tlvs: payment::ReceiveTlvs, min_final_cltv_expiry_delta: u16, - entropy_source: &ES, secp_ctx: &Secp256k1 - ) -> Result<(BlindedPayInfo, Self), ()> { + entropy_source: ES, secp_ctx: &Secp256k1 + ) -> Result<(BlindedPayInfo, Self), ()> 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(); @@ -164,11 +175,11 @@ impl BlindedPath { /// /// [`ForwardTlvs`]: crate::blinded_path::payment::ForwardTlvs // TODO: make all payloads the same size with padding + add dummy hops - pub fn new_for_payment( + pub fn new_for_payment( intermediate_nodes: &[payment::ForwardNode], payee_node_id: PublicKey, payee_tlvs: payment::ReceiveTlvs, htlc_maximum_msat: u64, min_final_cltv_expiry_delta: u16, - entropy_source: &ES, secp_ctx: &Secp256k1 - ) -> Result<(BlindedPayInfo, Self), ()> { + entropy_source: ES, secp_ctx: &Secp256k1 + ) -> Result<(BlindedPayInfo, Self), ()> where ES::Target: EntropySource { let introduction_node = IntroductionNode::NodeId( intermediate_nodes.first().map_or(payee_node_id, |n| n.node_id) ); @@ -207,6 +218,35 @@ impl BlindedPath { }, } } + + /// Attempts to a use a compact representation for the [`IntroductionNode`] by using a directed + /// short channel id from a channel in `network_graph` leading to the introduction node. + /// + /// While this may result in a smaller encoding, there is a trade off in that the path may + /// become invalid if the channel is closed or hasn't been propagated via gossip. Therefore, + /// calling this may not be suitable for long-lived blinded paths. + pub fn use_compact_introduction_node(&mut self, network_graph: &ReadOnlyNetworkGraph) { + if let IntroductionNode::NodeId(pubkey) = &self.introduction_node { + let node_id = NodeId::from_pubkey(pubkey); + 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)) + { + let direction = if node_id == channel_info.node_one { + Direction::NodeOne + } else { + debug_assert_eq!(node_id, channel_info.node_two); + Direction::NodeTwo + }; + self.introduction_node = + IntroductionNode::DirectedShortChannelId(direction, scid); + } + } + } + } } impl Writeable for BlindedPath {