X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-c-bindings;a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Fln%2Finbound_payment.rs;fp=lightning-c-bindings%2Fsrc%2Flightning%2Fln%2Finbound_payment.rs;h=339cc2bbfcbae560a1963e1c2322e05ad7f07da6;hp=0000000000000000000000000000000000000000;hb=45ad3320df3768514d968c70fc4b6a9d50028050;hpb=c014ce6a2df808ffec747f9a327c7f97bb1e3d08 diff --git a/lightning-c-bindings/src/lightning/ln/inbound_payment.rs b/lightning-c-bindings/src/lightning/ln/inbound_payment.rs new file mode 100644 index 0000000..339cc2b --- /dev/null +++ b/lightning-c-bindings/src/lightning/ln/inbound_payment.rs @@ -0,0 +1,116 @@ +// This file is Copyright its original authors, visible in version control +// history and in the source files from which this was generated. +// +// This file is licensed under the license available in the LICENSE or LICENSE.md +// file in the root of this repository or, if no such file exists, the same +// license as that which applies to the original source files from which this +// source was automatically generated. + +//! Utilities to generate inbound payment information in service of invoice creation. + +use alloc::str::FromStr; +use core::ffi::c_void; +use core::convert::Infallible; +use bitcoin::hashes::Hash; +use crate::c_types::*; +#[cfg(feature="no-std")] +use alloc::{vec::Vec, boxed::Box}; + + +use lightning::ln::inbound_payment::ExpandedKey as nativeExpandedKeyImport; +pub(crate) type nativeExpandedKey = nativeExpandedKeyImport; + +/// A set of keys that were HKDF-expanded from an initial call to +/// [`KeysInterface::get_inbound_payment_key_material`]. +/// +/// [`KeysInterface::get_inbound_payment_key_material`]: crate::chain::keysinterface::KeysInterface::get_inbound_payment_key_material +#[must_use] +#[repr(C)] +pub struct ExpandedKey { + /// A pointer to the opaque Rust object. + + /// Nearly everywhere, inner must be non-null, however in places where + /// the Rust equivalent takes an Option, it may be set to null to indicate None. + pub inner: *mut nativeExpandedKey, + /// Indicates that this is the only struct which contains the same pointer. + + /// Rust functions which take ownership of an object provided via an argument require + /// this to be true and invalidate the object pointed to by inner. + pub is_owned: bool, +} + +impl Drop for ExpandedKey { + fn drop(&mut self) { + if self.is_owned && !<*mut nativeExpandedKey>::is_null(self.inner) { + let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) }; + } + } +} +/// Frees any resources used by the ExpandedKey, if is_owned is set and inner is non-NULL. +#[no_mangle] +pub extern "C" fn ExpandedKey_free(this_obj: ExpandedKey) { } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn ExpandedKey_free_void(this_ptr: *mut c_void) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeExpandedKey); } +} +#[allow(unused)] +impl ExpandedKey { + pub(crate) fn get_native_ref(&self) -> &'static nativeExpandedKey { + unsafe { &*ObjOps::untweak_ptr(self.inner) } + } + pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeExpandedKey { + unsafe { &mut *ObjOps::untweak_ptr(self.inner) } + } + /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy + pub(crate) fn take_inner(mut self) -> *mut nativeExpandedKey { + assert!(self.is_owned); + let ret = ObjOps::untweak_ptr(self.inner); + self.inner = core::ptr::null_mut(); + ret + } +} +/// Create a new [`ExpandedKey`] for generating an inbound payment hash and secret. +/// +/// It is recommended to cache this value and not regenerate it for each new inbound payment. +#[must_use] +#[no_mangle] +pub extern "C" fn ExpandedKey_new(key_material: *const [u8; 32]) -> crate::lightning::ln::inbound_payment::ExpandedKey { + let mut ret = lightning::ln::inbound_payment::ExpandedKey::new(&::lightning::chain::keysinterface::KeyMaterial( unsafe { *key_material })); + crate::lightning::ln::inbound_payment::ExpandedKey { inner: ObjOps::heap_alloc(ret), is_owned: true } +} + +/// Equivalent to [`crate::ln::channelmanager::ChannelManager::create_inbound_payment`], but no +/// `ChannelManager` is required. Useful for generating invoices for [phantom node payments] without +/// a `ChannelManager`. +/// +/// `keys` is generated by calling [`KeysInterface::get_inbound_payment_key_material`] and then +/// calling [`ExpandedKey::new`] with its result. It is recommended to cache this value and not +/// regenerate it for each new inbound payment. +/// +/// `current_time` is a Unix timestamp representing the current time. +/// +/// [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager +#[no_mangle] +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, keys_manager: &crate::lightning::chain::keysinterface::KeysInterface, mut current_time: u64) -> crate::c_types::derived::CResult_C2Tuple_PaymentHashPaymentSecretZNoneZ { + let mut local_min_value_msat = if min_value_msat.is_some() { Some( { min_value_msat.take() }) } else { None }; + let mut ret = lightning::ln::inbound_payment::create::(keys.get_native_ref(), local_min_value_msat, invoice_expiry_delta_secs, keys_manager, current_time); + 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() }; + local_ret +} + +/// Equivalent to [`crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash`], +/// but no `ChannelManager` is required. Useful for generating invoices for [phantom node payments] +/// without a `ChannelManager`. +/// +/// See [`create`] for information on the `keys` and `current_time` parameters. +/// +/// [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager +#[no_mangle] +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) -> crate::c_types::derived::CResult_PaymentSecretNoneZ { + let mut local_min_value_msat = if min_value_msat.is_some() { Some( { min_value_msat.take() }) } else { None }; + 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); + 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() }; + local_ret +} +