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