Skip to content

Commit 39966e6

Browse files
Public api changes
This commit covers the initial public API changes for onion messages and subsequent documentation updates
1 parent b4c645a commit 39966e6

File tree

4 files changed

+114
-15
lines changed

4 files changed

+114
-15
lines changed

lightning/src/ln/msgs.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,17 @@ enum EncodingType {
724724
Uncompressed = 0x00,
725725
}
726726

727+
/// An onion message to be sent or received from a peer
728+
#[derive(Clone, Debug, PartialEq)]
729+
pub struct OnionMessage {
730+
/// The blinding point used in the shared secret that is used to decrypt the onion message's
731+
/// `encrypted_data` field.
732+
pub blinding_point: PublicKey,
733+
/// The length of the onion message routing packet.
734+
pub len: u16,
735+
pub(crate) onion_routing_packet: OnionPacket,
736+
}
737+
727738
/// Used to put an error message in a LightningError
728739
#[derive(Clone, Debug)]
729740
pub enum ErrorAction {
@@ -909,6 +920,13 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
909920
fn handle_query_short_channel_ids(&self, their_node_id: &PublicKey, msg: QueryShortChannelIds) -> Result<(), LightningError>;
910921
}
911922

923+
/// A trait to describe an object that can receive onion messages.
924+
pub trait OnionMessageHandler {
925+
/// Handle an incoming onion_message message from the given peer.
926+
fn handle_onion_message(&self, their_node_id: &PublicKey, msg: &OnionMessage);
927+
928+
}
929+
912930
mod fuzzy_internal_msgs {
913931
use prelude::*;
914932
use ln::{PaymentPreimage, PaymentSecret};

lightning/src/ln/onion_message.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/// The payload of an inbound onion message.
2+
pub struct Payload {
3+
/// Onion message senders may transmit custom TLVs for the recipient.
4+
custom_tlvs: Vec<CustomTlv>,
5+
// Coming soon: path_id, reply_path
6+
}
7+
8+
/// A sender, receiver and forwarder of onion messages. In upcoming releases, this object will be
9+
/// used to retrieve invoices and fulfill invoice requests from offers.
10+
pub struct OnionMessager<Signer: Sign, K: Deref>
11+
where K::Target: KeysInterface<Signer = Signer>
12+
{
13+
keys_manager: K,
14+
/// When `OnionMessager` receives onion messages for which our node is the final destination, it
15+
/// delegates these messages' payloads to this handler. See [`Receive`] for more information.
16+
inbound_handler: Receive,
17+
pending_msg_events: Vec<MessageSendEvent>,
18+
}
19+
20+
/// `Receive` defines behavior for receiving the final payload of an inbound onion message. Since
21+
/// these [`Payload`]s contain a list of custom TLVs that are unknown to LDK, it is up to the user
22+
/// how to handle them.
23+
///
24+
/// Until LDK implements BOLT 12 offers in upcoming releases, it is recommended to use
25+
/// [`IgnoringReceiver`] unless you have a specific need to receive custom onion messages.
26+
pub trait Receive {
27+
/// Receive an inbound onion message payload.
28+
fn receive(payload: Payload);
29+
}
30+
31+
/// A [`Receive`]r that simply ignores inbound onion messages.
32+
pub struct IgnoringReceiver {}
33+
impl Receive for IgnoringReceiver {
34+
fn receive(payload: Payload) {}
35+
}
36+
37+
impl<Signer: Sign, K: Deref> OnionMessager<Signer, K>
38+
where K::Target: KeysInterface<Signer = Signer>,
39+
{
40+
/// Constructs a new `OnionMessager` to send, forward, and receive onion messages.
41+
pub fn new(keys_manager: K) -> Self {}
42+
43+
/// Send an empty onion message to `recipient`, routing it through `intermediate_nodes`.
44+
///
45+
/// LDK will not make peer connections for you, so you must be connected to the first hop of
46+
/// `intermediate_nodes` for sending to work.
47+
// NOTE: sending custom TLVs, reply paths, and sending to blinded routes aren't included atm
48+
pub fn send_onion_message(&self, recipient: PublicKey, intermediate_nodes: Vec<PublicKey>) -> Result<(), APIError> {}
49+
}
50+
51+
impl OnionMessageHandler for OnionMessager {
52+
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &msgs::OnionMessage) {
53+
}
54+
}
55+
56+
/// Useful for simplifying the parameters of [`SimpleArcChannelManager`] and
57+
/// [`SimpleArcPeerManager`]. See their docs for more details.
58+
pub type SimpleArcOnionMessager = OnionMessager<Arc<KeysManager>>;
59+
/// Useful for simplifying the parameters of [`SimpleRefChannelManager`] and
60+
/// [`SimpleRefPeerManager`]. See their docs for more details.
61+
pub type SimpleRefOnionMessager<'a> = OnionMessager<&'a KeysManager>;

