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 paths 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_path::BlindedPath as nativeBlindedPathImport;
21 pub(crate) type nativeBlindedPath = nativeBlindedPathImport;
23 /// Onion messages can be sent and received to blinded paths, which serve to hide the identity of
27 pub struct BlindedPath {
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 nativeBlindedPath,
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 BlindedPath {
42 if self.is_owned && !<*mut nativeBlindedPath>::is_null(self.inner) {
43 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
47 /// Frees any resources used by the BlindedPath, if is_owned is set and inner is non-NULL.
49 pub extern "C" fn BlindedPath_free(this_obj: BlindedPath) { }
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) };
57 pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedPath {
58 unsafe { &*ObjOps::untweak_ptr(self.inner) }
60 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedPath {
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 nativeBlindedPath {
65 assert!(self.is_owned);
66 let ret = ObjOps::untweak_ptr(self.inner);
67 self.inner = core::ptr::null_mut();
71 impl Clone for BlindedPath {
72 fn clone(&self) -> 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()) },
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
86 /// Creates a copy of the BlindedPath
87 pub extern "C" fn BlindedPath_clone(orig: &BlindedPath) -> BlindedPath {
91 use lightning::onion_message::blinded_path::BlindedHop as nativeBlindedHopImport;
92 pub(crate) type nativeBlindedHop = nativeBlindedHopImport;
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.
98 pub struct BlindedHop {
99 /// A pointer to the opaque Rust object.
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.
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.
111 impl Drop for BlindedHop {
113 if self.is_owned && !<*mut nativeBlindedHop>::is_null(self.inner) {
114 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
118 /// Frees any resources used by the BlindedHop, if is_owned is set and inner is non-NULL.
120 pub extern "C" fn BlindedHop_free(this_obj: BlindedHop) { }
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) };
128 pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedHop {
129 unsafe { &*ObjOps::untweak_ptr(self.inner) }
131 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedHop {
132 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
142 impl Clone for BlindedHop {
143 fn clone(&self) -> 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()) },
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
157 /// Creates a copy of the BlindedHop
158 pub extern "C" fn BlindedHop_clone(orig: &BlindedHop) -> BlindedHop {
161 /// Create a blinded path to be forwarded along `node_pks`. The last node pubkey in `node_pks`
162 /// will be the destination node.
164 /// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
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() };
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())
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) })
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() };
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())
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) })
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() };