2014-07-18 13:56:17 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written in 2014 by
|
2015-04-07 01:51:11 +00:00
|
|
|
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
2014-07-18 13:56:17 +00:00
|
|
|
//
|
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
2018-08-08 21:38:50 +00:00
|
|
|
//! Utility functions
|
2014-07-18 13:56:17 +00:00
|
|
|
//!
|
|
|
|
//! Functions needed by all parts of the Bitcoin library
|
|
|
|
|
2018-03-14 03:53:03 +00:00
|
|
|
pub mod privkey;
|
2015-10-14 20:29:19 +00:00
|
|
|
pub mod address;
|
2014-08-25 06:03:47 +00:00
|
|
|
pub mod base58;
|
2016-06-24 19:15:57 +00:00
|
|
|
pub mod bip32;
|
2018-01-15 17:54:32 +00:00
|
|
|
pub mod bip143;
|
2015-10-14 20:29:19 +00:00
|
|
|
pub mod contracthash;
|
2015-11-22 16:51:19 +00:00
|
|
|
pub mod decimal;
|
2014-07-18 13:56:17 +00:00
|
|
|
pub mod hash;
|
|
|
|
pub mod iter;
|
|
|
|
pub mod misc;
|
|
|
|
pub mod uint;
|
|
|
|
|
2018-03-20 16:05:33 +00:00
|
|
|
#[cfg(feature = "fuzztarget")]
|
|
|
|
pub mod sha2;
|
|
|
|
|
2018-08-17 15:25:11 +00:00
|
|
|
use std::{error, fmt};
|
2015-04-07 01:51:11 +00:00
|
|
|
|
2018-03-09 20:27:13 +00:00
|
|
|
use secp256k1;
|
|
|
|
|
2018-08-17 15:25:11 +00:00
|
|
|
use network;
|
|
|
|
use network::serialize;
|
|
|
|
|
2014-07-18 13:56:17 +00:00
|
|
|
/// A trait which allows numbers to act as fixed-size bit arrays
|
|
|
|
pub trait BitArray {
|
2015-04-07 01:51:11 +00:00
|
|
|
/// Is bit set?
|
|
|
|
fn bit(&self, idx: usize) -> bool;
|
|
|
|
|
|
|
|
/// Returns an array which is just the bits from start to end
|
|
|
|
fn bit_slice(&self, start: usize, end: usize) -> Self;
|
|
|
|
|
|
|
|
/// Bitwise and with `n` ones
|
|
|
|
fn mask(&self, n: usize) -> Self;
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2015-04-07 01:51:11 +00:00
|
|
|
/// Trailing zeros
|
|
|
|
fn trailing_zeros(&self) -> usize;
|
2018-02-18 15:21:05 +00:00
|
|
|
|
|
|
|
/// Create all-zeros value
|
|
|
|
fn zero() -> Self;
|
|
|
|
|
|
|
|
/// Create value represeting one
|
|
|
|
fn one() -> Self;
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2018-08-21 08:31:54 +00:00
|
|
|
/// A general error code, other errors should implement conversions to/from this
|
|
|
|
/// if appropriate.
|
2015-04-07 01:51:11 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2018-08-17 15:25:11 +00:00
|
|
|
/// secp-related error
|
|
|
|
Secp256k1(secp256k1::Error),
|
|
|
|
/// Serialization error
|
|
|
|
Serialize(serialize::Error),
|
|
|
|
/// Network error
|
|
|
|
Network(network::Error),
|
2018-08-21 08:31:54 +00:00
|
|
|
/// The header hash is not below the target
|
|
|
|
SpvBadProofOfWork,
|
|
|
|
/// The `target` field of a block header did not match the expected difficulty
|
|
|
|
SpvBadTarget,
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
2016-06-20 01:25:54 +00:00
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2018-03-09 20:27:13 +00:00
|
|
|
Error::Secp256k1(ref e) => fmt::Display::fmt(e, f),
|
2018-08-17 15:25:11 +00:00
|
|
|
Error::Serialize(ref e) => fmt::Display::fmt(e, f),
|
2018-08-21 08:42:12 +00:00
|
|
|
Error::Network(ref e) => fmt::Display::fmt(e, f),
|
|
|
|
Error::SpvBadProofOfWork | Error::SpvBadTarget => f.write_str(error::Error::description(self)),
|
2016-06-20 01:25:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for Error {
|
|
|
|
fn cause(&self) -> Option<&error::Error> {
|
|
|
|
match *self {
|
2018-03-09 20:27:13 +00:00
|
|
|
Error::Secp256k1(ref e) => Some(e),
|
2018-08-17 15:25:11 +00:00
|
|
|
Error::Serialize(ref e) => Some(e),
|
2018-08-21 08:42:12 +00:00
|
|
|
Error::Network(ref e) => Some(e),
|
|
|
|
Error::SpvBadProofOfWork | Error::SpvBadTarget => None
|
2016-06-20 01:25:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
match *self {
|
2018-03-09 20:27:13 +00:00
|
|
|
Error::Secp256k1(ref e) => e.description(),
|
2018-08-17 15:25:11 +00:00
|
|
|
Error::Serialize(ref e) => e.description(),
|
|
|
|
Error::Network(ref e) => e.description(),
|
2018-08-21 08:31:54 +00:00
|
|
|
Error::SpvBadProofOfWork => "target correct but not attained",
|
|
|
|
Error::SpvBadTarget => "target incorrect",
|
2016-06-20 01:25:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2018-08-12 16:57:59 +00:00
|
|
|
#[doc(hidden)]
|
2018-08-17 15:25:11 +00:00
|
|
|
impl From<secp256k1::Error> for Error {
|
|
|
|
fn from(e: secp256k1::Error) -> Error {
|
|
|
|
Error::Secp256k1(e)
|
2018-03-09 20:27:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:57:59 +00:00
|
|
|
#[doc(hidden)]
|
2018-08-17 15:25:11 +00:00
|
|
|
impl From<serialize::Error> for Error {
|
|
|
|
fn from(e: serialize::Error) -> Error {
|
|
|
|
Error::Serialize(e)
|
2018-03-09 20:27:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:57:59 +00:00
|
|
|
#[doc(hidden)]
|
2018-08-17 15:25:11 +00:00
|
|
|
impl From<network::Error> for Error {
|
|
|
|
fn from(e: network::Error) -> Error {
|
|
|
|
Error::Network(e)
|
2018-03-09 20:27:13 +00:00
|
|
|
}
|
|
|
|
}
|