2022-06-29 04:05:31 +00:00
|
|
|
// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
|
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2021-11-05 21:58:18 +00:00
|
|
|
//! Utility functions.
|
|
|
|
//!
|
|
|
|
//! Functions needed by all parts of the Bitcoin library.
|
2014-07-18 13:56:17 +00:00
|
|
|
//!
|
|
|
|
|
2022-01-09 06:46:24 +00:00
|
|
|
pub mod key;
|
2022-01-09 06:50:02 +00:00
|
|
|
pub mod ecdsa;
|
2021-04-12 11:24:25 +00:00
|
|
|
pub mod schnorr;
|
2018-11-10 00:45:00 +00:00
|
|
|
pub mod amount;
|
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;
|
2019-03-24 15:20:46 +00:00
|
|
|
pub mod bip152;
|
2014-07-18 13:56:17 +00:00
|
|
|
pub mod hash;
|
2019-05-27 12:36:35 +00:00
|
|
|
pub mod merkleblock;
|
2014-07-18 13:56:17 +00:00
|
|
|
pub mod misc;
|
2018-08-08 06:36:51 +00:00
|
|
|
pub mod psbt;
|
2019-05-14 17:07:53 +00:00
|
|
|
pub mod taproot;
|
2014-07-18 13:56:17 +00:00
|
|
|
pub mod uint;
|
2019-07-26 07:36:25 +00:00
|
|
|
pub mod bip158;
|
2021-07-16 08:14:07 +00:00
|
|
|
pub mod sighash;
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2019-10-17 21:40:45 +00:00
|
|
|
pub(crate) mod endian;
|
|
|
|
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
use crate::io;
|
2021-06-09 10:40:41 +00:00
|
|
|
use core::fmt;
|
2015-04-07 01:51:11 +00:00
|
|
|
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::consensus::encode;
|
2022-06-07 03:25:27 +00:00
|
|
|
use crate::internal_macros::write_err;
|
2018-08-17 15:25:11 +00:00
|
|
|
|
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;
|
|
|
|
|
2019-01-23 18:30:32 +00:00
|
|
|
/// Create value representing one
|
2018-02-18 15:21:05 +00:00
|
|
|
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)]
|
2022-05-31 04:29:50 +00:00
|
|
|
#[non_exhaustive]
|
2015-04-07 01:51:11 +00:00
|
|
|
pub enum Error {
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
/// Encoding error
|
|
|
|
Encode(encode::Error),
|
2018-08-21 08:31:54 +00:00
|
|
|
/// The header hash is not below the target
|
2019-06-07 13:12:07 +00:00
|
|
|
BlockBadProofOfWork,
|
2018-08-21 08:31:54 +00:00
|
|
|
/// The `target` field of a block header did not match the expected difficulty
|
2019-06-07 13:12:07 +00:00
|
|
|
BlockBadTarget,
|
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 {
|
2022-05-25 02:56:51 +00:00
|
|
|
Error::Encode(ref e) => write_err!(f, "encoding error"; e),
|
2020-03-29 13:49:48 +00:00
|
|
|
Error::BlockBadProofOfWork => f.write_str("block target correct but not attained"),
|
|
|
|
Error::BlockBadTarget => f.write_str("block target incorrect"),
|
2016-06-20 01:25:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 10:34:44 +00:00
|
|
|
#[cfg(feature = "std")]
|
2022-05-04 05:56:24 +00:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
|
|
|
impl std::error::Error for Error {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
|
|
use self::Error::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
Encode(e) => Some(e),
|
2022-06-01 22:24:23 +00:00
|
|
|
BlockBadProofOfWork | BlockBadTarget => None
|
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)]
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
impl From<encode::Error> for Error {
|
|
|
|
fn from(e: encode::Error) -> Error {
|
|
|
|
Error::Encode(e)
|
2018-03-09 20:27:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 10:34:44 +00:00
|
|
|
// core2 doesn't have read_to_end
|
|
|
|
pub(crate) fn read_to_end<D: io::Read>(mut d: D) -> Result<Vec<u8>, io::Error> {
|
|
|
|
let mut result = vec![];
|
|
|
|
let mut buf = [0u8; 64];
|
|
|
|
loop {
|
|
|
|
match d.read(&mut buf) {
|
|
|
|
Ok(0) => break,
|
|
|
|
Ok(n) => result.extend_from_slice(&buf[0..n]),
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {},
|
2021-11-03 09:20:34 +00:00
|
|
|
Err(e) => return Err(e),
|
2021-06-09 10:34:44 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(result)
|
2021-11-05 21:58:18 +00:00
|
|
|
}
|
2022-08-05 03:37:41 +00:00
|
|
|
|
|
|
|
/// The `address` module now lives at the crate root, re-export everything so as not to break the
|
|
|
|
/// API, however deprecate the re-exports so folks know to upgrade sooner or later.
|
|
|
|
#[deprecated(since = "0.30.0", note = "Please use crate::address")]
|
|
|
|
pub mod address {
|
|
|
|
pub use crate::address::*;
|
|
|
|
}
|