use std::{env, process::ExitCode, str::FromStr}; use keyfork_derive_util::{ request::{DerivationAlgorithm, DerivationError, DerivationRequest}, DerivationPath, }; use keyforkd_client::Client; #[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 = std::result::Result; 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 run() -> Result<(), Box> { let mut args = env::args(); let program_name = args.next().expect("program name"); let args = args.collect::>(); 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(()) } fn main() -> ExitCode { if let Err(e) = run() { eprintln!("Error: {e}"); ExitCode::FAILURE } else { ExitCode::SUCCESS } }