bitcoin/util/
mod.rs

1// Rust Bitcoin Library
2// Written in 2014 by
3//     Andrew Poelstra <apoelstra@wpsoftware.net>
4//
5// To the extent possible under law, the author(s) have dedicated all
6// copyright and related and neighboring rights to this software to
7// the public domain worldwide. This software is distributed without
8// any warranty.
9//
10// You should have received a copy of the CC0 Public Domain Dedication
11// along with this software.
12// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
13//
14
15//! Utility functions
16//!
17//! Functions needed by all parts of the Bitcoin library
18
19pub mod privkey;
20pub mod address;
21pub mod base58;
22pub mod bip32;
23pub mod bip143;
24pub mod contracthash;
25pub mod decimal;
26pub mod hash;
27pub mod misc;
28pub mod uint;
29
30use std::{error, fmt};
31
32use secp256k1;
33
34use network;
35use consensus::encode;
36
37/// A trait which allows numbers to act as fixed-size bit arrays
38pub trait BitArray {
39    /// Is bit set?
40    fn bit(&self, idx: usize) -> bool;
41
42    /// Returns an array which is just the bits from start to end
43    fn bit_slice(&self, start: usize, end: usize) -> Self;
44
45    /// Bitwise and with `n` ones
46    fn mask(&self, n: usize) -> Self;
47
48    /// Trailing zeros
49    fn trailing_zeros(&self) -> usize;
50
51    /// Create all-zeros value
52    fn zero() -> Self;
53
54    /// Create value representing one
55    fn one() -> Self;
56}
57
58/// A general error code, other errors should implement conversions to/from this
59/// if appropriate.
60#[derive(Debug)]
61pub enum Error {
62    /// secp-related error
63    Secp256k1(secp256k1::Error),
64    /// Encoding error
65    Encode(encode::Error),
66    /// Network error
67    Network(network::Error),
68    /// The header hash is not below the target
69    SpvBadProofOfWork,
70    /// The `target` field of a block header did not match the expected difficulty
71    SpvBadTarget,
72}
73
74impl fmt::Display for Error {
75    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76        match *self {
77            Error::Secp256k1(ref e) => fmt::Display::fmt(e, f),
78            Error::Encode(ref e) => fmt::Display::fmt(e, f),
79            Error::Network(ref e) => fmt::Display::fmt(e, f),
80            Error::SpvBadProofOfWork | Error::SpvBadTarget => f.write_str(error::Error::description(self)),
81        }
82    }
83}
84
85impl error::Error for Error {
86    fn cause(&self) -> Option<&error::Error> {
87        match *self {
88            Error::Secp256k1(ref e) => Some(e),
89            Error::Encode(ref e) => Some(e),
90            Error::Network(ref e) => Some(e),
91            Error::SpvBadProofOfWork | Error::SpvBadTarget => None
92        }
93    }
94
95    fn description(&self) -> &str {
96        match *self {
97            Error::Secp256k1(ref e) => e.description(),
98            Error::Encode(ref e) => e.description(),
99            Error::Network(ref e) => e.description(),
100            Error::SpvBadProofOfWork => "target correct but not attained",
101            Error::SpvBadTarget => "target incorrect",
102        }
103    }
104}
105
106#[doc(hidden)]
107impl From<secp256k1::Error> for Error {
108    fn from(e: secp256k1::Error) -> Error {
109        Error::Secp256k1(e)
110    }
111}
112
113#[doc(hidden)]
114impl From<encode::Error> for Error {
115    fn from(e: encode::Error) -> Error {
116        Error::Encode(e)
117    }
118}
119
120#[doc(hidden)]
121impl From<network::Error> for Error {
122    fn from(e: network::Error) -> Error {
123        Error::Network(e)
124    }
125}