Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ zbase32 = "0.1.2"
ctrlc = "3.4.4"
reqwest = { version="0.12.12", default-features = false, features = ["json", "rustls-tls", "http2"]}
rand = {version = "0.8"}
hex = "0.4.3"

[dev-dependencies]
tempfile = "3.20.0"
10 changes: 5 additions & 5 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use crate::commands::{cli_publickey, generate::cli_generate_seed, publish::cli_p
pub async fn run_cli() {
const VERSION: &str = env!("CARGO_PKG_VERSION");

let cmd = clap::Command::new("pkarr-cli")
let cmd = clap::Command::new("pkdns-cli")
.version(VERSION)
.arg_required_else_help(true)
.subcommand(
clap::Command::new("publish")
.about("Publish pkarr dns records.")
.arg(
clap::Arg::new("seed")
.help("File path to the pkarr seed file.")
.help("File path to the seed file.")
.default_value("./seed.txt"),
)
.arg(
Expand All @@ -25,11 +25,11 @@ pub async fn run_cli() {
)
.subcommand(
clap::Command::new("resolve")
.about("Resolve pkarr dns records.")
.about("Resolve a public key domain on the DHT.")
.arg_required_else_help(true)
.arg(clap::Arg::new("pubkey").required(false).help("Pkarr public key uri.")),
.arg(clap::Arg::new("pubkey").required(false).help("Public Key Domain")),
)
.subcommand(clap::Command::new("generate").about("Generate a new zbase32 pkarr seed"))
.subcommand(clap::Command::new("generate").about("Generate a new seed"))
.subcommand(
clap::Command::new("publickey")
.about("Derive the public key from the seed.")
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ use pkarr::Keypair;

pub async fn cli_generate_seed(_matches: &ArgMatches) {
let keypair = Keypair::random();
let encoded = zbase32::encode_full_bytes(&keypair.secret_key());
let encoded = hex::encode(keypair.secret_key());
println!("{encoded}");
}
45 changes: 35 additions & 10 deletions cli/src/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,47 @@ fn read_seed_file(seed_file_path: &str) -> Keypair {
std::process::exit(1);
};
let seed = seed.unwrap();
parse_seed(&seed)
match parse_seed(&seed) {
Ok(keypair) => keypair,
Err(e) => {
eprintln!("Failed to parse the seed file. {e} {seed}");
std::process::exit(1);
}
}
}

fn parse_seed(seed: &str) -> Keypair {
/// Parse a seed file into a keypair.
/// Tries to parse as hex first, then as zbase32.
/// Errors if the seed is not valid.
fn parse_seed(seed: &str) -> anyhow::Result<Keypair> {
let seed = seed.trim();
let decode_result = zbase32::decode_full_bytes_str(seed);
if let Err(e) = decode_result {
eprintln!("Failed to parse the seed file. {e} {seed}");
std::process::exit(1);
};

let plain_secret = decode_result.unwrap();
if seed.len() == 52 {
return parse_seed_zbase32(seed);
}

let slice: &[u8; SECRET_KEY_LENGTH] = &plain_secret[0..SECRET_KEY_LENGTH].try_into().unwrap();
parse_seed_hex(seed)
}

Keypair::from_secret_key(slice)
/// Parse a hex seed into a keypair.
/// The seed is expected to be 64 characters long.
/// This is the new format of the seed.
fn parse_seed_hex(seed: &str) -> anyhow::Result<Keypair> {
let decode_result = hex::decode(seed)?;
let slice: &[u8; SECRET_KEY_LENGTH] = &decode_result[0..SECRET_KEY_LENGTH].try_into()?;
Ok(Keypair::from_secret_key(slice))
}

/// Parse a zbase32 seed into a keypair.
/// The seed is expected to be 52 characters long.
/// This is the old format of the seed.
fn parse_seed_zbase32(seed: &str) -> anyhow::Result<Keypair> {
let decode_result = match zbase32::decode_full_bytes_str(seed) {
Ok(bytes) => bytes,
Err(_) => return Err(anyhow!("Invalid zbase32 seed")),
};
let slice: &[u8; SECRET_KEY_LENGTH] = &decode_result[0..SECRET_KEY_LENGTH].try_into()?;
Ok(Keypair::from_secret_key(slice))
}

pub async fn cli_publish(matches: &ArgMatches) {
Expand Down
Loading