keyfork/keyfork-derive-key/src/main.rs

45 lines
1.4 KiB
Rust
Raw Normal View History

use std::{env, str::FromStr};
use keyfork_derive_util::{
request::{DerivationAlgorithm, DerivationError, DerivationRequest},
DerivationPath,
};
use keyforkd_client::Client;
2023-09-12 04:00:30 +00:00
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Could not parse the given algorithm {0:?}: {1}")]
AlgoFormat(String, DerivationError),
#[error("Could not parse the given path: {0}")]
PathFormat(#[from] keyfork_derive_util::path::Error),
#[error("Unable to perform key derivation request: {0}")]
KeyforkdClient(#[from] keyforkd_client::Error),
}
pub type Result<T, E = Error> = std::result::Result<T, E>;
fn validate(algo: &str, path: &str) -> Result<(DerivationAlgorithm, DerivationPath)> {
let algo =
DerivationAlgorithm::from_str(algo).map_err(|e| Error::AlgoFormat(algo.to_string(), e))?;
let path = DerivationPath::from_str(path)?;
Ok((algo, path))
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args = env::args();
let program_name = args.next().expect("program name");
let args = args.collect::<Vec<_>>();
let (algo, path) = match args.as_slice() {
[algo, path] => validate(algo, path)?,
_ => panic!("Usage: {program_name} algorithm path"),
};
let mut client = Client::discover_socket()?;
let request = DerivationRequest::new(algo, &path);
let response = client.request(&request)?;
println!("{}", smex::encode(&response.data));
Ok(())
}