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
use crate::{Utilities, UtilitiesError, UtilitiesResult};

#[cfg(feature = "tai64")]
use std::time::SystemTime;

impl Utilities {
    /// Convert some bytes to a 12 byte array
    pub fn to_12byte_array(bytes: &[u8]) -> UtilitiesResult<[u8; 12]> {
        if bytes.len() < 12 {
            return Err(UtilitiesError::LengthLessThan12Bytes);
        }

        if bytes.len() > 12 {
            return Err(UtilitiesError::LengthGreaterThan12Bytes);
        }

        Ok(bytes.try_into().unwrap()) // Never fails due to checks above
    }

    /// Convert some bytes to a 16 byte array
    pub fn to_16byte_array(bytes: &[u8]) -> UtilitiesResult<[u8; 16]> {
        if bytes.len() < 16 {
            return Err(UtilitiesError::LengthLessThan16Bytes);
        }

        if bytes.len() > 16 {
            return Err(UtilitiesError::LengthGreaterThan16Bytes);
        }

        Ok(bytes.try_into().unwrap()) // Never fails due to checks above
    }

    /// Convert some bytes to a 24 byte array
    pub fn to_24byte_array(bytes: &[u8]) -> UtilitiesResult<[u8; 24]> {
        if bytes.len() < 24 {
            return Err(UtilitiesError::LengthLessThan24Bytes);
        }

        if bytes.len() > 24 {
            return Err(UtilitiesError::LengthGreaterThan24Bytes);
        }

        Ok(bytes.try_into().unwrap()) // Never fails due to checks above
    }

    /// Convert some bytes to a 32 byte array
    pub fn to_32byte_array(bytes: &[u8]) -> UtilitiesResult<[u8; 32]> {
        if bytes.len() < 32 {
            return Err(UtilitiesError::LengthLessThan32Bytes);
        }

        if bytes.len() > 32 {
            return Err(UtilitiesError::LengthGreaterThan32Bytes);
        }

        Ok(bytes.try_into().unwrap()) // Never fails due to checks above
    }

    /// Convert some bytes to a 64 byte array
    pub fn to_64byte_array(bytes: &[u8]) -> UtilitiesResult<[u8; 64]> {
        if bytes.len() < 64 {
            return Err(UtilitiesError::LengthLessThan64Bytes);
        }

        if bytes.len() > 64 {
            return Err(UtilitiesError::LengthGreaterThan64Bytes);
        }

        Ok(bytes.try_into().unwrap()) // Never fails due to checks above
    }

    /// Convert some bytes to a 128 byte array
    pub fn to_128byte_array(bytes: &[u8]) -> UtilitiesResult<[u8; 128]> {
        if bytes.len() < 128 {
            return Err(UtilitiesError::LengthLessThan128Bytes);
        }

        if bytes.len() > 128 {
            return Err(UtilitiesError::LengthGreaterThan128Bytes);
        }

        Ok(bytes.try_into().unwrap()) // Never fails due to checks above
    }

    /// decode hex to bytes
    #[cfg(feature = "hex")]
    pub fn hex_to_bytes(value: &str) -> UtilitiesResult<Vec<u8>> {
        Ok(hex::decode(value)?)
    }

    /// Decode a hex string to a buffer
    #[cfg(feature = "hex")]
    pub fn hex_to_buffer(value: &str, buffer: &mut [u8]) -> UtilitiesResult<()> {
        Ok(hex::decode_to_slice(value, buffer)?)
    }

    /// Decode base58 string
    #[cfg(feature = "base58")]
    pub fn base58_to_bytes(value: &str) -> UtilitiesResult<Vec<u8>> {
        Ok(bs58::decode(value).into_vec()?)
    }

    /// Convert given bytes to a Tai64N structure
    #[cfg(feature = "tai64")]
    pub fn bytes_to_tai64n(value: &[u8]) -> UtilitiesResult<tai64::Tai64N> {
        Ok(tai64::Tai64N::from_slice(value)?)
    }

    /// Convert a `SystemTime` to Tai64N
    #[cfg(feature = "tai64")]
    pub fn systemtime_to_tai64(value: &SystemTime) -> tai64::Tai64N {
        tai64::Tai64N::from_system_time(value)
    }

    /// Get the seconds since `Tai64N::UNIX_EPOCH`
    #[cfg(feature = "tai64")]
    pub fn tai64_get_secs(value: tai64::Tai64N) -> UtilitiesResult<u64> {
        match value.duration_since(&tai64::Tai64N::UNIX_EPOCH) {
            Ok(duration) => Ok(duration.as_secs()),
            Err(_) => Err(UtilitiesError::Tai64InvalidEarlierDuaration),
        }
    }

    /// Get the milliseconds since `Tai64N::UNIX_EPOCH`
    #[cfg(feature = "tai64")]
    pub fn tai64_get_millis(value: tai64::Tai64N) -> UtilitiesResult<u128> {
        match value.duration_since(&tai64::Tai64N::UNIX_EPOCH) {
            Ok(duration) => Ok(duration.as_millis()),
            Err(_) => Err(UtilitiesError::Tai64InvalidEarlierDuaration),
        }
    }

    /// Get the nanoseconds since `Tai64N::UNIX_EPOCH`
    #[cfg(feature = "tai64")]
    pub fn tai64_get_nanos(value: tai64::Tai64N) -> UtilitiesResult<u128> {
        match value.duration_since(&tai64::Tai64N::UNIX_EPOCH) {
            Ok(duration) => Ok(duration.as_nanos()),
            Err(_) => Err(UtilitiesError::Tai64InvalidEarlierDuaration),
        }
    }

    /// Get the seconds since `std::time::UNIX_EPOCH`
    #[cfg(feature = "tai64")]
    pub fn systemtime_get_secs(value: SystemTime) -> UtilitiesResult<u64> {
        use std::time::UNIX_EPOCH;

        match value.duration_since(UNIX_EPOCH) {
            Ok(duration) => Ok(duration.as_secs()),
            Err(_) => Err(UtilitiesError::SystemtimeInvalidEarlierDuaration),
        }
    }

    /// Get the milliseconds since `std::time::UNIX_EPOCH`
    #[cfg(feature = "tai64")]
    pub fn systemtime_get_millis(value: SystemTime) -> UtilitiesResult<u128> {
        use std::time::UNIX_EPOCH;

        match value.duration_since(UNIX_EPOCH) {
            Ok(duration) => Ok(duration.as_millis()),
            Err(_) => Err(UtilitiesError::SystemtimeInvalidEarlierDuaration),
        }
    }

    /// Get the nanoseconds since `std::time::UNIX_EPOCH`
    #[cfg(feature = "tai64")]
    pub fn systemtime_get_nanos(value: SystemTime) -> UtilitiesResult<u128> {
        use std::time::UNIX_EPOCH;

        match value.duration_since(UNIX_EPOCH) {
            Ok(duration) => Ok(duration.as_nanos()),
            Err(_) => Err(UtilitiesError::SystemtimeInvalidEarlierDuaration),
        }
    }
}