X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fonion_message%2Fmessenger.rs;h=723e105d044cf549cfd8f392e206782b38a479af;hb=d974a07e96199932e98f6c34b8f3e1f151b53500;hp=79bd5724364c1701e4393b2d70ae288b3270f5af;hpb=622f7f2f7943295ccf9393547390dc09852c8143;p=rust-lightning diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 79bd5724..723e105d 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -19,6 +19,7 @@ use crate::blinded_path::BlindedPath; use crate::blinded_path::message::{advance_path_by_one, ForwardTlvs, ReceiveTlvs}; use crate::blinded_path::utils; use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient}; +use crate::ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager}; use crate::ln::features::{InitFeatures, NodeFeatures}; use crate::ln::msgs::{self, OnionMessage, OnionMessageHandler}; use crate::ln::onion_utils; @@ -176,14 +177,18 @@ pub trait MessageRouter { ) -> Result; } -/// A [`MessageRouter`] that always fails. +/// A [`MessageRouter`] that can only route to a directly connected [`Destination`]. pub struct DefaultMessageRouter; impl MessageRouter for DefaultMessageRouter { fn find_path( - &self, _sender: PublicKey, _peers: Vec, _destination: Destination + &self, _sender: PublicKey, peers: Vec, destination: Destination ) -> Result { - Err(()) + if peers.contains(&destination.first_node()) { + Ok(OnionMessagePath { intermediate_nodes: vec![], destination }) + } else { + Err(()) + } } } @@ -213,6 +218,13 @@ impl Destination { Destination::BlindedPath(BlindedPath { blinded_hops, .. }) => blinded_hops.len(), } } + + fn first_node(&self) -> PublicKey { + match self { + Destination::Node(node_id) => *node_id, + Destination::BlindedPath(BlindedPath { introduction_node_id: node_id, .. }) => *node_id, + } + } } /// Errors that may occur when [sending an onion message]. @@ -344,11 +356,13 @@ where })) } -/// Decode one layer of an incoming onion message -/// Returns either a Forward (another onion message), or Receive (decrypted content) -pub fn peel_onion( - node_signer: NS, secp_ctx: &Secp256k1, logger: L, custom_handler: CMH, - msg: &OnionMessage, +/// Decode one layer of an incoming [`OnionMessage`]. +/// +/// Returns either the next layer of the onion for forwarding or the decrypted content for the +/// receiver. +pub fn peel_onion_message( + msg: &OnionMessage, secp_ctx: &Secp256k1, node_signer: NS, logger: L, + custom_handler: CMH, ) -> Result::Target as CustomOnionMessageHandler>::CustomMessage>, ()> where NS::Target: NodeSigner, @@ -584,8 +598,8 @@ where CMH::Target: CustomOnionMessageHandler, { fn handle_onion_message(&self, _peer_node_id: &PublicKey, msg: &OnionMessage) { - match peel_onion( - &*self.node_signer, &self.secp_ctx, &*self.logger, &*self.custom_handler, msg + match peel_onion_message( + msg, &self.secp_ctx, &*self.node_signer, &*self.logger, &*self.custom_handler ) { Ok(PeeledOnion::Receive(message, path_id, reply_path)) => { log_trace!(self.logger, @@ -702,12 +716,12 @@ where /// /// [`SimpleArcChannelManager`]: crate::ln::channelmanager::SimpleArcChannelManager /// [`SimpleArcPeerManager`]: crate::ln::peer_handler::SimpleArcPeerManager -pub type SimpleArcOnionMessenger = OnionMessenger< +pub type SimpleArcOnionMessenger = OnionMessenger< Arc, Arc, Arc, Arc, - IgnoringMessageHandler, + Arc>, IgnoringMessageHandler >; @@ -718,12 +732,14 @@ pub type SimpleArcOnionMessenger = OnionMessenger< /// /// [`SimpleRefChannelManager`]: crate::ln::channelmanager::SimpleRefChannelManager /// [`SimpleRefPeerManager`]: crate::ln::peer_handler::SimpleRefPeerManager -pub type SimpleRefOnionMessenger<'a, 'b, 'c, L> = OnionMessenger< +pub type SimpleRefOnionMessenger< + 'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, M, T, F, L +> = OnionMessenger< &'a KeysManager, &'a KeysManager, &'b L, - &'c DefaultMessageRouter, - IgnoringMessageHandler, + &'i DefaultMessageRouter, + &'j SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L>, IgnoringMessageHandler >;