chaincraft_rust/utils.rs
1//! Utility functions and helpers
2
3use std::time::{Duration, SystemTime, UNIX_EPOCH};
4
5/// Get current timestamp in milliseconds
6pub fn current_time_millis() -> u64 {
7 SystemTime::now()
8 .duration_since(UNIX_EPOCH)
9 .unwrap_or(Duration::from_secs(0))
10 .as_millis() as u64
11}
12
13/// Convert bytes to a hex string
14pub fn bytes_to_hex(bytes: &[u8]) -> String {
15 hex::encode(bytes)
16}
17
18/// Convert a hex string to bytes
19pub fn hex_to_bytes(hex: &str) -> Result<Vec<u8>, hex::FromHexError> {
20 hex::decode(hex)
21}
22
23/// Check if a string is a valid hex string
24pub fn is_valid_hex(hex: &str) -> bool {
25 hex.chars().all(|c| c.is_ascii_hexdigit())
26}