Pin compiler_builtins to 0.1.109 when building std
[ldk-c-bindings] / lightning-c-bindings / src / lightning / ln / onion_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 decode payment onions and do contextless validation of incoming payments.
10 //!
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.
13
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};
22
23
24 use lightning::ln::onion_payment::InboundHTLCErr as nativeInboundHTLCErrImport;
25 pub(crate) type nativeInboundHTLCErr = nativeInboundHTLCErrImport;
26
27 /// Invalid inbound onion payment.
28 #[must_use]
29 #[repr(C)]
30 pub struct InboundHTLCErr {
31         /// A pointer to the opaque Rust object.
32
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.
37
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.
40         pub is_owned: bool,
41 }
42
43 impl Drop for InboundHTLCErr {
44         fn drop(&mut self) {
45                 if self.is_owned && !<*mut nativeInboundHTLCErr>::is_null(self.inner) {
46                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
47                 }
48         }
49 }
50 /// Frees any resources used by the InboundHTLCErr, if is_owned is set and inner is non-NULL.
51 #[no_mangle]
52 pub extern "C" fn InboundHTLCErr_free(this_obj: InboundHTLCErr) { }
53 #[allow(unused)]
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) };
57 }
58 #[allow(unused)]
59 impl InboundHTLCErr {
60         pub(crate) fn get_native_ref(&self) -> &'static nativeInboundHTLCErr {
61                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
62         }
63         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeInboundHTLCErr {
64                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
65         }
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();
71                 ret
72         }
73 }
74 /// BOLT 4 error code.
75 #[no_mangle]
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;
78         *inner_val
79 }
80 /// BOLT 4 error code.
81 #[no_mangle]
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;
84 }
85 /// Data attached to this error.
86 ///
87 /// Returns a copy of the field.
88 #[no_mangle]
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()
93 }
94 /// Data attached to this error.
95 #[no_mangle]
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;
99 }
100 /// Error message text.
101 #[no_mangle]
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;
104         inner_val.into()
105 }
106 /// Error message text.
107 #[no_mangle]
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();
110 }
111 /// Constructs a new InboundHTLCErr given each field
112 #[must_use]
113 #[no_mangle]
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(),
120         }), is_owned: true }
121 }
122 impl Clone for InboundHTLCErr {
123         fn clone(&self) -> Self {
124                 Self {
125                         inner: if <*mut nativeInboundHTLCErr>::is_null(self.inner) { core::ptr::null_mut() } else {
126                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
127                         is_owned: true,
128                 }
129         }
130 }
131 #[allow(unused)]
132 /// Used only if an object of this type is returned as a trait impl by a method
133 pub(crate) extern "C" fn InboundHTLCErr_clone_void(this_ptr: *const c_void) -> *mut c_void {
134         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeInboundHTLCErr)).clone() })) as *mut c_void
135 }
136 #[no_mangle]
137 /// Creates a copy of the InboundHTLCErr
138 pub extern "C" fn InboundHTLCErr_clone(orig: &InboundHTLCErr) -> InboundHTLCErr {
139         orig.clone()
140 }
141 /// Get a string which allows debug introspection of a InboundHTLCErr object
142 pub extern "C" fn InboundHTLCErr_debug_str_void(o: *const c_void) -> Str {
143         alloc::format!("{:?}", unsafe { o as *const crate::lightning::ln::onion_payment::InboundHTLCErr }).into()}
144 /// Generates a non-cryptographic 64-bit hash of the InboundHTLCErr.
145 #[no_mangle]
146 pub extern "C" fn InboundHTLCErr_hash(o: &InboundHTLCErr) -> u64 {
147         if o.inner.is_null() { return 0; }
148         // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
149         #[allow(deprecated)]
150         let mut hasher = core::hash::SipHasher::new();
151         core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
152         core::hash::Hasher::finish(&hasher)
153 }
154 /// Checks if two InboundHTLCErrs contain equal inner contents.
155 /// This ignores pointers and is_owned flags and looks at the values in fields.
156 /// Two objects with NULL inner values will be considered "equal" here.
157 #[no_mangle]
158 pub extern "C" fn InboundHTLCErr_eq(a: &InboundHTLCErr, b: &InboundHTLCErr) -> bool {
159         if a.inner == b.inner { return true; }
160         if a.inner.is_null() || b.inner.is_null() { return false; }
161         if a.get_native_ref() == b.get_native_ref() { true } else { false }
162 }
163 /// Peel one layer off an incoming onion, returning a [`PendingHTLCInfo`] that contains information
164 /// about the intended next-hop for the HTLC.
165 ///
166 /// This does all the relevant context-free checks that LDK requires for payment relay or
167 /// acceptance. If the payment is to be received, and the amount matches the expected amount for
168 /// a given invoice, this indicates the [`msgs::UpdateAddHTLC`], once fully committed in the
169 /// channel, will generate an [`Event::PaymentClaimable`].
170 ///
171 /// [`Event::PaymentClaimable`]: crate::events::Event::PaymentClaimable
172 #[no_mangle]
173 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 {
174         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);
175         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() };
176         local_ret
177 }
178