1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::crypto::blake2b_256_hash;
use sbor::rust::borrow::ToOwned;
use sbor::rust::convert::TryFrom;
use sbor::rust::fmt;
use sbor::rust::str::FromStr;
use sbor::rust::string::String;
use sbor::rust::vec::Vec;
use sbor::*;
use utils::copy_u8_array;

/// Represents a 32-byte hash digest.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Sbor)]
#[sbor(transparent)]
pub struct Hash(pub [u8; Self::LENGTH]);

impl Hash {
    pub const LENGTH: usize = 32;

    pub fn lower_bytes<const N: usize>(&self) -> [u8; N] {
        self.0[(Self::LENGTH - N)..Self::LENGTH].try_into().unwrap()
    }
}

pub trait IsHash: AsRef<[u8]> + Sized + From<Hash> + Into<Hash> + AsRef<Hash> {
    fn as_bytes(&self) -> &[u8; Hash::LENGTH] {
        &<Self as AsRef<Hash>>::as_ref(self).0
    }

    fn as_slice(&self) -> &[u8] {
        &<Self as AsRef<Hash>>::as_ref(self).0
    }

    fn as_hash(&self) -> &Hash {
        &<Self as AsRef<Hash>>::as_ref(self)
    }

    fn into_bytes(self) -> [u8; Hash::LENGTH] {
        self.into_hash().0
    }

    fn into_hash(self) -> Hash {
        self.into()
    }

    fn from_bytes(bytes: [u8; Hash::LENGTH]) -> Self {
        Hash(bytes).into()
    }

    fn from_hash(hash: Hash) -> Self {
        hash.into()
    }
}

impl IsHash for Hash {}

impl AsRef<Hash> for Hash {
    fn as_ref(&self) -> &Hash {
        self
    }
}

impl AsRef<[u8]> for Hash {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

/// Computes the hash digest of a message.
pub fn hash<T: AsRef<[u8]>>(data: T) -> Hash {
    blake2b_256_hash(data)
}

//========
// error
//========

/// Represents an error when parsing hash.
#[derive(Debug, Clone, PartialEq, Eq, Sbor)]
pub enum ParseHashError {
    InvalidHex(String),
    InvalidLength(usize),
}

#[cfg(not(feature = "alloc"))]
impl std::error::Error for ParseHashError {}

#[cfg(not(feature = "alloc"))]
impl fmt::Display for ParseHashError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

//========
// binary
//========

impl TryFrom<&[u8]> for Hash {
    type Error = ParseHashError;

    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
        if slice.len() != Hash::LENGTH {
            return Err(ParseHashError::InvalidLength(slice.len()));
        }
        Ok(Self(copy_u8_array(slice)))
    }
}

impl From<Hash> for Vec<u8> {
    fn from(value: Hash) -> Self {
        value.to_vec()
    }
}

impl Hash {
    pub fn to_vec(&self) -> Vec<u8> {
        self.0.to_vec()
    }
}

//======
// text
//======

impl FromStr for Hash {
    type Err = ParseHashError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let bytes = hex::decode(s).map_err(|_| ParseHashError::InvalidHex(s.to_owned()))?;
        Self::try_from(bytes.as_slice())
    }
}

impl fmt::Display for Hash {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "{}", hex::encode(self.0))
    }
}

impl fmt::Debug for Hash {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "{}", self)
    }
}

#[macro_export]
macro_rules! define_wrapped_hash {
    ($(#[$docs:meta])* $name:ident) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Sbor)]
        #[sbor(transparent)]
        $(#[$docs])*
        pub struct $name(pub Hash);

        impl AsRef<[u8]> for $name {
            fn as_ref(&self) -> &[u8] {
                self.0.as_ref()
            }
        }

        impl AsRef<Hash> for $name {
            fn as_ref(&self) -> &Hash {
                &self.0
            }
        }

        impl From<Hash> for $name {
            fn from(value: Hash) -> Self {
                Self(value)
            }
        }

        impl From<$name> for Hash {
            fn from(value: $name) -> Self {
                value.0
            }
        }

        impl IsHash for $name {}
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use sbor::rust::string::ToString;

    #[test]
    fn test_from_to_string() {
        let s = "b177968c9c68877dc8d33e25759183c556379daa45a4d78a2b91c70133c873ca";
        let h = Hash::from_str(s).unwrap();
        assert_eq!(h.to_string(), s);
    }
}