begin the thing

This commit is contained in:
mcneb10 2024-07-27 02:47:01 -05:00
commit e6484a6cfa
4 changed files with 117 additions and 0 deletions

49
src/main.rs Normal file
View file

@ -0,0 +1,49 @@
use anyhow::{bail, Context, Result};
use num_bigint::BigInt;
use std::str::FromStr;
fn derive_hex_seed_from_nostr_key(key: Vec<u8>) -> Vec<u8> {
let key_num = BigInt::from_bytes_be(num_bigint::Sign::Plus, &key);
let reduction_constant = BigInt::from_str(
"7237005577332262213973186563042994240857116359379907606001950938285454250989",
)
.unwrap();
let hex_seed = key_num % reduction_constant;
let mut hex_seed_bytes = hex_seed.to_bytes_be().1;
hex_seed_bytes.resize(32, 0); // Pad with zeros to ensure the array is 32 bytes
hex_seed_bytes
}
fn extract_nostr_key_from_nsec(nsec: String) -> Result<Vec<u8>> {
let (hrp, data) = bech32::decode(&nsec).with_context(|| "bech32 decode failed")?;
if hrp.as_str() != "nsec" {
bail!("human readable part is {} when it should be nsec", hrp)
} else if data.len() != 32 {
bail!("private key has length {} when it should be 32", data.len())
} else {
Ok(data)
}
}
fn main() {
println!("Please enter a nostr nsec private key below and hit enter:");
let mut nsec = String::new();
std::io::stdin()
.read_line(&mut nsec)
.with_context(|| "Failed to read nostr private key")
.unwrap();
nsec = nsec.trim_end().to_string();
let nostr_key = extract_nostr_key_from_nsec(nsec)
.with_context(|| "Failed to parse nostr private key")
.unwrap();
let hex_seed = derive_hex_seed_from_nostr_key(nostr_key);
hex_seed.iter().for_each(|b| print!("{b:02x}"));
println!();
}