From 223cd670577c55d65762a056782124dc4c905f04 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Thu, 13 May 2021 05:35:37 +0000 Subject: Use input files for binary input and add utilities for hex encoding --- src/cmd.rs | 22 +++++++++++++++++++++- src/cmd/hash.rs | 28 +++------------------------- src/cmd/hex.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/cmd/sign.rs | 24 ++++++++---------------- src/main.rs | 3 +++ 5 files changed, 84 insertions(+), 42 deletions(-) create mode 100644 src/cmd/hex.rs (limited to 'src') diff --git a/src/cmd.rs b/src/cmd.rs index 63470a4..c10f834 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -3,11 +3,17 @@ pub mod address; pub mod export; pub mod hash; +pub mod hex; pub mod new; pub mod sign; use crate::{account::PrivateKey, hdk, mnemonic::Mnemonic}; use anyhow::Result; +use std::{ + fs, + io::{self, Read as _}, + path::Path, +}; use structopt::StructOpt; /// Shared account options. @@ -50,8 +56,22 @@ impl AccountOptions { fn permissive_hex(s: &str) -> Result> { let trimmed = s.chars().filter(|c| !c.is_whitespace()).collect::(); let hex_string = trimmed.strip_prefix("0x").unwrap_or(&trimmed); - let bytes = hex::decode(&hex_string)?; + let bytes = ::hex::decode(&hex_string)?; // NOTE: Use a boxed slice instead of a `Vec` as the former has special // scemantics with `structopt`. Ok(bytes.into_boxed_slice()) } + +/// Read input for the specified path with `-` used to signify standard in. +fn read_input(input: &Path) -> Result> { + let data = match input.to_str() { + Some("-") => { + let mut buf = Vec::new(); + io::stdin().read_to_end(&mut buf)?; + buf + } + _ => fs::read(&input)?, + }; + + Ok(data) +} diff --git a/src/cmd/hash.rs b/src/cmd/hash.rs index 78f33cb..aade76b 100644 --- a/src/cmd/hash.rs +++ b/src/cmd/hash.rs @@ -2,40 +2,18 @@ use crate::{cmd, hash}; use anyhow::Result; -use std::{ - fs, - io::{self, Read}, - path::PathBuf, - str, -}; +use std::path::PathBuf; use structopt::StructOpt; #[derive(Debug, StructOpt)] pub struct Options { - /// The data is hexidecimal encoded. - #[structopt(short, long)] - hex: bool, - /// Path to the data to hash. Use `-` for standard in. #[structopt(name = "DATA", default_value = "-")] data: PathBuf, } pub fn run(options: Options) -> Result<()> { - let data = match options.data.to_str() { - Some("-") => { - let mut buf = Vec::new(); - io::stdin().read_to_end(&mut buf)?; - buf - } - _ => fs::read(&options.data)?, - }; - let bytes = if options.hex { - cmd::permissive_hex(str::from_utf8(&data)?)? - } else { - data.into_boxed_slice() - }; - - println!("0x{}", hex::encode(hash::keccak256(bytes))); + let data = cmd::read_input(&options.data)?; + println!("0x{}", hex::encode(hash::keccak256(data))); Ok(()) } diff --git a/src/cmd/hex.rs b/src/cmd/hex.rs new file mode 100644 index 0000000..7b4e606 --- /dev/null +++ b/src/cmd/hex.rs @@ -0,0 +1,49 @@ +//! Module implementing the `address` subcommand for displaying the public +//! address for corresponding account. + +use crate::cmd; +use anyhow::Result; +use std::{ + io::{self, Write}, + path::PathBuf, + str, +}; +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +pub struct Options { + #[structopt(subcommand)] + op: Op, +} + +#[derive(Debug, StructOpt)] +enum Op { + /// Encode binary data as a hexadecimal string. + Encode { + /// Path to the data to encode. Use `-` for standard in. + #[structopt(name = "DATA", default_value = "-")] + data: PathBuf, + }, + + /// Decode a hexadecimal string as binary data. + Decode { + /// Path to the data to decode. Use `-` for standard in. + #[structopt(name = "DATA", default_value = "-")] + data: PathBuf, + }, +} + +pub fn run(options: Options) -> Result<()> { + match options.op { + Op::Encode { data } => { + let data = cmd::read_input(&data)?; + println!("0x{}", hex::encode(data)); + } + Op::Decode { data } => { + let data = cmd::read_input(&data)?; + let bytes = cmd::permissive_hex(str::from_utf8(&data)?)?; + io::stdout().write_all(&bytes)?; + } + } + Ok(()) +} diff --git a/src/cmd/sign.rs b/src/cmd/sign.rs index 355ec57..0fa002c 100644 --- a/src/cmd/sign.rs +++ b/src/cmd/sign.rs @@ -8,7 +8,7 @@ use crate::{ }; use anyhow::{anyhow, Context as _, Result}; use ethnum::U256; -use std::convert::TryInto; +use std::{convert::TryInto, path::PathBuf}; use structopt::StructOpt; #[derive(Debug, StructOpt)] @@ -40,15 +40,11 @@ enum Data { /// Sign an Ethereum message. Message { - /// The message is encoded as a hexadecimal string. - #[structopt(short, long)] - hex: bool, - - /// The message to sign in the "eth_sign" scheme. This message will be - /// prefixed with "\x19Ethereum Signed Message:\n" and its length before - /// hashing and singing. + /// Path to the message to sign in the "eth_sign" scheme. This message + /// will be prefixed with "\x19Ethereum Signed Message:\n" and its + /// length before hashing and singing. #[structopt(name = "MESSAGE")] - message: String, + message: PathBuf, }, /// Sign a raw data. @@ -99,14 +95,10 @@ impl Data { } => Ok(hash::keccak256( &transaction.as_parameters().signing_message(*chain_id), )), - Data::Message { hex, message } => { - let bytes = if *hex { - cmd::permissive_hex(&message)? - } else { - message.clone().into_bytes().into_boxed_slice() - }; + Data::Message { message } => { + let bytes = cmd::read_input(&message)?; - let mut buffer = Vec::new(); + let mut buffer = Vec::with_capacity(46 + bytes.len()); buffer.extend_from_slice(b"\x19Ethereum Signed Message:\n"); buffer.extend_from_slice(bytes.len().to_string().as_bytes()); buffer.extend_from_slice(&bytes); diff --git a/src/main.rs b/src/main.rs index 4fe666a..29d8ea5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,8 @@ enum Options { Export(export::Options), #[structopt(about = "Keccak256 hash data")] Hash(cmd::hash::Options), + #[structopt(about = "Hex encoding and decoding")] + Hex(cmd::hex::Options), #[structopt(about = "Generate a new HD wallet mnemonic")] New(new::Options), #[structopt(about = "Sign a message")] @@ -36,6 +38,7 @@ fn main() { Options::Address(options) => address::run(options), Options::Export(options) => export::run(options), Options::Hash(options) => cmd::hash::run(options), + Options::Hex(options) => cmd::hex::run(options), Options::New(options) => new::run(options), Options::Sign(options) => sign::run(options), } { -- cgit v1.2.3