Skip to content

Commit 9ff0e82

Browse files
Significantly expand onion message documentation
1 parent 4b6d3dc commit 9ff0e82

File tree

1 file changed

+65
-1
lines changed
  • lightning/src/onion_message

1 file changed

+65
-1
lines changed

lightning/src/onion_message/mod.rs

+65-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
// licenses.
99

1010
//! Onion Messages: sending, receiving, forwarding, and ancillary utilities live here
11+
//!
12+
//! Onion messages are multi-purpose messages sent between peers over the lightning network. In the
13+
//! near future, they will be used to communicate invoices for [Offers], unlocking use cases such as
14+
//! static invoices, refunds and proof of payer. Further, you will be able to accept payments
15+
//! without revealing your node id through the use of [blinded routes].
16+
//!
17+
//! LDK sends and receives onion messages via the [`OnionMessenger`]. See its documentation for more
18+
//! information on its usage.
19+
//!
20+
//! [Offers]: https://github.com/lightning/bolts/pull/798
21+
//! [blinded routes]: crate::onion_message::blinded_route::BlindedRoute
22+
//! [`OnionMessenger`]: crate::onion_message::OnionMessenger
1123
1224
use bitcoin::hashes::{Hash, HashEngine};
1325
use bitcoin::hashes::hmac::{Hmac, HmacEngine};
@@ -39,9 +51,61 @@ pub(crate) const SMALL_PACKET_HOP_DATA_LEN: usize = 1300;
3951
pub(crate) const BIG_PACKET_HOP_DATA_LEN: usize = 32768;
4052

4153
/// A sender, receiver and forwarder of onion messages. In upcoming releases, this object will be
42-
/// used to retrieve invoices and fulfill invoice requests from [offers].
54+
/// used to retrieve invoices and fulfill invoice requests from [offers]. Currently, only sending
55+
/// and receiving empty onion messages is supported.
56+
///
57+
/// To set up the [`OnionMessenger`], provide it to the [`PeerManager`] via
58+
/// [`MessageHandler::onion_message_handler`], or directly if you're initializing the `PeerManager`
59+
/// via [`PeerManager::new_channel_only`].
60+
///
61+
/// # Example
62+
///
63+
/// ```
64+
/// # extern crate bitcoin;
65+
/// # use bitcoin::hashes::_export::_core::time::Duration;
66+
/// # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
67+
/// # use lightning::chain::keysinterface::{KeysManager, KeysInterface};
68+
/// # use lightning::onion_message::{Destination, OnionMessenger};
69+
/// # use lightning::onion_message::blinded_route::BlindedRoute;
70+
/// # use lightning::util::logger::{Logger, Record};
71+
/// # use std::sync::Arc;
72+
/// # struct FakeLogger {};
73+
/// # impl Logger for FakeLogger {
74+
/// # fn log(&self, record: &Record) { unimplemented!() }
75+
/// # }
76+
/// # let seed = [42u8; 32];
77+
/// # let time = Duration::from_secs(123456);
78+
/// # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
79+
/// # let logger = Arc::new(FakeLogger {});
80+
/// # let node_secret = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
81+
/// # let secp_ctx = Secp256k1::new();
82+
/// # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
83+
/// # let hop_node_id2 = hop_node_id1.clone();
84+
/// # let destination_node_id = hop_node_id1.clone();
85+
/// #
86+
/// // Create the onion messenger. This must use the same `keys_manager` as is passed to your
87+
/// // ChannelManager.
88+
/// let onion_messenger = OnionMessenger::new(&keys_manager, logger);
89+
///
90+
/// // Send an empty onion message to a node id.
91+
/// let intermediate_hops = vec![hop_node_id1, hop_node_id2];
92+
/// onion_messenger.send_onion_message(intermediate_hops, Destination::Node(destination_node_id));
93+
///
94+
/// // Create a blinded route to yourself, for someone to send an onion message to.
95+
/// # let your_node_id = hop_node_id1.clone();
96+
/// let hops = vec![hop_node_id1, hop_node_id2, your_node_id];
97+
/// let blinded_route = BlindedRoute::new(hops, &&keys_manager, &secp_ctx).unwrap();
98+
///
99+
/// // Send an empty onion message to a blinded route.
100+
/// # let intermediate_hops = vec![hop_node_id1, hop_node_id2];
101+
/// onion_messenger.send_onion_message(intermediate_hops, Destination::BlindedRoute(blinded_route));
102+
/// ```
43103
///
44104
/// [offers]: <https://github.com/lightning/bolts/pull/798>
105+
/// [`OnionMessenger`]: crate::onion_message::OnionMessenger
106+
/// [`PeerManager`]: crate::ln::peer_handler::PeerManager
107+
/// [`MessageHandler::onion_message_handler`]: crate::ln::peer_handler::MessageHandler::onion_message_handler
108+
/// [`PeerManager::new_channel_only`]: crate::ln::peer_handler::PeerManager::new_channel_only
45109
pub struct OnionMessenger<Signer: Sign, K: Deref, L: Deref>
46110
where K::Target: KeysInterface<Signer = Signer>,
47111
L::Target: Logger,

0 commit comments

Comments
 (0)