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.
This commit is contained in:
Carl Dong 2019-01-21 14:29:50 -05:00
parent fc448ba47c
commit a944c7fbd0
3 changed files with 15 additions and 23 deletions

View File

@ -42,6 +42,7 @@ use hex::encode as hex_encode;
use bitcoin_bech32; use bitcoin_bech32;
use bitcoin_hashes::{sha256d, Hash as HashTrait}; use bitcoin_hashes::{sha256d, Hash as HashTrait};
use secp256k1;
use util::base58; use util::base58;
@ -56,6 +57,8 @@ pub enum Error {
Bech32(bitcoin_bech32::Error), Bech32(bitcoin_bech32::Error),
/// Error from the `byteorder` crate /// Error from the `byteorder` crate
ByteOrder(io::Error), ByteOrder(io::Error),
/// secp-related error
Secp256k1(secp256k1::Error),
/// Network magic was not expected /// Network magic was not expected
UnexpectedNetworkMagic { UnexpectedNetworkMagic {
/// The expected network magic /// The expected network magic
@ -98,6 +101,7 @@ impl fmt::Display for Error {
Error::Base58(ref e) => fmt::Display::fmt(e, f), Error::Base58(ref e) => fmt::Display::fmt(e, f),
Error::Bech32(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::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::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::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)), 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::Base58(ref e) => Some(e),
Error::Bech32(ref e) => Some(e), Error::Bech32(ref e) => Some(e),
Error::ByteOrder(ref e) => Some(e), Error::ByteOrder(ref e) => Some(e),
Error::Secp256k1(ref e) => Some(e),
Error::UnexpectedNetworkMagic { .. } Error::UnexpectedNetworkMagic { .. }
| Error::OversizedVectorAllocation { .. } | Error::OversizedVectorAllocation { .. }
| Error::InvalidChecksum { .. } | Error::InvalidChecksum { .. }
@ -136,6 +141,7 @@ impl error::Error for Error {
Error::Base58(ref e) => e.description(), Error::Base58(ref e) => e.description(),
Error::Bech32(ref e) => e.description(), Error::Bech32(ref e) => e.description(),
Error::ByteOrder(ref e) => e.description(), Error::ByteOrder(ref e) => e.description(),
Error::Secp256k1(ref e) => e.description(),
Error::UnexpectedNetworkMagic { .. } => "unexpected network magic", Error::UnexpectedNetworkMagic { .. } => "unexpected network magic",
Error::OversizedVectorAllocation { .. } => "allocation of oversized vector requested", Error::OversizedVectorAllocation { .. } => "allocation of oversized vector requested",
Error::InvalidChecksum { .. } => "invalid checksum", Error::InvalidChecksum { .. } => "invalid checksum",
@ -163,6 +169,12 @@ impl From<bitcoin_bech32::Error> for Error {
} }
} }
#[doc(hidden)]
impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Error {
Error::Secp256k1(e)
}
}
#[doc(hidden)] #[doc(hidden)]
impl From<io::Error> for Error { impl From<io::Error> for Error {

View File

@ -46,18 +46,15 @@ impl PublicKey {
/// Deserialize a public key from a slice /// Deserialize a public key from a slice
pub fn from_slice(data: &[u8]) -> Result<PublicKey, encode::Error> { pub fn from_slice(data: &[u8]) -> Result<PublicKey, encode::Error> {
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() { let compressed: bool = match data.len() {
33 => true, 33 => true,
65 => false, 65 => false,
_ => { return Err(base58::Error::InvalidLength(data.len()).into()); }, len => { return Err(base58::Error::InvalidLength(len).into()); },
}; };
Ok(PublicKey { Ok(PublicKey {
compressed: compressed, 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]))); } 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 { Ok(PrivateKey {
compressed: compressed, compressed: compressed,
network: network, network: network,
key: key key: secp256k1::SecretKey::from_slice(&data[1..33])?,
}) })
} }
} }

View File

@ -29,8 +29,6 @@ pub mod uint;
use std::{error, fmt}; use std::{error, fmt};
use secp256k1;
use network; use network;
use consensus::encode; use consensus::encode;
@ -59,8 +57,6 @@ pub trait BitArray {
/// if appropriate. /// if appropriate.
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
/// secp-related error
Secp256k1(secp256k1::Error),
/// Encoding error /// Encoding error
Encode(encode::Error), Encode(encode::Error),
/// Network error /// Network error
@ -74,7 +70,6 @@ pub enum Error {
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
Error::Secp256k1(ref e) => fmt::Display::fmt(e, f),
Error::Encode(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::Network(ref e) => fmt::Display::fmt(e, f),
Error::SpvBadProofOfWork | Error::SpvBadTarget => f.write_str(error::Error::description(self)), 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 { impl error::Error for Error {
fn cause(&self) -> Option<&error::Error> { fn cause(&self) -> Option<&error::Error> {
match *self { match *self {
Error::Secp256k1(ref e) => Some(e),
Error::Encode(ref e) => Some(e), Error::Encode(ref e) => Some(e),
Error::Network(ref e) => Some(e), Error::Network(ref e) => Some(e),
Error::SpvBadProofOfWork | Error::SpvBadTarget => None Error::SpvBadProofOfWork | Error::SpvBadTarget => None
@ -94,7 +88,6 @@ impl error::Error for Error {
fn description(&self) -> &str { fn description(&self) -> &str {
match *self { match *self {
Error::Secp256k1(ref e) => e.description(),
Error::Encode(ref e) => e.description(), Error::Encode(ref e) => e.description(),
Error::Network(ref e) => e.description(), Error::Network(ref e) => e.description(),
Error::SpvBadProofOfWork => "target correct but not attained", Error::SpvBadProofOfWork => "target correct but not attained",
@ -103,13 +96,6 @@ impl error::Error for Error {
} }
} }
#[doc(hidden)]
impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Error {
Error::Secp256k1(e)
}
}
#[doc(hidden)] #[doc(hidden)]
impl From<encode::Error> for Error { impl From<encode::Error> for Error {
fn from(e: encode::Error) -> Error { fn from(e: encode::Error) -> Error {