From 1b6241f92b7e22a5430e29d03c6cf8ffea735d55 Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 27 Sep 2023 21:30:56 -0500 Subject: [PATCH] keyforkd-client: extract from keyfork-derive-key --- Cargo.lock | 23 +++++- Cargo.toml | 1 + keyfork-derive-key/Cargo.toml | 1 + keyfork-derive-key/src/cli.rs | 9 ++- keyfork-derive-key/src/client.rs | 25 ------- keyfork-derive-key/src/lib.rs | 26 +------ keyfork-derive-key/src/main.rs | 3 - keyfork-derive-key/src/socket.rs | 25 ------- keyforkd-client/Cargo.toml | 18 +++++ keyforkd-client/src/lib.rs | 73 +++++++++++++++++++ .../src/tests.rs | 3 +- 11 files changed, 122 insertions(+), 85 deletions(-) delete mode 100644 keyfork-derive-key/src/client.rs delete mode 100644 keyfork-derive-key/src/socket.rs create mode 100644 keyforkd-client/Cargo.toml create mode 100644 keyforkd-client/src/lib.rs rename {keyfork-derive-key => keyforkd-client}/src/tests.rs (97%) diff --git a/Cargo.lock b/Cargo.lock index 547536a..23a30e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -533,6 +533,7 @@ dependencies = [ "keyfork-frame", "keyfork-slip10-test-data", "keyforkd", + "keyforkd-client", "tempdir", "thiserror", "tokio", @@ -617,6 +618,20 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "keyforkd-client" +version = "0.1.0" +dependencies = [ + "bincode", + "keyfork-derive-util", + "keyfork-frame", + "keyfork-slip10-test-data", + "keyforkd", + "tempdir", + "thiserror", + "tokio", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -1072,18 +1087,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.48" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d6d7a740b8a666a7e828dd00da9c0dc290dff53154ea77ac109281de90589b7" +checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.48" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35" +checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index a87f22d..274e5ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,5 +11,6 @@ members = [ "keyfork-mnemonic-util", "keyfork-slip10-test-data", "keyforkd", + "keyforkd-client", "smex", ] diff --git a/keyfork-derive-key/Cargo.toml b/keyfork-derive-key/Cargo.toml index 6ffd8cd..e43af99 100644 --- a/keyfork-derive-key/Cargo.toml +++ b/keyfork-derive-key/Cargo.toml @@ -10,6 +10,7 @@ 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" } thiserror = "1.0.48" [dev-dependencies] diff --git a/keyfork-derive-key/src/cli.rs b/keyfork-derive-key/src/cli.rs index 0382860..720c1f6 100644 --- a/keyfork-derive-key/src/cli.rs +++ b/keyfork-derive-key/src/cli.rs @@ -1,6 +1,9 @@ -use crate::client::Client; use clap::Parser; -use keyfork_derive_util::{request::{DerivationAlgorithm, DerivationRequest, DerivationResponse}, DerivationPath}; +use keyfork_derive_util::{ + request::{DerivationAlgorithm, DerivationRequest, DerivationResponse}, + DerivationPath, +}; +use keyforkd_client::{Client, Result}; #[derive(Parser, Clone, Debug)] pub struct Command { @@ -12,7 +15,7 @@ pub struct Command { } impl Command { - pub fn handle(&self) -> super::Result { + 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/client.rs b/keyfork-derive-key/src/client.rs deleted file mode 100644 index 298e8f7..0000000 --- a/keyfork-derive-key/src/client.rs +++ /dev/null @@ -1,25 +0,0 @@ -use crate::Result; -use keyfork_derive_util::request::{DerivationRequest, DerivationResponse}; -use keyfork_frame::{try_decode_from, try_encode_to}; -use std::os::unix::net::UnixStream; - -#[derive(Debug)] -pub struct Client { - socket: UnixStream, -} - -impl Client { - pub fn new(socket: UnixStream) -> Self { - Self { socket } - } - - pub fn discover_socket() -> Result { - super::socket::get_socket().map(|socket| Self { socket }) - } - - pub fn request(&mut self, req: &DerivationRequest) -> Result { - try_encode_to(&bincode::serialize(&req)?, &mut self.socket)?; - let resp = try_decode_from(&mut self.socket)?; - bincode::deserialize(&resp).map_err(From::from) - } -} diff --git a/keyfork-derive-key/src/lib.rs b/keyfork-derive-key/src/lib.rs index 39f9be4..d2af920 100644 --- a/keyfork-derive-key/src/lib.rs +++ b/keyfork-derive-key/src/lib.rs @@ -1,11 +1,4 @@ -use keyfork_frame::{DecodeError, EncodeError}; -use std::path::PathBuf; - pub mod cli; -pub mod client; -pub mod socket; - -pub use client::Client; #[derive(Debug, thiserror::Error)] pub enum Error { @@ -15,23 +8,8 @@ pub enum Error { #[error("The given path was incorrectly formatted: {0}")] ArgsFormat(#[from] keyfork_derive_util::path::Error), - #[error("Neither KEYFORK_SOCKET_PATH nor XDG_RUNTIME_DIR were set")] - EnvVarsNotFound, - - #[error("Socket was unable to connect to {1}: {0}")] - Connect(std::io::Error, PathBuf), - - #[error("Could not write to or from the socket: {0}")] - Io(#[from] std::io::Error), - - #[error("Could not perform bincode transformation: {0}")] - Bincode(#[from] Box), - - #[error("Could not perform frame transformation: {0}")] - FrameEnc(#[from] EncodeError), - - #[error("Could not perform frame transformation: {0}")] - FrameDec(#[from] DecodeError), + #[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 9ba8862..2cbae4f 100644 --- a/keyfork-derive-key/src/main.rs +++ b/keyfork-derive-key/src/main.rs @@ -1,8 +1,5 @@ use clap::Parser; -#[cfg(test)] -mod tests; - #[allow(clippy::wildcard_imports)] use keyfork_derive_key::*; diff --git a/keyfork-derive-key/src/socket.rs b/keyfork-derive-key/src/socket.rs deleted file mode 100644 index ae9ca17..0000000 --- a/keyfork-derive-key/src/socket.rs +++ /dev/null @@ -1,25 +0,0 @@ -use super::Error; -use std::{collections::HashMap, os::unix::net::UnixStream, path::PathBuf}; - -#[allow(clippy::module_name_repetitions)] -pub fn get_socket() -> Result { - let socket_vars = std::env::vars() - .filter(|(key, _)| ["XDG_RUNTIME_DIR", "KEYFORKD_SOCKET_PATH"].contains(&key.as_str())) - .collect::>(); - let mut socket_path: PathBuf; - #[allow(clippy::single_match_else)] - match socket_vars.get("KEYFORKD_SOCKET_PATH") { - Some(occupied) => { - socket_path = PathBuf::from(occupied); - } - None => { - socket_path = PathBuf::from( - socket_vars - .get("XDG_RUNTIME_DIR") - .ok_or(Error::EnvVarsNotFound)?, - ); - socket_path.extend(["keyforkd", "keyforkd.sock"]); - } - } - UnixStream::connect(&socket_path).map_err(|e| Error::Connect(e, socket_path)) -} diff --git a/keyforkd-client/Cargo.toml b/keyforkd-client/Cargo.toml new file mode 100644 index 0000000..260fe0e --- /dev/null +++ b/keyforkd-client/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "keyforkd-client" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bincode = "1.3.3" +keyfork-derive-util = { version = "0.1.0", path = "../keyfork-derive-util" } +keyfork-frame = { version = "0.1.0", path = "../keyfork-frame" } +thiserror = "1.0.49" + +[dev-dependencies] +keyfork-slip10-test-data = { path = "../keyfork-slip10-test-data" } +keyforkd = { path = "../keyforkd" } +tempdir = "0.3.7" +tokio = { version = "1.32.0", features = ["rt", "sync", "rt-multi-thread"] } diff --git a/keyforkd-client/src/lib.rs b/keyforkd-client/src/lib.rs new file mode 100644 index 0000000..7dcae33 --- /dev/null +++ b/keyforkd-client/src/lib.rs @@ -0,0 +1,73 @@ +use std::{collections::HashMap, os::unix::net::UnixStream, path::PathBuf}; + +use keyfork_derive_util::request::{DerivationRequest, DerivationResponse}; +use keyfork_frame::{try_decode_from, try_encode_to, DecodeError, EncodeError}; + +#[cfg(test)] +mod tests; + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("Neither KEYFORK_SOCKET_PATH nor XDG_RUNTIME_DIR were set")] + EnvVarsNotFound, + + #[error("Socket was unable to connect to {1}: {0}")] + Connect(std::io::Error, PathBuf), + + #[error("Could not write to or from the socket: {0}")] + Io(#[from] std::io::Error), + + #[error("Could not perform bincode transformation: {0}")] + Bincode(#[from] Box), + + #[error("Could not perform frame transformation: {0}")] + FrameEnc(#[from] EncodeError), + + #[error("Could not perform frame transformation: {0}")] + FrameDec(#[from] DecodeError), +} + +pub type Result = std::result::Result; + +pub fn get_socket() -> Result { + let socket_vars = std::env::vars() + .filter(|(key, _)| ["XDG_RUNTIME_DIR", "KEYFORKD_SOCKET_PATH"].contains(&key.as_str())) + .collect::>(); + let mut socket_path: PathBuf; + #[allow(clippy::single_match_else)] + match socket_vars.get("KEYFORKD_SOCKET_PATH") { + Some(occupied) => { + socket_path = PathBuf::from(occupied); + } + None => { + socket_path = PathBuf::from( + socket_vars + .get("XDG_RUNTIME_DIR") + .ok_or(Error::EnvVarsNotFound)?, + ); + socket_path.extend(["keyforkd", "keyforkd.sock"]); + } + } + UnixStream::connect(&socket_path).map_err(|e| Error::Connect(e, socket_path)) +} + +#[derive(Debug)] +pub struct Client { + socket: UnixStream, +} + +impl Client { + pub fn new(socket: UnixStream) -> Self { + Self { socket } + } + + pub fn discover_socket() -> Result { + get_socket().map(|socket| Self { socket }) + } + + pub fn request(&mut self, req: &DerivationRequest) -> Result { + try_encode_to(&bincode::serialize(&req)?, &mut self.socket)?; + let resp = try_decode_from(&mut self.socket)?; + bincode::deserialize(&resp).map_err(From::from) + } +} diff --git a/keyfork-derive-key/src/tests.rs b/keyforkd-client/src/tests.rs similarity index 97% rename from keyfork-derive-key/src/tests.rs rename to keyforkd-client/src/tests.rs index da9383c..08f5840 100644 --- a/keyfork-derive-key/src/tests.rs +++ b/keyforkd-client/src/tests.rs @@ -1,4 +1,4 @@ -use crate::client::Client; +use crate::Client; use keyfork_derive_util::{request::*, DerivationPath}; use keyfork_slip10_test_data::test_data; use std::sync::mpsc::channel; @@ -13,6 +13,7 @@ fn secp256k1() { .remove(&"secp256k1".to_string()) .unwrap(); + // todo: move to tokio::sync channels to avoid using multi thread tokio let rt = Builder::new_multi_thread().enable_io().build().unwrap(); let tempdir = TempDir::new("keyfork-seed").unwrap(); for (i, per_seed) in tests.into_iter().enumerate() {