193a22c63941138d725e80cf9ea464b494429f9f
[ldk-c-bindings] / lightning-c-bindings / src / lightning / util / ser.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 //! A very simple serialization framework which is used to serialize/deserialize messages as well
10 //! as [`ChannelManager`]s and [`ChannelMonitor`]s.
11 //!
12 //! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
13 //! [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
14
15 use alloc::str::FromStr;
16 use core::ffi::c_void;
17 use core::convert::Infallible;
18 use bitcoin::hashes::Hash;
19 use crate::c_types::*;
20 #[cfg(feature="no-std")]
21 use alloc::{vec::Vec, boxed::Box};
22
23 /// serialization buffer size
24
25 #[no_mangle]
26 pub static MAX_BUF_SIZE: usize = lightning::util::ser::MAX_BUF_SIZE;
27
28 use lightning::util::ser::BigSize as nativeBigSizeImport;
29 pub(crate) type nativeBigSize = nativeBigSizeImport;
30
31 /// Lightning TLV uses a custom variable-length integer called `BigSize`. It is similar to Bitcoin's
32 /// variable-length integers except that it is serialized in big-endian instead of little-endian.
33 ///
34 /// Like Bitcoin's variable-length integer, it exhibits ambiguity in that certain values can be
35 /// encoded in several different ways, which we must check for at deserialization-time. Thus, if
36 /// you're looking for an example of a variable-length integer to use for your own project, move
37 /// along, this is a rather poor design.
38 #[must_use]
39 #[repr(C)]
40 pub struct BigSize {
41         /// A pointer to the opaque Rust object.
42
43         /// Nearly everywhere, inner must be non-null, however in places where
44         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
45         pub inner: *mut nativeBigSize,
46         /// Indicates that this is the only struct which contains the same pointer.
47
48         /// Rust functions which take ownership of an object provided via an argument require
49         /// this to be true and invalidate the object pointed to by inner.
50         pub is_owned: bool,
51 }
52
53 impl Drop for BigSize {
54         fn drop(&mut self) {
55                 if self.is_owned && !<*mut nativeBigSize>::is_null(self.inner) {
56                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
57                 }
58         }
59 }
60 /// Frees any resources used by the BigSize, if is_owned is set and inner is non-NULL.
61 #[no_mangle]
62 pub extern "C" fn BigSize_free(this_obj: BigSize) { }
63 #[allow(unused)]
64 /// Used only if an object of this type is returned as a trait impl by a method
65 pub(crate) extern "C" fn BigSize_free_void(this_ptr: *mut c_void) {
66         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeBigSize) };
67 }
68 #[allow(unused)]
69 impl BigSize {
70         pub(crate) fn get_native_ref(&self) -> &'static nativeBigSize {
71                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
72         }
73         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBigSize {
74                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
75         }
76         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
77         pub(crate) fn take_inner(mut self) -> *mut nativeBigSize {
78                 assert!(self.is_owned);
79                 let ret = ObjOps::untweak_ptr(self.inner);
80                 self.inner = core::ptr::null_mut();
81                 ret
82         }
83 }
84 #[no_mangle]
85 pub extern "C" fn BigSize_get_a(this_ptr: &BigSize) -> u64 {
86         let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
87         *inner_val
88 }
89 #[no_mangle]
90 pub extern "C" fn BigSize_set_a(this_ptr: &mut BigSize, mut val: u64) {
91         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val;
92 }
93 /// Constructs a new BigSize given each field
94 #[must_use]
95 #[no_mangle]
96 pub extern "C" fn BigSize_new(mut a_arg: u64) -> BigSize {
97         BigSize { inner: ObjOps::heap_alloc(lightning::util::ser::BigSize (
98                 a_arg,
99         )), is_owned: true }
100 }
101 impl Clone for BigSize {
102         fn clone(&self) -> Self {
103                 Self {
104                         inner: if <*mut nativeBigSize>::is_null(self.inner) { core::ptr::null_mut() } else {
105                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
106                         is_owned: true,
107                 }
108         }
109 }
110 #[allow(unused)]
111 /// Used only if an object of this type is returned as a trait impl by a method
112 pub(crate) extern "C" fn BigSize_clone_void(this_ptr: *const c_void) -> *mut c_void {
113         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeBigSize)).clone() })) as *mut c_void
114 }
115 #[no_mangle]
116 /// Creates a copy of the BigSize
117 pub extern "C" fn BigSize_clone(orig: &BigSize) -> BigSize {
118         orig.clone()
119 }
120 /// Generates a non-cryptographic 64-bit hash of the BigSize.
121 #[no_mangle]
122 pub extern "C" fn BigSize_hash(o: &BigSize) -> u64 {
123         if o.inner.is_null() { return 0; }
124         // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
125         #[allow(deprecated)]
126         let mut hasher = core::hash::SipHasher::new();
127         core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
128         core::hash::Hasher::finish(&hasher)
129 }
130 /// Checks if two BigSizes contain equal inner contents.
131 /// This ignores pointers and is_owned flags and looks at the values in fields.
132 /// Two objects with NULL inner values will be considered "equal" here.
133 #[no_mangle]
134 pub extern "C" fn BigSize_eq(a: &BigSize, b: &BigSize) -> bool {
135         if a.inner == b.inner { return true; }
136         if a.inner.is_null() || b.inner.is_null() { return false; }
137         if a.get_native_ref() == b.get_native_ref() { true } else { false }
138 }
139 #[no_mangle]
140 /// Serialize the BigSize object into a byte array which can be read by BigSize_read
141 pub extern "C" fn BigSize_write(obj: &crate::lightning::util::ser::BigSize) -> crate::c_types::derived::CVec_u8Z {
142         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
143 }
144 #[no_mangle]
145 pub(crate) extern "C" fn BigSize_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
146         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBigSize) })
147 }
148 #[no_mangle]
149 /// Read a BigSize from a byte array, created by BigSize_write
150 pub extern "C" fn BigSize_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BigSizeDecodeErrorZ {
151         let res: Result<lightning::util::ser::BigSize, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
152         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::util::ser::BigSize { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError::native_into(e) }).into() };
153         local_res
154 }
155
156 use lightning::util::ser::Hostname as nativeHostnameImport;
157 pub(crate) type nativeHostname = nativeHostnameImport;
158
159 /// Represents a hostname for serialization purposes.
160 /// Only the character set and length will be validated.
161 /// The character set consists of ASCII alphanumeric characters, hyphens, and periods.
162 /// Its length is guaranteed to be representable by a single byte.
163 /// This serialization is used by [`BOLT 7`] hostnames.
164 ///
165 /// [`BOLT 7`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md
166 #[must_use]
167 #[repr(C)]
168 pub struct Hostname {
169         /// A pointer to the opaque Rust object.
170
171         /// Nearly everywhere, inner must be non-null, however in places where
172         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
173         pub inner: *mut nativeHostname,
174         /// Indicates that this is the only struct which contains the same pointer.
175
176         /// Rust functions which take ownership of an object provided via an argument require
177         /// this to be true and invalidate the object pointed to by inner.
178         pub is_owned: bool,
179 }
180
181 impl Drop for Hostname {
182         fn drop(&mut self) {
183                 if self.is_owned && !<*mut nativeHostname>::is_null(self.inner) {
184                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
185                 }
186         }
187 }
188 /// Frees any resources used by the Hostname, if is_owned is set and inner is non-NULL.
189 #[no_mangle]
190 pub extern "C" fn Hostname_free(this_obj: Hostname) { }
191 #[allow(unused)]
192 /// Used only if an object of this type is returned as a trait impl by a method
193 pub(crate) extern "C" fn Hostname_free_void(this_ptr: *mut c_void) {
194         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeHostname) };
195 }
196 #[allow(unused)]
197 impl Hostname {
198         pub(crate) fn get_native_ref(&self) -> &'static nativeHostname {
199                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
200         }
201         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeHostname {
202                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
203         }
204         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
205         pub(crate) fn take_inner(mut self) -> *mut nativeHostname {
206                 assert!(self.is_owned);
207                 let ret = ObjOps::untweak_ptr(self.inner);
208                 self.inner = core::ptr::null_mut();
209                 ret
210         }
211 }
212 impl Clone for Hostname {
213         fn clone(&self) -> Self {
214                 Self {
215                         inner: if <*mut nativeHostname>::is_null(self.inner) { core::ptr::null_mut() } else {
216                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
217                         is_owned: true,
218                 }
219         }
220 }
221 #[allow(unused)]
222 /// Used only if an object of this type is returned as a trait impl by a method
223 pub(crate) extern "C" fn Hostname_clone_void(this_ptr: *const c_void) -> *mut c_void {
224         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHostname)).clone() })) as *mut c_void
225 }
226 #[no_mangle]
227 /// Creates a copy of the Hostname
228 pub extern "C" fn Hostname_clone(orig: &Hostname) -> Hostname {
229         orig.clone()
230 }
231 /// Checks if two Hostnames contain equal inner contents.
232 /// This ignores pointers and is_owned flags and looks at the values in fields.
233 /// Two objects with NULL inner values will be considered "equal" here.
234 #[no_mangle]
235 pub extern "C" fn Hostname_eq(a: &Hostname, b: &Hostname) -> bool {
236         if a.inner == b.inner { return true; }
237         if a.inner.is_null() || b.inner.is_null() { return false; }
238         if a.get_native_ref() == b.get_native_ref() { true } else { false }
239 }
240 /// Returns the length of the hostname.
241 #[must_use]
242 #[no_mangle]
243 pub extern "C" fn Hostname_len(this_arg: &crate::lightning::util::ser::Hostname) -> u8 {
244         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.len();
245         ret
246 }
247
248 #[no_mangle]
249 /// Serialize the Hostname object into a byte array which can be read by Hostname_read
250 pub extern "C" fn Hostname_write(obj: &crate::lightning::util::ser::Hostname) -> crate::c_types::derived::CVec_u8Z {
251         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
252 }
253 #[no_mangle]
254 pub(crate) extern "C" fn Hostname_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
255         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeHostname) })
256 }
257 #[no_mangle]
258 /// Read a Hostname from a byte array, created by Hostname_write
259 pub extern "C" fn Hostname_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_HostnameDecodeErrorZ {
260         let res: Result<lightning::util::ser::Hostname, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
261         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::util::ser::Hostname { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError::native_into(e) }).into() };
262         local_res
263 }
264
265 use lightning::util::ser::TransactionU16LenLimited as nativeTransactionU16LenLimitedImport;
266 pub(crate) type nativeTransactionU16LenLimited = nativeTransactionU16LenLimitedImport;
267
268 /// A wrapper for a `Transaction` which can only be constructed with [`TransactionU16LenLimited::new`]
269 /// if the `Transaction`'s consensus-serialized length is <= u16::MAX.
270 ///
271 /// Use [`TransactionU16LenLimited::into_transaction`] to convert into the contained `Transaction`.
272 #[must_use]
273 #[repr(C)]
274 pub struct TransactionU16LenLimited {
275         /// A pointer to the opaque Rust object.
276
277         /// Nearly everywhere, inner must be non-null, however in places where
278         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
279         pub inner: *mut nativeTransactionU16LenLimited,
280         /// Indicates that this is the only struct which contains the same pointer.
281
282         /// Rust functions which take ownership of an object provided via an argument require
283         /// this to be true and invalidate the object pointed to by inner.
284         pub is_owned: bool,
285 }
286
287 impl Drop for TransactionU16LenLimited {
288         fn drop(&mut self) {
289                 if self.is_owned && !<*mut nativeTransactionU16LenLimited>::is_null(self.inner) {
290                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
291                 }
292         }
293 }
294 /// Frees any resources used by the TransactionU16LenLimited, if is_owned is set and inner is non-NULL.
295 #[no_mangle]
296 pub extern "C" fn TransactionU16LenLimited_free(this_obj: TransactionU16LenLimited) { }
297 #[allow(unused)]
298 /// Used only if an object of this type is returned as a trait impl by a method
299 pub(crate) extern "C" fn TransactionU16LenLimited_free_void(this_ptr: *mut c_void) {
300         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeTransactionU16LenLimited) };
301 }
302 #[allow(unused)]
303 impl TransactionU16LenLimited {
304         pub(crate) fn get_native_ref(&self) -> &'static nativeTransactionU16LenLimited {
305                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
306         }
307         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeTransactionU16LenLimited {
308                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
309         }
310         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
311         pub(crate) fn take_inner(mut self) -> *mut nativeTransactionU16LenLimited {
312                 assert!(self.is_owned);
313                 let ret = ObjOps::untweak_ptr(self.inner);
314                 self.inner = core::ptr::null_mut();
315                 ret
316         }
317 }
318 impl Clone for TransactionU16LenLimited {
319         fn clone(&self) -> Self {
320                 Self {
321                         inner: if <*mut nativeTransactionU16LenLimited>::is_null(self.inner) { core::ptr::null_mut() } else {
322                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
323                         is_owned: true,
324                 }
325         }
326 }
327 #[allow(unused)]
328 /// Used only if an object of this type is returned as a trait impl by a method
329 pub(crate) extern "C" fn TransactionU16LenLimited_clone_void(this_ptr: *const c_void) -> *mut c_void {
330         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeTransactionU16LenLimited)).clone() })) as *mut c_void
331 }
332 #[no_mangle]
333 /// Creates a copy of the TransactionU16LenLimited
334 pub extern "C" fn TransactionU16LenLimited_clone(orig: &TransactionU16LenLimited) -> TransactionU16LenLimited {
335         orig.clone()
336 }
337 /// Checks if two TransactionU16LenLimiteds contain equal inner contents.
338 /// This ignores pointers and is_owned flags and looks at the values in fields.
339 /// Two objects with NULL inner values will be considered "equal" here.
340 #[no_mangle]
341 pub extern "C" fn TransactionU16LenLimited_eq(a: &TransactionU16LenLimited, b: &TransactionU16LenLimited) -> bool {
342         if a.inner == b.inner { return true; }
343         if a.inner.is_null() || b.inner.is_null() { return false; }
344         if a.get_native_ref() == b.get_native_ref() { true } else { false }
345 }
346 /// Constructs a new `TransactionU16LenLimited` from a `Transaction` only if it's consensus-
347 /// serialized length is <= u16::MAX.
348 #[must_use]
349 #[no_mangle]
350 pub extern "C" fn TransactionU16LenLimited_new(mut transaction: crate::c_types::Transaction) -> crate::c_types::derived::CResult_TransactionU16LenLimitedNoneZ {
351         let mut ret = lightning::util::ser::TransactionU16LenLimited::new(transaction.into_bitcoin());
352         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::util::ser::TransactionU16LenLimited { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
353         local_ret
354 }
355
356 /// Consumes this `TransactionU16LenLimited` and returns its contained `Transaction`.
357 #[must_use]
358 #[no_mangle]
359 pub extern "C" fn TransactionU16LenLimited_into_transaction(mut this_arg: crate::lightning::util::ser::TransactionU16LenLimited) -> crate::c_types::Transaction {
360         let mut ret = (*unsafe { Box::from_raw(this_arg.take_inner()) }).into_transaction();
361         crate::c_types::Transaction::from_bitcoin(&ret)
362 }
363
364 #[no_mangle]
365 /// Serialize the TransactionU16LenLimited object into a byte array which can be read by TransactionU16LenLimited_read
366 pub extern "C" fn TransactionU16LenLimited_write(obj: &crate::lightning::util::ser::TransactionU16LenLimited) -> crate::c_types::derived::CVec_u8Z {
367         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
368 }
369 #[no_mangle]
370 pub(crate) extern "C" fn TransactionU16LenLimited_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
371         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeTransactionU16LenLimited) })
372 }
373 #[no_mangle]
374 /// Read a TransactionU16LenLimited from a byte array, created by TransactionU16LenLimited_write
375 pub extern "C" fn TransactionU16LenLimited_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_TransactionU16LenLimitedDecodeErrorZ {
376         let res: Result<lightning::util::ser::TransactionU16LenLimited, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
377         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::util::ser::TransactionU16LenLimited { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError::native_into(e) }).into() };
378         local_res
379 }