diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 7a83b98dd..484b72293 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -23,18 +23,11 @@ use crate::script::{self, ScriptExt as _}; use crate::transaction::{Transaction, Wtxid}; use crate::VarInt; -hashes::hash_newtype! { - /// A bitcoin block hash. - pub struct BlockHash(sha256d::Hash); - /// A hash corresponding to the witness structure commitment in the coinbase transaction. - pub struct WitnessCommitment(sha256d::Hash); -} -impl_hashencode!(BlockHash); +#[rustfmt::skip] // Keep public re-exports separate. +#[doc(inline)] +pub use primitives::block::*; -impl BlockHash { - /// Dummy hash used as the previous blockhash of the genesis block. - pub const GENESIS_PREVIOUS_BLOCK_HASH: Self = Self::from_byte_array([0; 32]); -} +impl_hashencode!(BlockHash); /// Bitcoin block header. /// @@ -74,7 +67,7 @@ impl Header { pub fn block_hash(&self) -> BlockHash { let mut engine = sha256d::Hash::engine(); self.consensus_encode(&mut engine).expect("engines don't error"); - BlockHash(sha256d::Hash::from_engine(engine)) + BlockHash::from_byte_array(sha256d::Hash::from_engine(engine).to_byte_array()) } /// Computes the target (range [0, T] inclusive) that a blockhash must land in to be valid. @@ -296,7 +289,7 @@ impl Block { let mut encoder = sha256d::Hash::engine(); witness_root.consensus_encode(&mut encoder).expect("engines don't error"); encoder.input(witness_reserved_value); - WitnessCommitment(sha256d::Hash::from_engine(encoder)) + WitnessCommitment::from_byte_array(sha256d::Hash::from_engine(encoder).to_byte_array()) } /// Computes the Merkle root of transactions hashed for witness. diff --git a/primitives/src/block.rs b/primitives/src/block.rs new file mode 100644 index 000000000..3ddf01034 --- /dev/null +++ b/primitives/src/block.rs @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: CC0-1.0 + +//! Bitcoin blocks. +//! +//! A block is a bundle of transactions with a proof-of-work attached, +//! which commits to an earlier block to form the blockchain. This +//! module describes structures and functions needed to describe +//! these blocks and the blockchain. + +use hashes::sha256d; + +hashes::hash_newtype! { + /// A bitcoin block hash. + pub struct BlockHash(sha256d::Hash); + /// A hash corresponding to the witness structure commitment in the coinbase transaction. + pub struct WitnessCommitment(sha256d::Hash); +} + +impl BlockHash { + /// Dummy hash used as the previous blockhash of the genesis block. + pub const GENESIS_PREVIOUS_BLOCK_HASH: Self = Self::from_byte_array([0; 32]); +} diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 83149ba20..de1b10694 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -29,6 +29,7 @@ extern crate std; #[macro_use] extern crate serde; +pub mod block; #[cfg(feature = "alloc")] pub mod locktime; pub mod opcodes; @@ -44,6 +45,7 @@ pub use units::*; pub use self::locktime::{absolute, relative}; #[doc(inline)] pub use self::{ + block::{BlockHash, WitnessCommitment}, pow::CompactTarget, sequence::Sequence, transaction::{Txid, Wtxid},