litecoinlib/
hash_types.rs

1// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
2// SPDX-License-Identifier: CC0-1.0
3
4//! Bitcoin hash types.
5//!
6//! This module defines types for hashes used throughout the library. These
7//! types are needed in order to avoid mixing data of the same hash format
8//! (e.g. `SHA256d`) but of different meaning (such as transaction id, block
9//! hash).
10//!
11
12#[rustfmt::skip]
13macro_rules! impl_hashencode {
14    ($hashtype:ident) => {
15        impl $crate::consensus::Encodable for $hashtype {
16            fn consensus_encode<W: $crate::io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, $crate::io::Error> {
17                self.0.consensus_encode(w)
18            }
19        }
20
21        impl $crate::consensus::Decodable for $hashtype {
22            fn consensus_decode<R: $crate::io::Read + ?Sized>(r: &mut R) -> Result<Self, $crate::consensus::encode::Error> {
23                use $crate::hashes::Hash;
24                Ok(Self::from_inner(<<$hashtype as $crate::hashes::Hash>::Inner>::consensus_decode(r)?))
25            }
26        }
27    };
28}
29
30#[rustfmt::skip]
31macro_rules! impl_asref_push_bytes {
32    ($($hashtype:ident),*) => {
33        $(
34            impl AsRef<$crate::blockdata::script::PushBytes> for $hashtype {
35                fn as_ref(&self) -> &$crate::blockdata::script::PushBytes {
36                    use $crate::hashes::Hash;
37                    self.as_inner().as_ref()
38                }
39            }
40
41            impl From<$hashtype> for $crate::blockdata::script::PushBytesBuf {
42                fn from(hash: $hashtype) -> Self {
43                    use $crate::hashes::Hash;
44                    hash.as_inner().into()
45                }
46            }
47        )*
48    };
49}
50
51// newtypes module is solely here so we can rustfmt::skip.
52pub use newtypes::*;
53
54#[rustfmt::skip]
55mod newtypes {
56    use crate::hashes::{sha256, sha256d, hash160, hash_newtype};
57
58    hash_newtype!(
59        Txid, sha256d::Hash, 32, doc="A bitcoin transaction hash/transaction ID.
60
61For compatibility with the existing Bitcoin infrastructure and historical
62and current versions of the Bitcoin Core software itself, this and
63other [`sha256d::Hash`] types, are serialized in reverse
64byte order when converted to a hex string via [`std::fmt::Display`] trait operations.
65See [`hashes::Hash::DISPLAY_BACKWARD`] for more details.
66");
67    hash_newtype!(Wtxid, sha256d::Hash, 32, doc="A bitcoin witness transaction ID.");
68    hash_newtype!(BlockHash, sha256d::Hash, 32, doc="A bitcoin block hash.");
69
70    hash_newtype!(PubkeyHash, hash160::Hash, 20, doc="A hash of a public key.");
71    hash_newtype!(ScriptHash, hash160::Hash, 20, doc="A hash of Bitcoin Script bytecode.");
72    hash_newtype!(WPubkeyHash, hash160::Hash, 20, doc="SegWit version of a public key hash.");
73    hash_newtype!(WScriptHash, sha256::Hash, 32, doc="SegWit version of a Bitcoin Script bytecode hash.");
74
75    hash_newtype!(TxMerkleNode, sha256d::Hash, 32, doc="A hash of the Merkle tree branch or root for transactions");
76    hash_newtype!(WitnessMerkleNode, sha256d::Hash, 32, doc="A hash corresponding to the Merkle tree root for witness data");
77    hash_newtype!(WitnessCommitment, sha256d::Hash, 32, doc="A hash corresponding to the witness structure commitment in the coinbase transaction");
78    hash_newtype!(XpubIdentifier, hash160::Hash, 20, doc="XpubIdentifier as defined in BIP-32.");
79
80    hash_newtype!(FilterHash, sha256d::Hash, 32, doc="Filter hash, as defined in BIP-157");
81    hash_newtype!(FilterHeader, sha256d::Hash, 32, doc="Filter header, as defined in BIP-157");
82
83    impl_hashencode!(Txid);
84    impl_hashencode!(Wtxid);
85    impl_hashencode!(BlockHash);
86
87    impl_hashencode!(TxMerkleNode);
88    impl_hashencode!(WitnessMerkleNode);
89
90    impl_hashencode!(FilterHash);
91    impl_hashencode!(FilterHeader);
92
93    impl_asref_push_bytes!(PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash);
94}