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 //! Utilities to send payments and manage outbound payment information.
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 /// Strategies available to retry payment path failures.
24 /// Max number of attempts to retry payment.
26 /// Each attempt may be multiple HTLCs along multiple paths if the router decides to split up a
27 /// retry, and may retry multiple failed HTLCs at once if they failed around the same time and
28 /// were retried along a route from a single call to [`Router::find_route_with_id`].
31 /// Time elapsed before abandoning retries for a payment. At least one attempt at payment is made;
32 /// see [`PaymentParameters::expiry_time`] to avoid any attempt at payment after a specific time.
34 /// [`PaymentParameters::expiry_time`]: crate::routing::router::PaymentParameters::expiry_time
38 use lightning::ln::outbound_payment::Retry as RetryImport;
39 pub(crate) type nativeRetry = RetryImport;
43 pub(crate) fn to_native(&self) -> nativeRetry {
45 Retry::Attempts (ref a, ) => {
46 let mut a_nonref = Clone::clone(a);
47 nativeRetry::Attempts (
51 Retry::Timeout (ref a, ) => {
52 let mut a_nonref = Clone::clone(a);
53 nativeRetry::Timeout (
54 core::time::Duration::from_secs(a_nonref),
60 pub(crate) fn into_native(self) -> nativeRetry {
62 Retry::Attempts (mut a, ) => {
63 nativeRetry::Attempts (
67 Retry::Timeout (mut a, ) => {
68 nativeRetry::Timeout (
69 core::time::Duration::from_secs(a),
75 pub(crate) fn from_native(native: &nativeRetry) -> Self {
77 nativeRetry::Attempts (ref a, ) => {
78 let mut a_nonref = Clone::clone(a);
83 nativeRetry::Timeout (ref a, ) => {
84 let mut a_nonref = Clone::clone(a);
92 pub(crate) fn native_into(native: nativeRetry) -> Self {
94 nativeRetry::Attempts (mut a, ) => {
99 nativeRetry::Timeout (mut a, ) => {
107 /// Frees any resources used by the Retry
109 pub extern "C" fn Retry_free(this_ptr: Retry) { }
110 /// Creates a copy of the Retry
112 pub extern "C" fn Retry_clone(orig: &Retry) -> Retry {
116 /// Utility method to constructs a new Attempts-variant Retry
117 pub extern "C" fn Retry_attempts(a: usize) -> Retry {
121 /// Utility method to constructs a new Timeout-variant Retry
122 pub extern "C" fn Retry_timeout(a: u64) -> Retry {
125 /// Checks if two Retrys contain equal inner contents.
126 /// This ignores pointers and is_owned flags and looks at the values in fields.
128 pub extern "C" fn Retry_eq(a: &Retry, b: &Retry) -> bool {
129 if &a.to_native() == &b.to_native() { true } else { false }
131 /// Generates a non-cryptographic 64-bit hash of the Retry.
133 pub extern "C" fn Retry_hash(o: &Retry) -> u64 {
134 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
136 let mut hasher = core::hash::SipHasher::new();
137 core::hash::Hash::hash(&o.to_native(), &mut hasher);
138 core::hash::Hasher::finish(&hasher)
140 /// Indicates an immediate error on [`ChannelManager::send_payment`]. Further errors may be
141 /// surfaced later via [`Event::PaymentPathFailed`] and [`Event::PaymentFailed`].
143 /// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
144 /// [`Event::PaymentPathFailed`]: crate::events::Event::PaymentPathFailed
145 /// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
149 pub enum RetryableSendFailure {
150 /// The provided [`PaymentParameters::expiry_time`] indicated that the payment has expired. Note
151 /// that this error is *not* caused by [`Retry::Timeout`].
153 /// [`PaymentParameters::expiry_time`]: crate::routing::router::PaymentParameters::expiry_time
155 /// We were unable to find a route to the destination.
157 /// Indicates that a payment for the provided [`PaymentId`] is already in-flight and has not
158 /// yet completed (i.e. generated an [`Event::PaymentSent`] or [`Event::PaymentFailed`]).
160 /// [`PaymentId`]: crate::ln::channelmanager::PaymentId
161 /// [`Event::PaymentSent`]: crate::events::Event::PaymentSent
162 /// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
165 use lightning::ln::outbound_payment::RetryableSendFailure as RetryableSendFailureImport;
166 pub(crate) type nativeRetryableSendFailure = RetryableSendFailureImport;
168 impl RetryableSendFailure {
170 pub(crate) fn to_native(&self) -> nativeRetryableSendFailure {
172 RetryableSendFailure::PaymentExpired => nativeRetryableSendFailure::PaymentExpired,
173 RetryableSendFailure::RouteNotFound => nativeRetryableSendFailure::RouteNotFound,
174 RetryableSendFailure::DuplicatePayment => nativeRetryableSendFailure::DuplicatePayment,
178 pub(crate) fn into_native(self) -> nativeRetryableSendFailure {
180 RetryableSendFailure::PaymentExpired => nativeRetryableSendFailure::PaymentExpired,
181 RetryableSendFailure::RouteNotFound => nativeRetryableSendFailure::RouteNotFound,
182 RetryableSendFailure::DuplicatePayment => nativeRetryableSendFailure::DuplicatePayment,
186 pub(crate) fn from_native(native: &nativeRetryableSendFailure) -> Self {
188 nativeRetryableSendFailure::PaymentExpired => RetryableSendFailure::PaymentExpired,
189 nativeRetryableSendFailure::RouteNotFound => RetryableSendFailure::RouteNotFound,
190 nativeRetryableSendFailure::DuplicatePayment => RetryableSendFailure::DuplicatePayment,
194 pub(crate) fn native_into(native: nativeRetryableSendFailure) -> Self {
196 nativeRetryableSendFailure::PaymentExpired => RetryableSendFailure::PaymentExpired,
197 nativeRetryableSendFailure::RouteNotFound => RetryableSendFailure::RouteNotFound,
198 nativeRetryableSendFailure::DuplicatePayment => RetryableSendFailure::DuplicatePayment,
202 /// Creates a copy of the RetryableSendFailure
204 pub extern "C" fn RetryableSendFailure_clone(orig: &RetryableSendFailure) -> RetryableSendFailure {
208 /// Utility method to constructs a new PaymentExpired-variant RetryableSendFailure
209 pub extern "C" fn RetryableSendFailure_payment_expired() -> RetryableSendFailure {
210 RetryableSendFailure::PaymentExpired}
212 /// Utility method to constructs a new RouteNotFound-variant RetryableSendFailure
213 pub extern "C" fn RetryableSendFailure_route_not_found() -> RetryableSendFailure {
214 RetryableSendFailure::RouteNotFound}
216 /// Utility method to constructs a new DuplicatePayment-variant RetryableSendFailure
217 pub extern "C" fn RetryableSendFailure_duplicate_payment() -> RetryableSendFailure {
218 RetryableSendFailure::DuplicatePayment}
219 /// Checks if two RetryableSendFailures contain equal inner contents.
220 /// This ignores pointers and is_owned flags and looks at the values in fields.
222 pub extern "C" fn RetryableSendFailure_eq(a: &RetryableSendFailure, b: &RetryableSendFailure) -> bool {
223 if &a.to_native() == &b.to_native() { true } else { false }
225 /// If a payment fails to send with [`ChannelManager::send_payment_with_route`], it can be in one
226 /// of several states. This enum is returned as the Err() type describing which state the payment
227 /// is in, see the description of individual enum states for more.
229 /// [`ChannelManager::send_payment_with_route`]: crate::ln::channelmanager::ChannelManager::send_payment_with_route
233 pub enum PaymentSendFailure {
234 /// A parameter which was passed to send_payment was invalid, preventing us from attempting to
235 /// send the payment at all.
237 /// You can freely resend the payment in full (with the parameter error fixed).
239 /// Because the payment failed outright, no payment tracking is done and no
240 /// [`Event::PaymentPathFailed`] or [`Event::PaymentFailed`] events will be generated.
242 /// [`Event::PaymentPathFailed`]: crate::events::Event::PaymentPathFailed
243 /// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
245 crate::lightning::util::errors::APIError),
246 /// A parameter in a single path which was passed to send_payment was invalid, preventing us
247 /// from attempting to send the payment at all.
249 /// You can freely resend the payment in full (with the parameter error fixed).
251 /// Because the payment failed outright, no payment tracking is done and no
252 /// [`Event::PaymentPathFailed`] or [`Event::PaymentFailed`] events will be generated.
254 /// The results here are ordered the same as the paths in the route object which was passed to
257 /// [`Event::PaymentPathFailed`]: crate::events::Event::PaymentPathFailed
258 /// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
260 crate::c_types::derived::CVec_CResult_NoneAPIErrorZZ),
261 /// All paths which were attempted failed to send, with no channel state change taking place.
262 /// You can freely resend the payment in full (though you probably want to do so over different
263 /// paths than the ones selected).
265 /// Because the payment failed outright, no payment tracking is done and no
266 /// [`Event::PaymentPathFailed`] or [`Event::PaymentFailed`] events will be generated.
268 /// [`Event::PaymentPathFailed`]: crate::events::Event::PaymentPathFailed
269 /// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
271 crate::c_types::derived::CVec_APIErrorZ),
272 /// Indicates that a payment for the provided [`PaymentId`] is already in-flight and has not
273 /// yet completed (i.e. generated an [`Event::PaymentSent`] or [`Event::PaymentFailed`]).
275 /// [`PaymentId`]: crate::ln::channelmanager::PaymentId
276 /// [`Event::PaymentSent`]: crate::events::Event::PaymentSent
277 /// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
279 /// Some paths that were attempted failed to send, though some paths may have succeeded. At least
280 /// some paths have irrevocably committed to the HTLC.
282 /// The results here are ordered the same as the paths in the route object that was passed to
285 /// Any entries that contain `Err(APIError::MonitorUpdateInprogress)` will send once a
286 /// [`MonitorEvent::Completed`] is provided for the next-hop channel with the latest update_id.
288 /// [`MonitorEvent::Completed`]: crate::chain::channelmonitor::MonitorEvent::Completed
290 /// The errors themselves, in the same order as the paths from the route.
291 results: crate::c_types::derived::CVec_CResult_NoneAPIErrorZZ,
292 /// If some paths failed without irrevocably committing to the new HTLC(s), this will
293 /// contain a [`RouteParameters`] object for the failing paths.
295 /// Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None
296 failed_paths_retry: crate::lightning::routing::router::RouteParameters,
297 /// The payment id for the payment, which is now at least partially pending.
298 payment_id: crate::c_types::ThirtyTwoBytes,
301 use lightning::ln::outbound_payment::PaymentSendFailure as PaymentSendFailureImport;
302 pub(crate) type nativePaymentSendFailure = PaymentSendFailureImport;
304 impl PaymentSendFailure {
306 pub(crate) fn to_native(&self) -> nativePaymentSendFailure {
308 PaymentSendFailure::ParameterError (ref a, ) => {
309 let mut a_nonref = Clone::clone(a);
310 nativePaymentSendFailure::ParameterError (
311 a_nonref.into_native(),
314 PaymentSendFailure::PathParameterError (ref a, ) => {
315 let mut a_nonref = Clone::clone(a);
316 let mut local_a_nonref = Vec::new(); for mut item in a_nonref.into_rust().drain(..) { local_a_nonref.push( { let mut local_a_nonref_0 = match item.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut item.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut item.contents.err)) }).into_native() })}; local_a_nonref_0 }); };
317 nativePaymentSendFailure::PathParameterError (
321 PaymentSendFailure::AllFailedResendSafe (ref a, ) => {
322 let mut a_nonref = Clone::clone(a);
323 let mut local_a_nonref = Vec::new(); for mut item in a_nonref.into_rust().drain(..) { local_a_nonref.push( { item.into_native() }); };
324 nativePaymentSendFailure::AllFailedResendSafe (
328 PaymentSendFailure::DuplicatePayment => nativePaymentSendFailure::DuplicatePayment,
329 PaymentSendFailure::PartialFailure {ref results, ref failed_paths_retry, ref payment_id, } => {
330 let mut results_nonref = Clone::clone(results);
331 let mut local_results_nonref = Vec::new(); for mut item in results_nonref.into_rust().drain(..) { local_results_nonref.push( { let mut local_results_nonref_0 = match item.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut item.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut item.contents.err)) }).into_native() })}; local_results_nonref_0 }); };
332 let mut failed_paths_retry_nonref = Clone::clone(failed_paths_retry);
333 let mut local_failed_paths_retry_nonref = if failed_paths_retry_nonref.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(failed_paths_retry_nonref.take_inner()) } }) };
334 let mut payment_id_nonref = Clone::clone(payment_id);
335 nativePaymentSendFailure::PartialFailure {
336 results: local_results_nonref,
337 failed_paths_retry: local_failed_paths_retry_nonref,
338 payment_id: ::lightning::ln::channelmanager::PaymentId(payment_id_nonref.data),
344 pub(crate) fn into_native(self) -> nativePaymentSendFailure {
346 PaymentSendFailure::ParameterError (mut a, ) => {
347 nativePaymentSendFailure::ParameterError (
351 PaymentSendFailure::PathParameterError (mut a, ) => {
352 let mut local_a = Vec::new(); for mut item in a.into_rust().drain(..) { local_a.push( { let mut local_a_0 = match item.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut item.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut item.contents.err)) }).into_native() })}; local_a_0 }); };
353 nativePaymentSendFailure::PathParameterError (
357 PaymentSendFailure::AllFailedResendSafe (mut a, ) => {
358 let mut local_a = Vec::new(); for mut item in a.into_rust().drain(..) { local_a.push( { item.into_native() }); };
359 nativePaymentSendFailure::AllFailedResendSafe (
363 PaymentSendFailure::DuplicatePayment => nativePaymentSendFailure::DuplicatePayment,
364 PaymentSendFailure::PartialFailure {mut results, mut failed_paths_retry, mut payment_id, } => {
365 let mut local_results = Vec::new(); for mut item in results.into_rust().drain(..) { local_results.push( { let mut local_results_0 = match item.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut item.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut item.contents.err)) }).into_native() })}; local_results_0 }); };
366 let mut local_failed_paths_retry = if failed_paths_retry.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(failed_paths_retry.take_inner()) } }) };
367 nativePaymentSendFailure::PartialFailure {
368 results: local_results,
369 failed_paths_retry: local_failed_paths_retry,
370 payment_id: ::lightning::ln::channelmanager::PaymentId(payment_id.data),
376 pub(crate) fn from_native(native: &nativePaymentSendFailure) -> Self {
378 nativePaymentSendFailure::ParameterError (ref a, ) => {
379 let mut a_nonref = Clone::clone(a);
380 PaymentSendFailure::ParameterError (
381 crate::lightning::util::errors::APIError::native_into(a_nonref),
384 nativePaymentSendFailure::PathParameterError (ref a, ) => {
385 let mut a_nonref = Clone::clone(a);
386 let mut local_a_nonref = Vec::new(); for mut item in a_nonref.drain(..) { local_a_nonref.push( { let mut local_a_nonref_0 = match item { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::util::errors::APIError::native_into(e) }).into() }; local_a_nonref_0 }); };
387 PaymentSendFailure::PathParameterError (
388 local_a_nonref.into(),
391 nativePaymentSendFailure::AllFailedResendSafe (ref a, ) => {
392 let mut a_nonref = Clone::clone(a);
393 let mut local_a_nonref = Vec::new(); for mut item in a_nonref.drain(..) { local_a_nonref.push( { crate::lightning::util::errors::APIError::native_into(item) }); };
394 PaymentSendFailure::AllFailedResendSafe (
395 local_a_nonref.into(),
398 nativePaymentSendFailure::DuplicatePayment => PaymentSendFailure::DuplicatePayment,
399 nativePaymentSendFailure::PartialFailure {ref results, ref failed_paths_retry, ref payment_id, } => {
400 let mut results_nonref = Clone::clone(results);
401 let mut local_results_nonref = Vec::new(); for mut item in results_nonref.drain(..) { local_results_nonref.push( { let mut local_results_nonref_0 = match item { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::util::errors::APIError::native_into(e) }).into() }; local_results_nonref_0 }); };
402 let mut failed_paths_retry_nonref = Clone::clone(failed_paths_retry);
403 let mut local_failed_paths_retry_nonref = crate::lightning::routing::router::RouteParameters { inner: if failed_paths_retry_nonref.is_none() { core::ptr::null_mut() } else { { ObjOps::heap_alloc((failed_paths_retry_nonref.unwrap())) } }, is_owned: true };
404 let mut payment_id_nonref = Clone::clone(payment_id);
405 PaymentSendFailure::PartialFailure {
406 results: local_results_nonref.into(),
407 failed_paths_retry: local_failed_paths_retry_nonref,
408 payment_id: crate::c_types::ThirtyTwoBytes { data: payment_id_nonref.0 },
414 pub(crate) fn native_into(native: nativePaymentSendFailure) -> Self {
416 nativePaymentSendFailure::ParameterError (mut a, ) => {
417 PaymentSendFailure::ParameterError (
418 crate::lightning::util::errors::APIError::native_into(a),
421 nativePaymentSendFailure::PathParameterError (mut a, ) => {
422 let mut local_a = Vec::new(); for mut item in a.drain(..) { local_a.push( { let mut local_a_0 = match item { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::util::errors::APIError::native_into(e) }).into() }; local_a_0 }); };
423 PaymentSendFailure::PathParameterError (
427 nativePaymentSendFailure::AllFailedResendSafe (mut a, ) => {
428 let mut local_a = Vec::new(); for mut item in a.drain(..) { local_a.push( { crate::lightning::util::errors::APIError::native_into(item) }); };
429 PaymentSendFailure::AllFailedResendSafe (
433 nativePaymentSendFailure::DuplicatePayment => PaymentSendFailure::DuplicatePayment,
434 nativePaymentSendFailure::PartialFailure {mut results, mut failed_paths_retry, mut payment_id, } => {
435 let mut local_results = Vec::new(); for mut item in results.drain(..) { local_results.push( { let mut local_results_0 = match item { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::util::errors::APIError::native_into(e) }).into() }; local_results_0 }); };
436 let mut local_failed_paths_retry = crate::lightning::routing::router::RouteParameters { inner: if failed_paths_retry.is_none() { core::ptr::null_mut() } else { { ObjOps::heap_alloc((failed_paths_retry.unwrap())) } }, is_owned: true };
437 PaymentSendFailure::PartialFailure {
438 results: local_results.into(),
439 failed_paths_retry: local_failed_paths_retry,
440 payment_id: crate::c_types::ThirtyTwoBytes { data: payment_id.0 },
446 /// Frees any resources used by the PaymentSendFailure
448 pub extern "C" fn PaymentSendFailure_free(this_ptr: PaymentSendFailure) { }
449 /// Creates a copy of the PaymentSendFailure
451 pub extern "C" fn PaymentSendFailure_clone(orig: &PaymentSendFailure) -> PaymentSendFailure {
455 /// Utility method to constructs a new ParameterError-variant PaymentSendFailure
456 pub extern "C" fn PaymentSendFailure_parameter_error(a: crate::lightning::util::errors::APIError) -> PaymentSendFailure {
457 PaymentSendFailure::ParameterError(a, )
460 /// Utility method to constructs a new PathParameterError-variant PaymentSendFailure
461 pub extern "C" fn PaymentSendFailure_path_parameter_error(a: crate::c_types::derived::CVec_CResult_NoneAPIErrorZZ) -> PaymentSendFailure {
462 PaymentSendFailure::PathParameterError(a, )
465 /// Utility method to constructs a new AllFailedResendSafe-variant PaymentSendFailure
466 pub extern "C" fn PaymentSendFailure_all_failed_resend_safe(a: crate::c_types::derived::CVec_APIErrorZ) -> PaymentSendFailure {
467 PaymentSendFailure::AllFailedResendSafe(a, )
470 /// Utility method to constructs a new DuplicatePayment-variant PaymentSendFailure
471 pub extern "C" fn PaymentSendFailure_duplicate_payment() -> PaymentSendFailure {
472 PaymentSendFailure::DuplicatePayment}
474 /// Utility method to constructs a new PartialFailure-variant PaymentSendFailure
475 pub extern "C" fn PaymentSendFailure_partial_failure(results: crate::c_types::derived::CVec_CResult_NoneAPIErrorZZ, failed_paths_retry: crate::lightning::routing::router::RouteParameters, payment_id: crate::c_types::ThirtyTwoBytes) -> PaymentSendFailure {
476 PaymentSendFailure::PartialFailure {
483 use lightning::ln::outbound_payment::RecipientOnionFields as nativeRecipientOnionFieldsImport;
484 pub(crate) type nativeRecipientOnionFields = nativeRecipientOnionFieldsImport;
486 /// Information which is provided, encrypted, to the payment recipient when sending HTLCs.
488 /// This should generally be constructed with data communicated to us from the recipient (via a
489 /// BOLT11 or BOLT12 invoice).
492 pub struct RecipientOnionFields {
493 /// A pointer to the opaque Rust object.
495 /// Nearly everywhere, inner must be non-null, however in places where
496 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
497 pub inner: *mut nativeRecipientOnionFields,
498 /// Indicates that this is the only struct which contains the same pointer.
500 /// Rust functions which take ownership of an object provided via an argument require
501 /// this to be true and invalidate the object pointed to by inner.
505 impl Drop for RecipientOnionFields {
507 if self.is_owned && !<*mut nativeRecipientOnionFields>::is_null(self.inner) {
508 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
512 /// Frees any resources used by the RecipientOnionFields, if is_owned is set and inner is non-NULL.
514 pub extern "C" fn RecipientOnionFields_free(this_obj: RecipientOnionFields) { }
516 /// Used only if an object of this type is returned as a trait impl by a method
517 pub(crate) extern "C" fn RecipientOnionFields_free_void(this_ptr: *mut c_void) {
518 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeRecipientOnionFields) };
521 impl RecipientOnionFields {
522 pub(crate) fn get_native_ref(&self) -> &'static nativeRecipientOnionFields {
523 unsafe { &*ObjOps::untweak_ptr(self.inner) }
525 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRecipientOnionFields {
526 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
528 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
529 pub(crate) fn take_inner(mut self) -> *mut nativeRecipientOnionFields {
530 assert!(self.is_owned);
531 let ret = ObjOps::untweak_ptr(self.inner);
532 self.inner = core::ptr::null_mut();
536 /// The [`PaymentSecret`] is an arbitrary 32 bytes provided by the recipient for us to repeat
537 /// in the onion. It is unrelated to `payment_hash` (or [`PaymentPreimage`]) and exists to
538 /// authenticate the sender to the recipient and prevent payment-probing (deanonymization)
541 /// If you do not have one, the [`Route`] you pay over must not contain multiple paths as
542 /// multi-path payments require a recipient-provided secret.
544 /// Some implementations may reject spontaneous payments with payment secrets, so you may only
545 /// want to provide a secret for a spontaneous payment if MPP is needed and you know your
546 /// recipient will not reject it.
548 pub extern "C" fn RecipientOnionFields_get_payment_secret(this_ptr: &RecipientOnionFields) -> crate::c_types::derived::COption_PaymentSecretZ {
549 let mut inner_val = &mut this_ptr.get_native_mut_ref().payment_secret;
550 let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_PaymentSecretZ::None } else { crate::c_types::derived::COption_PaymentSecretZ::Some(/* WARNING: CLONING CONVERSION HERE! &Option<Enum> is otherwise un-expressable. */ { crate::c_types::ThirtyTwoBytes { data: (*inner_val.as_ref().unwrap()).clone().0 } }) };
553 /// The [`PaymentSecret`] is an arbitrary 32 bytes provided by the recipient for us to repeat
554 /// in the onion. It is unrelated to `payment_hash` (or [`PaymentPreimage`]) and exists to
555 /// authenticate the sender to the recipient and prevent payment-probing (deanonymization)
558 /// If you do not have one, the [`Route`] you pay over must not contain multiple paths as
559 /// multi-path payments require a recipient-provided secret.
561 /// Some implementations may reject spontaneous payments with payment secrets, so you may only
562 /// want to provide a secret for a spontaneous payment if MPP is needed and you know your
563 /// recipient will not reject it.
565 pub extern "C" fn RecipientOnionFields_set_payment_secret(this_ptr: &mut RecipientOnionFields, mut val: crate::c_types::derived::COption_PaymentSecretZ) {
566 let mut local_val = { /*val*/ let val_opt = val; if val_opt.is_none() { None } else { Some({ { ::lightning::ln::PaymentSecret({ val_opt.take() }.data) }})} };
567 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.payment_secret = local_val;
569 /// The payment metadata serves a similar purpose as [`Self::payment_secret`] but is of
570 /// arbitrary length. This gives recipients substantially more flexibility to receive
573 /// In LDK, while the [`Self::payment_secret`] is fixed based on an internal authentication
574 /// scheme to authenticate received payments against expected payments and invoices, this field
575 /// is not used in LDK for received payments, and can be used to store arbitrary data in
576 /// invoices which will be received with the payment.
578 /// Note that this field was added to the lightning specification more recently than
579 /// [`Self::payment_secret`] and while nearly all lightning senders support secrets, metadata
580 /// may not be supported as universally.
582 /// Returns a copy of the field.
584 pub extern "C" fn RecipientOnionFields_get_payment_metadata(this_ptr: &RecipientOnionFields) -> crate::c_types::derived::COption_CVec_u8ZZ {
585 let mut inner_val = this_ptr.get_native_mut_ref().payment_metadata.clone();
586 let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_CVec_u8ZZ::None } else { crate::c_types::derived::COption_CVec_u8ZZ::Some( { let mut local_inner_val_0 = Vec::new(); for mut item in inner_val.unwrap().drain(..) { local_inner_val_0.push( { item }); }; local_inner_val_0.into() }) };
589 /// The payment metadata serves a similar purpose as [`Self::payment_secret`] but is of
590 /// arbitrary length. This gives recipients substantially more flexibility to receive
593 /// In LDK, while the [`Self::payment_secret`] is fixed based on an internal authentication
594 /// scheme to authenticate received payments against expected payments and invoices, this field
595 /// is not used in LDK for received payments, and can be used to store arbitrary data in
596 /// invoices which will be received with the payment.
598 /// Note that this field was added to the lightning specification more recently than
599 /// [`Self::payment_secret`] and while nearly all lightning senders support secrets, metadata
600 /// may not be supported as universally.
602 pub extern "C" fn RecipientOnionFields_set_payment_metadata(this_ptr: &mut RecipientOnionFields, mut val: crate::c_types::derived::COption_CVec_u8ZZ) {
603 let mut local_val = { /*val*/ let val_opt = val; if val_opt.is_none() { None } else { Some({ { let mut local_val_0 = Vec::new(); for mut item in { val_opt.take() }.into_rust().drain(..) { local_val_0.push( { item }); }; local_val_0 }})} };
604 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.payment_metadata = local_val;
606 /// Constructs a new RecipientOnionFields given each field
609 pub extern "C" fn RecipientOnionFields_new(mut payment_secret_arg: crate::c_types::derived::COption_PaymentSecretZ, mut payment_metadata_arg: crate::c_types::derived::COption_CVec_u8ZZ) -> RecipientOnionFields {
610 let mut local_payment_secret_arg = { /*payment_secret_arg*/ let payment_secret_arg_opt = payment_secret_arg; if payment_secret_arg_opt.is_none() { None } else { Some({ { ::lightning::ln::PaymentSecret({ payment_secret_arg_opt.take() }.data) }})} };
611 let mut local_payment_metadata_arg = { /*payment_metadata_arg*/ let payment_metadata_arg_opt = payment_metadata_arg; if payment_metadata_arg_opt.is_none() { None } else { Some({ { let mut local_payment_metadata_arg_0 = Vec::new(); for mut item in { payment_metadata_arg_opt.take() }.into_rust().drain(..) { local_payment_metadata_arg_0.push( { item }); }; local_payment_metadata_arg_0 }})} };
612 RecipientOnionFields { inner: ObjOps::heap_alloc(nativeRecipientOnionFields {
613 payment_secret: local_payment_secret_arg,
614 payment_metadata: local_payment_metadata_arg,
617 impl Clone for RecipientOnionFields {
618 fn clone(&self) -> Self {
620 inner: if <*mut nativeRecipientOnionFields>::is_null(self.inner) { core::ptr::null_mut() } else {
621 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
627 /// Used only if an object of this type is returned as a trait impl by a method
628 pub(crate) extern "C" fn RecipientOnionFields_clone_void(this_ptr: *const c_void) -> *mut c_void {
629 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRecipientOnionFields)).clone() })) as *mut c_void
632 /// Creates a copy of the RecipientOnionFields
633 pub extern "C" fn RecipientOnionFields_clone(orig: &RecipientOnionFields) -> RecipientOnionFields {
636 /// Checks if two RecipientOnionFieldss contain equal inner contents.
637 /// This ignores pointers and is_owned flags and looks at the values in fields.
638 /// Two objects with NULL inner values will be considered "equal" here.
640 pub extern "C" fn RecipientOnionFields_eq(a: &RecipientOnionFields, b: &RecipientOnionFields) -> bool {
641 if a.inner == b.inner { return true; }
642 if a.inner.is_null() || b.inner.is_null() { return false; }
643 if a.get_native_ref() == b.get_native_ref() { true } else { false }
646 /// Serialize the RecipientOnionFields object into a byte array which can be read by RecipientOnionFields_read
647 pub extern "C" fn RecipientOnionFields_write(obj: &crate::lightning::ln::outbound_payment::RecipientOnionFields) -> crate::c_types::derived::CVec_u8Z {
648 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
651 pub(crate) extern "C" fn RecipientOnionFields_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
652 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeRecipientOnionFields) })
655 /// Read a RecipientOnionFields from a byte array, created by RecipientOnionFields_write
656 pub extern "C" fn RecipientOnionFields_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_RecipientOnionFieldsDecodeErrorZ {
657 let res: Result<lightning::ln::outbound_payment::RecipientOnionFields, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
658 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::outbound_payment::RecipientOnionFields { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError::native_into(e) }).into() };
661 /// Creates a [`RecipientOnionFields`] from only a [`PaymentSecret`]. This is the most common
662 /// set of onion fields for today's BOLT11 invoices - most nodes require a [`PaymentSecret`]
663 /// but do not require or provide any further data.
666 pub extern "C" fn RecipientOnionFields_secret_only(mut payment_secret: crate::c_types::ThirtyTwoBytes) -> crate::lightning::ln::outbound_payment::RecipientOnionFields {
667 let mut ret = lightning::ln::outbound_payment::RecipientOnionFields::secret_only(::lightning::ln::PaymentSecret(payment_secret.data));
668 crate::lightning::ln::outbound_payment::RecipientOnionFields { inner: ObjOps::heap_alloc(ret), is_owned: true }
671 /// Creates a new [`RecipientOnionFields`] with no fields. This generally does not create
672 /// payable HTLCs except for single-path spontaneous payments, i.e. this should generally
673 /// only be used for calls to [`ChannelManager::send_spontaneous_payment`]. If you are sending
674 /// a spontaneous MPP this will not work as all MPP require payment secrets; you may
675 /// instead want to use [`RecipientOnionFields::secret_only`].
677 /// [`ChannelManager::send_spontaneous_payment`]: super::channelmanager::ChannelManager::send_spontaneous_payment
678 /// [`RecipientOnionFields::secret_only`]: RecipientOnionFields::secret_only
681 pub extern "C" fn RecipientOnionFields_spontaneous_empty() -> crate::lightning::ln::outbound_payment::RecipientOnionFields {
682 let mut ret = lightning::ln::outbound_payment::RecipientOnionFields::spontaneous_empty();
683 crate::lightning::ln::outbound_payment::RecipientOnionFields { inner: ObjOps::heap_alloc(ret), is_owned: true }