X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning_invoice%2Fmod.rs;h=f91c4a63482156f0421168116cc7460b2a9dbda7;hb=a21177846ed75d66f68a1603f768410f07a65791;hp=84f5ae3146daf8670cb6194be1d9914282699bbe;hpb=ae39d0e5a34ef021db50b12a7be96f0694fd1b18;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 84f5ae3..f91c4a6 100644 --- a/lightning-c-bindings/src/lightning_invoice/mod.rs +++ b/lightning-c-bindings/src/lightning_invoice/mod.rs @@ -485,6 +485,12 @@ 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. @@ -586,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 @@ -692,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; @@ -775,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; @@ -869,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; @@ -959,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; @@ -1043,6 +1095,16 @@ 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 #[derive(Clone)] #[must_use] @@ -1125,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] @@ -1945,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] @@ -2069,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 } @@ -2100,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 } } @@ -2225,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] @@ -2237,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] @@ -2474,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 {