Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning / blinded_path / mod.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 paths and related utilities live here.
10
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};
19
20 pub mod payment;
21 mod message {
22
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};
31
32 }
33 mod utils {
34
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};
43
44 }
45
46 use lightning::blinded_path::BlindedPath as nativeBlindedPathImport;
47 pub(crate) type nativeBlindedPath = nativeBlindedPathImport;
48
49 /// Onion messages and payments can be sent and received to blinded paths, which serve to hide the
50 /// identity of the recipient.
51 #[must_use]
52 #[repr(C)]
53 pub struct BlindedPath {
54         /// A pointer to the opaque Rust object.
55
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.
60
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.
63         pub is_owned: bool,
64 }
65
66 impl Drop for BlindedPath {
67         fn drop(&mut self) {
68                 if self.is_owned && !<*mut nativeBlindedPath>::is_null(self.inner) {
69                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
70                 }
71         }
72 }
73 /// Frees any resources used by the BlindedPath, if is_owned is set and inner is non-NULL.
74 #[no_mangle]
75 pub extern "C" fn BlindedPath_free(this_obj: BlindedPath) { }
76 #[allow(unused)]
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) };
80 }
81 #[allow(unused)]
82 impl BlindedPath {
83         pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedPath {
84                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
85         }
86         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedPath {
87                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
88         }
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();
94                 ret
95         }
96 }
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.
100 ///
101 /// [`encrypted_payload`]: BlindedHop::encrypted_payload
102 #[no_mangle]
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)
106 }
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.
110 ///
111 /// [`encrypted_payload`]: BlindedHop::encrypted_payload
112 #[no_mangle]
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();
115 }
116 /// Used by the introduction node to decrypt its [`encrypted_payload`] to forward the onion
117 /// message or payment.
118 ///
119 /// [`encrypted_payload`]: BlindedHop::encrypted_payload
120 #[no_mangle]
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)
124 }
125 /// Used by the introduction node to decrypt its [`encrypted_payload`] to forward the onion
126 /// message or payment.
127 ///
128 /// [`encrypted_payload`]: BlindedHop::encrypted_payload
129 #[no_mangle]
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();
132 }
133 /// The hops composing the blinded path.
134 #[no_mangle]
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()
139 }
140 /// The hops composing the blinded path.
141 #[no_mangle]
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;
145 }
146 /// Constructs a new BlindedPath given each field
147 #[must_use]
148 #[no_mangle]
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,
155         }), is_owned: true }
156 }
157 impl Clone for BlindedPath {
158         fn clone(&self) -> Self {
159                 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()) },
162                         is_owned: true,
163                 }
164         }
165 }
166 #[allow(unused)]
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 *mut nativeBlindedPath)).clone() })) as *mut c_void
170 }
171 #[no_mangle]
172 /// Creates a copy of the BlindedPath
173 pub extern "C" fn BlindedPath_clone(orig: &BlindedPath) -> BlindedPath {
174         orig.clone()
175 }
176 /// Generates a non-cryptographic 64-bit hash of the BlindedPath.
177 #[no_mangle]
178 pub extern "C" fn BlindedPath_hash(o: &BlindedPath) -> u64 {
179         if o.inner.is_null() { return 0; }
180         // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
181         #[allow(deprecated)]
182         let mut hasher = core::hash::SipHasher::new();
183         core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
184         core::hash::Hasher::finish(&hasher)
185 }
186 /// Checks if two BlindedPaths contain equal inner contents.
187 /// This ignores pointers and is_owned flags and looks at the values in fields.
188 /// Two objects with NULL inner values will be considered "equal" here.
189 #[no_mangle]
190 pub extern "C" fn BlindedPath_eq(a: &BlindedPath, b: &BlindedPath) -> bool {
191         if a.inner == b.inner { return true; }
192         if a.inner.is_null() || b.inner.is_null() { return false; }
193         if a.get_native_ref() == b.get_native_ref() { true } else { false }
194 }
195
196 use lightning::blinded_path::BlindedHop as nativeBlindedHopImport;
197 pub(crate) type nativeBlindedHop = nativeBlindedHopImport;
198
199 /// An encrypted payload and node id corresponding to a hop in a payment or onion message path, to
200 /// be encoded in the sender's onion packet. These hops cannot be identified by outside observers
201 /// and thus can be used to hide the identity of the recipient.
202 #[must_use]
203 #[repr(C)]
204 pub struct BlindedHop {
205         /// A pointer to the opaque Rust object.
206
207         /// Nearly everywhere, inner must be non-null, however in places where
208         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
209         pub inner: *mut nativeBlindedHop,
210         /// Indicates that this is the only struct which contains the same pointer.
211
212         /// Rust functions which take ownership of an object provided via an argument require
213         /// this to be true and invalidate the object pointed to by inner.
214         pub is_owned: bool,
215 }
216
217 impl Drop for BlindedHop {
218         fn drop(&mut self) {
219                 if self.is_owned && !<*mut nativeBlindedHop>::is_null(self.inner) {
220                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
221                 }
222         }
223 }
224 /// Frees any resources used by the BlindedHop, if is_owned is set and inner is non-NULL.
225 #[no_mangle]
226 pub extern "C" fn BlindedHop_free(this_obj: BlindedHop) { }
227 #[allow(unused)]
228 /// Used only if an object of this type is returned as a trait impl by a method
229 pub(crate) extern "C" fn BlindedHop_free_void(this_ptr: *mut c_void) {
230         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeBlindedHop) };
231 }
232 #[allow(unused)]
233 impl BlindedHop {
234         pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedHop {
235                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
236         }
237         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedHop {
238                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
239         }
240         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
241         pub(crate) fn take_inner(mut self) -> *mut nativeBlindedHop {
242                 assert!(self.is_owned);
243                 let ret = ObjOps::untweak_ptr(self.inner);
244                 self.inner = core::ptr::null_mut();
245                 ret
246         }
247 }
248 /// The blinded node id of this hop in a [`BlindedPath`].
249 #[no_mangle]
250 pub extern "C" fn BlindedHop_get_blinded_node_id(this_ptr: &BlindedHop) -> crate::c_types::PublicKey {
251         let mut inner_val = &mut this_ptr.get_native_mut_ref().blinded_node_id;
252         crate::c_types::PublicKey::from_rust(&inner_val)
253 }
254 /// The blinded node id of this hop in a [`BlindedPath`].
255 #[no_mangle]
256 pub extern "C" fn BlindedHop_set_blinded_node_id(this_ptr: &mut BlindedHop, mut val: crate::c_types::PublicKey) {
257         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.blinded_node_id = val.into_rust();
258 }
259 /// The encrypted payload intended for this hop in a [`BlindedPath`].
260 ///
261 /// Returns a copy of the field.
262 #[no_mangle]
263 pub extern "C" fn BlindedHop_get_encrypted_payload(this_ptr: &BlindedHop) -> crate::c_types::derived::CVec_u8Z {
264         let mut inner_val = this_ptr.get_native_mut_ref().encrypted_payload.clone();
265         let mut local_inner_val = Vec::new(); for mut item in inner_val.drain(..) { local_inner_val.push( { item }); };
266         local_inner_val.into()
267 }
268 /// The encrypted payload intended for this hop in a [`BlindedPath`].
269 #[no_mangle]
270 pub extern "C" fn BlindedHop_set_encrypted_payload(this_ptr: &mut BlindedHop, mut val: crate::c_types::derived::CVec_u8Z) {
271         let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item }); };
272         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.encrypted_payload = local_val;
273 }
274 /// Constructs a new BlindedHop given each field
275 #[must_use]
276 #[no_mangle]
277 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 {
278         let mut local_encrypted_payload_arg = Vec::new(); for mut item in encrypted_payload_arg.into_rust().drain(..) { local_encrypted_payload_arg.push( { item }); };
279         BlindedHop { inner: ObjOps::heap_alloc(nativeBlindedHop {
280                 blinded_node_id: blinded_node_id_arg.into_rust(),
281                 encrypted_payload: local_encrypted_payload_arg,
282         }), is_owned: true }
283 }
284 impl Clone for BlindedHop {
285         fn clone(&self) -> Self {
286                 Self {
287                         inner: if <*mut nativeBlindedHop>::is_null(self.inner) { core::ptr::null_mut() } else {
288                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
289                         is_owned: true,
290                 }
291         }
292 }
293 #[allow(unused)]
294 /// Used only if an object of this type is returned as a trait impl by a method
295 pub(crate) extern "C" fn BlindedHop_clone_void(this_ptr: *const c_void) -> *mut c_void {
296         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeBlindedHop)).clone() })) as *mut c_void
297 }
298 #[no_mangle]
299 /// Creates a copy of the BlindedHop
300 pub extern "C" fn BlindedHop_clone(orig: &BlindedHop) -> BlindedHop {
301         orig.clone()
302 }
303 /// Generates a non-cryptographic 64-bit hash of the BlindedHop.
304 #[no_mangle]
305 pub extern "C" fn BlindedHop_hash(o: &BlindedHop) -> u64 {
306         if o.inner.is_null() { return 0; }
307         // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
308         #[allow(deprecated)]
309         let mut hasher = core::hash::SipHasher::new();
310         core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
311         core::hash::Hasher::finish(&hasher)
312 }
313 /// Checks if two BlindedHops contain equal inner contents.
314 /// This ignores pointers and is_owned flags and looks at the values in fields.
315 /// Two objects with NULL inner values will be considered "equal" here.
316 #[no_mangle]
317 pub extern "C" fn BlindedHop_eq(a: &BlindedHop, b: &BlindedHop) -> bool {
318         if a.inner == b.inner { return true; }
319         if a.inner.is_null() || b.inner.is_null() { return false; }
320         if a.get_native_ref() == b.get_native_ref() { true } else { false }
321 }
322 /// Create a blinded path for an onion message, to be forwarded along `node_pks`. The last node
323 /// pubkey in `node_pks` will be the destination node.
324 ///
325 /// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
326 #[must_use]
327 #[no_mangle]
328 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 {
329         let mut local_node_pks = Vec::new(); for mut item in node_pks.into_rust().drain(..) { local_node_pks.push( { item.into_rust() }); };
330         let mut ret = lightning::blinded_path::BlindedPath::new_for_message(&local_node_pks[..], entropy_source, secp256k1::global::SECP256K1);
331         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() };
332         local_ret
333 }
334
335 /// Create a one-hop blinded path for a payment.
336 #[must_use]
337 #[no_mangle]
338 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 {
339         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);
340         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() };
341         local_ret
342 }
343
344 #[no_mangle]
345 /// Serialize the BlindedPath object into a byte array which can be read by BlindedPath_read
346 pub extern "C" fn BlindedPath_write(obj: &crate::lightning::blinded_path::BlindedPath) -> crate::c_types::derived::CVec_u8Z {
347         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
348 }
349 #[no_mangle]
350 pub(crate) extern "C" fn BlindedPath_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
351         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBlindedPath) })
352 }
353 #[no_mangle]
354 /// Read a BlindedPath from a byte array, created by BlindedPath_write
355 pub extern "C" fn BlindedPath_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BlindedPathDecodeErrorZ {
356         let res: Result<lightning::blinded_path::BlindedPath, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
357         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() };
358         local_res
359 }
360 #[no_mangle]
361 /// Serialize the BlindedHop object into a byte array which can be read by BlindedHop_read
362 pub extern "C" fn BlindedHop_write(obj: &crate::lightning::blinded_path::BlindedHop) -> crate::c_types::derived::CVec_u8Z {
363         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
364 }
365 #[no_mangle]
366 pub(crate) extern "C" fn BlindedHop_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
367         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBlindedHop) })
368 }
369 #[no_mangle]
370 /// Read a BlindedHop from a byte array, created by BlindedHop_write
371 pub extern "C" fn BlindedHop_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BlindedHopDecodeErrorZ {
372         let res: Result<lightning::blinded_path::BlindedHop, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
373         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() };
374         local_res
375 }