X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-c-bindings;a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning_invoice%2Fmod.rs;h=4afdd97ce0f8d46734a9baaaad527c3ac519b1b6;hp=53314331cf26b146aa5665b794bb1808cf1d2822;hb=4e715a943a67a3c846cc8af185b59cb24a4e8ae2;hpb=5271372bfe3ddc870c806f7b7ee13c4a7660e7e5 diff --git a/lightning-c-bindings/src/lightning_invoice/mod.rs b/lightning-c-bindings/src/lightning_invoice/mod.rs index 5331433..4afdd97 100644 --- a/lightning-c-bindings/src/lightning_invoice/mod.rs +++ b/lightning-c-bindings/src/lightning_invoice/mod.rs @@ -15,110 +15,564 @@ //! * For constructing invoices use the `InvoiceBuilder` //! * For serializing invoices use the `Display`/`ToString` traits -use std::str::FromStr; -use std::ffi::c_void; +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}; pub mod payment; pub mod utils; pub mod constants; mod de { -use std::str::FromStr; -use std::ffi::c_void; +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 hrp_sm { -use std::str::FromStr; -use std::ffi::c_void; +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}; } #[no_mangle] /// Read a SiPrefix object from a string -pub extern "C" fn SiPrefix_from_str(s: crate::c_types::Str) -> crate::c_types::derived::CResult_SiPrefixNoneZ { +pub extern "C" fn SiPrefix_from_str(s: crate::c_types::Str) -> crate::c_types::derived::CResult_SiPrefixParseErrorZ { match lightning_invoice::SiPrefix::from_str(s.into_str()) { Ok(r) => { crate::c_types::CResultTempl::ok( crate::lightning_invoice::SiPrefix::native_into(r) ) }, - Err(e) => crate::c_types::CResultTempl::err(()), + Err(e) => { + crate::c_types::CResultTempl::err( + crate::lightning_invoice::ParseError::native_into(e) + ) + }, }.into() } #[no_mangle] /// Read a Invoice object from a string -pub extern "C" fn Invoice_from_str(s: crate::c_types::Str) -> crate::c_types::derived::CResult_InvoiceNoneZ { +pub extern "C" fn Invoice_from_str(s: crate::c_types::Str) -> crate::c_types::derived::CResult_InvoiceParseOrSemanticErrorZ { match lightning_invoice::Invoice::from_str(s.into_str()) { Ok(r) => { crate::c_types::CResultTempl::ok( crate::lightning_invoice::Invoice { inner: ObjOps::heap_alloc(r), is_owned: true } ) }, - Err(e) => crate::c_types::CResultTempl::err(()), + Err(e) => { + crate::c_types::CResultTempl::err( + crate::lightning_invoice::ParseOrSemanticError::native_into(e) + ) + }, }.into() } #[no_mangle] /// Read a SignedRawInvoice object from a string -pub extern "C" fn SignedRawInvoice_from_str(s: crate::c_types::Str) -> crate::c_types::derived::CResult_SignedRawInvoiceNoneZ { +pub extern "C" fn SignedRawInvoice_from_str(s: crate::c_types::Str) -> crate::c_types::derived::CResult_SignedRawInvoiceParseErrorZ { match lightning_invoice::SignedRawInvoice::from_str(s.into_str()) { Ok(r) => { crate::c_types::CResultTempl::ok( crate::lightning_invoice::SignedRawInvoice { inner: ObjOps::heap_alloc(r), is_owned: true } ) }, - Err(e) => crate::c_types::CResultTempl::err(()), + Err(e) => { + crate::c_types::CResultTempl::err( + crate::lightning_invoice::ParseError::native_into(e) + ) + }, }.into() } +#[no_mangle] +/// Get the string representation of a ParseError object +pub extern "C" fn ParseError_to_str(o: &crate::lightning_invoice::ParseError) -> Str { + alloc::format!("{}", &o.to_native()).into() +} +#[no_mangle] +/// Get the string representation of a ParseOrSemanticError object +pub extern "C" fn ParseOrSemanticError_to_str(o: &crate::lightning_invoice::ParseOrSemanticError) -> Str { + alloc::format!("{}", &o.to_native()).into() +} } mod ser { -use std::str::FromStr; -use std::ffi::c_void; +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}; #[no_mangle] /// Get the string representation of a Invoice object pub extern "C" fn Invoice_to_str(o: &crate::lightning_invoice::Invoice) -> Str { - format!("{}", o.get_native_ref()).into() + alloc::format!("{}", o.get_native_ref()).into() } #[no_mangle] /// Get the string representation of a SignedRawInvoice object pub extern "C" fn SignedRawInvoice_to_str(o: &crate::lightning_invoice::SignedRawInvoice) -> Str { - format!("{}", o.get_native_ref()).into() + alloc::format!("{}", o.get_native_ref()).into() } #[no_mangle] /// Get the string representation of a Currency object pub extern "C" fn Currency_to_str(o: &crate::lightning_invoice::Currency) -> Str { - format!("{}", &o.to_native()).into() + alloc::format!("{}", &o.to_native()).into() } #[no_mangle] /// Get the string representation of a SiPrefix object pub extern "C" fn SiPrefix_to_str(o: &crate::lightning_invoice::SiPrefix) -> Str { - format!("{}", &o.to_native()).into() + alloc::format!("{}", &o.to_native()).into() } } mod tb { -use std::str::FromStr; -use std::ffi::c_void; +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 prelude { + +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 sync { + +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}; + +} +/// 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)] +#[repr(C)] +pub enum ParseError { + 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), + PaddingError, + IntegerOverflowError, + InvalidSegWitProgramLength, + InvalidPubKeyHashLength, + InvalidScriptHashLength, + InvalidRecoveryId, + 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; +impl ParseError { + #[allow(unused)] + pub(crate) fn to_native(&self) -> nativeParseError { + match self { + ParseError::Bech32Error (ref a, ) => { + let mut a_nonref = (*a).clone(); + nativeParseError::Bech32Error ( + a_nonref.into_rust(), + ) + }, + ParseError::ParseAmountError (ref a, ) => { + let mut a_nonref = (*a).clone(); + nativeParseError::ParseAmountError ( + u8::from_str_radix(" a", 10).unwrap_err() /*a_nonref*/, + ) + }, + ParseError::MalformedSignature (ref a, ) => { + let mut a_nonref = (*a).clone(); + nativeParseError::MalformedSignature ( + a_nonref.into_rust(), + ) + }, + ParseError::BadPrefix => nativeParseError::BadPrefix, + ParseError::UnknownCurrency => nativeParseError::UnknownCurrency, + ParseError::UnknownSiPrefix => nativeParseError::UnknownSiPrefix, + ParseError::MalformedHRP => nativeParseError::MalformedHRP, + ParseError::TooShortDataPart => nativeParseError::TooShortDataPart, + ParseError::UnexpectedEndOfTaggedFields => nativeParseError::UnexpectedEndOfTaggedFields, + ParseError::DescriptionDecodeError (ref a, ) => { + let mut a_nonref = (*a).clone(); + nativeParseError::DescriptionDecodeError ( + core::str::from_utf8(&[0xff]).unwrap_err() /*a_nonref*/, + ) + }, + ParseError::PaddingError => nativeParseError::PaddingError, + ParseError::IntegerOverflowError => nativeParseError::IntegerOverflowError, + ParseError::InvalidSegWitProgramLength => nativeParseError::InvalidSegWitProgramLength, + ParseError::InvalidPubKeyHashLength => nativeParseError::InvalidPubKeyHashLength, + ParseError::InvalidScriptHashLength => nativeParseError::InvalidScriptHashLength, + ParseError::InvalidRecoveryId => nativeParseError::InvalidRecoveryId, + ParseError::InvalidSliceLength (ref a, ) => { + let mut a_nonref = (*a).clone(); + nativeParseError::InvalidSliceLength ( + a_nonref.into_string(), + ) + }, + ParseError::Skip => nativeParseError::Skip, + } + } + #[allow(unused)] + pub(crate) fn into_native(self) -> nativeParseError { + match self { + ParseError::Bech32Error (mut a, ) => { + nativeParseError::Bech32Error ( + a.into_rust(), + ) + }, + ParseError::ParseAmountError (mut a, ) => { + nativeParseError::ParseAmountError ( + u8::from_str_radix(" a", 10).unwrap_err() /*a*/, + ) + }, + ParseError::MalformedSignature (mut a, ) => { + nativeParseError::MalformedSignature ( + a.into_rust(), + ) + }, + ParseError::BadPrefix => nativeParseError::BadPrefix, + ParseError::UnknownCurrency => nativeParseError::UnknownCurrency, + ParseError::UnknownSiPrefix => nativeParseError::UnknownSiPrefix, + ParseError::MalformedHRP => nativeParseError::MalformedHRP, + ParseError::TooShortDataPart => nativeParseError::TooShortDataPart, + ParseError::UnexpectedEndOfTaggedFields => nativeParseError::UnexpectedEndOfTaggedFields, + ParseError::DescriptionDecodeError (mut a, ) => { + nativeParseError::DescriptionDecodeError ( + core::str::from_utf8(&[0xff]).unwrap_err() /*a*/, + ) + }, + ParseError::PaddingError => nativeParseError::PaddingError, + ParseError::IntegerOverflowError => nativeParseError::IntegerOverflowError, + ParseError::InvalidSegWitProgramLength => nativeParseError::InvalidSegWitProgramLength, + ParseError::InvalidPubKeyHashLength => nativeParseError::InvalidPubKeyHashLength, + ParseError::InvalidScriptHashLength => nativeParseError::InvalidScriptHashLength, + ParseError::InvalidRecoveryId => nativeParseError::InvalidRecoveryId, + ParseError::InvalidSliceLength (mut a, ) => { + nativeParseError::InvalidSliceLength ( + a.into_string(), + ) + }, + ParseError::Skip => nativeParseError::Skip, + } + } + #[allow(unused)] + pub(crate) fn from_native(native: &nativeParseError) -> Self { + match native { + nativeParseError::Bech32Error (ref a, ) => { + let mut a_nonref = (*a).clone(); + ParseError::Bech32Error ( + crate::c_types::Bech32Error::from_rust(a_nonref), + ) + }, + nativeParseError::ParseAmountError (ref a, ) => { + let mut a_nonref = (*a).clone(); + ParseError::ParseAmountError ( + crate::c_types::Error { _dummy: 0 } /*a_nonref*/, + ) + }, + nativeParseError::MalformedSignature (ref a, ) => { + let mut a_nonref = (*a).clone(); + ParseError::MalformedSignature ( + crate::c_types::Secp256k1Error::from_rust(a_nonref), + ) + }, + nativeParseError::BadPrefix => ParseError::BadPrefix, + nativeParseError::UnknownCurrency => ParseError::UnknownCurrency, + nativeParseError::UnknownSiPrefix => ParseError::UnknownSiPrefix, + nativeParseError::MalformedHRP => ParseError::MalformedHRP, + nativeParseError::TooShortDataPart => ParseError::TooShortDataPart, + nativeParseError::UnexpectedEndOfTaggedFields => ParseError::UnexpectedEndOfTaggedFields, + nativeParseError::DescriptionDecodeError (ref a, ) => { + let mut a_nonref = (*a).clone(); + ParseError::DescriptionDecodeError ( + crate::c_types::Error { _dummy: 0 } /*a_nonref*/, + ) + }, + nativeParseError::PaddingError => ParseError::PaddingError, + nativeParseError::IntegerOverflowError => ParseError::IntegerOverflowError, + nativeParseError::InvalidSegWitProgramLength => ParseError::InvalidSegWitProgramLength, + nativeParseError::InvalidPubKeyHashLength => ParseError::InvalidPubKeyHashLength, + nativeParseError::InvalidScriptHashLength => ParseError::InvalidScriptHashLength, + nativeParseError::InvalidRecoveryId => ParseError::InvalidRecoveryId, + nativeParseError::InvalidSliceLength (ref a, ) => { + let mut a_nonref = (*a).clone(); + ParseError::InvalidSliceLength ( + a_nonref.into(), + ) + }, + nativeParseError::Skip => ParseError::Skip, + } + } + #[allow(unused)] + pub(crate) fn native_into(native: nativeParseError) -> Self { + match native { + nativeParseError::Bech32Error (mut a, ) => { + ParseError::Bech32Error ( + crate::c_types::Bech32Error::from_rust(a), + ) + }, + nativeParseError::ParseAmountError (mut a, ) => { + ParseError::ParseAmountError ( + crate::c_types::Error { _dummy: 0 } /*a*/, + ) + }, + nativeParseError::MalformedSignature (mut a, ) => { + ParseError::MalformedSignature ( + crate::c_types::Secp256k1Error::from_rust(a), + ) + }, + nativeParseError::BadPrefix => ParseError::BadPrefix, + nativeParseError::UnknownCurrency => ParseError::UnknownCurrency, + nativeParseError::UnknownSiPrefix => ParseError::UnknownSiPrefix, + nativeParseError::MalformedHRP => ParseError::MalformedHRP, + nativeParseError::TooShortDataPart => ParseError::TooShortDataPart, + nativeParseError::UnexpectedEndOfTaggedFields => ParseError::UnexpectedEndOfTaggedFields, + nativeParseError::DescriptionDecodeError (mut a, ) => { + ParseError::DescriptionDecodeError ( + crate::c_types::Error { _dummy: 0 } /*a*/, + ) + }, + nativeParseError::PaddingError => ParseError::PaddingError, + nativeParseError::IntegerOverflowError => ParseError::IntegerOverflowError, + nativeParseError::InvalidSegWitProgramLength => ParseError::InvalidSegWitProgramLength, + nativeParseError::InvalidPubKeyHashLength => ParseError::InvalidPubKeyHashLength, + nativeParseError::InvalidScriptHashLength => ParseError::InvalidScriptHashLength, + nativeParseError::InvalidRecoveryId => ParseError::InvalidRecoveryId, + nativeParseError::InvalidSliceLength (mut a, ) => { + ParseError::InvalidSliceLength ( + a.into(), + ) + }, + nativeParseError::Skip => ParseError::Skip, + } + } +} +/// Frees any resources used by the ParseError +#[no_mangle] +pub extern "C" fn ParseError_free(this_ptr: ParseError) { } +/// Creates a copy of the ParseError +#[no_mangle] +pub extern "C" fn ParseError_clone(orig: &ParseError) -> ParseError { + orig.clone() +} +#[no_mangle] +/// Utility method to constructs a new Bech32Error-variant ParseError +pub extern "C" fn ParseError_bech32_error(a: crate::c_types::Bech32Error) -> ParseError { + ParseError::Bech32Error(a, ) +} +#[no_mangle] +/// Utility method to constructs a new ParseAmountError-variant ParseError +pub extern "C" fn ParseError_parse_amount_error(a: crate::c_types::Error) -> ParseError { + ParseError::ParseAmountError(a, ) +} +#[no_mangle] +/// Utility method to constructs a new MalformedSignature-variant ParseError +pub extern "C" fn ParseError_malformed_signature(a: crate::c_types::Secp256k1Error) -> ParseError { + ParseError::MalformedSignature(a, ) +} +#[no_mangle] +/// Utility method to constructs a new BadPrefix-variant ParseError +pub extern "C" fn ParseError_bad_prefix() -> ParseError { + ParseError::BadPrefix} +#[no_mangle] +/// Utility method to constructs a new UnknownCurrency-variant ParseError +pub extern "C" fn ParseError_unknown_currency() -> ParseError { + ParseError::UnknownCurrency} +#[no_mangle] +/// Utility method to constructs a new UnknownSiPrefix-variant ParseError +pub extern "C" fn ParseError_unknown_si_prefix() -> ParseError { + ParseError::UnknownSiPrefix} +#[no_mangle] +/// Utility method to constructs a new MalformedHRP-variant ParseError +pub extern "C" fn ParseError_malformed_hrp() -> ParseError { + ParseError::MalformedHRP} +#[no_mangle] +/// Utility method to constructs a new TooShortDataPart-variant ParseError +pub extern "C" fn ParseError_too_short_data_part() -> ParseError { + ParseError::TooShortDataPart} +#[no_mangle] +/// Utility method to constructs a new UnexpectedEndOfTaggedFields-variant ParseError +pub extern "C" fn ParseError_unexpected_end_of_tagged_fields() -> ParseError { + ParseError::UnexpectedEndOfTaggedFields} +#[no_mangle] +/// Utility method to constructs a new DescriptionDecodeError-variant ParseError +pub extern "C" fn ParseError_description_decode_error(a: crate::c_types::Error) -> ParseError { + ParseError::DescriptionDecodeError(a, ) +} +#[no_mangle] +/// Utility method to constructs a new PaddingError-variant ParseError +pub extern "C" fn ParseError_padding_error() -> ParseError { + ParseError::PaddingError} +#[no_mangle] +/// Utility method to constructs a new IntegerOverflowError-variant ParseError +pub extern "C" fn ParseError_integer_overflow_error() -> ParseError { + ParseError::IntegerOverflowError} +#[no_mangle] +/// Utility method to constructs a new InvalidSegWitProgramLength-variant ParseError +pub extern "C" fn ParseError_invalid_seg_wit_program_length() -> ParseError { + ParseError::InvalidSegWitProgramLength} +#[no_mangle] +/// Utility method to constructs a new InvalidPubKeyHashLength-variant ParseError +pub extern "C" fn ParseError_invalid_pub_key_hash_length() -> ParseError { + ParseError::InvalidPubKeyHashLength} +#[no_mangle] +/// Utility method to constructs a new InvalidScriptHashLength-variant ParseError +pub extern "C" fn ParseError_invalid_script_hash_length() -> ParseError { + ParseError::InvalidScriptHashLength} +#[no_mangle] +/// Utility method to constructs a new InvalidRecoveryId-variant ParseError +pub extern "C" fn ParseError_invalid_recovery_id() -> ParseError { + ParseError::InvalidRecoveryId} +#[no_mangle] +/// Utility method to constructs a new InvalidSliceLength-variant ParseError +pub extern "C" fn ParseError_invalid_slice_length(a: crate::c_types::Str) -> ParseError { + ParseError::InvalidSliceLength(a, ) +} +#[no_mangle] +/// Utility method to constructs a new Skip-variant ParseError +pub extern "C" fn ParseError_skip() -> ParseError { + ParseError::Skip} +/// 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)] +#[repr(C)] +pub enum ParseOrSemanticError { + /// The invoice couldn't be decoded + ParseError(crate::lightning_invoice::ParseError), + /// The invoice could be decoded but violates the BOLT11 standard + SemanticError(crate::lightning_invoice::SemanticError), +} +use lightning_invoice::ParseOrSemanticError as nativeParseOrSemanticError; +impl ParseOrSemanticError { + #[allow(unused)] + pub(crate) fn to_native(&self) -> nativeParseOrSemanticError { + match self { + ParseOrSemanticError::ParseError (ref a, ) => { + let mut a_nonref = (*a).clone(); + nativeParseOrSemanticError::ParseError ( + a_nonref.into_native(), + ) + }, + ParseOrSemanticError::SemanticError (ref a, ) => { + let mut a_nonref = (*a).clone(); + nativeParseOrSemanticError::SemanticError ( + a_nonref.into_native(), + ) + }, + } + } + #[allow(unused)] + pub(crate) fn into_native(self) -> nativeParseOrSemanticError { + match self { + ParseOrSemanticError::ParseError (mut a, ) => { + nativeParseOrSemanticError::ParseError ( + a.into_native(), + ) + }, + ParseOrSemanticError::SemanticError (mut a, ) => { + nativeParseOrSemanticError::SemanticError ( + a.into_native(), + ) + }, + } + } + #[allow(unused)] + pub(crate) fn from_native(native: &nativeParseOrSemanticError) -> Self { + match native { + nativeParseOrSemanticError::ParseError (ref a, ) => { + let mut a_nonref = (*a).clone(); + ParseOrSemanticError::ParseError ( + crate::lightning_invoice::ParseError::native_into(a_nonref), + ) + }, + nativeParseOrSemanticError::SemanticError (ref a, ) => { + let mut a_nonref = (*a).clone(); + ParseOrSemanticError::SemanticError ( + crate::lightning_invoice::SemanticError::native_into(a_nonref), + ) + }, + } + } + #[allow(unused)] + pub(crate) fn native_into(native: nativeParseOrSemanticError) -> Self { + match native { + nativeParseOrSemanticError::ParseError (mut a, ) => { + ParseOrSemanticError::ParseError ( + crate::lightning_invoice::ParseError::native_into(a), + ) + }, + nativeParseOrSemanticError::SemanticError (mut a, ) => { + ParseOrSemanticError::SemanticError ( + crate::lightning_invoice::SemanticError::native_into(a), + ) + }, + } + } +} +/// Frees any resources used by the ParseOrSemanticError +#[no_mangle] +pub extern "C" fn ParseOrSemanticError_free(this_ptr: ParseOrSemanticError) { } +/// Creates a copy of the ParseOrSemanticError +#[no_mangle] +pub extern "C" fn ParseOrSemanticError_clone(orig: &ParseOrSemanticError) -> ParseOrSemanticError { + orig.clone() +} +#[no_mangle] +/// Utility method to constructs a new ParseError-variant ParseOrSemanticError +pub extern "C" fn ParseOrSemanticError_parse_error(a: crate::lightning_invoice::ParseError) -> ParseOrSemanticError { + ParseOrSemanticError::ParseError(a, ) +} +#[no_mangle] +/// Utility method to constructs a new SemanticError-variant ParseOrSemanticError +pub extern "C" fn ParseOrSemanticError_semantic_error(a: crate::lightning_invoice::SemanticError) -> ParseOrSemanticError { + ParseOrSemanticError::SemanticError(a, ) +} +/// 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 + +#[no_mangle] +pub static MAX_TIMESTAMP: u64 = lightning_invoice::MAX_TIMESTAMP; /// Default expiry time as defined by [BOLT 11]. /// -/// [BOLT 11]: https://github.com/lightningnetwork/lightning-rfc/blob/master/11-payment-encoding.md +/// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md #[no_mangle] pub static DEFAULT_EXPIRY_TIME: u64 = lightning_invoice::DEFAULT_EXPIRY_TIME; @@ -127,29 +581,11 @@ pub static DEFAULT_EXPIRY_TIME: u64 = lightning_invoice::DEFAULT_EXPIRY_TIME; /// Note that this is *not* the same value as rust-lightning's minimum CLTV expiry, which is /// provided in [`MIN_FINAL_CLTV_EXPIRY`]. /// -/// [BOLT 11]: https://github.com/lightningnetwork/lightning-rfc/blob/master/11-payment-encoding.md +/// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md /// [`MIN_FINAL_CLTV_EXPIRY`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY #[no_mangle] pub static DEFAULT_MIN_FINAL_CLTV_EXPIRY: u64 = lightning_invoice::DEFAULT_MIN_FINAL_CLTV_EXPIRY; -/// **Call this function on startup to ensure that all assumptions about the platform are valid.** -/// -/// Unfortunately we have to make assumptions about the upper bounds of the `SystemTime` type on -/// your platform which we can't fully verify at compile time and which isn't part of it's contract. -/// To our best knowledge our assumptions hold for all platforms officially supported by rust, but -/// since this check is fast we recommend to do it anyway. -/// -/// If this function fails this is considered a bug. Please open an issue describing your -/// platform and stating your current system time. -/// -/// # Panics -/// If the check fails this function panics. By calling this function on startup you ensure that -/// this wont happen at an arbitrary later point in time. -#[no_mangle] -pub extern "C" fn check_platform() { - lightning_invoice::check_platform() -} - use lightning_invoice::Invoice as nativeInvoiceImport; pub(crate) type nativeInvoice = nativeInvoiceImport; @@ -202,7 +638,7 @@ impl Invoice { pub(crate) fn take_inner(mut self) -> *mut nativeInvoice { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); - self.inner = std::ptr::null_mut(); + self.inner = core::ptr::null_mut(); ret } } @@ -218,7 +654,7 @@ pub extern "C" fn Invoice_eq(a: &Invoice, b: &Invoice) -> bool { impl Clone for Invoice { fn clone(&self) -> Self { Self { - inner: if <*mut nativeInvoice>::is_null(self.inner) { std::ptr::null_mut() } else { + inner: if <*mut nativeInvoice>::is_null(self.inner) { core::ptr::null_mut() } else { ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } @@ -285,7 +721,7 @@ impl SignedRawInvoice { pub(crate) fn take_inner(mut self) -> *mut nativeSignedRawInvoice { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); - self.inner = std::ptr::null_mut(); + self.inner = core::ptr::null_mut(); ret } } @@ -301,7 +737,7 @@ pub extern "C" fn SignedRawInvoice_eq(a: &SignedRawInvoice, b: &SignedRawInvoice impl Clone for SignedRawInvoice { fn clone(&self) -> Self { Self { - inner: if <*mut nativeSignedRawInvoice>::is_null(self.inner) { std::ptr::null_mut() } else { + inner: if <*mut nativeSignedRawInvoice>::is_null(self.inner) { core::ptr::null_mut() } else { ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } @@ -368,7 +804,7 @@ impl RawInvoice { pub(crate) fn take_inner(mut self) -> *mut nativeRawInvoice { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); - self.inner = std::ptr::null_mut(); + self.inner = core::ptr::null_mut(); ret } } @@ -395,7 +831,7 @@ pub extern "C" fn RawInvoice_eq(a: &RawInvoice, b: &RawInvoice) -> bool { impl Clone for RawInvoice { fn clone(&self) -> Self { Self { - inner: if <*mut nativeRawInvoice>::is_null(self.inner) { std::ptr::null_mut() } else { + inner: if <*mut nativeRawInvoice>::is_null(self.inner) { core::ptr::null_mut() } else { ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } @@ -458,7 +894,7 @@ impl RawDataPart { pub(crate) fn take_inner(mut self) -> *mut nativeRawDataPart { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); - self.inner = std::ptr::null_mut(); + self.inner = core::ptr::null_mut(); ret } } @@ -485,7 +921,7 @@ pub extern "C" fn RawDataPart_eq(a: &RawDataPart, b: &RawDataPart) -> bool { impl Clone for RawDataPart { fn clone(&self) -> Self { Self { - inner: if <*mut nativeRawDataPart>::is_null(self.inner) { std::ptr::null_mut() } else { + inner: if <*mut nativeRawDataPart>::is_null(self.inner) { core::ptr::null_mut() } else { ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } @@ -505,12 +941,12 @@ pub extern "C" fn RawDataPart_clone(orig: &RawDataPart) -> RawDataPart { use lightning_invoice::PositiveTimestamp as nativePositiveTimestampImport; pub(crate) type nativePositiveTimestamp = nativePositiveTimestampImport; -/// A timestamp that refers to a date after 1 January 1970 which means its representation as UNIX -/// timestamp is positive. +/// A timestamp that refers to a date after 1 January 1970. /// /// # Invariants -/// The UNIX timestamp representing the stored time has to be positive and small enough so that -/// a `EpiryTime` can be added to it without an overflow. +/// +/// The Unix timestamp representing the stored time has to be positive and no greater than +/// [`MAX_TIMESTAMP`]. #[must_use] #[repr(C)] pub struct PositiveTimestamp { @@ -553,7 +989,7 @@ impl PositiveTimestamp { pub(crate) fn take_inner(mut self) -> *mut nativePositiveTimestamp { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); - self.inner = std::ptr::null_mut(); + self.inner = core::ptr::null_mut(); ret } } @@ -569,7 +1005,7 @@ pub extern "C" fn PositiveTimestamp_eq(a: &PositiveTimestamp, b: &PositiveTimest impl Clone for PositiveTimestamp { fn clone(&self) -> Self { Self { - inner: if <*mut nativePositiveTimestamp>::is_null(self.inner) { std::ptr::null_mut() } else { + inner: if <*mut nativePositiveTimestamp>::is_null(self.inner) { core::ptr::null_mut() } else { ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } @@ -761,11 +1197,11 @@ pub extern "C" fn Currency_signet() -> Currency { /// Checks if two Currencys contain equal inner contents. #[no_mangle] pub extern "C" fn Currency_hash(o: &Currency) -> u64 { - // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core + // 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(); - std::hash::Hash::hash(&o.to_native(), &mut hasher); - std::hash::Hasher::finish(&hasher) + core::hash::Hash::hash(&o.to_native(), &mut hasher); + core::hash::Hasher::finish(&hasher) } /// Checks if two Currencys contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. @@ -820,14 +1256,14 @@ impl Sha256 { pub(crate) fn take_inner(mut self) -> *mut nativeSha256 { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); - self.inner = std::ptr::null_mut(); + self.inner = core::ptr::null_mut(); ret } } impl Clone for Sha256 { fn clone(&self) -> Self { Self { - inner: if <*mut nativeSha256>::is_null(self.inner) { std::ptr::null_mut() } else { + inner: if <*mut nativeSha256>::is_null(self.inner) { core::ptr::null_mut() } else { ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } @@ -847,11 +1283,11 @@ pub extern "C" fn Sha256_clone(orig: &Sha256) -> Sha256 { #[no_mangle] pub extern "C" fn Sha256_hash(o: &Sha256) -> u64 { if o.inner.is_null() { return 0; } - // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core + // 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(); - std::hash::Hash::hash(o.get_native_ref(), &mut hasher); - std::hash::Hasher::finish(&hasher) + core::hash::Hash::hash(o.get_native_ref(), &mut hasher); + core::hash::Hasher::finish(&hasher) } /// Checks if two Sha256s contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. @@ -912,14 +1348,14 @@ impl Description { pub(crate) fn take_inner(mut self) -> *mut nativeDescription { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); - self.inner = std::ptr::null_mut(); + self.inner = core::ptr::null_mut(); ret } } impl Clone for Description { fn clone(&self) -> Self { Self { - inner: if <*mut nativeDescription>::is_null(self.inner) { std::ptr::null_mut() } else { + inner: if <*mut nativeDescription>::is_null(self.inner) { core::ptr::null_mut() } else { ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } @@ -939,11 +1375,11 @@ pub extern "C" fn Description_clone(orig: &Description) -> Description { #[no_mangle] pub extern "C" fn Description_hash(o: &Description) -> u64 { if o.inner.is_null() { return 0; } - // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core + // 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(); - std::hash::Hash::hash(o.get_native_ref(), &mut hasher); - std::hash::Hasher::finish(&hasher) + core::hash::Hash::hash(o.get_native_ref(), &mut hasher); + core::hash::Hasher::finish(&hasher) } /// Checks if two Descriptions contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. @@ -1001,7 +1437,7 @@ impl PayeePubKey { pub(crate) fn take_inner(mut self) -> *mut nativePayeePubKey { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); - self.inner = std::ptr::null_mut(); + self.inner = core::ptr::null_mut(); ret } } @@ -1025,7 +1461,7 @@ pub extern "C" fn PayeePubKey_new(mut a_arg: crate::c_types::PublicKey) -> Payee impl Clone for PayeePubKey { fn clone(&self) -> Self { Self { - inner: if <*mut nativePayeePubKey>::is_null(self.inner) { std::ptr::null_mut() } else { + inner: if <*mut nativePayeePubKey>::is_null(self.inner) { core::ptr::null_mut() } else { ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } @@ -1045,11 +1481,11 @@ pub extern "C" fn PayeePubKey_clone(orig: &PayeePubKey) -> PayeePubKey { #[no_mangle] pub extern "C" fn PayeePubKey_hash(o: &PayeePubKey) -> u64 { if o.inner.is_null() { return 0; } - // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core + // 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(); - std::hash::Hash::hash(o.get_native_ref(), &mut hasher); - std::hash::Hasher::finish(&hasher) + core::hash::Hash::hash(o.get_native_ref(), &mut hasher); + core::hash::Hasher::finish(&hasher) } /// Checks if two PayeePubKeys contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. @@ -1066,11 +1502,6 @@ pub(crate) type nativeExpiryTime = nativeExpiryTimeImport; /// Positive duration that defines when (relatively to the timestamp) in the future the invoice /// expires -/// -/// # Invariants -/// The number of seconds this expiry time represents has to be in the range -/// `0...(SYSTEM_TIME_MAX_UNIX_TIMESTAMP - MAX_EXPIRY_TIME)` to avoid overflows when adding it to a -/// timestamp #[must_use] #[repr(C)] pub struct ExpiryTime { @@ -1113,14 +1544,14 @@ impl ExpiryTime { pub(crate) fn take_inner(mut self) -> *mut nativeExpiryTime { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); - self.inner = std::ptr::null_mut(); + self.inner = core::ptr::null_mut(); ret } } impl Clone for ExpiryTime { fn clone(&self) -> Self { Self { - inner: if <*mut nativeExpiryTime>::is_null(self.inner) { std::ptr::null_mut() } else { + inner: if <*mut nativeExpiryTime>::is_null(self.inner) { core::ptr::null_mut() } else { ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } @@ -1140,11 +1571,11 @@ pub extern "C" fn ExpiryTime_clone(orig: &ExpiryTime) -> ExpiryTime { #[no_mangle] pub extern "C" fn ExpiryTime_hash(o: &ExpiryTime) -> u64 { if o.inner.is_null() { return 0; } - // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core + // 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(); - std::hash::Hash::hash(o.get_native_ref(), &mut hasher); - std::hash::Hasher::finish(&hasher) + core::hash::Hash::hash(o.get_native_ref(), &mut hasher); + core::hash::Hasher::finish(&hasher) } /// Checks if two ExpiryTimes contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. @@ -1202,7 +1633,7 @@ impl MinFinalCltvExpiry { pub(crate) fn take_inner(mut self) -> *mut nativeMinFinalCltvExpiry { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); - self.inner = std::ptr::null_mut(); + self.inner = core::ptr::null_mut(); ret } } @@ -1226,7 +1657,7 @@ pub extern "C" fn MinFinalCltvExpiry_new(mut a_arg: u64) -> MinFinalCltvExpiry { impl Clone for MinFinalCltvExpiry { fn clone(&self) -> Self { Self { - inner: if <*mut nativeMinFinalCltvExpiry>::is_null(self.inner) { std::ptr::null_mut() } else { + inner: if <*mut nativeMinFinalCltvExpiry>::is_null(self.inner) { core::ptr::null_mut() } else { ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } @@ -1246,11 +1677,11 @@ pub extern "C" fn MinFinalCltvExpiry_clone(orig: &MinFinalCltvExpiry) -> MinFina #[no_mangle] pub extern "C" fn MinFinalCltvExpiry_hash(o: &MinFinalCltvExpiry) -> u64 { if o.inner.is_null() { return 0; } - // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core + // 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(); - std::hash::Hash::hash(o.get_native_ref(), &mut hasher); - std::hash::Hasher::finish(&hasher) + core::hash::Hash::hash(o.get_native_ref(), &mut hasher); + core::hash::Hasher::finish(&hasher) } /// Checks if two MinFinalCltvExpirys contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. @@ -1401,11 +1832,11 @@ pub extern "C" fn Fallback_script_hash(a: crate::c_types::TwentyBytes) -> Fallba /// Checks if two Fallbacks contain equal inner contents. #[no_mangle] pub extern "C" fn Fallback_hash(o: &Fallback) -> u64 { - // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core + // 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(); - std::hash::Hash::hash(&o.to_native(), &mut hasher); - std::hash::Hasher::finish(&hasher) + core::hash::Hash::hash(&o.to_native(), &mut hasher); + core::hash::Hasher::finish(&hasher) } /// Checks if two Fallbacks contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. @@ -1460,14 +1891,14 @@ impl InvoiceSignature { pub(crate) fn take_inner(mut self) -> *mut nativeInvoiceSignature { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); - self.inner = std::ptr::null_mut(); + self.inner = core::ptr::null_mut(); ret } } impl Clone for InvoiceSignature { fn clone(&self) -> Self { Self { - inner: if <*mut nativeInvoiceSignature>::is_null(self.inner) { std::ptr::null_mut() } else { + inner: if <*mut nativeInvoiceSignature>::is_null(self.inner) { core::ptr::null_mut() } else { ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } @@ -1543,14 +1974,14 @@ impl PrivateRoute { pub(crate) fn take_inner(mut self) -> *mut nativePrivateRoute { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); - self.inner = std::ptr::null_mut(); + self.inner = core::ptr::null_mut(); ret } } impl Clone for PrivateRoute { fn clone(&self) -> Self { Self { - inner: if <*mut nativePrivateRoute>::is_null(self.inner) { std::ptr::null_mut() } else { + inner: if <*mut nativePrivateRoute>::is_null(self.inner) { core::ptr::null_mut() } else { ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } @@ -1570,11 +2001,11 @@ pub extern "C" fn PrivateRoute_clone(orig: &PrivateRoute) -> PrivateRoute { #[no_mangle] pub extern "C" fn PrivateRoute_hash(o: &PrivateRoute) -> u64 { if o.inner.is_null() { return 0; } - // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core + // 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(); - std::hash::Hash::hash(o.get_native_ref(), &mut hasher); - std::hash::Hasher::finish(&hasher) + core::hash::Hash::hash(o.get_native_ref(), &mut hasher); + core::hash::Hasher::finish(&hasher) } /// Checks if two PrivateRoutes contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. @@ -1653,7 +2084,7 @@ pub extern "C" fn RawInvoice_hash(this_arg: &RawInvoice) -> crate::c_types::Thir #[no_mangle] pub extern "C" fn RawInvoice_payment_hash(this_arg: &RawInvoice) -> crate::lightning_invoice::Sha256 { let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.payment_hash(); - let mut local_ret = crate::lightning_invoice::Sha256 { inner: unsafe { (if ret.is_none() { std::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning_invoice::Sha256<>) as *mut _ }, is_owned: false }; + let mut local_ret = crate::lightning_invoice::Sha256 { inner: unsafe { (if ret.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning_invoice::Sha256<>) as *mut _ }, is_owned: false }; local_ret } @@ -1663,7 +2094,7 @@ pub extern "C" fn RawInvoice_payment_hash(this_arg: &RawInvoice) -> crate::light #[no_mangle] pub extern "C" fn RawInvoice_description(this_arg: &RawInvoice) -> crate::lightning_invoice::Description { let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.description(); - let mut local_ret = crate::lightning_invoice::Description { inner: unsafe { (if ret.is_none() { std::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning_invoice::Description<>) as *mut _ }, is_owned: false }; + let mut local_ret = crate::lightning_invoice::Description { inner: unsafe { (if ret.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning_invoice::Description<>) as *mut _ }, is_owned: false }; local_ret } @@ -1673,7 +2104,7 @@ pub extern "C" fn RawInvoice_description(this_arg: &RawInvoice) -> crate::lightn #[no_mangle] pub extern "C" fn RawInvoice_payee_pub_key(this_arg: &RawInvoice) -> crate::lightning_invoice::PayeePubKey { let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.payee_pub_key(); - let mut local_ret = crate::lightning_invoice::PayeePubKey { inner: unsafe { (if ret.is_none() { std::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning_invoice::PayeePubKey<>) as *mut _ }, is_owned: false }; + let mut local_ret = crate::lightning_invoice::PayeePubKey { inner: unsafe { (if ret.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning_invoice::PayeePubKey<>) as *mut _ }, is_owned: false }; local_ret } @@ -1683,7 +2114,7 @@ pub extern "C" fn RawInvoice_payee_pub_key(this_arg: &RawInvoice) -> crate::ligh #[no_mangle] pub extern "C" fn RawInvoice_description_hash(this_arg: &RawInvoice) -> crate::lightning_invoice::Sha256 { let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.description_hash(); - let mut local_ret = crate::lightning_invoice::Sha256 { inner: unsafe { (if ret.is_none() { std::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning_invoice::Sha256<>) as *mut _ }, is_owned: false }; + let mut local_ret = crate::lightning_invoice::Sha256 { inner: unsafe { (if ret.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning_invoice::Sha256<>) as *mut _ }, is_owned: false }; local_ret } @@ -1693,7 +2124,7 @@ pub extern "C" fn RawInvoice_description_hash(this_arg: &RawInvoice) -> crate::l #[no_mangle] pub extern "C" fn RawInvoice_expiry_time(this_arg: &RawInvoice) -> crate::lightning_invoice::ExpiryTime { let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.expiry_time(); - let mut local_ret = crate::lightning_invoice::ExpiryTime { inner: unsafe { (if ret.is_none() { std::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning_invoice::ExpiryTime<>) as *mut _ }, is_owned: false }; + let mut local_ret = crate::lightning_invoice::ExpiryTime { inner: unsafe { (if ret.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning_invoice::ExpiryTime<>) as *mut _ }, is_owned: false }; local_ret } @@ -1703,7 +2134,7 @@ pub extern "C" fn RawInvoice_expiry_time(this_arg: &RawInvoice) -> crate::lightn #[no_mangle] pub extern "C" fn RawInvoice_min_final_cltv_expiry(this_arg: &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() { std::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning_invoice::MinFinalCltvExpiry<>) as *mut _ }, is_owned: false }; + 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 }; local_ret } @@ -1723,7 +2154,7 @@ pub extern "C" fn RawInvoice_payment_secret(this_arg: &RawInvoice) -> crate::c_t #[no_mangle] pub extern "C" fn RawInvoice_features(this_arg: &RawInvoice) -> crate::lightning::ln::features::InvoiceFeatures { let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.features(); - let mut local_ret = crate::lightning::ln::features::InvoiceFeatures { inner: unsafe { (if ret.is_none() { std::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning::ln::features::InvoiceFeatures<>) as *mut _ }, is_owned: false }; + let mut local_ret = crate::lightning::ln::features::InvoiceFeatures { inner: unsafe { (if ret.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning::ln::features::InvoiceFeatures<>) as *mut _ }, is_owned: false }; local_ret } @@ -1750,9 +2181,9 @@ pub extern "C" fn RawInvoice_currency(this_arg: &RawInvoice) -> crate::lightning crate::lightning_invoice::Currency::native_into(ret) } -/// Create a new `PositiveTimestamp` from a unix timestamp in the Range -/// `0...SYSTEM_TIME_MAX_UNIX_TIMESTAMP - MAX_EXPIRY_TIME`, otherwise return a -/// `CreationError::TimestampOutOfBounds`. +/// Creates a `PositiveTimestamp` from a Unix timestamp in the range `0..=MAX_TIMESTAMP`. +/// +/// Otherwise, returns a [`CreationError::TimestampOutOfBounds`]. #[must_use] #[no_mangle] pub extern "C" fn PositiveTimestamp_from_unix_timestamp(mut unix_seconds: u64) -> crate::c_types::derived::CResult_PositiveTimestampCreationErrorZ { @@ -1761,9 +2192,10 @@ pub extern "C" fn PositiveTimestamp_from_unix_timestamp(mut unix_seconds: u64) - local_ret } -/// Create a new `PositiveTimestamp` from a `SystemTime` with a corresponding unix timestamp in -/// the Range `0...SYSTEM_TIME_MAX_UNIX_TIMESTAMP - MAX_EXPIRY_TIME`, otherwise return a -/// `CreationError::TimestampOutOfBounds`. +/// Creates a `PositiveTimestamp` from a [`SystemTime`] with a corresponding Unix timestamp in +/// the range `0..=MAX_TIMESTAMP`. +/// +/// Otherwise, returns a [`CreationError::TimestampOutOfBounds`]. #[must_use] #[no_mangle] pub extern "C" fn PositiveTimestamp_from_system_time(mut time: u64) -> crate::c_types::derived::CResult_PositiveTimestampCreationErrorZ { @@ -1772,7 +2204,19 @@ pub extern "C" fn PositiveTimestamp_from_system_time(mut time: u64) -> crate::c_ local_ret } -/// Returns the UNIX timestamp representing the stored time +/// Creates a `PositiveTimestamp` from a [`Duration`] since the Unix epoch in the range +/// `0..=MAX_TIMESTAMP`. +/// +/// Otherwise, returns a [`CreationError::TimestampOutOfBounds`]. +#[must_use] +#[no_mangle] +pub extern "C" fn PositiveTimestamp_from_duration_since_epoch(mut duration: u64) -> crate::c_types::derived::CResult_PositiveTimestampCreationErrorZ { + let mut ret = lightning_invoice::PositiveTimestamp::from_duration_since_epoch(core::time::Duration::from_secs(duration)); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning_invoice::PositiveTimestamp { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::CreationError::native_into(e) }).into() }; + local_ret +} + +/// Returns the Unix timestamp representing the stored time #[must_use] #[no_mangle] pub extern "C" fn PositiveTimestamp_as_unix_timestamp(this_arg: &PositiveTimestamp) -> u64 { @@ -1780,7 +2224,15 @@ pub extern "C" fn PositiveTimestamp_as_unix_timestamp(this_arg: &PositiveTimesta ret } -/// Returns a reference to the internal `SystemTime` time representation +/// Returns the duration of the stored time since the Unix epoch +#[must_use] +#[no_mangle] +pub extern "C" fn PositiveTimestamp_as_duration_since_epoch(this_arg: &PositiveTimestamp) -> u64 { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.as_duration_since_epoch(); + ret.as_secs() +} + +/// Returns the [`SystemTime`] representing the stored time #[must_use] #[no_mangle] pub extern "C" fn PositiveTimestamp_as_time(this_arg: &PositiveTimestamp) -> u64 { @@ -1833,7 +2285,7 @@ pub extern "C" fn Invoice_from_signed(mut signed_invoice: crate::lightning_invoi local_ret } -/// Returns the `Invoice`'s timestamp (should equal it's creation time) +/// Returns the `Invoice`'s timestamp (should equal its creation time) #[must_use] #[no_mangle] pub extern "C" fn Invoice_timestamp(this_arg: &Invoice) -> u64 { @@ -1841,6 +2293,14 @@ pub extern "C" fn Invoice_timestamp(this_arg: &Invoice) -> u64 { ret.duration_since(::std::time::SystemTime::UNIX_EPOCH).expect("Times must be post-1970").as_secs() } +/// Returns the `Invoice`'s timestamp as a duration since the Unix epoch +#[must_use] +#[no_mangle] +pub extern "C" fn Invoice_duration_since_epoch(this_arg: &Invoice) -> u64 { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.duration_since_epoch(); + ret.as_secs() +} + /// Returns the hash to which we will receive the preimage on completion of the payment #[must_use] #[no_mangle] @@ -1875,7 +2335,7 @@ pub extern "C" fn Invoice_payment_secret(this_arg: &Invoice) -> *const [u8; 32] #[no_mangle] pub extern "C" fn Invoice_features(this_arg: &Invoice) -> crate::lightning::ln::features::InvoiceFeatures { let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.features(); - let mut local_ret = crate::lightning::ln::features::InvoiceFeatures { inner: unsafe { (if ret.is_none() { std::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning::ln::features::InvoiceFeatures<>) as *mut _ }, is_owned: false }; + let mut local_ret = crate::lightning::ln::features::InvoiceFeatures { inner: unsafe { (if ret.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning::ln::features::InvoiceFeatures<>) as *mut _ }, is_owned: false }; local_ret } @@ -1903,6 +2363,15 @@ pub extern "C" fn Invoice_is_expired(this_arg: &Invoice) -> bool { ret } +/// Returns whether the expiry time would pass at the given point in time. +/// `at_time` is the timestamp as a duration since the Unix epoch. +#[must_use] +#[no_mangle] +pub extern "C" fn Invoice_would_expire(this_arg: &Invoice, mut at_time: u64) -> bool { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.would_expire(core::time::Duration::from_secs(at_time)); + ret +} + /// Returns the invoice's `min_final_cltv_expiry` time, if present, otherwise /// [`DEFAULT_MIN_FINAL_CLTV_EXPIRY`]. #[must_use] @@ -1967,26 +2436,20 @@ pub extern "C" fn Description_into_inner(mut this_arg: Description) -> crate::c_ ret.into() } -/// Construct an `ExpiryTime` from seconds. If there exists a `PositiveTimestamp` which would -/// overflow on adding the `EpiryTime` to it then this function will return a -/// `CreationError::ExpiryTimeOutOfBounds`. +/// Construct an `ExpiryTime` from seconds. #[must_use] #[no_mangle] -pub extern "C" fn ExpiryTime_from_seconds(mut seconds: u64) -> crate::c_types::derived::CResult_ExpiryTimeCreationErrorZ { +pub extern "C" fn ExpiryTime_from_seconds(mut seconds: u64) -> crate::lightning_invoice::ExpiryTime { let mut ret = lightning_invoice::ExpiryTime::from_seconds(seconds); - let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning_invoice::ExpiryTime { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::CreationError::native_into(e) }).into() }; - local_ret + crate::lightning_invoice::ExpiryTime { inner: ObjOps::heap_alloc(ret), is_owned: true } } -/// Construct an `ExpiryTime` from a `Duration`. If there exists a `PositiveTimestamp` which -/// would overflow on adding the `EpiryTime` to it then this function will return a -/// `CreationError::ExpiryTimeOutOfBounds`. +/// Construct an `ExpiryTime` from a `Duration`. #[must_use] #[no_mangle] -pub extern "C" fn ExpiryTime_from_duration(mut duration: u64) -> crate::c_types::derived::CResult_ExpiryTimeCreationErrorZ { - let mut ret = lightning_invoice::ExpiryTime::from_duration(std::time::Duration::from_secs(duration)); - let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning_invoice::ExpiryTime { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::CreationError::native_into(e) }).into() }; - local_ret +pub extern "C" fn ExpiryTime_from_duration(mut duration: u64) -> crate::lightning_invoice::ExpiryTime { + let mut ret = lightning_invoice::ExpiryTime::from_duration(core::time::Duration::from_secs(duration)); + crate::lightning_invoice::ExpiryTime { inner: ObjOps::heap_alloc(ret), is_owned: true } } /// Returns the expiry time in seconds @@ -2031,10 +2494,15 @@ pub enum CreationError { DescriptionTooLong, /// The specified route has too many hops and can't be encoded RouteTooLong, - /// The unix timestamp of the supplied date is <0 or can't be represented as `SystemTime` + /// The Unix timestamp of the supplied date is less than zero or greater than 35-bits TimestampOutOfBounds, - /// The supplied expiry time could cause an overflow if added to a `PositiveTimestamp` - ExpiryTimeOutOfBounds, + /// The supplied millisatoshi amount was greater than the total bitcoin supply. + InvalidAmount, + /// Route hints were required for this invoice and were missing. Applies to + /// [phantom invoices]. + /// + /// [phantom invoices]: crate::utils::create_phantom_invoice + MissingRouteHints, } use lightning_invoice::CreationError as nativeCreationError; impl CreationError { @@ -2044,7 +2512,8 @@ impl CreationError { CreationError::DescriptionTooLong => nativeCreationError::DescriptionTooLong, CreationError::RouteTooLong => nativeCreationError::RouteTooLong, CreationError::TimestampOutOfBounds => nativeCreationError::TimestampOutOfBounds, - CreationError::ExpiryTimeOutOfBounds => nativeCreationError::ExpiryTimeOutOfBounds, + CreationError::InvalidAmount => nativeCreationError::InvalidAmount, + CreationError::MissingRouteHints => nativeCreationError::MissingRouteHints, } } #[allow(unused)] @@ -2053,7 +2522,8 @@ impl CreationError { CreationError::DescriptionTooLong => nativeCreationError::DescriptionTooLong, CreationError::RouteTooLong => nativeCreationError::RouteTooLong, CreationError::TimestampOutOfBounds => nativeCreationError::TimestampOutOfBounds, - CreationError::ExpiryTimeOutOfBounds => nativeCreationError::ExpiryTimeOutOfBounds, + CreationError::InvalidAmount => nativeCreationError::InvalidAmount, + CreationError::MissingRouteHints => nativeCreationError::MissingRouteHints, } } #[allow(unused)] @@ -2062,7 +2532,8 @@ impl CreationError { nativeCreationError::DescriptionTooLong => CreationError::DescriptionTooLong, nativeCreationError::RouteTooLong => CreationError::RouteTooLong, nativeCreationError::TimestampOutOfBounds => CreationError::TimestampOutOfBounds, - nativeCreationError::ExpiryTimeOutOfBounds => CreationError::ExpiryTimeOutOfBounds, + nativeCreationError::InvalidAmount => CreationError::InvalidAmount, + nativeCreationError::MissingRouteHints => CreationError::MissingRouteHints, } } #[allow(unused)] @@ -2071,7 +2542,8 @@ impl CreationError { nativeCreationError::DescriptionTooLong => CreationError::DescriptionTooLong, nativeCreationError::RouteTooLong => CreationError::RouteTooLong, nativeCreationError::TimestampOutOfBounds => CreationError::TimestampOutOfBounds, - nativeCreationError::ExpiryTimeOutOfBounds => CreationError::ExpiryTimeOutOfBounds, + nativeCreationError::InvalidAmount => CreationError::InvalidAmount, + nativeCreationError::MissingRouteHints => CreationError::MissingRouteHints, } } } @@ -2093,9 +2565,13 @@ pub extern "C" fn CreationError_route_too_long() -> CreationError { pub extern "C" fn CreationError_timestamp_out_of_bounds() -> CreationError { CreationError::TimestampOutOfBounds} #[no_mangle] -/// Utility method to constructs a new ExpiryTimeOutOfBounds-variant CreationError -pub extern "C" fn CreationError_expiry_time_out_of_bounds() -> CreationError { - CreationError::ExpiryTimeOutOfBounds} +/// Utility method to constructs a new InvalidAmount-variant CreationError +pub extern "C" fn CreationError_invalid_amount() -> CreationError { + CreationError::InvalidAmount} +#[no_mangle] +/// Utility method to constructs a new MissingRouteHints-variant CreationError +pub extern "C" fn CreationError_missing_route_hints() -> CreationError { + CreationError::MissingRouteHints} /// Checks if two CreationErrors contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. #[no_mangle] @@ -2105,7 +2581,7 @@ pub extern "C" fn CreationError_eq(a: &CreationError, b: &CreationError) -> bool #[no_mangle] /// Get the string representation of a CreationError object pub extern "C" fn CreationError_to_str(o: &crate::lightning_invoice::CreationError) -> Str { - format!("{}", &o.to_native()).into() + alloc::format!("{}", &o.to_native()).into() } /// Errors that may occur when converting a `RawInvoice` to an `Invoice`. They relate to the /// requirements sections in BOLT #11 @@ -2252,7 +2728,7 @@ pub extern "C" fn SemanticError_eq(a: &SemanticError, b: &SemanticError) -> bool #[no_mangle] /// Get the string representation of a SemanticError object pub extern "C" fn SemanticError_to_str(o: &crate::lightning_invoice::SemanticError) -> Str { - format!("{}", &o.to_native()).into() + alloc::format!("{}", &o.to_native()).into() } /// When signing using a fallible method either an user-supplied `SignError` or a `CreationError` /// may occur. @@ -2351,5 +2827,5 @@ pub extern "C" fn SignOrCreationError_eq(a: &SignOrCreationError, b: &SignOrCrea #[no_mangle] /// Get the string representation of a SignOrCreationError object pub extern "C" fn SignOrCreationError_to_str(o: &crate::lightning_invoice::SignOrCreationError) -> Str { - format!("{}", &o.to_native()).into() + alloc::format!("{}", &o.to_native()).into() }