X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-invoice%2Fsrc%2Fde.rs;h=b4eb04a3d21666369fb6956ca026aebcf8e44496;hb=c70bd1fd558513e72fa03dc16f9556882883046e;hp=2ed356dafb8d3959b0c43cebed8ccdc161e69f39;hpb=0cff1f7bb40bac658023551b22dca0a95f0b7cf3;p=rust-lightning diff --git a/lightning-invoice/src/de.rs b/lightning-invoice/src/de.rs index 2ed356da..b4eb04a3 100644 --- a/lightning-invoice/src/de.rs +++ b/lightning-invoice/src/de.rs @@ -1,34 +1,36 @@ +#[cfg(feature = "std")] use std::error; -use std::fmt; -use std::fmt::{Display, Formatter}; -use std::num::ParseIntError; -use std::str; -use std::str::FromStr; +use core::fmt; +use core::fmt::{Display, Formatter}; +use core::num::ParseIntError; +use core::str; +use core::str::FromStr; use bech32; use bech32::{u5, FromBase32}; use bitcoin_hashes::Hash; use bitcoin_hashes::sha256; +use crate::prelude::*; use lightning::ln::PaymentSecret; -use lightning::routing::network_graph::RoutingFees; +use lightning::routing::gossip::RoutingFees; use lightning::routing::router::{RouteHint, RouteHintHop}; use num_traits::{CheckedAdd, CheckedMul}; use secp256k1; -use secp256k1::recovery::{RecoveryId, RecoverableSignature}; -use secp256k1::key::PublicKey; +use secp256k1::ecdsa::{RecoveryId, RecoverableSignature}; +use secp256k1::PublicKey; use super::{Invoice, Sha256, TaggedField, ExpiryTime, MinFinalCltvExpiry, Fallback, PayeePubKey, InvoiceSignature, PositiveTimestamp, - SemanticError, PrivateRoute, Description, RawTaggedField, Currency, RawHrp, SiPrefix, RawInvoice, constants, SignedRawInvoice, - RawDataPart, CreationError, InvoiceFeatures}; + SemanticError, PrivateRoute, ParseError, ParseOrSemanticError, Description, RawTaggedField, Currency, RawHrp, SiPrefix, RawInvoice, + constants, SignedRawInvoice, RawDataPart, InvoiceFeatures}; use self::hrp_sm::parse_hrp; /// State machine to parse the hrp mod hrp_sm { - use std::ops::Range; + use core::ops::Range; #[derive(PartialEq, Eq, Debug)] enum States { @@ -77,7 +79,7 @@ mod hrp_sm { } else if ['m', 'u', 'n', 'p'].contains(&read_symbol) { Ok(States::ParseAmountSiPrefix) } else { - Err(super::ParseError::MalformedHRP) + Err(super::ParseError::UnknownSiPrefix) } }, States::ParseAmountSiPrefix => Err(super::ParseError::MalformedHRP), @@ -357,7 +359,6 @@ impl FromBase32 for PositiveTimestamp { .expect("7*5bit < 64bit, no overflow possible"); match PositiveTimestamp::from_unix_timestamp(timestamp) { Ok(t) => Ok(t), - Err(CreationError::TimestampOutOfBounds) => Err(ParseError::TimestampOverflow), Err(_) => unreachable!(), } } @@ -419,7 +420,7 @@ fn parse_tagged_parts(data: &[u5]) -> Result, ParseError> { Ok(field) => { parts.push(RawTaggedField::KnownSemantics(field)) }, - Err(ParseError::Skip) => { + Err(ParseError::Skip)|Err(ParseError::Bech32Error(bech32::Error::InvalidLength)) => { parts.push(RawTaggedField::UnknownSemantics(field.into())) }, Err(e) => {return Err(e)} @@ -514,7 +515,7 @@ impl FromBase32 for ExpiryTime { fn from_base32(field_data: &[u5]) -> Result { match parse_int_be::(field_data, 32) - .and_then(|t| ExpiryTime::from_seconds(t).ok()) // ok, since the only error is out of bounds + .map(|t| ExpiryTime::from_seconds(t)) { Some(t) => Ok(t), None => Err(ParseError::IntegerOverflowError), @@ -618,47 +619,6 @@ impl FromBase32 for PrivateRoute { } } -/// 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, - TimestampOverflow, -} - -/// 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), -} - impl Display for ParseError { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match *self { @@ -707,9 +667,6 @@ impl Display for ParseError { ParseError::Skip => { f.write_str("the tagged field has to be skipped because of an unexpected, but allowed property") }, - ParseError::TimestampOverflow => { - f.write_str("the invoice's timestamp could not be represented as SystemTime") - }, } } } @@ -723,8 +680,10 @@ impl Display for ParseOrSemanticError { } } +#[cfg(feature = "std")] impl error::Error for ParseError {} +#[cfg(feature = "std")] impl error::Error for ParseOrSemanticError {} macro_rules! from_error { @@ -873,7 +832,7 @@ mod test { use bech32::FromBase32; let input = from_bech32("pu".as_bytes()); - let expected = Ok(ExpiryTime::from_seconds(60).unwrap()); + let expected = Ok(ExpiryTime::from_seconds(60)); assert_eq!(ExpiryTime::from_base32(&input), expected); let input_too_large = from_bech32("sqqqqqqqqqqqq".as_bytes()); @@ -950,7 +909,7 @@ mod test { #[test] fn test_parse_route() { - use lightning::routing::network_graph::RoutingFees; + use lightning::routing::gossip::RoutingFees; use lightning::routing::router::{RouteHint, RouteHintHop}; use ::PrivateRoute; use bech32::FromBase32; @@ -1008,7 +967,7 @@ mod test { #[test] fn test_payment_secret_and_features_de_and_ser() { use lightning::ln::features::InvoiceFeatures; - use secp256k1::recovery::{RecoveryId, RecoverableSignature}; + use secp256k1::ecdsa::{RecoveryId, RecoverableSignature}; use TaggedField::*; use {SiPrefix, SignedRawInvoice, InvoiceSignature, RawInvoice, RawHrp, RawDataPart, Currency, Sha256, PositiveTimestamp}; @@ -1055,7 +1014,7 @@ mod test { #[test] fn test_raw_signed_invoice_deserialization() { use TaggedField::*; - use secp256k1::recovery::{RecoveryId, RecoverableSignature}; + use secp256k1::ecdsa::{RecoveryId, RecoverableSignature}; use {SignedRawInvoice, InvoiceSignature, RawInvoice, RawHrp, RawDataPart, Currency, Sha256, PositiveTimestamp};