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 [`Invoice`], retrying if needed based on [`Retry`].
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.
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::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() };
33 /// Pays the given [`Invoice`] with a custom idempotency key, retrying if needed based on [`Retry`].
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.
38 /// You should ensure that the [`Invoice::payment_hash`] is unique and the same [`PaymentHash`]
39 /// has never been paid before.
41 /// See [`pay_invoice`] for a variant which uses the [`PaymentHash`] for the idempotency token.
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() };
49 /// Pays the given zero-value [`Invoice`] using the given amount, retrying if needed based on
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.
56 /// If you wish to use a different payment idempotency token, see
57 /// [`pay_zero_value_invoice_with_id`].
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() };
65 /// Pays the given zero-value [`Invoice`] using the given amount and custom idempotency key,
66 /// , retrying if needed based on [`Retry`].
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.
71 /// You should ensure that the [`Invoice::payment_hash`] is unique and the same [`PaymentHash`]
72 /// has never been paid before.
74 /// See [`pay_zero_value_invoice`] for a variant which uses the [`PaymentHash`] for the
75 /// idempotency token.
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() };
83 /// An error that may occur when making a payment.
87 pub enum PaymentError {
88 /// An error resulting from the provided [`Invoice`] or payment hash.
91 /// An error occurring when sending a payment.
93 crate::lightning::ln::outbound_payment::RetryableSendFailure),
95 use lightning_invoice::payment::PaymentError as PaymentErrorImport;
96 pub(crate) type nativePaymentError = PaymentErrorImport;
100 pub(crate) fn to_native(&self) -> nativePaymentError {
102 PaymentError::Invoice (ref a, ) => {
103 let mut a_nonref = Clone::clone(a);
104 nativePaymentError::Invoice (
108 PaymentError::Sending (ref a, ) => {
109 let mut a_nonref = Clone::clone(a);
110 nativePaymentError::Sending (
111 a_nonref.into_native(),
117 pub(crate) fn into_native(self) -> nativePaymentError {
119 PaymentError::Invoice (mut a, ) => {
120 nativePaymentError::Invoice (
124 PaymentError::Sending (mut a, ) => {
125 nativePaymentError::Sending (
132 pub(crate) fn from_native(native: &nativePaymentError) -> Self {
134 nativePaymentError::Invoice (ref a, ) => {
135 let mut a_nonref = Clone::clone(a);
136 PaymentError::Invoice (
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),
149 pub(crate) fn native_into(native: nativePaymentError) -> Self {
151 nativePaymentError::Invoice (mut a, ) => {
152 PaymentError::Invoice (
156 nativePaymentError::Sending (mut a, ) => {
157 PaymentError::Sending (
158 crate::lightning::ln::outbound_payment::RetryableSendFailure::native_into(a),
164 /// Frees any resources used by the PaymentError
166 pub extern "C" fn PaymentError_free(this_ptr: PaymentError) { }
167 /// Creates a copy of the PaymentError
169 pub extern "C" fn PaymentError_clone(orig: &PaymentError) -> PaymentError {
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, )
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, )