2fd7649dd42ca1436fceb04b4ae863a49364f5d0
[ldk-c-bindings] / lightning-c-bindings / src / lightning / onion_message / blinded_route.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 routes 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_route::BlindedRoute as nativeBlindedRouteImport;
21 pub(crate) type nativeBlindedRoute = nativeBlindedRouteImport;
22
23 /// Onion messages can be sent and received to blinded routes, which serve to hide the identity of
24 /// the recipient.
25 #[must_use]
26 #[repr(C)]
27 pub struct BlindedRoute {
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 nativeBlindedRoute,
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 BlindedRoute {
41         fn drop(&mut self) {
42                 if self.is_owned && !<*mut nativeBlindedRoute>::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 BlindedRoute, if is_owned is set and inner is non-NULL.
48 #[no_mangle]
49 pub extern "C" fn BlindedRoute_free(this_obj: BlindedRoute) { }
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 BlindedRoute_free_void(this_ptr: *mut c_void) {
53         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeBlindedRoute); }
54 }
55 #[allow(unused)]
56 impl BlindedRoute {
57         pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedRoute {
58                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
59         }
60         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedRoute {
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 nativeBlindedRoute {
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
72 use lightning::onion_message::blinded_route::BlindedHop as nativeBlindedHopImport;
73 pub(crate) type nativeBlindedHop = nativeBlindedHopImport;
74
75 /// Used to construct the blinded hops portion of a blinded route. These hops cannot be identified
76 /// by outside observers and thus can be used to hide the identity of the recipient.
77 #[must_use]
78 #[repr(C)]
79 pub struct BlindedHop {
80         /// A pointer to the opaque Rust object.
81
82         /// Nearly everywhere, inner must be non-null, however in places where
83         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
84         pub inner: *mut nativeBlindedHop,
85         /// Indicates that this is the only struct which contains the same pointer.
86
87         /// Rust functions which take ownership of an object provided via an argument require
88         /// this to be true and invalidate the object pointed to by inner.
89         pub is_owned: bool,
90 }
91
92 impl Drop for BlindedHop {
93         fn drop(&mut self) {
94                 if self.is_owned && !<*mut nativeBlindedHop>::is_null(self.inner) {
95                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
96                 }
97         }
98 }
99 /// Frees any resources used by the BlindedHop, if is_owned is set and inner is non-NULL.
100 #[no_mangle]
101 pub extern "C" fn BlindedHop_free(this_obj: BlindedHop) { }
102 #[allow(unused)]
103 /// Used only if an object of this type is returned as a trait impl by a method
104 pub(crate) extern "C" fn BlindedHop_free_void(this_ptr: *mut c_void) {
105         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeBlindedHop); }
106 }
107 #[allow(unused)]
108 impl BlindedHop {
109         pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedHop {
110                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
111         }
112         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedHop {
113                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
114         }
115         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
116         pub(crate) fn take_inner(mut self) -> *mut nativeBlindedHop {
117                 assert!(self.is_owned);
118                 let ret = ObjOps::untweak_ptr(self.inner);
119                 self.inner = core::ptr::null_mut();
120                 ret
121         }
122 }
123 /// Create a blinded route to be forwarded along `node_pks`. The last node pubkey in `node_pks`
124 /// will be the destination node.
125 ///
126 /// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
127 #[must_use]
128 #[no_mangle]
129 pub extern "C" fn BlindedRoute_new(mut node_pks: crate::c_types::derived::CVec_PublicKeyZ, keys_manager: &crate::lightning::chain::keysinterface::KeysInterface) -> crate::c_types::derived::CResult_BlindedRouteNoneZ {
130         let mut local_node_pks = Vec::new(); for mut item in node_pks.into_rust().drain(..) { local_node_pks.push( { item.into_rust() }); };
131         let mut ret = lightning::onion_message::blinded_route::BlindedRoute::new(&local_node_pks[..], keys_manager, secp256k1::global::SECP256K1);
132         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::onion_message::blinded_route::BlindedRoute { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
133         local_ret
134 }
135
136 #[no_mangle]
137 /// Serialize the BlindedRoute object into a byte array which can be read by BlindedRoute_read
138 pub extern "C" fn BlindedRoute_write(obj: &crate::lightning::onion_message::blinded_route::BlindedRoute) -> crate::c_types::derived::CVec_u8Z {
139         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
140 }
141 #[no_mangle]
142 pub(crate) extern "C" fn BlindedRoute_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
143         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBlindedRoute) })
144 }
145 #[no_mangle]
146 /// Read a BlindedRoute from a byte array, created by BlindedRoute_write
147 pub extern "C" fn BlindedRoute_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BlindedRouteDecodeErrorZ {
148         let res: Result<lightning::onion_message::blinded_route::BlindedRoute, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
149         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::onion_message::blinded_route::BlindedRoute { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
150         local_res
151 }
152 #[no_mangle]
153 /// Serialize the BlindedHop object into a byte array which can be read by BlindedHop_read
154 pub extern "C" fn BlindedHop_write(obj: &crate::lightning::onion_message::blinded_route::BlindedHop) -> crate::c_types::derived::CVec_u8Z {
155         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
156 }
157 #[no_mangle]
158 pub(crate) extern "C" fn BlindedHop_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
159         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBlindedHop) })
160 }
161 #[no_mangle]
162 /// Read a BlindedHop from a byte array, created by BlindedHop_write
163 pub extern "C" fn BlindedHop_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BlindedHopDecodeErrorZ {
164         let res: Result<lightning::onion_message::blinded_route::BlindedHop, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
165         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::onion_message::blinded_route::BlindedHop { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
166         local_res
167 }