extended_primitives/
hash.rs

1use encodings::hex::{FromHex, FromHexError, ToHex};
2use std::fmt;
3use std::str::FromStr;
4
5#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
6//Possibly make this generic on the size?
7//Not sure if we'll need that, but just a reminder
8//I think we might actually want to implement this as a trait?
9//@todo from<Uint256>
10pub struct Hash([u8; 32]);
11
12impl Hash {
13    pub fn to_array(&self) -> [u8; 32] {
14        self.0
15    }
16
17    pub fn is_null(&self) -> bool {
18        for byte in self.0.iter() {
19            if *byte != 0 {
20                return false;
21            }
22        }
23        true
24    }
25}
26
27//Needs to be TryFrom
28//Need more checks here for length, and errors
29impl From<Vec<u8>> for Hash {
30    fn from(hex_vec: Vec<u8>) -> Self {
31        let mut array = [0; 32];
32        array.copy_from_slice(&hex_vec);
33        Hash(array)
34    }
35}
36
37//This should only be implemented on Blake2b hash
38//Redo this when we split to blake2b/ run into problems TODO
39impl From<[u8; 32]> for Hash {
40    fn from(bytes: [u8; 32]) -> Self {
41        Hash(bytes)
42    }
43}
44
45impl AsRef<[u8]> for Hash {
46    fn as_ref(&self) -> &[u8] {
47        &self.0
48    }
49}
50
51impl FromHex for Hash {
52    type Error = FromHexError;
53    fn from_hex<T: AsRef<[u8]>>(hex: T) -> std::result::Result<Self, Self::Error> {
54        let bytes = Vec::from_hex(hex)?;
55        if bytes.len() != 32 {
56            Err(FromHexError::InvalidHexLength)
57        } else {
58            let mut ret = [0; 32];
59            ret.copy_from_slice(&bytes);
60            Ok(Hash::from(ret))
61        }
62    }
63}
64
65impl ToHex for Hash {
66    fn to_hex(&self) -> String {
67        self.0.to_vec().to_hex()
68    }
69}
70
71impl FromStr for Hash {
72    type Err = FromHexError;
73
74    fn from_str(s: &str) -> Result<Self, Self::Err> {
75        Hash::from_hex(s)
76    }
77}
78
79impl fmt::Display for Hash {
80    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81        write!(f, "{}", self.to_hex())
82    }
83}
84
85impl fmt::Debug for Hash {
86    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
87        write!(f, "{}", self.to_hex())
88    }
89}
90
91#[cfg(feature = "serialization")]
92impl serde::Serialize for Hash {
93    fn serialize<S: serde::Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
94        if s.is_human_readable() {
95            s.serialize_str(&self.to_hex())
96        } else {
97            s.serialize_bytes(&self.to_array())
98        }
99    }
100}
101
102#[cfg(feature = "serialization")]
103impl<'de> serde::Deserialize<'de> for Hash {
104    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> std::result::Result<Hash, D::Error> {
105        if d.is_human_readable() {
106            struct HexVisitor;
107
108            impl<'de> serde::de::Visitor<'de> for HexVisitor {
109                type Value = Hash;
110
111                fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
112                    formatter.write_str("an ASCII hex string")
113                }
114
115                fn visit_bytes<E>(self, v: &[u8]) -> std::result::Result<Self::Value, E>
116                where
117                    E: ::serde::de::Error,
118                {
119                    if let Ok(hex) = ::std::str::from_utf8(v) {
120                        Hash::from_hex(hex).map_err(E::custom)
121                    } else {
122                        return Err(E::invalid_value(serde::de::Unexpected::Bytes(v), &self));
123                    }
124                }
125
126                fn visit_str<E>(self, v: &str) -> std::result::Result<Self::Value, E>
127                where
128                    E: ::serde::de::Error,
129                {
130                    Hash::from_hex(v).map_err(E::custom)
131                }
132            }
133
134            d.deserialize_str(HexVisitor)
135        } else {
136            struct BytesVisitor;
137
138            impl<'de> ::serde::de::Visitor<'de> for BytesVisitor {
139                type Value = Hash;
140
141                fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
142                    formatter.write_str("a bytestring")
143                }
144
145                fn visit_bytes<E>(self, v: &[u8]) -> std::result::Result<Self::Value, E>
146                where
147                    E: ::serde::de::Error,
148                {
149                    if v.len() != 32 {
150                        Err(E::invalid_length(v.len(), &stringify!(32)))
151                    } else {
152                        let mut ret = [0; 32];
153                        ret.copy_from_slice(v);
154                        Ok(Hash(ret))
155                    }
156                }
157            }
158
159            d.deserialize_bytes(BytesVisitor)
160        }
161    }
162}
163
164//TODO need to test this, and add testing for serde stuff.