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 key;
20pub mod address;
21pub mod amount;
22pub mod base58;
23pub mod bip32;
24pub mod bip143;
25pub mod contracthash;
26pub mod hash;
27pub mod merkleblock;
28pub mod misc;
29pub mod psbt;
30pub mod taproot;
31pub mod uint;
32pub mod bip158;
33
34pub(crate) mod endian;
35
36use std::{error, fmt};
37
38use network;
39use consensus::encode;
40
41/// A trait which allows numbers to act as fixed-size bit arrays
42pub trait BitArray {
43    /// Is bit set?
44    fn bit(&self, idx: usize) -> bool;
45
46    /// Returns an array which is just the bits from start to end
47    fn bit_slice(&self, start: usize, end: usize) -> Self;
48
49    /// Bitwise and with `n` ones
50    fn mask(&self, n: usize) -> Self;
51
52    /// Trailing zeros
53    fn trailing_zeros(&self) -> usize;
54
55    /// Create all-zeros value
56    fn zero() -> Self;
57
58    /// Create value representing one
59    fn one() -> Self;
60}
61
62/// A general error code, other errors should implement conversions to/from this
63/// if appropriate.
64#[derive(Debug)]
65pub enum Error {
66    /// Encoding error
67    Encode(encode::Error),
68    /// Network error
69    Network(network::Error),
70    /// The header hash is not below the target
71    BlockBadProofOfWork,
72    /// The `target` field of a block header did not match the expected difficulty
73    BlockBadTarget,
74}
75
76impl fmt::Display for Error {
77    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78        match *self {
79            Error::Encode(ref e) => fmt::Display::fmt(e, f),
80            Error::Network(ref e) => fmt::Display::fmt(e, f),
81            Error::BlockBadProofOfWork => f.write_str("block target correct but not attained"),
82            Error::BlockBadTarget => f.write_str("block target incorrect"),
83        }
84    }
85}
86
87impl error::Error for Error {
88    fn cause(&self) -> Option<&dyn error::Error> {
89        match *self {
90            Error::Encode(ref e) => Some(e),
91            Error::Network(ref e) => Some(e),
92            Error::BlockBadProofOfWork | Error::BlockBadTarget => None
93        }
94    }
95}
96
97#[doc(hidden)]
98impl From<encode::Error> for Error {
99    fn from(e: encode::Error) -> Error {
100        Error::Encode(e)
101    }
102}
103
104#[doc(hidden)]
105impl From<network::Error> for Error {
106    fn from(e: network::Error) -> Error {
107        Error::Network(e)
108    }
109}