keyfork-seed: initial commit, vaguely tested

This commit is contained in:
Ryan Heywood 2023-09-11 21:31:52 -05:00
parent 76779ee91c
commit 0f4bc3c78d
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
6 changed files with 152 additions and 4 deletions

50
Cargo.lock generated
View File

@ -26,6 +26,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "anstyle"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b84bf0a05bbb2a83e5eb6fa36bb6e87baa08193c35ff52bbf6b38d8af2890e46"
[[package]]
name = "backtrace"
version = "0.3.69"
@ -109,6 +115,31 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a13b88d2c62ff462f88e4a121f17a82c1af05693a2f192b5c38d14de73c19f6"
dependencies = [
"clap_builder",
]
[[package]]
name = "clap_builder"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08"
dependencies = [
"anstyle",
"clap_lex",
]
[[package]]
name = "clap_lex"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961"
[[package]]
name = "console"
version = "0.15.7"
@ -448,6 +479,17 @@ dependencies = [
"sha2",
]
[[package]]
name = "keyfork-seed"
version = "0.1.0"
dependencies = [
"bincode",
"clap",
"keyfork-derive-util",
"keyfork-frame",
"thiserror",
]
[[package]]
name = "keyforkd"
version = "0.1.0"
@ -855,18 +897,18 @@ dependencies = [
[[package]]
name = "thiserror"
version = "1.0.47"
version = "1.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97a802ec30afc17eee47b2855fc72e0c4cd62be9b4efe6591edde0ec5bd68d8f"
checksum = "9d6d7a740b8a666a7e828dd00da9c0dc290dff53154ea77ac109281de90589b7"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.47"
version = "1.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b"
checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35"
dependencies = [
"proc-macro2",
"quote",

View File

@ -5,6 +5,7 @@ members = [
"keyfork-mnemonic-generate",
"keyfork-mnemonic-util",
"keyfork-derive-util",
"keyfork-seed",
"keyforkd",
"keyfork-frame"
]

13
keyfork-seed/Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "keyfork-seed"
version = "0.1.0"
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"] }
keyfork-derive-util = { version = "0.1.0", path = "../keyfork-derive-util" }
keyfork-frame = { version = "0.1.0", path = "../keyfork-frame", default-features = false }
thiserror = "1.0.48"

19
keyfork-seed/src/cli.rs Normal file
View File

@ -0,0 +1,19 @@
use clap::{arg, value_parser, ArgMatches, Command};
use keyfork_derive_util::{request::*, DerivationPath};
pub fn get_args() -> ArgMatches {
Command::new("keyfork-seed")
.arg(
arg!(--path <PATH>)
.required(true)
.help("string value of a DerivationPath")
.value_parser(value_parser!(DerivationPath)),
)
.arg(
arg!(--algorithm <ALGO>)
.required(true)
.help("string value of a DerivationAlgorithm")
.value_parser(value_parser!(DerivationAlgorithm)),
)
.get_matches()
}

49
keyfork-seed/src/main.rs Normal file
View File

@ -0,0 +1,49 @@
use keyfork_derive_util::{request::*, DerivationPath};
use keyfork_frame::*;
use std::path::PathBuf;
mod cli;
mod socket;
#[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("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<bincode::ErrorKind>),
#[error("Could not perform frame transformation: {0}")]
FrameEnc(#[from] EncodeError),
#[error("Could not perform frame transformation: {0}")]
FrameDec(#[from] DecodeError),
}
fn main() -> Result<(), Error> {
let args = cli::get_args();
let mut socket = socket::get_socket()?;
let path = args.get_one::<DerivationPath>("path").expect("required");
let algo = args
.get_one::<DerivationAlgorithm>("algorithm")
.expect("required");
let req = DerivationRequest::new(algo.clone(), path.clone());
let ser_req = bincode::serialize(&req)?;
try_encode_to(&ser_req, &mut socket)?;
let ser_response = try_decode_from(&mut socket)?;
let response: DerivationResponse = bincode::deserialize(&ser_response)?;
dbg!(&response);
Ok(())
}

View File

@ -0,0 +1,24 @@
use super::Error;
use std::{collections::HashMap, os::unix::net::UnixStream, path::PathBuf};
pub fn get_socket() -> Result<UnixStream, Error> {
let socket_vars = std::env::vars()
.filter(|(key, _)| ["XDG_RUNTIME_DIR", "KEYFORKD_SOCKET_PATH"].contains(&key.as_str()))
.collect::<HashMap<String, String>>();
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))
}