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::blinded_path::BlindedPath;
26 * # use lightning::onion_message::messenger::{CustomOnionMessageContents, Destination, OnionMessageContents, OnionMessenger};
27 * # use lightning::util::logger::{Logger, Record};
28 * # use lightning::util::ser::{Writeable, Writer};
29 * # use lightning::io;
30 * # use std::sync::Arc;
31 * # struct FakeLogger {};
32 * # impl Logger for FakeLogger {
33 * # fn log(&self, record: &Record) { unimplemented!() }
35 * # let seed = [42u8; 32];
36 * # let time = Duration::from_secs(123456);
37 * # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
38 * # let logger = Arc::new(FakeLogger {});
39 * # let node_secret = SecretKey::from_slice(&hex::decode(\"0101010101010101010101010101010101010101010101010101010101010101\").unwrap()[..]).unwrap();
40 * # let secp_ctx = Secp256k1::new();
41 * # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
42 * # let (hop_node_id2, hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1, hop_node_id1);
43 * # let destination_node_id = hop_node_id1;
44 * # let your_custom_message_handler = IgnoringMessageHandler {};
45 * Create the onion messenger. This must use the same `keys_manager` as is passed to your
47 * let onion_messenger = OnionMessenger::new(&keys_manager, logger, your_custom_message_handler);
50 * # struct YourCustomMessage {}
51 * impl Writeable for YourCustomMessage {
52 * \tfn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
54 * \t\t// Write your custom onion message to `w`
57 * impl CustomOnionMessageContents for YourCustomMessage {
58 * \tfn tlv_type(&self) -> u64 {
59 * \t\t# let your_custom_message_type = 42;
60 * \t\tyour_custom_message_type
63 * Send a custom onion message to a node id.
64 * let intermediate_hops = [hop_node_id1, hop_node_id2];
65 * let reply_path = None;
66 * # let your_custom_message = YourCustomMessage {};
67 * let message = OnionMessageContents::Custom(your_custom_message);
68 * onion_messenger.send_onion_message(&intermediate_hops, Destination::Node(destination_node_id), message, reply_path);
70 * Create a blinded path 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_path = BlindedPath::new(&hops, &keys_manager, &secp_ctx).unwrap();
75 * Send a custom onion message to a blinded path.
76 * # let intermediate_hops = [hop_node_id1, hop_node_id2];
77 * let reply_path = None;
78 * # let your_custom_message = YourCustomMessage {};
79 * let message = OnionMessageContents::Custom(your_custom_message);
80 * onion_messenger.send_onion_message(&intermediate_hops, Destination::BlindedPath(blinded_path), message, reply_path);
83 * [offers]: <https://github.com/lightning/bolts/pull/798>
84 * [`OnionMessenger`]: crate::onion_message::OnionMessenger
86 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
87 public class OnionMessenger extends CommonBase {
88 OnionMessenger(Object _dummy, long ptr) { super(ptr); }
89 @Override @SuppressWarnings("deprecation")
90 protected void finalize() throws Throwable {
92 if (ptr != 0) { bindings.OnionMessenger_free(ptr); }
96 * Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
97 * their respective handlers.
99 public static OnionMessenger of(org.ldk.structs.KeysInterface keys_manager, org.ldk.structs.Logger logger, org.ldk.structs.CustomOnionMessageHandler custom_handler) {
100 long ret = bindings.OnionMessenger_new(keys_manager == null ? 0 : keys_manager.ptr, logger == null ? 0 : logger.ptr, custom_handler == null ? 0 : custom_handler.ptr);
101 Reference.reachabilityFence(keys_manager);
102 Reference.reachabilityFence(logger);
103 Reference.reachabilityFence(custom_handler);
104 if (ret >= 0 && ret <= 4096) { return null; }
105 org.ldk.structs.OnionMessenger ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OnionMessenger(null, ret); }
106 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
107 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(keys_manager); };
108 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
109 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(custom_handler); };
114 * Send an onion message with contents `message` to `destination`, routing it through `intermediate_nodes`.
115 * See [`OnionMessenger`] for example usage.
117 * Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None
119 public Result_NoneSendErrorZ send_onion_message(byte[][] intermediate_nodes, org.ldk.structs.Destination destination, org.ldk.structs.OnionMessageContents message, @Nullable org.ldk.structs.BlindedPath reply_path) {
120 long ret = bindings.OnionMessenger_send_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, message.ptr, reply_path == null ? 0 : reply_path.ptr);
121 Reference.reachabilityFence(this);
122 Reference.reachabilityFence(intermediate_nodes);
123 Reference.reachabilityFence(destination);
124 Reference.reachabilityFence(message);
125 Reference.reachabilityFence(reply_path);
126 if (ret >= 0 && ret <= 4096) { return null; }
127 Result_NoneSendErrorZ ret_hu_conv = Result_NoneSendErrorZ.constr_from_ptr(ret);
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); };