X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning_invoice%2Fmod.rs;h=4b12f320c9fe1c94ad7b5bcd2f5f6efddafacd95;hb=11b997c3a0452ea1da5b7b352e7887798105db29;hp=e8840bf21198196bc73bb6eaf92de91a7f1851b0;hpb=3fafbe3b1625eb163737b705bac7e2f911b53bdb;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/lightning_invoice/mod.rs b/lightning-c-bindings/src/lightning_invoice/mod.rs index e8840bf..4b12f32 100644 --- a/lightning-c-bindings/src/lightning_invoice/mod.rs +++ b/lightning-c-bindings/src/lightning_invoice/mod.rs @@ -7,7 +7,7 @@ // source was automatically generated. //! This crate provides data structures to represent -//! [lightning BOLT11](https://github.com/lightningnetwork/lightning-rfc/blob/master/11-payment-encoding.md) +//! [lightning BOLT11](https://github.com/lightning/bolts/blob/master/11-payment-encoding.md) //! invoices and functions to create, encode and decode these. If you just want to use the standard //! en-/decoding functionality this should get you started: //! @@ -26,6 +26,17 @@ use alloc::{vec::Vec, boxed::Box}; pub mod payment; pub mod utils; pub mod constants; +mod time_utils { + +use alloc::str::FromStr; +use core::ffi::c_void; +use core::convert::Infallible; +use bitcoin::hashes::Hash; +use crate::c_types::*; +#[cfg(feature="no-std")] +use alloc::{vec::Vec, boxed::Box}; + +} mod de { use alloc::str::FromStr; @@ -172,50 +183,57 @@ use alloc::{vec::Vec, boxed::Box}; } /// Errors that indicate what is wrong with the invoice. They have some granularity for debug /// reasons, but should generally result in an \"invalid BOLT11 invoice\" message for the user. -#[must_use] #[derive(Clone)] +#[must_use] #[repr(C)] pub enum ParseError { - Bech32Error(crate::c_types::Bech32Error), - ParseAmountError(crate::c_types::Error), - MalformedSignature(crate::c_types::Secp256k1Error), + Bech32Error( + crate::c_types::Bech32Error), + ParseAmountError( + crate::c_types::Error), + MalformedSignature( + crate::c_types::Secp256k1Error), BadPrefix, UnknownCurrency, UnknownSiPrefix, MalformedHRP, TooShortDataPart, UnexpectedEndOfTaggedFields, - DescriptionDecodeError(crate::c_types::Error), + DescriptionDecodeError( + crate::c_types::Error), PaddingError, IntegerOverflowError, InvalidSegWitProgramLength, InvalidPubKeyHashLength, InvalidScriptHashLength, InvalidRecoveryId, - InvalidSliceLength(crate::c_types::Str), + InvalidSliceLength( + crate::c_types::Str), /// Not an error, but used internally to signal that a part of the invoice should be ignored /// according to BOLT11 Skip, } -use lightning_invoice::ParseError as nativeParseError; +use lightning_invoice::ParseError as ParseErrorImport; +pub(crate) type nativeParseError = ParseErrorImport; + impl ParseError { #[allow(unused)] pub(crate) fn to_native(&self) -> nativeParseError { match self { ParseError::Bech32Error (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); nativeParseError::Bech32Error ( a_nonref.into_rust(), ) }, ParseError::ParseAmountError (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); nativeParseError::ParseAmountError ( u8::from_str_radix(" a", 10).unwrap_err() /*a_nonref*/, ) }, ParseError::MalformedSignature (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); nativeParseError::MalformedSignature ( a_nonref.into_rust(), ) @@ -227,7 +245,7 @@ impl ParseError { ParseError::TooShortDataPart => nativeParseError::TooShortDataPart, ParseError::UnexpectedEndOfTaggedFields => nativeParseError::UnexpectedEndOfTaggedFields, ParseError::DescriptionDecodeError (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); nativeParseError::DescriptionDecodeError ( core::str::from_utf8(&[0xff]).unwrap_err() /*a_nonref*/, ) @@ -239,7 +257,7 @@ impl ParseError { ParseError::InvalidScriptHashLength => nativeParseError::InvalidScriptHashLength, ParseError::InvalidRecoveryId => nativeParseError::InvalidRecoveryId, ParseError::InvalidSliceLength (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); nativeParseError::InvalidSliceLength ( a_nonref.into_string(), ) @@ -294,19 +312,19 @@ impl ParseError { pub(crate) fn from_native(native: &nativeParseError) -> Self { match native { nativeParseError::Bech32Error (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); ParseError::Bech32Error ( crate::c_types::Bech32Error::from_rust(a_nonref), ) }, nativeParseError::ParseAmountError (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); ParseError::ParseAmountError ( crate::c_types::Error { _dummy: 0 } /*a_nonref*/, ) }, nativeParseError::MalformedSignature (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); ParseError::MalformedSignature ( crate::c_types::Secp256k1Error::from_rust(a_nonref), ) @@ -318,7 +336,7 @@ impl ParseError { nativeParseError::TooShortDataPart => ParseError::TooShortDataPart, nativeParseError::UnexpectedEndOfTaggedFields => ParseError::UnexpectedEndOfTaggedFields, nativeParseError::DescriptionDecodeError (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); ParseError::DescriptionDecodeError ( crate::c_types::Error { _dummy: 0 } /*a_nonref*/, ) @@ -330,7 +348,7 @@ impl ParseError { nativeParseError::InvalidScriptHashLength => ParseError::InvalidScriptHashLength, nativeParseError::InvalidRecoveryId => ParseError::InvalidRecoveryId, nativeParseError::InvalidSliceLength (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); ParseError::InvalidSliceLength ( a_nonref.into(), ) @@ -467,31 +485,41 @@ pub extern "C" fn ParseError_invalid_slice_length(a: crate::c_types::Str) -> Par /// Utility method to constructs a new Skip-variant ParseError pub extern "C" fn ParseError_skip() -> ParseError { ParseError::Skip} +/// Checks if two ParseErrors contain equal inner contents. +/// This ignores pointers and is_owned flags and looks at the values in fields. +#[no_mangle] +pub extern "C" fn ParseError_eq(a: &ParseError, b: &ParseError) -> bool { + if &a.to_native() == &b.to_native() { true } else { false } +} /// Indicates that something went wrong while parsing or validating the invoice. Parsing errors /// should be mostly seen as opaque and are only there for debugging reasons. Semantic errors /// like wrong signatures, missing fields etc. could mean that someone tampered with the invoice. -#[must_use] #[derive(Clone)] +#[must_use] #[repr(C)] pub enum ParseOrSemanticError { /// The invoice couldn't be decoded - ParseError(crate::lightning_invoice::ParseError), + ParseError( + crate::lightning_invoice::ParseError), /// The invoice could be decoded but violates the BOLT11 standard - SemanticError(crate::lightning_invoice::SemanticError), + SemanticError( + crate::lightning_invoice::SemanticError), } -use lightning_invoice::ParseOrSemanticError as nativeParseOrSemanticError; +use lightning_invoice::ParseOrSemanticError as ParseOrSemanticErrorImport; +pub(crate) type nativeParseOrSemanticError = ParseOrSemanticErrorImport; + impl ParseOrSemanticError { #[allow(unused)] pub(crate) fn to_native(&self) -> nativeParseOrSemanticError { match self { ParseOrSemanticError::ParseError (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); nativeParseOrSemanticError::ParseError ( a_nonref.into_native(), ) }, ParseOrSemanticError::SemanticError (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); nativeParseOrSemanticError::SemanticError ( a_nonref.into_native(), ) @@ -517,13 +545,13 @@ impl ParseOrSemanticError { pub(crate) fn from_native(native: &nativeParseOrSemanticError) -> Self { match native { nativeParseOrSemanticError::ParseError (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); ParseOrSemanticError::ParseError ( crate::lightning_invoice::ParseError::native_into(a_nonref), ) }, nativeParseOrSemanticError::SemanticError (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); ParseOrSemanticError::SemanticError ( crate::lightning_invoice::SemanticError::native_into(a_nonref), ) @@ -564,6 +592,12 @@ pub extern "C" fn ParseOrSemanticError_parse_error(a: crate::lightning_invoice:: pub extern "C" fn ParseOrSemanticError_semantic_error(a: crate::lightning_invoice::SemanticError) -> ParseOrSemanticError { ParseOrSemanticError::SemanticError(a, ) } +/// Checks if two ParseOrSemanticErrors contain equal inner contents. +/// This ignores pointers and is_owned flags and looks at the values in fields. +#[no_mangle] +pub extern "C" fn ParseOrSemanticError_eq(a: &ParseOrSemanticError, b: &ParseOrSemanticError) -> bool { + if &a.to_native() == &b.to_native() { true } else { false } +} /// The maximum timestamp as [`Duration::as_secs`] since the Unix epoch allowed by [`BOLT 11`]. /// /// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md @@ -579,13 +613,13 @@ pub static DEFAULT_EXPIRY_TIME: u64 = lightning_invoice::DEFAULT_EXPIRY_TIME; /// Default minimum final CLTV expiry as defined by [BOLT 11]. /// /// Note that this is *not* the same value as rust-lightning's minimum CLTV expiry, which is -/// provided in [`MIN_FINAL_CLTV_EXPIRY`]. +/// provided in [`MIN_FINAL_CLTV_EXPIRY_DELTA`]. /// /// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md -/// [`MIN_FINAL_CLTV_EXPIRY`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY +/// [`MIN_FINAL_CLTV_EXPIRY_DELTA`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA #[no_mangle] -pub static DEFAULT_MIN_FINAL_CLTV_EXPIRY: u64 = lightning_invoice::DEFAULT_MIN_FINAL_CLTV_EXPIRY; +pub static DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA: u64 = lightning_invoice::DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA; use lightning_invoice::Invoice as nativeInvoiceImport; pub(crate) type nativeInvoice = nativeInvoiceImport; @@ -624,7 +658,7 @@ pub extern "C" fn Invoice_free(this_obj: Invoice) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn Invoice_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeInvoice); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeInvoice) }; } #[allow(unused)] impl Invoice { @@ -670,6 +704,16 @@ pub(crate) extern "C" fn Invoice_clone_void(this_ptr: *const c_void) -> *mut c_v pub extern "C" fn Invoice_clone(orig: &Invoice) -> Invoice { orig.clone() } +/// Checks if two Invoices contain equal inner contents. +#[no_mangle] +pub extern "C" fn Invoice_hash(o: &Invoice) -> u64 { + if o.inner.is_null() { return 0; } + // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core + #[allow(deprecated)] + let mut hasher = core::hash::SipHasher::new(); + core::hash::Hash::hash(o.get_native_ref(), &mut hasher); + core::hash::Hasher::finish(&hasher) +} use lightning_invoice::SignedRawInvoice as nativeSignedRawInvoiceImport; pub(crate) type nativeSignedRawInvoice = nativeSignedRawInvoiceImport; @@ -707,7 +751,7 @@ pub extern "C" fn SignedRawInvoice_free(this_obj: SignedRawInvoice) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn SignedRawInvoice_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeSignedRawInvoice); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeSignedRawInvoice) }; } #[allow(unused)] impl SignedRawInvoice { @@ -753,6 +797,16 @@ pub(crate) extern "C" fn SignedRawInvoice_clone_void(this_ptr: *const c_void) -> pub extern "C" fn SignedRawInvoice_clone(orig: &SignedRawInvoice) -> SignedRawInvoice { orig.clone() } +/// Checks if two SignedRawInvoices contain equal inner contents. +#[no_mangle] +pub extern "C" fn SignedRawInvoice_hash(o: &SignedRawInvoice) -> u64 { + if o.inner.is_null() { return 0; } + // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core + #[allow(deprecated)] + let mut hasher = core::hash::SipHasher::new(); + core::hash::Hash::hash(o.get_native_ref(), &mut hasher); + core::hash::Hasher::finish(&hasher) +} use lightning_invoice::RawInvoice as nativeRawInvoiceImport; pub(crate) type nativeRawInvoice = nativeRawInvoiceImport; @@ -790,7 +844,7 @@ pub extern "C" fn RawInvoice_free(this_obj: RawInvoice) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn RawInvoice_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRawInvoice); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeRawInvoice) }; } #[allow(unused)] impl RawInvoice { @@ -847,6 +901,16 @@ pub(crate) extern "C" fn RawInvoice_clone_void(this_ptr: *const c_void) -> *mut pub extern "C" fn RawInvoice_clone(orig: &RawInvoice) -> RawInvoice { orig.clone() } +/// Checks if two RawInvoices contain equal inner contents. +#[no_mangle] +pub extern "C" fn RawInvoice_hash(o: &RawInvoice) -> u64 { + if o.inner.is_null() { return 0; } + // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core + #[allow(deprecated)] + let mut hasher = core::hash::SipHasher::new(); + core::hash::Hash::hash(o.get_native_ref(), &mut hasher); + core::hash::Hasher::finish(&hasher) +} use lightning_invoice::RawDataPart as nativeRawDataPartImport; pub(crate) type nativeRawDataPart = nativeRawDataPartImport; @@ -880,7 +944,7 @@ pub extern "C" fn RawDataPart_free(this_obj: RawDataPart) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn RawDataPart_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRawDataPart); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeRawDataPart) }; } #[allow(unused)] impl RawDataPart { @@ -937,6 +1001,16 @@ pub(crate) extern "C" fn RawDataPart_clone_void(this_ptr: *const c_void) -> *mut pub extern "C" fn RawDataPart_clone(orig: &RawDataPart) -> RawDataPart { orig.clone() } +/// Checks if two RawDataParts contain equal inner contents. +#[no_mangle] +pub extern "C" fn RawDataPart_hash(o: &RawDataPart) -> u64 { + if o.inner.is_null() { return 0; } + // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core + #[allow(deprecated)] + let mut hasher = core::hash::SipHasher::new(); + core::hash::Hash::hash(o.get_native_ref(), &mut hasher); + core::hash::Hasher::finish(&hasher) +} use lightning_invoice::PositiveTimestamp as nativePositiveTimestampImport; pub(crate) type nativePositiveTimestamp = nativePositiveTimestampImport; @@ -975,7 +1049,7 @@ pub extern "C" fn PositiveTimestamp_free(this_obj: PositiveTimestamp) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn PositiveTimestamp_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativePositiveTimestamp); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativePositiveTimestamp) }; } #[allow(unused)] impl PositiveTimestamp { @@ -1021,9 +1095,19 @@ pub(crate) extern "C" fn PositiveTimestamp_clone_void(this_ptr: *const c_void) - pub extern "C" fn PositiveTimestamp_clone(orig: &PositiveTimestamp) -> PositiveTimestamp { orig.clone() } +/// Checks if two PositiveTimestamps contain equal inner contents. +#[no_mangle] +pub extern "C" fn PositiveTimestamp_hash(o: &PositiveTimestamp) -> u64 { + if o.inner.is_null() { return 0; } + // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core + #[allow(deprecated)] + let mut hasher = core::hash::SipHasher::new(); + core::hash::Hash::hash(o.get_native_ref(), &mut hasher); + core::hash::Hasher::finish(&hasher) +} /// SI prefixes for the human readable part -#[must_use] #[derive(Clone)] +#[must_use] #[repr(C)] pub enum SiPrefix { /// 10^-3 @@ -1035,7 +1119,9 @@ pub enum SiPrefix { /// 10^-12 Pico, } -use lightning_invoice::SiPrefix as nativeSiPrefix; +use lightning_invoice::SiPrefix as SiPrefixImport; +pub(crate) type nativeSiPrefix = SiPrefixImport; + impl SiPrefix { #[allow(unused)] pub(crate) fn to_native(&self) -> nativeSiPrefix { @@ -1101,6 +1187,15 @@ pub extern "C" fn SiPrefix_pico() -> SiPrefix { pub extern "C" fn SiPrefix_eq(a: &SiPrefix, b: &SiPrefix) -> bool { if &a.to_native() == &b.to_native() { true } else { false } } +/// Checks if two SiPrefixs contain equal inner contents. +#[no_mangle] +pub extern "C" fn SiPrefix_hash(o: &SiPrefix) -> u64 { + // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core + #[allow(deprecated)] + let mut hasher = core::hash::SipHasher::new(); + core::hash::Hash::hash(&o.to_native(), &mut hasher); + core::hash::Hasher::finish(&hasher) +} /// Returns the multiplier to go from a BTC value to picoBTC implied by this SiPrefix. /// This is effectively 10^12 * the prefix multiplier #[must_use] @@ -1111,8 +1206,8 @@ pub extern "C" fn SiPrefix_multiplier(this_arg: &crate::lightning_invoice::SiPre } /// Enum representing the crypto currencies (or networks) supported by this library -#[must_use] #[derive(Clone)] +#[must_use] #[repr(C)] pub enum Currency { /// Bitcoin mainnet @@ -1126,7 +1221,9 @@ pub enum Currency { /// Bitcoin signet Signet, } -use lightning_invoice::Currency as nativeCurrency; +use lightning_invoice::Currency as CurrencyImport; +pub(crate) type nativeCurrency = CurrencyImport; + impl Currency { #[allow(unused)] pub(crate) fn to_native(&self) -> nativeCurrency { @@ -1242,7 +1339,7 @@ pub extern "C" fn Sha256_free(this_obj: Sha256) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn Sha256_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeSha256); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeSha256) }; } #[allow(unused)] impl Sha256 { @@ -1334,7 +1431,7 @@ pub extern "C" fn Description_free(this_obj: Description) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn Description_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeDescription); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeDescription) }; } #[allow(unused)] impl Description { @@ -1423,7 +1520,7 @@ pub extern "C" fn PayeePubKey_free(this_obj: PayeePubKey) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn PayeePubKey_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativePayeePubKey); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativePayeePubKey) }; } #[allow(unused)] impl PayeePubKey { @@ -1530,7 +1627,7 @@ pub extern "C" fn ExpiryTime_free(this_obj: ExpiryTime) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn ExpiryTime_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeExpiryTime); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeExpiryTime) }; } #[allow(unused)] impl ExpiryTime { @@ -1587,18 +1684,18 @@ pub extern "C" fn ExpiryTime_eq(a: &ExpiryTime, b: &ExpiryTime) -> bool { if a.get_native_ref() == b.get_native_ref() { true } else { false } } -use lightning_invoice::MinFinalCltvExpiry as nativeMinFinalCltvExpiryImport; -pub(crate) type nativeMinFinalCltvExpiry = nativeMinFinalCltvExpiryImport; +use lightning_invoice::MinFinalCltvExpiryDelta as nativeMinFinalCltvExpiryDeltaImport; +pub(crate) type nativeMinFinalCltvExpiryDelta = nativeMinFinalCltvExpiryDeltaImport; -/// `min_final_cltv_expiry` to use for the last HTLC in the route +/// `min_final_cltv_expiry_delta` to use for the last HTLC in the route #[must_use] #[repr(C)] -pub struct MinFinalCltvExpiry { +pub struct MinFinalCltvExpiryDelta { /// A pointer to the opaque Rust object. /// Nearly everywhere, inner must be non-null, however in places where /// the Rust equivalent takes an Option, it may be set to null to indicate None. - pub inner: *mut nativeMinFinalCltvExpiry, + pub inner: *mut nativeMinFinalCltvExpiryDelta, /// Indicates that this is the only struct which contains the same pointer. /// Rust functions which take ownership of an object provided via an argument require @@ -1606,31 +1703,31 @@ pub struct MinFinalCltvExpiry { pub is_owned: bool, } -impl Drop for MinFinalCltvExpiry { +impl Drop for MinFinalCltvExpiryDelta { fn drop(&mut self) { - if self.is_owned && !<*mut nativeMinFinalCltvExpiry>::is_null(self.inner) { + if self.is_owned && !<*mut nativeMinFinalCltvExpiryDelta>::is_null(self.inner) { let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) }; } } } -/// Frees any resources used by the MinFinalCltvExpiry, if is_owned is set and inner is non-NULL. +/// Frees any resources used by the MinFinalCltvExpiryDelta, if is_owned is set and inner is non-NULL. #[no_mangle] -pub extern "C" fn MinFinalCltvExpiry_free(this_obj: MinFinalCltvExpiry) { } +pub extern "C" fn MinFinalCltvExpiryDelta_free(this_obj: MinFinalCltvExpiryDelta) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -pub(crate) extern "C" fn MinFinalCltvExpiry_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeMinFinalCltvExpiry); } +pub(crate) extern "C" fn MinFinalCltvExpiryDelta_free_void(this_ptr: *mut c_void) { + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeMinFinalCltvExpiryDelta) }; } #[allow(unused)] -impl MinFinalCltvExpiry { - pub(crate) fn get_native_ref(&self) -> &'static nativeMinFinalCltvExpiry { +impl MinFinalCltvExpiryDelta { + pub(crate) fn get_native_ref(&self) -> &'static nativeMinFinalCltvExpiryDelta { unsafe { &*ObjOps::untweak_ptr(self.inner) } } - pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeMinFinalCltvExpiry { + pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeMinFinalCltvExpiryDelta { unsafe { &mut *ObjOps::untweak_ptr(self.inner) } } /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy - pub(crate) fn take_inner(mut self) -> *mut nativeMinFinalCltvExpiry { + pub(crate) fn take_inner(mut self) -> *mut nativeMinFinalCltvExpiryDelta { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); self.inner = core::ptr::null_mut(); @@ -1638,26 +1735,26 @@ impl MinFinalCltvExpiry { } } #[no_mangle] -pub extern "C" fn MinFinalCltvExpiry_get_a(this_ptr: &MinFinalCltvExpiry) -> u64 { +pub extern "C" fn MinFinalCltvExpiryDelta_get_a(this_ptr: &MinFinalCltvExpiryDelta) -> u64 { let mut inner_val = &mut this_ptr.get_native_mut_ref().0; *inner_val } #[no_mangle] -pub extern "C" fn MinFinalCltvExpiry_set_a(this_ptr: &mut MinFinalCltvExpiry, mut val: u64) { +pub extern "C" fn MinFinalCltvExpiryDelta_set_a(this_ptr: &mut MinFinalCltvExpiryDelta, mut val: u64) { unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val; } -/// Constructs a new MinFinalCltvExpiry given each field +/// Constructs a new MinFinalCltvExpiryDelta given each field #[must_use] #[no_mangle] -pub extern "C" fn MinFinalCltvExpiry_new(mut a_arg: u64) -> MinFinalCltvExpiry { - MinFinalCltvExpiry { inner: ObjOps::heap_alloc(lightning_invoice::MinFinalCltvExpiry ( +pub extern "C" fn MinFinalCltvExpiryDelta_new(mut a_arg: u64) -> MinFinalCltvExpiryDelta { + MinFinalCltvExpiryDelta { inner: ObjOps::heap_alloc(lightning_invoice::MinFinalCltvExpiryDelta ( a_arg, )), is_owned: true } } -impl Clone for MinFinalCltvExpiry { +impl Clone for MinFinalCltvExpiryDelta { fn clone(&self) -> Self { Self { - inner: if <*mut nativeMinFinalCltvExpiry>::is_null(self.inner) { core::ptr::null_mut() } else { + inner: if <*mut nativeMinFinalCltvExpiryDelta>::is_null(self.inner) { core::ptr::null_mut() } else { ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } @@ -1665,17 +1762,17 @@ impl Clone for MinFinalCltvExpiry { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -pub(crate) extern "C" fn MinFinalCltvExpiry_clone_void(this_ptr: *const c_void) -> *mut c_void { - Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeMinFinalCltvExpiry)).clone() })) as *mut c_void +pub(crate) extern "C" fn MinFinalCltvExpiryDelta_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeMinFinalCltvExpiryDelta)).clone() })) as *mut c_void } #[no_mangle] -/// Creates a copy of the MinFinalCltvExpiry -pub extern "C" fn MinFinalCltvExpiry_clone(orig: &MinFinalCltvExpiry) -> MinFinalCltvExpiry { +/// Creates a copy of the MinFinalCltvExpiryDelta +pub extern "C" fn MinFinalCltvExpiryDelta_clone(orig: &MinFinalCltvExpiryDelta) -> MinFinalCltvExpiryDelta { orig.clone() } -/// Checks if two MinFinalCltvExpirys contain equal inner contents. +/// Checks if two MinFinalCltvExpiryDeltas contain equal inner contents. #[no_mangle] -pub extern "C" fn MinFinalCltvExpiry_hash(o: &MinFinalCltvExpiry) -> u64 { +pub extern "C" fn MinFinalCltvExpiryDelta_hash(o: &MinFinalCltvExpiryDelta) -> u64 { if o.inner.is_null() { return 0; } // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core #[allow(deprecated)] @@ -1683,35 +1780,39 @@ pub extern "C" fn MinFinalCltvExpiry_hash(o: &MinFinalCltvExpiry) -> u64 { core::hash::Hash::hash(o.get_native_ref(), &mut hasher); core::hash::Hasher::finish(&hasher) } -/// Checks if two MinFinalCltvExpirys contain equal inner contents. +/// Checks if two MinFinalCltvExpiryDeltas contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. /// Two objects with NULL inner values will be considered "equal" here. #[no_mangle] -pub extern "C" fn MinFinalCltvExpiry_eq(a: &MinFinalCltvExpiry, b: &MinFinalCltvExpiry) -> bool { +pub extern "C" fn MinFinalCltvExpiryDelta_eq(a: &MinFinalCltvExpiryDelta, b: &MinFinalCltvExpiryDelta) -> bool { if a.inner == b.inner { return true; } if a.inner.is_null() || b.inner.is_null() { return false; } if a.get_native_ref() == b.get_native_ref() { true } else { false } } /// Fallback address in case no LN payment is possible -#[must_use] #[derive(Clone)] +#[must_use] #[repr(C)] pub enum Fallback { SegWitProgram { - version: crate::c_types::u5, + version: crate::c_types::U5, program: crate::c_types::derived::CVec_u8Z, }, - PubKeyHash(crate::c_types::TwentyBytes), - ScriptHash(crate::c_types::TwentyBytes), + PubKeyHash( + crate::c_types::TwentyBytes), + ScriptHash( + crate::c_types::TwentyBytes), } -use lightning_invoice::Fallback as nativeFallback; +use lightning_invoice::Fallback as FallbackImport; +pub(crate) type nativeFallback = FallbackImport; + impl Fallback { #[allow(unused)] pub(crate) fn to_native(&self) -> nativeFallback { match self { Fallback::SegWitProgram {ref version, ref program, } => { - let mut version_nonref = (*version).clone(); - let mut program_nonref = (*program).clone(); + let mut version_nonref = Clone::clone(version); + let mut program_nonref = Clone::clone(program); let mut local_program_nonref = Vec::new(); for mut item in program_nonref.into_rust().drain(..) { local_program_nonref.push( { item }); }; nativeFallback::SegWitProgram { version: version_nonref.into(), @@ -1719,13 +1820,13 @@ impl Fallback { } }, Fallback::PubKeyHash (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); nativeFallback::PubKeyHash ( a_nonref.data, ) }, Fallback::ScriptHash (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); nativeFallback::ScriptHash ( a_nonref.data, ) @@ -1758,8 +1859,8 @@ impl Fallback { pub(crate) fn from_native(native: &nativeFallback) -> Self { match native { nativeFallback::SegWitProgram {ref version, ref program, } => { - let mut version_nonref = (*version).clone(); - let mut program_nonref = (*program).clone(); + let mut version_nonref = Clone::clone(version); + let mut program_nonref = Clone::clone(program); let mut local_program_nonref = Vec::new(); for mut item in program_nonref.drain(..) { local_program_nonref.push( { item }); }; Fallback::SegWitProgram { version: version_nonref.into(), @@ -1767,13 +1868,13 @@ impl Fallback { } }, nativeFallback::PubKeyHash (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); Fallback::PubKeyHash ( crate::c_types::TwentyBytes { data: a_nonref }, ) }, nativeFallback::ScriptHash (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); Fallback::ScriptHash ( crate::c_types::TwentyBytes { data: a_nonref }, ) @@ -1813,7 +1914,7 @@ pub extern "C" fn Fallback_clone(orig: &Fallback) -> Fallback { } #[no_mangle] /// Utility method to constructs a new SegWitProgram-variant Fallback -pub extern "C" fn Fallback_seg_wit_program(version: crate::c_types::u5, program: crate::c_types::derived::CVec_u8Z) -> Fallback { +pub extern "C" fn Fallback_seg_wit_program(version: crate::c_types::U5, program: crate::c_types::derived::CVec_u8Z) -> Fallback { Fallback::SegWitProgram { version, program, @@ -1877,7 +1978,7 @@ pub extern "C" fn InvoiceSignature_free(this_obj: InvoiceSignature) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn InvoiceSignature_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeInvoiceSignature); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeInvoiceSignature) }; } #[allow(unused)] impl InvoiceSignature { @@ -1915,6 +2016,16 @@ pub extern "C" fn InvoiceSignature_clone(orig: &InvoiceSignature) -> InvoiceSign orig.clone() } /// Checks if two InvoiceSignatures contain equal inner contents. +#[no_mangle] +pub extern "C" fn InvoiceSignature_hash(o: &InvoiceSignature) -> u64 { + if o.inner.is_null() { return 0; } + // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core + #[allow(deprecated)] + let mut hasher = core::hash::SipHasher::new(); + core::hash::Hash::hash(o.get_native_ref(), &mut hasher); + core::hash::Hasher::finish(&hasher) +} +/// Checks if two InvoiceSignatures contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. /// Two objects with NULL inner values will be considered "equal" here. #[no_mangle] @@ -1960,7 +2071,7 @@ pub extern "C" fn PrivateRoute_free(this_obj: PrivateRoute) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn PrivateRoute_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativePrivateRoute); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativePrivateRoute) }; } #[allow(unused)] impl PrivateRoute { @@ -2039,8 +2150,8 @@ pub extern "C" fn SignedRawInvoice_raw_invoice(this_arg: &crate::lightning_invoi /// The hash of the `RawInvoice` that was signed. #[must_use] #[no_mangle] -pub extern "C" fn SignedRawInvoice_hash(this_arg: &crate::lightning_invoice::SignedRawInvoice) -> *const [u8; 32] { - let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.hash(); +pub extern "C" fn SignedRawInvoice_signable_hash(this_arg: &crate::lightning_invoice::SignedRawInvoice) -> *const [u8; 32] { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.signable_hash(); ret } @@ -2070,11 +2181,11 @@ pub extern "C" fn SignedRawInvoice_check_signature(this_arg: &crate::lightning_i ret } -/// Calculate the hash of the encoded `RawInvoice` +/// Calculate the hash of the encoded `RawInvoice` which should be signed. #[must_use] #[no_mangle] -pub extern "C" fn RawInvoice_hash(this_arg: &crate::lightning_invoice::RawInvoice) -> crate::c_types::ThirtyTwoBytes { - let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.hash(); +pub extern "C" fn RawInvoice_signable_hash(this_arg: &crate::lightning_invoice::RawInvoice) -> crate::c_types::ThirtyTwoBytes { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.signable_hash(); crate::c_types::ThirtyTwoBytes { data: ret } } @@ -2132,9 +2243,9 @@ pub extern "C" fn RawInvoice_expiry_time(this_arg: &crate::lightning_invoice::Ra /// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None #[must_use] #[no_mangle] -pub extern "C" fn RawInvoice_min_final_cltv_expiry(this_arg: &crate::lightning_invoice::RawInvoice) -> crate::lightning_invoice::MinFinalCltvExpiry { - let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.min_final_cltv_expiry(); - let mut local_ret = crate::lightning_invoice::MinFinalCltvExpiry { inner: unsafe { (if ret.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning_invoice::MinFinalCltvExpiry<>) as *mut _ }, is_owned: false }; +pub extern "C" fn RawInvoice_min_final_cltv_expiry_delta(this_arg: &crate::lightning_invoice::RawInvoice) -> crate::lightning_invoice::MinFinalCltvExpiryDelta { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.min_final_cltv_expiry_delta(); + let mut local_ret = crate::lightning_invoice::MinFinalCltvExpiryDelta { inner: unsafe { (if ret.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning_invoice::MinFinalCltvExpiryDelta<>) as *mut _ }, is_owned: false }; local_ret } @@ -2195,6 +2306,8 @@ pub extern "C" fn PositiveTimestamp_from_unix_timestamp(mut unix_seconds: u64) - /// Creates a `PositiveTimestamp` from a [`SystemTime`] with a corresponding Unix timestamp in /// the range `0..=MAX_TIMESTAMP`. /// +/// Note that the subsecond part is dropped as it is not representable in BOLT 11 invoices. +/// /// Otherwise, returns a [`CreationError::TimestampOutOfBounds`]. #[must_use] #[no_mangle] @@ -2207,6 +2320,8 @@ pub extern "C" fn PositiveTimestamp_from_system_time(mut time: u64) -> crate::c_ /// Creates a `PositiveTimestamp` from a [`Duration`] since the Unix epoch in the range /// `0..=MAX_TIMESTAMP`. /// +/// Note that the subsecond part is dropped as it is not representable in BOLT 11 invoices. +/// /// Otherwise, returns a [`CreationError::TimestampOutOfBounds`]. #[must_use] #[no_mangle] @@ -2372,12 +2487,12 @@ pub extern "C" fn Invoice_would_expire(this_arg: &crate::lightning_invoice::Invo ret } -/// Returns the invoice's `min_final_cltv_expiry` time, if present, otherwise -/// [`DEFAULT_MIN_FINAL_CLTV_EXPIRY`]. +/// Returns the invoice's `min_final_cltv_expiry_delta` time, if present, otherwise +/// [`DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA`]. #[must_use] #[no_mangle] -pub extern "C" fn Invoice_min_final_cltv_expiry(this_arg: &crate::lightning_invoice::Invoice) -> u64 { - let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.min_final_cltv_expiry(); +pub extern "C" fn Invoice_min_final_cltv_expiry_delta(this_arg: &crate::lightning_invoice::Invoice) -> u64 { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.min_final_cltv_expiry_delta(); ret } @@ -2444,7 +2559,7 @@ pub extern "C" fn ExpiryTime_from_seconds(mut seconds: u64) -> crate::lightning_ crate::lightning_invoice::ExpiryTime { inner: ObjOps::heap_alloc(ret), is_owned: true } } -/// Construct an `ExpiryTime` from a `Duration`. +/// Construct an `ExpiryTime` from a `Duration`, dropping the sub-second part. #[must_use] #[no_mangle] pub extern "C" fn ExpiryTime_from_duration(mut duration: u64) -> crate::lightning_invoice::ExpiryTime { @@ -2486,8 +2601,8 @@ pub extern "C" fn PrivateRoute_into_inner(mut this_arg: crate::lightning_invoice } /// Errors that may occur when constructing a new `RawInvoice` or `Invoice` -#[must_use] #[derive(Clone)] +#[must_use] #[repr(C)] pub enum CreationError { /// The supplied description string was longer than 639 __bytes__ (see [`Description::new(…)`](./struct.Description.html#method.new)) @@ -2503,8 +2618,14 @@ pub enum CreationError { /// /// [phantom invoices]: crate::utils::create_phantom_invoice MissingRouteHints, + /// The provided `min_final_cltv_expiry_delta` was less than [`MIN_FINAL_CLTV_EXPIRY_DELTA`]. + /// + /// [`MIN_FINAL_CLTV_EXPIRY_DELTA`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA + MinFinalCltvExpiryDeltaTooShort, } -use lightning_invoice::CreationError as nativeCreationError; +use lightning_invoice::CreationError as CreationErrorImport; +pub(crate) type nativeCreationError = CreationErrorImport; + impl CreationError { #[allow(unused)] pub(crate) fn to_native(&self) -> nativeCreationError { @@ -2514,6 +2635,7 @@ impl CreationError { CreationError::TimestampOutOfBounds => nativeCreationError::TimestampOutOfBounds, CreationError::InvalidAmount => nativeCreationError::InvalidAmount, CreationError::MissingRouteHints => nativeCreationError::MissingRouteHints, + CreationError::MinFinalCltvExpiryDeltaTooShort => nativeCreationError::MinFinalCltvExpiryDeltaTooShort, } } #[allow(unused)] @@ -2524,6 +2646,7 @@ impl CreationError { CreationError::TimestampOutOfBounds => nativeCreationError::TimestampOutOfBounds, CreationError::InvalidAmount => nativeCreationError::InvalidAmount, CreationError::MissingRouteHints => nativeCreationError::MissingRouteHints, + CreationError::MinFinalCltvExpiryDeltaTooShort => nativeCreationError::MinFinalCltvExpiryDeltaTooShort, } } #[allow(unused)] @@ -2534,6 +2657,7 @@ impl CreationError { nativeCreationError::TimestampOutOfBounds => CreationError::TimestampOutOfBounds, nativeCreationError::InvalidAmount => CreationError::InvalidAmount, nativeCreationError::MissingRouteHints => CreationError::MissingRouteHints, + nativeCreationError::MinFinalCltvExpiryDeltaTooShort => CreationError::MinFinalCltvExpiryDeltaTooShort, } } #[allow(unused)] @@ -2544,6 +2668,7 @@ impl CreationError { nativeCreationError::TimestampOutOfBounds => CreationError::TimestampOutOfBounds, nativeCreationError::InvalidAmount => CreationError::InvalidAmount, nativeCreationError::MissingRouteHints => CreationError::MissingRouteHints, + nativeCreationError::MinFinalCltvExpiryDeltaTooShort => CreationError::MinFinalCltvExpiryDeltaTooShort, } } } @@ -2572,6 +2697,10 @@ pub extern "C" fn CreationError_invalid_amount() -> CreationError { /// Utility method to constructs a new MissingRouteHints-variant CreationError pub extern "C" fn CreationError_missing_route_hints() -> CreationError { CreationError::MissingRouteHints} +#[no_mangle] +/// Utility method to constructs a new MinFinalCltvExpiryDeltaTooShort-variant CreationError +pub extern "C" fn CreationError_min_final_cltv_expiry_delta_too_short() -> CreationError { + CreationError::MinFinalCltvExpiryDeltaTooShort} /// Checks if two CreationErrors contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. #[no_mangle] @@ -2585,8 +2714,8 @@ pub extern "C" fn CreationError_to_str(o: &crate::lightning_invoice::CreationErr } /// Errors that may occur when converting a `RawInvoice` to an `Invoice`. They relate to the /// requirements sections in BOLT #11 -#[must_use] #[derive(Clone)] +#[must_use] #[repr(C)] pub enum SemanticError { /// The invoice is missing the mandatory payment hash @@ -2611,7 +2740,9 @@ pub enum SemanticError { /// The invoice's amount was not a whole number of millisatoshis ImpreciseAmount, } -use lightning_invoice::SemanticError as nativeSemanticError; +use lightning_invoice::SemanticError as SemanticErrorImport; +pub(crate) type nativeSemanticError = SemanticErrorImport; + impl SemanticError { #[allow(unused)] pub(crate) fn to_native(&self) -> nativeSemanticError { @@ -2732,16 +2863,19 @@ pub extern "C" fn SemanticError_to_str(o: &crate::lightning_invoice::SemanticErr } /// When signing using a fallible method either an user-supplied `SignError` or a `CreationError` /// may occur. -#[must_use] #[derive(Clone)] +#[must_use] #[repr(C)] pub enum SignOrCreationError { /// An error occurred during signing SignError, /// An error occurred while building the transaction - CreationError(crate::lightning_invoice::CreationError), + CreationError( + crate::lightning_invoice::CreationError), } -use lightning_invoice::SignOrCreationError as nativeSignOrCreationError; +use lightning_invoice::SignOrCreationError as SignOrCreationErrorImport; +pub(crate) type nativeSignOrCreationError = SignOrCreationErrorImport<>; + impl SignOrCreationError { #[allow(unused)] pub(crate) fn to_native(&self) -> nativeSignOrCreationError { @@ -2752,7 +2886,7 @@ impl SignOrCreationError { ) }, SignOrCreationError::CreationError (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); nativeSignOrCreationError::CreationError ( a_nonref.into_native(), ) @@ -2780,7 +2914,7 @@ impl SignOrCreationError { nativeSignOrCreationError::SignError (ref a, ) => { SignOrCreationError::SignError }, nativeSignOrCreationError::CreationError (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); SignOrCreationError::CreationError ( crate::lightning_invoice::CreationError::native_into(a_nonref), )