2315e3bae777f1089bca3762fa0c3928cafd9cdf
[ldk-java] / src / main / java / org / ldk / structs / OnionMessenger.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10
11 /**
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.
15  * 
16  * # Example
17  * 
18  * ```
19  * # extern crate bitcoin;
20  * # use bitcoin::hashes::_export::_core::time::Duration;
21  * # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
22  * # use lightning::blinded_path::BlindedPath;
23  * # use lightning::chain::keysinterface::KeysManager;
24  * # use lightning::ln::peer_handler::IgnoringMessageHandler;
25  * # use lightning::onion_message::messenger::{Destination, OnionMessenger};
26  * # use lightning::onion_message::packet::{CustomOnionMessageContents, OnionMessageContents};
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!() }
34  * # }
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
46  * ChannelManager.
47  * let onion_messenger = OnionMessenger::new(&keys_manager, &keys_manager, logger, &your_custom_message_handler);
48  * 
49  * # #[derive(Clone)]
50  * # struct YourCustomMessage {}
51  * impl Writeable for YourCustomMessage {
52  * \tfn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
53  * \t\t# Ok(())
54  * \t\t// Write your custom onion message to `w`
55  * \t}
56  * }
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
61  * \t}
62  * }
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);
69  * 
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_for_message(&hops, &keys_manager, &secp_ctx).unwrap();
74  * 
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);
81  * ```
82  * 
83  * [offers]: <https://github.com/lightning/bolts/pull/798>
84  * [`OnionMessenger`]: crate::onion_message::OnionMessenger
85  */
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 {
91                 super.finalize();
92                 if (ptr != 0) { bindings.OnionMessenger_free(ptr); }
93         }
94
95         /**
96          * Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
97          * their respective handlers.
98          */
99         public static OnionMessenger of(org.ldk.structs.EntropySource entropy_source, org.ldk.structs.NodeSigner node_signer, org.ldk.structs.Logger logger, org.ldk.structs.CustomOnionMessageHandler custom_handler) {
100                 long ret = bindings.OnionMessenger_new(entropy_source == null ? 0 : entropy_source.ptr, node_signer == null ? 0 : node_signer.ptr, logger == null ? 0 : logger.ptr, custom_handler == null ? 0 : custom_handler.ptr);
101                 Reference.reachabilityFence(entropy_source);
102                 Reference.reachabilityFence(node_signer);
103                 Reference.reachabilityFence(logger);
104                 Reference.reachabilityFence(custom_handler);
105                 if (ret >= 0 && ret <= 4096) { return null; }
106                 org.ldk.structs.OnionMessenger ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OnionMessenger(null, ret); }
107                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
108                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(entropy_source); };
109                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(node_signer); };
110                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
111                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(custom_handler); };
112                 return ret_hu_conv;
113         }
114
115         /**
116          * Send an onion message with contents `message` to `destination`, routing it through `intermediate_nodes`.
117          * See [`OnionMessenger`] for example usage.
118          * 
119          * Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None
120          */
121         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) {
122                 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);
123                 Reference.reachabilityFence(this);
124                 Reference.reachabilityFence(intermediate_nodes);
125                 Reference.reachabilityFence(destination);
126                 Reference.reachabilityFence(message);
127                 Reference.reachabilityFence(reply_path);
128                 if (ret >= 0 && ret <= 4096) { return null; }
129                 Result_NoneSendErrorZ ret_hu_conv = Result_NoneSendErrorZ.constr_from_ptr(ret);
130                 if (this != null) { this.ptrs_to.add(destination); };
131                 if (this != null) { this.ptrs_to.add(message); };
132                 if (this != null) { this.ptrs_to.add(reply_path); };
133                 return ret_hu_conv;
134         }
135
136         /**
137          * Constructs a new OnionMessageHandler which calls the relevant methods on this_arg.
138          * This copies the `inner` pointer in this_arg and thus the returned OnionMessageHandler must be freed before this_arg is
139          */
140         public OnionMessageHandler as_OnionMessageHandler() {
141                 long ret = bindings.OnionMessenger_as_OnionMessageHandler(this.ptr);
142                 Reference.reachabilityFence(this);
143                 if (ret >= 0 && ret <= 4096) { return null; }
144                 OnionMessageHandler ret_hu_conv = new OnionMessageHandler(null, ret);
145                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
146                 return ret_hu_conv;
147         }
148
149         /**
150          * Constructs a new OnionMessageProvider which calls the relevant methods on this_arg.
151          * This copies the `inner` pointer in this_arg and thus the returned OnionMessageProvider must be freed before this_arg is
152          */
153         public OnionMessageProvider as_OnionMessageProvider() {
154                 long ret = bindings.OnionMessenger_as_OnionMessageProvider(this.ptr);
155                 Reference.reachabilityFence(this);
156                 if (ret >= 0 && ret <= 4096) { return null; }
157                 OnionMessageProvider ret_hu_conv = new OnionMessageProvider(null, ret);
158                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
159                 return ret_hu_conv;
160         }
161
162 }