Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning / ln / inbound_payment.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 //! Utilities to generate inbound payment information in service of invoice creation.
10
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};
18
19
20 use lightning::ln::inbound_payment::ExpandedKey as nativeExpandedKeyImport;
21 pub(crate) type nativeExpandedKey = nativeExpandedKeyImport;
22
23 /// A set of keys that were HKDF-expanded from an initial call to
24 /// [`NodeSigner::get_inbound_payment_key_material`].
25 ///
26 /// [`NodeSigner::get_inbound_payment_key_material`]: crate::chain::keysinterface::NodeSigner::get_inbound_payment_key_material
27 #[must_use]
28 #[repr(C)]
29 pub struct ExpandedKey {
30         /// A pointer to the opaque Rust object.
31
32         /// Nearly everywhere, inner must be non-null, however in places where
33         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
34         pub inner: *mut nativeExpandedKey,
35         /// Indicates that this is the only struct which contains the same pointer.
36
37         /// Rust functions which take ownership of an object provided via an argument require
38         /// this to be true and invalidate the object pointed to by inner.
39         pub is_owned: bool,
40 }
41
42 impl Drop for ExpandedKey {
43         fn drop(&mut self) {
44                 if self.is_owned && !<*mut nativeExpandedKey>::is_null(self.inner) {
45                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
46                 }
47         }
48 }
49 /// Frees any resources used by the ExpandedKey, if is_owned is set and inner is non-NULL.
50 #[no_mangle]
51 pub extern "C" fn ExpandedKey_free(this_obj: ExpandedKey) { }
52 #[allow(unused)]
53 /// Used only if an object of this type is returned as a trait impl by a method
54 pub(crate) extern "C" fn ExpandedKey_free_void(this_ptr: *mut c_void) {
55         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeExpandedKey) };
56 }
57 #[allow(unused)]
58 impl ExpandedKey {
59         pub(crate) fn get_native_ref(&self) -> &'static nativeExpandedKey {
60                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
61         }
62         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeExpandedKey {
63                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
64         }
65         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
66         pub(crate) fn take_inner(mut self) -> *mut nativeExpandedKey {
67                 assert!(self.is_owned);
68                 let ret = ObjOps::untweak_ptr(self.inner);
69                 self.inner = core::ptr::null_mut();
70                 ret
71         }
72 }
73 /// Create a  new [`ExpandedKey`] for generating an inbound payment hash and secret.
74 ///
75 /// It is recommended to cache this value and not regenerate it for each new inbound payment.
76 #[must_use]
77 #[no_mangle]
78 pub extern "C" fn ExpandedKey_new(key_material: *const [u8; 32]) -> crate::lightning::ln::inbound_payment::ExpandedKey {
79         let mut ret = lightning::ln::inbound_payment::ExpandedKey::new(&::lightning::chain::keysinterface::KeyMaterial( unsafe { *key_material }));
80         crate::lightning::ln::inbound_payment::ExpandedKey { inner: ObjOps::heap_alloc(ret), is_owned: true }
81 }
82
83 /// Equivalent to [`crate::ln::channelmanager::ChannelManager::create_inbound_payment`], but no
84 /// `ChannelManager` is required. Useful for generating invoices for [phantom node payments] without
85 /// a `ChannelManager`.
86 ///
87 /// `keys` is generated by calling [`NodeSigner::get_inbound_payment_key_material`] and then
88 /// calling [`ExpandedKey::new`] with its result. It is recommended to cache this value and not
89 /// regenerate it for each new inbound payment.
90 ///
91 /// `current_time` is a Unix timestamp representing the current time.
92 ///
93 /// Note that if `min_final_cltv_expiry_delta` is set to some value, then the payment will not be receivable
94 /// on versions of LDK prior to 0.0.114.
95 ///
96 /// [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager
97 /// [`NodeSigner::get_inbound_payment_key_material`]: crate::chain::keysinterface::NodeSigner::get_inbound_payment_key_material
98 #[no_mangle]
99 pub extern "C" fn create(keys: &crate::lightning::ln::inbound_payment::ExpandedKey, mut min_value_msat: crate::c_types::derived::COption_u64Z, mut invoice_expiry_delta_secs: u32, entropy_source: &crate::lightning::chain::keysinterface::EntropySource, mut current_time: u64, mut min_final_cltv_expiry_delta: crate::c_types::derived::COption_u16Z) -> crate::c_types::derived::CResult_C2Tuple_PaymentHashPaymentSecretZNoneZ {
100         let mut local_min_value_msat = if min_value_msat.is_some() { Some( { min_value_msat.take() }) } else { None };
101         let mut local_min_final_cltv_expiry_delta = if min_final_cltv_expiry_delta.is_some() { Some( { min_final_cltv_expiry_delta.take() }) } else { None };
102         let mut ret = lightning::ln::inbound_payment::create::<crate::lightning::chain::keysinterface::EntropySource>(keys.get_native_ref(), local_min_value_msat, invoice_expiry_delta_secs, entropy_source, current_time, local_min_final_cltv_expiry_delta);
103         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::c_types::ThirtyTwoBytes { data: orig_ret_0_0.0 }, crate::c_types::ThirtyTwoBytes { data: orig_ret_0_1.0 }).into(); local_ret_0 }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
104         local_ret
105 }
106
107 /// Equivalent to [`crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash`],
108 /// but no `ChannelManager` is required. Useful for generating invoices for [phantom node payments]
109 /// without a `ChannelManager`.
110 ///
111 /// See [`create`] for information on the `keys` and `current_time` parameters.
112 ///
113 /// Note that if `min_final_cltv_expiry_delta` is set to some value, then the payment will not be receivable
114 /// on versions of LDK prior to 0.0.114.
115 ///
116 /// [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager
117 #[no_mangle]
118 pub extern "C" fn create_from_hash(keys: &crate::lightning::ln::inbound_payment::ExpandedKey, mut min_value_msat: crate::c_types::derived::COption_u64Z, mut payment_hash: crate::c_types::ThirtyTwoBytes, mut invoice_expiry_delta_secs: u32, mut current_time: u64, mut min_final_cltv_expiry_delta: crate::c_types::derived::COption_u16Z) -> crate::c_types::derived::CResult_PaymentSecretNoneZ {
119         let mut local_min_value_msat = if min_value_msat.is_some() { Some( { min_value_msat.take() }) } else { None };
120         let mut local_min_final_cltv_expiry_delta = if min_final_cltv_expiry_delta.is_some() { Some( { min_final_cltv_expiry_delta.take() }) } else { None };
121         let mut ret = lightning::ln::inbound_payment::create_from_hash(keys.get_native_ref(), local_min_value_msat, ::lightning::ln::PaymentHash(payment_hash.data), invoice_expiry_delta_secs, current_time, local_min_final_cltv_expiry_delta);
122         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::ThirtyTwoBytes { data: o.0 } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
123         local_ret
124 }
125