diff --git a/zebra-chain/src/lib.rs b/zebra-chain/src/lib.rs index 372b36a5b..c823e6728 100644 --- a/zebra-chain/src/lib.rs +++ b/zebra-chain/src/lib.rs @@ -24,6 +24,7 @@ pub mod proofs; pub mod serialization; pub mod transaction; pub mod types; +pub mod utils; pub use ed25519_zebra; pub use redjubjub; diff --git a/zebra-chain/src/utils.rs b/zebra-chain/src/utils.rs new file mode 100644 index 000000000..3498f88de --- /dev/null +++ b/zebra-chain/src/utils.rs @@ -0,0 +1,15 @@ +//! Utility functions for chain data. + +/// Returns the hexadecimal-encoded string `s` in byte-reversed order. +pub fn byte_reverse_hex(s: &str) -> String { + String::from_utf8( + s.as_bytes() + .chunks(2) + .rev() + .map(|c| c.iter()) + .flatten() + .cloned() + .collect::>(), + ) + .expect("input should be ascii") +} diff --git a/zebrad/src/commands/revhex.rs b/zebrad/src/commands/revhex.rs index c453daa8b..168bd241b 100644 --- a/zebrad/src/commands/revhex.rs +++ b/zebrad/src/commands/revhex.rs @@ -16,20 +16,6 @@ pub struct RevhexCmd { input: String, } -/// Returns the hexadecimal-encoded string `s` in byte-reversed order. -fn byte_reverse_hex(s: &str) -> String { - String::from_utf8( - s.as_bytes() - .chunks(2) - .rev() - .map(|c| c.iter()) - .flatten() - .cloned() - .collect::>(), - ) - .expect("input should be ascii") -} - impl Runnable for RevhexCmd { /// Print endian-reversed hex string. fn run(&self) { @@ -40,10 +26,10 @@ impl Runnable for RevhexCmd { // We can distinguish EOF from an empty line, because the newline is // included in the buffer, so empty lines return Ok(1). while stdin().read_line(&mut input).unwrap_or(0) > 0 { - println!("{}", byte_reverse_hex(&input.trim())); + println!("{}", zebra_chain::utils::byte_reverse_hex(&input.trim())); } } else { - println!("{}", byte_reverse_hex(&self.input)); + println!("{}", zebra_chain::utils::byte_reverse_hex(&self.input)); } } }