Update auto-generated bindings
[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 and sending spontaneous payments.
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 /// Pays the given [`Invoice`], retrying if needed based on [`Retry`].
20 ///
21 /// [`Invoice::payment_hash`] is used as the [`PaymentId`], which ensures idempotency as long
22 /// as the payment is still pending. Once the payment completes or fails, you must ensure that
23 /// a second payment with the same [`PaymentHash`] is never sent.
24 ///
25 /// If you wish to use a different payment idempotency token, see [`pay_invoice_with_id`].
26 #[no_mangle]
27 pub extern "C" fn pay_invoice(invoice: &crate::lightning_invoice::Invoice, mut retry_strategy: crate::lightning::ln::outbound_payment::Retry, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager) -> crate::c_types::derived::CResult_PaymentIdPaymentErrorZ {
28         let mut ret = lightning_invoice::payment::pay_invoice::<crate::lightning::chain::Watch, crate::lightning::chain::chaininterface::BroadcasterInterface, crate::lightning::chain::keysinterface::EntropySource, crate::lightning::chain::keysinterface::NodeSigner, crate::lightning::chain::keysinterface::SignerProvider, crate::lightning::chain::chaininterface::FeeEstimator, crate::lightning::routing::router::Router, crate::lightning::util::logger::Logger>(invoice.get_native_ref(), retry_strategy.into_native(), channelmanager.get_native_ref());
29         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() };
30         local_ret
31 }
32
33 /// Pays the given [`Invoice`] with a custom idempotency key, retrying if needed based on [`Retry`].
34 ///
35 /// Note that idempotency is only guaranteed as long as the payment is still pending. Once the
36 /// payment completes or fails, no idempotency guarantees are made.
37 ///
38 /// You should ensure that the [`Invoice::payment_hash`] is unique and the same [`PaymentHash`]
39 /// has never been paid before.
40 ///
41 /// See [`pay_invoice`] for a variant which uses the [`PaymentHash`] for the idempotency token.
42 #[no_mangle]
43 pub extern "C" fn pay_invoice_with_id(invoice: &crate::lightning_invoice::Invoice, 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 {
44         let mut ret = lightning_invoice::payment::pay_invoice_with_id::<crate::lightning::chain::Watch, crate::lightning::chain::chaininterface::BroadcasterInterface, crate::lightning::chain::keysinterface::EntropySource, crate::lightning::chain::keysinterface::NodeSigner, crate::lightning::chain::keysinterface::SignerProvider, crate::lightning::chain::chaininterface::FeeEstimator, crate::lightning::routing::router::Router, crate::lightning::util::logger::Logger>(invoice.get_native_ref(), ::lightning::ln::channelmanager::PaymentId(payment_id.data), retry_strategy.into_native(), channelmanager.get_native_ref());
45         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() };
46         local_ret
47 }
48
49 /// Pays the given zero-value [`Invoice`] using the given amount, retrying if needed based on
50 /// [`Retry`].
51 ///
52 /// [`Invoice::payment_hash`] is used as the [`PaymentId`], which ensures idempotency as long
53 /// as the payment is still pending. Once the payment completes or fails, you must ensure that
54 /// a second payment with the same [`PaymentHash`] is never sent.
55 ///
56 /// If you wish to use a different payment idempotency token, see
57 /// [`pay_zero_value_invoice_with_id`].
58 #[no_mangle]
59 pub extern "C" fn pay_zero_value_invoice(invoice: &crate::lightning_invoice::Invoice, mut amount_msats: u64, mut retry_strategy: crate::lightning::ln::outbound_payment::Retry, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager) -> crate::c_types::derived::CResult_PaymentIdPaymentErrorZ {
60         let mut ret = lightning_invoice::payment::pay_zero_value_invoice::<crate::lightning::chain::Watch, crate::lightning::chain::chaininterface::BroadcasterInterface, crate::lightning::chain::keysinterface::EntropySource, crate::lightning::chain::keysinterface::NodeSigner, crate::lightning::chain::keysinterface::SignerProvider, crate::lightning::chain::chaininterface::FeeEstimator, crate::lightning::routing::router::Router, crate::lightning::util::logger::Logger>(invoice.get_native_ref(), amount_msats, retry_strategy.into_native(), channelmanager.get_native_ref());
61         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() };
62         local_ret
63 }
64
65 /// Pays the given zero-value [`Invoice`] using the given amount and custom idempotency key,
66 /// , retrying if needed based on [`Retry`].
67 ///
68 /// Note that idempotency is only guaranteed as long as the payment is still pending. Once the
69 /// payment completes or fails, no idempotency guarantees are made.
70 ///
71 /// You should ensure that the [`Invoice::payment_hash`] is unique and the same [`PaymentHash`]
72 /// has never been paid before.
73 ///
74 /// See [`pay_zero_value_invoice`] for a variant which uses the [`PaymentHash`] for the
75 /// idempotency token.
76 #[no_mangle]
77 pub extern "C" fn pay_zero_value_invoice_with_id(invoice: &crate::lightning_invoice::Invoice, 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 {
78         let mut ret = lightning_invoice::payment::pay_zero_value_invoice_with_id::<crate::lightning::chain::Watch, crate::lightning::chain::chaininterface::BroadcasterInterface, crate::lightning::chain::keysinterface::EntropySource, crate::lightning::chain::keysinterface::NodeSigner, crate::lightning::chain::keysinterface::SignerProvider, crate::lightning::chain::chaininterface::FeeEstimator, crate::lightning::routing::router::Router, crate::lightning::util::logger::Logger>(invoice.get_native_ref(), amount_msats, ::lightning::ln::channelmanager::PaymentId(payment_id.data), retry_strategy.into_native(), channelmanager.get_native_ref());
79         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() };
80         local_ret
81 }
82
83 /// An error that may occur when making a payment.
84 #[derive(Clone)]
85 #[must_use]
86 #[repr(C)]
87 pub enum PaymentError {
88         /// An error resulting from the provided [`Invoice`] or payment hash.
89         Invoice(
90                 crate::c_types::Str),
91         /// An error occurring when sending a payment.
92         Sending(
93                 crate::lightning::ln::outbound_payment::RetryableSendFailure),
94 }
95 use lightning_invoice::payment::PaymentError as PaymentErrorImport;
96 pub(crate) type nativePaymentError = PaymentErrorImport;
97
98 impl PaymentError {
99         #[allow(unused)]
100         pub(crate) fn to_native(&self) -> nativePaymentError {
101                 match self {
102                         PaymentError::Invoice (ref a, ) => {
103                                 let mut a_nonref = Clone::clone(a);
104                                 nativePaymentError::Invoice (
105                                         a_nonref.into_str(),
106                                 )
107                         },
108                         PaymentError::Sending (ref a, ) => {
109                                 let mut a_nonref = Clone::clone(a);
110                                 nativePaymentError::Sending (
111                                         a_nonref.into_native(),
112                                 )
113                         },
114                 }
115         }
116         #[allow(unused)]
117         pub(crate) fn into_native(self) -> nativePaymentError {
118                 match self {
119                         PaymentError::Invoice (mut a, ) => {
120                                 nativePaymentError::Invoice (
121                                         a.into_str(),
122                                 )
123                         },
124                         PaymentError::Sending (mut a, ) => {
125                                 nativePaymentError::Sending (
126                                         a.into_native(),
127                                 )
128                         },
129                 }
130         }
131         #[allow(unused)]
132         pub(crate) fn from_native(native: &nativePaymentError) -> Self {
133                 match native {
134                         nativePaymentError::Invoice (ref a, ) => {
135                                 let mut a_nonref = Clone::clone(a);
136                                 PaymentError::Invoice (
137                                         a_nonref.into(),
138                                 )
139                         },
140                         nativePaymentError::Sending (ref a, ) => {
141                                 let mut a_nonref = Clone::clone(a);
142                                 PaymentError::Sending (
143                                         crate::lightning::ln::outbound_payment::RetryableSendFailure::native_into(a_nonref),
144                                 )
145                         },
146                 }
147         }
148         #[allow(unused)]
149         pub(crate) fn native_into(native: nativePaymentError) -> Self {
150                 match native {
151                         nativePaymentError::Invoice (mut a, ) => {
152                                 PaymentError::Invoice (
153                                         a.into(),
154                                 )
155                         },
156                         nativePaymentError::Sending (mut a, ) => {
157                                 PaymentError::Sending (
158                                         crate::lightning::ln::outbound_payment::RetryableSendFailure::native_into(a),
159                                 )
160                         },
161                 }
162         }
163 }
164 /// Frees any resources used by the PaymentError
165 #[no_mangle]
166 pub extern "C" fn PaymentError_free(this_ptr: PaymentError) { }
167 /// Creates a copy of the PaymentError
168 #[no_mangle]
169 pub extern "C" fn PaymentError_clone(orig: &PaymentError) -> PaymentError {
170         orig.clone()
171 }
172 #[no_mangle]
173 /// Utility method to constructs a new Invoice-variant PaymentError
174 pub extern "C" fn PaymentError_invoice(a: crate::c_types::Str) -> PaymentError {
175         PaymentError::Invoice(a, )
176 }
177 #[no_mangle]
178 /// Utility method to constructs a new Sending-variant PaymentError
179 pub extern "C" fn PaymentError_sending(a: crate::lightning::ln::outbound_payment::RetryableSendFailure) -> PaymentError {
180         PaymentError::Sending(a, )
181 }