Rename and expose message-specific `NextHop`
authorMatt Corallo <git@bluematt.me>
Tue, 23 Apr 2024 15:56:13 +0000 (15:56 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 1 May 2024 15:16:57 +0000 (15:16 +0000)
`onion::message::messenger::PeeledOnion` is a public enum which
included the private enum `NextHop`, which is not acceptable. Thus,
we here expose `NextHop` but rename it `NextMessageHop` to make
clear that it is specific to messages.

lightning/src/blinded_path/message.rs
lightning/src/blinded_path/mod.rs
lightning/src/onion_message/messenger.rs
lightning/src/onion_message/packet.rs

index df7f8e7ad6128ee90b1d6048c3cdef7a5875b28c..9a282a5f87d42658599870dd7446d5eef2e1ea5c 100644 (file)
@@ -3,7 +3,7 @@ use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
 #[allow(unused_imports)]
 use crate::prelude::*;
 
-use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NodeIdLookUp};
+use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NextMessageHop, NodeIdLookUp};
 use crate::blinded_path::utils;
 use crate::io;
 use crate::io::Cursor;
@@ -20,7 +20,7 @@ use core::ops::Deref;
 /// route, they are encoded into [`BlindedHop::encrypted_payload`].
 pub(crate) struct ForwardTlvs {
        /// The next hop in the onion message's path.
-       pub(crate) next_hop: NextHop,
+       pub(crate) next_hop: NextMessageHop,
        /// Senders to a blinded path use this value to concatenate the route they find to the
        /// introduction node with the blinded path.
        pub(crate) next_blinding_override: Option<PublicKey>,
@@ -34,20 +34,11 @@ pub(crate) struct ReceiveTlvs {
        pub(crate) path_id: Option<[u8; 32]>,
 }
 
-/// The next hop to forward the onion message along its path.
-#[derive(Debug)]
-pub enum NextHop {
-       /// The node id of the next hop.
-       NodeId(PublicKey),
-       /// The short channel id leading to the next hop.
-       ShortChannelId(u64),
-}
-
 impl Writeable for ForwardTlvs {
        fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
                let (next_node_id, short_channel_id) = match self.next_hop {
-                       NextHop::NodeId(pubkey) => (Some(pubkey), None),
-                       NextHop::ShortChannelId(scid) => (None, Some(scid)),
+                       NextMessageHop::NodeId(pubkey) => (Some(pubkey), None),
+                       NextMessageHop::ShortChannelId(scid) => (None, Some(scid)),
                };
                // TODO: write padding
                encode_tlv_stream!(writer, {
@@ -75,7 +66,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
 ) -> Result<Vec<BlindedHop>, secp256k1::Error> {
        let blinded_tlvs = unblinded_path.iter()
                .skip(1) // The first node's TLVs contains the next node's pubkey
-               .map(|pk| ForwardTlvs { next_hop: NextHop::NodeId(*pk), next_blinding_override: None })
+               .map(|pk| ForwardTlvs { next_hop: NextMessageHop::NodeId(*pk), next_blinding_override: None })
                .map(|tlvs| ControlTlvs::Forward(tlvs))
                .chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { path_id: None })));
 
@@ -102,8 +93,8 @@ where
                        readable: ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override })
                }) => {
                        let next_node_id = match next_hop {
-                               NextHop::NodeId(pubkey) => pubkey,
-                               NextHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid) {
+                               NextMessageHop::NodeId(pubkey) => pubkey,
+                               NextMessageHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid) {
                                        Some(pubkey) => pubkey,
                                        None => return Err(()),
                                },
index 07fa7b770249cae94e1ea18269b63805dc933f93..3b4eac883646da74c7aa886066632840b622894d 100644 (file)
@@ -24,6 +24,17 @@ use crate::util::ser::{Readable, Writeable, Writer};
 use crate::io;
 use crate::prelude::*;
 
+/// The next hop to forward an onion message along its path.
+///
+/// Note that payment blinded paths always specify their next hop using an explicit node id.
+#[derive(Debug)]
+pub enum NextMessageHop {
+       /// The node id of the next hop.
+       NodeId(PublicKey),
+       /// The short channel id leading to the next hop.
+       ShortChannelId(u64),
+}
+
 /// Onion messages and payments can be sent and received to blinded paths, which serve to hide the
 /// identity of the recipient.
 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
index 1d7a730fa3625126097fd6d25de150e5ba46c742..8fbcb5877d8d7f60108b4a3239d7c44f00e37b22 100644 (file)
@@ -15,8 +15,8 @@ use bitcoin::hashes::hmac::{Hmac, HmacEngine};
 use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
 
-use crate::blinded_path::{BlindedPath, IntroductionNode, NodeIdLookUp};
-use crate::blinded_path::message::{advance_path_by_one, ForwardTlvs, NextHop, ReceiveTlvs};
+use crate::blinded_path::{BlindedPath, IntroductionNode, NextMessageHop, NodeIdLookUp};
+use crate::blinded_path::message::{advance_path_by_one, ForwardTlvs, ReceiveTlvs};
 use crate::blinded_path::utils;
 use crate::events::{Event, EventHandler, EventsProvider};
 use crate::sign::{EntropySource, NodeSigner, Recipient};
@@ -572,7 +572,7 @@ pub trait CustomOnionMessageHandler {
 #[derive(Debug)]
 pub enum PeeledOnion<T: OnionMessageContents> {
        /// Forwarded onion, with the next node id and a new onion
-       Forward(NextHop, OnionMessage),
+       Forward(NextMessageHop, OnionMessage),
        /// Received onion message, with decrypted contents, path_id, and reply path
        Receive(ParsedOnionMessageContents<T>, Option<[u8; 32]>, Option<BlindedPath>)
 }
@@ -1050,8 +1050,8 @@ where
                        },
                        Ok(PeeledOnion::Forward(next_hop, onion_message)) => {
                                let next_node_id = match next_hop {
-                                       NextHop::NodeId(pubkey) => pubkey,
-                                       NextHop::ShortChannelId(scid) => match self.node_id_lookup.next_node_id(scid) {
+                                       NextMessageHop::NodeId(pubkey) => pubkey,
+                                       NextMessageHop::ShortChannelId(scid) => match self.node_id_lookup.next_node_id(scid) {
                                                Some(pubkey) => pubkey,
                                                None => {
                                                        log_trace!(self.logger, "Dropping forwarded onion messager: unable to resolve next hop using SCID {}", scid);
@@ -1255,7 +1255,7 @@ fn packet_payloads_and_keys<T: OnionMessageContents, S: secp256k1::Signing + sec
                                if let Some(ss) = prev_control_tlvs_ss.take() {
                                        payloads.push((Payload::Forward(ForwardControlTlvs::Unblinded(
                                                ForwardTlvs {
-                                                       next_hop: NextHop::NodeId(unblinded_pk_opt.unwrap()),
+                                                       next_hop: NextMessageHop::NodeId(unblinded_pk_opt.unwrap()),
                                                        next_blinding_override: None,
                                                }
                                        )), ss));
@@ -1265,7 +1265,7 @@ fn packet_payloads_and_keys<T: OnionMessageContents, S: secp256k1::Signing + sec
                        } else if let Some((intro_node_id, blinding_pt)) = intro_node_id_blinding_pt.take() {
                                if let Some(control_tlvs_ss) = prev_control_tlvs_ss.take() {
                                        payloads.push((Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {
-                                               next_hop: NextHop::NodeId(intro_node_id),
+                                               next_hop: NextMessageHop::NodeId(intro_node_id),
                                                next_blinding_override: Some(blinding_pt),
                                        })), control_tlvs_ss));
                                }
index 510f0ea025a0d615b0f54292d865602ac7e103a6..e1054c5dbe1886a10aa7deb447908e52cbee5325 100644 (file)
@@ -12,8 +12,8 @@
 use bitcoin::secp256k1::PublicKey;
 use bitcoin::secp256k1::ecdh::SharedSecret;
 
-use crate::blinded_path::BlindedPath;
-use crate::blinded_path::message::{ForwardTlvs, NextHop, ReceiveTlvs};
+use crate::blinded_path::{BlindedPath, NextMessageHop};
+use crate::blinded_path::message::{ForwardTlvs, ReceiveTlvs};
 use crate::blinded_path::utils::Padding;
 use crate::ln::msgs::DecodeError;
 use crate::ln::onion_utils;
@@ -293,8 +293,8 @@ impl Readable for ControlTlvs {
 
                let next_hop = match (short_channel_id, next_node_id) {
                        (Some(_), Some(_)) => return Err(DecodeError::InvalidValue),
-                       (Some(scid), None) => Some(NextHop::ShortChannelId(scid)),
-                       (None, Some(pubkey)) => Some(NextHop::NodeId(pubkey)),
+                       (Some(scid), None) => Some(NextMessageHop::ShortChannelId(scid)),
+                       (None, Some(pubkey)) => Some(NextMessageHop::NodeId(pubkey)),
                        (None, None) => None,
                };