From 611fce4dfbd6cdde98e8d239642f7274967803b4 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Wed, 11 Apr 2018 19:44:56 +0300 Subject: [PATCH] Add simple from_hex() helper --- src/daemon.rs | 7 +++---- src/util.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index 8f96492..6eafcea 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -40,10 +40,9 @@ impl Daemon { fn get_headers(&self) -> (HeaderMap, Bytes) { let mut headers = HashMap::new(); - let mut blockhash: Bytes = vec![ - 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, - 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, - ]; // genesis block hash + let mut blockhash: Bytes = util::from_hex( + "6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000", + ).unwrap(); // genesis block hash loop { let data = self.get(&format!("headers/2000/{}.bin", util::revhex(&blockhash))); assert!(!data.is_empty()); diff --git a/src/util.rs b/src/util.rs index 90251be..3cb9d03 100644 --- a/src/util.rs +++ b/src/util.rs @@ -8,3 +8,33 @@ pub fn revhex(data: &[u8]) -> String { } ret } + +pub fn from_hex(s: &str) -> Option> { + 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, + } +}