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
|
|
|
|
|
|
|
//! # Rust Bitcoin Library
|
|
|
|
//!
|
2021-11-05 21:58:18 +00:00
|
|
|
//! This is a library that supports the Bitcoin network protocol and associated
|
2014-07-18 13:56:17 +00:00
|
|
|
//! primitives. It is designed for Rust programs built to work with the Bitcoin
|
|
|
|
//! network.
|
|
|
|
//!
|
|
|
|
//! It is also written entirely in Rust to illustrate the benefits of strong type
|
|
|
|
//! safety, including ownership and lifetime, for financial and/or cryptographic
|
|
|
|
//! software.
|
|
|
|
//!
|
2021-09-20 19:29:44 +00:00
|
|
|
//! See README.md for detailed documentation about development and supported
|
|
|
|
//! environments.
|
|
|
|
//!
|
2021-07-28 19:38:58 +00:00
|
|
|
//! ## Available feature flags
|
|
|
|
//!
|
|
|
|
//! * `std` - the usual dependency on `std` (default).
|
|
|
|
//! * `secp-recovery` - enables calculating public key from a signature and message.
|
|
|
|
//! * `base64` - (dependency), enables encoding of PSBTs and message signatures.
|
2021-11-05 21:58:18 +00:00
|
|
|
//! * `rand` - (dependency), makes it more convenient to generate random values.
|
2022-05-25 06:41:59 +00:00
|
|
|
//! * `serde` - (dependency), implements `serde`-based serialization and
|
2021-11-05 21:58:18 +00:00
|
|
|
//! deserialization.
|
|
|
|
//! * `secp-lowmemory` - optimizations for low-memory devices.
|
2021-07-28 19:38:58 +00:00
|
|
|
//! * `no-std` - enables additional features required for this crate to be usable
|
2023-01-22 06:29:18 +00:00
|
|
|
//! without std. Does **not** disable `std`. Depends on `core2`.
|
2022-10-25 11:35:32 +00:00
|
|
|
//! * `bitcoinconsensus-std` - enables `std` in `bitcoinconsensus` and communicates it
|
|
|
|
//! to this crate so it knows how to implement
|
|
|
|
//! `std::error::Error`. At this time there's a hack to
|
|
|
|
//! achieve the same without this feature but it could
|
|
|
|
//! happen the implementations diverge one day.
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2021-06-09 10:34:44 +00:00
|
|
|
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
|
2022-07-12 00:07:38 +00:00
|
|
|
// Experimental features we need.
|
|
|
|
#![cfg_attr(bench, feature(test))]
|
2021-07-28 19:38:58 +00:00
|
|
|
#![cfg_attr(docsrs, feature(doc_cfg))]
|
2014-07-18 13:56:17 +00:00
|
|
|
// Coding conventions
|
2023-02-10 19:01:42 +00:00
|
|
|
#![warn(missing_docs)]
|
2019-07-23 21:29:45 +00:00
|
|
|
|
2022-11-30 04:05:05 +00:00
|
|
|
// Instead of littering the codebase for non-fuzzing code just globally allow.
|
|
|
|
#![cfg_attr(fuzzing, allow(dead_code, unused_imports))]
|
|
|
|
|
2021-06-09 10:34:44 +00:00
|
|
|
#[cfg(not(any(feature = "std", feature = "no-std")))]
|
|
|
|
compile_error!("at least one of the `std` or `no-std` features must be enabled");
|
|
|
|
|
2021-09-20 19:29:44 +00:00
|
|
|
// Disable 16-bit support at least for now as we can't guarantee it yet.
|
|
|
|
#[cfg(target_pointer_width = "16")]
|
2022-06-06 04:14:41 +00:00
|
|
|
compile_error!(
|
|
|
|
"rust-bitcoin currently only supports architectures with pointers wider than 16 bits, let us
|
|
|
|
know if you want 16-bit support. Note that we do NOT guarantee that we will implement it!"
|
|
|
|
);
|
2021-09-20 19:29:44 +00:00
|
|
|
|
2022-06-06 04:34:09 +00:00
|
|
|
#[cfg(bench)]
|
|
|
|
extern crate test;
|
2022-07-12 00:07:38 +00:00
|
|
|
|
2021-06-09 10:34:44 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate alloc;
|
|
|
|
|
2022-10-25 09:02:02 +00:00
|
|
|
#[cfg(feature = "base64")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "base64")))]
|
2022-11-07 21:25:08 +00:00
|
|
|
pub extern crate base64;
|
|
|
|
pub extern crate bech32;
|
|
|
|
pub extern crate bitcoin_hashes as hashes;
|
|
|
|
#[cfg(feature = "bitcoinconsensus")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
|
|
|
|
pub extern crate bitcoinconsensus;
|
2022-10-25 23:54:43 +00:00
|
|
|
#[cfg(feature = "hashbrown")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "hashbrown")))]
|
2022-11-07 21:25:08 +00:00
|
|
|
pub extern crate hashbrown;
|
|
|
|
pub extern crate secp256k1;
|
2022-10-25 23:54:43 +00:00
|
|
|
|
2022-06-06 04:34:09 +00:00
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate actual_serde as serde;
|
2022-06-07 03:00:01 +00:00
|
|
|
|
2015-04-10 23:15:57 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
mod test_macros;
|
2014-07-18 13:56:17 +00:00
|
|
|
mod internal_macros;
|
2022-08-02 06:00:11 +00:00
|
|
|
mod parse;
|
2020-10-25 18:27:45 +00:00
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
mod serde_utils;
|
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
#[macro_use]
|
2014-07-18 13:56:17 +00:00
|
|
|
pub mod network;
|
2022-08-05 03:37:41 +00:00
|
|
|
pub mod address;
|
2022-10-20 22:12:44 +00:00
|
|
|
pub mod amount;
|
2022-11-28 22:45:28 +00:00
|
|
|
pub mod base58;
|
2022-08-24 06:29:13 +00:00
|
|
|
pub mod bip152;
|
2022-08-24 06:33:05 +00:00
|
|
|
pub mod bip158;
|
2022-08-24 06:15:33 +00:00
|
|
|
pub mod bip32;
|
2014-07-18 13:56:17 +00:00
|
|
|
pub mod blockdata;
|
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
|
|
|
pub mod consensus;
|
2022-11-08 00:36:52 +00:00
|
|
|
pub mod crypto;
|
2022-07-26 12:50:28 +00:00
|
|
|
pub mod error;
|
2019-12-09 22:37:52 +00:00
|
|
|
pub mod hash_types;
|
2022-10-20 22:30:20 +00:00
|
|
|
pub mod merkle_tree;
|
2021-04-05 12:58:36 +00:00
|
|
|
pub mod policy;
|
2022-08-16 05:07:59 +00:00
|
|
|
pub mod pow;
|
2022-08-05 03:23:03 +00:00
|
|
|
pub mod psbt;
|
2022-09-12 04:50:37 +00:00
|
|
|
pub mod sign_message;
|
2022-11-18 03:06:57 +00:00
|
|
|
pub mod string;
|
2022-11-07 23:32:52 +00:00
|
|
|
pub mod taproot;
|
2022-06-06 04:34:09 +00:00
|
|
|
pub mod util;
|
2018-08-28 15:57:46 +00:00
|
|
|
|
2022-09-08 14:50:24 +00:00
|
|
|
// May depend on crate features and we don't want to bother with it
|
|
|
|
#[allow(unused)]
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
use std::error::Error as StdError;
|
2022-10-19 16:51:41 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
use std::io;
|
2022-09-08 14:50:24 +00:00
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
use core2::error::Error as StdError;
|
2022-10-19 16:51:41 +00:00
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
use core2::io;
|
2022-09-08 14:50:24 +00:00
|
|
|
|
2022-08-05 03:40:26 +00:00
|
|
|
pub use crate::address::{Address, AddressType};
|
2022-11-15 23:22:16 +00:00
|
|
|
pub use crate::amount::{Amount, Denomination, SignedAmount};
|
2022-10-27 01:29:34 +00:00
|
|
|
pub use crate::blockdata::block::{self, Block};
|
2022-08-16 03:14:24 +00:00
|
|
|
pub use crate::blockdata::locktime::{self, absolute, relative};
|
2022-07-30 12:22:18 +00:00
|
|
|
pub use crate::blockdata::script::{self, Script, ScriptBuf};
|
2022-08-08 04:41:24 +00:00
|
|
|
pub use crate::blockdata::transaction::{self, OutPoint, Sequence, Transaction, TxIn, TxOut};
|
|
|
|
pub use crate::blockdata::witness::{self, Witness};
|
|
|
|
pub use crate::blockdata::{constants, opcodes};
|
2022-05-02 22:13:57 +00:00
|
|
|
pub use crate::consensus::encode::VarInt;
|
2022-11-08 00:36:52 +00:00
|
|
|
pub use crate::crypto::key::{self, PrivateKey, PublicKey};
|
2023-02-07 03:35:03 +00:00
|
|
|
pub use crate::crypto::sighash;
|
2022-08-02 06:02:10 +00:00
|
|
|
pub use crate::error::Error;
|
2023-02-03 03:04:37 +00:00
|
|
|
pub use crate::hash_types::{Txid, Wtxid, BlockHash, PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash};
|
2022-11-15 23:22:16 +00:00
|
|
|
pub use crate::merkle_tree::MerkleBlock;
|
2022-05-02 22:13:57 +00:00
|
|
|
pub use crate::network::constants::Network;
|
2022-08-16 05:07:59 +00:00
|
|
|
pub use crate::pow::{CompactTarget, Target, Work};
|
2021-06-09 10:34:44 +00:00
|
|
|
|
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
mod io_extras {
|
|
|
|
/// A writer which will move data into the void.
|
|
|
|
pub struct Sink {
|
|
|
|
_priv: (),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates an instance of a writer which will successfully consume all data.
|
2022-06-06 04:34:09 +00:00
|
|
|
pub const fn sink() -> Sink { Sink { _priv: () } }
|
2021-06-09 10:34:44 +00:00
|
|
|
|
|
|
|
impl core2::io::Write for Sink {
|
|
|
|
#[inline]
|
2022-06-06 04:34:09 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> core2::io::Result<usize> { Ok(buf.len()) }
|
2021-06-09 10:34:44 +00:00
|
|
|
|
|
|
|
#[inline]
|
2022-06-06 04:34:09 +00:00
|
|
|
fn flush(&mut self) -> core2::io::Result<()> { Ok(()) }
|
2021-06-09 10:34:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 04:32:19 +00:00
|
|
|
#[rustfmt::skip]
|
2021-06-09 10:34:44 +00:00
|
|
|
mod prelude {
|
|
|
|
#[cfg(all(not(feature = "std"), not(test)))]
|
2022-08-11 20:48:21 +00:00
|
|
|
pub use alloc::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, Cow, ToOwned}, slice, rc, sync};
|
2021-06-09 10:34:44 +00:00
|
|
|
|
|
|
|
#[cfg(any(feature = "std", test))]
|
2022-08-11 20:48:21 +00:00
|
|
|
pub use std::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, Cow, ToOwned}, slice, rc, sync};
|
2021-06-09 10:34:44 +00:00
|
|
|
|
|
|
|
#[cfg(all(not(feature = "std"), not(test)))]
|
2021-11-06 11:56:51 +00:00
|
|
|
pub use alloc::collections::{BTreeMap, BTreeSet, btree_map, BinaryHeap};
|
2021-06-09 10:34:44 +00:00
|
|
|
|
|
|
|
#[cfg(any(feature = "std", test))]
|
2021-11-06 11:56:51 +00:00
|
|
|
pub use std::collections::{BTreeMap, BTreeSet, btree_map, BinaryHeap};
|
2021-06-09 10:34:44 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
pub use std::io::sink;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "std"))]
|
2022-05-02 23:02:58 +00:00
|
|
|
pub use crate::io_extras::sink;
|
2021-06-09 10:34:44 +00:00
|
|
|
|
2023-01-09 22:54:32 +00:00
|
|
|
pub use bitcoin_internals::hex::display::DisplayHex;
|
2021-06-09 10:34:44 +00:00
|
|
|
}
|
2021-06-09 10:40:41 +00:00
|
|
|
|
2022-06-06 04:34:09 +00:00
|
|
|
#[cfg(bench)]
|
|
|
|
use bench::EmptyWrite;
|
2020-03-29 12:54:15 +00:00
|
|
|
|
2022-07-12 00:07:38 +00:00
|
|
|
#[cfg(bench)]
|
|
|
|
mod bench {
|
2021-06-09 10:34:44 +00:00
|
|
|
use core::fmt::Arguments;
|
2022-06-06 04:34:09 +00:00
|
|
|
|
2022-05-02 23:02:58 +00:00
|
|
|
use crate::io::{IoSlice, Result, Write};
|
2020-03-29 12:54:15 +00:00
|
|
|
|
|
|
|
#[derive(Default, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub struct EmptyWrite;
|
|
|
|
|
|
|
|
impl Write for EmptyWrite {
|
2022-06-06 04:34:09 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize> { Ok(buf.len()) }
|
2020-03-29 12:54:15 +00:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice]) -> Result<usize> {
|
|
|
|
Ok(bufs.iter().map(|s| s.len()).sum())
|
|
|
|
}
|
2022-06-06 04:34:09 +00:00
|
|
|
fn flush(&mut self) -> Result<()> { Ok(()) }
|
2020-03-29 12:54:15 +00:00
|
|
|
|
2022-06-06 04:34:09 +00:00
|
|
|
fn write_all(&mut self, _: &[u8]) -> Result<()> { Ok(()) }
|
|
|
|
fn write_fmt(&mut self, _: Arguments) -> Result<()> { Ok(()) }
|
2020-03-29 12:54:15 +00:00
|
|
|
}
|
|
|
|
}
|