From 4015c73cae9e31c52217e1b86c5e5b5ac63adc4c Mon Sep 17 00:00:00 2001 From: Odysseas Lamtzidis Date: Mon, 13 Sep 2021 18:38:54 +0300 Subject: [PATCH 1/5] feat: seth --to-hex --- dapptools/src/seth.rs | 6 ++++++ seth/src/lib.rs | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/dapptools/src/seth.rs b/dapptools/src/seth.rs index 846771f0bd9a..a4fe4629626f 100644 --- a/dapptools/src/seth.rs +++ b/dapptools/src/seth.rs @@ -18,6 +18,9 @@ pub enum Subcommands { #[structopt(name = "--from-ascii")] #[structopt(about = "convert text data into hexdata")] FromAscii { text: String }, + #[structopt(name = "--to-hex")] + #[structopt(about = "convert a decimal number into hex")] + ToHex { decimal: u128 }, #[structopt(name = "--to-checksum-address")] #[structopt(about = "convert an address to a checksummed format (EIP-55)")] ToCheckSumAddress { address: Address }, @@ -131,6 +134,9 @@ async fn main() -> eyre::Result<()> { Subcommands::FromAscii { text } => { println!("{}", SimpleSeth::from_ascii(&text)); } + Subcommands::ToHex { decimal } => { + println!("{}", SimpleSeth::to_hex(decimal)); + } Subcommands::ToCheckSumAddress { address } => { println!("{}", SimpleSeth::to_checksum_address(&address)?); } diff --git a/seth/src/lib.rs b/seth/src/lib.rs index a4e77a7141ca..27952ca1a81b 100644 --- a/seth/src/lib.rs +++ b/seth/src/lib.rs @@ -211,6 +211,18 @@ impl SimpleSeth { let s: String = s.as_bytes().to_hex(); format!("0x{}", s) } + /// Converts decimal input to hex + /// + /// ``` + /// use seth::SimpleSeth as Seth; + /// + /// let bin = Seth::to_hex(424242); + /// assert_eq!(bin, "0x67932") + /// ``` + + pub fn to_hex(u: u128) -> String { + format!("{:#X}", u) + } /// Converts an Ethereum address to its checksum format /// according to [EIP-55](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md) From c022b1e7f0ad6547cd1227851bb48805639de88f Mon Sep 17 00:00:00 2001 From: Odysseas Lamtzidis Date: Mon, 13 Sep 2021 19:22:36 +0300 Subject: [PATCH 2/5] update readme with seth --to-hex --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f4d0d24b2dd7..e61c326d0214 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,7 @@ ARGS: * seth * [x] `--from-ascii` + * [x] `--to-hex` * [x] `--to-checksum-address` * [x] `--to-bytes32` * [x] `block` From d329007185a8d4dceb57d58d870667e2ff1b5aee Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Mon, 13 Sep 2021 20:58:10 +0300 Subject: [PATCH 3/5] fix(seth/to_hex): always output as lowercase --- seth/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/seth/src/lib.rs b/seth/src/lib.rs index 27952ca1a81b..969dbb38355f 100644 --- a/seth/src/lib.rs +++ b/seth/src/lib.rs @@ -216,12 +216,12 @@ impl SimpleSeth { /// ``` /// use seth::SimpleSeth as Seth; /// - /// let bin = Seth::to_hex(424242); - /// assert_eq!(bin, "0x67932") + /// assert_eq!(Seth::to_hex(424242), "0x67932"); + /// assert_eq!(Seth::to_hex(1234), "0x4d2"); /// ``` pub fn to_hex(u: u128) -> String { - format!("{:#X}", u) + format!("{:#x}", u) } /// Converts an Ethereum address to its checksum format From 8d0f7e34475335498392e343a3c8442b3cd16e65 Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Mon, 13 Sep 2021 20:58:28 +0300 Subject: [PATCH 4/5] feat: to_hex accepts from stdin --- dapptools/src/seth.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dapptools/src/seth.rs b/dapptools/src/seth.rs index a4fe4629626f..63e88a6e11d3 100644 --- a/dapptools/src/seth.rs +++ b/dapptools/src/seth.rs @@ -20,7 +20,7 @@ pub enum Subcommands { FromAscii { text: String }, #[structopt(name = "--to-hex")] #[structopt(about = "convert a decimal number into hex")] - ToHex { decimal: u128 }, + ToHex { decimal: Option }, #[structopt(name = "--to-checksum-address")] #[structopt(about = "convert an address to a checksummed format (EIP-55)")] ToCheckSumAddress { address: Address }, @@ -135,7 +135,7 @@ async fn main() -> eyre::Result<()> { println!("{}", SimpleSeth::from_ascii(&text)); } Subcommands::ToHex { decimal } => { - println!("{}", SimpleSeth::to_hex(decimal)); + println!("{}", SimpleSeth::to_hex(unwrap_or_stdin(decimal)?)); } Subcommands::ToCheckSumAddress { address } => { println!("{}", SimpleSeth::to_checksum_address(&address)?); From f13ae158de7f4bb9968c92a9ebbe62e7163a9233 Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Mon, 13 Sep 2021 21:03:40 +0300 Subject: [PATCH 5/5] chore: remove whitespace --- seth/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/seth/src/lib.rs b/seth/src/lib.rs index 969dbb38355f..fed3ffd42399 100644 --- a/seth/src/lib.rs +++ b/seth/src/lib.rs @@ -219,7 +219,6 @@ impl SimpleSeth { /// assert_eq!(Seth::to_hex(424242), "0x67932"); /// assert_eq!(Seth::to_hex(1234), "0x4d2"); /// ``` - pub fn to_hex(u: u128) -> String { format!("{:#x}", u) }