X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fonion_message%2Fmessenger.rs;h=b884c2ffebd9f90abdbb6051e83e5c802aad4f8a;hb=1c8a06cf61921b5edd637c066ca8bd001183042d;hp=ff20f19a7fc16a6bfea57e59f1b00eca27dc1d77;hpb=f4f1093edc506314484c8d52a40dd155e692394b;p=rust-lightning diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index ff20f19a..b884c2ff 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -15,7 +15,7 @@ use bitcoin::hashes::hmac::{Hmac, HmacEngine}; use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey}; -use crate::chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager, Recipient, Sign}; +use crate::chain::keysinterface::{KeysInterface, KeysManager, Recipient}; use crate::ln::features::{InitFeatures, NodeFeatures}; use crate::ln::msgs::{self, OnionMessageHandler}; use crate::ln::onion_utils; @@ -29,6 +29,7 @@ use crate::util::logger::Logger; use crate::util::ser::Writeable; use core::ops::Deref; +use crate::io; use crate::sync::{Arc, Mutex}; use crate::prelude::*; @@ -47,7 +48,7 @@ use crate::prelude::*; /// # use lightning::ln::peer_handler::IgnoringMessageHandler; /// # use lightning::onion_message::{BlindedRoute, CustomOnionMessageContents, Destination, OnionMessageContents, OnionMessenger}; /// # use lightning::util::logger::{Logger, Record}; -/// # use lightning::util::ser::{MaybeReadableArgs, Writeable, Writer}; +/// # use lightning::util::ser::{Writeable, Writer}; /// # use lightning::io; /// # use std::sync::Arc; /// # struct FakeLogger {}; @@ -81,13 +82,6 @@ use crate::prelude::*; /// your_custom_message_type /// } /// } -/// impl MaybeReadableArgs for YourCustomMessage { -/// fn read(r: &mut R, message_type: u64) -> Result, DecodeError> { -/// # unreachable!() -/// // Read your custom onion message of type `message_type` from `r`, or return `None` -/// // if the message type is unknown -/// } -/// } /// // Send a custom onion message to a node id. /// let intermediate_hops = [hop_node_id1, hop_node_id2]; /// let reply_path = None; @@ -110,8 +104,8 @@ use crate::prelude::*; /// /// [offers]: /// [`OnionMessenger`]: crate::onion_message::OnionMessenger -pub struct OnionMessenger - where K::Target: KeysInterface, +pub struct OnionMessenger + where K::Target: KeysInterface, L::Target: Logger, CMH:: Target: CustomOnionMessageHandler, { @@ -160,6 +154,15 @@ pub enum SendError { InvalidMessage, /// Our next-hop peer's buffer was full or our total outbound buffer was full. BufferFull, + /// Failed to retrieve our node id from the provided [`KeysInterface`]. + /// + /// [`KeysInterface`]: crate::chain::keysinterface::KeysInterface + GetNodeIdFailed, + /// We attempted to send to a blinded route where we are the introduction node, and failed to + /// advance the blinded route to make the second hop the new introduction node. Either + /// [`KeysInterface::ecdh`] failed, we failed to tweak the current blinding point to get the + /// new blinding point, or we were attempting to send to ourselves. + BlindedRouteAdvanceFailed, } /// Handler for custom onion messages. If you are using [`SimpleArcOnionMessenger`], @@ -178,10 +181,13 @@ pub trait CustomOnionMessageHandler { type CustomMessage: CustomOnionMessageContents; /// Called with the custom message that was received. fn handle_custom_message(&self, msg: Self::CustomMessage); + /// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the + /// message type is unknown. + fn read_custom_message(&self, message_type: u64, buffer: &mut R) -> Result, msgs::DecodeError>; } -impl OnionMessenger - where K::Target: KeysInterface, +impl OnionMessenger + where K::Target: KeysInterface, L::Target: Logger, CMH::Target: CustomOnionMessageHandler, { @@ -201,7 +207,7 @@ impl OnionMessenger(&self, intermediate_nodes: &[PublicKey], destination: Destination, message: OnionMessageContents, reply_path: Option) -> Result<(), SendError> { + pub fn send_onion_message(&self, intermediate_nodes: &[PublicKey], mut destination: Destination, message: OnionMessageContents, reply_path: Option) -> Result<(), SendError> { if let Destination::BlindedRoute(BlindedRoute { ref blinded_hops, .. }) = destination { if blinded_hops.len() < 2 { return Err(SendError::TooFewBlindedHops); @@ -210,6 +216,19 @@ impl OnionMessenger OnionMessageHandler for OnionMessenger - where K::Target: KeysInterface, +impl OnionMessageHandler for OnionMessenger + where K::Target: KeysInterface, L::Target: Logger, - CMH::Target: CustomOnionMessageHandler, + CMH::Target: CustomOnionMessageHandler + Sized, { /// Handle an incoming onion message. Currently, if a message was destined for us we will log, but /// soon we'll delegate the onion message to a handler that can generate invoices or send @@ -308,8 +327,8 @@ impl OnionMessageHandler for Onion } } }; - match onion_utils::decode_next_hop(onion_decode_ss, &msg.onion_routing_packet.hop_data[..], - msg.onion_routing_packet.hmac, control_tlvs_ss) + match onion_utils::decode_next_untagged_hop(onion_decode_ss, &msg.onion_routing_packet.hop_data[..], + msg.onion_routing_packet.hmac, (control_tlvs_ss, &*self.custom_handler)) { Ok((Payload::Receive::<<::Target as CustomOnionMessageHandler>::CustomMessage> { message, control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id }), reply_path, @@ -420,8 +439,8 @@ impl OnionMessageHandler for Onion } } -impl OnionMessageProvider for OnionMessenger - where K::Target: KeysInterface, +impl OnionMessageProvider for OnionMessenger + where K::Target: KeysInterface, L::Target: Logger, CMH::Target: CustomOnionMessageHandler, { @@ -443,7 +462,7 @@ impl OnionMessageProvider for Onio /// /// [`SimpleArcChannelManager`]: crate::ln::channelmanager::SimpleArcChannelManager /// [`SimpleArcPeerManager`]: crate::ln::peer_handler::SimpleArcPeerManager -pub type SimpleArcOnionMessenger = OnionMessenger, Arc, IgnoringMessageHandler>; +pub type SimpleArcOnionMessenger = OnionMessenger, Arc, IgnoringMessageHandler>; /// Useful for simplifying the parameters of [`SimpleRefChannelManager`] and /// [`SimpleRefPeerManager`]. See their docs for more details. /// @@ -451,7 +470,7 @@ pub type SimpleArcOnionMessenger = OnionMessenger = OnionMessenger; +pub type SimpleRefOnionMessenger<'a, 'b, L> = OnionMessenger<&'a KeysManager, &'b L, IgnoringMessageHandler>; /// Construct onion packet payloads and keys for sending an onion message along the given /// `unblinded_path` to the given `destination`. @@ -491,12 +510,8 @@ fn packet_payloads_and_keys