zebra_chain/transaction/
auth_digest.rs1use std::{fmt, sync::Arc};
4
5use hex::{FromHex, ToHex};
6
7use crate::{
8    primitives::zcash_primitives::auth_digest,
9    serialization::{
10        BytesInDisplayOrder, ReadZcashExt, SerializationError, WriteZcashExt, ZcashDeserialize,
11        ZcashSerialize,
12    },
13};
14
15use super::Transaction;
16
17#[cfg(any(test, feature = "proptest-impl"))]
18use proptest_derive::Arbitrary;
19
20#[derive(Copy, Clone, Eq, PartialEq, Hash, serde::Deserialize, serde::Serialize)]
27#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
28pub struct AuthDigest(pub [u8; 32]);
29
30impl BytesInDisplayOrder<true> for AuthDigest {
31    fn bytes_in_serialized_order(&self) -> [u8; 32] {
32        self.0
33    }
34
35    fn from_bytes_in_serialized_order(bytes: [u8; 32]) -> Self {
36        AuthDigest(bytes)
37    }
38}
39
40impl From<Transaction> for AuthDigest {
41    fn from(transaction: Transaction) -> Self {
47        AuthDigest::from(&transaction)
49    }
50}
51
52impl From<&Transaction> for AuthDigest {
53    fn from(transaction: &Transaction) -> Self {
59        auth_digest(transaction)
60    }
61}
62
63impl From<Arc<Transaction>> for AuthDigest {
64    fn from(transaction: Arc<Transaction>) -> Self {
70        transaction.as_ref().into()
71    }
72}
73
74impl From<[u8; 32]> for AuthDigest {
75    fn from(bytes: [u8; 32]) -> Self {
76        Self(bytes)
77    }
78}
79
80impl From<AuthDigest> for [u8; 32] {
81    fn from(auth_digest: AuthDigest) -> Self {
82        auth_digest.0
83    }
84}
85
86impl From<&AuthDigest> for [u8; 32] {
87    fn from(auth_digest: &AuthDigest) -> Self {
88        (*auth_digest).into()
89    }
90}
91
92impl ToHex for &AuthDigest {
93    fn encode_hex<T: FromIterator<char>>(&self) -> T {
94        self.bytes_in_display_order().encode_hex()
95    }
96
97    fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
98        self.bytes_in_display_order().encode_hex_upper()
99    }
100}
101
102impl ToHex for AuthDigest {
103    fn encode_hex<T: FromIterator<char>>(&self) -> T {
104        (&self).encode_hex()
105    }
106
107    fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
108        (&self).encode_hex_upper()
109    }
110}
111
112impl FromHex for AuthDigest {
113    type Error = <[u8; 32] as FromHex>::Error;
114
115    fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
116        let mut hash = <[u8; 32]>::from_hex(hex)?;
117        hash.reverse();
118
119        Ok(hash.into())
120    }
121}
122
123impl fmt::Display for AuthDigest {
124    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125        f.write_str(&self.encode_hex::<String>())
126    }
127}
128
129impl fmt::Debug for AuthDigest {
130    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131        f.debug_tuple("AuthDigest")
132            .field(&self.encode_hex::<String>())
133            .finish()
134    }
135}
136
137impl std::str::FromStr for AuthDigest {
138    type Err = SerializationError;
139
140    fn from_str(s: &str) -> Result<Self, Self::Err> {
141        let mut bytes = [0; 32];
142        if hex::decode_to_slice(s, &mut bytes[..]).is_err() {
143            Err(SerializationError::Parse("hex decoding error"))
144        } else {
145            bytes.reverse();
146            Ok(AuthDigest(bytes))
147        }
148    }
149}
150
151impl ZcashSerialize for AuthDigest {
152    fn zcash_serialize<W: std::io::Write>(&self, mut writer: W) -> Result<(), std::io::Error> {
153        writer.write_32_bytes(&self.into())
154    }
155}
156
157impl ZcashDeserialize for AuthDigest {
158    fn zcash_deserialize<R: std::io::Read>(mut reader: R) -> Result<Self, SerializationError> {
159        Ok(reader.read_32_bytes()?.into())
160    }
161}