Skip to content

Commit 4a504b1

Browse files
Significantly expand onion message documentation
1 parent 6426b4a commit 4a504b1

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,56 @@ use sync::{Arc, Mutex};
2828
use prelude::*;
2929

3030
/// A sender, receiver and forwarder of onion messages. In upcoming releases, this object will be
31-
/// used to retrieve invoices and fulfill invoice requests from [offers].
31+
/// used to retrieve invoices and fulfill invoice requests from [offers]. Currently, only sending
32+
/// and receiving empty onion messages is supported.
33+
///
34+
/// # Example
35+
///
36+
// Needs to be `ignore` until the `onion_message` module is made public, otherwise this is a test
37+
// failure.
38+
/// ```ignore
39+
/// # extern crate bitcoin;
40+
/// # use bitcoin::hashes::_export::_core::time::Duration;
41+
/// # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
42+
/// # use lightning::chain::keysinterface::{InMemorySigner, KeysManager, KeysInterface};
43+
/// # use lightning::onion_message::{BlindedRoute, Destination, OnionMessenger};
44+
/// # use lightning::util::logger::{Logger, Record};
45+
/// # use std::sync::Arc;
46+
/// # struct FakeLogger {};
47+
/// # impl Logger for FakeLogger {
48+
/// # fn log(&self, record: &Record) { unimplemented!() }
49+
/// # }
50+
/// # let seed = [42u8; 32];
51+
/// # let time = Duration::from_secs(123456);
52+
/// # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
53+
/// # let logger = Arc::new(FakeLogger {});
54+
/// # let node_secret = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
55+
/// # let secp_ctx = Secp256k1::new();
56+
/// # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
57+
/// # let (hop_node_id2, hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1,
58+
/// hop_node_id1);
59+
/// # let destination_node_id = hop_node_id1;
60+
/// #
61+
/// // Create the onion messenger. This must use the same `keys_manager` as is passed to your
62+
/// // ChannelManager.
63+
/// let onion_messenger = OnionMessenger::new(&keys_manager, logger);
64+
///
65+
/// // Send an empty onion message to a node id.
66+
/// let intermediate_hops = [hop_node_id1, hop_node_id2];
67+
/// onion_messenger.send_onion_message(&intermediate_hops, Destination::Node(destination_node_id));
68+
///
69+
/// // Create a blinded route to yourself, for someone to send an onion message to.
70+
/// # let your_node_id = hop_node_id1;
71+
/// let hops = [hop_node_id3, hop_node_id4, your_node_id];
72+
/// let blinded_route = BlindedRoute::new::<InMemorySigner, _, _>(&hops, &keys_manager, &secp_ctx).unwrap();
73+
///
74+
/// // Send an empty onion message to a blinded route.
75+
/// # let intermediate_hops = [hop_node_id1, hop_node_id2];
76+
/// onion_messenger.send_onion_message(&intermediate_hops, Destination::BlindedRoute(blinded_route));
77+
/// ```
3278
///
3379
/// [offers]: <https://github.com/lightning/bolts/pull/798>
80+
/// [`OnionMessenger`]: crate::onion_message::OnionMessenger
3481
pub struct OnionMessenger<Signer: Sign, K: Deref, L: Deref>
3582
where K::Target: KeysInterface<Signer = Signer>,
3683
L::Target: Logger,
@@ -79,6 +126,7 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
79126
}
80127

81128
/// Send an empty onion message to `destination`, routing it through `intermediate_nodes`.
129+
/// See [`OnionMessenger`] for example usage.
82130
pub fn send_onion_message(&self, intermediate_nodes: &[PublicKey], destination: Destination) -> Result<(), secp256k1::Error> {
83131
let blinding_secret_bytes = self.keys_manager.get_secure_random_bytes();
84132
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");

lightning/src/onion_message/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
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
1122
1223
mod blinded_route;
1324
mod messenger;

0 commit comments

Comments
 (0)