593a68324c1cfd3d8881cfeafa3a9dd2bddf71cd
[ldk-c-bindings] / lightning-c-bindings / src / lightning_invoice / 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 //! Convenient utilities for paying Lightning invoices.
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 /// Pays the given [`Bolt11Invoice`], retrying if needed based on [`Retry`].
21 ///
22 /// [`Bolt11Invoice::payment_hash`] is used as the [`PaymentId`], which ensures idempotency as long
23 /// as the payment is still pending. If the payment succeeds, you must ensure that a second payment
24 /// with the same [`PaymentHash`] is never sent.
25 ///
26 /// If you wish to use a different payment idempotency token, see [`pay_invoice_with_id`].
27 #[no_mangle]
28 pub extern "C" fn pay_invoice(invoice: &crate::lightning_invoice::Bolt11Invoice, mut retry_strategy: crate::lightning::ln::outbound_payment::Retry, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager) -> crate::c_types::derived::CResult_ThirtyTwoBytesPaymentErrorZ {
29         let mut ret = lightning_invoice::payment::pay_invoice::<_>(invoice.get_native_ref(), retry_strategy.into_native(), channelmanager.get_native_ref());
30         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( { crate::lightning_invoice::payment::PaymentError::native_into(e) }).into() };
31         local_ret
32 }
33
34 /// Pays the given [`Bolt11Invoice`] with a custom idempotency key, retrying if needed based on
35 /// [`Retry`].
36 ///
37 /// Note that idempotency is only guaranteed as long as the payment is still pending. Once the
38 /// payment completes or fails, no idempotency guarantees are made.
39 ///
40 /// You should ensure that the [`Bolt11Invoice::payment_hash`] is unique and the same
41 /// [`PaymentHash`] has never been paid before.
42 ///
43 /// See [`pay_invoice`] for a variant which uses the [`PaymentHash`] for the idempotency token.
44 #[no_mangle]
45 pub extern "C" fn pay_invoice_with_id(invoice: &crate::lightning_invoice::Bolt11Invoice, mut payment_id: crate::c_types::ThirtyTwoBytes, mut retry_strategy: crate::lightning::ln::outbound_payment::Retry, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager) -> crate::c_types::derived::CResult_NonePaymentErrorZ {
46         let mut ret = lightning_invoice::payment::pay_invoice_with_id::<_>(invoice.get_native_ref(), ::lightning::ln::channelmanager::PaymentId(payment_id.data), retry_strategy.into_native(), channelmanager.get_native_ref());
47         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::payment::PaymentError::native_into(e) }).into() };
48         local_ret
49 }
50
51 /// Pays the given zero-value [`Bolt11Invoice`] using the given amount, retrying if needed based on
52 /// [`Retry`].
53 ///
54 /// [`Bolt11Invoice::payment_hash`] is used as the [`PaymentId`], which ensures idempotency as long
55 /// as the payment is still pending. If the payment succeeds, you must ensure that a second payment
56 /// with the same [`PaymentHash`] is never sent.
57 ///
58 /// If you wish to use a different payment idempotency token, see
59 /// [`pay_zero_value_invoice_with_id`].
60 #[no_mangle]
61 pub extern "C" fn pay_zero_value_invoice(invoice: &crate::lightning_invoice::Bolt11Invoice, mut amount_msats: u64, mut retry_strategy: crate::lightning::ln::outbound_payment::Retry, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager) -> crate::c_types::derived::CResult_ThirtyTwoBytesPaymentErrorZ {
62         let mut ret = lightning_invoice::payment::pay_zero_value_invoice::<_>(invoice.get_native_ref(), amount_msats, retry_strategy.into_native(), channelmanager.get_native_ref());
63         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( { crate::lightning_invoice::payment::PaymentError::native_into(e) }).into() };
64         local_ret
65 }
66
67 /// Pays the given zero-value [`Bolt11Invoice`] using the given amount and custom idempotency key,
68 /// retrying if needed based on [`Retry`].
69 ///
70 /// Note that idempotency is only guaranteed as long as the payment is still pending. Once the
71 /// payment completes or fails, no idempotency guarantees are made.
72 ///
73 /// You should ensure that the [`Bolt11Invoice::payment_hash`] is unique and the same
74 /// [`PaymentHash`] has never been paid before.
75 ///
76 /// See [`pay_zero_value_invoice`] for a variant which uses the [`PaymentHash`] for the
77 /// idempotency token.
78 #[no_mangle]
79 pub extern "C" fn pay_zero_value_invoice_with_id(invoice: &crate::lightning_invoice::Bolt11Invoice, mut amount_msats: u64, mut payment_id: crate::c_types::ThirtyTwoBytes, mut retry_strategy: crate::lightning::ln::outbound_payment::Retry, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager) -> crate::c_types::derived::CResult_NonePaymentErrorZ {
80         let mut ret = lightning_invoice::payment::pay_zero_value_invoice_with_id::<_>(invoice.get_native_ref(), amount_msats, ::lightning::ln::channelmanager::PaymentId(payment_id.data), retry_strategy.into_native(), channelmanager.get_native_ref());
81         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::payment::PaymentError::native_into(e) }).into() };
82         local_ret
83 }
84
85 /// Sends payment probes over all paths of a route that would be used to pay the given invoice.
86 ///
87 /// See [`ChannelManager::send_preflight_probes`] for more information.
88 #[no_mangle]
89 pub extern "C" fn preflight_probe_invoice(invoice: &crate::lightning_invoice::Bolt11Invoice, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager, mut liquidity_limit_multiplier: crate::c_types::derived::COption_u64Z) -> crate::c_types::derived::CResult_CVec_C2Tuple_ThirtyTwoBytesThirtyTwoBytesZZProbingErrorZ {
90         let mut local_liquidity_limit_multiplier = if liquidity_limit_multiplier.is_some() { Some( { liquidity_limit_multiplier.take() }) } else { None };
91         let mut ret = lightning_invoice::payment::preflight_probe_invoice::<_>(invoice.get_native_ref(), channelmanager.get_native_ref(), local_liquidity_limit_multiplier);
92         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for mut item in o.drain(..) { local_ret_0.push( { let (mut orig_ret_0_0_0, mut orig_ret_0_0_1) = item; let mut local_ret_0_0 = (crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0_0.0 }, crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0_1.0 }).into(); local_ret_0_0 }); }; local_ret_0.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::payment::ProbingError::native_into(e) }).into() };
93         local_ret
94 }
95
96 /// Sends payment probes over all paths of a route that would be used to pay the given zero-value
97 /// invoice using the given amount.
98 ///
99 /// See [`ChannelManager::send_preflight_probes`] for more information.
100 #[no_mangle]
101 pub extern "C" fn preflight_probe_zero_value_invoice(invoice: &crate::lightning_invoice::Bolt11Invoice, mut amount_msat: u64, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager, mut liquidity_limit_multiplier: crate::c_types::derived::COption_u64Z) -> crate::c_types::derived::CResult_CVec_C2Tuple_ThirtyTwoBytesThirtyTwoBytesZZProbingErrorZ {
102         let mut local_liquidity_limit_multiplier = if liquidity_limit_multiplier.is_some() { Some( { liquidity_limit_multiplier.take() }) } else { None };
103         let mut ret = lightning_invoice::payment::preflight_probe_zero_value_invoice::<_>(invoice.get_native_ref(), amount_msat, channelmanager.get_native_ref(), local_liquidity_limit_multiplier);
104         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for mut item in o.drain(..) { local_ret_0.push( { let (mut orig_ret_0_0_0, mut orig_ret_0_0_1) = item; let mut local_ret_0_0 = (crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0_0.0 }, crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0_1.0 }).into(); local_ret_0_0 }); }; local_ret_0.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::payment::ProbingError::native_into(e) }).into() };
105         local_ret
106 }
107
108 /// An error that may occur when making a payment.
109 #[derive(Clone)]
110 #[must_use]
111 #[repr(C)]
112 pub enum PaymentError {
113         /// An error resulting from the provided [`Bolt11Invoice`] or payment hash.
114         Invoice(
115                 crate::c_types::Str),
116         /// An error occurring when sending a payment.
117         Sending(
118                 crate::lightning::ln::outbound_payment::RetryableSendFailure),
119 }
120 use lightning_invoice::payment::PaymentError as PaymentErrorImport;
121 pub(crate) type nativePaymentError = PaymentErrorImport;
122
123 impl PaymentError {
124         #[allow(unused)]
125         pub(crate) fn to_native(&self) -> nativePaymentError {
126                 match self {
127                         PaymentError::Invoice (ref a, ) => {
128                                 let mut a_nonref = Clone::clone(a);
129                                 nativePaymentError::Invoice (
130                                         a_nonref.into_str(),
131                                 )
132                         },
133                         PaymentError::Sending (ref a, ) => {
134                                 let mut a_nonref = Clone::clone(a);
135                                 nativePaymentError::Sending (
136                                         a_nonref.into_native(),
137                                 )
138                         },
139                 }
140         }
141         #[allow(unused)]
142         pub(crate) fn into_native(self) -> nativePaymentError {
143                 match self {
144                         PaymentError::Invoice (mut a, ) => {
145                                 nativePaymentError::Invoice (
146                                         a.into_str(),
147                                 )
148                         },
149                         PaymentError::Sending (mut a, ) => {
150                                 nativePaymentError::Sending (
151                                         a.into_native(),
152                                 )
153                         },
154                 }
155         }
156         #[allow(unused)]
157         pub(crate) fn from_native(native: &nativePaymentError) -> Self {
158                 match native {
159                         nativePaymentError::Invoice (ref a, ) => {
160                                 let mut a_nonref = Clone::clone(a);
161                                 PaymentError::Invoice (
162                                         a_nonref.into(),
163                                 )
164                         },
165                         nativePaymentError::Sending (ref a, ) => {
166                                 let mut a_nonref = Clone::clone(a);
167                                 PaymentError::Sending (
168                                         crate::lightning::ln::outbound_payment::RetryableSendFailure::native_into(a_nonref),
169                                 )
170                         },
171                 }
172         }
173         #[allow(unused)]
174         pub(crate) fn native_into(native: nativePaymentError) -> Self {
175                 match native {
176                         nativePaymentError::Invoice (mut a, ) => {
177                                 PaymentError::Invoice (
178                                         a.into(),
179                                 )
180                         },
181                         nativePaymentError::Sending (mut a, ) => {
182                                 PaymentError::Sending (
183                                         crate::lightning::ln::outbound_payment::RetryableSendFailure::native_into(a),
184                                 )
185                         },
186                 }
187         }
188 }
189 /// Frees any resources used by the PaymentError
190 #[no_mangle]
191 pub extern "C" fn PaymentError_free(this_ptr: PaymentError) { }
192 /// Creates a copy of the PaymentError
193 #[no_mangle]
194 pub extern "C" fn PaymentError_clone(orig: &PaymentError) -> PaymentError {
195         orig.clone()
196 }
197 #[allow(unused)]
198 /// Used only if an object of this type is returned as a trait impl by a method
199 pub(crate) extern "C" fn PaymentError_clone_void(this_ptr: *const c_void) -> *mut c_void {
200         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const PaymentError)).clone() })) as *mut c_void
201 }
202 #[allow(unused)]
203 /// Used only if an object of this type is returned as a trait impl by a method
204 pub(crate) extern "C" fn PaymentError_free_void(this_ptr: *mut c_void) {
205         let _ = unsafe { Box::from_raw(this_ptr as *mut PaymentError) };
206 }
207 #[no_mangle]
208 /// Utility method to constructs a new Invoice-variant PaymentError
209 pub extern "C" fn PaymentError_invoice(a: crate::c_types::Str) -> PaymentError {
210         PaymentError::Invoice(a, )
211 }
212 #[no_mangle]
213 /// Utility method to constructs a new Sending-variant PaymentError
214 pub extern "C" fn PaymentError_sending(a: crate::lightning::ln::outbound_payment::RetryableSendFailure) -> PaymentError {
215         PaymentError::Sending(a, )
216 }
217 /// Checks if two PaymentErrors contain equal inner contents.
218 /// This ignores pointers and is_owned flags and looks at the values in fields.
219 #[no_mangle]
220 pub extern "C" fn PaymentError_eq(a: &PaymentError, b: &PaymentError) -> bool {
221         if &a.to_native() == &b.to_native() { true } else { false }
222 }
223 /// An error that may occur when sending a payment probe.
224 #[derive(Clone)]
225 #[must_use]
226 #[repr(C)]
227 pub enum ProbingError {
228         /// An error resulting from the provided [`Bolt11Invoice`].
229         Invoice(
230                 crate::c_types::Str),
231         /// An error occurring when sending a payment probe.
232         Sending(
233                 crate::lightning::ln::outbound_payment::ProbeSendFailure),
234 }
235 use lightning_invoice::payment::ProbingError as ProbingErrorImport;
236 pub(crate) type nativeProbingError = ProbingErrorImport;
237
238 impl ProbingError {
239         #[allow(unused)]
240         pub(crate) fn to_native(&self) -> nativeProbingError {
241                 match self {
242                         ProbingError::Invoice (ref a, ) => {
243                                 let mut a_nonref = Clone::clone(a);
244                                 nativeProbingError::Invoice (
245                                         a_nonref.into_str(),
246                                 )
247                         },
248                         ProbingError::Sending (ref a, ) => {
249                                 let mut a_nonref = Clone::clone(a);
250                                 nativeProbingError::Sending (
251                                         a_nonref.into_native(),
252                                 )
253                         },
254                 }
255         }
256         #[allow(unused)]
257         pub(crate) fn into_native(self) -> nativeProbingError {
258                 match self {
259                         ProbingError::Invoice (mut a, ) => {
260                                 nativeProbingError::Invoice (
261                                         a.into_str(),
262                                 )
263                         },
264                         ProbingError::Sending (mut a, ) => {
265                                 nativeProbingError::Sending (
266                                         a.into_native(),
267                                 )
268                         },
269                 }
270         }
271         #[allow(unused)]
272         pub(crate) fn from_native(native: &nativeProbingError) -> Self {
273                 match native {
274                         nativeProbingError::Invoice (ref a, ) => {
275                                 let mut a_nonref = Clone::clone(a);
276                                 ProbingError::Invoice (
277                                         a_nonref.into(),
278                                 )
279                         },
280                         nativeProbingError::Sending (ref a, ) => {
281                                 let mut a_nonref = Clone::clone(a);
282                                 ProbingError::Sending (
283                                         crate::lightning::ln::outbound_payment::ProbeSendFailure::native_into(a_nonref),
284                                 )
285                         },
286                 }
287         }
288         #[allow(unused)]
289         pub(crate) fn native_into(native: nativeProbingError) -> Self {
290                 match native {
291                         nativeProbingError::Invoice (mut a, ) => {
292                                 ProbingError::Invoice (
293                                         a.into(),
294                                 )
295                         },
296                         nativeProbingError::Sending (mut a, ) => {
297                                 ProbingError::Sending (
298                                         crate::lightning::ln::outbound_payment::ProbeSendFailure::native_into(a),
299                                 )
300                         },
301                 }
302         }
303 }
304 /// Frees any resources used by the ProbingError
305 #[no_mangle]
306 pub extern "C" fn ProbingError_free(this_ptr: ProbingError) { }
307 /// Creates a copy of the ProbingError
308 #[no_mangle]
309 pub extern "C" fn ProbingError_clone(orig: &ProbingError) -> ProbingError {
310         orig.clone()
311 }
312 #[allow(unused)]
313 /// Used only if an object of this type is returned as a trait impl by a method
314 pub(crate) extern "C" fn ProbingError_clone_void(this_ptr: *const c_void) -> *mut c_void {
315         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const ProbingError)).clone() })) as *mut c_void
316 }
317 #[allow(unused)]
318 /// Used only if an object of this type is returned as a trait impl by a method
319 pub(crate) extern "C" fn ProbingError_free_void(this_ptr: *mut c_void) {
320         let _ = unsafe { Box::from_raw(this_ptr as *mut ProbingError) };
321 }
322 #[no_mangle]
323 /// Utility method to constructs a new Invoice-variant ProbingError
324 pub extern "C" fn ProbingError_invoice(a: crate::c_types::Str) -> ProbingError {
325         ProbingError::Invoice(a, )
326 }
327 #[no_mangle]
328 /// Utility method to constructs a new Sending-variant ProbingError
329 pub extern "C" fn ProbingError_sending(a: crate::lightning::ln::outbound_payment::ProbeSendFailure) -> ProbingError {
330         ProbingError::Sending(a, )
331 }
332 /// Checks if two ProbingErrors contain equal inner contents.
333 /// This ignores pointers and is_owned flags and looks at the values in fields.
334 #[no_mangle]
335 pub extern "C" fn ProbingError_eq(a: &ProbingError, b: &ProbingError) -> bool {
336         if &a.to_native() == &b.to_native() { true } else { false }
337 }