X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-invoice%2Fsrc%2Fde.rs;h=925d7265c553526d993f94b29354adcac1db9ac1;hb=1ceb41e08b2d76b23d2505a10a88db8d840895ca;hp=92c1cb5c28f5385b5eb2f37c2cbbac62b58ac398;hpb=ccf92157620da45032d75f06b5972eaf142c1ce3;p=rust-lightning diff --git a/lightning-invoice/src/de.rs b/lightning-invoice/src/de.rs index 92c1cb5c..925d7265 100644 --- a/lightning-invoice/src/de.rs +++ b/lightning-invoice/src/de.rs @@ -1,14 +1,16 @@ #[cfg(feature = "std")] use std::error; +use core::convert::TryFrom; 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::{PubkeyHash, ScriptHash}; +use bitcoin::util::address::WitnessVersion; use bitcoin_hashes::Hash; use bitcoin_hashes::sha256; use crate::prelude::*; @@ -18,7 +20,6 @@ use lightning::routing::router::{RouteHint, RouteHintHop}; use num_traits::{CheckedAdd, CheckedMul}; -use secp256k1; use secp256k1::ecdsa::{RecoveryId, RecoverableSignature}; use secp256k1::PublicKey; @@ -323,9 +324,9 @@ impl FromStr for RawHrp { }; Ok(RawHrp { - currency: currency, + currency, raw_amount: amount, - si_prefix: si_prefix, + si_prefix, }) } } @@ -342,7 +343,7 @@ impl FromBase32 for RawDataPart { let tagged = parse_tagged_parts(&data[7..])?; Ok(RawDataPart { - timestamp: timestamp, + timestamp, tagged_fields: tagged, }) } @@ -515,7 +516,7 @@ impl FromBase32 for ExpiryTime { fn from_base32(field_data: &[u5]) -> Result { match parse_int_be::(field_data, 32) - .map(|t| ExpiryTime::from_seconds(t)) + .map(ExpiryTime::from_seconds) { Some(t) => Ok(t), None => Err(ParseError::IntegerOverflowError), @@ -540,7 +541,7 @@ impl FromBase32 for Fallback { type Err = ParseError; fn from_base32(field_data: &[u5]) -> Result { - if field_data.len() < 1 { + if field_data.is_empty() { return Err(ParseError::UnexpectedEndOfTaggedFields); } @@ -552,27 +553,24 @@ impl FromBase32 for Fallback { if bytes.len() < 2 || bytes.len() > 40 { return Err(ParseError::InvalidSegWitProgramLength); } - + let version = WitnessVersion::try_from(version).expect("0 through 16 are valid SegWit versions"); Ok(Fallback::SegWitProgram { - version: version, + version, program: bytes }) }, 17 => { - if bytes.len() != 20 { - return Err(ParseError::InvalidPubKeyHashLength); - } - //TODO: refactor once const generics are available - let mut pkh = [0u8; 20]; - pkh.copy_from_slice(&bytes); + let pkh = match PubkeyHash::from_slice(&bytes) { + Ok(pkh) => pkh, + Err(bitcoin_hashes::Error::InvalidLength(_, _)) => return Err(ParseError::InvalidPubKeyHashLength), + }; Ok(Fallback::PubKeyHash(pkh)) } 18 => { - if bytes.len() != 20 { - return Err(ParseError::InvalidScriptHashLength); - } - let mut sh = [0u8; 20]; - sh.copy_from_slice(&bytes); + let sh = match ScriptHash::from_slice(&bytes) { + Ok(sh) => sh, + Err(bitcoin_hashes::Error::InvalidLength(_, _)) => return Err(ParseError::InvalidScriptHashLength), + }; Ok(Fallback::ScriptHash(sh)) } _ => Err(ParseError::Skip) @@ -854,26 +852,29 @@ mod test { fn test_parse_fallback() { use crate::Fallback; use bech32::FromBase32; + use bitcoin::{PubkeyHash, ScriptHash}; + use bitcoin::util::address::WitnessVersion; + use bitcoin_hashes::Hash; let cases = vec![ ( from_bech32("3x9et2e20v6pu37c5d9vax37wxq72un98".as_bytes()), - Ok(Fallback::PubKeyHash([ + Ok(Fallback::PubKeyHash(PubkeyHash::from_slice(&[ 0x31, 0x72, 0xb5, 0x65, 0x4f, 0x66, 0x83, 0xc8, 0xfb, 0x14, 0x69, 0x59, 0xd3, 0x47, 0xce, 0x30, 0x3c, 0xae, 0x4c, 0xa7 - ])) + ]).unwrap())) ), ( from_bech32("j3a24vwu6r8ejrss3axul8rxldph2q7z9".as_bytes()), - Ok(Fallback::ScriptHash([ + Ok(Fallback::ScriptHash(ScriptHash::from_slice(&[ 0x8f, 0x55, 0x56, 0x3b, 0x9a, 0x19, 0xf3, 0x21, 0xc2, 0x11, 0xe9, 0xb9, 0xf3, 0x8c, 0xdf, 0x68, 0x6e, 0xa0, 0x78, 0x45 - ])) + ]).unwrap())) ), ( from_bech32("qw508d6qejxtdg4y5r3zarvary0c5xw7k".as_bytes()), Ok(Fallback::SegWitProgram { - version: u5::try_from_u8(0).unwrap(), + version: WitnessVersion::V0, program: Vec::from(&[ 0x75u8, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6