From 55ff62052b153f53ece50d58b7b3e5090005f66d Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 2 Oct 2023 21:55:04 -0500 Subject: [PATCH] keyfork-derive-key: slim dependencies and formats --- Cargo.lock | 9 +------ keyfork-derive-key/Cargo.toml | 11 +------- keyfork-derive-key/src/cli.rs | 23 ----------------- keyfork-derive-key/src/lib.rs | 15 ----------- keyfork-derive-key/src/main.rs | 46 +++++++++++++++++++++++++++++----- 5 files changed, 42 insertions(+), 62 deletions(-) delete mode 100644 keyfork-derive-key/src/cli.rs delete mode 100644 keyfork-derive-key/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 547846c..2648aec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -876,17 +876,10 @@ dependencies = [ name = "keyfork-derive-key" version = "0.1.0" dependencies = [ - "bincode", - "clap", - "ed25519-dalek", "keyfork-derive-util", - "keyfork-frame", - "keyfork-slip10-test-data", - "keyforkd", "keyforkd-client", - "tempdir", + "smex", "thiserror", - "tokio", ] [[package]] diff --git a/keyfork-derive-key/Cargo.toml b/keyfork-derive-key/Cargo.toml index e43af99..2c90699 100644 --- a/keyfork-derive-key/Cargo.toml +++ b/keyfork-derive-key/Cargo.toml @@ -6,16 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bincode = { version = "1.3.3", default-features = false } -clap = { version = "4.4.2", default-features = false, features = ["std", "usage", "help", "derive"] } keyfork-derive-util = { version = "0.1.0", path = "../keyfork-derive-util" } -keyfork-frame = { version = "0.1.0", path = "../keyfork-frame", default-features = false } keyforkd-client = { version = "0.1.0", path = "../keyforkd-client" } +smex = { version = "0.1.0", path = "../smex" } thiserror = "1.0.48" - -[dev-dependencies] -ed25519-dalek = "2.0.0" -keyfork-slip10-test-data = { path = "../keyfork-slip10-test-data" } -keyforkd = { path = "../keyforkd", default-features = false } -tempdir = "0.3.7" -tokio = { version = "1.32.0", features = ["rt", "rt-multi-thread"] } diff --git a/keyfork-derive-key/src/cli.rs b/keyfork-derive-key/src/cli.rs deleted file mode 100644 index 720c1f6..0000000 --- a/keyfork-derive-key/src/cli.rs +++ /dev/null @@ -1,23 +0,0 @@ -use clap::Parser; -use keyfork_derive_util::{ - request::{DerivationAlgorithm, DerivationRequest, DerivationResponse}, - DerivationPath, -}; -use keyforkd_client::{Client, Result}; - -#[derive(Parser, Clone, Debug)] -pub struct Command { - #[arg(long)] - pub path: DerivationPath, - - #[arg(long)] - pub algorithm: DerivationAlgorithm, -} - -impl Command { - pub fn handle(&self) -> Result { - let mut client = Client::discover_socket()?; - let request = DerivationRequest::new(self.algorithm.clone(), &self.path); - client.request(&request) - } -} diff --git a/keyfork-derive-key/src/lib.rs b/keyfork-derive-key/src/lib.rs deleted file mode 100644 index d2af920..0000000 --- a/keyfork-derive-key/src/lib.rs +++ /dev/null @@ -1,15 +0,0 @@ -pub mod cli; - -#[derive(Debug, thiserror::Error)] -pub enum Error { - #[error("The first argument to the command should be a derivation path")] - Args, - - #[error("The given path was incorrectly formatted: {0}")] - ArgsFormat(#[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; diff --git a/keyfork-derive-key/src/main.rs b/keyfork-derive-key/src/main.rs index 2cbae4f..ecdab9b 100644 --- a/keyfork-derive-key/src/main.rs +++ b/keyfork-derive-key/src/main.rs @@ -1,10 +1,44 @@ -use clap::Parser; +use std::{env, str::FromStr}; -#[allow(clippy::wildcard_imports)] -use keyfork_derive_key::*; +use keyfork_derive_util::{ + request::{DerivationAlgorithm, DerivationError, DerivationRequest}, + DerivationPath, +}; +use keyforkd_client::Client; -fn main() -> Result<()> { - let args = cli::Command::parse(); - args.handle()?; +#[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 main() -> 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(()) }