1use ethereum_types::{Address, H256, U256};
2use std::str::FromStr;
3
4pub fn maybe_address(address: &Address) -> Option<Address> {
5 if address.is_zero() {
6 None
7 } else {
8 Some(*address)
9 }
10}
11
12pub fn hexstr_to_h256(hex_str: &str) -> H256 {
13 assert_eq!(hex_str.len(), 64);
14 let mut bytes: [u8; 32] = Default::default();
15
16 for i in 0..32 {
17 bytes[i] = u8::from_str_radix(&hex_str[i * 2..i * 2 + 2], 16).unwrap();
18 }
19
20 H256::from(bytes)
21}
22
23pub fn option_vec_to_hex(data: Option<&Vec<u8>>) -> String {
24 match data {
25 Some(vec) => {
26 format!("0x{}", hex::encode(vec))
27 }
28 None => String::from("None"),
29 }
30}
31
32pub fn parse_hex_string<F: FromStr>(hex_str: &str) -> Result<F, F::Err> {
33 hex_str.strip_prefix("0x").unwrap_or(hex_str).parse()
34}
35
36pub fn u256_to_h256_be(value: U256) -> H256 {
37 let mut buf = [0u8; 32];
38 value.to_big_endian(&mut buf);
39 H256::from(buf)
40}
41
42pub fn h256_to_u256_be(value: H256) -> U256 {
43 U256::from_big_endian(value.as_bytes())
44}