Skip to content

Commit

Permalink
Seth: namehash
Browse files Browse the repository at this point in the history
  • Loading branch information
Anish-Agnihotri committed Sep 20, 2021
1 parent 91000f7 commit 734ae41
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions dapptools/src/seth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ async fn main() -> eyre::Result<()> {
let provider = Provider::try_from(rpc_url)?;
println!("{}", Seth::new(provider).chain_id().await?);
}
Subcommands::Namehash { name } => {
println!("{}", SimpleSeth::namehash(&name));
}
Subcommands::SendTx { eth, to, sig, args } => {
let provider = Provider::try_from(eth.rpc_url.as_str())?;
if let Some(signer) = eth.signer()? {
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 @@ -63,6 +63,9 @@ pub enum Subcommands {
#[structopt(long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "namehash")]
#[structopt(about = "returns ENS namehash of provided name")]
Namehash { name: String },
#[structopt(name = "send")]
#[structopt(about = "Publish a transaction signed by <from> to call <to> with <data>")]
SendTx {
Expand Down
35 changes: 34 additions & 1 deletion seth/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Seth
//!
//! TODO
use ethers_core::{types::*, utils};
use ethers_core::{types::*, utils::{self, keccak256}};
use ethers_providers::{Middleware, PendingTransaction};
use eyre::Result;
use rustc_hex::ToHex;
Expand Down Expand Up @@ -343,6 +343,39 @@ impl SimpleSeth {
// need to use the Debug implementation
Ok(format!("{:?}", H256::from_str(&padded)?))
}

/// 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)
///
/// ```
/// use seth::SimpleSeth as Seth;
///
/// assert_eq!(Seth::namehash(""), "0x0000000000000000000000000000000000000000000000000000000000000000");
/// assert_eq!(Seth::namehash("eth"), "0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae");
/// assert_eq!(Seth::namehash("foo.eth"), "0xde9b09fd7c5f901e23a3f19fecc54828e9c848539801e86591bd9801b019f84f");
/// assert_eq!(Seth::namehash("sub.foo.eth"), "0x500d86f9e663479e5aaa6e99276e55fc139c597211ee47d17e1e92da16a83402");
/// ```
pub fn namehash(ens: &str) -> String {
let mut node = vec![0u8; 32];

if !ens.is_empty() {
let ens_lower = ens.to_lowercase();
let mut labels: Vec<&str> = ens_lower.split(".").collect();
labels.reverse();

for label in labels {
let mut label_hash = keccak256(label.as_bytes());
node.append(&mut label_hash.to_vec());

label_hash = keccak256(node.as_slice());
node = label_hash.to_vec();
}
}

let namehash: String = node.to_hex();
format!("0x{}", namehash)
}
}

fn strip_0x(s: &str) -> &str {
Expand Down

0 comments on commit 734ae41

Please sign in to comment.