1
0
Fork 0
mirror of https://github.com/romanz/electrs.git synced 2025-02-24 15:02:21 +01:00

Add simple from_hex() helper

This commit is contained in:
Roman Zeyde 2018-04-11 19:44:56 +03:00
parent c68a6fdedb
commit 611fce4dfb
No known key found for this signature in database
GPG key ID: 87CAE5FA46917CBB
2 changed files with 33 additions and 4 deletions

View file

@ -40,10 +40,9 @@ impl Daemon {
fn get_headers(&self) -> (HeaderMap, Bytes) { fn get_headers(&self) -> (HeaderMap, Bytes) {
let mut headers = HashMap::new(); let mut headers = HashMap::new();
let mut blockhash: Bytes = vec![ let mut blockhash: Bytes = util::from_hex(
111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, "6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000",
131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, ).unwrap(); // genesis block hash
]; // genesis block hash
loop { loop {
let data = self.get(&format!("headers/2000/{}.bin", util::revhex(&blockhash))); let data = self.get(&format!("headers/2000/{}.bin", util::revhex(&blockhash)));
assert!(!data.is_empty()); assert!(!data.is_empty());

View file

@ -8,3 +8,33 @@ pub fn revhex(data: &[u8]) -> String {
} }
ret ret
} }
pub fn from_hex(s: &str) -> Option<Vec<u8>> {
let mut b = Vec::with_capacity(s.len() / 2);
let mut modulus = 0;
let mut buf = 0;
for byte in s.bytes() {
buf <<= 4;
match byte {
b'A'...b'F' => buf |= byte - b'A' + 10,
b'a'...b'f' => buf |= byte - b'a' + 10,
b'0'...b'9' => buf |= byte - b'0',
_ => {
return None;
}
}
modulus += 1;
if modulus == 2 {
modulus = 0;
b.push(buf);
}
}
match modulus {
0 => Some(b),
_ => None,
}
}