Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning / onion_message / packet.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 //! Structs and enums useful for constructing and reading an onion message packet.
10
11 use alloc::str::FromStr;
12 use alloc::string::String;
13 use core::ffi::c_void;
14 use core::convert::Infallible;
15 use bitcoin::hashes::Hash;
16 use crate::c_types::*;
17 #[cfg(feature="no-std")]
18 use alloc::{vec::Vec, boxed::Box};
19
20
21 use lightning::onion_message::packet::Packet as nativePacketImport;
22 pub(crate) type nativePacket = nativePacketImport;
23
24 /// Packet of hop data for next peer
25 #[must_use]
26 #[repr(C)]
27 pub struct Packet {
28         /// A pointer to the opaque Rust object.
29
30         /// Nearly everywhere, inner must be non-null, however in places where
31         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
32         pub inner: *mut nativePacket,
33         /// Indicates that this is the only struct which contains the same pointer.
34
35         /// Rust functions which take ownership of an object provided via an argument require
36         /// this to be true and invalidate the object pointed to by inner.
37         pub is_owned: bool,
38 }
39
40 impl Drop for Packet {
41         fn drop(&mut self) {
42                 if self.is_owned && !<*mut nativePacket>::is_null(self.inner) {
43                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
44                 }
45         }
46 }
47 /// Frees any resources used by the Packet, if is_owned is set and inner is non-NULL.
48 #[no_mangle]
49 pub extern "C" fn Packet_free(this_obj: Packet) { }
50 #[allow(unused)]
51 /// Used only if an object of this type is returned as a trait impl by a method
52 pub(crate) extern "C" fn Packet_free_void(this_ptr: *mut c_void) {
53         let _ = unsafe { Box::from_raw(this_ptr as *mut nativePacket) };
54 }
55 #[allow(unused)]
56 impl Packet {
57         pub(crate) fn get_native_ref(&self) -> &'static nativePacket {
58                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
59         }
60         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativePacket {
61                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
62         }
63         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
64         pub(crate) fn take_inner(mut self) -> *mut nativePacket {
65                 assert!(self.is_owned);
66                 let ret = ObjOps::untweak_ptr(self.inner);
67                 self.inner = core::ptr::null_mut();
68                 ret
69         }
70 }
71 /// Bolt 04 version number
72 #[no_mangle]
73 pub extern "C" fn Packet_get_version(this_ptr: &Packet) -> u8 {
74         let mut inner_val = &mut this_ptr.get_native_mut_ref().version;
75         *inner_val
76 }
77 /// Bolt 04 version number
78 #[no_mangle]
79 pub extern "C" fn Packet_set_version(this_ptr: &mut Packet, mut val: u8) {
80         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.version = val;
81 }
82 /// A random sepc256k1 point, used to build the ECDH shared secret to decrypt hop_data
83 #[no_mangle]
84 pub extern "C" fn Packet_get_public_key(this_ptr: &Packet) -> crate::c_types::PublicKey {
85         let mut inner_val = &mut this_ptr.get_native_mut_ref().public_key;
86         crate::c_types::PublicKey::from_rust(&inner_val)
87 }
88 /// A random sepc256k1 point, used to build the ECDH shared secret to decrypt hop_data
89 #[no_mangle]
90 pub extern "C" fn Packet_set_public_key(this_ptr: &mut Packet, mut val: crate::c_types::PublicKey) {
91         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.public_key = val.into_rust();
92 }
93 /// Encrypted payload for the next hop
94 ///
95 /// Returns a copy of the field.
96 #[no_mangle]
97 pub extern "C" fn Packet_get_hop_data(this_ptr: &Packet) -> crate::c_types::derived::CVec_u8Z {
98         let mut inner_val = this_ptr.get_native_mut_ref().hop_data.clone();
99         let mut local_inner_val = Vec::new(); for mut item in inner_val.drain(..) { local_inner_val.push( { item }); };
100         local_inner_val.into()
101 }
102 /// Encrypted payload for the next hop
103 #[no_mangle]
104 pub extern "C" fn Packet_set_hop_data(this_ptr: &mut Packet, mut val: crate::c_types::derived::CVec_u8Z) {
105         let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item }); };
106         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.hop_data = local_val;
107 }
108 /// HMAC to verify the integrity of hop_data
109 #[no_mangle]
110 pub extern "C" fn Packet_get_hmac(this_ptr: &Packet) -> *const [u8; 32] {
111         let mut inner_val = &mut this_ptr.get_native_mut_ref().hmac;
112         inner_val
113 }
114 /// HMAC to verify the integrity of hop_data
115 #[no_mangle]
116 pub extern "C" fn Packet_set_hmac(this_ptr: &mut Packet, mut val: crate::c_types::ThirtyTwoBytes) {
117         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.hmac = val.data;
118 }
119 /// Constructs a new Packet given each field
120 #[must_use]
121 #[no_mangle]
122 pub extern "C" fn Packet_new(mut version_arg: u8, mut public_key_arg: crate::c_types::PublicKey, mut hop_data_arg: crate::c_types::derived::CVec_u8Z, mut hmac_arg: crate::c_types::ThirtyTwoBytes) -> Packet {
123         let mut local_hop_data_arg = Vec::new(); for mut item in hop_data_arg.into_rust().drain(..) { local_hop_data_arg.push( { item }); };
124         Packet { inner: ObjOps::heap_alloc(nativePacket {
125                 version: version_arg,
126                 public_key: public_key_arg.into_rust(),
127                 hop_data: local_hop_data_arg,
128                 hmac: hmac_arg.data,
129         }), is_owned: true }
130 }
131 impl Clone for Packet {
132         fn clone(&self) -> Self {
133                 Self {
134                         inner: if <*mut nativePacket>::is_null(self.inner) { core::ptr::null_mut() } else {
135                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
136                         is_owned: true,
137                 }
138         }
139 }
140 #[allow(unused)]
141 /// Used only if an object of this type is returned as a trait impl by a method
142 pub(crate) extern "C" fn Packet_clone_void(this_ptr: *const c_void) -> *mut c_void {
143         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativePacket)).clone() })) as *mut c_void
144 }
145 #[no_mangle]
146 /// Creates a copy of the Packet
147 pub extern "C" fn Packet_clone(orig: &Packet) -> Packet {
148         orig.clone()
149 }
150 /// Checks if two Packets contain equal inner contents.
151 /// This ignores pointers and is_owned flags and looks at the values in fields.
152 /// Two objects with NULL inner values will be considered "equal" here.
153 #[no_mangle]
154 pub extern "C" fn Packet_eq(a: &Packet, b: &Packet) -> bool {
155         if a.inner == b.inner { return true; }
156         if a.inner.is_null() || b.inner.is_null() { return false; }
157         if a.get_native_ref() == b.get_native_ref() { true } else { false }
158 }
159 #[no_mangle]
160 /// Serialize the Packet object into a byte array which can be read by Packet_read
161 pub extern "C" fn Packet_write(obj: &crate::lightning::onion_message::packet::Packet) -> crate::c_types::derived::CVec_u8Z {
162         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
163 }
164 #[no_mangle]
165 pub(crate) extern "C" fn Packet_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
166         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativePacket) })
167 }
168 /// The contents of an onion message. In the context of offers, this would be the invoice, invoice
169 /// request, or invoice error.
170 #[derive(Clone)]
171 #[must_use]
172 #[repr(C)]
173 pub enum OnionMessageContents {
174         /// A message related to BOLT 12 Offers.
175         Offers(
176                 crate::lightning::onion_message::offers::OffersMessage),
177         /// A custom onion message specified by the user.
178         Custom(
179                 crate::lightning::onion_message::packet::CustomOnionMessageContents),
180 }
181 use lightning::onion_message::packet::OnionMessageContents as OnionMessageContentsImport;
182 pub(crate) type nativeOnionMessageContents = OnionMessageContentsImport<crate::lightning::onion_message::packet::CustomOnionMessageContents>;
183
184 impl OnionMessageContents {
185         #[allow(unused)]
186         pub(crate) fn to_native(&self) -> nativeOnionMessageContents {
187                 match self {
188                         OnionMessageContents::Offers (ref a, ) => {
189                                 let mut a_nonref = Clone::clone(a);
190                                 nativeOnionMessageContents::Offers (
191                                         a_nonref.into_native(),
192                                 )
193                         },
194                         OnionMessageContents::Custom (ref a, ) => {
195                                 let mut a_nonref = Clone::clone(a);
196                                 nativeOnionMessageContents::Custom (
197                                         a_nonref,
198                                 )
199                         },
200                 }
201         }
202         #[allow(unused)]
203         pub(crate) fn into_native(self) -> nativeOnionMessageContents {
204                 match self {
205                         OnionMessageContents::Offers (mut a, ) => {
206                                 nativeOnionMessageContents::Offers (
207                                         a.into_native(),
208                                 )
209                         },
210                         OnionMessageContents::Custom (mut a, ) => {
211                                 nativeOnionMessageContents::Custom (
212                                         a,
213                                 )
214                         },
215                 }
216         }
217         #[allow(unused)]
218         pub(crate) fn from_native(native: &nativeOnionMessageContents) -> Self {
219                 match native {
220                         nativeOnionMessageContents::Offers (ref a, ) => {
221                                 let mut a_nonref = Clone::clone(a);
222                                 OnionMessageContents::Offers (
223                                         crate::lightning::onion_message::offers::OffersMessage::native_into(a_nonref),
224                                 )
225                         },
226                         nativeOnionMessageContents::Custom (ref a, ) => {
227                                 let mut a_nonref = Clone::clone(a);
228                                 OnionMessageContents::Custom (
229                                         Into::into(a_nonref),
230                                 )
231                         },
232                 }
233         }
234         #[allow(unused)]
235         pub(crate) fn native_into(native: nativeOnionMessageContents) -> Self {
236                 match native {
237                         nativeOnionMessageContents::Offers (mut a, ) => {
238                                 OnionMessageContents::Offers (
239                                         crate::lightning::onion_message::offers::OffersMessage::native_into(a),
240                                 )
241                         },
242                         nativeOnionMessageContents::Custom (mut a, ) => {
243                                 OnionMessageContents::Custom (
244                                         Into::into(a),
245                                 )
246                         },
247                 }
248         }
249 }
250 /// Frees any resources used by the OnionMessageContents
251 #[no_mangle]
252 pub extern "C" fn OnionMessageContents_free(this_ptr: OnionMessageContents) { }
253 /// Creates a copy of the OnionMessageContents
254 #[no_mangle]
255 pub extern "C" fn OnionMessageContents_clone(orig: &OnionMessageContents) -> OnionMessageContents {
256         orig.clone()
257 }
258 #[no_mangle]
259 /// Utility method to constructs a new Offers-variant OnionMessageContents
260 pub extern "C" fn OnionMessageContents_offers(a: crate::lightning::onion_message::offers::OffersMessage) -> OnionMessageContents {
261         OnionMessageContents::Offers(a, )
262 }
263 #[no_mangle]
264 /// Utility method to constructs a new Custom-variant OnionMessageContents
265 pub extern "C" fn OnionMessageContents_custom(a: crate::lightning::onion_message::packet::CustomOnionMessageContents) -> OnionMessageContents {
266         OnionMessageContents::Custom(a, )
267 }
268 /// The contents of a custom onion message.
269 #[repr(C)]
270 pub struct CustomOnionMessageContents {
271         /// An opaque pointer which is passed to your function implementations as an argument.
272         /// This has no meaning in the LDK, and can be NULL or any other value.
273         pub this_arg: *mut c_void,
274         /// Returns the TLV type identifying the message contents. MUST be >= 64.
275         pub tlv_type: extern "C" fn (this_arg: *const c_void) -> u64,
276         /// Serialize the object into a byte array
277         pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
278         /// Called, if set, after this CustomOnionMessageContents has been cloned into a duplicate object.
279         /// The new CustomOnionMessageContents is provided, and should be mutated as needed to perform a
280         /// deep copy of the object pointed to by this_arg or avoid any double-freeing.
281         pub cloned: Option<extern "C" fn (new_CustomOnionMessageContents: &mut CustomOnionMessageContents)>,
282         /// Frees any resources associated with this object given its this_arg pointer.
283         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
284         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
285 }
286 unsafe impl Send for CustomOnionMessageContents {}
287 unsafe impl Sync for CustomOnionMessageContents {}
288 pub(crate) fn CustomOnionMessageContents_clone_fields(orig: &CustomOnionMessageContents) -> CustomOnionMessageContents {
289         CustomOnionMessageContents {
290                 this_arg: orig.this_arg,
291                 tlv_type: Clone::clone(&orig.tlv_type),
292                 write: Clone::clone(&orig.write),
293                 cloned: Clone::clone(&orig.cloned),
294                 free: Clone::clone(&orig.free),
295         }
296 }
297 impl lightning::util::ser::Writeable for CustomOnionMessageContents {
298         fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
299                 let vec = (self.write)(self.this_arg);
300                 w.write_all(vec.as_slice())
301         }
302 }
303 #[no_mangle]
304 /// Creates a copy of a CustomOnionMessageContents
305 pub extern "C" fn CustomOnionMessageContents_clone(orig: &CustomOnionMessageContents) -> CustomOnionMessageContents {
306         let mut res = CustomOnionMessageContents_clone_fields(orig);
307         if let Some(f) = orig.cloned { (f)(&mut res) };
308         res
309 }
310 impl Clone for CustomOnionMessageContents {
311         fn clone(&self) -> Self {
312                 CustomOnionMessageContents_clone(self)
313         }
314 }
315
316 use lightning::onion_message::packet::CustomOnionMessageContents as rustCustomOnionMessageContents;
317 impl rustCustomOnionMessageContents for CustomOnionMessageContents {
318         fn tlv_type(&self) -> u64 {
319                 let mut ret = (self.tlv_type)(self.this_arg);
320                 ret
321         }
322 }
323
324 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
325 // directly as a Deref trait in higher-level structs:
326 impl core::ops::Deref for CustomOnionMessageContents {
327         type Target = Self;
328         fn deref(&self) -> &Self {
329                 self
330         }
331 }
332 impl core::ops::DerefMut for CustomOnionMessageContents {
333         fn deref_mut(&mut self) -> &mut Self {
334                 self
335         }
336 }
337 /// Calls the free function if one is set
338 #[no_mangle]
339 pub extern "C" fn CustomOnionMessageContents_free(this_ptr: CustomOnionMessageContents) { }
340 impl Drop for CustomOnionMessageContents {
341         fn drop(&mut self) {
342                 if let Some(f) = self.free {
343                         f(self.this_arg);
344                 }
345         }
346 }