Include argument info docs on all-pub struct constructors
[ldk-c-bindings] / lightning-c-bindings / src / lightning / blinded_path.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 //! Creating blinded paths and related utilities live here.
10
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};
18
19 mod utils {
20
21 use alloc::str::FromStr;
22 use core::ffi::c_void;
23 use core::convert::Infallible;
24 use bitcoin::hashes::Hash;
25 use crate::c_types::*;
26 #[cfg(feature="no-std")]
27 use alloc::{vec::Vec, boxed::Box};
28
29 }
30
31 use lightning::blinded_path::BlindedPath as nativeBlindedPathImport;
32 pub(crate) type nativeBlindedPath = nativeBlindedPathImport;
33
34 /// Onion messages and payments can be sent and received to blinded paths, which serve to hide the
35 /// identity of the recipient.
36 #[must_use]
37 #[repr(C)]
38 pub struct BlindedPath {
39         /// A pointer to the opaque Rust object.
40
41         /// Nearly everywhere, inner must be non-null, however in places where
42         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
43         pub inner: *mut nativeBlindedPath,
44         /// Indicates that this is the only struct which contains the same pointer.
45
46         /// Rust functions which take ownership of an object provided via an argument require
47         /// this to be true and invalidate the object pointed to by inner.
48         pub is_owned: bool,
49 }
50
51 impl Drop for BlindedPath {
52         fn drop(&mut self) {
53                 if self.is_owned && !<*mut nativeBlindedPath>::is_null(self.inner) {
54                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
55                 }
56         }
57 }
58 /// Frees any resources used by the BlindedPath, if is_owned is set and inner is non-NULL.
59 #[no_mangle]
60 pub extern "C" fn BlindedPath_free(this_obj: BlindedPath) { }
61 #[allow(unused)]
62 /// Used only if an object of this type is returned as a trait impl by a method
63 pub(crate) extern "C" fn BlindedPath_free_void(this_ptr: *mut c_void) {
64         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeBlindedPath) };
65 }
66 #[allow(unused)]
67 impl BlindedPath {
68         pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedPath {
69                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
70         }
71         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedPath {
72                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
73         }
74         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
75         pub(crate) fn take_inner(mut self) -> *mut nativeBlindedPath {
76                 assert!(self.is_owned);
77                 let ret = ObjOps::untweak_ptr(self.inner);
78                 self.inner = core::ptr::null_mut();
79                 ret
80         }
81 }
82 impl Clone for BlindedPath {
83         fn clone(&self) -> Self {
84                 Self {
85                         inner: if <*mut nativeBlindedPath>::is_null(self.inner) { core::ptr::null_mut() } else {
86                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
87                         is_owned: true,
88                 }
89         }
90 }
91 #[allow(unused)]
92 /// Used only if an object of this type is returned as a trait impl by a method
93 pub(crate) extern "C" fn BlindedPath_clone_void(this_ptr: *const c_void) -> *mut c_void {
94         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeBlindedPath)).clone() })) as *mut c_void
95 }
96 #[no_mangle]
97 /// Creates a copy of the BlindedPath
98 pub extern "C" fn BlindedPath_clone(orig: &BlindedPath) -> BlindedPath {
99         orig.clone()
100 }
101 /// Generates a non-cryptographic 64-bit hash of the BlindedPath.
102 #[no_mangle]
103 pub extern "C" fn BlindedPath_hash(o: &BlindedPath) -> u64 {
104         if o.inner.is_null() { return 0; }
105         // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
106         #[allow(deprecated)]
107         let mut hasher = core::hash::SipHasher::new();
108         core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
109         core::hash::Hasher::finish(&hasher)
110 }
111 /// Checks if two BlindedPaths contain equal inner contents.
112 /// This ignores pointers and is_owned flags and looks at the values in fields.
113 /// Two objects with NULL inner values will be considered "equal" here.
114 #[no_mangle]
115 pub extern "C" fn BlindedPath_eq(a: &BlindedPath, b: &BlindedPath) -> bool {
116         if a.inner == b.inner { return true; }
117         if a.inner.is_null() || b.inner.is_null() { return false; }
118         if a.get_native_ref() == b.get_native_ref() { true } else { false }
119 }
120
121 use lightning::blinded_path::BlindedHop as nativeBlindedHopImport;
122 pub(crate) type nativeBlindedHop = nativeBlindedHopImport;
123
124 /// Used to construct the blinded hops portion of a blinded path. These hops cannot be identified
125 /// by outside observers and thus can be used to hide the identity of the recipient.
126 #[must_use]
127 #[repr(C)]
128 pub struct BlindedHop {
129         /// A pointer to the opaque Rust object.
130
131         /// Nearly everywhere, inner must be non-null, however in places where
132         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
133         pub inner: *mut nativeBlindedHop,
134         /// Indicates that this is the only struct which contains the same pointer.
135
136         /// Rust functions which take ownership of an object provided via an argument require
137         /// this to be true and invalidate the object pointed to by inner.
138         pub is_owned: bool,
139 }
140
141 impl Drop for BlindedHop {
142         fn drop(&mut self) {
143                 if self.is_owned && !<*mut nativeBlindedHop>::is_null(self.inner) {
144                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
145                 }
146         }
147 }
148 /// Frees any resources used by the BlindedHop, if is_owned is set and inner is non-NULL.
149 #[no_mangle]
150 pub extern "C" fn BlindedHop_free(this_obj: BlindedHop) { }
151 #[allow(unused)]
152 /// Used only if an object of this type is returned as a trait impl by a method
153 pub(crate) extern "C" fn BlindedHop_free_void(this_ptr: *mut c_void) {
154         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeBlindedHop) };
155 }
156 #[allow(unused)]
157 impl BlindedHop {
158         pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedHop {
159                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
160         }
161         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedHop {
162                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
163         }
164         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
165         pub(crate) fn take_inner(mut self) -> *mut nativeBlindedHop {
166                 assert!(self.is_owned);
167                 let ret = ObjOps::untweak_ptr(self.inner);
168                 self.inner = core::ptr::null_mut();
169                 ret
170         }
171 }
172 impl Clone for BlindedHop {
173         fn clone(&self) -> Self {
174                 Self {
175                         inner: if <*mut nativeBlindedHop>::is_null(self.inner) { core::ptr::null_mut() } else {
176                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
177                         is_owned: true,
178                 }
179         }
180 }
181 #[allow(unused)]
182 /// Used only if an object of this type is returned as a trait impl by a method
183 pub(crate) extern "C" fn BlindedHop_clone_void(this_ptr: *const c_void) -> *mut c_void {
184         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeBlindedHop)).clone() })) as *mut c_void
185 }
186 #[no_mangle]
187 /// Creates a copy of the BlindedHop
188 pub extern "C" fn BlindedHop_clone(orig: &BlindedHop) -> BlindedHop {
189         orig.clone()
190 }
191 /// Generates a non-cryptographic 64-bit hash of the BlindedHop.
192 #[no_mangle]
193 pub extern "C" fn BlindedHop_hash(o: &BlindedHop) -> u64 {
194         if o.inner.is_null() { return 0; }
195         // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
196         #[allow(deprecated)]
197         let mut hasher = core::hash::SipHasher::new();
198         core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
199         core::hash::Hasher::finish(&hasher)
200 }
201 /// Checks if two BlindedHops contain equal inner contents.
202 /// This ignores pointers and is_owned flags and looks at the values in fields.
203 /// Two objects with NULL inner values will be considered "equal" here.
204 #[no_mangle]
205 pub extern "C" fn BlindedHop_eq(a: &BlindedHop, b: &BlindedHop) -> bool {
206         if a.inner == b.inner { return true; }
207         if a.inner.is_null() || b.inner.is_null() { return false; }
208         if a.get_native_ref() == b.get_native_ref() { true } else { false }
209 }
210 /// Create a blinded path for an onion message, to be forwarded along `node_pks`. The last node
211 /// pubkey in `node_pks` will be the destination node.
212 ///
213 /// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
214 #[must_use]
215 #[no_mangle]
216 pub extern "C" fn BlindedPath_new_for_message(mut node_pks: crate::c_types::derived::CVec_PublicKeyZ, entropy_source: &crate::lightning::sign::EntropySource) -> crate::c_types::derived::CResult_BlindedPathNoneZ {
217         let mut local_node_pks = Vec::new(); for mut item in node_pks.into_rust().drain(..) { local_node_pks.push( { item.into_rust() }); };
218         let mut ret = lightning::blinded_path::BlindedPath::new_for_message(&local_node_pks[..], entropy_source, secp256k1::global::SECP256K1);
219         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
220         local_ret
221 }
222
223 #[no_mangle]
224 /// Serialize the BlindedPath object into a byte array which can be read by BlindedPath_read
225 pub extern "C" fn BlindedPath_write(obj: &crate::lightning::blinded_path::BlindedPath) -> crate::c_types::derived::CVec_u8Z {
226         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
227 }
228 #[no_mangle]
229 pub(crate) extern "C" fn BlindedPath_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
230         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBlindedPath) })
231 }
232 #[no_mangle]
233 /// Read a BlindedPath from a byte array, created by BlindedPath_write
234 pub extern "C" fn BlindedPath_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BlindedPathDecodeErrorZ {
235         let res: Result<lightning::blinded_path::BlindedPath, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
236         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::blinded_path::BlindedPath { 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() };
237         local_res
238 }
239 #[no_mangle]
240 /// Serialize the BlindedHop object into a byte array which can be read by BlindedHop_read
241 pub extern "C" fn BlindedHop_write(obj: &crate::lightning::blinded_path::BlindedHop) -> crate::c_types::derived::CVec_u8Z {
242         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
243 }
244 #[no_mangle]
245 pub(crate) extern "C" fn BlindedHop_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
246         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBlindedHop) })
247 }
248 #[no_mangle]
249 /// Read a BlindedHop from a byte array, created by BlindedHop_write
250 pub extern "C" fn BlindedHop_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BlindedHopDecodeErrorZ {
251         let res: Result<lightning::blinded_path::BlindedHop, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
252         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::blinded_path::BlindedHop { 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() };
253         local_res
254 }