Skip to content

Commit

Permalink
feat(seth): improve piped input support (gakonst#149)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexey Shekhirin <a.shekhirin@gmail.com>
  • Loading branch information
gakonst and shekhirin authored Nov 18, 2021
1 parent c26e32e commit cb3bcc4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
26 changes: 16 additions & 10 deletions dapptools/src/seth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ async fn main() -> eyre::Result<()> {
let opts = Opts::from_args();
match opts.sub {
Subcommands::FromUtf8 { text } => {
println!("{}", SimpleSeth::from_utf8(&text));
let val = unwrap_or_stdin(text)?;
println!("{}", SimpleSeth::from_utf8(&val));
}
Subcommands::ToHex { decimal } => {
let val = unwrap_or_stdin(decimal)?;
println!("{}", SimpleSeth::hex(U256::from_dec_str(&val)?));
}
Subcommands::ToHexdata { input } => {
let output = match input {
let val = unwrap_or_stdin(input)?;
let output = match val {
s if s.starts_with('@') => {
let var = std::env::var(&s[1..])?;
var.as_bytes().to_hex()
Expand All @@ -45,16 +47,20 @@ async fn main() -> eyre::Result<()> {
println!("0x{}", output);
}
Subcommands::ToCheckSumAddress { address } => {
println!("{}", SimpleSeth::checksum_address(&address)?);
let val = unwrap_or_stdin(address)?;
println!("{}", SimpleSeth::checksum_address(&val)?);
}
Subcommands::ToAscii { hexdata } => {
println!("{}", SimpleSeth::ascii(&hexdata)?);
let val = unwrap_or_stdin(hexdata)?;
println!("{}", SimpleSeth::ascii(&val)?);
}
Subcommands::ToBytes32 { bytes } => {
println!("{}", SimpleSeth::bytes32(&bytes)?);
let val = unwrap_or_stdin(bytes)?;
println!("{}", SimpleSeth::bytes32(&val)?);
}
Subcommands::ToDec { hexvalue } => {
println!("{}", SimpleSeth::to_dec(&hexvalue)?);
let val = unwrap_or_stdin(hexvalue)?;
println!("{}", SimpleSeth::to_dec(&val)?);
}
Subcommands::ToFix { decimals, value } => {
let val = unwrap_or_stdin(value)?;
Expand All @@ -64,7 +70,8 @@ async fn main() -> eyre::Result<()> {
);
}
Subcommands::ToUint256 { value } => {
println!("{}", SimpleSeth::to_uint256(value)?);
let val = unwrap_or_stdin(value)?;
println!("{}", SimpleSeth::to_uint256(&val)?);
}
Subcommands::ToWei { value, unit } => {
let val = unwrap_or_stdin(value)?;
Expand Down Expand Up @@ -179,10 +186,9 @@ where
Ok(match what {
Some(what) => what,
None => {
use std::io::Read;
let mut input = std::io::stdin();
let input = std::io::stdin();
let mut what = String::new();
input.read_to_string(&mut what)?;
input.read_line(&mut what)?;
T::from_str(&what.replace("\n", ""))?
}
})
Expand Down
14 changes: 7 additions & 7 deletions dapptools/src/seth_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub enum Subcommands {
#[structopt(aliases = &["--from-ascii"])]
#[structopt(name = "--from-utf8")]
#[structopt(about = "convert text data into hexdata")]
FromUtf8 { text: String },
FromUtf8 { text: Option<String> },
#[structopt(name = "--to-hex")]
#[structopt(about = "convert a decimal number into hex")]
ToHex { decimal: Option<String> },
Expand All @@ -26,25 +26,25 @@ pub enum Subcommands {
- absolute path to file
- @tag, where $TAG is defined in environment variables
"#)]
ToHexdata { input: String },
ToHexdata { input: Option<String> },
#[structopt(name = "--to-checksum-address")]
#[structopt(about = "convert an address to a checksummed format (EIP-55)")]
ToCheckSumAddress { address: Address },
ToCheckSumAddress { address: Option<Address> },
#[structopt(name = "--to-ascii")]
#[structopt(about = "convert hex data to text data")]
ToAscii { hexdata: String },
ToAscii { hexdata: Option<String> },
#[structopt(name = "--to-bytes32")]
#[structopt(about = "left-pads a hex bytes string to 32 bytes)")]
ToBytes32 { bytes: String },
ToBytes32 { bytes: Option<String> },
#[structopt(name = "--to-dec")]
#[structopt(about = "convert hex value into decimal number")]
ToDec { hexvalue: String },
ToDec { hexvalue: Option<String> },
#[structopt(name = "--to-fix")]
#[structopt(about = "convert integers into fixed point with specified decimals")]
ToFix { decimals: Option<u128>, value: Option<String> },
#[structopt(name = "--to-uint256")]
#[structopt(about = "convert a number into uint256 hex string with 0x prefix")]
ToUint256 { value: String },
ToUint256 { value: Option<String> },
#[structopt(name = "--to-wei")]
#[structopt(about = "convert an ETH amount into wei")]
ToWei { value: Option<String>, unit: Option<String> },
Expand Down
10 changes: 5 additions & 5 deletions seth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,18 +385,18 @@ impl SimpleSeth {
/// use seth::SimpleSeth as Seth;
///
/// fn main() -> eyre::Result<()> {
/// assert_eq!(Seth::to_uint256("100".to_string())?, "0x0000000000000000000000000000000000000000000000000000000000000064");
/// assert_eq!(Seth::to_uint256("192038293923".to_string())?, "0x0000000000000000000000000000000000000000000000000000002cb65fd1a3");
/// assert_eq!(Seth::to_uint256("100")?, "0x0000000000000000000000000000000000000000000000000000000000000064");
/// assert_eq!(Seth::to_uint256("192038293923")?, "0x0000000000000000000000000000000000000000000000000000002cb65fd1a3");
/// assert_eq!(
/// Seth::to_uint256("115792089237316195423570985008687907853269984665640564039457584007913129639935".to_string())?,
/// Seth::to_uint256("115792089237316195423570985008687907853269984665640564039457584007913129639935")?,
/// "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
/// );
///
/// Ok(())
/// }
/// ```
pub fn to_uint256(value: String) -> Result<String> {
let num_u256 = U256::from_str_radix(&value, 10)?;
pub fn to_uint256(value: &str) -> Result<String> {
let num_u256 = U256::from_str_radix(value, 10)?;
let num_hex = format!("{:x}", num_u256);
Ok(format!("0x{}{}", "0".repeat(64 - num_hex.len()), num_hex))
}
Expand Down

0 comments on commit cb3bcc4

Please sign in to comment.