1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
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.
9 //! Structs and enums useful for constructing and reading an onion message packet.
11 use alloc::str::FromStr;
12 use core::ffi::c_void;
13 use core::convert::Infallible;
14 use bitcoin::hashes::Hash;
15 use crate::c_types::*;
16 #[cfg(feature="no-std")]
17 use alloc::{vec::Vec, boxed::Box};
19 /// The contents of an onion message. In the context of offers, this would be the invoice, invoice
20 /// request, or invoice error.
23 pub enum OnionMessageContents {
24 /// A custom onion message specified by the user.
26 crate::lightning::onion_message::packet::CustomOnionMessageContents),
28 use lightning::onion_message::packet::OnionMessageContents as OnionMessageContentsImport;
29 pub(crate) type nativeOnionMessageContents = OnionMessageContentsImport<crate::lightning::onion_message::packet::CustomOnionMessageContents>;
31 impl OnionMessageContents {
33 pub(crate) fn into_native(self) -> nativeOnionMessageContents {
35 OnionMessageContents::Custom (mut a, ) => {
36 nativeOnionMessageContents::Custom (
43 pub(crate) fn native_into(native: nativeOnionMessageContents) -> Self {
45 nativeOnionMessageContents::Custom (mut a, ) => {
46 OnionMessageContents::Custom (
53 /// Frees any resources used by the OnionMessageContents
55 pub extern "C" fn OnionMessageContents_free(this_ptr: OnionMessageContents) { }
57 /// Utility method to constructs a new Custom-variant OnionMessageContents
58 pub extern "C" fn OnionMessageContents_custom(a: crate::lightning::onion_message::packet::CustomOnionMessageContents) -> OnionMessageContents {
59 OnionMessageContents::Custom(a, )
61 /// The contents of a custom onion message.
63 pub struct CustomOnionMessageContents {
64 /// An opaque pointer which is passed to your function implementations as an argument.
65 /// This has no meaning in the LDK, and can be NULL or any other value.
66 pub this_arg: *mut c_void,
67 /// Returns the TLV type identifying the message contents. MUST be >= 64.
69 pub tlv_type: extern "C" fn (this_arg: *const c_void) -> u64,
70 /// Serialize the object into a byte array
71 pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
72 /// Frees any resources associated with this object given its this_arg pointer.
73 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
74 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
76 unsafe impl Send for CustomOnionMessageContents {}
77 unsafe impl Sync for CustomOnionMessageContents {}
79 pub(crate) extern "C" fn CustomOnionMessageContents_clone_fields(orig: &CustomOnionMessageContents) -> CustomOnionMessageContents {
80 CustomOnionMessageContents {
81 this_arg: orig.this_arg,
82 tlv_type: Clone::clone(&orig.tlv_type),
83 write: Clone::clone(&orig.write),
84 free: Clone::clone(&orig.free),
87 impl lightning::util::ser::Writeable for CustomOnionMessageContents {
88 fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
89 let vec = (self.write)(self.this_arg);
90 w.write_all(vec.as_slice())
94 use lightning::onion_message::packet::CustomOnionMessageContents as rustCustomOnionMessageContents;
95 impl rustCustomOnionMessageContents for CustomOnionMessageContents {
96 fn tlv_type(&self) -> u64 {
97 let mut ret = (self.tlv_type)(self.this_arg);
102 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
103 // directly as a Deref trait in higher-level structs:
104 impl core::ops::Deref for CustomOnionMessageContents {
106 fn deref(&self) -> &Self {
110 /// Calls the free function if one is set
112 pub extern "C" fn CustomOnionMessageContents_free(this_ptr: CustomOnionMessageContents) { }
113 impl Drop for CustomOnionMessageContents {
115 if let Some(f) = self.free {