2022-06-29 04:05:31 +00:00
|
|
|
// 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.
|
|
|
|
//!
|
2023-06-08 12:21:41 +00:00
|
|
|
//! Except for its dependency on libsecp256k1 (and optionally libbitcoinconsensus),
|
|
|
|
//! this library is written entirely in Rust. It illustrates the benefits of
|
|
|
|
//! strong type safety, including ownership and lifetime, for financial and/or cryptographic software.
|
2014-07-18 13:56:17 +00:00
|
|
|
//!
|
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.
|
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.
|
2024-04-01 21:10:38 +00:00
|
|
|
//! * `ordered` - (dependency), adds implementations of `ArbitraryOrdOrd` to some structs.
|
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.
|
2023-03-29 03:40:23 +00:00
|
|
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
2024-01-25 06:41:01 +00:00
|
|
|
#![cfg_attr(bench, feature(test))]
|
|
|
|
// Coding conventions.
|
2023-02-10 19:01:42 +00:00
|
|
|
#![warn(missing_docs)]
|
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))]
|
2024-01-25 06:41:01 +00:00
|
|
|
// Exclude lints we don't think are valuable.
|
2023-10-20 14:58:42 +00:00
|
|
|
#![allow(clippy::needless_question_mark)] // https://github.com/rust-bitcoin/rust-bitcoin/pull/2134
|
2024-01-25 06:41:01 +00:00
|
|
|
#![allow(clippy::manual_range_contains)] // More readable than clippy's format.
|
2024-03-19 23:53:35 +00:00
|
|
|
#![allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/12454
|
2022-11-30 04:05:05 +00:00
|
|
|
|
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")]
|
2023-05-08 08:57:49 +00:00
|
|
|
/// Encodes and decodes base64 as bytes or utf8.
|
2022-11-07 21:25:08 +00:00
|
|
|
pub extern crate base64;
|
2023-05-08 08:57:49 +00:00
|
|
|
|
2024-02-25 21:45:58 +00:00
|
|
|
/// Bitcoin base58 encoding and decoding.
|
|
|
|
pub extern crate base58;
|
|
|
|
|
2024-01-05 15:10:31 +00:00
|
|
|
/// Rust implementation of cryptographic hash function algorithms.
|
2023-03-28 01:16:47 +00:00
|
|
|
pub extern crate hashes;
|
2023-05-08 08:57:49 +00:00
|
|
|
|
2023-07-21 00:38:34 +00:00
|
|
|
/// Re-export the `hex-conservative` crate.
|
|
|
|
pub extern crate hex;
|
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
/// Re-export the `bitcoin-io` crate.
|
|
|
|
pub extern crate io;
|
|
|
|
|
2023-12-01 21:50:51 +00:00
|
|
|
/// Re-export the `ordered` crate.
|
|
|
|
#[cfg(feature = "ordered")]
|
|
|
|
pub extern crate ordered;
|
|
|
|
|
2023-05-08 08:57:49 +00:00
|
|
|
/// Rust wrapper library for Pieter Wuille's libsecp256k1. Implements ECDSA and BIP 340 signatures
|
|
|
|
/// for the SECG elliptic curve group secp256k1 and related utilities.
|
2022-11-07 21:25:08 +00:00
|
|
|
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;
|
2020-10-25 18:27:45 +00:00
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
mod serde_utils;
|
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
#[macro_use]
|
2023-05-24 23:14:42 +00:00
|
|
|
pub mod p2p;
|
2022-08-05 03:37:41 +00:00
|
|
|
pub mod address;
|
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;
|
2023-02-27 22:47:29 +00:00
|
|
|
// Private until we either make this a crate or flatten it - still to be decided.
|
|
|
|
pub(crate) 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;
|
2023-06-09 06:18:39 +00:00
|
|
|
pub mod network;
|
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-07 23:32:52 +00:00
|
|
|
pub mod taproot;
|
2018-08-28 15:57:46 +00:00
|
|
|
|
2023-10-10 04:08:50 +00:00
|
|
|
#[rustfmt::skip] // Keep public re-exports separate.
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use crate::{
|
2024-01-23 05:51:44 +00:00
|
|
|
address::{Address, AddressType, KnownHrp},
|
2023-10-10 04:08:50 +00:00
|
|
|
amount::{Amount, Denomination, SignedAmount},
|
2023-11-07 03:01:14 +00:00
|
|
|
bip158::{FilterHash, FilterHeader},
|
2023-10-10 04:08:50 +00:00
|
|
|
bip32::XKeyIdentifier,
|
2023-11-07 02:34:13 +00:00
|
|
|
blockdata::block::{self, Block, BlockHash, TxMerkleNode, WitnessMerkleNode, WitnessCommitment},
|
2023-10-10 04:08:50 +00:00
|
|
|
blockdata::constants,
|
|
|
|
blockdata::fee_rate::FeeRate,
|
|
|
|
blockdata::locktime::{self, absolute, relative},
|
|
|
|
blockdata::opcodes::{self, Opcode},
|
|
|
|
blockdata::script::witness_program::{self, WitnessProgram},
|
|
|
|
blockdata::script::witness_version::{self, WitnessVersion},
|
|
|
|
blockdata::script::{self, Script, ScriptBuf, ScriptHash, WScriptHash},
|
2023-11-07 02:40:01 +00:00
|
|
|
blockdata::transaction::{self, OutPoint, Sequence, Transaction, TxIn, TxOut, Txid, Wtxid},
|
2023-10-10 04:08:50 +00:00
|
|
|
blockdata::weight::Weight,
|
|
|
|
blockdata::witness::{self, Witness},
|
|
|
|
consensus::encode::VarInt,
|
2024-03-14 00:26:09 +00:00
|
|
|
consensus::params,
|
2023-10-10 04:08:50 +00:00
|
|
|
crypto::ecdsa,
|
2023-12-11 20:10:32 +00:00
|
|
|
crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, CompressedPublicKey, WPubkeyHash, XOnlyPublicKey},
|
2023-10-10 04:08:50 +00:00
|
|
|
crypto::sighash::{self, LegacySighash, SegwitV0Sighash, TapSighash, TapSighashTag},
|
|
|
|
merkle_tree::MerkleBlock,
|
2023-11-17 02:21:06 +00:00
|
|
|
network::{Network, NetworkKind},
|
2023-10-10 04:08:50 +00:00
|
|
|
pow::{CompactTarget, Target, Work},
|
|
|
|
psbt::Psbt,
|
|
|
|
sighash::{EcdsaSighashType, TapSighashType},
|
|
|
|
taproot::{TapBranchTag, TapLeafHash, TapLeafTag, TapNodeHash, TapTweakHash, TapTweakTag},
|
2023-08-11 01:40:53 +00:00
|
|
|
};
|
2021-06-09 10:34:44 +00:00
|
|
|
|
2022-06-06 04:32:19 +00:00
|
|
|
#[rustfmt::skip]
|
2024-02-20 05:11:14 +00:00
|
|
|
#[allow(unused_imports)]
|
2021-06-09 10:34:44 +00:00
|
|
|
mod prelude {
|
|
|
|
#[cfg(all(not(feature = "std"), not(test)))]
|
2023-05-25 00:08:55 +00:00
|
|
|
pub use alloc::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, BorrowMut, Cow, ToOwned}, slice, rc};
|
2023-03-09 16:30:03 +00:00
|
|
|
|
|
|
|
#[cfg(all(not(feature = "std"), not(test), any(not(rust_v_1_60), target_has_atomic = "ptr")))]
|
|
|
|
pub use alloc::sync;
|
2021-06-09 10:34:44 +00:00
|
|
|
|
|
|
|
#[cfg(any(feature = "std", test))]
|
2023-12-08 11:42:18 +00:00
|
|
|
pub use std::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, BorrowMut, Cow, ToOwned}, 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
|
|
|
|
2023-09-12 17:47:47 +00:00
|
|
|
pub use crate::io::sink;
|
2021-06-09 10:34:44 +00:00
|
|
|
|
2023-07-21 00:38:34 +00:00
|
|
|
pub use hex::DisplayHex;
|
2021-06-09 10:34:44 +00:00
|
|
|
}
|
2023-08-08 08:47:05 +00:00
|
|
|
|
|
|
|
pub mod amount {
|
|
|
|
//! Bitcoin amounts.
|
|
|
|
//!
|
|
|
|
//! This module mainly introduces the [Amount] and [SignedAmount] types.
|
|
|
|
//! We refer to the documentation on the types for more information.
|
|
|
|
|
|
|
|
use crate::consensus::{encode, Decodable, Encodable};
|
2023-12-08 01:16:55 +00:00
|
|
|
use crate::io::{BufRead, Write};
|
2023-08-08 08:47:05 +00:00
|
|
|
|
|
|
|
#[rustfmt::skip] // Keep public re-exports separate.
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use units::amount::{
|
|
|
|
Amount, CheckedSum, Denomination, Display, ParseAmountError, SignedAmount,
|
|
|
|
};
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
pub use units::amount::serde;
|
|
|
|
|
|
|
|
impl Decodable for Amount {
|
|
|
|
#[inline]
|
2023-12-08 01:16:55 +00:00
|
|
|
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
|
2023-08-08 08:47:05 +00:00
|
|
|
Ok(Amount::from_sat(Decodable::consensus_decode(r)?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for Amount {
|
|
|
|
#[inline]
|
2023-12-08 01:16:55 +00:00
|
|
|
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
|
2023-08-08 08:47:05 +00:00
|
|
|
self.to_sat().consensus_encode(w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-11 05:00:19 +00:00
|
|
|
|
|
|
|
/// Unit parsing utilities.
|
|
|
|
pub mod parse {
|
|
|
|
/// Re-export everything from the [`units::parse`] module.
|
|
|
|
pub use units::parse::ParseIntError;
|
|
|
|
}
|