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};
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};
31 use lightning::blinded_path::BlindedPath as nativeBlindedPathImport;
32 pub(crate) type nativeBlindedPath = nativeBlindedPathImport;
34 /// Onion messages and payments can be sent and received to blinded paths, which serve to hide the
35 /// identity of the recipient.
38 pub struct BlindedPath {
39 /// A pointer to the opaque Rust object.
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.
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.
51 impl Drop for BlindedPath {
53 if self.is_owned && !<*mut nativeBlindedPath>::is_null(self.inner) {
54 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
58 /// Frees any resources used by the BlindedPath, if is_owned is set and inner is non-NULL.
60 pub extern "C" fn BlindedPath_free(this_obj: BlindedPath) { }
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) };
68 pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedPath {
69 unsafe { &*ObjOps::untweak_ptr(self.inner) }
71 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedPath {
72 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
82 impl Clone for BlindedPath {
83 fn clone(&self) -> 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()) },
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
97 /// Creates a copy of the BlindedPath
98 pub extern "C" fn BlindedPath_clone(orig: &BlindedPath) -> BlindedPath {
101 /// Generates a non-cryptographic 64-bit hash of the BlindedPath.
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
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)
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.
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 }
121 use lightning::blinded_path::BlindedHop as nativeBlindedHopImport;
122 pub(crate) type nativeBlindedHop = nativeBlindedHopImport;
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.
128 pub struct BlindedHop {
129 /// A pointer to the opaque Rust object.
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.
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.
141 impl Drop for BlindedHop {
143 if self.is_owned && !<*mut nativeBlindedHop>::is_null(self.inner) {
144 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
148 /// Frees any resources used by the BlindedHop, if is_owned is set and inner is non-NULL.
150 pub extern "C" fn BlindedHop_free(this_obj: BlindedHop) { }
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) };
158 pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedHop {
159 unsafe { &*ObjOps::untweak_ptr(self.inner) }
161 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedHop {
162 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
172 impl Clone for BlindedHop {
173 fn clone(&self) -> 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()) },
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
187 /// Creates a copy of the BlindedHop
188 pub extern "C" fn BlindedHop_clone(orig: &BlindedHop) -> BlindedHop {
191 /// Generates a non-cryptographic 64-bit hash of the BlindedHop.
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
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)
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.
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 }
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.
213 /// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
216 pub extern "C" fn BlindedPath_new_for_message(mut node_pks: crate::c_types::derived::CVec_PublicKeyZ, entropy_source: &crate::lightning::chain::keysinterface::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() };
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())
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) })
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() };
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())
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) })
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() };