lightning/src/ln/peer_handler.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,16 @@ impl Deref for ErroringMessageHandler {
197197
fn deref(&self) -> &Self { self }
198198
}
199199

200+
impl OnionMessageHandler for IgnoringMessageHandler {
201+
fn handle_onion_message(&self, _their_node_id: &PublicKey, _msg: &msgs::OnionMessage) {}
202+
}
203+
200204
/// Provides references to trait impls which handle different types of messages.
201-
pub struct MessageHandler<CM: Deref, RM: Deref> where
205+
pub struct MessageHandler<CM: Deref, RM: Deref, OM: Deref> where
202206
CM::Target: ChannelMessageHandler,
203-
RM::Target: RoutingMessageHandler {
207+
RM::Target: RoutingMessageHandler,
208+
OM::Target: OnionMessageHandler,
209+
{
204210
/// A message handler which handles messages specific to channels. Usually this is just a
205211
/// [`ChannelManager`] object or an [`ErroringMessageHandler`].
206212
///
@@ -212,6 +218,11 @@ pub struct MessageHandler<CM: Deref, RM: Deref> where
212218
///
213219
/// [`NetGraphMsgHandler`]: crate::routing::network_graph::NetGraphMsgHandler
214220
pub route_handler: RM,
221+
/// A message handler which handles onion messages. Using this is just an [`OnionMessager`]
222+
/// object or an [`IgnoringMessageHandler`].
223+
///
224+
/// [`OnionMessager`]: crate::ln::onion_messages::OnionMessager
225+
pub onion_message_handler: OM,
215226
}
216227

217228
/// Provides an object which can be used to send data to and which uniquely identifies a connection
@@ -379,7 +390,7 @@ struct PeerHolder<Descriptor: SocketDescriptor> {
379390
/// issues such as overly long function definitions.
380391
///
381392
/// (C-not exported) as Arcs don't make sense in bindings
382-
pub type SimpleArcPeerManager<SD, M, T, F, C, L> = PeerManager<SD, Arc<SimpleArcChannelManager<M, T, F, L>>, Arc<NetGraphMsgHandler<Arc<NetworkGraph>, Arc<C>, Arc<L>>>, Arc<L>, Arc<IgnoringMessageHandler>>;
393+
pub type SimpleArcPeerManager<SD, M, T, F, C, L> = PeerManager<SD, Arc<SimpleArcChannelManager<M, T, F, L>>, Arc<NetGraphMsgHandler<Arc<NetworkGraph>, Arc<C>, Arc<L>>>, Arc<L>, SimpleArcOnionMessager, Arc<IgnoringMessageHandler>>;
383394

384395
/// SimpleRefPeerManager is a type alias for a PeerManager reference, and is the reference
385396
/// counterpart to the SimpleArcPeerManager type alias. Use this type by default when you don't
@@ -389,7 +400,7 @@ pub type SimpleArcPeerManager<SD, M, T, F, C, L> = PeerManager<SD, Arc<SimpleArc
389400
/// helps with issues such as long function definitions.
390401
///
391402
/// (C-not exported) as Arcs don't make sense in bindings
392-
pub type SimpleRefPeerManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, SD, M, T, F, C, L> = PeerManager<SD, SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L>, &'e NetGraphMsgHandler<&'g NetworkGraph, &'h C, &'f L>, &'f L, IgnoringMessageHandler>;
403+
pub type SimpleRefPeerManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, SD, M, T, F, C, L> = PeerManager<SD, SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L>, &'e NetGraphMsgHandler<&'g NetworkGraph, &'h C, &'f L>, SimpleRefOnionMessager<'c>, &'f L, IgnoringMessageHandler>;
393404

394405
/// A PeerManager manages a set of peers, described by their [`SocketDescriptor`] and marshalls
395406
/// socket events into messages which it passes on to its [`MessageHandler`].
@@ -410,12 +421,13 @@ pub type SimpleRefPeerManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, SD, M, T, F, C, L>
410421
/// you're using lightning-net-tokio.
411422
///
412423
/// [`read_event`]: PeerManager::read_event
413-
pub struct PeerManager<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> where
424+
pub struct PeerManager<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CMH: Deref> where
414425
CM::Target: ChannelMessageHandler,
415426
RM::Target: RoutingMessageHandler,
427+
OM::Target: OnionMessageHandler,
416428
L::Target: Logger,
417429
CMH::Target: CustomMessageHandler {
418-
message_handler: MessageHandler<CM, RM>,
430+
message_handler: MessageHandler<CM, RM, OM>,
419431
peers: Mutex<PeerHolder<Descriptor>>,
420432
our_node_secret: SecretKey,
421433
ephemeral_key_midstate: Sha256Engine,
@@ -451,31 +463,32 @@ macro_rules! encode_msg {
451463
}}
452464
}
453465

454-
impl<Descriptor: SocketDescriptor, CM: Deref, L: Deref> PeerManager<Descriptor, CM, IgnoringMessageHandler, L, IgnoringMessageHandler> where
466+
impl<Descriptor: SocketDescriptor, CM: Deref, OM: Deref, L: Deref> PeerManager<Descriptor, CM, IgnoringMessageHandler, OM, L, IgnoringMessageHandler> where
455467
CM::Target: ChannelMessageHandler,
468+
OM::Target: OnionMessageHandler,
456469
L::Target: Logger {
457-
/// Constructs a new PeerManager with the given ChannelMessageHandler. No routing message
458-
/// handler is used and network graph messages are ignored.
470+
/// Constructs a new PeerManager with the given ChannelMessageHandler and OnionMessageHandler. No
471+
/// routing message handler is used and network graph messages are ignored.
459472
///
460473
/// ephemeral_random_data is used to derive per-connection ephemeral keys and must be
461474
/// cryptographically secure random bytes.
462475
///
463476
/// (C-not exported) as we can't export a PeerManager with a dummy route handler
464-
pub fn new_channel_only(channel_message_handler: CM, our_node_secret: SecretKey, ephemeral_random_data: &[u8; 32], logger: L) -> Self {
477+
pub fn new_channel_only(channel_message_handler: CM, onion_message_handler: OM, our_node_secret: SecretKey, ephemeral_random_data: &[u8; 32], logger: L) -> Self {
465478
Self::new(MessageHandler {
466479
chan_handler: channel_message_handler,
467480
route_handler: IgnoringMessageHandler{},
468481
}, our_node_secret, ephemeral_random_data, logger, IgnoringMessageHandler{})
469482
}
470483
}
471484

472-
impl<Descriptor: SocketDescriptor, RM: Deref, L: Deref> PeerManager<Descriptor, ErroringMessageHandler, RM, L, IgnoringMessageHandler> where
485+
impl<Descriptor: SocketDescriptor, RM: Deref, L: Deref> PeerManager<Descriptor, ErroringMessageHandler, RM, IgnoringMessageHandler, L, IgnoringMessageHandler> where
473486
RM::Target: RoutingMessageHandler,
474487
L::Target: Logger {
475-
/// Constructs a new PeerManager with the given RoutingMessageHandler. No channel message
476-
/// handler is used and messages related to channels will be ignored (or generate error
477-
/// messages). Note that some other lightning implementations time-out connections after some
478-
/// time if no channel is built with the peer.
488+
/// Constructs a new PeerManager with the given RoutingMessageHandler. No channel message handler
489+
/// or onion message handler is used and messages related to channels will be ignored (or generate
490+
/// error messages). Note that some other lightning implementations time-out connections after
491+
/// some time if no channel is built with the peer.
479492
///
480493
/// ephemeral_random_data is used to derive per-connection ephemeral keys and must be
481494
/// cryptographically secure random bytes.

lightning/src/util/events.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,13 @@ pub enum MessageSendEvent {
933933
/// The gossip_timestamp_filter which should be sent.
934934
msg: msgs::GossipTimestampFilter,
935935
},
936+
/// Sends an onion message.
937+
SendOnionMessage {
938+
/// The node_id of this message recipient
939+
node_id: PublicKey,
940+
/// The onion message which should be sent.
941+
msg: msgs::OnionMessage,
942+
},
936943
}
937944

938945
/// A trait indicating an object may generate message send events

0 commit comments

Comments
 (0)