Introduce key::Error
This commit is contained in:
parent
1d209362eb
commit
725884bab5
|
@ -41,7 +41,6 @@ use hashes::{sha256d, Hash as HashTrait};
|
||||||
use secp256k1;
|
use secp256k1;
|
||||||
|
|
||||||
use util::endian;
|
use util::endian;
|
||||||
use util::base58;
|
|
||||||
use util::psbt;
|
use util::psbt;
|
||||||
|
|
||||||
use blockdata::transaction::{TxOut, Transaction, TxIn};
|
use blockdata::transaction::{TxOut, Transaction, TxIn};
|
||||||
|
@ -53,8 +52,6 @@ use network::address::Address;
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// And I/O error
|
/// And I/O error
|
||||||
Io(io::Error),
|
Io(io::Error),
|
||||||
/// Base58 encoding error
|
|
||||||
Base58(base58::Error),
|
|
||||||
/// Error from the `byteorder` crate
|
/// Error from the `byteorder` crate
|
||||||
ByteOrder(io::Error),
|
ByteOrder(io::Error),
|
||||||
/// secp-related error
|
/// secp-related error
|
||||||
|
@ -98,7 +95,6 @@ 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::Io(ref e) => fmt::Display::fmt(e, f),
|
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::ByteOrder(ref e) => fmt::Display::fmt(e, f),
|
||||||
Error::Secp256k1(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),
|
Error::Psbt(ref e) => fmt::Display::fmt(e, f),
|
||||||
|
@ -118,7 +114,6 @@ impl error::Error for Error {
|
||||||
fn cause(&self) -> Option<&error::Error> {
|
fn cause(&self) -> Option<&error::Error> {
|
||||||
match *self {
|
match *self {
|
||||||
Error::Io(ref e) => Some(e),
|
Error::Io(ref e) => Some(e),
|
||||||
Error::Base58(ref e) => Some(e),
|
|
||||||
Error::ByteOrder(ref e) => Some(e),
|
Error::ByteOrder(ref e) => Some(e),
|
||||||
Error::Secp256k1(ref e) => Some(e),
|
Error::Secp256k1(ref e) => Some(e),
|
||||||
Error::Psbt(ref e) => Some(e),
|
Error::Psbt(ref e) => Some(e),
|
||||||
|
@ -136,7 +131,6 @@ impl error::Error for Error {
|
||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
match *self {
|
match *self {
|
||||||
Error::Io(ref e) => e.description(),
|
Error::Io(ref e) => e.description(),
|
||||||
Error::Base58(ref e) => e.description(),
|
|
||||||
Error::ByteOrder(ref e) => e.description(),
|
Error::ByteOrder(ref e) => e.description(),
|
||||||
Error::Secp256k1(ref e) => e.description(),
|
Error::Secp256k1(ref e) => e.description(),
|
||||||
Error::Psbt(ref e) => e.description(),
|
Error::Psbt(ref e) => e.description(),
|
||||||
|
@ -153,11 +147,6 @@ impl error::Error for Error {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
impl From<base58::Error> for Error {
|
|
||||||
fn from(e: base58::Error) -> Error {
|
|
||||||
Error::Base58(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
impl From<secp256k1::Error> for Error {
|
impl From<secp256k1::Error> for Error {
|
||||||
|
|
|
@ -17,14 +17,59 @@
|
||||||
//!
|
//!
|
||||||
|
|
||||||
use std::fmt::{self, Write};
|
use std::fmt::{self, Write};
|
||||||
use std::{io, ops};
|
use std::{io, ops, error};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use secp256k1::{self, Secp256k1};
|
use secp256k1::{self, Secp256k1};
|
||||||
use consensus::encode;
|
|
||||||
use network::constants::Network;
|
use network::constants::Network;
|
||||||
use util::base58;
|
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<base58::Error> for Error {
|
||||||
|
fn from(e: base58::Error) -> Error {
|
||||||
|
Error::Base58(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
impl From<secp256k1::Error> for Error {
|
||||||
|
fn from(e: secp256k1::Error) -> Error {
|
||||||
|
Error::Secp256k1(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A Bitcoin ECDSA public key
|
/// A Bitcoin ECDSA public key
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct PublicKey {
|
pub struct PublicKey {
|
||||||
|
@ -53,7 +98,7 @@ 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, Error> {
|
||||||
let compressed: bool = match data.len() {
|
let compressed: bool = match data.len() {
|
||||||
33 => true,
|
33 => true,
|
||||||
65 => false,
|
65 => false,
|
||||||
|
@ -88,8 +133,8 @@ impl fmt::Display for PublicKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for PublicKey {
|
impl FromStr for PublicKey {
|
||||||
type Err = encode::Error;
|
type Err = Error;
|
||||||
fn from_str(s: &str) -> Result<PublicKey, encode::Error> {
|
fn from_str(s: &str) -> Result<PublicKey, Error> {
|
||||||
let key = secp256k1::PublicKey::from_str(s)?;
|
let key = secp256k1::PublicKey::from_str(s)?;
|
||||||
Ok(PublicKey {
|
Ok(PublicKey {
|
||||||
key: key,
|
key: key,
|
||||||
|
@ -149,19 +194,19 @@ impl PrivateKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse WIF encoded private key.
|
/// Parse WIF encoded private key.
|
||||||
pub fn from_wif(wif: &str) -> Result<PrivateKey, encode::Error> {
|
pub fn from_wif(wif: &str) -> Result<PrivateKey, Error> {
|
||||||
let data = base58::from_check(wif)?;
|
let data = base58::from_check(wif)?;
|
||||||
|
|
||||||
let compressed = match data.len() {
|
let compressed = match data.len() {
|
||||||
33 => false,
|
33 => false,
|
||||||
34 => true,
|
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] {
|
let network = match data[0] {
|
||||||
128 => Network::Bitcoin,
|
128 => Network::Bitcoin,
|
||||||
239 => Network::Testnet,
|
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 {
|
Ok(PrivateKey {
|
||||||
|
@ -185,8 +230,8 @@ impl fmt::Debug for PrivateKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for PrivateKey {
|
impl FromStr for PrivateKey {
|
||||||
type Err = encode::Error;
|
type Err = Error;
|
||||||
fn from_str(s: &str) -> Result<PrivateKey, encode::Error> {
|
fn from_str(s: &str) -> Result<PrivateKey, Error> {
|
||||||
PrivateKey::from_wif(s)
|
PrivateKey::from_wif(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue