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.
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};
20 /// Pays the given [`Bolt11Invoice`], retrying if needed based on [`Retry`].
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.
26 /// If you wish to use a different payment idempotency token, see [`pay_invoice_with_id`].
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() };
34 /// Pays the given [`Bolt11Invoice`] with a custom idempotency key, retrying if needed based on
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.
40 /// You should ensure that the [`Bolt11Invoice::payment_hash`] is unique and the same
41 /// [`PaymentHash`] has never been paid before.
43 /// See [`pay_invoice`] for a variant which uses the [`PaymentHash`] for the idempotency token.
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() };
51 /// Pays the given zero-value [`Bolt11Invoice`] using the given amount, retrying if needed based on
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.
58 /// If you wish to use a different payment idempotency token, see
59 /// [`pay_zero_value_invoice_with_id`].
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() };
67 /// Pays the given zero-value [`Bolt11Invoice`] using the given amount and custom idempotency key,
68 /// retrying if needed based on [`Retry`].
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.
73 /// You should ensure that the [`Bolt11Invoice::payment_hash`] is unique and the same
74 /// [`PaymentHash`] has never been paid before.
76 /// See [`pay_zero_value_invoice`] for a variant which uses the [`PaymentHash`] for the
77 /// idempotency token.
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() };
85 /// Sends payment probes over all paths of a route that would be used to pay the given invoice.
87 /// See [`ChannelManager::send_preflight_probes`] for more information.
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() };
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.
99 /// See [`ChannelManager::send_preflight_probes`] for more information.
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() };
108 /// An error that may occur when making a payment.
112 pub enum PaymentError {
113 /// An error resulting from the provided [`Bolt11Invoice`] or payment hash.
115 crate::c_types::Str),
116 /// An error occurring when sending a payment.
118 crate::lightning::ln::outbound_payment::RetryableSendFailure),
120 use lightning_invoice::payment::PaymentError as PaymentErrorImport;
121 pub(crate) type nativePaymentError = PaymentErrorImport;
125 pub(crate) fn to_native(&self) -> nativePaymentError {
127 PaymentError::Invoice (ref a, ) => {
128 let mut a_nonref = Clone::clone(a);
129 nativePaymentError::Invoice (
133 PaymentError::Sending (ref a, ) => {
134 let mut a_nonref = Clone::clone(a);
135 nativePaymentError::Sending (
136 a_nonref.into_native(),
142 pub(crate) fn into_native(self) -> nativePaymentError {
144 PaymentError::Invoice (mut a, ) => {
145 nativePaymentError::Invoice (
149 PaymentError::Sending (mut a, ) => {
150 nativePaymentError::Sending (
157 pub(crate) fn from_native(native: &nativePaymentError) -> Self {
159 nativePaymentError::Invoice (ref a, ) => {
160 let mut a_nonref = Clone::clone(a);
161 PaymentError::Invoice (
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),
174 pub(crate) fn native_into(native: nativePaymentError) -> Self {
176 nativePaymentError::Invoice (mut a, ) => {
177 PaymentError::Invoice (
181 nativePaymentError::Sending (mut a, ) => {
182 PaymentError::Sending (
183 crate::lightning::ln::outbound_payment::RetryableSendFailure::native_into(a),
189 /// Frees any resources used by the PaymentError
191 pub extern "C" fn PaymentError_free(this_ptr: PaymentError) { }
192 /// Creates a copy of the PaymentError
194 pub extern "C" fn PaymentError_clone(orig: &PaymentError) -> PaymentError {
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
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) };
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, )
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, )
217 /// Checks if two PaymentErrors contain equal inner contents.
218 /// This ignores pointers and is_owned flags and looks at the values in fields.
220 pub extern "C" fn PaymentError_eq(a: &PaymentError, b: &PaymentError) -> bool {
221 if &a.to_native() == &b.to_native() { true } else { false }
223 /// An error that may occur when sending a payment probe.
227 pub enum ProbingError {
228 /// An error resulting from the provided [`Bolt11Invoice`].
230 crate::c_types::Str),
231 /// An error occurring when sending a payment probe.
233 crate::lightning::ln::outbound_payment::ProbeSendFailure),
235 use lightning_invoice::payment::ProbingError as ProbingErrorImport;
236 pub(crate) type nativeProbingError = ProbingErrorImport;
240 pub(crate) fn to_native(&self) -> nativeProbingError {
242 ProbingError::Invoice (ref a, ) => {
243 let mut a_nonref = Clone::clone(a);
244 nativeProbingError::Invoice (
248 ProbingError::Sending (ref a, ) => {
249 let mut a_nonref = Clone::clone(a);
250 nativeProbingError::Sending (
251 a_nonref.into_native(),
257 pub(crate) fn into_native(self) -> nativeProbingError {
259 ProbingError::Invoice (mut a, ) => {
260 nativeProbingError::Invoice (
264 ProbingError::Sending (mut a, ) => {
265 nativeProbingError::Sending (
272 pub(crate) fn from_native(native: &nativeProbingError) -> Self {
274 nativeProbingError::Invoice (ref a, ) => {
275 let mut a_nonref = Clone::clone(a);
276 ProbingError::Invoice (
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),
289 pub(crate) fn native_into(native: nativeProbingError) -> Self {
291 nativeProbingError::Invoice (mut a, ) => {
292 ProbingError::Invoice (
296 nativeProbingError::Sending (mut a, ) => {
297 ProbingError::Sending (
298 crate::lightning::ln::outbound_payment::ProbeSendFailure::native_into(a),
304 /// Frees any resources used by the ProbingError
306 pub extern "C" fn ProbingError_free(this_ptr: ProbingError) { }
307 /// Creates a copy of the ProbingError
309 pub extern "C" fn ProbingError_clone(orig: &ProbingError) -> ProbingError {
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
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) };
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, )
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, )
332 /// Checks if two ProbingErrors contain equal inner contents.
333 /// This ignores pointers and is_owned flags and looks at the values in fields.
335 pub extern "C" fn ProbingError_eq(a: &ProbingError, b: &ProbingError) -> bool {
336 if &a.to_native() == &b.to_native() { true } else { false }