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 onion messages. In upcoming releases, this object will be
13 * used to retrieve invoices and fulfill invoice requests from [offers]. Currently, only sending
14 * and receiving custom onion messages is supported.
19 * # extern crate bitcoin;
20 * # use bitcoin::hashes::_export::_core::time::Duration;
21 * # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
22 * # use lightning::chain::keysinterface::{InMemorySigner, KeysManager, KeysInterface};
23 * # use lightning::ln::msgs::DecodeError;
24 * # use lightning::ln::peer_handler::IgnoringMessageHandler;
25 * # use lightning::onion_message::messenger::{Destination, OnionMessenger};
26 * # use lightning::onion_message::packet::CustomOnionMessageContents;
27 * # use lightning::onion_message::blinded_route::BlindedRoute;
28 * # use lightning::util::logger::{Logger, Record};
29 * # use lightning::util::ser::{Writeable, Writer};
30 * # use lightning::io;
31 * # use std::sync::Arc;
32 * # struct FakeLogger {};
33 * # impl Logger for FakeLogger {
34 * # fn log(&self, record: &Record) { unimplemented!() }
36 * # let seed = [42u8; 32];
37 * # let time = Duration::from_secs(123456);
38 * # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
39 * # let logger = Arc::new(FakeLogger {});
40 * # let node_secret = SecretKey::from_slice(&hex::decode(\"0101010101010101010101010101010101010101010101010101010101010101\").unwrap()[..]).unwrap();
41 * # let secp_ctx = Secp256k1::new();
42 * # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
43 * # let (hop_node_id2, hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1, hop_node_id1);
44 * # let destination_node_id = hop_node_id1;
45 * # let your_custom_message_handler = IgnoringMessageHandler {};
46 * Create the onion messenger. This must use the same `keys_manager` as is passed to your
48 * let onion_messenger = OnionMessenger::new(&keys_manager, logger, your_custom_message_handler);
51 * # struct YourCustomMessage {}
52 * impl Writeable for YourCustomMessage {
53 * \tfn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
55 * \t\t// Write your custom onion message to `w`
58 * impl CustomOnionMessageContents for YourCustomMessage {
59 * \tfn tlv_type(&self) -> u64 {
60 * \t\t# let your_custom_message_type = 42;
61 * \t\tyour_custom_message_type
64 * Send a custom onion message to a node id.
65 * let intermediate_hops = [hop_node_id1, hop_node_id2];
66 * let reply_path = None;
67 * # let your_custom_message = YourCustomMessage {};
68 * onion_messenger.send_custom_onion_message(&intermediate_hops, Destination::Node(destination_node_id), your_custom_message, reply_path);
70 * Create a blinded route to yourself, for someone to send an onion message to.
71 * # let your_node_id = hop_node_id1;
72 * let hops = [hop_node_id3, hop_node_id4, your_node_id];
73 * let blinded_route = BlindedRoute::new(&hops, &keys_manager, &secp_ctx).unwrap();
75 * Send a custom onion message to a blinded route.
76 * # let intermediate_hops = [hop_node_id1, hop_node_id2];
77 * let reply_path = None;
78 * # let your_custom_message = YourCustomMessage {};
79 * onion_messenger.send_custom_onion_message(&intermediate_hops, Destination::BlindedRoute(blinded_route), your_custom_message, reply_path);
82 * [offers]: <https://github.com/lightning/bolts/pull/798>
83 * [`OnionMessenger`]: crate::onion_message::OnionMessenger
85 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
86 public class OnionMessenger extends CommonBase {
87 OnionMessenger(Object _dummy, long ptr) { super(ptr); }
88 @Override @SuppressWarnings("deprecation")
89 protected void finalize() throws Throwable {
91 if (ptr != 0) { bindings.OnionMessenger_free(ptr); }
95 * Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
96 * their respective handlers.
98 public static OnionMessenger of(KeysInterface keys_manager, Logger logger, CustomOnionMessageHandler custom_handler) {
99 long ret = bindings.OnionMessenger_new(keys_manager == null ? 0 : keys_manager.ptr, logger == null ? 0 : logger.ptr, custom_handler == null ? 0 : custom_handler.ptr);
100 Reference.reachabilityFence(keys_manager);
101 Reference.reachabilityFence(logger);
102 Reference.reachabilityFence(custom_handler);
103 if (ret >= 0 && ret <= 4096) { return null; }
104 org.ldk.structs.OnionMessenger ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OnionMessenger(null, ret); }
105 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
106 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(keys_manager); };
107 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
108 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(custom_handler); };
113 * Send an onion message with contents `message` to `destination`, routing it through `intermediate_nodes`.
114 * See [`OnionMessenger`] for example usage.
116 * Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None
118 public Result_NoneSendErrorZ send_custom_onion_message(byte[][] intermediate_nodes, Destination destination, CustomOnionMessageContents msg, @Nullable BlindedRoute reply_path) {
119 long ret = bindings.OnionMessenger_send_custom_onion_message(this.ptr, intermediate_nodes != null ? Arrays.stream(intermediate_nodes).map(intermediate_nodes_conv_8 -> InternalUtils.check_arr_len(intermediate_nodes_conv_8, 33)).toArray(byte[][]::new) : null, destination.ptr, msg == null ? 0 : msg.ptr, reply_path == null ? 0 : reply_path.ptr);
120 Reference.reachabilityFence(this);
121 Reference.reachabilityFence(intermediate_nodes);
122 Reference.reachabilityFence(destination);
123 Reference.reachabilityFence(msg);
124 Reference.reachabilityFence(reply_path);
125 if (ret >= 0 && ret <= 4096) { return null; }
126 Result_NoneSendErrorZ ret_hu_conv = Result_NoneSendErrorZ.constr_from_ptr(ret);
127 if (this != null) { this.ptrs_to.add(msg); };
128 if (this != null) { this.ptrs_to.add(reply_path); };
133 * Constructs a new OnionMessageHandler which calls the relevant methods on this_arg.
134 * This copies the `inner` pointer in this_arg and thus the returned OnionMessageHandler must be freed before this_arg is
136 public OnionMessageHandler as_OnionMessageHandler() {
137 long ret = bindings.OnionMessenger_as_OnionMessageHandler(this.ptr);
138 Reference.reachabilityFence(this);
139 if (ret >= 0 && ret <= 4096) { return null; }
140 OnionMessageHandler ret_hu_conv = new OnionMessageHandler(null, ret);
141 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
146 * Constructs a new OnionMessageProvider which calls the relevant methods on this_arg.
147 * This copies the `inner` pointer in this_arg and thus the returned OnionMessageProvider must be freed before this_arg is
149 public OnionMessageProvider as_OnionMessageProvider() {
150 long ret = bindings.OnionMessenger_as_OnionMessageProvider(this.ptr);
151 Reference.reachabilityFence(this);
152 if (ret >= 0 && ret <= 4096) { return null; }
153 OnionMessageProvider ret_hu_conv = new OnionMessageProvider(null, ret);
154 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };