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 //! Convenient utilities for paying Lightning invoices and sending spontaneous payments.
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};
19 /// Pays the given [`Bolt11Invoice`], retrying if needed based on [`Retry`].
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.
25 /// If you wish to use a different payment idempotency token, see [`pay_invoice_with_id`].
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() };
33 /// Pays the given [`Bolt11Invoice`] with a custom idempotency key, retrying if needed based on
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.
39 /// You should ensure that the [`Bolt11Invoice::payment_hash`] is unique and the same
40 /// [`PaymentHash`] has never been paid before.
42 /// See [`pay_invoice`] for a variant which uses the [`PaymentHash`] for the idempotency token.
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() };
50 /// Pays the given zero-value [`Bolt11Invoice`] using the given amount, retrying if needed based on
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.
57 /// If you wish to use a different payment idempotency token, see
58 /// [`pay_zero_value_invoice_with_id`].
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() };
66 /// Pays the given zero-value [`Bolt11Invoice`] using the given amount and custom idempotency key,
67 /// retrying if needed based on [`Retry`].
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.
72 /// You should ensure that the [`Bolt11Invoice::payment_hash`] is unique and the same
73 /// [`PaymentHash`] has never been paid before.
75 /// See [`pay_zero_value_invoice`] for a variant which uses the [`PaymentHash`] for the
76 /// idempotency token.
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() };
84 /// An error that may occur when making a payment.
88 pub enum PaymentError {
89 /// An error resulting from the provided [`Bolt11Invoice`] or payment hash.
92 /// An error occurring when sending a payment.
94 crate::lightning::ln::outbound_payment::RetryableSendFailure),
96 use lightning_invoice::payment::PaymentError as PaymentErrorImport;
97 pub(crate) type nativePaymentError = PaymentErrorImport;
101 pub(crate) fn to_native(&self) -> nativePaymentError {
103 PaymentError::Invoice (ref a, ) => {
104 let mut a_nonref = Clone::clone(a);
105 nativePaymentError::Invoice (
109 PaymentError::Sending (ref a, ) => {
110 let mut a_nonref = Clone::clone(a);
111 nativePaymentError::Sending (
112 a_nonref.into_native(),
118 pub(crate) fn into_native(self) -> nativePaymentError {
120 PaymentError::Invoice (mut a, ) => {
121 nativePaymentError::Invoice (
125 PaymentError::Sending (mut a, ) => {
126 nativePaymentError::Sending (
133 pub(crate) fn from_native(native: &nativePaymentError) -> Self {
135 nativePaymentError::Invoice (ref a, ) => {
136 let mut a_nonref = Clone::clone(a);
137 PaymentError::Invoice (
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),
150 pub(crate) fn native_into(native: nativePaymentError) -> Self {
152 nativePaymentError::Invoice (mut a, ) => {
153 PaymentError::Invoice (
157 nativePaymentError::Sending (mut a, ) => {
158 PaymentError::Sending (
159 crate::lightning::ln::outbound_payment::RetryableSendFailure::native_into(a),
165 /// Frees any resources used by the PaymentError
167 pub extern "C" fn PaymentError_free(this_ptr: PaymentError) { }
168 /// Creates a copy of the PaymentError
170 pub extern "C" fn PaymentError_clone(orig: &PaymentError) -> PaymentError {
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, )
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, )
183 /// Checks if two PaymentErrors contain equal inner contents.
184 /// This ignores pointers and is_owned flags and looks at the values in fields.
186 pub extern "C" fn PaymentError_eq(a: &PaymentError, b: &PaymentError) -> bool {
187 if &a.to_native() == &b.to_native() { true } else { false }