|
8 | 8 | // licenses.
|
9 | 9 |
|
10 | 10 | //! 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 |
11 | 23 | use bitcoin::hashes::{Hash, HashEngine};
|
12 | 24 | use bitcoin::hashes::hmac::{Hmac, HmacEngine};
|
13 | 25 | use bitcoin::hashes::sha256::Hash as Sha256;
|
@@ -347,8 +359,59 @@ impl Destination {
|
347 | 359 | }
|
348 | 360 |
|
349 | 361 | /// 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 |
| 364 | +/// receiving empty onion messages is supported. |
351 | 365 | ///
|
| 366 | +/// To set up the [`OnionMessenger`], provide it to the [`PeerManager`] via |
| 367 | +/// [`MessageHandler::onion_message_handler`], or directly if you're initializing the `PeerManager` |
| 368 | +/// via [`PeerManager::new_channel_only`]. |
| 369 | +/// |
| 370 | +/// # Example |
| 371 | +/// |
| 372 | +/// ``` |
| 373 | +/// # extern crate bitcoin; |
| 374 | +/// # use bitcoin::hashes::_export::_core::time::Duration; |
| 375 | +/// # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey}; |
| 376 | +/// # use lightning::chain::keysinterface::{KeysManager, KeysInterface}; |
| 377 | +/// # use lightning::onion_message::{BlindedRoute, Destination, OnionMessenger}; |
| 378 | +/// # use lightning::util::logger::{Logger, Record}; |
| 379 | +/// # use std::sync::Arc; |
| 380 | +/// # struct FakeLogger {}; |
| 381 | +/// # impl Logger for FakeLogger { |
| 382 | +/// # fn log(&self, record: &Record) { unimplemented!() } |
| 383 | +/// # } |
| 384 | +/// # let seed = [42u8; 32]; |
| 385 | +/// # let time = Duration::from_secs(123456); |
| 386 | +/// # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos()); |
| 387 | +/// # let logger = Arc::new(FakeLogger {}); |
| 388 | +/// # let node_secret = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap(); |
| 389 | +/// # let secp_ctx = Secp256k1::new(); |
| 390 | +/// # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret); |
| 391 | +/// # let hop_node_id2 = hop_node_id1.clone(); |
| 392 | +/// # let destination_node_id = hop_node_id1.clone(); |
| 393 | +/// # |
| 394 | +/// // Create the onion messenger. This must use the same `keys_manager` as is passed to your |
| 395 | +/// // ChannelManager. |
| 396 | +/// let onion_messenger = OnionMessenger::new(&keys_manager, logger); |
| 397 | +/// |
| 398 | +/// // Send an empty onion message to a node id. |
| 399 | +/// let intermediate_hops = vec![hop_node_id1, hop_node_id2]; |
| 400 | +/// onion_messenger.send_onion_message(intermediate_hops, Destination::Node(destination_node_id)); |
| 401 | +/// |
| 402 | +/// // Create a blinded route to yourself, for someone to send an onion message to. |
| 403 | +/// # let your_node_id = hop_node_id1.clone(); |
| 404 | +/// let hops = vec![hop_node_id1, hop_node_id2, your_node_id]; |
| 405 | +/// let blinded_route = BlindedRoute::new(hops, &&keys_manager, &secp_ctx).unwrap(); |
| 406 | +/// |
| 407 | +/// // Send an empty onion message to a blinded route. |
| 408 | +/// # let intermediate_hops = vec![hop_node_id1, hop_node_id2]; |
| 409 | +/// onion_messenger.send_onion_message(intermediate_hops, Destination::BlindedRoute(blinded_route)); |
| 410 | +/// ``` |
| 411 | +/// |
| 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 |
352 | 415 | /// [offers]: https://github.com/lightning/bolts/pull/798
|
353 | 416 | pub struct OnionMessenger<Signer: Sign, K: Deref, L: Deref>
|
354 | 417 | where K::Target: KeysInterface<Signer = Signer>,
|
|
0 commit comments