X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fblinded_path%2Fmod.rs;h=3dc6b121b006b80f95f05bfb59f23a5ff0764b98;hb=806b7f0e312c59c87fd628fb71e7c4a77a39645a;hp=daa9f033d94e4f0b5544c8cc71bb6e7523ca8acb;hpb=7002180261d495f15c12f9a341af1e7dbcac2990;p=rust-lightning diff --git a/lightning/src/blinded_path/mod.rs b/lightning/src/blinded_path/mod.rs index daa9f033..3dc6b121 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(Clone, Debug, Hash, PartialEq, Eq)] +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)] @@ -58,7 +69,7 @@ pub enum IntroductionNode { /// /// [BOLT 7]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-channel_announcement-message /// [`ChannelAnnouncement`]: crate::ln::msgs::ChannelAnnouncement -#[derive(Clone, Debug, Hash, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] pub enum Direction { /// The lesser node id when compared lexicographically in ascending order. NodeOne, @@ -66,6 +77,29 @@ pub enum Direction { NodeTwo, } +/// An interface for looking up the node id of a channel counterparty for the purpose of forwarding +/// an [`OnionMessage`]. +/// +/// [`OnionMessage`]: crate::ln::msgs::OnionMessage +pub trait NodeIdLookUp { + /// Returns the node id of the forwarding node's channel counterparty with `short_channel_id`. + /// + /// Here, the forwarding node is referring to the node of the [`OnionMessenger`] parameterized + /// by the [`NodeIdLookUp`] and the counterparty to one of that node's peers. + /// + /// [`OnionMessenger`]: crate::onion_message::messenger::OnionMessenger + fn next_node_id(&self, short_channel_id: u64) -> Option; +} + +/// A [`NodeIdLookUp`] that always returns `None`. +pub struct EmptyNodeIdLookUp {} + +impl NodeIdLookUp for EmptyNodeIdLookUp { + fn next_node_id(&self, _short_channel_id: u64) -> Option { + None + } +} + /// 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. @@ -238,4 +272,17 @@ impl Direction { Direction::NodeTwo => core::cmp::max(node_a, node_b), } } + + /// Returns the [`PublicKey`] from the inputs corresponding to the direction. + pub fn select_pubkey<'a>(&self, node_a: &'a PublicKey, node_b: &'a PublicKey) -> &'a PublicKey { + let (node_one, node_two) = if NodeId::from_pubkey(node_a) < NodeId::from_pubkey(node_b) { + (node_a, node_b) + } else { + (node_b, node_a) + }; + match self { + Direction::NodeOne => node_one, + Direction::NodeTwo => node_two, + } + } }