6 namespace org { namespace ldk { namespace structs {
10 * A sender, receiver and forwarder of [`OnionMessage`]s.
14 * `OnionMessenger` implements [`OnionMessageHandler`], making it responsible for either forwarding
15 * messages to peers or delegating to the appropriate handler for the message type. Currently, the
16 * available handlers are:
17 * [`OffersMessageHandler`], for responding to [`InvoiceRequest`]s and paying [`Bolt12Invoice`]s
18 * [`CustomOnionMessageHandler`], for handling user-defined message types
22 * [`OnionMessage`]s are sent initially using [`OnionMessenger::send_onion_message`]. When handling
23 * a message, the matched handler may return a response message which `OnionMessenger` will send
29 * # extern crate bitcoin;
30 * # use bitcoin::hashes::_export::_core::time::Duration;
31 * # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
32 * # use lightning::blinded_path::BlindedPath;
33 * # use lightning::sign::KeysManager;
34 * # use lightning::ln::peer_handler::IgnoringMessageHandler;
35 * # use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessenger, OnionMessagePath};
36 * # use lightning::onion_message::packet::OnionMessageContents;
37 * # use lightning::util::logger::{Logger, Record};
38 * # use lightning::util::ser::{Writeable, Writer};
39 * # use lightning::io;
40 * # use std::sync::Arc;
41 * # struct FakeLogger;
42 * # impl Logger for FakeLogger {
43 * # fn log(&self, record: &Record) { unimplemented!() }
45 * # struct FakeMessageRouter {}
46 * # impl MessageRouter for FakeMessageRouter {
47 * # fn find_path(&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination) -> Result<OnionMessagePath, ()> {
51 * # let seed = [42u8; 32];
52 * # let time = Duration::from_secs(123456);
53 * # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
54 * # let logger = Arc::new(FakeLogger {});
55 * # let node_secret = SecretKey::from_slice(&hex::decode(\"0101010101010101010101010101010101010101010101010101010101010101\").unwrap()[..]).unwrap();
56 * # let secp_ctx = Secp256k1::new();
57 * # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
58 * # let (hop_node_id2, hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1, hop_node_id1);
59 * # let destination_node_id = hop_node_id1;
60 * # let message_router = Arc::new(FakeMessageRouter {});
61 * # let custom_message_handler = IgnoringMessageHandler {};
62 * # let offers_message_handler = IgnoringMessageHandler {};
63 * Create the onion messenger. This must use the same `keys_manager` as is passed to your
65 * let onion_messenger = OnionMessenger::new(
66 * &keys_manager, &keys_manager, logger, message_router, &offers_message_handler,
67 * &custom_message_handler
71 * # struct YourCustomMessage {}
72 * impl Writeable for YourCustomMessage {
73 * \tfn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
75 * \t\t// Write your custom onion message to `w`
78 * impl OnionMessageContents for YourCustomMessage {
79 * \tfn tlv_type(&self) -> u64 {
80 * \t\t# let your_custom_message_type = 42;
81 * \t\tyour_custom_message_type
84 * Send a custom onion message to a node id.
85 * let path = OnionMessagePath {
86 * \tintermediate_nodes: vec![hop_node_id1, hop_node_id2],
87 * \tdestination: Destination::Node(destination_node_id),
89 * let reply_path = None;
90 * # let message = YourCustomMessage {};
91 * onion_messenger.send_onion_message(path, message, reply_path);
93 * Create a blinded path to yourself, for someone to send an onion message to.
94 * # let your_node_id = hop_node_id1;
95 * let hops = [hop_node_id3, hop_node_id4, your_node_id];
96 * let blinded_path = BlindedPath::new_for_message(&hops, &keys_manager, &secp_ctx).unwrap();
98 * Send a custom onion message to a blinded path.
99 * let path = OnionMessagePath {
100 * \tintermediate_nodes: vec![hop_node_id1, hop_node_id2],
101 * \tdestination: Destination::BlindedPath(blinded_path),
103 * let reply_path = None;
104 * # let message = YourCustomMessage {};
105 * onion_messenger.send_onion_message(path, message, reply_path);
108 * [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
109 * [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
111 public class OnionMessenger : CommonBase {
112 internal OnionMessenger(object _dummy, long ptr) : base(ptr) { }
114 if (ptr != 0) { bindings.OnionMessenger_free(ptr); }
118 * Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
119 * their respective handlers.
121 public static OnionMessenger of(org.ldk.structs.EntropySource entropy_source, org.ldk.structs.NodeSigner node_signer, org.ldk.structs.Logger logger, org.ldk.structs.MessageRouter message_router, org.ldk.structs.OffersMessageHandler offers_handler, org.ldk.structs.CustomOnionMessageHandler custom_handler) {
122 long ret = bindings.OnionMessenger_new(entropy_source.ptr, node_signer.ptr, logger.ptr, message_router.ptr, offers_handler.ptr, custom_handler.ptr);
123 GC.KeepAlive(entropy_source);
124 GC.KeepAlive(node_signer);
125 GC.KeepAlive(logger);
126 GC.KeepAlive(message_router);
127 GC.KeepAlive(offers_handler);
128 GC.KeepAlive(custom_handler);
129 if (ret >= 0 && ret <= 4096) { return null; }
130 org.ldk.structs.OnionMessenger ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OnionMessenger(null, ret); }
131 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
132 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(entropy_source); };
133 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(node_signer); };
134 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(logger); };
135 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(message_router); };
136 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(offers_handler); };
137 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(custom_handler); };
142 * Sends an [`OnionMessage`] with the given `contents` for sending to the destination of
145 * See [`OnionMessenger`] for example usage.
147 * Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None
149 public Result_NoneSendErrorZ send_onion_message(org.ldk.structs.OnionMessagePath path, org.ldk.structs.OnionMessageContents contents, org.ldk.structs.BlindedPath reply_path) {
150 long ret = bindings.OnionMessenger_send_onion_message(this.ptr, path == null ? 0 : path.ptr, contents.ptr, reply_path == null ? 0 : reply_path.ptr);
153 GC.KeepAlive(contents);
154 GC.KeepAlive(reply_path);
155 if (ret >= 0 && ret <= 4096) { return null; }
156 Result_NoneSendErrorZ ret_hu_conv = Result_NoneSendErrorZ.constr_from_ptr(ret);
157 if (this != null) { this.ptrs_to.AddLast(path); };
158 if (this != null) { this.ptrs_to.AddLast(contents); };
159 if (this != null) { this.ptrs_to.AddLast(reply_path); };
164 * Constructs a new OnionMessageHandler which calls the relevant methods on this_arg.
165 * This copies the `inner` pointer in this_arg and thus the returned OnionMessageHandler must be freed before this_arg is
167 public OnionMessageHandler as_OnionMessageHandler() {
168 long ret = bindings.OnionMessenger_as_OnionMessageHandler(this.ptr);
170 if (ret >= 0 && ret <= 4096) { return null; }
171 OnionMessageHandler ret_hu_conv = new OnionMessageHandler(null, ret);
172 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };