Skip to content

Commit

Permalink
Seth: keccak
Browse files Browse the repository at this point in the history
  • Loading branch information
Anish-Agnihotri committed Sep 20, 2021
1 parent 734ae41 commit a2b43af
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dapptools/src/seth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ async fn main() -> eyre::Result<()> {
let provider = Provider::try_from(rpc_url)?;
println!("{}", Seth::new(provider).base_fee(block.unwrap_or(BlockId::Number(Latest))).await?);
}

Subcommands::Keccak { data } => {
println!("{}", SimpleSeth::keccak(&data));
}
Subcommands::ResolveName {
who,
rpc_url,
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 @@ -104,6 +104,9 @@ pub enum Subcommands {
#[structopt(short, long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "keccak")]
#[structopt(about = "Keccak-256 hash arbitrary data")]
Keccak { data: String },
#[structopt(name = "resolve-name")]
#[structopt(about = "Returns the address the provided ENS name resolves to")]
ResolveName {
Expand Down
13 changes: 13 additions & 0 deletions seth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@ impl SimpleSeth {
Ok(format!("{:?}", H256::from_str(&padded)?))
}

/// Keccak-256 hashes arbitrary data
///
/// ```
/// use seth::SimpleSeth as Seth;
///
/// assert_eq!(Seth::keccak("foo"), "0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d");
/// assert_eq!(Seth::keccak("123abc"), "0xb1f1c74a1ba56f07a892ea1110a39349d40f66ca01d245e704621033cb7046a4");
/// ```
pub fn keccak(d: &str) -> String {
let hash: String = keccak256(d.as_bytes()).to_hex();
format!("0x{}", hash)
}

/// Converts ENS names to their namehash representation
/// [Namehash reference](https://docs.ens.domains/contract-api-reference/name-processing#hashing-names)
/// [namehash-rust reference](https://github.com/InstateDev/namehash-rust/blob/master/src/lib.rs)
Expand Down

0 comments on commit a2b43af

Please sign in to comment.