X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-invoice%2Fsrc%2Flib.rs;h=9fcb4af1b624257b6818d1c121c4dd9f7c6c6eaa;hb=36817e0539378b34f3d07d99e88443107c813200;hp=1dd7123a486d1077f1295b7655b1fac497cfb57f;hpb=ca163c3fae94e64b7f70a7a549cd761cfa7e52d2;p=rust-lightning diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index 1dd7123a..9fcb4af1 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -25,6 +25,8 @@ compile_error!("at least one of the `std` or `no-std` features must be enabled") pub mod payment; pub mod utils; +pub(crate) mod time_utils; + extern crate bech32; extern crate bitcoin_hashes; #[macro_use] extern crate lightning; @@ -47,15 +49,17 @@ use lightning::routing::network_graph::RoutingFees; use lightning::routing::router::RouteHint; use lightning::util::invoice::construct_invoice_preimage; -use secp256k1::key::PublicKey; +use secp256k1::PublicKey; use secp256k1::{Message, Secp256k1}; -use secp256k1::recovery::RecoverableSignature; +use secp256k1::ecdsa::RecoverableSignature; use core::fmt::{Display, Formatter, self}; use core::iter::FilterMap; +use core::num::ParseIntError; use core::ops::Deref; use core::slice::Iter; use core::time::Duration; +use core::str; mod de; mod ser; @@ -86,7 +90,45 @@ mod sync { #[cfg(not(feature = "std"))] mod sync; -pub use de::{ParseError, ParseOrSemanticError}; +/// 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. +#[allow(missing_docs)] +#[derive(PartialEq, Debug, Clone)] +pub enum ParseError { + Bech32Error(bech32::Error), + ParseAmountError(ParseIntError), + MalformedSignature(secp256k1::Error), + BadPrefix, + UnknownCurrency, + UnknownSiPrefix, + MalformedHRP, + TooShortDataPart, + UnexpectedEndOfTaggedFields, + DescriptionDecodeError(str::Utf8Error), + PaddingError, + IntegerOverflowError, + InvalidSegWitProgramLength, + InvalidPubKeyHashLength, + InvalidScriptHashLength, + InvalidRecoveryId, + InvalidSliceLength(String), + + /// Not an error, but used internally to signal that a part of the invoice should be ignored + /// according to BOLT11 + 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. +#[derive(PartialEq, Debug, Clone)] +pub enum ParseOrSemanticError { + /// The invoice couldn't be decoded + ParseError(ParseError), + + /// The invoice could be decoded but violates the BOLT11 standard + SemanticError(::SemanticError), +} /// The number of bits used to represent timestamps as defined in BOLT 11. const TIMESTAMP_BITS: usize = 35; @@ -123,7 +165,7 @@ pub const DEFAULT_MIN_FINAL_CLTV_EXPIRY: u64 = 18; /// use bitcoin_hashes::sha256; /// /// use secp256k1::Secp256k1; -/// use secp256k1::key::SecretKey; +/// use secp256k1::SecretKey; /// /// use lightning::ln::PaymentSecret; /// @@ -151,7 +193,7 @@ pub const DEFAULT_MIN_FINAL_CLTV_EXPIRY: u64 = 18; /// .current_timestamp() /// .min_final_cltv_expiry(144) /// .build_signed(|hash| { -/// Secp256k1::new().sign_recoverable(hash, &private_key) +/// Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key) /// }) /// .unwrap(); /// @@ -709,7 +751,7 @@ impl SignedRawInvoice { let hash = Message::from_slice(&self.hash[..]) .expect("Hash is 32 bytes long, same as MESSAGE_SIZE"); - Ok(PayeePubKey(Secp256k1::new().recover( + Ok(PayeePubKey(Secp256k1::new().recover_ecdsa( &hash, &self.signature )?)) @@ -736,7 +778,7 @@ impl SignedRawInvoice { .expect("Hash is 32 bytes long, same as MESSAGE_SIZE"); let secp_context = Secp256k1::new(); - let verification_result = secp_context.verify( + let verification_result = secp_context.verify_ecdsa( &hash, &self.signature.to_standard(), pub_key @@ -1536,8 +1578,8 @@ mod test { fn test_check_signature() { use TaggedField::*; use secp256k1::Secp256k1; - use secp256k1::recovery::{RecoveryId, RecoverableSignature}; - use secp256k1::key::{SecretKey, PublicKey}; + use secp256k1::ecdsa::{RecoveryId, RecoverableSignature}; + use secp256k1::{SecretKey, PublicKey}; use {SignedRawInvoice, InvoiceSignature, RawInvoice, RawHrp, RawDataPart, Currency, Sha256, PositiveTimestamp}; @@ -1595,7 +1637,7 @@ mod test { let (raw_invoice, _, _) = invoice.into_parts(); let new_signed = raw_invoice.sign::<_, ()>(|hash| { - Ok(Secp256k1::new().sign_recoverable(hash, &private_key)) + Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)) }).unwrap(); assert!(new_signed.check_signature()); @@ -1606,7 +1648,7 @@ mod test { use TaggedField::*; use lightning::ln::features::InvoiceFeatures; use secp256k1::Secp256k1; - use secp256k1::key::SecretKey; + use secp256k1::SecretKey; use {RawInvoice, RawHrp, RawDataPart, Currency, Sha256, PositiveTimestamp, Invoice, SemanticError}; @@ -1637,7 +1679,7 @@ mod test { let invoice = { let mut invoice = invoice_template.clone(); invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into()); - invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_recoverable(hash, &private_key))) + invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key))) }.unwrap(); assert_eq!(Invoice::from_signed(invoice), Err(SemanticError::InvalidFeatures)); @@ -1646,7 +1688,7 @@ mod test { let mut invoice = invoice_template.clone(); invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into()); invoice.data.tagged_fields.push(Features(InvoiceFeatures::empty()).into()); - invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_recoverable(hash, &private_key))) + invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key))) }.unwrap(); assert_eq!(Invoice::from_signed(invoice), Err(SemanticError::InvalidFeatures)); @@ -1655,14 +1697,14 @@ mod test { let mut invoice = invoice_template.clone(); invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into()); invoice.data.tagged_fields.push(Features(InvoiceFeatures::known()).into()); - invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_recoverable(hash, &private_key))) + invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key))) }.unwrap(); assert!(Invoice::from_signed(invoice).is_ok()); // No payment secret or features let invoice = { let invoice = invoice_template.clone(); - invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_recoverable(hash, &private_key))) + invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key))) }.unwrap(); assert_eq!(Invoice::from_signed(invoice), Err(SemanticError::NoPaymentSecret)); @@ -1670,7 +1712,7 @@ mod test { let invoice = { let mut invoice = invoice_template.clone(); invoice.data.tagged_fields.push(Features(InvoiceFeatures::empty()).into()); - invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_recoverable(hash, &private_key))) + invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key))) }.unwrap(); assert_eq!(Invoice::from_signed(invoice), Err(SemanticError::NoPaymentSecret)); @@ -1678,7 +1720,7 @@ mod test { let invoice = { let mut invoice = invoice_template.clone(); invoice.data.tagged_fields.push(Features(InvoiceFeatures::known()).into()); - invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_recoverable(hash, &private_key))) + invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key))) }.unwrap(); assert_eq!(Invoice::from_signed(invoice), Err(SemanticError::NoPaymentSecret)); @@ -1687,7 +1729,7 @@ mod test { let mut invoice = invoice_template.clone(); invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into()); invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into()); - invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_recoverable(hash, &private_key))) + invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key))) }.unwrap(); assert_eq!(Invoice::from_signed(invoice), Err(SemanticError::MultiplePaymentSecrets)); } @@ -1724,7 +1766,7 @@ mod test { use ::*; use lightning::routing::router::RouteHintHop; use std::iter::FromIterator; - use secp256k1::key::PublicKey; + use secp256k1::PublicKey; let builder = InvoiceBuilder::new(Currency::Bitcoin) .payment_hash(sha256::Hash::from_slice(&[0;32][..]).unwrap()) @@ -1778,7 +1820,7 @@ mod test { use ::*; use lightning::routing::router::RouteHintHop; use secp256k1::Secp256k1; - use secp256k1::key::{SecretKey, PublicKey}; + use secp256k1::{SecretKey, PublicKey}; use std::time::{UNIX_EPOCH, Duration}; let secp_ctx = Secp256k1::new(); @@ -1857,7 +1899,7 @@ mod test { .basic_mpp(); let invoice = builder.clone().build_signed(|hash| { - secp_ctx.sign_recoverable(hash, &private_key) + secp_ctx.sign_ecdsa_recoverable(hash, &private_key) }).unwrap(); assert!(invoice.check_signature().is_ok()); @@ -1892,7 +1934,7 @@ mod test { fn test_default_values() { use ::*; use secp256k1::Secp256k1; - use secp256k1::key::SecretKey; + use secp256k1::SecretKey; let signed_invoice = InvoiceBuilder::new(Currency::Bitcoin) .description("Test".into()) @@ -1904,7 +1946,7 @@ mod test { .sign::<_, ()>(|hash| { let privkey = SecretKey::from_slice(&[41; 32]).unwrap(); let secp_ctx = Secp256k1::new(); - Ok(secp_ctx.sign_recoverable(hash, &privkey)) + Ok(secp_ctx.sign_ecdsa_recoverable(hash, &privkey)) }) .unwrap(); let invoice = Invoice::from_signed(signed_invoice).unwrap(); @@ -1918,7 +1960,7 @@ mod test { fn test_expiration() { use ::*; use secp256k1::Secp256k1; - use secp256k1::key::SecretKey; + use secp256k1::SecretKey; let signed_invoice = InvoiceBuilder::new(Currency::Bitcoin) .description("Test".into()) @@ -1930,7 +1972,7 @@ mod test { .sign::<_, ()>(|hash| { let privkey = SecretKey::from_slice(&[41; 32]).unwrap(); let secp_ctx = Secp256k1::new(); - Ok(secp_ctx.sign_recoverable(hash, &privkey)) + Ok(secp_ctx.sign_ecdsa_recoverable(hash, &privkey)) }) .unwrap(); let invoice = Invoice::from_signed(signed_invoice).unwrap();