Add file input and optional file output

This commit is contained in:
Aadi Desai 2023-05-07 18:45:47 +01:00
parent f851cd71be
commit d13ddb1498
Signed by: supleed2
SSH key fingerprint: SHA256:CkbNRs0yVzXEiUp2zd0PSxsfRUMFF9bLlKXtE1xEbKM
3 changed files with 46 additions and 25 deletions

2
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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();
} }