Skip to content

Commit

Permalink
Seth: --to-ascii
Browse files Browse the repository at this point in the history
  • Loading branch information
Anish-Agnihotri committed Sep 20, 2021
1 parent 4364e44 commit fb310df
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
5 changes: 4 additions & 1 deletion dapptools/src/seth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ async fn main() -> eyre::Result<()> {
Subcommands::ToCheckSumAddress { address } => {
println!("{}", SimpleSeth::to_checksum_address(&address)?);
}
Subcommands::ToAscii { hexdata } => {
println!("{}", SimpleSeth::to_ascii(&hexdata));
}
Subcommands::ToBytes32 { bytes } => {
println!("{}", SimpleSeth::to_bytes32(&bytes)?);
}
Subcommands::ToDec { hexvalue } => {
println!("{}", SimpleSeth::to_dec(hexvalue));
println!("{}", SimpleSeth::to_dec(&hexvalue));
}
Subcommands::ToFix { decimals, value } => {
println!("{}", SimpleSeth::to_fix(unwrap_or_stdin(decimals)?, unwrap_or_stdin(value)?));
Expand Down
3 changes: 3 additions & 0 deletions dapptools/src/seth_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub enum Subcommands {
#[structopt(name = "--to-checksum-address")]
#[structopt(about = "convert an address to a checksummed format (EIP-55)")]
ToCheckSumAddress { address: Address },
#[structopt(name = "--to-ascii")]
#[structopt(about = "convert hex data to text data")]
ToAscii { hexdata: String },
#[structopt(name = "--to-bytes32")]
#[structopt(about = "left-pads a hex bytes string to 32 bytes)")]
ToBytes32 { bytes: String },
Expand Down
23 changes: 21 additions & 2 deletions seth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use ethers_core::{types::*, utils::{self, keccak256}};
use ethers_providers::{Middleware, PendingTransaction};
use eyre::Result;
use rustc_hex::ToHex;
use rustc_hex::{ToHex, FromHexIter};
use std::str::FromStr;
use chrono::NaiveDateTime;

Expand Down Expand Up @@ -303,6 +303,25 @@ impl SimpleSeth {
format!("0x{}", s)
}


/// Converts hex data into text data
///
/// ```
/// use seth::SimpleSeth as Seth;
///
/// assert_eq!("Hello, World!", Seth::to_ascii("48656c6c6f2c20576f726c6421".to_string()));
/// assert_eq!("TurboDappTools", Seth::to_ascii("0x547572626f44617070546f6f6c73".to_string()));
/// ```
pub fn to_ascii(hex: &str) -> String {
let hex_trimmed = hex.trim_start_matches("0x");
let iter = FromHexIter::new(hex_trimmed);
let mut ascii = String::new();
for letter in iter.collect::<Vec<_>>() {
ascii.push(letter.unwrap() as char);
}
ascii
}

/// Converts hex input to decimal
///
/// ```
Expand All @@ -311,7 +330,7 @@ impl SimpleSeth {
/// assert_eq!(424242, Seth::to_dec("0x67932"));
/// assert_eq!(1234, Seth::to_dec("0x4d2"));
/// ```
pub fn to_dec(hex: String) -> u128 {
pub fn to_dec(hex: &str) -> u128 {
let hex_trimmed = hex.trim_start_matches("0x");
u128::from_str_radix(&hex_trimmed, 16).expect("Could not parse hex")
}
Expand Down

0 comments on commit fb310df

Please sign in to comment.