Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning / onion_message / 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
20 use lightning::onion_message::blinded_path::BlindedPath as nativeBlindedPathImport;
21 pub(crate) type nativeBlindedPath = nativeBlindedPathImport;
22
23 /// Onion messages can be sent and received to blinded paths, which serve to hide the identity of
24 /// the recipient.
25 #[must_use]
26 #[repr(C)]
27 pub struct BlindedPath {
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 nativeBlindedPath,
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 BlindedPath {
41         fn drop(&mut self) {
42                 if self.is_owned && !<*mut nativeBlindedPath>::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 BlindedPath, if is_owned is set and inner is non-NULL.
48 #[no_mangle]
49 pub extern "C" fn BlindedPath_free(this_obj: BlindedPath) { }
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 BlindedPath_free_void(this_ptr: *mut c_void) {
53         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeBlindedPath) };
54 }
55 #[allow(unused)]
56 impl BlindedPath {
57         pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedPath {
58                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
59         }
60         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedPath {
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 nativeBlindedPath {
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 impl Clone for BlindedPath {
72         fn clone(&self) -> Self {
73                 Self {
74                         inner: if <*mut nativeBlindedPath>::is_null(self.inner) { core::ptr::null_mut() } else {
75                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
76                         is_owned: true,
77                 }
78         }
79 }
80 #[allow(unused)]
81 /// Used only if an object of this type is returned as a trait impl by a method
82 pub(crate) extern "C" fn BlindedPath_clone_void(this_ptr: *const c_void) -> *mut c_void {
83         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeBlindedPath)).clone() })) as *mut c_void
84 }
85 #[no_mangle]
86 /// Creates a copy of the BlindedPath
87 pub extern "C" fn BlindedPath_clone(orig: &BlindedPath) -> BlindedPath {
88         orig.clone()
89 }
90
91 use lightning::onion_message::blinded_path::BlindedHop as nativeBlindedHopImport;
92 pub(crate) type nativeBlindedHop = nativeBlindedHopImport;
93
94 /// Used to construct the blinded hops portion of a blinded path. These hops cannot be identified
95 /// by outside observers and thus can be used to hide the identity of the recipient.
96 #[must_use]
97 #[repr(C)]
98 pub struct BlindedHop {
99         /// A pointer to the opaque Rust object.
100
101         /// Nearly everywhere, inner must be non-null, however in places where
102         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
103         pub inner: *mut nativeBlindedHop,
104         /// Indicates that this is the only struct which contains the same pointer.
105
106         /// Rust functions which take ownership of an object provided via an argument require
107         /// this to be true and invalidate the object pointed to by inner.
108         pub is_owned: bool,
109 }
110
111 impl Drop for BlindedHop {
112         fn drop(&mut self) {
113                 if self.is_owned && !<*mut nativeBlindedHop>::is_null(self.inner) {
114                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
115                 }
116         }
117 }
118 /// Frees any resources used by the BlindedHop, if is_owned is set and inner is non-NULL.
119 #[no_mangle]
120 pub extern "C" fn BlindedHop_free(this_obj: BlindedHop) { }
121 #[allow(unused)]
122 /// Used only if an object of this type is returned as a trait impl by a method
123 pub(crate) extern "C" fn BlindedHop_free_void(this_ptr: *mut c_void) {
124         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeBlindedHop) };
125 }
126 #[allow(unused)]
127 impl BlindedHop {
128         pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedHop {
129                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
130         }
131         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedHop {
132                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
133         }
134         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
135         pub(crate) fn take_inner(mut self) -> *mut nativeBlindedHop {
136                 assert!(self.is_owned);
137                 let ret = ObjOps::untweak_ptr(self.inner);
138                 self.inner = core::ptr::null_mut();
139                 ret
140         }
141 }
142 impl Clone for BlindedHop {
143         fn clone(&self) -> Self {
144                 Self {
145                         inner: if <*mut nativeBlindedHop>::is_null(self.inner) { core::ptr::null_mut() } else {
146                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
147                         is_owned: true,
148                 }
149         }
150 }
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_clone_void(this_ptr: *const c_void) -> *mut c_void {
154         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeBlindedHop)).clone() })) as *mut c_void
155 }
156 #[no_mangle]
157 /// Creates a copy of the BlindedHop
158 pub extern "C" fn BlindedHop_clone(orig: &BlindedHop) -> BlindedHop {
159         orig.clone()
160 }
161 /// Create a blinded path to be forwarded along `node_pks`. The last node pubkey in `node_pks`
162 /// will be the destination node.
163 ///
164 /// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
165 #[must_use]
166 #[no_mangle]
167 pub extern "C" fn BlindedPath_new(mut node_pks: crate::c_types::derived::CVec_PublicKeyZ, entropy_source: &crate::lightning::chain::keysinterface::EntropySource) -> crate::c_types::derived::CResult_BlindedPathNoneZ {
168         let mut local_node_pks = Vec::new(); for mut item in node_pks.into_rust().drain(..) { local_node_pks.push( { item.into_rust() }); };
169         let mut ret = lightning::onion_message::blinded_path::BlindedPath::new(&local_node_pks[..], entropy_source, secp256k1::global::SECP256K1);
170         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::onion_message::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
171         local_ret
172 }
173
174 #[no_mangle]
175 /// Serialize the BlindedPath object into a byte array which can be read by BlindedPath_read
176 pub extern "C" fn BlindedPath_write(obj: &crate::lightning::onion_message::blinded_path::BlindedPath) -> crate::c_types::derived::CVec_u8Z {
177         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
178 }
179 #[no_mangle]
180 pub(crate) extern "C" fn BlindedPath_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
181         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBlindedPath) })
182 }
183 #[no_mangle]
184 /// Read a BlindedPath from a byte array, created by BlindedPath_write
185 pub extern "C" fn BlindedPath_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BlindedPathDecodeErrorZ {
186         let res: Result<lightning::onion_message::blinded_path::BlindedPath, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
187         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::onion_message::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() };
188         local_res
189 }
190 #[no_mangle]
191 /// Serialize the BlindedHop object into a byte array which can be read by BlindedHop_read
192 pub extern "C" fn BlindedHop_write(obj: &crate::lightning::onion_message::blinded_path::BlindedHop) -> crate::c_types::derived::CVec_u8Z {
193         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
194 }
195 #[no_mangle]
196 pub(crate) extern "C" fn BlindedHop_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
197         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBlindedHop) })
198 }
199 #[no_mangle]
200 /// Read a BlindedHop from a byte array, created by BlindedHop_write
201 pub extern "C" fn BlindedHop_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BlindedHopDecodeErrorZ {
202         let res: Result<lightning::onion_message::blinded_path::BlindedHop, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
203         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::onion_message::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() };
204         local_res
205 }