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
use serde_bytes::ByteBuf;
use std::ops::Deref;

pub struct Bytes32(pub [u8; 32]);

impl Deref for Bytes32 {
    type Target = [u8; 32];
    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

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

impl From<Bytes32> for ByteBuf {
    fn from(value: Bytes32) -> Self {
        ByteBuf::from(value.0.to_vec())
    }
}

impl TryFrom<&[u8]> for Bytes32 {
    type Error = String;

    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        if value.len() != 32 {
            return Err(format!("expected 32 bytes, got {}", value.len()));
        }
        let mut bytes = [0u8; 32];
        bytes.copy_from_slice(value);
        Ok(Bytes32(bytes))
    }
}

impl TryFrom<Vec<u8>> for Bytes32 {
    type Error = String;

    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        let len = value.len();
        let bytes: [u8; 32] = value
            .try_into()
            .map_err(|_| format!("expected 32 bytes, got {}", len))?;
        Ok(Bytes32(bytes))
    }
}

impl TryFrom<&str> for Bytes32 {
    type Error = String;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        let data = hex::decode(value).map_err(|_| format!("failed to decode hex: {}", value))?;
        Self::try_from(data.as_slice())
    }
}

impl TryFrom<ByteBuf> for Bytes32 {
    type Error = String;

    fn try_from(value: ByteBuf) -> Result<Self, Self::Error> {
        Self::try_from(value.to_vec())
    }
}

impl TryFrom<&ByteBuf> for Bytes32 {
    type Error = String;

    fn try_from(value: &ByteBuf) -> Result<Self, Self::Error> {
        Self::try_from(value.as_slice())
    }
}