Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: seth --to-hex #7

Merged
merged 5 commits into from Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -197,6 +197,7 @@ ARGS:

* seth
* [x] `--from-ascii`
* [x] `--to-hex`
* [x] `--to-checksum-address`
* [x] `--to-bytes32`
* [x] `block`
Expand Down
6 changes: 6 additions & 0 deletions dapptools/src/seth.rs
Expand Up @@ -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: Option<u128> },
#[structopt(name = "--to-checksum-address")]
#[structopt(about = "convert an address to a checksummed format (EIP-55)")]
ToCheckSumAddress { address: Address },
Expand Down Expand Up @@ -131,6 +134,9 @@ async fn main() -> eyre::Result<()> {
Subcommands::FromAscii { text } => {
println!("{}", SimpleSeth::from_ascii(&text));
}
Subcommands::ToHex { decimal } => {
println!("{}", SimpleSeth::to_hex(unwrap_or_stdin(decimal)?));
}
Subcommands::ToCheckSumAddress { address } => {
println!("{}", SimpleSeth::to_checksum_address(&address)?);
}
Expand Down
11 changes: 11 additions & 0 deletions seth/src/lib.rs
Expand Up @@ -211,6 +211,17 @@ impl SimpleSeth {
let s: String = s.as_bytes().to_hex();
format!("0x{}", s)
}
/// Converts decimal input to hex
///
/// ```
/// use seth::SimpleSeth as Seth;
///
/// assert_eq!(Seth::to_hex(424242), "0x67932");
/// assert_eq!(Seth::to_hex(1234), "0x4d2");
/// ```
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)
Expand Down