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.
This commit is contained in:
Matt Corallo 2024-04-23 15:56:13 +00:00
parent b403411c24
commit 89a67e59ab
4 changed files with 29 additions and 27 deletions

View 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(()),
},

View 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)]

View 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));
}

View 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,
};