@@ -28,9 +28,56 @@ use sync::{Arc, Mutex};
28
28
use prelude:: * ;
29
29
30
30
/// 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
+ /// ```
32
78
///
33
79
/// [offers]: <https://github.com/lightning/bolts/pull/798>
80
+ /// [`OnionMessenger`]: crate::onion_message::OnionMessenger
34
81
pub struct OnionMessenger < Signer : Sign , K : Deref , L : Deref >
35
82
where K :: Target : KeysInterface < Signer = Signer > ,
36
83
L :: Target : Logger ,
@@ -79,6 +126,7 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
79
126
}
80
127
81
128
/// Send an empty onion message to `destination`, routing it through `intermediate_nodes`.
129
+ /// See [`OnionMessenger`] for example usage.
82
130
pub fn send_onion_message ( & self , intermediate_nodes : & [ PublicKey ] , destination : Destination ) -> Result < ( ) , secp256k1:: Error > {
83
131
let blinding_secret_bytes = self . keys_manager . get_secure_random_bytes ( ) ;
84
132
let blinding_secret = SecretKey :: from_slice ( & blinding_secret_bytes[ ..] ) . expect ( "RNG is busted" ) ;
0 commit comments