bitcoin/
hash_types.rs

1// Rust Bitcoin Library
2// Written in 2014 by
3//   Andrew Poelstra <apoelstra@wpsoftware.net>
4//
5// To the extent possible under law, the author(s) have dedicated all
6// copyright and related and neighboring rights to this software to
7// the public domain worldwide. This software is distributed without
8// any warranty.
9//
10// You should have received a copy of the CC0 Public Domain Dedication
11// along with this software.
12// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
13//
14
15//! File defines types for hashes used throughout the library. These types are needed in order
16//! to avoid mixing data of the same hash format (like SHA256d) but of different meaning
17//! (transaction id, block hash etc).
18
19use hashes::{Hash, sha256, sha256d, hash160};
20
21macro_rules! impl_hashencode {
22    ($hashtype:ident) => {
23        impl $crate::consensus::Encodable for $hashtype {
24            fn consensus_encode<S: ::std::io::Write>(&self, s: S) -> Result<usize, ::std::io::Error> {
25                self.0.consensus_encode(s)
26            }
27        }
28
29        impl $crate::consensus::Decodable for $hashtype {
30            fn consensus_decode<D: ::std::io::Read>(d: D) -> Result<Self, $crate::consensus::encode::Error> {
31                use $crate::hashes::Hash;
32                Ok(Self::from_inner(<<$hashtype as $crate::hashes::Hash>::Inner>::consensus_decode(d)?))
33            }
34        }
35    }
36}
37
38hash_newtype!(Txid, sha256d::Hash, 32, doc="A bitcoin transaction hash/transaction ID.");
39hash_newtype!(Wtxid, sha256d::Hash, 32, doc="A bitcoin witness transaction ID.");
40hash_newtype!(BlockHash, sha256d::Hash, 32, doc="A bitcoin block hash.");
41hash_newtype!(SigHash, sha256d::Hash, 32, doc="Hash of the transaction according to the signature algorithm");
42
43hash_newtype!(PubkeyHash, hash160::Hash, 20, doc="A hash of a public key.");
44hash_newtype!(ScriptHash, hash160::Hash, 20, doc="A hash of Bitcoin Script bytecode.");
45hash_newtype!(WPubkeyHash, hash160::Hash, 20, doc="SegWit version of a public key hash.");
46hash_newtype!(WScriptHash, sha256::Hash, 32, doc="SegWit version of a Bitcoin Script bytecode hash.");
47
48hash_newtype!(TxMerkleNode, sha256d::Hash, 32, doc="A hash of the Merkle tree branch or root for transactions");
49hash_newtype!(WitnessMerkleNode, sha256d::Hash, 32, doc="A hash corresponding to the Merkle tree root for witness data");
50hash_newtype!(WitnessCommitment, sha256d::Hash, 32, doc="A hash corresponding to the witness structure commitment in the coinbase transaction");
51hash_newtype!(XpubIdentifier, hash160::Hash, 20, doc="XpubIdentifier as defined in BIP-32.");
52
53hash_newtype!(FilterHash, sha256d::Hash, 32, doc="Filter hash, as defined in BIP-157");
54hash_newtype!(FilterHeader, sha256d::Hash, 32, doc="Filter header, as defined in BIP-157");
55
56
57impl_hashencode!(Txid);
58impl_hashencode!(Wtxid);
59impl_hashencode!(SigHash);
60impl_hashencode!(BlockHash);
61impl_hashencode!(TxMerkleNode);
62impl_hashencode!(WitnessMerkleNode);
63impl_hashencode!(FilterHash);
64impl_hashencode!(FilterHeader);