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
use std::fmt::Write;
use crate::error::{Result, ErrorKind};
pub use stack_test_epic_core::global::is_mainnet;
pub mod base58;
pub mod crypto;
pub mod secp;
pub fn to_hex(bytes: Vec<u8>) -> String {
let mut s = String::new();
for byte in bytes {
write!(&mut s, "{:02x}", byte).expect("Unable to write");
}
s
}
pub fn from_hex(hex_str: String) -> Result<Vec<u8>> {
if hex_str.len() % 2 == 1 {
Err(ErrorKind::NumberParsingError)?;
}
let hex_trim = if &hex_str[..2] == "0x" {
hex_str[2..].to_owned()
} else {
hex_str.clone()
};
let vec = split_n(&hex_trim.trim()[..], 2)
.iter()
.map(|b| u8::from_str_radix(b, 16).map_err(|_| ErrorKind::NumberParsingError.into()))
.collect::<Result<Vec<u8>>>()?;
Ok(vec)
}
fn split_n(s: &str, n: usize) -> Vec<&str> {
(0..(s.len() - n + 1) / 2 + 1)
.map(|i| &s[2 * i..2 * i + n])
.collect()
}