8bf9adb738ee8608c6215031650bdeb2e0cf30b0
[ldk-java] / c_sharp / src / org / ldk / structs / OnionMessenger.cs
1 using org.ldk.impl;
2 using org.ldk.enums;
3 using org.ldk.util;
4 using System;
5
6 namespace org { namespace ldk { namespace structs {
7
8
9 /**
10  * A sender, receiver and forwarder of onion messages. In upcoming releases, this object will be
11  * used to retrieve invoices and fulfill invoice requests from [offers]. Currently, only sending
12  * and receiving custom onion messages is supported.
13  * 
14  * # Example
15  * 
16  * ```
17  * # extern crate bitcoin;
18  * # use bitcoin::hashes::_export::_core::time::Duration;
19  * # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
20  * # use lightning::chain::keysinterface::{InMemorySigner, KeysManager, KeysInterface};
21  * # use lightning::ln::msgs::DecodeError;
22  * # use lightning::ln::peer_handler::IgnoringMessageHandler;
23  * # use lightning::onion_message::blinded_path::BlindedPath;
24  * # use lightning::onion_message::messenger::{CustomOnionMessageContents, Destination, OnionMessageContents, OnionMessenger};
25  * # use lightning::util::logger::{Logger, Record};
26  * # use lightning::util::ser::{Writeable, Writer};
27  * # use lightning::io;
28  * # use std::sync::Arc;
29  * # struct FakeLogger {};
30  * # impl Logger for FakeLogger {
31  * #     fn log(&self, record: &Record) { unimplemented!() }
32  * # }
33  * # let seed = [42u8; 32];
34  * # let time = Duration::from_secs(123456);
35  * # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
36  * # let logger = Arc::new(FakeLogger {});
37  * # let node_secret = SecretKey::from_slice(&hex::decode(\"0101010101010101010101010101010101010101010101010101010101010101\").unwrap()[..]).unwrap();
38  * # let secp_ctx = Secp256k1::new();
39  * # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
40  * # let (hop_node_id2, hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1, hop_node_id1);
41  * # let destination_node_id = hop_node_id1;
42  * # let your_custom_message_handler = IgnoringMessageHandler {};
43  * Create the onion messenger. This must use the same `keys_manager` as is passed to your
44  * ChannelManager.
45  * let onion_messenger = OnionMessenger::new(&keys_manager, logger, your_custom_message_handler);
46  * 
47  * # #[derive(Clone)]
48  * # struct YourCustomMessage {}
49  * impl Writeable for YourCustomMessage {
50  * \tfn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
51  * \t\t# Ok(())
52  * \t\t// Write your custom onion message to `w`
53  * \t}
54  * }
55  * impl CustomOnionMessageContents for YourCustomMessage {
56  * \tfn tlv_type(&self) -> u64 {
57  * \t\t# let your_custom_message_type = 42;
58  * \t\tyour_custom_message_type
59  * \t}
60  * }
61  * Send a custom onion message to a node id.
62  * let intermediate_hops = [hop_node_id1, hop_node_id2];
63  * let reply_path = None;
64  * # let your_custom_message = YourCustomMessage {};
65  * let message = OnionMessageContents::Custom(your_custom_message);
66  * onion_messenger.send_onion_message(&intermediate_hops, Destination::Node(destination_node_id), message, reply_path);
67  * 
68  * Create a blinded path to yourself, for someone to send an onion message to.
69  * # let your_node_id = hop_node_id1;
70  * let hops = [hop_node_id3, hop_node_id4, your_node_id];
71  * let blinded_path = BlindedPath::new(&hops, &keys_manager, &secp_ctx).unwrap();
72  * 
73  * Send a custom onion message to a blinded path.
74  * # let intermediate_hops = [hop_node_id1, hop_node_id2];
75  * let reply_path = None;
76  * # let your_custom_message = YourCustomMessage {};
77  * let message = OnionMessageContents::Custom(your_custom_message);
78  * onion_messenger.send_onion_message(&intermediate_hops, Destination::BlindedPath(blinded_path), message, reply_path);
79  * ```
80  * 
81  * [offers]: <https://github.com/lightning/bolts/pull/798>
82  * [`OnionMessenger`]: crate::onion_message::OnionMessenger
83  */
84 public class OnionMessenger : CommonBase {
85         internal OnionMessenger(object _dummy, long ptr) : base(ptr) { }
86         ~OnionMessenger() {
87                 if (ptr != 0) { bindings.OnionMessenger_free(ptr); }
88         }
89
90         /**
91          * Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
92          * their respective handlers.
93          */
94         public static OnionMessenger of(org.ldk.structs.KeysInterface keys_manager, org.ldk.structs.Logger logger, org.ldk.structs.CustomOnionMessageHandler custom_handler) {
95                 long ret = bindings.OnionMessenger_new(keys_manager == null ? 0 : keys_manager.ptr, logger == null ? 0 : logger.ptr, custom_handler == null ? 0 : custom_handler.ptr);
96                 GC.KeepAlive(keys_manager);
97                 GC.KeepAlive(logger);
98                 GC.KeepAlive(custom_handler);
99                 if (ret >= 0 && ret <= 4096) { return null; }
100                 org.ldk.structs.OnionMessenger ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OnionMessenger(null, ret); }
101                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
102                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(keys_manager); };
103                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(logger); };
104                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(custom_handler); };
105                 return ret_hu_conv;
106         }
107
108         /**
109          * Send an onion message with contents `message` to `destination`, routing it through `intermediate_nodes`.
110          * See [`OnionMessenger`] for example usage.
111          * 
112          * Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None
113          */
114         public Result_NoneSendErrorZ send_onion_message(byte[][] intermediate_nodes, org.ldk.structs.Destination destination, org.ldk.structs.OnionMessageContents message, org.ldk.structs.BlindedPath reply_path) {
115                 long ret = bindings.OnionMessenger_send_onion_message(this.ptr, intermediate_nodes != null ? InternalUtils.mapArray(intermediate_nodes, intermediate_nodes_conv_8 => InternalUtils.check_arr_len(intermediate_nodes_conv_8, 33)) : null, destination.ptr, message.ptr, reply_path == null ? 0 : reply_path.ptr);
116                 GC.KeepAlive(this);
117                 GC.KeepAlive(intermediate_nodes);
118                 GC.KeepAlive(destination);
119                 GC.KeepAlive(message);
120                 GC.KeepAlive(reply_path);
121                 if (ret >= 0 && ret <= 4096) { return null; }
122                 Result_NoneSendErrorZ ret_hu_conv = Result_NoneSendErrorZ.constr_from_ptr(ret);
123                 if (this != null) { this.ptrs_to.AddLast(reply_path); };
124                 return ret_hu_conv;
125         }
126
127         /**
128          * Constructs a new OnionMessageHandler which calls the relevant methods on this_arg.
129          * This copies the `inner` pointer in this_arg and thus the returned OnionMessageHandler must be freed before this_arg is
130          */
131         public OnionMessageHandler as_OnionMessageHandler() {
132                 long ret = bindings.OnionMessenger_as_OnionMessageHandler(this.ptr);
133                 GC.KeepAlive(this);
134                 if (ret >= 0 && ret <= 4096) { return null; }
135                 OnionMessageHandler ret_hu_conv = new OnionMessageHandler(null, ret);
136                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
137                 return ret_hu_conv;
138         }
139
140         /**
141          * Constructs a new OnionMessageProvider which calls the relevant methods on this_arg.
142          * This copies the `inner` pointer in this_arg and thus the returned OnionMessageProvider must be freed before this_arg is
143          */
144         public OnionMessageProvider as_OnionMessageProvider() {
145                 long ret = bindings.OnionMessenger_as_OnionMessageProvider(this.ptr);
146                 GC.KeepAlive(this);
147                 if (ret >= 0 && ret <= 4096) { return null; }
148                 OnionMessageProvider ret_hu_conv = new OnionMessageProvider(null, ret);
149                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
150                 return ret_hu_conv;
151         }
152
153 }
154 } } }