bitcoins/
hashes.rs

1//! This module holds `MarkedDigest` types used by Bitcoin transactions. Currently we represent
2//! only `TXID`s and `WTXID`s. In the future we may also represent sighash digests this way.
3
4use coins_core::{hashes, impl_hex_serde, marked_digest};
5
6marked_digest!(
7    /// A marked Hash256Digest representing transaction IDs
8    TXID,
9    hashes::Hash256
10);
11
12marked_digest!(
13    /// A marked Hash256Digest representing witness transaction IDs
14    WTXID,
15    hashes::Hash256
16);
17
18marked_digest!(
19    /// A marked Hash256Digest representing a block hash
20    BlockHash,
21    hashes::Hash256
22);
23
24impl_hex_serde!(TXID);
25impl_hex_serde!(WTXID);
26impl_hex_serde!(BlockHash);
27
28#[cfg(test)]
29mod test {
30    use super::*;
31    use coins_core::ser::ByteFormat;
32
33    #[test]
34    fn it_serializes_and_derializes_hash256digests() {
35        let cases = [(
36            TXID::default(),
37            "0000000000000000000000000000000000000000000000000000000000000000",
38        )];
39        for case in cases.iter() {
40            let digest = TXID::deserialize_hex(case.1).unwrap();
41            assert_eq!(digest.serialized_length(), 32);
42            assert_eq!(digest, case.0);
43            assert_eq!(digest.serialize_hex(), case.1);
44            assert_eq!(case.0.serialize_hex(), case.1);
45        }
46    }
47}