From: Matt Corallo Date: Tue, 23 Apr 2024 15:56:13 +0000 (+0000) Subject: Rename and expose message-specific `NextHop` X-Git-Tag: v0.0.123-rc1~1^2~2^2~4 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=89a67e59ab11b41033632d174e32fe479b32fdc3;p=rust-lightning Rename and expose message-specific `NextHop` `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. --- diff --git a/lightning/src/blinded_path/message.rs b/lightning/src/blinded_path/message.rs index df7f8e7ad..9a282a5f8 100644 --- a/lightning/src/blinded_path/message.rs +++ b/lightning/src/blinded_path/message.rs @@ -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, @@ -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(&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( ) -> Result, 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(()), }, diff --git a/lightning/src/blinded_path/mod.rs b/lightning/src/blinded_path/mod.rs index 07fa7b770..3b4eac883 100644 --- a/lightning/src/blinded_path/mod.rs +++ b/lightning/src/blinded_path/mod.rs @@ -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)] diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 1d7a730fa..8fbcb5877 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -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 { /// 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, Option<[u8; 32]>, Option) } @@ -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 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, };