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
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash, Debug)]
//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?
pub struct Hash([u8; 32]);

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

    pub fn to_string(&self) -> String {
        hex::encode(self.0)
    }
}

//
//We can have it from string, or we can have it be from hex //TODO both might be useful.
//Need more checks here for length, and errors
impl From<String> for Hash {
    fn from(hex: String) -> Self {
        //Do not unwrap here, we need to catch this error.
        let raw = hex::decode(hex).unwrap();
        // let hash: &[32] = &raw;
        // Hash(raw.try_into())
        Hash::from(raw)
    }
}

impl From<&str> for Hash {
    fn from(hex: &str) -> Self {
        //Do not unwrap here, we need to catch this error.
        let raw = hex::decode(hex).unwrap();
        // let hash: &[32] = &raw;
        // Hash(raw.try_into())
        Hash::from(raw)
    }
}

//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)
    }
}

//Custom type specificly for Name Hashes.
#[derive(PartialEq, Eq, Clone, Debug, Default)]
pub struct NameHash(Hash);