2014-07-18 13:56:17 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written in 2014 by
|
|
|
|
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
|
|
|
//
|
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
//! # 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.
|
|
|
|
//! * `unstable` - enables unstable features for testing.
|
2021-11-05 21:58:18 +00:00
|
|
|
//! * `rand` - (dependency), makes it more convenient to generate random values.
|
|
|
|
//! * `use-serde` - (dependency), implements `serde`-based serialization and
|
|
|
|
//! 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
|
|
|
|
//! without std. Does **not** disable `std`. Depends on `hashbrown`
|
|
|
|
//! and `core2`.
|
2021-11-05 21:58:18 +00:00
|
|
|
//!
|
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)]
|
|
|
|
|
2014-07-18 13:56:17 +00:00
|
|
|
// Experimental features we need
|
2015-09-20 17:22:39 +00:00
|
|
|
#![cfg_attr(all(test, feature = "unstable"), feature(test))]
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2021-07-28 19:38:58 +00:00
|
|
|
#![cfg_attr(docsrs, feature(doc_cfg))]
|
|
|
|
|
2014-07-18 13:56:17 +00:00
|
|
|
// Coding conventions
|
2019-02-20 22:16:21 +00:00
|
|
|
#![forbid(unsafe_code)]
|
2015-04-11 01:55:59 +00:00
|
|
|
#![deny(non_upper_case_globals)]
|
2014-07-18 13:56:17 +00:00
|
|
|
#![deny(non_camel_case_types)]
|
2014-08-30 23:08:38 +00:00
|
|
|
#![deny(non_snake_case)]
|
2014-07-18 13:56:17 +00:00
|
|
|
#![deny(unused_mut)]
|
2019-08-18 16:26:29 +00:00
|
|
|
#![deny(dead_code)]
|
|
|
|
#![deny(unused_imports)]
|
2015-04-11 01:55:59 +00:00
|
|
|
#![deny(missing_docs)]
|
2020-10-23 16:21:54 +00:00
|
|
|
#![deny(unused_must_use)]
|
2021-05-03 09:43:48 +00:00
|
|
|
#![deny(broken_intra_doc_links)]
|
2019-07-23 21:29:45 +00:00
|
|
|
|
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")]
|
|
|
|
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-06-09 10:34:44 +00:00
|
|
|
#[cfg(feature = "no-std")]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate alloc;
|
|
|
|
#[cfg(feature = "no-std")]
|
|
|
|
extern crate core2;
|
|
|
|
|
|
|
|
#[cfg(any(feature = "std", test))]
|
|
|
|
extern crate core; // for Rust 1.29 and no-std tests
|
2021-06-09 10:40:41 +00:00
|
|
|
|
2019-07-06 13:43:12 +00:00
|
|
|
// Re-exported dependencies.
|
2019-12-09 22:37:52 +00:00
|
|
|
#[macro_use] pub extern crate bitcoin_hashes as hashes;
|
2019-07-06 13:43:12 +00:00
|
|
|
pub extern crate secp256k1;
|
2019-08-16 20:08:14 +00:00
|
|
|
pub extern crate bech32;
|
2021-06-09 10:34:44 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "no-std")]
|
|
|
|
extern crate hashbrown;
|
|
|
|
|
2021-07-28 19:38:58 +00:00
|
|
|
#[cfg(feature = "base64")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "base64")))]
|
|
|
|
pub extern crate base64;
|
2019-07-06 13:43:12 +00:00
|
|
|
|
2020-09-14 14:57:42 +00:00
|
|
|
#[cfg(feature="bitcoinconsensus")] extern crate bitcoinconsensus;
|
2020-10-25 18:03:20 +00:00
|
|
|
#[cfg(feature = "serde")] #[macro_use] extern crate serde;
|
2018-11-10 00:45:00 +00:00
|
|
|
#[cfg(all(test, feature = "serde"))] extern crate serde_json;
|
|
|
|
#[cfg(all(test, feature = "serde"))] extern crate serde_test;
|
2021-01-05 22:01:21 +00:00
|
|
|
#[cfg(all(test, feature = "serde"))] extern crate bincode;
|
2015-09-20 17:22:39 +00:00
|
|
|
#[cfg(all(test, feature = "unstable"))] extern crate test;
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2019-06-05 13:59:44 +00:00
|
|
|
#[cfg(target_pointer_width = "16")]
|
|
|
|
compile_error!("rust-bitcoin cannot be used on 16-bit architectures");
|
|
|
|
|
2015-04-10 23:15:57 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
mod test_macros;
|
2015-01-18 18:16:01 +00:00
|
|
|
#[macro_use]
|
2014-07-18 13:56:17 +00:00
|
|
|
mod internal_macros;
|
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;
|
|
|
|
pub mod blockdata;
|
|
|
|
pub mod util;
|
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;
|
2019-12-09 22:37:52 +00:00
|
|
|
pub mod hash_types;
|
2021-04-05 12:58:36 +00:00
|
|
|
pub mod policy;
|
2018-08-28 15:57:46 +00:00
|
|
|
|
2019-12-09 22:37:52 +00:00
|
|
|
pub use hash_types::*;
|
2018-08-28 15:57:46 +00:00
|
|
|
pub use blockdata::block::Block;
|
|
|
|
pub use blockdata::block::BlockHeader;
|
|
|
|
pub use blockdata::script::Script;
|
|
|
|
pub use blockdata::transaction::Transaction;
|
|
|
|
pub use blockdata::transaction::TxIn;
|
|
|
|
pub use blockdata::transaction::TxOut;
|
|
|
|
pub use blockdata::transaction::OutPoint;
|
2021-11-15 20:55:24 +00:00
|
|
|
pub use blockdata::transaction::EcdsaSigHashType;
|
2022-01-13 16:51:01 +00:00
|
|
|
pub use blockdata::witness::Witness;
|
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 use consensus::encode::VarInt;
|
2018-10-21 22:28:27 +00:00
|
|
|
pub use network::constants::Network;
|
2018-08-28 15:57:46 +00:00
|
|
|
pub use util::Error;
|
|
|
|
pub use util::address::Address;
|
2019-07-16 20:31:49 +00:00
|
|
|
pub use util::address::AddressType;
|
2018-11-10 00:45:00 +00:00
|
|
|
pub use util::amount::Amount;
|
2020-02-28 11:51:44 +00:00
|
|
|
pub use util::amount::Denomination;
|
2018-11-10 00:45:00 +00:00
|
|
|
pub use util::amount::SignedAmount;
|
2021-04-12 11:31:09 +00:00
|
|
|
pub use util::merkleblock::MerkleBlock;
|
2021-11-15 20:38:24 +00:00
|
|
|
pub use util::sighash::SchnorrSigHashType;
|
2021-04-12 11:31:09 +00:00
|
|
|
|
2021-11-16 00:00:12 +00:00
|
|
|
pub use util::ecdsa::{self, EcdsaSig, EcdsaSigError};
|
|
|
|
pub use util::schnorr::{self, SchnorrSig, SchnorrSigError};
|
2022-01-08 12:46:11 +00:00
|
|
|
pub use util::key::{PrivateKey, PublicKey, XOnlyPublicKey, KeyPair};
|
2021-12-25 23:17:00 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
pub use blockdata::transaction::SigHashType;
|
2020-03-29 12:54:15 +00:00
|
|
|
|
2021-06-09 10:34:44 +00:00
|
|
|
#[cfg(feature = "std")]
|
2021-06-09 10:40:41 +00:00
|
|
|
use std::io;
|
2021-06-09 10:34:44 +00:00
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
use core2::io;
|
|
|
|
|
|
|
|
#[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.
|
|
|
|
pub const fn sink() -> Sink {
|
|
|
|
Sink { _priv: () }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl core2::io::Write for Sink {
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> core2::io::Result<usize> {
|
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> core2::io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod prelude {
|
|
|
|
#[cfg(all(not(feature = "std"), not(test)))]
|
|
|
|
pub use alloc::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Cow, ToOwned}, slice, rc, sync};
|
|
|
|
|
|
|
|
#[cfg(any(feature = "std", test))]
|
|
|
|
pub use std::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Cow, ToOwned}, slice, rc, sync};
|
|
|
|
|
|
|
|
#[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"))]
|
|
|
|
pub use io_extras::sink;
|
|
|
|
|
|
|
|
#[cfg(feature = "hashbrown")]
|
|
|
|
pub use hashbrown::HashSet;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "hashbrown"))]
|
|
|
|
pub use std::collections::HashSet;
|
|
|
|
}
|
2021-06-09 10:40:41 +00:00
|
|
|
|
2020-03-29 12:54:15 +00:00
|
|
|
#[cfg(all(test, feature = "unstable"))] use tests::EmptyWrite;
|
|
|
|
|
|
|
|
#[cfg(all(test, feature = "unstable"))]
|
|
|
|
mod tests {
|
2021-06-09 10:34:44 +00:00
|
|
|
use core::fmt::Arguments;
|
|
|
|
use 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 {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice]) -> Result<usize> {
|
|
|
|
Ok(bufs.iter().map(|s| s.len()).sum())
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_all(&mut self, _: &[u8]) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn write_fmt(&mut self, _: Arguments) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|