[Java] Update auto-generated Java files
[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 empty 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::chain::keysinterface::{InMemorySigner, KeysManager, KeysInterface};
23  * # use lightning::onion_message::messenger::{Destination, OnionMessenger};
24  * # use lightning::onion_message::blinded_route::BlindedRoute;
25  * # use lightning::util::logger::{Logger, Record};
26  * # use std::sync::Arc;
27  * # struct FakeLogger {};
28  * # impl Logger for FakeLogger {
29  * #     fn log(&self, record: &Record) { unimplemented!() }
30  * # }
31  * # let seed = [42u8; 32];
32  * # let time = Duration::from_secs(123456);
33  * # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
34  * # let logger = Arc::new(FakeLogger {});
35  * # let node_secret = SecretKey::from_slice(&hex::decode(\"0101010101010101010101010101010101010101010101010101010101010101\").unwrap()[..]).unwrap();
36  * # let secp_ctx = Secp256k1::new();
37  * # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
38  * # let (hop_node_id2, hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1,
39  * hop_node_id1);
40  * # let destination_node_id = hop_node_id1;
41  * #
42  * Create the onion messenger. This must use the same `keys_manager` as is passed to your
43  * ChannelManager.
44  * let onion_messenger = OnionMessenger::new(&keys_manager, logger);
45  * 
46  * Send an empty onion message to a node id.
47  * let intermediate_hops = [hop_node_id1, hop_node_id2];
48  * let reply_path = None;
49  * onion_messenger.send_onion_message(&intermediate_hops, Destination::Node(destination_node_id), reply_path);
50  * 
51  * Create a blinded route to yourself, for someone to send an onion message to.
52  * # let your_node_id = hop_node_id1;
53  * let hops = [hop_node_id3, hop_node_id4, your_node_id];
54  * let blinded_route = BlindedRoute::new(&hops, &keys_manager, &secp_ctx).unwrap();
55  * 
56  * Send an empty onion message to a blinded route.
57  * # let intermediate_hops = [hop_node_id1, hop_node_id2];
58  * let reply_path = None;
59  * onion_messenger.send_onion_message(&intermediate_hops, Destination::BlindedRoute(blinded_route), reply_path);
60  * ```
61  * 
62  * [offers]: <https://github.com/lightning/bolts/pull/798>
63  * [`OnionMessenger`]: crate::onion_message::OnionMessenger
64  */
65 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
66 public class OnionMessenger extends CommonBase {
67         OnionMessenger(Object _dummy, long ptr) { super(ptr); }
68         @Override @SuppressWarnings("deprecation")
69         protected void finalize() throws Throwable {
70                 super.finalize();
71                 if (ptr != 0) { bindings.OnionMessenger_free(ptr); }
72         }
73
74         /**
75          * Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
76          * their respective handlers.
77          */
78         public static OnionMessenger of(KeysInterface keys_manager, Logger logger) {
79                 long ret = bindings.OnionMessenger_new(keys_manager == null ? 0 : keys_manager.ptr, logger == null ? 0 : logger.ptr);
80                 Reference.reachabilityFence(keys_manager);
81                 Reference.reachabilityFence(logger);
82                 if (ret >= 0 && ret <= 4096) { return null; }
83                 org.ldk.structs.OnionMessenger ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OnionMessenger(null, ret); }
84                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
85                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(keys_manager); };
86                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
87                 return ret_hu_conv;
88         }
89
90         /**
91          * Send an empty onion message to `destination`, routing it through `intermediate_nodes`.
92          * See [`OnionMessenger`] for example usage.
93          * 
94          * Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None
95          */
96         public Result_NoneSendErrorZ send_onion_message(byte[][] intermediate_nodes, Destination destination, @Nullable BlindedRoute reply_path) {
97                 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, reply_path == null ? 0 : reply_path.ptr);
98                 Reference.reachabilityFence(this);
99                 Reference.reachabilityFence(intermediate_nodes);
100                 Reference.reachabilityFence(destination);
101                 Reference.reachabilityFence(reply_path);
102                 if (ret >= 0 && ret <= 4096) { return null; }
103                 Result_NoneSendErrorZ ret_hu_conv = Result_NoneSendErrorZ.constr_from_ptr(ret);
104                 if (this != null) { this.ptrs_to.add(reply_path); };
105                 return ret_hu_conv;
106         }
107
108         /**
109          * Constructs a new OnionMessageHandler which calls the relevant methods on this_arg.
110          * This copies the `inner` pointer in this_arg and thus the returned OnionMessageHandler must be freed before this_arg is
111          */
112         public OnionMessageHandler as_OnionMessageHandler() {
113                 long ret = bindings.OnionMessenger_as_OnionMessageHandler(this.ptr);
114                 Reference.reachabilityFence(this);
115                 if (ret >= 0 && ret <= 4096) { return null; }
116                 OnionMessageHandler ret_hu_conv = new OnionMessageHandler(null, ret);
117                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
118                 return ret_hu_conv;
119         }
120
121         /**
122          * Constructs a new OnionMessageProvider which calls the relevant methods on this_arg.
123          * This copies the `inner` pointer in this_arg and thus the returned OnionMessageProvider must be freed before this_arg is
124          */
125         public OnionMessageProvider as_OnionMessageProvider() {
126                 long ret = bindings.OnionMessenger_as_OnionMessageProvider(this.ptr);
127                 Reference.reachabilityFence(this);
128                 if (ret >= 0 && ret <= 4096) { return null; }
129                 OnionMessageProvider ret_hu_conv = new OnionMessageProvider(null, ret);
130                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
131                 return ret_hu_conv;
132         }
133
134 }