diff --git a/bitcoin/src/bip152.rs b/bitcoin/src/bip152.rs index 766a82e0e..e11bac45c 100644 --- a/bitcoin/src/bip152.rs +++ b/bitcoin/src/bip152.rs @@ -374,10 +374,10 @@ mod test { use hex::FromHex; use super::*; - use crate::blockdata::block::TxMerkleNode; use crate::blockdata::locktime::absolute; use crate::blockdata::transaction; use crate::consensus::encode::{deserialize, serialize}; + use crate::merkle_tree::TxMerkleNode; use crate::{Amount, CompactTarget, OutPoint, ScriptBuf, Sequence, TxIn, TxOut, Txid, Witness}; fn dummy_tx(nonce: &[u8]) -> Transaction { diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 68c2eae7b..5f36ce0bb 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -14,34 +14,21 @@ use io::{BufRead, Write}; use super::Weight; use crate::blockdata::script; -use crate::blockdata::transaction::{Transaction, Txid, Wtxid}; +use crate::blockdata::transaction::{Transaction, Wtxid}; use crate::consensus::{encode, Decodable, Encodable, Params}; use crate::internal_macros::{impl_consensus_encoding, impl_hashencode}; +use crate::merkle_tree::{self, TxMerkleNode, WitnessMerkleNode}; use crate::pow::{CompactTarget, Target, Work}; use crate::prelude::*; -use crate::{merkle_tree, VarInt}; +use crate::VarInt; hashes::hash_newtype! { /// A bitcoin block hash. pub struct BlockHash(sha256d::Hash); - /// A hash of the Merkle tree branch or root for transactions. - pub struct TxMerkleNode(sha256d::Hash); - /// A hash corresponding to the Merkle tree root for witness data. - pub struct WitnessMerkleNode(sha256d::Hash); /// A hash corresponding to the witness structure commitment in the coinbase transaction. pub struct WitnessCommitment(sha256d::Hash); } impl_hashencode!(BlockHash); -impl_hashencode!(TxMerkleNode); -impl_hashencode!(WitnessMerkleNode); - -impl From for TxMerkleNode { - fn from(txid: Txid) -> Self { Self::from_byte_array(txid.to_byte_array()) } -} - -impl From for WitnessMerkleNode { - fn from(wtxid: Wtxid) -> Self { Self::from_byte_array(wtxid.to_byte_array()) } -} /// Bitcoin block header. /// diff --git a/bitcoin/src/consensus/encode.rs b/bitcoin/src/consensus/encode.rs index beed6e5b2..7380805ef 100644 --- a/bitcoin/src/consensus/encode.rs +++ b/bitcoin/src/consensus/encode.rs @@ -23,9 +23,10 @@ use io::{BufRead, Cursor, Read, Write}; use crate::bip152::{PrefilledTransaction, ShortId}; use crate::bip158::{FilterHash, FilterHeader}; -use crate::blockdata::block::{self, BlockHash, TxMerkleNode}; +use crate::blockdata::block::{self, BlockHash}; use crate::blockdata::transaction::{Transaction, TxIn, TxOut}; use crate::consensus::{DecodeError, IterReader}; +use crate::merkle_tree::TxMerkleNode; #[cfg(feature = "std")] use crate::p2p::{ address::{AddrV2Message, Address}, diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index 1d03fef81..5fdbe4fb1 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -116,7 +116,7 @@ pub use crate::{ amount::{Amount, Denomination, SignedAmount}, bip158::{FilterHash, FilterHeader}, bip32::XKeyIdentifier, - blockdata::block::{self, Block, BlockHash, TxMerkleNode, WitnessMerkleNode, WitnessCommitment}, + blockdata::block::{self, Block, BlockHash, WitnessCommitment}, blockdata::constants, blockdata::fee_rate::FeeRate, blockdata::locktime::{self, absolute, relative}, @@ -132,7 +132,7 @@ pub use crate::{ crypto::ecdsa, crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, CompressedPublicKey, WPubkeyHash, XOnlyPublicKey}, crypto::sighash::{self, LegacySighash, SegwitV0Sighash, TapSighash, TapSighashTag}, - merkle_tree::MerkleBlock, + merkle_tree::{MerkleBlock, TxMerkleNode, WitnessMerkleNode}, network::{Network, NetworkKind}, pow::{CompactTarget, Target, Work}, psbt::Psbt, diff --git a/bitcoin/src/merkle_tree/block.rs b/bitcoin/src/merkle_tree/block.rs index fbd6f92ff..8f090edef 100644 --- a/bitcoin/src/merkle_tree/block.rs +++ b/bitcoin/src/merkle_tree/block.rs @@ -14,10 +14,11 @@ use core::fmt; use io::{BufRead, Write}; use self::MerkleBlockError::*; -use crate::blockdata::block::{self, Block, TxMerkleNode}; +use crate::blockdata::block::{self, Block}; use crate::blockdata::transaction::{Transaction, Txid}; use crate::blockdata::weight::Weight; use crate::consensus::encode::{self, Decodable, Encodable, MAX_VEC_SIZE}; +use crate::merkle_tree::TxMerkleNode; use crate::prelude::*; /// Data structure that represents a block header paired to a partial merkle tree. diff --git a/bitcoin/src/merkle_tree/mod.rs b/bitcoin/src/merkle_tree/mod.rs index d91a81e48..4ef118d6d 100644 --- a/bitcoin/src/merkle_tree/mod.rs +++ b/bitcoin/src/merkle_tree/mod.rs @@ -18,16 +18,35 @@ mod block; use core::cmp::min; use core::iter; -use hashes::Hash; +use hashes::{sha256d, Hash}; use io::Write; use crate::consensus::encode::Encodable; +use crate::internal_macros::impl_hashencode; use crate::prelude::*; +use crate::{Txid, Wtxid}; #[rustfmt::skip] #[doc(inline)] pub use self::block::{MerkleBlock, MerkleBlockError, PartialMerkleTree}; +hashes::hash_newtype! { + /// A hash of the Merkle tree branch or root for transactions. + pub struct TxMerkleNode(sha256d::Hash); + /// A hash corresponding to the Merkle tree root for witness data. + pub struct WitnessMerkleNode(sha256d::Hash); +} +impl_hashencode!(TxMerkleNode); +impl_hashencode!(WitnessMerkleNode); + +impl From for TxMerkleNode { + fn from(txid: Txid) -> Self { Self::from_byte_array(txid.to_byte_array()) } +} + +impl From for WitnessMerkleNode { + fn from(wtxid: Wtxid) -> Self { Self::from_byte_array(wtxid.to_byte_array()) } +} + /// Calculates the merkle root of a list of *hashes*, inline (in place) in `hashes`. /// /// In most cases, you'll want to use [`calculate_root`] instead. Please note, calling this function