1pub 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
37pub trait BitArray {
39 fn bit(&self, idx: usize) -> bool;
41
42 fn bit_slice(&self, start: usize, end: usize) -> Self;
44
45 fn mask(&self, n: usize) -> Self;
47
48 fn trailing_zeros(&self) -> usize;
50
51 fn zero() -> Self;
53
54 fn one() -> Self;
56}
57
58#[derive(Debug)]
61pub enum Error {
62 Secp256k1(secp256k1::Error),
64 Encode(encode::Error),
66 Network(network::Error),
68 SpvBadProofOfWork,
70 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}