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 message related to BOLT 12 Offers.
26 crate::lightning::onion_message::offers::OffersMessage),
27 /// A custom onion message specified by the user.
29 crate::lightning::onion_message::packet::CustomOnionMessageContents),
31 use lightning::onion_message::packet::OnionMessageContents as OnionMessageContentsImport;
32 pub(crate) type nativeOnionMessageContents = OnionMessageContentsImport<crate::lightning::onion_message::packet::CustomOnionMessageContents>;
34 impl OnionMessageContents {
36 pub(crate) fn into_native(self) -> nativeOnionMessageContents {
38 OnionMessageContents::Offers (mut a, ) => {
39 nativeOnionMessageContents::Offers (
43 OnionMessageContents::Custom (mut a, ) => {
44 nativeOnionMessageContents::Custom (
51 pub(crate) fn native_into(native: nativeOnionMessageContents) -> Self {
53 nativeOnionMessageContents::Offers (mut a, ) => {
54 OnionMessageContents::Offers (
55 crate::lightning::onion_message::offers::OffersMessage::native_into(a),
58 nativeOnionMessageContents::Custom (mut a, ) => {
59 OnionMessageContents::Custom (
66 /// Frees any resources used by the OnionMessageContents
68 pub extern "C" fn OnionMessageContents_free(this_ptr: OnionMessageContents) { }
70 /// Utility method to constructs a new Offers-variant OnionMessageContents
71 pub extern "C" fn OnionMessageContents_offers(a: crate::lightning::onion_message::offers::OffersMessage) -> OnionMessageContents {
72 OnionMessageContents::Offers(a, )
75 /// Utility method to constructs a new Custom-variant OnionMessageContents
76 pub extern "C" fn OnionMessageContents_custom(a: crate::lightning::onion_message::packet::CustomOnionMessageContents) -> OnionMessageContents {
77 OnionMessageContents::Custom(a, )
79 /// The contents of a custom onion message.
81 pub struct CustomOnionMessageContents {
82 /// An opaque pointer which is passed to your function implementations as an argument.
83 /// This has no meaning in the LDK, and can be NULL or any other value.
84 pub this_arg: *mut c_void,
85 /// Returns the TLV type identifying the message contents. MUST be >= 64.
86 pub tlv_type: extern "C" fn (this_arg: *const c_void) -> u64,
87 /// Serialize the object into a byte array
88 pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
89 /// Frees any resources associated with this object given its this_arg pointer.
90 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
91 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
93 unsafe impl Send for CustomOnionMessageContents {}
94 unsafe impl Sync for CustomOnionMessageContents {}
96 pub(crate) extern "C" fn CustomOnionMessageContents_clone_fields(orig: &CustomOnionMessageContents) -> CustomOnionMessageContents {
97 CustomOnionMessageContents {
98 this_arg: orig.this_arg,
99 tlv_type: Clone::clone(&orig.tlv_type),
100 write: Clone::clone(&orig.write),
101 free: Clone::clone(&orig.free),
104 impl lightning::util::ser::Writeable for CustomOnionMessageContents {
105 fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
106 let vec = (self.write)(self.this_arg);
107 w.write_all(vec.as_slice())
111 use lightning::onion_message::packet::CustomOnionMessageContents as rustCustomOnionMessageContents;
112 impl rustCustomOnionMessageContents for CustomOnionMessageContents {
113 fn tlv_type(&self) -> u64 {
114 let mut ret = (self.tlv_type)(self.this_arg);
119 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
120 // directly as a Deref trait in higher-level structs:
121 impl core::ops::Deref for CustomOnionMessageContents {
123 fn deref(&self) -> &Self {
127 /// Calls the free function if one is set
129 pub extern "C" fn CustomOnionMessageContents_free(this_ptr: CustomOnionMessageContents) { }
130 impl Drop for CustomOnionMessageContents {
132 if let Some(f) = self.free {