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 alloc::string::String;
13 use core::ffi::c_void;
14 use core::convert::Infallible;
15 use bitcoin::hashes::Hash;
16 use crate::c_types::*;
17 #[cfg(feature="no-std")]
18 use alloc::{vec::Vec, boxed::Box};
23 use alloc::str::FromStr;
24 use alloc::string::String;
25 use core::ffi::c_void;
26 use core::convert::Infallible;
27 use bitcoin::hashes::Hash;
28 use crate::c_types::*;
29 #[cfg(feature="no-std")]
30 use alloc::{vec::Vec, boxed::Box};
35 use alloc::str::FromStr;
36 use alloc::string::String;
37 use core::ffi::c_void;
38 use core::convert::Infallible;
39 use bitcoin::hashes::Hash;
40 use crate::c_types::*;
41 #[cfg(feature="no-std")]
42 use alloc::{vec::Vec, boxed::Box};
46 use lightning::blinded_path::BlindedPath as nativeBlindedPathImport;
47 pub(crate) type nativeBlindedPath = nativeBlindedPathImport;
49 /// Onion messages and payments can be sent and received to blinded paths, which serve to hide the
50 /// identity of the recipient.
53 pub struct BlindedPath {
54 /// A pointer to the opaque Rust object.
56 /// Nearly everywhere, inner must be non-null, however in places where
57 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
58 pub inner: *mut nativeBlindedPath,
59 /// Indicates that this is the only struct which contains the same pointer.
61 /// Rust functions which take ownership of an object provided via an argument require
62 /// this to be true and invalidate the object pointed to by inner.
66 impl Drop for BlindedPath {
68 if self.is_owned && !<*mut nativeBlindedPath>::is_null(self.inner) {
69 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
73 /// Frees any resources used by the BlindedPath, if is_owned is set and inner is non-NULL.
75 pub extern "C" fn BlindedPath_free(this_obj: BlindedPath) { }
77 /// Used only if an object of this type is returned as a trait impl by a method
78 pub(crate) extern "C" fn BlindedPath_free_void(this_ptr: *mut c_void) {
79 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeBlindedPath) };
83 pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedPath {
84 unsafe { &*ObjOps::untweak_ptr(self.inner) }
86 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedPath {
87 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
89 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
90 pub(crate) fn take_inner(mut self) -> *mut nativeBlindedPath {
91 assert!(self.is_owned);
92 let ret = ObjOps::untweak_ptr(self.inner);
93 self.inner = core::ptr::null_mut();
97 /// To send to a blinded path, the sender first finds a route to the unblinded
98 /// `introduction_node_id`, which can unblind its [`encrypted_payload`] to find out the onion
99 /// message or payment's next hop and forward it along.
101 /// [`encrypted_payload`]: BlindedHop::encrypted_payload
103 pub extern "C" fn BlindedPath_get_introduction_node_id(this_ptr: &BlindedPath) -> crate::c_types::PublicKey {
104 let mut inner_val = &mut this_ptr.get_native_mut_ref().introduction_node_id;
105 crate::c_types::PublicKey::from_rust(&inner_val)
107 /// To send to a blinded path, the sender first finds a route to the unblinded
108 /// `introduction_node_id`, which can unblind its [`encrypted_payload`] to find out the onion
109 /// message or payment's next hop and forward it along.
111 /// [`encrypted_payload`]: BlindedHop::encrypted_payload
113 pub extern "C" fn BlindedPath_set_introduction_node_id(this_ptr: &mut BlindedPath, mut val: crate::c_types::PublicKey) {
114 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.introduction_node_id = val.into_rust();
116 /// Used by the introduction node to decrypt its [`encrypted_payload`] to forward the onion
117 /// message or payment.
119 /// [`encrypted_payload`]: BlindedHop::encrypted_payload
121 pub extern "C" fn BlindedPath_get_blinding_point(this_ptr: &BlindedPath) -> crate::c_types::PublicKey {
122 let mut inner_val = &mut this_ptr.get_native_mut_ref().blinding_point;
123 crate::c_types::PublicKey::from_rust(&inner_val)
125 /// Used by the introduction node to decrypt its [`encrypted_payload`] to forward the onion
126 /// message or payment.
128 /// [`encrypted_payload`]: BlindedHop::encrypted_payload
130 pub extern "C" fn BlindedPath_set_blinding_point(this_ptr: &mut BlindedPath, mut val: crate::c_types::PublicKey) {
131 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.blinding_point = val.into_rust();
133 /// The hops composing the blinded path.
135 pub extern "C" fn BlindedPath_get_blinded_hops(this_ptr: &BlindedPath) -> crate::c_types::derived::CVec_BlindedHopZ {
136 let mut inner_val = &mut this_ptr.get_native_mut_ref().blinded_hops;
137 let mut local_inner_val = Vec::new(); for item in inner_val.iter() { local_inner_val.push( { crate::lightning::blinded_path::BlindedHop { inner: unsafe { ObjOps::nonnull_ptr_to_inner((item as *const lightning::blinded_path::BlindedHop<>) as *mut _) }, is_owned: false } }); };
138 local_inner_val.into()
140 /// The hops composing the blinded path.
142 pub extern "C" fn BlindedPath_set_blinded_hops(this_ptr: &mut BlindedPath, mut val: crate::c_types::derived::CVec_BlindedHopZ) {
143 let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
144 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.blinded_hops = local_val;
146 /// Constructs a new BlindedPath given each field
149 pub extern "C" fn BlindedPath_new(mut introduction_node_id_arg: crate::c_types::PublicKey, mut blinding_point_arg: crate::c_types::PublicKey, mut blinded_hops_arg: crate::c_types::derived::CVec_BlindedHopZ) -> BlindedPath {
150 let mut local_blinded_hops_arg = Vec::new(); for mut item in blinded_hops_arg.into_rust().drain(..) { local_blinded_hops_arg.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
151 BlindedPath { inner: ObjOps::heap_alloc(nativeBlindedPath {
152 introduction_node_id: introduction_node_id_arg.into_rust(),
153 blinding_point: blinding_point_arg.into_rust(),
154 blinded_hops: local_blinded_hops_arg,
157 impl Clone for BlindedPath {
158 fn clone(&self) -> Self {
160 inner: if <*mut nativeBlindedPath>::is_null(self.inner) { core::ptr::null_mut() } else {
161 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
167 /// Used only if an object of this type is returned as a trait impl by a method
168 pub(crate) extern "C" fn BlindedPath_clone_void(this_ptr: *const c_void) -> *mut c_void {
169 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeBlindedPath)).clone() })) as *mut c_void
172 /// Creates a copy of the BlindedPath
173 pub extern "C" fn BlindedPath_clone(orig: &BlindedPath) -> BlindedPath {
176 /// Get a string which allows debug introspection of a BlindedPath object
177 pub extern "C" fn BlindedPath_debug_str_void(o: *const c_void) -> Str {
178 alloc::format!("{:?}", unsafe { o as *const crate::lightning::blinded_path::BlindedPath }).into()}
179 /// Generates a non-cryptographic 64-bit hash of the BlindedPath.
181 pub extern "C" fn BlindedPath_hash(o: &BlindedPath) -> u64 {
182 if o.inner.is_null() { return 0; }
183 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
185 let mut hasher = core::hash::SipHasher::new();
186 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
187 core::hash::Hasher::finish(&hasher)
189 /// Checks if two BlindedPaths contain equal inner contents.
190 /// This ignores pointers and is_owned flags and looks at the values in fields.
191 /// Two objects with NULL inner values will be considered "equal" here.
193 pub extern "C" fn BlindedPath_eq(a: &BlindedPath, b: &BlindedPath) -> bool {
194 if a.inner == b.inner { return true; }
195 if a.inner.is_null() || b.inner.is_null() { return false; }
196 if a.get_native_ref() == b.get_native_ref() { true } else { false }
199 use lightning::blinded_path::BlindedHop as nativeBlindedHopImport;
200 pub(crate) type nativeBlindedHop = nativeBlindedHopImport;
202 /// An encrypted payload and node id corresponding to a hop in a payment or onion message path, to
203 /// be encoded in the sender's onion packet. These hops cannot be identified by outside observers
204 /// and thus can be used to hide the identity of the recipient.
207 pub struct BlindedHop {
208 /// A pointer to the opaque Rust object.
210 /// Nearly everywhere, inner must be non-null, however in places where
211 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
212 pub inner: *mut nativeBlindedHop,
213 /// Indicates that this is the only struct which contains the same pointer.
215 /// Rust functions which take ownership of an object provided via an argument require
216 /// this to be true and invalidate the object pointed to by inner.
220 impl Drop for BlindedHop {
222 if self.is_owned && !<*mut nativeBlindedHop>::is_null(self.inner) {
223 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
227 /// Frees any resources used by the BlindedHop, if is_owned is set and inner is non-NULL.
229 pub extern "C" fn BlindedHop_free(this_obj: BlindedHop) { }
231 /// Used only if an object of this type is returned as a trait impl by a method
232 pub(crate) extern "C" fn BlindedHop_free_void(this_ptr: *mut c_void) {
233 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeBlindedHop) };
237 pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedHop {
238 unsafe { &*ObjOps::untweak_ptr(self.inner) }
240 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedHop {
241 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
243 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
244 pub(crate) fn take_inner(mut self) -> *mut nativeBlindedHop {
245 assert!(self.is_owned);
246 let ret = ObjOps::untweak_ptr(self.inner);
247 self.inner = core::ptr::null_mut();
251 /// The blinded node id of this hop in a [`BlindedPath`].
253 pub extern "C" fn BlindedHop_get_blinded_node_id(this_ptr: &BlindedHop) -> crate::c_types::PublicKey {
254 let mut inner_val = &mut this_ptr.get_native_mut_ref().blinded_node_id;
255 crate::c_types::PublicKey::from_rust(&inner_val)
257 /// The blinded node id of this hop in a [`BlindedPath`].
259 pub extern "C" fn BlindedHop_set_blinded_node_id(this_ptr: &mut BlindedHop, mut val: crate::c_types::PublicKey) {
260 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.blinded_node_id = val.into_rust();
262 /// The encrypted payload intended for this hop in a [`BlindedPath`].
264 /// Returns a copy of the field.
266 pub extern "C" fn BlindedHop_get_encrypted_payload(this_ptr: &BlindedHop) -> crate::c_types::derived::CVec_u8Z {
267 let mut inner_val = this_ptr.get_native_mut_ref().encrypted_payload.clone();
268 let mut local_inner_val = Vec::new(); for mut item in inner_val.drain(..) { local_inner_val.push( { item }); };
269 local_inner_val.into()
271 /// The encrypted payload intended for this hop in a [`BlindedPath`].
273 pub extern "C" fn BlindedHop_set_encrypted_payload(this_ptr: &mut BlindedHop, mut val: crate::c_types::derived::CVec_u8Z) {
274 let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item }); };
275 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.encrypted_payload = local_val;
277 /// Constructs a new BlindedHop given each field
280 pub extern "C" fn BlindedHop_new(mut blinded_node_id_arg: crate::c_types::PublicKey, mut encrypted_payload_arg: crate::c_types::derived::CVec_u8Z) -> BlindedHop {
281 let mut local_encrypted_payload_arg = Vec::new(); for mut item in encrypted_payload_arg.into_rust().drain(..) { local_encrypted_payload_arg.push( { item }); };
282 BlindedHop { inner: ObjOps::heap_alloc(nativeBlindedHop {
283 blinded_node_id: blinded_node_id_arg.into_rust(),
284 encrypted_payload: local_encrypted_payload_arg,
287 impl Clone for BlindedHop {
288 fn clone(&self) -> Self {
290 inner: if <*mut nativeBlindedHop>::is_null(self.inner) { core::ptr::null_mut() } else {
291 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
297 /// Used only if an object of this type is returned as a trait impl by a method
298 pub(crate) extern "C" fn BlindedHop_clone_void(this_ptr: *const c_void) -> *mut c_void {
299 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeBlindedHop)).clone() })) as *mut c_void
302 /// Creates a copy of the BlindedHop
303 pub extern "C" fn BlindedHop_clone(orig: &BlindedHop) -> BlindedHop {
306 /// Get a string which allows debug introspection of a BlindedHop object
307 pub extern "C" fn BlindedHop_debug_str_void(o: *const c_void) -> Str {
308 alloc::format!("{:?}", unsafe { o as *const crate::lightning::blinded_path::BlindedHop }).into()}
309 /// Generates a non-cryptographic 64-bit hash of the BlindedHop.
311 pub extern "C" fn BlindedHop_hash(o: &BlindedHop) -> u64 {
312 if o.inner.is_null() { return 0; }
313 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
315 let mut hasher = core::hash::SipHasher::new();
316 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
317 core::hash::Hasher::finish(&hasher)
319 /// Checks if two BlindedHops contain equal inner contents.
320 /// This ignores pointers and is_owned flags and looks at the values in fields.
321 /// Two objects with NULL inner values will be considered "equal" here.
323 pub extern "C" fn BlindedHop_eq(a: &BlindedHop, b: &BlindedHop) -> bool {
324 if a.inner == b.inner { return true; }
325 if a.inner.is_null() || b.inner.is_null() { return false; }
326 if a.get_native_ref() == b.get_native_ref() { true } else { false }
328 /// Create a one-hop blinded path for a message.
331 pub extern "C" fn BlindedPath_one_hop_for_message(mut recipient_node_id: crate::c_types::PublicKey, entropy_source: &crate::lightning::sign::EntropySource) -> crate::c_types::derived::CResult_BlindedPathNoneZ {
332 let mut ret = lightning::blinded_path::BlindedPath::one_hop_for_message(recipient_node_id.into_rust(), entropy_source, secp256k1::global::SECP256K1);
333 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() };
337 /// Create a blinded path for an onion message, to be forwarded along `node_pks`. The last node
338 /// pubkey in `node_pks` will be the destination node.
340 /// Errors if no hops are provided or if `node_pk`(s) are invalid.
343 pub extern "C" fn BlindedPath_new_for_message(mut node_pks: crate::c_types::derived::CVec_PublicKeyZ, entropy_source: &crate::lightning::sign::EntropySource) -> crate::c_types::derived::CResult_BlindedPathNoneZ {
344 let mut local_node_pks = Vec::new(); for mut item in node_pks.into_rust().drain(..) { local_node_pks.push( { item.into_rust() }); };
345 let mut ret = lightning::blinded_path::BlindedPath::new_for_message(&local_node_pks[..], entropy_source, secp256k1::global::SECP256K1);
346 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() };
350 /// Create a one-hop blinded path for a payment.
353 pub extern "C" fn BlindedPath_one_hop_for_payment(mut payee_node_id: crate::c_types::PublicKey, mut payee_tlvs: crate::lightning::blinded_path::payment::ReceiveTlvs, entropy_source: &crate::lightning::sign::EntropySource) -> crate::c_types::derived::CResult_C2Tuple_BlindedPayInfoBlindedPathZNoneZ {
354 let mut ret = lightning::blinded_path::BlindedPath::one_hop_for_payment(payee_node_id.into_rust(), *unsafe { Box::from_raw(payee_tlvs.take_inner()) }, entropy_source, secp256k1::global::SECP256K1);
355 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let (mut orig_ret_0_0, mut orig_ret_0_1) = o; let mut local_ret_0 = (crate::lightning::offers::invoice::BlindedPayInfo { inner: ObjOps::heap_alloc(orig_ret_0_0), is_owned: true }, crate::lightning::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(orig_ret_0_1), is_owned: true }).into(); local_ret_0 }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
359 /// Create a blinded path for a payment, to be forwarded along `intermediate_nodes`.
362 /// * a provided node id is invalid
363 /// * [`BlindedPayInfo`] calculation results in an integer overflow
364 /// * any unknown features are required in the provided [`ForwardTlvs`]
366 /// [`ForwardTlvs`]: crate::blinded_path::payment::ForwardTlvs
369 pub extern "C" fn BlindedPath_new_for_payment(mut intermediate_nodes: crate::c_types::derived::CVec_ForwardNodeZ, mut payee_node_id: crate::c_types::PublicKey, mut payee_tlvs: crate::lightning::blinded_path::payment::ReceiveTlvs, mut htlc_maximum_msat: u64, entropy_source: &crate::lightning::sign::EntropySource) -> crate::c_types::derived::CResult_C2Tuple_BlindedPayInfoBlindedPathZNoneZ {
370 let mut local_intermediate_nodes = Vec::new(); for mut item in intermediate_nodes.into_rust().drain(..) { local_intermediate_nodes.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
371 let mut ret = lightning::blinded_path::BlindedPath::new_for_payment(local_intermediate_nodes, payee_node_id.into_rust(), *unsafe { Box::from_raw(payee_tlvs.take_inner()) }, htlc_maximum_msat, entropy_source, secp256k1::global::SECP256K1);
372 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let (mut orig_ret_0_0, mut orig_ret_0_1) = o; let mut local_ret_0 = (crate::lightning::offers::invoice::BlindedPayInfo { inner: ObjOps::heap_alloc(orig_ret_0_0), is_owned: true }, crate::lightning::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(orig_ret_0_1), is_owned: true }).into(); local_ret_0 }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
377 /// Serialize the BlindedPath object into a byte array which can be read by BlindedPath_read
378 pub extern "C" fn BlindedPath_write(obj: &crate::lightning::blinded_path::BlindedPath) -> crate::c_types::derived::CVec_u8Z {
379 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
382 pub(crate) extern "C" fn BlindedPath_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
383 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBlindedPath) })
386 /// Read a BlindedPath from a byte array, created by BlindedPath_write
387 pub extern "C" fn BlindedPath_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BlindedPathDecodeErrorZ {
388 let res: Result<lightning::blinded_path::BlindedPath, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
389 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() };
393 /// Serialize the BlindedHop object into a byte array which can be read by BlindedHop_read
394 pub extern "C" fn BlindedHop_write(obj: &crate::lightning::blinded_path::BlindedHop) -> crate::c_types::derived::CVec_u8Z {
395 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
398 pub(crate) extern "C" fn BlindedHop_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
399 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBlindedHop) })
402 /// Read a BlindedHop from a byte array, created by BlindedHop_write
403 pub extern "C" fn BlindedHop_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BlindedHopDecodeErrorZ {
404 let res: Result<lightning::blinded_path::BlindedHop, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
405 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() };