Skip to content

Commit 5d9b7ef

Browse files
Significantly expand onion message documentation
1 parent 9777f7b commit 5d9b7ef

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

lightning/src/onion_message.rs

Lines changed: 65 additions & 2 deletions
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::BlindedRoute
22+
//! [`OnionMessenger`]: crate::onion_message::OnionMessenger
1123
use bitcoin::hashes::{Hash, HashEngine};
1224
use bitcoin::hashes::hmac::{Hmac, HmacEngine};
1325
use bitcoin::hashes::sha256::Hash as Sha256;
@@ -347,9 +359,60 @@ impl Destination {
347359
}
348360

349361
/// A sender, receiver and forwarder of onion messages. In upcoming releases, this object will be
350-
/// used to retrieve invoices and fulfill invoice requests from [offers].
362+
/// used to retrieve invoices and fulfill invoice requests from [offers]. Currently, only sending
363+
/// and receiving empty onion messages is supported.
351364
///
352-
/// [offers]: https://github.com/lightning/bolts/pull/798
365+
/// To set up the [`OnionMessenger`], provide it to the [`PeerManager`] via
366+
/// [`MessageHandler::onion_message_handler`], or directly if you're initializing the `PeerManager`
367+
/// via [`PeerManager::new_channel_only`].
368+
///
369+
/// # Example
370+
///
371+
/// ```
372+
/// # extern crate bitcoin;
373+
/// # use bitcoin::hashes::_export::_core::time::Duration;
374+
/// # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
375+
/// # use lightning::chain::keysinterface::{KeysManager, KeysInterface};
376+
/// # use lightning::onion_message::{BlindedRoute, Destination, OnionMessenger};
377+
/// # use lightning::util::logger::{Logger, Record};
378+
/// # use std::sync::Arc;
379+
/// # struct FakeLogger {};
380+
/// # impl Logger for FakeLogger {
381+
/// # fn log(&self, record: &Record) { unimplemented!() }
382+
/// # }
383+
/// # let seed = [42u8; 32];
384+
/// # let time = Duration::from_secs(123456);
385+
/// # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
386+
/// # let logger = Arc::new(FakeLogger {});
387+
/// # let node_secret = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
388+
/// # let secp_ctx = Secp256k1::new();
389+
/// # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
390+
/// # let hop_node_id2 = hop_node_id1.clone();
391+
/// # let destination_node_id = hop_node_id1.clone();
392+
/// #
393+
/// // Create the onion messenger. This must use the same `keys_manager` as is passed to your
394+
/// // ChannelManager.
395+
/// let onion_messenger = OnionMessenger::new(&keys_manager, logger);
396+
///
397+
/// // Send an empty onion message to a node id.
398+
/// let intermediate_hops = vec![hop_node_id1, hop_node_id2];
399+
/// onion_messenger.send_onion_message(intermediate_hops, Destination::Node(destination_node_id));
400+
///
401+
/// // Create a blinded route to yourself, for someone to send an onion message to.
402+
/// # let your_node_id = hop_node_id1.clone();
403+
/// let hops = vec![hop_node_id1, hop_node_id2, your_node_id];
404+
/// let blinded_route = BlindedRoute::new(hops, &&keys_manager, &secp_ctx).unwrap();
405+
///
406+
/// // Send an empty onion message to a blinded route.
407+
/// # let intermediate_hops = vec![hop_node_id1, hop_node_id2];
408+
/// onion_messenger.send_onion_message(intermediate_hops, Destination::BlindedRoute(blinded_route));
409+
/// ```
410+
///
411+
/// [offers]: <https://github.com/lightning/bolts/pull/798>
412+
/// [`OnionMessenger`]: crate::onion_message::OnionMessenger
413+
/// [`PeerManager`]: crate::ln::peer_handler::PeerManager
414+
/// [`MessageHandler::onion_message_handler`]: crate::ln::peer_handler::MessageHandler::onion_message_handler
415+
/// [`PeerManager::new_channel_only`]: crate::ln::peer_handler::PeerManager::new_channel_only
353416
pub struct OnionMessenger<Signer: Sign, K: Deref, L: Deref>
354417
where K::Target: KeysInterface<Signer = Signer>,
355418
L::Target: Logger,

0 commit comments

Comments
 (0)