Skip to main content

bitcoin_primitives/
lib.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! # Rust Bitcoin Primitive Types
4//!
5//! Primitive data types that are used throughout the [`rust-bitcoin`] ecosystem.
6//!
7//! If you are using `rust-bitcoin` then you do not need to access this crate directly. Everything
8//! here is re-exported in `rust-bitcoin` at the same path.
9//!
10//! This crate can be used in a no-std environment but a lot of the functionality requires an
11//! allocator i.e., requires the `alloc` feature to be enabled.
12//!
13//! [`rust-bitcoin`]: <https://github.com/rust-bitcoin/rust-bitcoin>
14
15#![no_std]
16// Coding conventions.
17#![warn(missing_docs)]
18#![warn(deprecated_in_future)]
19#![doc(test(attr(warn(unused))))]
20// Package-specific lint overrides.
21
22#[cfg(feature = "alloc")]
23extern crate alloc;
24
25#[cfg(feature = "std")]
26extern crate std;
27
28#[cfg(feature = "serde")]
29#[macro_use]
30pub extern crate serde;
31
32#[cfg(feature = "arbitrary")]
33pub extern crate arbitrary;
34
35pub extern crate encoding;
36
37#[cfg(feature = "hex")]
38pub extern crate hex;
39
40#[doc(hidden)]
41pub mod _export {
42    /// A re-export of `core::*`.
43    pub mod _core {
44        pub use core::*;
45    }
46}
47
48mod hash_types;
49
50pub mod block;
51pub mod merkle_tree;
52pub mod opcodes;
53#[cfg(feature = "alloc")]
54pub mod script;
55pub mod transaction;
56#[cfg(feature = "alloc")]
57pub mod witness;
58pub mod witness_version;
59
60#[cfg(feature = "hex")]
61mod hex_codec;
62
63#[doc(inline)]
64pub use units::{
65    amount::{self, Amount, SignedAmount},
66    block::{BlockHeight, BlockHeightInterval, BlockMtp, BlockMtpInterval},
67    fee_rate::{self, FeeRate},
68    locktime::{self, absolute, relative},
69    parse_int,
70    pow::{self, CompactTarget, Target, Work},
71    result::{self, NumOpResult},
72    sequence::{self, Sequence},
73    time::{self, BlockTime},
74    weight::{self, Weight},
75};
76
77#[deprecated(since = "1.0.0-rc.0", note = "use `BlockHeightInterval` instead")]
78#[doc(hidden)]
79pub type BlockInterval = BlockHeightInterval;
80
81#[doc(inline)]
82#[cfg(feature = "alloc")]
83pub use self::{
84    block::{
85        Block, Checked as BlockChecked, Unchecked as BlockUnchecked, Validation as BlockValidation,
86    },
87    script::{
88        RedeemScript, RedeemScriptBuf, ScriptPubKey, ScriptPubKeyBuf, ScriptSig, ScriptSigBuf,
89        SignetBlockScript, SignetBlockScriptBuf, TapScript, TapScriptBuf, WitnessScript,
90        WitnessScriptBuf,
91    },
92    transaction::{Transaction, TxIn, TxOut},
93    witness::Witness,
94};
95#[doc(inline)]
96pub use self::{
97    block::{BlockHash, Header as BlockHeader, Version as BlockVersion, WitnessCommitment},
98    transaction::{OutPoint, Txid, Version as TransactionVersion, Wtxid},
99};
100
101#[rustfmt::skip]
102#[allow(unused_imports)]
103mod prelude {
104    #[cfg(feature = "alloc")]
105    pub use alloc::collections::{BTreeMap, BTreeSet, btree_map, BinaryHeap};
106
107    #[cfg(feature = "alloc")]
108    pub use alloc::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, BorrowMut, Cow, ToOwned}, slice, rc};
109
110    #[cfg(feature = "alloc")]
111    #[cfg(target_has_atomic = "ptr")]
112    pub use alloc::sync;
113}
114
115#[cfg(feature = "alloc")]
116use encoding::Encoder;
117#[cfg(feature = "alloc")]
118use internals::array_vec::ArrayVec;
119
120// Encode a compact size to a slice without allocating
121#[cfg(feature = "alloc")]
122pub(crate) fn compact_size_encode(value: usize) -> ArrayVec<u8, 9> {
123    let encoder = encoding::CompactSizeEncoder::new(value);
124    ArrayVec::from_slice(encoder.current_chunk())
125}
126
127#[cfg(feature = "alloc")]
128include!("../include/newtype.rs"); // Explained in `REPO_DIR/docs/README.md`.
129include!("../include/decoder_newtype.rs"); // decoder_newtype! macro
130#[cfg(feature = "alloc")]
131include!("../include/asref_push_bytes.rs"); // impl_asref_push_bytes! macro