pub mod privkey;
pub mod address;
pub mod base58;
pub mod bip32;
pub mod bip143;
pub mod contracthash;
pub mod decimal;
pub mod hash;
pub mod iter;
pub mod misc;
pub mod uint;
#[cfg(feature = "fuzztarget")]
pub mod sha2;
use std::{error, fmt, io};
use bitcoin_bech32;
use secp256k1;
pub trait BitArray {
fn bit(&self, idx: usize) -> bool;
fn bit_slice(&self, start: usize, end: usize) -> Self;
fn mask(&self, n: usize) -> Self;
fn trailing_zeros(&self) -> usize;
fn zero() -> Self;
fn one() -> Self;
}
#[derive(Debug)]
pub enum Error {
Io(io::Error),
Base58(base58::Error),
Bech32(bitcoin_bech32::Error),
ByteOrder(io::Error),
BadNetworkMagic(u32, u32),
BadNetworkMessage(String),
DuplicateHash,
BlockNotFound,
ParseFailed,
PrevHashNotFound,
Secp256k1(secp256k1::Error),
SpvBadTarget,
SpvBadProofOfWork,
Detail(String, Box<Error>),
UnsupportedWitnessVersion(u8)
}
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::Bech32(ref e) => fmt::Display::fmt(e, f),
Error::ByteOrder(ref e) => fmt::Display::fmt(e, f),
Error::BadNetworkMagic(exp, got) => write!(f, "expected network magic 0x{:x}, got 0x{:x}", exp, got),
Error::BadNetworkMessage(ref got) => write!(f, "incorrect network message {}", got),
Error::Detail(ref s, ref e) => write!(f, "{}: {}", s, e),
Error::Secp256k1(ref e) => fmt::Display::fmt(e, f),
ref x => f.write_str(error::Error::description(x))
}
}
}
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::Bech32(ref e) => Some(e),
Error::ByteOrder(ref e) => Some(e),
Error::Detail(_, ref e) => Some(e),
Error::Secp256k1(ref e) => Some(e),
_ => None
}
}
fn description(&self) -> &str {
match *self {
Error::Io(ref e) => e.description(),
Error::Base58(ref e) => e.description(),
Error::Bech32(ref e) => e.description(),
Error::ByteOrder(ref e) => e.description(),
Error::BadNetworkMagic(_, _) => "incorrect network magic",
Error::BadNetworkMessage(_) => "incorrect/unexpected network message",
Error::DuplicateHash => "duplicate hash",
Error::BlockNotFound => "no such block",
Error::ParseFailed => "parsing error",
Error::PrevHashNotFound => "prevhash not found",
Error::Secp256k1(ref e) => e.description(),
Error::SpvBadTarget => "target incorrect",
Error::SpvBadProofOfWork => "target correct but not attained",
Error::Detail(_, ref e) => e.description(),
Error::UnsupportedWitnessVersion(_) => "unsupported witness version"
}
}
}
pub fn propagate_err<T>(s: String, res: Result<T, Error>) -> Result<T, Error> {
res.map_err(|err| Error::Detail(s, Box::new(err)))
}
impl From<base58::Error> for Error {
fn from(e: base58::Error) -> Error {
Error::Base58(e)
}
}
impl From<bitcoin_bech32::Error> for Error {
fn from(e: bitcoin_bech32::Error) -> Error {
Error::Bech32(e)
}
}
impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Error {
Error::Secp256k1(e)
}
}