From a944c7fbd07f0bfa10adf082365d3e23f0e87959 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Mon, 21 Jan 2019 14:29:50 -0500 Subject: [PATCH] key: Use correct error for decoding This change also moves the secp256k1::Error wrapper from util::Error to consensus::encode::Error, since we do not use it anywhere else. We can add it back to util::Error once we have instances of secp256k1::Error that are not related to consensus::encode. --- src/consensus/encode.rs | 12 ++++++++++++ src/util/key.rs | 12 +++--------- src/util/mod.rs | 14 -------------- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/consensus/encode.rs b/src/consensus/encode.rs index cdbe273..9dd0526 100644 --- a/src/consensus/encode.rs +++ b/src/consensus/encode.rs @@ -42,6 +42,7 @@ use hex::encode as hex_encode; use bitcoin_bech32; use bitcoin_hashes::{sha256d, Hash as HashTrait}; +use secp256k1; use util::base58; @@ -56,6 +57,8 @@ pub enum Error { Bech32(bitcoin_bech32::Error), /// Error from the `byteorder` crate ByteOrder(io::Error), + /// secp-related error + Secp256k1(secp256k1::Error), /// Network magic was not expected UnexpectedNetworkMagic { /// The expected network magic @@ -98,6 +101,7 @@ impl fmt::Display for Error { Error::Base58(ref e) => fmt::Display::fmt(e, f), Error::Bech32(ref e) => fmt::Display::fmt(e, f), Error::ByteOrder(ref e) => fmt::Display::fmt(e, f), + Error::Secp256k1(ref e) => fmt::Display::fmt(e, f), Error::UnexpectedNetworkMagic { expected: ref e, actual: ref a } => write!(f, "{}: expected {}, actual {}", error::Error::description(self), e, a), Error::OversizedVectorAllocation { requested: ref r, max: ref m } => write!(f, "{}: requested {}, maximum {}", error::Error::description(self), r, m), Error::InvalidChecksum { expected: ref e, actual: ref a } => write!(f, "{}: expected {}, actual {}", error::Error::description(self), hex_encode(e), hex_encode(a)), @@ -118,6 +122,7 @@ impl error::Error for Error { Error::Base58(ref e) => Some(e), Error::Bech32(ref e) => Some(e), Error::ByteOrder(ref e) => Some(e), + Error::Secp256k1(ref e) => Some(e), Error::UnexpectedNetworkMagic { .. } | Error::OversizedVectorAllocation { .. } | Error::InvalidChecksum { .. } @@ -136,6 +141,7 @@ impl error::Error for Error { Error::Base58(ref e) => e.description(), Error::Bech32(ref e) => e.description(), Error::ByteOrder(ref e) => e.description(), + Error::Secp256k1(ref e) => e.description(), Error::UnexpectedNetworkMagic { .. } => "unexpected network magic", Error::OversizedVectorAllocation { .. } => "allocation of oversized vector requested", Error::InvalidChecksum { .. } => "invalid checksum", @@ -163,6 +169,12 @@ impl From for Error { } } +#[doc(hidden)] +impl From for Error { + fn from(e: secp256k1::Error) -> Error { + Error::Secp256k1(e) + } +} #[doc(hidden)] impl From for Error { diff --git a/src/util/key.rs b/src/util/key.rs index 282a3a0..2bcfdc9 100644 --- a/src/util/key.rs +++ b/src/util/key.rs @@ -46,18 +46,15 @@ impl PublicKey { /// Deserialize a public key from a slice pub fn from_slice(data: &[u8]) -> Result { - let key: secp256k1::PublicKey = secp256k1::PublicKey::from_slice(data) - .map_err(|_| base58::Error::Other("Public key out of range".to_owned()))?; - let compressed: bool = match data.len() { 33 => true, 65 => false, - _ => { return Err(base58::Error::InvalidLength(data.len()).into()); }, + len => { return Err(base58::Error::InvalidLength(len).into()); }, }; Ok(PublicKey { compressed: compressed, - key: key, + key: secp256k1::PublicKey::from_slice(data)?, }) } @@ -128,13 +125,10 @@ impl PrivateKey { x => { return Err(encode::Error::Base58(base58::Error::InvalidVersion(vec![x]))); } }; - let key = secp256k1::SecretKey::from_slice(&data[1..33]) - .map_err(|_| base58::Error::Other("Secret key out of range".to_owned()))?; - Ok(PrivateKey { compressed: compressed, network: network, - key: key + key: secp256k1::SecretKey::from_slice(&data[1..33])?, }) } } diff --git a/src/util/mod.rs b/src/util/mod.rs index 3f1508e..afad43b 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -29,8 +29,6 @@ pub mod uint; use std::{error, fmt}; -use secp256k1; - use network; use consensus::encode; @@ -59,8 +57,6 @@ pub trait BitArray { /// if appropriate. #[derive(Debug)] pub enum Error { - /// secp-related error - Secp256k1(secp256k1::Error), /// Encoding error Encode(encode::Error), /// Network error @@ -74,7 +70,6 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - Error::Secp256k1(ref e) => fmt::Display::fmt(e, f), Error::Encode(ref e) => fmt::Display::fmt(e, f), Error::Network(ref e) => fmt::Display::fmt(e, f), Error::SpvBadProofOfWork | Error::SpvBadTarget => f.write_str(error::Error::description(self)), @@ -85,7 +80,6 @@ impl fmt::Display for Error { impl error::Error for Error { fn cause(&self) -> Option<&error::Error> { match *self { - Error::Secp256k1(ref e) => Some(e), Error::Encode(ref e) => Some(e), Error::Network(ref e) => Some(e), Error::SpvBadProofOfWork | Error::SpvBadTarget => None @@ -94,7 +88,6 @@ impl error::Error for Error { fn description(&self) -> &str { match *self { - Error::Secp256k1(ref e) => e.description(), Error::Encode(ref e) => e.description(), Error::Network(ref e) => e.description(), Error::SpvBadProofOfWork => "target correct but not attained", @@ -103,13 +96,6 @@ impl error::Error for Error { } } -#[doc(hidden)] -impl From for Error { - fn from(e: secp256k1::Error) -> Error { - Error::Secp256k1(e) - } -} - #[doc(hidden)] impl From for Error { fn from(e: encode::Error) -> Error {