From 90d2f46cda31e16c0c906e4b5c0829941e6697fe Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 12 Sep 2023 00:58:30 -0500 Subject: [PATCH] keyfork: begin work on The Kitchen Sink --- Cargo.lock | 93 ++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + keyfork/Cargo.toml | 12 +++++ keyfork/src/cli/mnemonic.rs | 68 +++++++++++++++++++++++++++ keyfork/src/cli/mod.rs | 42 +++++++++++++++++ keyfork/src/main.rs | 9 ++++ 6 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 keyfork/Cargo.toml create mode 100644 keyfork/src/cli/mnemonic.rs create mode 100644 keyfork/src/cli/mod.rs create mode 100644 keyfork/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index a05bcb2..0adfe47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,12 +26,54 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstream" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f58811cfac344940f1a400b6e6231ce35171f614f26439e80f8c1465c5cc0c" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + [[package]] name = "anstyle" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b84bf0a05bbb2a83e5eb6fa36bb6e87baa08193c35ff52bbf6b38d8af2890e46" +[[package]] +name = "anstyle-parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys 0.48.0", +] + +[[package]] +name = "anstyle-wincon" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd" +dependencies = [ + "anstyle", + "windows-sys 0.48.0", +] + [[package]] name = "backtrace" version = "0.3.69" @@ -122,6 +164,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a13b88d2c62ff462f88e4a121f17a82c1af05693a2f192b5c38d14de73c19f6" dependencies = [ "clap_builder", + "clap_derive", ] [[package]] @@ -130,8 +173,22 @@ version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08" dependencies = [ + "anstream", "anstyle", "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -140,6 +197,12 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961" +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "console" version = "0.15.7" @@ -383,6 +446,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.2" @@ -441,6 +510,16 @@ dependencies = [ "once_cell", ] +[[package]] +name = "keyfork" +version = "0.1.0" +dependencies = [ + "clap", + "keyfork-mnemonic-generate", + "keyfork-seed", + "thiserror", +] + [[package]] name = "keyfork-derive-util" version = "0.1.0" @@ -470,7 +549,7 @@ dependencies = [ [[package]] name = "keyfork-mnemonic-generate" -version = "0.1.0" +version = "0.2.0" dependencies = [ "keyfork-mnemonic-util", ] @@ -934,6 +1013,12 @@ dependencies = [ "der", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "subtle" version = "2.5.0" @@ -1158,6 +1243,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "valuable" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 226af55..ab5631c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ resolver = "2" members = [ + "keyfork", "keyfork-mnemonic-generate", "keyfork-mnemonic-util", "keyfork-derive-util", diff --git a/keyfork/Cargo.toml b/keyfork/Cargo.toml new file mode 100644 index 0000000..1e722f3 --- /dev/null +++ b/keyfork/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "keyfork" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = { version = "4.4.2", features = ["derive", "env"] } +keyfork-mnemonic-generate = { version = "0.2.0", path = "../keyfork-mnemonic-generate" } +keyfork-seed = { version = "0.1.0", path = "../keyfork-seed" } +thiserror = "1.0.48" diff --git a/keyfork/src/cli/mnemonic.rs b/keyfork/src/cli/mnemonic.rs new file mode 100644 index 0000000..a046d18 --- /dev/null +++ b/keyfork/src/cli/mnemonic.rs @@ -0,0 +1,68 @@ +use super::{Keyfork, KeyforkCommands}; +use clap::{Parser, Subcommand}; +use keyfork_mnemonic_generate::generate_mnemonic; + +#[derive(Clone, Debug)] +pub enum EntropySize { + Bits128, + Bits256, +} + +#[derive(thiserror::Error, Debug, Clone)] +pub enum EntropySizeError { + #[error("Expected one of 128, 256")] + InvalidChoice, +} + +impl std::str::FromStr for EntropySize { + type Err = EntropySizeError; + + fn from_str(s: &str) -> Result { + Ok(match s { + "128" => EntropySize::Bits128, + "256" => EntropySize::Bits256, + _ => return Err(EntropySizeError::InvalidChoice), + }) + } +} + +impl From<&EntropySize> for usize { + fn from(value: &EntropySize) -> Self { + match value { + EntropySize::Bits128 => 128, + EntropySize::Bits256 => 256, + } + } +} + +#[derive(Subcommand, Clone, Debug)] +pub enum MnemonicSubcommands { + /// Generate a mnemonic using OS entropy. + Generate { + /// The size in bits to generate entropy for. + entropy: EntropySize, + }, +} + +impl MnemonicSubcommands { + pub fn handle( + &self, + _m: &Mnemonic, + _p: &KeyforkCommands, + _keyfork: &Keyfork, + ) -> Result<(), Box> { + match self { + MnemonicSubcommands::Generate { entropy } => { + let mnemonic = generate_mnemonic(usize::from(entropy))?; + println!("{mnemonic}"); + Ok(()) + } + } + } +} + +#[derive(Parser, Debug, Clone)] +pub struct Mnemonic { + #[command(subcommand)] + pub command: MnemonicSubcommands, +} diff --git a/keyfork/src/cli/mod.rs b/keyfork/src/cli/mod.rs new file mode 100644 index 0000000..4502922 --- /dev/null +++ b/keyfork/src/cli/mod.rs @@ -0,0 +1,42 @@ +use clap::{Parser, Subcommand}; + +mod mnemonic; + +/// The Kitchen Sink of Entropy. +#[derive(Parser, Clone, Debug)] +#[command(author, version, about, long_about = None)] +pub struct Keyfork { + // Global options + #[command(subcommand)] + pub command: KeyforkCommands, +} + +#[derive(Subcommand, Clone, Debug)] +pub enum KeyforkCommands { + /// Mnemonic generation and persistence utilities. + Mnemonic(mnemonic::Mnemonic), + + /// Seeded data generation utility. + Seed(keyfork_seed::cli::Command), + + /// Keyforkd background daemon to manage seed creation. + Daemon, +} + +impl KeyforkCommands { + pub fn handle(&self, keyfork: &Keyfork) -> Result<(), Box> { + match self { + KeyforkCommands::Mnemonic(m) => { + m.command.handle(m, self, keyfork)?; + } + KeyforkCommands::Seed(s) => { + let response = s.handle()?; + println!("{response:?}"); + } + KeyforkCommands::Daemon => { + todo!() + } + } + Ok(()) + } +} diff --git a/keyfork/src/main.rs b/keyfork/src/main.rs new file mode 100644 index 0000000..e1a7725 --- /dev/null +++ b/keyfork/src/main.rs @@ -0,0 +1,9 @@ +use clap::Parser; + +mod cli; + +fn main() { + let opts = cli::Keyfork::parse(); + + opts.command.handle(&opts).unwrap(); +}