Skip to content

Commit c205bf9

Browse files
Add module-level documentation for onion messages
1 parent fe26806 commit c205bf9

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

lightning/src/onion_message.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,61 @@
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+
//! # Example
18+
//!
19+
//! ```
20+
//! # use lightning::chain::keysinterface::{KeysManager, KeysInterface};
21+
//! # use lightning::onion_message::{BlindedRoute, OnionMessenger};
22+
//! # use lightning::util::logger::{Logger, Record};
23+
//! # struct FakeLogger {};
24+
//! # impl Logger for FakeLogger {
25+
//! # fn log(&self, record: &Record) { unimplemented!() }
26+
//! # }
27+
//! # let seed = [42u8; 32];
28+
//! # let time = Duration::from_secs(123456);
29+
//! # let keys_manager = Arc::new(KeysManager::new(seed, time.as_secs(), time.subsec_nanos()));
30+
//! # let logger = Arc::new(FakeLogger {});
31+
//! # let node_secret = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
32+
//! # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
33+
//! # let hop_node_id2 = hop_node_id1.clone();
34+
//! # let destination_node_id = hop_node_id1.clone();
35+
//! #
36+
//! // Create the onion messenger. This must use the same `keys_manager` as is passed to your
37+
//! // ChannelManager.
38+
//! let onion_messenger = OnionMessenger::new(keys_manager, logger);
39+
//!
40+
//! // Hook up the OnionMessenger to your PeerManager, for sending and receiving messages on the
41+
//! // wire.
42+
//! # let chan_handler = IgnoringMessageHandler {};
43+
//! # let route_handler = IgnoringMessageHandler {};
44+
//! # let custom_message_handler = IgnoringMessageHandler {};
45+
//! # let rand_bytes = [0; 32];
46+
//! let message_handler = MessageHandler { chan_handler, route_handler, onion_messenger };
47+
//! let peer_manager = PeerManager::new(message_handler, node_secret, &rand_bytes, logger,
48+
//! custom_message_handler);
49+
//!
50+
//! // Send an empty onion message to a node id.
51+
//! let intermediate_hops = vec![hop_node_id1, hop_node_id2];
52+
//! onion_messenger.send_onion_message(intermediate_hops, Destination::Node(destination_node_id));
53+
//!
54+
//! // Create a blinded route to yourself, for someone to send an onion message to.
55+
//! # let secp_ctx = Secp256k1::new();
56+
//! # let your_node_id = hop_node_id1.clone();
57+
//! let hops = vec![hop_node_id_1, hop_node_id_2, your_node_id];
58+
//! let blinded_route = BlindedRoute::new(hops, keys_manager, &secp_ctx).unwrap();
59+
//!
60+
//! // Send an empty onion message to a blinded route.
61+
//! onion_messenger.send_onion_message(intermediate_hops, Destination::BlindedRoute(blinded_route));
62+
//! ```
63+
//!
64+
//! [Offers]: https://github.com/lightning/bolts/pull/798
65+
//! [blinded routes]: crate::onion_message::BlindedRoute
1166
use bitcoin::hashes::{Hash, HashEngine};
1267
use bitcoin::hashes::hmac::{Hmac, HmacEngine};
1368
use bitcoin::hashes::sha256::Hash as Sha256;

0 commit comments

Comments
 (0)