//! 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::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)]
///
/// [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,
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<PublicKey>;
+}
+
+/// A [`NodeIdLookUp`] that always returns `None`.
+pub struct EmptyNodeIdLookUp {}
+
+impl NodeIdLookUp for EmptyNodeIdLookUp {
+ fn next_node_id(&self, _short_channel_id: u64) -> Option<PublicKey> {
+ None
+ }
+}
+
+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.
impl BlindedPath {
/// Create a one-hop blinded path for a message.
- pub fn one_hop_for_message<ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification>(
- recipient_node_id: PublicKey, entropy_source: &ES, secp_ctx: &Secp256k1<T>
- ) -> Result<Self, ()> {
- Self::new_for_message(&[recipient_node_id], entropy_source, secp_ctx)
+ pub fn one_hop_for_message<ES: Deref, T: secp256k1::Signing + secp256k1::Verification>(
+ recipient_node_id: PublicKey, entropy_source: ES, secp_ctx: &Secp256k1<T>
+ ) -> Result<Self, ()> 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
///
/// 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<ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification>(
- node_pks: &[PublicKey], entropy_source: &ES, secp_ctx: &Secp256k1<T>
- ) -> Result<Self, ()> {
- if node_pks.is_empty() { return Err(()) }
+ pub fn new_for_message<ES: Deref, T: secp256k1::Signing + secp256k1::Verification>(
+ intermediate_nodes: &[message::ForwardNode], recipient_node_id: PublicKey,
+ entropy_source: ES, secp_ctx: &Secp256k1<T>
+ ) -> Result<Self, ()> 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<ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification>(
+ pub fn one_hop_for_payment<ES: Deref, T: secp256k1::Signing + secp256k1::Verification>(
payee_node_id: PublicKey, payee_tlvs: payment::ReceiveTlvs, min_final_cltv_expiry_delta: u16,
- entropy_source: &ES, secp_ctx: &Secp256k1<T>
- ) -> Result<(BlindedPayInfo, Self), ()> {
+ entropy_source: ES, secp_ctx: &Secp256k1<T>
+ ) -> 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();
///
/// [`ForwardTlvs`]: crate::blinded_path::payment::ForwardTlvs
// TODO: make all payloads the same size with padding + add dummy hops
- pub fn new_for_payment<ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification>(
+ pub fn new_for_payment<ES: Deref, T: secp256k1::Signing + secp256k1::Verification>(
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<T>
- ) -> Result<(BlindedPayInfo, Self), ()> {
+ entropy_source: ES, secp_ctx: &Secp256k1<T>
+ ) -> Result<(BlindedPayInfo, Self), ()> where ES::Target: EntropySource {
let introduction_node = IntroductionNode::NodeId(
intermediate_nodes.first().map_or(payee_node_id, |n| n.node_id)
);
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,
+ }
+ }
}