From a946d039a6f7805dd3d0409372c03bd157e82256 Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 11 Sep 2023 22:41:55 -0500 Subject: [PATCH] keyfork-seed: add basic example, test keyforkd --- Cargo.lock | 76 ++++++++++++++++++++++++++++++++++++--- keyfork-seed/Cargo.toml | 6 ++++ keyfork-seed/src/main.rs | 3 ++ keyfork-seed/src/tests.rs | 37 +++++++++++++++++++ 4 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 keyfork-seed/src/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 33091c0..a05bcb2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -174,7 +174,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf4c2f4e1afd912bc40bfd6fed5d9dc1f288e0ba01bfcc835cc5bc3eb13efe15" dependencies = [ "generic-array", - "rand_core", + "rand_core 0.6.4", "subtle", "zeroize", ] @@ -286,7 +286,7 @@ dependencies = [ "generic-array", "group", "pkcs8", - "rand_core", + "rand_core 0.6.4", "sec1", "subtle", "zeroize", @@ -304,7 +304,7 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ded41244b729663b1e574f1b4fb731469f69f79c17667b5d776b16cda0479449" dependencies = [ - "rand_core", + "rand_core 0.6.4", "subtle", ] @@ -314,6 +314,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0870c84016d4b481be5c9f323c24f65e31e901ae618f0e80f4308fb00de1d2d" +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "futures-core" version = "0.3.28" @@ -373,7 +379,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" dependencies = [ "ff", - "rand_core", + "rand_core 0.6.4", "subtle", ] @@ -485,9 +491,13 @@ version = "0.1.0" dependencies = [ "bincode", "clap", + "hex-literal", "keyfork-derive-util", "keyfork-frame", + "keyforkd", + "tempdir", "thiserror", + "tokio", ] [[package]] @@ -674,6 +684,34 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + [[package]] name = "rand_core" version = "0.6.4" @@ -683,6 +721,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + [[package]] name = "regex" version = "1.9.3" @@ -727,6 +774,15 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + [[package]] name = "ripemd" version = "0.1.3" @@ -843,7 +899,7 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e1788eed21689f9cf370582dfc467ef36ed9c707f073528ddafa8d83e3b8500" dependencies = [ - "rand_core", + "rand_core 0.6.4", ] [[package]] @@ -895,6 +951,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tempdir" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" +dependencies = [ + "rand", + "remove_dir_all", +] + [[package]] name = "thiserror" version = "1.0.48" diff --git a/keyfork-seed/Cargo.toml b/keyfork-seed/Cargo.toml index 161bd02..5829ee5 100644 --- a/keyfork-seed/Cargo.toml +++ b/keyfork-seed/Cargo.toml @@ -11,3 +11,9 @@ clap = { version = "4.4.2", default-features = false, features = ["std", "usage" 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" + +[dev-dependencies] +hex-literal = "0.4.1" +keyforkd = { path = "../keyforkd", default-features = false } +tempdir = "0.3.7" +tokio = { version = "1.32.0", features = ["rt", "rt-multi-thread"] } diff --git a/keyfork-seed/src/main.rs b/keyfork-seed/src/main.rs index 12f253c..ff79e54 100644 --- a/keyfork-seed/src/main.rs +++ b/keyfork-seed/src/main.rs @@ -5,6 +5,9 @@ use std::path::PathBuf; mod cli; mod socket; +#[cfg(test)] +mod tests; + #[derive(Debug, thiserror::Error)] pub enum Error { #[error("The first argument to the command should be a derivation path")] diff --git a/keyfork-seed/src/tests.rs b/keyfork-seed/src/tests.rs new file mode 100644 index 0000000..6616100 --- /dev/null +++ b/keyfork-seed/src/tests.rs @@ -0,0 +1,37 @@ +use hex_literal::hex; +use keyfork_derive_util::{request::*, DerivationPath}; +use keyfork_frame::*; +use std::{os::unix::net::UnixStream, str::FromStr}; +use tempdir::TempDir; +use tokio::runtime::Builder; + +#[test] +fn it_works() { + // Test literals taken from keyfork-derive-util. + let entropy = &hex!("000102030405060708090a0b0c0d0e0f")[..]; + let mnemonic = keyforkd::Mnemonic::from_entropy(entropy, Default::default()).unwrap(); + let rt = Builder::new_multi_thread().enable_io().build().unwrap(); + let tempdir = TempDir::new("keyfork-seed").unwrap(); + let socket_path = tempdir.path().join("keyforkd.sock"); + let handle = rt.spawn({ + let socket_path = socket_path.clone(); + async move { + keyforkd::start_and_run_server_on(mnemonic, &socket_path) + .await + .unwrap(); + } + }); + // TODO: send a channel to start_and_run_server_on, or extract logic out of server, + // so we can send a "started" message back to main thread. + std::thread::sleep(std::time::Duration::from_secs(2)); + let mut socket = UnixStream::connect(&socket_path).unwrap(); + let req = DerivationRequest::new( + DerivationAlgorithm::Ed25519, + DerivationPath::from_str("m/0'/1'/2'/2'/1000000000'").unwrap(), + ); + try_encode_to(&bincode::serialize(&req).unwrap(), &mut socket).unwrap(); + let response: DerivationResponse = + bincode::deserialize(&try_decode_from(&mut socket).unwrap()).unwrap(); + assert_eq!(response.data, hex!("8f94d394a8e8fd6b1bc2f3f49f5c47e385281d5c17e65324b0f62483e37e8793")); + handle.abort(); +}