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

54 lines
1.7 KiB
Rust
Raw Normal View History

2023-10-19 13:53:59 +00:00
use std::{env, process::ExitCode, str::FromStr};
use keyfork_derive_util::{
request::{DerivationAlgorithm, DerivationError, DerivationRequest, DerivationResponse},
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))
}
2023-10-19 13:53:59 +00:00
fn run() -> 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.into())?;
println!("{}", smex::encode(&DerivationResponse::try_from(response)?.data));
Ok(())
}
2023-10-19 13:53:59 +00:00
fn main() -> ExitCode {
if let Err(e) = run() {
eprintln!("Error: {e}");
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
}
}