web3utilities/
formats.rs

1use crate::{Utilities, UtilitiesError, UtilitiesResult};
2
3#[cfg(feature = "tai64")]
4use std::time::SystemTime;
5
6impl Utilities {
7    /// Convert some bytes to a 12 byte array
8    pub fn to_12byte_array(bytes: &[u8]) -> UtilitiesResult<[u8; 12]> {
9        if bytes.len() < 12 {
10            return Err(UtilitiesError::LengthLessThan12Bytes);
11        }
12
13        if bytes.len() > 12 {
14            return Err(UtilitiesError::LengthGreaterThan12Bytes);
15        }
16
17        Ok(bytes.try_into().unwrap()) // Never fails due to checks above
18    }
19
20    /// Convert some bytes to a 16 byte array
21    pub fn to_16byte_array(bytes: &[u8]) -> UtilitiesResult<[u8; 16]> {
22        if bytes.len() < 16 {
23            return Err(UtilitiesError::LengthLessThan16Bytes);
24        }
25
26        if bytes.len() > 16 {
27            return Err(UtilitiesError::LengthGreaterThan16Bytes);
28        }
29
30        Ok(bytes.try_into().unwrap()) // Never fails due to checks above
31    }
32
33    /// Convert some bytes to a 24 byte array
34    pub fn to_24byte_array(bytes: &[u8]) -> UtilitiesResult<[u8; 24]> {
35        if bytes.len() < 24 {
36            return Err(UtilitiesError::LengthLessThan24Bytes);
37        }
38
39        if bytes.len() > 24 {
40            return Err(UtilitiesError::LengthGreaterThan24Bytes);
41        }
42
43        Ok(bytes.try_into().unwrap()) // Never fails due to checks above
44    }
45
46    /// Convert some bytes to a 32 byte array
47    pub fn to_32byte_array(bytes: &[u8]) -> UtilitiesResult<[u8; 32]> {
48        if bytes.len() < 32 {
49            return Err(UtilitiesError::LengthLessThan32Bytes);
50        }
51
52        if bytes.len() > 32 {
53            return Err(UtilitiesError::LengthGreaterThan32Bytes);
54        }
55
56        Ok(bytes.try_into().unwrap()) // Never fails due to checks above
57    }
58
59    /// Convert some bytes to a 64 byte array
60    pub fn to_64byte_array(bytes: &[u8]) -> UtilitiesResult<[u8; 64]> {
61        if bytes.len() < 64 {
62            return Err(UtilitiesError::LengthLessThan64Bytes);
63        }
64
65        if bytes.len() > 64 {
66            return Err(UtilitiesError::LengthGreaterThan64Bytes);
67        }
68
69        Ok(bytes.try_into().unwrap()) // Never fails due to checks above
70    }
71
72    /// Convert some bytes to a 128 byte array
73    pub fn to_128byte_array(bytes: &[u8]) -> UtilitiesResult<[u8; 128]> {
74        if bytes.len() < 128 {
75            return Err(UtilitiesError::LengthLessThan128Bytes);
76        }
77
78        if bytes.len() > 128 {
79            return Err(UtilitiesError::LengthGreaterThan128Bytes);
80        }
81
82        Ok(bytes.try_into().unwrap()) // Never fails due to checks above
83    }
84
85    /// decode hex to bytes
86    #[cfg(feature = "hex")]
87    pub fn hex_to_bytes(value: &str) -> UtilitiesResult<Vec<u8>> {
88        Ok(hex::decode(value)?)
89    }
90
91    /// Decode a hex string to a buffer
92    #[cfg(feature = "hex")]
93    pub fn hex_to_buffer(value: &str, buffer: &mut [u8]) -> UtilitiesResult<()> {
94        Ok(hex::decode_to_slice(value, buffer)?)
95    }
96
97    /// Decode base58 string
98    #[cfg(feature = "base58")]
99    pub fn base58_to_bytes(value: &str) -> UtilitiesResult<Vec<u8>> {
100        Ok(bs58::decode(value).into_vec()?)
101    }
102
103    /// Convert given bytes to a Tai64N structure
104    #[cfg(feature = "tai64")]
105    pub fn bytes_to_tai64n(value: &[u8]) -> UtilitiesResult<tai64::Tai64N> {
106        Ok(tai64::Tai64N::from_slice(value)?)
107    }
108
109    /// Convert a `SystemTime` to Tai64N
110    #[cfg(feature = "tai64")]
111    pub fn systemtime_to_tai64(value: &SystemTime) -> tai64::Tai64N {
112        tai64::Tai64N::from_system_time(value)
113    }
114
115    /// Get the seconds since `Tai64N::UNIX_EPOCH`
116    #[cfg(feature = "tai64")]
117    pub fn tai64_get_secs(value: tai64::Tai64N) -> UtilitiesResult<u64> {
118        match value.duration_since(&tai64::Tai64N::UNIX_EPOCH) {
119            Ok(duration) => Ok(duration.as_secs()),
120            Err(_) => Err(UtilitiesError::Tai64InvalidEarlierDuaration),
121        }
122    }
123
124    /// Get the milliseconds since `Tai64N::UNIX_EPOCH`
125    #[cfg(feature = "tai64")]
126    pub fn tai64_get_millis(value: tai64::Tai64N) -> UtilitiesResult<u128> {
127        match value.duration_since(&tai64::Tai64N::UNIX_EPOCH) {
128            Ok(duration) => Ok(duration.as_millis()),
129            Err(_) => Err(UtilitiesError::Tai64InvalidEarlierDuaration),
130        }
131    }
132
133    /// Get the nanoseconds since `Tai64N::UNIX_EPOCH`
134    #[cfg(feature = "tai64")]
135    pub fn tai64_get_nanos(value: tai64::Tai64N) -> UtilitiesResult<u128> {
136        match value.duration_since(&tai64::Tai64N::UNIX_EPOCH) {
137            Ok(duration) => Ok(duration.as_nanos()),
138            Err(_) => Err(UtilitiesError::Tai64InvalidEarlierDuaration),
139        }
140    }
141
142    /// Get the seconds since `std::time::UNIX_EPOCH`
143    #[cfg(feature = "tai64")]
144    pub fn systemtime_get_secs(value: SystemTime) -> UtilitiesResult<u64> {
145        use std::time::UNIX_EPOCH;
146
147        match value.duration_since(UNIX_EPOCH) {
148            Ok(duration) => Ok(duration.as_secs()),
149            Err(_) => Err(UtilitiesError::SystemtimeInvalidEarlierDuaration),
150        }
151    }
152
153    /// Get the milliseconds since `std::time::UNIX_EPOCH`
154    #[cfg(feature = "tai64")]
155    pub fn systemtime_get_millis(value: SystemTime) -> UtilitiesResult<u128> {
156        use std::time::UNIX_EPOCH;
157
158        match value.duration_since(UNIX_EPOCH) {
159            Ok(duration) => Ok(duration.as_millis()),
160            Err(_) => Err(UtilitiesError::SystemtimeInvalidEarlierDuaration),
161        }
162    }
163
164    /// Get the nanoseconds since `std::time::UNIX_EPOCH`
165    #[cfg(feature = "tai64")]
166    pub fn systemtime_get_nanos(value: SystemTime) -> UtilitiesResult<u128> {
167        use std::time::UNIX_EPOCH;
168
169        match value.duration_since(UNIX_EPOCH) {
170            Ok(duration) => Ok(duration.as_nanos()),
171            Err(_) => Err(UtilitiesError::SystemtimeInvalidEarlierDuaration),
172        }
173    }
174}