5ef9665afb5f275b3b1a39d7a4616c6d34d89d42
[ldk-c-bindings] / lightning-c-bindings / src / lightning / onion_message / messenger.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
8
9 //! LDK sends, receives, and forwards onion messages via the [`OnionMessenger`]. See its docs for
10 //! more information.
11
12 use alloc::str::FromStr;
13 use alloc::string::String;
14 use core::ffi::c_void;
15 use core::convert::Infallible;
16 use bitcoin::hashes::Hash;
17 use crate::c_types::*;
18 #[cfg(feature="no-std")]
19 use alloc::{vec::Vec, boxed::Box};
20
21
22 use lightning::onion_message::messenger::OnionMessenger as nativeOnionMessengerImport;
23 pub(crate) type nativeOnionMessenger = nativeOnionMessengerImport<crate::lightning::sign::EntropySource, crate::lightning::sign::NodeSigner, crate::lightning::util::logger::Logger, crate::lightning::onion_message::messenger::MessageRouter, crate::lightning::onion_message::offers::OffersMessageHandler, crate::lightning::onion_message::messenger::CustomOnionMessageHandler>;
24
25 /// A sender, receiver and forwarder of [`OnionMessage`]s.
26 ///
27 /// # Handling Messages
28 ///
29 /// `OnionMessenger` implements [`OnionMessageHandler`], making it responsible for either forwarding
30 /// messages to peers or delegating to the appropriate handler for the message type. Currently, the
31 /// available handlers are:
32 /// * [`OffersMessageHandler`], for responding to [`InvoiceRequest`]s and paying [`Bolt12Invoice`]s
33 /// * [`CustomOnionMessageHandler`], for handling user-defined message types
34 ///
35 /// # Sending Messages
36 ///
37 /// [`OnionMessage`]s are sent initially using [`OnionMessenger::send_onion_message`]. When handling
38 /// a message, the matched handler may return a response message which `OnionMessenger` will send
39 /// on its behalf.
40 ///
41 /// # Example
42 ///
43 /// ```
44 /// # extern crate bitcoin;
45 /// # use bitcoin::hashes::_export::_core::time::Duration;
46 /// # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
47 /// # use lightning::blinded_path::BlindedPath;
48 /// # use lightning::sign::KeysManager;
49 /// # use lightning::ln::peer_handler::IgnoringMessageHandler;
50 /// # use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessenger, OnionMessagePath};
51 /// # use lightning::onion_message::packet::OnionMessageContents;
52 /// # use lightning::util::logger::{Logger, Record};
53 /// # use lightning::util::ser::{Writeable, Writer};
54 /// # use lightning::io;
55 /// # use std::sync::Arc;
56 /// # struct FakeLogger;
57 /// # impl Logger for FakeLogger {
58 /// #     fn log(&self, record: &Record) { unimplemented!() }
59 /// # }
60 /// # struct FakeMessageRouter {}
61 /// # impl MessageRouter for FakeMessageRouter {
62 /// #     fn find_path(&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination) -> Result<OnionMessagePath, ()> {
63 /// #         unimplemented!()
64 /// #     }
65 /// # }
66 /// # let seed = [42u8; 32];
67 /// # let time = Duration::from_secs(123456);
68 /// # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
69 /// # let logger = Arc::new(FakeLogger {});
70 /// # let node_secret = SecretKey::from_slice(&hex::decode(\"0101010101010101010101010101010101010101010101010101010101010101\").unwrap()[..]).unwrap();
71 /// # let secp_ctx = Secp256k1::new();
72 /// # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
73 /// # let (hop_node_id2, hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1, hop_node_id1);
74 /// # let destination_node_id = hop_node_id1;
75 /// # let message_router = Arc::new(FakeMessageRouter {});
76 /// # let custom_message_handler = IgnoringMessageHandler {};
77 /// # let offers_message_handler = IgnoringMessageHandler {};
78 /// // Create the onion messenger. This must use the same `keys_manager` as is passed to your
79 /// // ChannelManager.
80 /// let onion_messenger = OnionMessenger::new(
81 ///     &keys_manager, &keys_manager, logger, message_router, &offers_message_handler,
82 ///     &custom_message_handler
83 /// );
84 ///
85 /// # #[derive(Clone)]
86 /// # struct YourCustomMessage {}
87 /// impl Writeable for YourCustomMessage {
88 /// \tfn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
89 /// \t\t# Ok(())
90 /// \t\t// Write your custom onion message to `w`
91 /// \t}
92 /// }
93 /// impl OnionMessageContents for YourCustomMessage {
94 /// \tfn tlv_type(&self) -> u64 {
95 /// \t\t# let your_custom_message_type = 42;
96 /// \t\tyour_custom_message_type
97 /// \t}
98 /// }
99 /// // Send a custom onion message to a node id.
100 /// let path = OnionMessagePath {
101 /// \tintermediate_nodes: vec![hop_node_id1, hop_node_id2],
102 /// \tdestination: Destination::Node(destination_node_id),
103 /// };
104 /// let reply_path = None;
105 /// # let message = YourCustomMessage {};
106 /// onion_messenger.send_onion_message(path, message, reply_path);
107 ///
108 /// // Create a blinded path to yourself, for someone to send an onion message to.
109 /// # let your_node_id = hop_node_id1;
110 /// let hops = [hop_node_id3, hop_node_id4, your_node_id];
111 /// let blinded_path = BlindedPath::new_for_message(&hops, &keys_manager, &secp_ctx).unwrap();
112 ///
113 /// // Send a custom onion message to a blinded path.
114 /// let path = OnionMessagePath {
115 /// \tintermediate_nodes: vec![hop_node_id1, hop_node_id2],
116 /// \tdestination: Destination::BlindedPath(blinded_path),
117 /// };
118 /// let reply_path = None;
119 /// # let message = YourCustomMessage {};
120 /// onion_messenger.send_onion_message(path, message, reply_path);
121 /// ```
122 ///
123 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
124 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
125 #[must_use]
126 #[repr(C)]
127 pub struct OnionMessenger {
128         /// A pointer to the opaque Rust object.
129
130         /// Nearly everywhere, inner must be non-null, however in places where
131         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
132         pub inner: *mut nativeOnionMessenger,
133         /// Indicates that this is the only struct which contains the same pointer.
134
135         /// Rust functions which take ownership of an object provided via an argument require
136         /// this to be true and invalidate the object pointed to by inner.
137         pub is_owned: bool,
138 }
139
140 impl Drop for OnionMessenger {
141         fn drop(&mut self) {
142                 if self.is_owned && !<*mut nativeOnionMessenger>::is_null(self.inner) {
143                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
144                 }
145         }
146 }
147 /// Frees any resources used by the OnionMessenger, if is_owned is set and inner is non-NULL.
148 #[no_mangle]
149 pub extern "C" fn OnionMessenger_free(this_obj: OnionMessenger) { }
150 #[allow(unused)]
151 /// Used only if an object of this type is returned as a trait impl by a method
152 pub(crate) extern "C" fn OnionMessenger_free_void(this_ptr: *mut c_void) {
153         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeOnionMessenger) };
154 }
155 #[allow(unused)]
156 impl OnionMessenger {
157         pub(crate) fn get_native_ref(&self) -> &'static nativeOnionMessenger {
158                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
159         }
160         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeOnionMessenger {
161                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
162         }
163         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
164         pub(crate) fn take_inner(mut self) -> *mut nativeOnionMessenger {
165                 assert!(self.is_owned);
166                 let ret = ObjOps::untweak_ptr(self.inner);
167                 self.inner = core::ptr::null_mut();
168                 ret
169         }
170 }
171 /// A trait defining behavior for routing an [`OnionMessage`].
172 #[repr(C)]
173 pub struct MessageRouter {
174         /// An opaque pointer which is passed to your function implementations as an argument.
175         /// This has no meaning in the LDK, and can be NULL or any other value.
176         pub this_arg: *mut c_void,
177         /// Returns a route for sending an [`OnionMessage`] to the given [`Destination`].
178         pub find_path: extern "C" fn (this_arg: *const c_void, sender: crate::c_types::PublicKey, peers: crate::c_types::derived::CVec_PublicKeyZ, destination: crate::lightning::onion_message::messenger::Destination) -> crate::c_types::derived::CResult_OnionMessagePathNoneZ,
179         /// Frees any resources associated with this object given its this_arg pointer.
180         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
181         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
182 }
183 unsafe impl Send for MessageRouter {}
184 unsafe impl Sync for MessageRouter {}
185 #[allow(unused)]
186 pub(crate) fn MessageRouter_clone_fields(orig: &MessageRouter) -> MessageRouter {
187         MessageRouter {
188                 this_arg: orig.this_arg,
189                 find_path: Clone::clone(&orig.find_path),
190                 free: Clone::clone(&orig.free),
191         }
192 }
193
194 use lightning::onion_message::messenger::MessageRouter as rustMessageRouter;
195 impl rustMessageRouter for MessageRouter {
196         fn find_path(&self, mut sender: bitcoin::secp256k1::PublicKey, mut peers: Vec<bitcoin::secp256k1::PublicKey>, mut destination: lightning::onion_message::messenger::Destination) -> Result<lightning::onion_message::messenger::OnionMessagePath, ()> {
197                 let mut local_peers = Vec::new(); for mut item in peers.drain(..) { local_peers.push( { crate::c_types::PublicKey::from_rust(&item) }); };
198                 let mut ret = (self.find_path)(self.this_arg, crate::c_types::PublicKey::from_rust(&sender), local_peers.into(), crate::lightning::onion_message::messenger::Destination::native_into(destination));
199                 let mut local_ret = match ret.result_ok { true => Ok( { *unsafe { Box::from_raw((*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).take_inner()) } }), false => Err( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) })*/ })};
200                 local_ret
201         }
202 }
203
204 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
205 // directly as a Deref trait in higher-level structs:
206 impl core::ops::Deref for MessageRouter {
207         type Target = Self;
208         fn deref(&self) -> &Self {
209                 self
210         }
211 }
212 impl core::ops::DerefMut for MessageRouter {
213         fn deref_mut(&mut self) -> &mut Self {
214                 self
215         }
216 }
217 /// Calls the free function if one is set
218 #[no_mangle]
219 pub extern "C" fn MessageRouter_free(this_ptr: MessageRouter) { }
220 impl Drop for MessageRouter {
221         fn drop(&mut self) {
222                 if let Some(f) = self.free {
223                         f(self.this_arg);
224                 }
225         }
226 }
227
228 use lightning::onion_message::messenger::DefaultMessageRouter as nativeDefaultMessageRouterImport;
229 pub(crate) type nativeDefaultMessageRouter = nativeDefaultMessageRouterImport;
230
231 /// A [`MessageRouter`] that can only route to a directly connected [`Destination`].
232 #[must_use]
233 #[repr(C)]
234 pub struct DefaultMessageRouter {
235         /// A pointer to the opaque Rust object.
236
237         /// Nearly everywhere, inner must be non-null, however in places where
238         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
239         pub inner: *mut nativeDefaultMessageRouter,
240         /// Indicates that this is the only struct which contains the same pointer.
241
242         /// Rust functions which take ownership of an object provided via an argument require
243         /// this to be true and invalidate the object pointed to by inner.
244         pub is_owned: bool,
245 }
246
247 impl Drop for DefaultMessageRouter {
248         fn drop(&mut self) {
249                 if self.is_owned && !<*mut nativeDefaultMessageRouter>::is_null(self.inner) {
250                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
251                 }
252         }
253 }
254 /// Frees any resources used by the DefaultMessageRouter, if is_owned is set and inner is non-NULL.
255 #[no_mangle]
256 pub extern "C" fn DefaultMessageRouter_free(this_obj: DefaultMessageRouter) { }
257 #[allow(unused)]
258 /// Used only if an object of this type is returned as a trait impl by a method
259 pub(crate) extern "C" fn DefaultMessageRouter_free_void(this_ptr: *mut c_void) {
260         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeDefaultMessageRouter) };
261 }
262 #[allow(unused)]
263 impl DefaultMessageRouter {
264         pub(crate) fn get_native_ref(&self) -> &'static nativeDefaultMessageRouter {
265                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
266         }
267         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeDefaultMessageRouter {
268                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
269         }
270         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
271         pub(crate) fn take_inner(mut self) -> *mut nativeDefaultMessageRouter {
272                 assert!(self.is_owned);
273                 let ret = ObjOps::untweak_ptr(self.inner);
274                 self.inner = core::ptr::null_mut();
275                 ret
276         }
277 }
278 /// Constructs a new DefaultMessageRouter given each field
279 #[must_use]
280 #[no_mangle]
281 pub extern "C" fn DefaultMessageRouter_new() -> DefaultMessageRouter {
282         DefaultMessageRouter { inner: ObjOps::heap_alloc(lightning::onion_message::messenger::DefaultMessageRouter {}), is_owned: true }
283 }
284 impl From<nativeDefaultMessageRouter> for crate::lightning::onion_message::messenger::MessageRouter {
285         fn from(obj: nativeDefaultMessageRouter) -> Self {
286                 let rust_obj = crate::lightning::onion_message::messenger::DefaultMessageRouter { inner: ObjOps::heap_alloc(obj), is_owned: true };
287                 let mut ret = DefaultMessageRouter_as_MessageRouter(&rust_obj);
288                 // We want to free rust_obj when ret gets drop()'d, not rust_obj, so forget it and set ret's free() fn
289                 core::mem::forget(rust_obj);
290                 ret.free = Some(DefaultMessageRouter_free_void);
291                 ret
292         }
293 }
294 /// Constructs a new MessageRouter which calls the relevant methods on this_arg.
295 /// This copies the `inner` pointer in this_arg and thus the returned MessageRouter must be freed before this_arg is
296 #[no_mangle]
297 pub extern "C" fn DefaultMessageRouter_as_MessageRouter(this_arg: &DefaultMessageRouter) -> crate::lightning::onion_message::messenger::MessageRouter {
298         crate::lightning::onion_message::messenger::MessageRouter {
299                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
300                 free: None,
301                 find_path: DefaultMessageRouter_MessageRouter_find_path,
302         }
303 }
304
305 #[must_use]
306 extern "C" fn DefaultMessageRouter_MessageRouter_find_path(this_arg: *const c_void, mut sender: crate::c_types::PublicKey, mut peers: crate::c_types::derived::CVec_PublicKeyZ, mut destination: crate::lightning::onion_message::messenger::Destination) -> crate::c_types::derived::CResult_OnionMessagePathNoneZ {
307         let mut local_peers = Vec::new(); for mut item in peers.into_rust().drain(..) { local_peers.push( { item.into_rust() }); };
308         let mut ret = <nativeDefaultMessageRouter as lightning::onion_message::messenger::MessageRouter<>>::find_path(unsafe { &mut *(this_arg as *mut nativeDefaultMessageRouter) }, sender.into_rust(), local_peers, destination.into_native());
309         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::onion_message::messenger::OnionMessagePath { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
310         local_ret
311 }
312
313
314 use lightning::onion_message::messenger::OnionMessagePath as nativeOnionMessagePathImport;
315 pub(crate) type nativeOnionMessagePath = nativeOnionMessagePathImport;
316
317 /// A path for sending an [`OnionMessage`].
318 #[must_use]
319 #[repr(C)]
320 pub struct OnionMessagePath {
321         /// A pointer to the opaque Rust object.
322
323         /// Nearly everywhere, inner must be non-null, however in places where
324         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
325         pub inner: *mut nativeOnionMessagePath,
326         /// Indicates that this is the only struct which contains the same pointer.
327
328         /// Rust functions which take ownership of an object provided via an argument require
329         /// this to be true and invalidate the object pointed to by inner.
330         pub is_owned: bool,
331 }
332
333 impl Drop for OnionMessagePath {
334         fn drop(&mut self) {
335                 if self.is_owned && !<*mut nativeOnionMessagePath>::is_null(self.inner) {
336                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
337                 }
338         }
339 }
340 /// Frees any resources used by the OnionMessagePath, if is_owned is set and inner is non-NULL.
341 #[no_mangle]
342 pub extern "C" fn OnionMessagePath_free(this_obj: OnionMessagePath) { }
343 #[allow(unused)]
344 /// Used only if an object of this type is returned as a trait impl by a method
345 pub(crate) extern "C" fn OnionMessagePath_free_void(this_ptr: *mut c_void) {
346         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeOnionMessagePath) };
347 }
348 #[allow(unused)]
349 impl OnionMessagePath {
350         pub(crate) fn get_native_ref(&self) -> &'static nativeOnionMessagePath {
351                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
352         }
353         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeOnionMessagePath {
354                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
355         }
356         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
357         pub(crate) fn take_inner(mut self) -> *mut nativeOnionMessagePath {
358                 assert!(self.is_owned);
359                 let ret = ObjOps::untweak_ptr(self.inner);
360                 self.inner = core::ptr::null_mut();
361                 ret
362         }
363 }
364 /// Nodes on the path between the sender and the destination.
365 ///
366 /// Returns a copy of the field.
367 #[no_mangle]
368 pub extern "C" fn OnionMessagePath_get_intermediate_nodes(this_ptr: &OnionMessagePath) -> crate::c_types::derived::CVec_PublicKeyZ {
369         let mut inner_val = this_ptr.get_native_mut_ref().intermediate_nodes.clone();
370         let mut local_inner_val = Vec::new(); for mut item in inner_val.drain(..) { local_inner_val.push( { crate::c_types::PublicKey::from_rust(&item) }); };
371         local_inner_val.into()
372 }
373 /// Nodes on the path between the sender and the destination.
374 #[no_mangle]
375 pub extern "C" fn OnionMessagePath_set_intermediate_nodes(this_ptr: &mut OnionMessagePath, mut val: crate::c_types::derived::CVec_PublicKeyZ) {
376         let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item.into_rust() }); };
377         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.intermediate_nodes = local_val;
378 }
379 /// The recipient of the message.
380 #[no_mangle]
381 pub extern "C" fn OnionMessagePath_get_destination(this_ptr: &OnionMessagePath) -> crate::lightning::onion_message::messenger::Destination {
382         let mut inner_val = &mut this_ptr.get_native_mut_ref().destination;
383         crate::lightning::onion_message::messenger::Destination::from_native(inner_val)
384 }
385 /// The recipient of the message.
386 #[no_mangle]
387 pub extern "C" fn OnionMessagePath_set_destination(this_ptr: &mut OnionMessagePath, mut val: crate::lightning::onion_message::messenger::Destination) {
388         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.destination = val.into_native();
389 }
390 /// Constructs a new OnionMessagePath given each field
391 #[must_use]
392 #[no_mangle]
393 pub extern "C" fn OnionMessagePath_new(mut intermediate_nodes_arg: crate::c_types::derived::CVec_PublicKeyZ, mut destination_arg: crate::lightning::onion_message::messenger::Destination) -> OnionMessagePath {
394         let mut local_intermediate_nodes_arg = Vec::new(); for mut item in intermediate_nodes_arg.into_rust().drain(..) { local_intermediate_nodes_arg.push( { item.into_rust() }); };
395         OnionMessagePath { inner: ObjOps::heap_alloc(nativeOnionMessagePath {
396                 intermediate_nodes: local_intermediate_nodes_arg,
397                 destination: destination_arg.into_native(),
398         }), is_owned: true }
399 }
400 impl Clone for OnionMessagePath {
401         fn clone(&self) -> Self {
402                 Self {
403                         inner: if <*mut nativeOnionMessagePath>::is_null(self.inner) { core::ptr::null_mut() } else {
404                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
405                         is_owned: true,
406                 }
407         }
408 }
409 #[allow(unused)]
410 /// Used only if an object of this type is returned as a trait impl by a method
411 pub(crate) extern "C" fn OnionMessagePath_clone_void(this_ptr: *const c_void) -> *mut c_void {
412         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeOnionMessagePath)).clone() })) as *mut c_void
413 }
414 #[no_mangle]
415 /// Creates a copy of the OnionMessagePath
416 pub extern "C" fn OnionMessagePath_clone(orig: &OnionMessagePath) -> OnionMessagePath {
417         orig.clone()
418 }
419 /// The destination of an onion message.
420 #[derive(Clone)]
421 #[must_use]
422 #[repr(C)]
423 pub enum Destination {
424         /// We're sending this onion message to a node.
425         Node(
426                 crate::c_types::PublicKey),
427         /// We're sending this onion message to a blinded path.
428         BlindedPath(
429                 crate::lightning::blinded_path::BlindedPath),
430 }
431 use lightning::onion_message::messenger::Destination as DestinationImport;
432 pub(crate) type nativeDestination = DestinationImport;
433
434 impl Destination {
435         #[allow(unused)]
436         pub(crate) fn to_native(&self) -> nativeDestination {
437                 match self {
438                         Destination::Node (ref a, ) => {
439                                 let mut a_nonref = Clone::clone(a);
440                                 nativeDestination::Node (
441                                         a_nonref.into_rust(),
442                                 )
443                         },
444                         Destination::BlindedPath (ref a, ) => {
445                                 let mut a_nonref = Clone::clone(a);
446                                 nativeDestination::BlindedPath (
447                                         *unsafe { Box::from_raw(a_nonref.take_inner()) },
448                                 )
449                         },
450                 }
451         }
452         #[allow(unused)]
453         pub(crate) fn into_native(self) -> nativeDestination {
454                 match self {
455                         Destination::Node (mut a, ) => {
456                                 nativeDestination::Node (
457                                         a.into_rust(),
458                                 )
459                         },
460                         Destination::BlindedPath (mut a, ) => {
461                                 nativeDestination::BlindedPath (
462                                         *unsafe { Box::from_raw(a.take_inner()) },
463                                 )
464                         },
465                 }
466         }
467         #[allow(unused)]
468         pub(crate) fn from_native(native: &nativeDestination) -> Self {
469                 match native {
470                         nativeDestination::Node (ref a, ) => {
471                                 let mut a_nonref = Clone::clone(a);
472                                 Destination::Node (
473                                         crate::c_types::PublicKey::from_rust(&a_nonref),
474                                 )
475                         },
476                         nativeDestination::BlindedPath (ref a, ) => {
477                                 let mut a_nonref = Clone::clone(a);
478                                 Destination::BlindedPath (
479                                         crate::lightning::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(a_nonref), is_owned: true },
480                                 )
481                         },
482                 }
483         }
484         #[allow(unused)]
485         pub(crate) fn native_into(native: nativeDestination) -> Self {
486                 match native {
487                         nativeDestination::Node (mut a, ) => {
488                                 Destination::Node (
489                                         crate::c_types::PublicKey::from_rust(&a),
490                                 )
491                         },
492                         nativeDestination::BlindedPath (mut a, ) => {
493                                 Destination::BlindedPath (
494                                         crate::lightning::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(a), is_owned: true },
495                                 )
496                         },
497                 }
498         }
499 }
500 /// Frees any resources used by the Destination
501 #[no_mangle]
502 pub extern "C" fn Destination_free(this_ptr: Destination) { }
503 /// Creates a copy of the Destination
504 #[no_mangle]
505 pub extern "C" fn Destination_clone(orig: &Destination) -> Destination {
506         orig.clone()
507 }
508 #[allow(unused)]
509 /// Used only if an object of this type is returned as a trait impl by a method
510 pub(crate) extern "C" fn Destination_clone_void(this_ptr: *const c_void) -> *mut c_void {
511         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const Destination)).clone() })) as *mut c_void
512 }
513 #[allow(unused)]
514 /// Used only if an object of this type is returned as a trait impl by a method
515 pub(crate) extern "C" fn Destination_free_void(this_ptr: *mut c_void) {
516         let _ = unsafe { Box::from_raw(this_ptr as *mut Destination) };
517 }
518 #[no_mangle]
519 /// Utility method to constructs a new Node-variant Destination
520 pub extern "C" fn Destination_node(a: crate::c_types::PublicKey) -> Destination {
521         Destination::Node(a, )
522 }
523 #[no_mangle]
524 /// Utility method to constructs a new BlindedPath-variant Destination
525 pub extern "C" fn Destination_blinded_path(a: crate::lightning::blinded_path::BlindedPath) -> Destination {
526         Destination::BlindedPath(a, )
527 }
528 /// Errors that may occur when [sending an onion message].
529 ///
530 /// [sending an onion message]: OnionMessenger::send_onion_message
531 #[derive(Clone)]
532 #[must_use]
533 #[repr(C)]
534 pub enum SendError {
535         /// Errored computing onion message packet keys.
536         Secp256k1(
537                 crate::c_types::Secp256k1Error),
538         /// Because implementations such as Eclair will drop onion messages where the message packet
539         /// exceeds 32834 bytes, we refuse to send messages where the packet exceeds this size.
540         TooBigPacket,
541         /// The provided [`Destination`] was an invalid [`BlindedPath`] due to not having any blinded
542         /// hops.
543         TooFewBlindedHops,
544         /// Our next-hop peer was offline or does not support onion message forwarding.
545         InvalidFirstHop,
546         /// Onion message contents must have a TLV type >= 64.
547         InvalidMessage,
548         /// Our next-hop peer's buffer was full or our total outbound buffer was full.
549         BufferFull,
550         /// Failed to retrieve our node id from the provided [`NodeSigner`].
551         ///
552         /// [`NodeSigner`]: crate::sign::NodeSigner
553         GetNodeIdFailed,
554         /// We attempted to send to a blinded path where we are the introduction node, and failed to
555         /// advance the blinded path to make the second hop the new introduction node. Either
556         /// [`NodeSigner::ecdh`] failed, we failed to tweak the current blinding point to get the
557         /// new blinding point, or we were attempting to send to ourselves.
558         BlindedPathAdvanceFailed,
559 }
560 use lightning::onion_message::messenger::SendError as SendErrorImport;
561 pub(crate) type nativeSendError = SendErrorImport;
562
563 impl SendError {
564         #[allow(unused)]
565         pub(crate) fn to_native(&self) -> nativeSendError {
566                 match self {
567                         SendError::Secp256k1 (ref a, ) => {
568                                 let mut a_nonref = Clone::clone(a);
569                                 nativeSendError::Secp256k1 (
570                                         a_nonref.into_rust(),
571                                 )
572                         },
573                         SendError::TooBigPacket => nativeSendError::TooBigPacket,
574                         SendError::TooFewBlindedHops => nativeSendError::TooFewBlindedHops,
575                         SendError::InvalidFirstHop => nativeSendError::InvalidFirstHop,
576                         SendError::InvalidMessage => nativeSendError::InvalidMessage,
577                         SendError::BufferFull => nativeSendError::BufferFull,
578                         SendError::GetNodeIdFailed => nativeSendError::GetNodeIdFailed,
579                         SendError::BlindedPathAdvanceFailed => nativeSendError::BlindedPathAdvanceFailed,
580                 }
581         }
582         #[allow(unused)]
583         pub(crate) fn into_native(self) -> nativeSendError {
584                 match self {
585                         SendError::Secp256k1 (mut a, ) => {
586                                 nativeSendError::Secp256k1 (
587                                         a.into_rust(),
588                                 )
589                         },
590                         SendError::TooBigPacket => nativeSendError::TooBigPacket,
591                         SendError::TooFewBlindedHops => nativeSendError::TooFewBlindedHops,
592                         SendError::InvalidFirstHop => nativeSendError::InvalidFirstHop,
593                         SendError::InvalidMessage => nativeSendError::InvalidMessage,
594                         SendError::BufferFull => nativeSendError::BufferFull,
595                         SendError::GetNodeIdFailed => nativeSendError::GetNodeIdFailed,
596                         SendError::BlindedPathAdvanceFailed => nativeSendError::BlindedPathAdvanceFailed,
597                 }
598         }
599         #[allow(unused)]
600         pub(crate) fn from_native(native: &nativeSendError) -> Self {
601                 match native {
602                         nativeSendError::Secp256k1 (ref a, ) => {
603                                 let mut a_nonref = Clone::clone(a);
604                                 SendError::Secp256k1 (
605                                         crate::c_types::Secp256k1Error::from_rust(a_nonref),
606                                 )
607                         },
608                         nativeSendError::TooBigPacket => SendError::TooBigPacket,
609                         nativeSendError::TooFewBlindedHops => SendError::TooFewBlindedHops,
610                         nativeSendError::InvalidFirstHop => SendError::InvalidFirstHop,
611                         nativeSendError::InvalidMessage => SendError::InvalidMessage,
612                         nativeSendError::BufferFull => SendError::BufferFull,
613                         nativeSendError::GetNodeIdFailed => SendError::GetNodeIdFailed,
614                         nativeSendError::BlindedPathAdvanceFailed => SendError::BlindedPathAdvanceFailed,
615                 }
616         }
617         #[allow(unused)]
618         pub(crate) fn native_into(native: nativeSendError) -> Self {
619                 match native {
620                         nativeSendError::Secp256k1 (mut a, ) => {
621                                 SendError::Secp256k1 (
622                                         crate::c_types::Secp256k1Error::from_rust(a),
623                                 )
624                         },
625                         nativeSendError::TooBigPacket => SendError::TooBigPacket,
626                         nativeSendError::TooFewBlindedHops => SendError::TooFewBlindedHops,
627                         nativeSendError::InvalidFirstHop => SendError::InvalidFirstHop,
628                         nativeSendError::InvalidMessage => SendError::InvalidMessage,
629                         nativeSendError::BufferFull => SendError::BufferFull,
630                         nativeSendError::GetNodeIdFailed => SendError::GetNodeIdFailed,
631                         nativeSendError::BlindedPathAdvanceFailed => SendError::BlindedPathAdvanceFailed,
632                 }
633         }
634 }
635 /// Frees any resources used by the SendError
636 #[no_mangle]
637 pub extern "C" fn SendError_free(this_ptr: SendError) { }
638 /// Creates a copy of the SendError
639 #[no_mangle]
640 pub extern "C" fn SendError_clone(orig: &SendError) -> SendError {
641         orig.clone()
642 }
643 #[allow(unused)]
644 /// Used only if an object of this type is returned as a trait impl by a method
645 pub(crate) extern "C" fn SendError_clone_void(this_ptr: *const c_void) -> *mut c_void {
646         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const SendError)).clone() })) as *mut c_void
647 }
648 #[allow(unused)]
649 /// Used only if an object of this type is returned as a trait impl by a method
650 pub(crate) extern "C" fn SendError_free_void(this_ptr: *mut c_void) {
651         let _ = unsafe { Box::from_raw(this_ptr as *mut SendError) };
652 }
653 #[no_mangle]
654 /// Utility method to constructs a new Secp256k1-variant SendError
655 pub extern "C" fn SendError_secp256k1(a: crate::c_types::Secp256k1Error) -> SendError {
656         SendError::Secp256k1(a, )
657 }
658 #[no_mangle]
659 /// Utility method to constructs a new TooBigPacket-variant SendError
660 pub extern "C" fn SendError_too_big_packet() -> SendError {
661         SendError::TooBigPacket}
662 #[no_mangle]
663 /// Utility method to constructs a new TooFewBlindedHops-variant SendError
664 pub extern "C" fn SendError_too_few_blinded_hops() -> SendError {
665         SendError::TooFewBlindedHops}
666 #[no_mangle]
667 /// Utility method to constructs a new InvalidFirstHop-variant SendError
668 pub extern "C" fn SendError_invalid_first_hop() -> SendError {
669         SendError::InvalidFirstHop}
670 #[no_mangle]
671 /// Utility method to constructs a new InvalidMessage-variant SendError
672 pub extern "C" fn SendError_invalid_message() -> SendError {
673         SendError::InvalidMessage}
674 #[no_mangle]
675 /// Utility method to constructs a new BufferFull-variant SendError
676 pub extern "C" fn SendError_buffer_full() -> SendError {
677         SendError::BufferFull}
678 #[no_mangle]
679 /// Utility method to constructs a new GetNodeIdFailed-variant SendError
680 pub extern "C" fn SendError_get_node_id_failed() -> SendError {
681         SendError::GetNodeIdFailed}
682 #[no_mangle]
683 /// Utility method to constructs a new BlindedPathAdvanceFailed-variant SendError
684 pub extern "C" fn SendError_blinded_path_advance_failed() -> SendError {
685         SendError::BlindedPathAdvanceFailed}
686 /// Checks if two SendErrors contain equal inner contents.
687 /// This ignores pointers and is_owned flags and looks at the values in fields.
688 #[no_mangle]
689 pub extern "C" fn SendError_eq(a: &SendError, b: &SendError) -> bool {
690         if &a.to_native() == &b.to_native() { true } else { false }
691 }
692 /// Handler for custom onion messages. If you are using [`SimpleArcOnionMessenger`],
693 /// [`SimpleRefOnionMessenger`], or prefer to ignore inbound custom onion messages,
694 /// [`IgnoringMessageHandler`] must be provided to [`OnionMessenger::new`]. Otherwise, a custom
695 /// implementation of this trait must be provided, with [`CustomMessage`] specifying the supported
696 /// message types.
697 ///
698 /// See [`OnionMessenger`] for example usage.
699 ///
700 /// [`IgnoringMessageHandler`]: crate::ln::peer_handler::IgnoringMessageHandler
701 /// [`CustomMessage`]: Self::CustomMessage
702 #[repr(C)]
703 pub struct CustomOnionMessageHandler {
704         /// An opaque pointer which is passed to your function implementations as an argument.
705         /// This has no meaning in the LDK, and can be NULL or any other value.
706         pub this_arg: *mut c_void,
707         /// Called with the custom message that was received, returning a response to send, if any.
708         ///
709         /// The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
710         pub handle_custom_message: extern "C" fn (this_arg: *const c_void, msg: crate::lightning::onion_message::packet::OnionMessageContents) -> crate::c_types::derived::COption_OnionMessageContentsZ,
711         /// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
712         /// message type is unknown.
713         pub read_custom_message: extern "C" fn (this_arg: *const c_void, message_type: u64, buffer: crate::c_types::u8slice) -> crate::c_types::derived::CResult_COption_OnionMessageContentsZDecodeErrorZ,
714         /// Releases any [`Self::CustomMessage`]s that need to be sent.
715         ///
716         /// Typically, this is used for messages initiating a message flow rather than in response to
717         /// another message. The latter should use the return value of [`Self::handle_custom_message`].
718         pub release_pending_custom_messages: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_C3Tuple_OnionMessageContentsDestinationBlindedPathZZ,
719         /// Frees any resources associated with this object given its this_arg pointer.
720         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
721         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
722 }
723 unsafe impl Send for CustomOnionMessageHandler {}
724 unsafe impl Sync for CustomOnionMessageHandler {}
725 #[allow(unused)]
726 pub(crate) fn CustomOnionMessageHandler_clone_fields(orig: &CustomOnionMessageHandler) -> CustomOnionMessageHandler {
727         CustomOnionMessageHandler {
728                 this_arg: orig.this_arg,
729                 handle_custom_message: Clone::clone(&orig.handle_custom_message),
730                 read_custom_message: Clone::clone(&orig.read_custom_message),
731                 release_pending_custom_messages: Clone::clone(&orig.release_pending_custom_messages),
732                 free: Clone::clone(&orig.free),
733         }
734 }
735
736 use lightning::onion_message::messenger::CustomOnionMessageHandler as rustCustomOnionMessageHandler;
737 impl rustCustomOnionMessageHandler for CustomOnionMessageHandler {
738         type CustomMessage = crate::lightning::onion_message::packet::OnionMessageContents;
739         fn handle_custom_message(&self, mut msg: crate::lightning::onion_message::packet::OnionMessageContents) -> Option<crate::lightning::onion_message::packet::OnionMessageContents> {
740                 let mut ret = (self.handle_custom_message)(self.this_arg, Into::into(msg));
741                 let mut local_ret = { /*ret*/ let ret_opt = ret; if ret_opt.is_none() { None } else { Some({ { { ret_opt.take() } }})} };
742                 local_ret
743         }
744         fn read_custom_message<R:crate::c_types::io::Read>(&self, mut message_type: u64, mut buffer: &mut R) -> Result<Option<crate::lightning::onion_message::packet::OnionMessageContents>, lightning::ln::msgs::DecodeError> {
745                 let mut ret = (self.read_custom_message)(self.this_arg, message_type, crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec(buffer)));
746                 let mut local_ret = match ret.result_ok { true => Ok( { let mut local_ret_0 = { /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ let ret_0_opt = (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }); if ret_0_opt.is_none() { None } else { Some({ { { ret_0_opt.take() } }})} }; local_ret_0 }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })};
747                 local_ret
748         }
749         fn release_pending_custom_messages(&self) -> Vec<(crate::lightning::onion_message::packet::OnionMessageContents, lightning::onion_message::messenger::Destination, Option<lightning::blinded_path::BlindedPath>)> {
750                 let mut ret = (self.release_pending_custom_messages)(self.this_arg);
751                 let mut local_ret = Vec::new(); for mut item in ret.into_rust().drain(..) { local_ret.push( { let (mut orig_ret_0_0, mut orig_ret_0_1, mut orig_ret_0_2) = item.to_rust(); let mut local_orig_ret_0_2 = if orig_ret_0_2.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(orig_ret_0_2.take_inner()) } }) }; let mut local_ret_0 = (orig_ret_0_0, orig_ret_0_1.into_native(), local_orig_ret_0_2); local_ret_0 }); };
752                 local_ret
753         }
754 }
755
756 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
757 // directly as a Deref trait in higher-level structs:
758 impl core::ops::Deref for CustomOnionMessageHandler {
759         type Target = Self;
760         fn deref(&self) -> &Self {
761                 self
762         }
763 }
764 impl core::ops::DerefMut for CustomOnionMessageHandler {
765         fn deref_mut(&mut self) -> &mut Self {
766                 self
767         }
768 }
769 /// Calls the free function if one is set
770 #[no_mangle]
771 pub extern "C" fn CustomOnionMessageHandler_free(this_ptr: CustomOnionMessageHandler) { }
772 impl Drop for CustomOnionMessageHandler {
773         fn drop(&mut self) {
774                 if let Some(f) = self.free {
775                         f(self.this_arg);
776                 }
777         }
778 }
779 /// A processed incoming onion message, containing either a Forward (another onion message)
780 /// or a Receive payload with decrypted contents.
781 #[derive(Clone)]
782 #[must_use]
783 #[repr(C)]
784 pub enum PeeledOnion {
785         /// Forwarded onion, with the next node id and a new onion
786         Forward(
787                 crate::c_types::PublicKey,
788                 crate::lightning::ln::msgs::OnionMessage),
789         /// Received onion message, with decrypted contents, path_id, and reply path
790         Receive(
791                 crate::lightning::onion_message::packet::ParsedOnionMessageContents,
792                 ///
793                 /// Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None
794                 crate::c_types::ThirtyTwoBytes,
795                 ///
796                 /// Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None
797                 crate::lightning::blinded_path::BlindedPath),
798 }
799 use lightning::onion_message::messenger::PeeledOnion as PeeledOnionImport;
800 pub(crate) type nativePeeledOnion = PeeledOnionImport<crate::lightning::onion_message::packet::OnionMessageContents>;
801
802 impl PeeledOnion {
803         #[allow(unused)]
804         pub(crate) fn to_native(&self) -> nativePeeledOnion {
805                 match self {
806                         PeeledOnion::Forward (ref a, ref b, ) => {
807                                 let mut a_nonref = Clone::clone(a);
808                                 let mut b_nonref = Clone::clone(b);
809                                 nativePeeledOnion::Forward (
810                                         a_nonref.into_rust(),
811                                         *unsafe { Box::from_raw(b_nonref.take_inner()) },
812                                 )
813                         },
814                         PeeledOnion::Receive (ref a, ref b, ref c, ) => {
815                                 let mut a_nonref = Clone::clone(a);
816                                 let mut b_nonref = Clone::clone(b);
817                                 let mut local_b_nonref = if b_nonref.data == [0; 32] { None } else { Some( { b_nonref.data }) };
818                                 let mut c_nonref = Clone::clone(c);
819                                 let mut local_c_nonref = if c_nonref.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(c_nonref.take_inner()) } }) };
820                                 nativePeeledOnion::Receive (
821                                         a_nonref.into_native(),
822                                         local_b_nonref,
823                                         local_c_nonref,
824                                 )
825                         },
826                 }
827         }
828         #[allow(unused)]
829         pub(crate) fn into_native(self) -> nativePeeledOnion {
830                 match self {
831                         PeeledOnion::Forward (mut a, mut b, ) => {
832                                 nativePeeledOnion::Forward (
833                                         a.into_rust(),
834                                         *unsafe { Box::from_raw(b.take_inner()) },
835                                 )
836                         },
837                         PeeledOnion::Receive (mut a, mut b, mut c, ) => {
838                                 let mut local_b = if b.data == [0; 32] { None } else { Some( { b.data }) };
839                                 let mut local_c = if c.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(c.take_inner()) } }) };
840                                 nativePeeledOnion::Receive (
841                                         a.into_native(),
842                                         local_b,
843                                         local_c,
844                                 )
845                         },
846                 }
847         }
848         #[allow(unused)]
849         pub(crate) fn from_native(native: &nativePeeledOnion) -> Self {
850                 match native {
851                         nativePeeledOnion::Forward (ref a, ref b, ) => {
852                                 let mut a_nonref = Clone::clone(a);
853                                 let mut b_nonref = Clone::clone(b);
854                                 PeeledOnion::Forward (
855                                         crate::c_types::PublicKey::from_rust(&a_nonref),
856                                         crate::lightning::ln::msgs::OnionMessage { inner: ObjOps::heap_alloc(b_nonref), is_owned: true },
857                                 )
858                         },
859                         nativePeeledOnion::Receive (ref a, ref b, ref c, ) => {
860                                 let mut a_nonref = Clone::clone(a);
861                                 let mut b_nonref = Clone::clone(b);
862                                 let mut local_b_nonref = if b_nonref.is_none() { crate::c_types::ThirtyTwoBytes { data: [0; 32] } } else {  { crate::c_types::ThirtyTwoBytes { data: (b_nonref.unwrap()) } } };
863                                 let mut c_nonref = Clone::clone(c);
864                                 let mut local_c_nonref = crate::lightning::blinded_path::BlindedPath { inner: if c_nonref.is_none() { core::ptr::null_mut() } else {  { ObjOps::heap_alloc((c_nonref.unwrap())) } }, is_owned: true };
865                                 PeeledOnion::Receive (
866                                         crate::lightning::onion_message::packet::ParsedOnionMessageContents::native_into(a_nonref),
867                                         local_b_nonref,
868                                         local_c_nonref,
869                                 )
870                         },
871                 }
872         }
873         #[allow(unused)]
874         pub(crate) fn native_into(native: nativePeeledOnion) -> Self {
875                 match native {
876                         nativePeeledOnion::Forward (mut a, mut b, ) => {
877                                 PeeledOnion::Forward (
878                                         crate::c_types::PublicKey::from_rust(&a),
879                                         crate::lightning::ln::msgs::OnionMessage { inner: ObjOps::heap_alloc(b), is_owned: true },
880                                 )
881                         },
882                         nativePeeledOnion::Receive (mut a, mut b, mut c, ) => {
883                                 let mut local_b = if b.is_none() { crate::c_types::ThirtyTwoBytes { data: [0; 32] } } else {  { crate::c_types::ThirtyTwoBytes { data: (b.unwrap()) } } };
884                                 let mut local_c = crate::lightning::blinded_path::BlindedPath { inner: if c.is_none() { core::ptr::null_mut() } else {  { ObjOps::heap_alloc((c.unwrap())) } }, is_owned: true };
885                                 PeeledOnion::Receive (
886                                         crate::lightning::onion_message::packet::ParsedOnionMessageContents::native_into(a),
887                                         local_b,
888                                         local_c,
889                                 )
890                         },
891                 }
892         }
893 }
894 /// Frees any resources used by the PeeledOnion
895 #[no_mangle]
896 pub extern "C" fn PeeledOnion_free(this_ptr: PeeledOnion) { }
897 /// Creates a copy of the PeeledOnion
898 #[no_mangle]
899 pub extern "C" fn PeeledOnion_clone(orig: &PeeledOnion) -> PeeledOnion {
900         orig.clone()
901 }
902 #[allow(unused)]
903 /// Used only if an object of this type is returned as a trait impl by a method
904 pub(crate) extern "C" fn PeeledOnion_clone_void(this_ptr: *const c_void) -> *mut c_void {
905         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const PeeledOnion)).clone() })) as *mut c_void
906 }
907 #[allow(unused)]
908 /// Used only if an object of this type is returned as a trait impl by a method
909 pub(crate) extern "C" fn PeeledOnion_free_void(this_ptr: *mut c_void) {
910         let _ = unsafe { Box::from_raw(this_ptr as *mut PeeledOnion) };
911 }
912 #[no_mangle]
913 /// Utility method to constructs a new Forward-variant PeeledOnion
914 pub extern "C" fn PeeledOnion_forward(a: crate::c_types::PublicKey,b: crate::lightning::ln::msgs::OnionMessage) -> PeeledOnion {
915         PeeledOnion::Forward(a, b, )
916 }
917 #[no_mangle]
918 /// Utility method to constructs a new Receive-variant PeeledOnion
919 pub extern "C" fn PeeledOnion_receive(a: crate::lightning::onion_message::packet::ParsedOnionMessageContents,b: crate::c_types::ThirtyTwoBytes,c: crate::lightning::blinded_path::BlindedPath) -> PeeledOnion {
920         PeeledOnion::Receive(a, b, c, )
921 }
922 /// Creates an [`OnionMessage`] with the given `contents` for sending to the destination of
923 /// `path`.
924 ///
925 /// Returns both the node id of the peer to send the message to and the message itself.
926 ///
927 /// Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None
928 #[no_mangle]
929 pub extern "C" fn create_onion_message(entropy_source: &crate::lightning::sign::EntropySource, node_signer: &crate::lightning::sign::NodeSigner, mut path: crate::lightning::onion_message::messenger::OnionMessagePath, mut contents: crate::lightning::onion_message::packet::OnionMessageContents, mut reply_path: crate::lightning::blinded_path::BlindedPath) -> crate::c_types::derived::CResult_C2Tuple_PublicKeyOnionMessageZSendErrorZ {
930         let mut local_reply_path = if reply_path.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(reply_path.take_inner()) } }) };
931         let mut ret = lightning::onion_message::messenger::create_onion_message::<crate::lightning::sign::EntropySource, crate::lightning::sign::NodeSigner, crate::lightning::onion_message::packet::OnionMessageContents>(entropy_source, node_signer, secp256k1::global::SECP256K1, *unsafe { Box::from_raw(path.take_inner()) }, contents, local_reply_path);
932         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let (mut orig_ret_0_0, mut orig_ret_0_1) = o; let mut local_ret_0 = (crate::c_types::PublicKey::from_rust(&orig_ret_0_0), crate::lightning::ln::msgs::OnionMessage { inner: ObjOps::heap_alloc(orig_ret_0_1), is_owned: true }).into(); local_ret_0 }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::onion_message::messenger::SendError::native_into(e) }).into() };
933         local_ret
934 }
935
936 /// Decode one layer of an incoming [`OnionMessage`].
937 ///
938 /// Returns either the next layer of the onion for forwarding or the decrypted content for the
939 /// receiver.
940 #[no_mangle]
941 pub extern "C" fn peel_onion_message(msg: &crate::lightning::ln::msgs::OnionMessage, mut node_signer: crate::lightning::sign::NodeSigner, mut logger: crate::lightning::util::logger::Logger, mut custom_handler: crate::lightning::onion_message::messenger::CustomOnionMessageHandler) -> crate::c_types::derived::CResult_PeeledOnionNoneZ {
942         let mut ret = lightning::onion_message::messenger::peel_onion_message::<crate::lightning::sign::NodeSigner, crate::lightning::util::logger::Logger, crate::lightning::onion_message::messenger::CustomOnionMessageHandler>(msg.get_native_ref(), secp256k1::global::SECP256K1, node_signer, logger, custom_handler);
943         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::onion_message::messenger::PeeledOnion::native_into(o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
944         local_ret
945 }
946
947 /// Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
948 /// their respective handlers.
949 #[must_use]
950 #[no_mangle]
951 pub extern "C" fn OnionMessenger_new(mut entropy_source: crate::lightning::sign::EntropySource, mut node_signer: crate::lightning::sign::NodeSigner, mut logger: crate::lightning::util::logger::Logger, mut message_router: crate::lightning::onion_message::messenger::MessageRouter, mut offers_handler: crate::lightning::onion_message::offers::OffersMessageHandler, mut custom_handler: crate::lightning::onion_message::messenger::CustomOnionMessageHandler) -> crate::lightning::onion_message::messenger::OnionMessenger {
952         let mut ret = lightning::onion_message::messenger::OnionMessenger::new(entropy_source, node_signer, logger, message_router, offers_handler, custom_handler);
953         crate::lightning::onion_message::messenger::OnionMessenger { inner: ObjOps::heap_alloc(ret), is_owned: true }
954 }
955
956 /// Sends an [`OnionMessage`] with the given `contents` for sending to the destination of
957 /// `path`.
958 ///
959 /// See [`OnionMessenger`] for example usage.
960 ///
961 /// Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None
962 #[must_use]
963 #[no_mangle]
964 pub extern "C" fn OnionMessenger_send_onion_message(this_arg: &crate::lightning::onion_message::messenger::OnionMessenger, mut path: crate::lightning::onion_message::messenger::OnionMessagePath, mut contents: crate::lightning::onion_message::packet::OnionMessageContents, mut reply_path: crate::lightning::blinded_path::BlindedPath) -> crate::c_types::derived::CResult_NoneSendErrorZ {
965         let mut local_reply_path = if reply_path.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(reply_path.take_inner()) } }) };
966         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.send_onion_message(*unsafe { Box::from_raw(path.take_inner()) }, contents, local_reply_path);
967         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::onion_message::messenger::SendError::native_into(e) }).into() };
968         local_ret
969 }
970
971 impl From<nativeOnionMessenger> for crate::lightning::ln::msgs::OnionMessageHandler {
972         fn from(obj: nativeOnionMessenger) -> Self {
973                 let rust_obj = crate::lightning::onion_message::messenger::OnionMessenger { inner: ObjOps::heap_alloc(obj), is_owned: true };
974                 let mut ret = OnionMessenger_as_OnionMessageHandler(&rust_obj);
975                 // We want to free rust_obj when ret gets drop()'d, not rust_obj, so forget it and set ret's free() fn
976                 core::mem::forget(rust_obj);
977                 ret.free = Some(OnionMessenger_free_void);
978                 ret
979         }
980 }
981 /// Constructs a new OnionMessageHandler which calls the relevant methods on this_arg.
982 /// This copies the `inner` pointer in this_arg and thus the returned OnionMessageHandler must be freed before this_arg is
983 #[no_mangle]
984 pub extern "C" fn OnionMessenger_as_OnionMessageHandler(this_arg: &OnionMessenger) -> crate::lightning::ln::msgs::OnionMessageHandler {
985         crate::lightning::ln::msgs::OnionMessageHandler {
986                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
987                 free: None,
988                 handle_onion_message: OnionMessenger_OnionMessageHandler_handle_onion_message,
989                 next_onion_message_for_peer: OnionMessenger_OnionMessageHandler_next_onion_message_for_peer,
990                 peer_connected: OnionMessenger_OnionMessageHandler_peer_connected,
991                 peer_disconnected: OnionMessenger_OnionMessageHandler_peer_disconnected,
992                 provided_node_features: OnionMessenger_OnionMessageHandler_provided_node_features,
993                 provided_init_features: OnionMessenger_OnionMessageHandler_provided_init_features,
994         }
995 }
996
997 extern "C" fn OnionMessenger_OnionMessageHandler_handle_onion_message(this_arg: *const c_void, mut peer_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::OnionMessage) {
998         <nativeOnionMessenger as lightning::ln::msgs::OnionMessageHandler<>>::handle_onion_message(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, &peer_node_id.into_rust(), msg.get_native_ref())
999 }
1000 #[must_use]
1001 extern "C" fn OnionMessenger_OnionMessageHandler_next_onion_message_for_peer(this_arg: *const c_void, mut peer_node_id: crate::c_types::PublicKey) -> crate::lightning::ln::msgs::OnionMessage {
1002         let mut ret = <nativeOnionMessenger as lightning::ln::msgs::OnionMessageHandler<>>::next_onion_message_for_peer(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, peer_node_id.into_rust());
1003         let mut local_ret = crate::lightning::ln::msgs::OnionMessage { inner: if ret.is_none() { core::ptr::null_mut() } else {  { ObjOps::heap_alloc((ret.unwrap())) } }, is_owned: true };
1004         local_ret
1005 }
1006 #[must_use]
1007 extern "C" fn OnionMessenger_OnionMessageHandler_peer_connected(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, init: &crate::lightning::ln::msgs::Init, mut inbound: bool) -> crate::c_types::derived::CResult_NoneNoneZ {
1008         let mut ret = <nativeOnionMessenger as lightning::ln::msgs::OnionMessageHandler<>>::peer_connected(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, &their_node_id.into_rust(), init.get_native_ref(), inbound);
1009         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
1010         local_ret
1011 }
1012 extern "C" fn OnionMessenger_OnionMessageHandler_peer_disconnected(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey) {
1013         <nativeOnionMessenger as lightning::ln::msgs::OnionMessageHandler<>>::peer_disconnected(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, &their_node_id.into_rust())
1014 }
1015 #[must_use]
1016 extern "C" fn OnionMessenger_OnionMessageHandler_provided_node_features(this_arg: *const c_void) -> crate::lightning::ln::features::NodeFeatures {
1017         let mut ret = <nativeOnionMessenger as lightning::ln::msgs::OnionMessageHandler<>>::provided_node_features(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, );
1018         crate::lightning::ln::features::NodeFeatures { inner: ObjOps::heap_alloc(ret), is_owned: true }
1019 }
1020 #[must_use]
1021 extern "C" fn OnionMessenger_OnionMessageHandler_provided_init_features(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey) -> crate::lightning::ln::features::InitFeatures {
1022         let mut ret = <nativeOnionMessenger as lightning::ln::msgs::OnionMessageHandler<>>::provided_init_features(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, &their_node_id.into_rust());
1023         crate::lightning::ln::features::InitFeatures { inner: ObjOps::heap_alloc(ret), is_owned: true }
1024 }
1025