1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
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.
9 //! Creating blinded routes and related utilities live here.
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};
20 use lightning::onion_message::blinded_route::BlindedRoute as nativeBlindedRouteImport;
21 pub(crate) type nativeBlindedRoute = nativeBlindedRouteImport;
23 /// Onion messages can be sent and received to blinded routes, which serve to hide the identity of
27 pub struct BlindedRoute {
28 /// A pointer to the opaque Rust object.
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.
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.
40 impl Drop for BlindedRoute {
42 if self.is_owned && !<*mut nativeBlindedRoute>::is_null(self.inner) {
43 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
47 /// Frees any resources used by the BlindedRoute, if is_owned is set and inner is non-NULL.
49 pub extern "C" fn BlindedRoute_free(this_obj: BlindedRoute) { }
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); }
57 pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedRoute {
58 unsafe { &*ObjOps::untweak_ptr(self.inner) }
60 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedRoute {
61 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
72 use lightning::onion_message::blinded_route::BlindedHop as nativeBlindedHopImport;
73 pub(crate) type nativeBlindedHop = nativeBlindedHopImport;
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.
79 pub struct BlindedHop {
80 /// A pointer to the opaque Rust object.
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.
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.
92 impl Drop for BlindedHop {
94 if self.is_owned && !<*mut nativeBlindedHop>::is_null(self.inner) {
95 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
99 /// Frees any resources used by the BlindedHop, if is_owned is set and inner is non-NULL.
101 pub extern "C" fn BlindedHop_free(this_obj: BlindedHop) { }
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); }
109 pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedHop {
110 unsafe { &*ObjOps::untweak_ptr(self.inner) }
112 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedHop {
113 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
123 /// Create a blinded route to be forwarded along `node_pks`. The last node pubkey in `node_pks`
124 /// will be the destination node.
126 /// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
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() };
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())
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) })
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() };
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())
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) })
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() };