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 //! Utilities to decode payment onions and do contextless validation of incoming payments.
11 //! Primarily features [`peel_payment_onion`], which allows the decoding of an onion statelessly
12 //! and can be used to predict whether we'd accept a payment.
14 use alloc::str::FromStr;
15 use alloc::string::String;
16 use core::ffi::c_void;
17 use core::convert::Infallible;
18 use bitcoin::hashes::Hash;
19 use crate::c_types::*;
20 #[cfg(feature="no-std")]
21 use alloc::{vec::Vec, boxed::Box};
24 use lightning::ln::onion_payment::InboundHTLCErr as nativeInboundHTLCErrImport;
25 pub(crate) type nativeInboundHTLCErr = nativeInboundHTLCErrImport;
27 /// Invalid inbound onion payment.
30 pub struct InboundHTLCErr {
31 /// A pointer to the opaque Rust object.
33 /// Nearly everywhere, inner must be non-null, however in places where
34 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
35 pub inner: *mut nativeInboundHTLCErr,
36 /// Indicates that this is the only struct which contains the same pointer.
38 /// Rust functions which take ownership of an object provided via an argument require
39 /// this to be true and invalidate the object pointed to by inner.
43 impl Drop for InboundHTLCErr {
45 if self.is_owned && !<*mut nativeInboundHTLCErr>::is_null(self.inner) {
46 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
50 /// Frees any resources used by the InboundHTLCErr, if is_owned is set and inner is non-NULL.
52 pub extern "C" fn InboundHTLCErr_free(this_obj: InboundHTLCErr) { }
54 /// Used only if an object of this type is returned as a trait impl by a method
55 pub(crate) extern "C" fn InboundHTLCErr_free_void(this_ptr: *mut c_void) {
56 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeInboundHTLCErr) };
60 pub(crate) fn get_native_ref(&self) -> &'static nativeInboundHTLCErr {
61 unsafe { &*ObjOps::untweak_ptr(self.inner) }
63 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeInboundHTLCErr {
64 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
66 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
67 pub(crate) fn take_inner(mut self) -> *mut nativeInboundHTLCErr {
68 assert!(self.is_owned);
69 let ret = ObjOps::untweak_ptr(self.inner);
70 self.inner = core::ptr::null_mut();
74 /// BOLT 4 error code.
76 pub extern "C" fn InboundHTLCErr_get_err_code(this_ptr: &InboundHTLCErr) -> u16 {
77 let mut inner_val = &mut this_ptr.get_native_mut_ref().err_code;
80 /// BOLT 4 error code.
82 pub extern "C" fn InboundHTLCErr_set_err_code(this_ptr: &mut InboundHTLCErr, mut val: u16) {
83 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.err_code = val;
85 /// Data attached to this error.
87 /// Returns a copy of the field.
89 pub extern "C" fn InboundHTLCErr_get_err_data(this_ptr: &InboundHTLCErr) -> crate::c_types::derived::CVec_u8Z {
90 let mut inner_val = this_ptr.get_native_mut_ref().err_data.clone();
91 let mut local_inner_val = Vec::new(); for mut item in inner_val.drain(..) { local_inner_val.push( { item }); };
92 local_inner_val.into()
94 /// Data attached to this error.
96 pub extern "C" fn InboundHTLCErr_set_err_data(this_ptr: &mut InboundHTLCErr, mut val: crate::c_types::derived::CVec_u8Z) {
97 let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item }); };
98 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.err_data = local_val;
100 /// Error message text.
102 pub extern "C" fn InboundHTLCErr_get_msg(this_ptr: &InboundHTLCErr) -> crate::c_types::Str {
103 let mut inner_val = &mut this_ptr.get_native_mut_ref().msg;
106 /// Error message text.
108 pub extern "C" fn InboundHTLCErr_set_msg(this_ptr: &mut InboundHTLCErr, mut val: crate::c_types::Str) {
109 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.msg = val.into_str();
111 /// Constructs a new InboundHTLCErr given each field
114 pub extern "C" fn InboundHTLCErr_new(mut err_code_arg: u16, mut err_data_arg: crate::c_types::derived::CVec_u8Z, mut msg_arg: crate::c_types::Str) -> InboundHTLCErr {
115 let mut local_err_data_arg = Vec::new(); for mut item in err_data_arg.into_rust().drain(..) { local_err_data_arg.push( { item }); };
116 InboundHTLCErr { inner: ObjOps::heap_alloc(nativeInboundHTLCErr {
117 err_code: err_code_arg,
118 err_data: local_err_data_arg,
119 msg: msg_arg.into_str(),
122 /// Get a string which allows debug introspection of a InboundHTLCErr object
123 pub extern "C" fn InboundHTLCErr_debug_str_void(o: *const c_void) -> Str {
124 alloc::format!("{:?}", unsafe { o as *const crate::lightning::ln::onion_payment::InboundHTLCErr }).into()}
125 /// Peel one layer off an incoming onion, returning a [`PendingHTLCInfo`] that contains information
126 /// about the intended next-hop for the HTLC.
128 /// This does all the relevant context-free checks that LDK requires for payment relay or
129 /// acceptance. If the payment is to be received, and the amount matches the expected amount for
130 /// a given invoice, this indicates the [`msgs::UpdateAddHTLC`], once fully committed in the
131 /// channel, will generate an [`Event::PaymentClaimable`].
133 /// [`Event::PaymentClaimable`]: crate::events::Event::PaymentClaimable
135 pub extern "C" fn peel_payment_onion(msg: &crate::lightning::ln::msgs::UpdateAddHTLC, node_signer: &crate::lightning::sign::NodeSigner, logger: &crate::lightning::util::logger::Logger, mut cur_height: u32, mut accept_mpp_keysend: bool, mut allow_skimmed_fees: bool) -> crate::c_types::derived::CResult_PendingHTLCInfoInboundHTLCErrZ {
136 let mut ret = lightning::ln::onion_payment::peel_payment_onion(msg.get_native_ref(), node_signer, logger, secp256k1::global::SECP256K1, cur_height, accept_mpp_keysend, allow_skimmed_fees);
137 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::channelmanager::PendingHTLCInfo { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::onion_payment::InboundHTLCErr { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };