From 725884bab5fcb5e946764d55d548f82cf18d05ea Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Mon, 9 Sep 2019 10:53:50 +0100 Subject: [PATCH] Introduce key::Error --- src/consensus/encode.rs | 11 ------- src/util/key.rs | 65 ++++++++++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/src/consensus/encode.rs b/src/consensus/encode.rs index 63a91227..b3b7c4a8 100644 --- a/src/consensus/encode.rs +++ b/src/consensus/encode.rs @@ -41,7 +41,6 @@ use hashes::{sha256d, Hash as HashTrait}; use secp256k1; use util::endian; -use util::base58; use util::psbt; use blockdata::transaction::{TxOut, Transaction, TxIn}; @@ -53,8 +52,6 @@ use network::address::Address; pub enum Error { /// And I/O error Io(io::Error), - /// Base58 encoding error - Base58(base58::Error), /// Error from the `byteorder` crate ByteOrder(io::Error), /// secp-related error @@ -98,7 +95,6 @@ impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Error::Io(ref e) => fmt::Display::fmt(e, f), - Error::Base58(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::Psbt(ref e) => fmt::Display::fmt(e, f), @@ -118,7 +114,6 @@ impl error::Error for Error { fn cause(&self) -> Option<&error::Error> { match *self { Error::Io(ref e) => Some(e), - Error::Base58(ref e) => Some(e), Error::ByteOrder(ref e) => Some(e), Error::Secp256k1(ref e) => Some(e), Error::Psbt(ref e) => Some(e), @@ -136,7 +131,6 @@ impl error::Error for Error { fn description(&self) -> &str { match *self { Error::Io(ref e) => e.description(), - Error::Base58(ref e) => e.description(), Error::ByteOrder(ref e) => e.description(), Error::Secp256k1(ref e) => e.description(), Error::Psbt(ref e) => e.description(), @@ -153,11 +147,6 @@ impl error::Error for Error { } #[doc(hidden)] -impl From for Error { - fn from(e: base58::Error) -> Error { - Error::Base58(e) - } -} #[doc(hidden)] impl From for Error { diff --git a/src/util/key.rs b/src/util/key.rs index 07dfb017..c9f81faf 100644 --- a/src/util/key.rs +++ b/src/util/key.rs @@ -17,14 +17,59 @@ //! use std::fmt::{self, Write}; -use std::{io, ops}; +use std::{io, ops, error}; use std::str::FromStr; use secp256k1::{self, Secp256k1}; -use consensus::encode; use network::constants::Network; use util::base58; +/// A key-related error. +#[derive(Debug)] +pub enum Error { + /// Base58 encoding error + Base58(base58::Error), + /// secp256k1-related error + Secp256k1(secp256k1::Error), +} + + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Error::Base58(ref e) => write!(f, "base58 error: {}", e), + Error::Secp256k1(ref e) => write!(f, "secp256k1 error: {}", e), + } + } +} + +impl error::Error for Error { + fn cause(&self) -> Option<&error::Error> { + match *self { + Error::Base58(ref e) => Some(e), + Error::Secp256k1(ref e) => Some(e), + } + } + + fn description(&self) -> &str { + "Bitcoin key error" + } +} + +#[doc(hidden)] +impl From for Error { + fn from(e: base58::Error) -> Error { + Error::Base58(e) + } +} + +#[doc(hidden)] +impl From for Error { + fn from(e: secp256k1::Error) -> Error { + Error::Secp256k1(e) + } +} + /// A Bitcoin ECDSA public key #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct PublicKey { @@ -53,7 +98,7 @@ impl PublicKey { } /// Deserialize a public key from a slice - pub fn from_slice(data: &[u8]) -> Result { + pub fn from_slice(data: &[u8]) -> Result { let compressed: bool = match data.len() { 33 => true, 65 => false, @@ -88,8 +133,8 @@ impl fmt::Display for PublicKey { } impl FromStr for PublicKey { - type Err = encode::Error; - fn from_str(s: &str) -> Result { + type Err = Error; + fn from_str(s: &str) -> Result { let key = secp256k1::PublicKey::from_str(s)?; Ok(PublicKey { key: key, @@ -149,19 +194,19 @@ impl PrivateKey { } /// Parse WIF encoded private key. - pub fn from_wif(wif: &str) -> Result { + pub fn from_wif(wif: &str) -> Result { let data = base58::from_check(wif)?; let compressed = match data.len() { 33 => false, 34 => true, - _ => { return Err(encode::Error::Base58(base58::Error::InvalidLength(data.len()))); } + _ => { return Err(Error::Base58(base58::Error::InvalidLength(data.len()))); } }; let network = match data[0] { 128 => Network::Bitcoin, 239 => Network::Testnet, - x => { return Err(encode::Error::Base58(base58::Error::InvalidVersion(vec![x]))); } + x => { return Err(Error::Base58(base58::Error::InvalidVersion(vec![x]))); } }; Ok(PrivateKey { @@ -185,8 +230,8 @@ impl fmt::Debug for PrivateKey { } impl FromStr for PrivateKey { - type Err = encode::Error; - fn from_str(s: &str) -> Result { + type Err = Error; + fn from_str(s: &str) -> Result { PrivateKey::from_wif(s) } }