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
use encodings::hex::{FromHex, FromHexError, ToHex};
use std::fmt;
use std::str::FromStr;

#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
//Possibly make this generic on the size?
//Not sure if we'll need that, but just a reminder
//I think we might actually want to implement this as a trait?
//@todo from<Uint256>
pub struct Hash([u8; 32]);

impl Hash {
    pub fn to_array(&self) -> [u8; 32] {
        self.0
    }

    //TODO remove this and use the trait.
    pub fn to_string(&self) -> String {
        self.to_hex()
    }

    pub fn is_null(&self) -> bool {
        for byte in self.0.iter() {
            if *byte != 0 {
                return false;
            }
        }
        true
    }
}

//Needs to be TryFrom
//Need more checks here for length, and errors
impl From<Vec<u8>> for Hash {
    fn from(hex_vec: Vec<u8>) -> Self {
        let mut array = [0; 32];
        array.copy_from_slice(&hex_vec);
        Hash(array)
    }
}

//This should only be implemented on Blake2b hash
//Redo this when we split to blake2b/ run into problems TODO
impl From<[u8; 32]> for Hash {
    fn from(bytes: [u8; 32]) -> Self {
        Hash(bytes)
    }
}

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

impl FromHex for Hash {
    type Error = FromHexError;
    fn from_hex<T: AsRef<[u8]>>(hex: T) -> std::result::Result<Self, Self::Error> {
        let bytes = Vec::from_hex(hex)?;
        if bytes.len() != 32 {
            Err(FromHexError::InvalidHexLength)
        } else {
            let mut ret = [0; 32];
            ret.copy_from_slice(&bytes);
            Ok(Hash::from(ret))
        }
    }
}

impl ToHex for Hash {
    fn to_hex(&self) -> String {
        self.0.to_vec().to_hex()
    }
}

impl FromStr for Hash {
    type Err = FromHexError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Hash::from_hex(s)?)
    }
}

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

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

#[cfg(feature = "serialization")]
impl serde::Serialize for Hash {
    fn serialize<S: serde::Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
        if s.is_human_readable() {
            s.serialize_str(&self.to_hex())
        } else {
            s.serialize_bytes(&self.to_array())
        }
    }
}

#[cfg(feature = "serialization")]
impl<'de> serde::Deserialize<'de> for Hash {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> std::result::Result<Hash, D::Error> {
        if d.is_human_readable() {
            struct HexVisitor;

            impl<'de> serde::de::Visitor<'de> for HexVisitor {
                type Value = Hash;

                fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                    formatter.write_str("an ASCII hex string")
                }

                fn visit_bytes<E>(self, v: &[u8]) -> std::result::Result<Self::Value, E>
                where
                    E: ::serde::de::Error,
                {
                    if let Ok(hex) = ::std::str::from_utf8(v) {
                        Hash::from_hex(hex).map_err(E::custom)
                    } else {
                        return Err(E::invalid_value(serde::de::Unexpected::Bytes(v), &self));
                    }
                }

                fn visit_str<E>(self, v: &str) -> std::result::Result<Self::Value, E>
                where
                    E: ::serde::de::Error,
                {
                    Hash::from_hex(v).map_err(E::custom)
                }
            }

            d.deserialize_str(HexVisitor)
        } else {
            struct BytesVisitor;

            impl<'de> ::serde::de::Visitor<'de> for BytesVisitor {
                type Value = Hash;

                fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                    formatter.write_str("a bytestring")
                }

                fn visit_bytes<E>(self, v: &[u8]) -> std::result::Result<Self::Value, E>
                where
                    E: ::serde::de::Error,
                {
                    if v.len() != 32 {
                        Err(E::invalid_length(v.len(), &stringify!(32)))
                    } else {
                        let mut ret = [0; 32];
                        ret.copy_from_slice(v);
                        Ok(Hash(ret))
                    }
                }
            }

            d.deserialize_bytes(BytesVisitor)
        }
    }
}

//TODO need to test this, and add testing for serde stuff.