[C#] Update auto-generated C# bindings
[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 [`OnionMessage`]s.
11  * 
12  * # Handling Messages
13  * 
14  * `OnionMessenger` implements [`OnionMessageHandler`], making it responsible for either forwarding
15  * messages to peers or delegating to the appropriate handler for the message type. Currently, the
16  * available handlers are:
17  * [`OffersMessageHandler`], for responding to [`InvoiceRequest`]s and paying [`Bolt12Invoice`]s
18  * [`CustomOnionMessageHandler`], for handling user-defined message types
19  * 
20  * # Sending Messages
21  * 
22  * [`OnionMessage`]s are sent initially using [`OnionMessenger::send_onion_message`]. When handling
23  * a message, the matched handler may return a response message which `OnionMessenger` will send
24  * on its behalf.
25  * 
26  * # Example
27  * 
28  * ```
29  * # extern crate bitcoin;
30  * # use bitcoin::hashes::_export::_core::time::Duration;
31  * # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
32  * # use lightning::blinded_path::BlindedPath;
33  * # use lightning::sign::KeysManager;
34  * # use lightning::ln::peer_handler::IgnoringMessageHandler;
35  * # use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessenger, OnionMessagePath};
36  * # use lightning::onion_message::packet::OnionMessageContents;
37  * # use lightning::util::logger::{Logger, Record};
38  * # use lightning::util::ser::{Writeable, Writer};
39  * # use lightning::io;
40  * # use std::sync::Arc;
41  * # struct FakeLogger;
42  * # impl Logger for FakeLogger {
43  * #     fn log(&self, record: &Record) { unimplemented!() }
44  * # }
45  * # struct FakeMessageRouter {}
46  * # impl MessageRouter for FakeMessageRouter {
47  * #     fn find_path(&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination) -> Result<OnionMessagePath, ()> {
48  * #         unimplemented!()
49  * #     }
50  * # }
51  * # let seed = [42u8; 32];
52  * # let time = Duration::from_secs(123456);
53  * # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
54  * # let logger = Arc::new(FakeLogger {});
55  * # let node_secret = SecretKey::from_slice(&hex::decode(\"0101010101010101010101010101010101010101010101010101010101010101\").unwrap()[..]).unwrap();
56  * # let secp_ctx = Secp256k1::new();
57  * # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
58  * # let (hop_node_id2, hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1, hop_node_id1);
59  * # let destination_node_id = hop_node_id1;
60  * # let message_router = Arc::new(FakeMessageRouter {});
61  * # let custom_message_handler = IgnoringMessageHandler {};
62  * # let offers_message_handler = IgnoringMessageHandler {};
63  * Create the onion messenger. This must use the same `keys_manager` as is passed to your
64  * ChannelManager.
65  * let onion_messenger = OnionMessenger::new(
66  * &keys_manager, &keys_manager, logger, message_router, &offers_message_handler,
67  * &custom_message_handler
68  * );
69  * 
70  * # #[derive(Clone)]
71  * # struct YourCustomMessage {}
72  * impl Writeable for YourCustomMessage {
73  * \tfn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
74  * \t\t# Ok(())
75  * \t\t// Write your custom onion message to `w`
76  * \t}
77  * }
78  * impl OnionMessageContents for YourCustomMessage {
79  * \tfn tlv_type(&self) -> u64 {
80  * \t\t# let your_custom_message_type = 42;
81  * \t\tyour_custom_message_type
82  * \t}
83  * }
84  * Send a custom onion message to a node id.
85  * let path = OnionMessagePath {
86  * \tintermediate_nodes: vec![hop_node_id1, hop_node_id2],
87  * \tdestination: Destination::Node(destination_node_id),
88  * };
89  * let reply_path = None;
90  * # let message = YourCustomMessage {};
91  * onion_messenger.send_onion_message(path, message, reply_path);
92  * 
93  * Create a blinded path to yourself, for someone to send an onion message to.
94  * # let your_node_id = hop_node_id1;
95  * let hops = [hop_node_id3, hop_node_id4, your_node_id];
96  * let blinded_path = BlindedPath::new_for_message(&hops, &keys_manager, &secp_ctx).unwrap();
97  * 
98  * Send a custom onion message to a blinded path.
99  * let path = OnionMessagePath {
100  * \tintermediate_nodes: vec![hop_node_id1, hop_node_id2],
101  * \tdestination: Destination::BlindedPath(blinded_path),
102  * };
103  * let reply_path = None;
104  * # let message = YourCustomMessage {};
105  * onion_messenger.send_onion_message(path, message, reply_path);
106  * ```
107  * 
108  * [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
109  * [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
110  */
111 public class OnionMessenger : CommonBase {
112         internal OnionMessenger(object _dummy, long ptr) : base(ptr) { }
113         ~OnionMessenger() {
114                 if (ptr != 0) { bindings.OnionMessenger_free(ptr); }
115         }
116
117         /**
118          * Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
119          * their respective handlers.
120          */
121         public static OnionMessenger of(org.ldk.structs.EntropySource entropy_source, org.ldk.structs.NodeSigner node_signer, org.ldk.structs.Logger logger, org.ldk.structs.MessageRouter message_router, org.ldk.structs.OffersMessageHandler offers_handler, org.ldk.structs.CustomOnionMessageHandler custom_handler) {
122                 long ret = bindings.OnionMessenger_new(entropy_source.ptr, node_signer.ptr, logger.ptr, message_router.ptr, offers_handler.ptr, custom_handler.ptr);
123                 GC.KeepAlive(entropy_source);
124                 GC.KeepAlive(node_signer);
125                 GC.KeepAlive(logger);
126                 GC.KeepAlive(message_router);
127                 GC.KeepAlive(offers_handler);
128                 GC.KeepAlive(custom_handler);
129                 if (ret >= 0 && ret <= 4096) { return null; }
130                 org.ldk.structs.OnionMessenger ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OnionMessenger(null, ret); }
131                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
132                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(entropy_source); };
133                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(node_signer); };
134                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(logger); };
135                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(message_router); };
136                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(offers_handler); };
137                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(custom_handler); };
138                 return ret_hu_conv;
139         }
140
141         /**
142          * Sends an [`OnionMessage`] with the given `contents` for sending to the destination of
143          * `path`.
144          * 
145          * See [`OnionMessenger`] for example usage.
146          * 
147          * Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None
148          */
149         public Result_NoneSendErrorZ send_onion_message(org.ldk.structs.OnionMessagePath path, org.ldk.structs.OnionMessageContents contents, org.ldk.structs.BlindedPath reply_path) {
150                 long ret = bindings.OnionMessenger_send_onion_message(this.ptr, path == null ? 0 : path.ptr, contents.ptr, reply_path == null ? 0 : reply_path.ptr);
151                 GC.KeepAlive(this);
152                 GC.KeepAlive(path);
153                 GC.KeepAlive(contents);
154                 GC.KeepAlive(reply_path);
155                 if (ret >= 0 && ret <= 4096) { return null; }
156                 Result_NoneSendErrorZ ret_hu_conv = Result_NoneSendErrorZ.constr_from_ptr(ret);
157                 if (this != null) { this.ptrs_to.AddLast(path); };
158                 if (this != null) { this.ptrs_to.AddLast(contents); };
159                 if (this != null) { this.ptrs_to.AddLast(reply_path); };
160                 return ret_hu_conv;
161         }
162
163         /**
164          * Constructs a new OnionMessageHandler which calls the relevant methods on this_arg.
165          * This copies the `inner` pointer in this_arg and thus the returned OnionMessageHandler must be freed before this_arg is
166          */
167         public OnionMessageHandler as_OnionMessageHandler() {
168                 long ret = bindings.OnionMessenger_as_OnionMessageHandler(this.ptr);
169                 GC.KeepAlive(this);
170                 if (ret >= 0 && ret <= 4096) { return null; }
171                 OnionMessageHandler ret_hu_conv = new OnionMessageHandler(null, ret);
172                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
173                 return ret_hu_conv;
174         }
175
176 }
177 } } }