diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2021-05-13 05:35:37 +0000 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2021-05-13 05:35:37 +0000 |
| commit | 223cd670577c55d65762a056782124dc4c905f04 (patch) | |
| tree | 1bc0cf9010c7af1c3ff2732856360b88c289b43e /src/cmd | |
| parent | acd9b487c189d7f82ae9187f0fffd0607478abc4 (diff) | |
Use input files for binary input and add utilities for hex encoding
Diffstat (limited to 'src/cmd')
| -rw-r--r-- | src/cmd/hash.rs | 28 | ||||
| -rw-r--r-- | src/cmd/hex.rs | 49 | ||||
| -rw-r--r-- | src/cmd/sign.rs | 24 |
3 files changed, 60 insertions, 41 deletions
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); |
