mirror of
https://github.com/supleed2/namehash-rs.git
synced 2024-12-22 14:15:50 +00:00
Add file input and optional file output
This commit is contained in:
parent
f851cd71be
commit
d13ddb1498
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -193,7 +193,7 @@ checksum = "ece97ea872ece730aed82664c424eb4c8291e1ff2480247ccf7409044bc6479f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "namehash"
|
name = "namehash"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"hex",
|
"hex",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "namehash"
|
name = "namehash"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "CLI program for hashing domain names according to EIP-137"
|
description = "CLI program for hashing domain names according to EIP-137"
|
||||||
homepage = "https://github.com/supleed2/namehash-rs"
|
homepage = "https://github.com/supleed2/namehash-rs"
|
||||||
|
|
67
src/main.rs
67
src/main.rs
|
@ -1,4 +1,4 @@
|
||||||
// use std::path::PathBuf;
|
use std::io::prelude::*;
|
||||||
|
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
|
||||||
|
@ -15,30 +15,51 @@ enum Commands {
|
||||||
/// Domain to get namehash of
|
/// Domain to get namehash of
|
||||||
domain: String,
|
domain: String,
|
||||||
},
|
},
|
||||||
// /// Get the namehashes of many domains at once, THIS COMMAND IS INCOMPLETE
|
/// Get the namehashes of many domains at once
|
||||||
// File {
|
File {
|
||||||
// /// Path to input file, domains to hash with 1 per line
|
/// Path to input file, domains to hash with 1 per line
|
||||||
// input: PathBuf,
|
input: std::path::PathBuf,
|
||||||
// /// File to save hashes to, stdout if not given
|
/// File to save hashes to, stdout if not given
|
||||||
// #[arg(short, long, value_name = "FILE")]
|
#[arg(short, long, value_name = "FILE")]
|
||||||
// output: Option<PathBuf>,
|
output: Option<std::path::PathBuf>,
|
||||||
// },
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> std::io::Result<()> {
|
||||||
match Cli::parse().command {
|
match Cli::parse().command {
|
||||||
Commands::Domain { domain } => println!("{domain}: 0x{}", hex::encode(namehash(&domain))),
|
Commands::Domain { domain } => {
|
||||||
// Commands::File { input, output } => match output {
|
println!("{domain}: 0x{}", hex::encode(namehash(&domain)));
|
||||||
// None => println!(
|
Ok(())
|
||||||
// "Hashing domains in {}, output to stdout, THIS COMMAND IS INCOMPLETE",
|
}
|
||||||
// input.display()
|
Commands::File { input, output } => match output {
|
||||||
// ),
|
None => {
|
||||||
// Some(output) => println!(
|
let input = std::fs::File::open(input)?;
|
||||||
// "Hashing domains in {}, output to {}, THIS COMMAND IS INCOMPLETE",
|
for line in std::io::BufReader::new(input).lines() {
|
||||||
// input.display(),
|
match line {
|
||||||
// output.display()
|
Ok(domain) => {
|
||||||
// ),
|
println!("{domain}: 0x{}", hex::encode(namehash(&domain)))
|
||||||
// },
|
}
|
||||||
|
Err(error) => eprintln!("Error: {error}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Some(output) => {
|
||||||
|
let input = std::fs::File::open(input)?;
|
||||||
|
let mut outstr = String::new();
|
||||||
|
for line in std::io::BufReader::new(input).lines() {
|
||||||
|
match line {
|
||||||
|
Ok(domain) => outstr
|
||||||
|
.push_str(&format!("{domain}: 0x{}\n", hex::encode(namehash(&domain)))),
|
||||||
|
Err(error) => eprintln!("Error: {error}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(folder) = std::path::Path::new(&output).parent() {
|
||||||
|
std::fs::create_dir_all(folder).unwrap();
|
||||||
|
}
|
||||||
|
write!(std::fs::File::create(output)?, "{}", outstr)
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +77,7 @@ fn namehash(name: &str) -> Vec<u8> {
|
||||||
return vec![0u8; 32];
|
return vec![0u8; 32];
|
||||||
}
|
}
|
||||||
let mut hash = vec![0u8; 32];
|
let mut hash = vec![0u8; 32];
|
||||||
for label in name.rsplit(".") {
|
for label in name.rsplit('.') {
|
||||||
hash.append(&mut keccak256(label.as_bytes()).to_vec());
|
hash.append(&mut keccak256(label.as_bytes()).to_vec());
|
||||||
hash = keccak256(hash.as_slice()).to_vec();
|
hash = keccak256(hash.as_slice()).to_vec();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue