helios/src/common/utils.rs

27 lines
658 B
Rust
Raw Normal View History

2022-08-21 16:59:47 +00:00
use ethers::prelude::Address;
2022-08-16 22:59:07 +00:00
use eyre::Result;
use ssz_rs::{Node, Vector};
2022-08-20 20:33:32 +00:00
2022-08-21 15:21:50 +00:00
use crate::consensus::types::Bytes32;
2022-08-16 22:59:07 +00:00
pub fn hex_str_to_bytes(s: &str) -> Result<Vec<u8>> {
let stripped = s.strip_prefix("0x").unwrap_or(s);
Ok(hex::decode(stripped)?)
}
pub fn bytes32_to_node(bytes: &Bytes32) -> Result<Node> {
Ok(Node::from_bytes(bytes.as_slice().try_into()?))
}
pub fn bytes_to_bytes32(bytes: &[u8]) -> Bytes32 {
Vector::from_iter(bytes.to_vec())
}
2022-08-21 16:59:47 +00:00
pub fn address_to_hex_string(address: &Address) -> String {
format!("0x{}", hex::encode(address.as_bytes()))
}
pub fn u64_to_hex_string(val: u64) -> String {
format!("0x{:x}", val)
}