1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
12 * A sender, receiver and forwarder of [`OnionMessage`]s.
16 * `OnionMessenger` implements [`OnionMessageHandler`], making it responsible for either forwarding
17 * messages to peers or delegating to the appropriate handler for the message type. Currently, the
18 * available handlers are:
19 * [`OffersMessageHandler`], for responding to [`InvoiceRequest`]s and paying [`Bolt12Invoice`]s
20 * [`CustomOnionMessageHandler`], for handling user-defined message types
24 * [`OnionMessage`]s are sent initially using [`OnionMessenger::send_onion_message`]. When handling
25 * a message, the matched handler may return a response message which `OnionMessenger` will send
31 * # extern crate bitcoin;
32 * # use bitcoin::hashes::_export::_core::time::Duration;
33 * # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
34 * # use lightning::blinded_path::BlindedPath;
35 * # use lightning::sign::KeysManager;
36 * # use lightning::ln::peer_handler::IgnoringMessageHandler;
37 * # use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessenger, OnionMessagePath};
38 * # use lightning::onion_message::packet::OnionMessageContents;
39 * # use lightning::util::logger::{Logger, Record};
40 * # use lightning::util::ser::{Writeable, Writer};
41 * # use lightning::io;
42 * # use std::sync::Arc;
43 * # struct FakeLogger;
44 * # impl Logger for FakeLogger {
45 * # fn log(&self, record: &Record) { unimplemented!() }
47 * # struct FakeMessageRouter {}
48 * # impl MessageRouter for FakeMessageRouter {
49 * # fn find_path(&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination) -> Result<OnionMessagePath, ()> {
53 * # let seed = [42u8; 32];
54 * # let time = Duration::from_secs(123456);
55 * # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
56 * # let logger = Arc::new(FakeLogger {});
57 * # let node_secret = SecretKey::from_slice(&hex::decode(\"0101010101010101010101010101010101010101010101010101010101010101\").unwrap()[..]).unwrap();
58 * # let secp_ctx = Secp256k1::new();
59 * # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
60 * # let (hop_node_id2, hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1, hop_node_id1);
61 * # let destination_node_id = hop_node_id1;
62 * # let message_router = Arc::new(FakeMessageRouter {});
63 * # let custom_message_handler = IgnoringMessageHandler {};
64 * # let offers_message_handler = IgnoringMessageHandler {};
65 * Create the onion messenger. This must use the same `keys_manager` as is passed to your
67 * let onion_messenger = OnionMessenger::new(
68 * &keys_manager, &keys_manager, logger, message_router, &offers_message_handler,
69 * &custom_message_handler
73 * # struct YourCustomMessage {}
74 * impl Writeable for YourCustomMessage {
75 * \tfn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
77 * \t\t// Write your custom onion message to `w`
80 * impl OnionMessageContents for YourCustomMessage {
81 * \tfn tlv_type(&self) -> u64 {
82 * \t\t# let your_custom_message_type = 42;
83 * \t\tyour_custom_message_type
86 * Send a custom onion message to a node id.
87 * let path = OnionMessagePath {
88 * \tintermediate_nodes: vec![hop_node_id1, hop_node_id2],
89 * \tdestination: Destination::Node(destination_node_id),
91 * let reply_path = None;
92 * # let message = YourCustomMessage {};
93 * onion_messenger.send_onion_message(path, message, reply_path);
95 * Create a blinded path to yourself, for someone to send an onion message to.
96 * # let your_node_id = hop_node_id1;
97 * let hops = [hop_node_id3, hop_node_id4, your_node_id];
98 * let blinded_path = BlindedPath::new_for_message(&hops, &keys_manager, &secp_ctx).unwrap();
100 * Send a custom onion message to a blinded path.
101 * let path = OnionMessagePath {
102 * \tintermediate_nodes: vec![hop_node_id1, hop_node_id2],
103 * \tdestination: Destination::BlindedPath(blinded_path),
105 * let reply_path = None;
106 * # let message = YourCustomMessage {};
107 * onion_messenger.send_onion_message(path, message, reply_path);
110 * [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
111 * [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
113 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
114 public class OnionMessenger extends CommonBase {
115 OnionMessenger(Object _dummy, long ptr) { super(ptr); }
116 @Override @SuppressWarnings("deprecation")
117 protected void finalize() throws Throwable {
119 if (ptr != 0) { bindings.OnionMessenger_free(ptr); }
123 * Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
124 * their respective handlers.
126 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) {
127 long ret = bindings.OnionMessenger_new(entropy_source.ptr, node_signer.ptr, logger.ptr, message_router.ptr, offers_handler.ptr, custom_handler.ptr);
128 Reference.reachabilityFence(entropy_source);
129 Reference.reachabilityFence(node_signer);
130 Reference.reachabilityFence(logger);
131 Reference.reachabilityFence(message_router);
132 Reference.reachabilityFence(offers_handler);
133 Reference.reachabilityFence(custom_handler);
134 if (ret >= 0 && ret <= 4096) { return null; }
135 org.ldk.structs.OnionMessenger ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OnionMessenger(null, ret); }
136 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
137 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(entropy_source); };
138 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(node_signer); };
139 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
140 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_router); };
141 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(offers_handler); };
142 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(custom_handler); };
147 * Sends an [`OnionMessage`] with the given `contents` for sending to the destination of
150 * See [`OnionMessenger`] for example usage.
152 * Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None
154 public Result_NoneSendErrorZ send_onion_message(org.ldk.structs.OnionMessagePath path, org.ldk.structs.OnionMessageContents contents, @Nullable org.ldk.structs.BlindedPath reply_path) {
155 long ret = bindings.OnionMessenger_send_onion_message(this.ptr, path == null ? 0 : path.ptr, contents.ptr, reply_path == null ? 0 : reply_path.ptr);
156 Reference.reachabilityFence(this);
157 Reference.reachabilityFence(path);
158 Reference.reachabilityFence(contents);
159 Reference.reachabilityFence(reply_path);
160 if (ret >= 0 && ret <= 4096) { return null; }
161 Result_NoneSendErrorZ ret_hu_conv = Result_NoneSendErrorZ.constr_from_ptr(ret);
162 if (this != null) { this.ptrs_to.add(path); };
163 if (this != null) { this.ptrs_to.add(contents); };
164 if (this != null) { this.ptrs_to.add(reply_path); };
169 * Constructs a new OnionMessageHandler which calls the relevant methods on this_arg.
170 * This copies the `inner` pointer in this_arg and thus the returned OnionMessageHandler must be freed before this_arg is
172 public OnionMessageHandler as_OnionMessageHandler() {
173 long ret = bindings.OnionMessenger_as_OnionMessageHandler(this.ptr);
174 Reference.reachabilityFence(this);
175 if (ret >= 0 && ret <= 4096) { return null; }
176 OnionMessageHandler ret_hu_conv = new OnionMessageHandler(null, ret);
177 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };