Compare commits

..

1 Commits

Author SHA1 Message Date
Ryan Heywood 3fd992d582
keyfork-prompt: add choice mechanism 2024-08-01 07:16:05 -04:00
5 changed files with 43 additions and 169 deletions

View File

@ -1,10 +1,8 @@
//! # The Keyforkd Client //! # The Keyforkd Client
//! //!
//! Keyfork allows securing the master key and highest-level derivation keys by having derivation //! Keyfork allows securing the master key and highest-level derivation keys by having derivation
//! requests performed against a server, "Keyforkd" or the "Keyfork Server". This allows //! requests performed against a server, "Keyforkd" or the "Keyfork Server". The server is operated
//! enforcement of policies, such as requiring at least two leves of a derivation path (for //! on a UNIX socket with messages sent using the Keyfork Frame format.
//! instance, `m/0'` would not be allowed, but `m/0'/0'` would). The server is operated on a UNIX
//! socket with messages sent using the Keyfork Frame format.
//! //!
//! Programs using the Keyfork Client should ensure they are built against a compatible version of //! Programs using the Keyfork Client should ensure they are built against a compatible version of
//! the Keyfork Server. For versions prior to `1.0.0`, all versions within a "minor" version (i.e., //! the Keyfork Server. For versions prior to `1.0.0`, all versions within a "minor" version (i.e.,
@ -12,58 +10,9 @@
//! after `1.0.0`, all versions within a "major" version (i.e., `1.0.0`) will be compatible, but //! after `1.0.0`, all versions within a "major" version (i.e., `1.0.0`) will be compatible, but
//! `1.x.y` will not be compatible with `2.0.0`. //! `1.x.y` will not be compatible with `2.0.0`.
//! //!
//! The Keyfork Client documentation makes extensive use of the `keyforkd::test_util` module. //! Presently, the Keyfork server only supports the following requests:
//! This provides testing infrastructure to set up a temporary Keyfork Daemon. In
//! your code, you should assume the daemon has already been initialized, whether by another
//! process, on another terminal, or some other instance. At no point should a program deriving an
//! "endpoint" key have control over a mnemonic or a seed.
//! //!
//! ## Server Requests //! * Derive Key
//!
//! Keyfork is designed as a client-request/server-response model. The client sends a request, such
//! as a derivation request, and the server sends its response. Presently, the Keyfork server
//! supports the following requests:
//!
//! ### Request: Derive Key
//!
//! The client creates a derivation path of at least two indices and requests a derived XPrv
//! (Extended Private Key) from the server.
//!
//! ```rust
//! use std::str::FromStr;
//!
//! use keyforkd_client::Client;
//! use keyfork_derive_util::DerivationPath;
//! # use keyfork_derive_util::private_key::TestPrivateKey as PrivateKey;
//! // use k256::SecretKey as PrivateKey;
//! // use ed25519_dalek::SigningKey as PrivateKey;
//!
//! #[derive(Debug, thiserror::Error)]
//! enum Error {
//! #[error(transparent)]
//! Path(#[from] keyfork_derive_util::PathError),
//!
//! #[error(transparent)]
//! Keyforkd(#[from] keyforkd_client::Error),
//! }
//!
//! fn main() -> Result<(), Error> {
//! # let seed = b"funky accordion noises";
//! # keyforkd::test_util::run_test(seed, |socket_path| {
//! let derivation_path = DerivationPath::from_str("m/44'/0'")?;
//! let mut client = Client::discover_socket()?;
//! let xprv = client.request_xprv::<PrivateKey>(&derivation_path)?;
//! # Ok::<_, Error>(())
//! # })?;
//! Ok(())
//! }
//! ```
//!
//! ---
//!
//! Request objects are typically handled by the Keyfork Client library (such as with
//! [`Client::request_xprv`]). While unadvised, clients can also attempt to handle their own
//! requests, using [`Client::request`].
//! //!
//! ## Extended Private Keys //! ## Extended Private Keys
//! //!
@ -72,87 +21,27 @@
//! The tests for this library ensure that all levels of Keyfork derivation beyond the required two //! The tests for this library ensure that all levels of Keyfork derivation beyond the required two
//! will be derived similarly between the server and the client. //! will be derived similarly between the server and the client.
//! //!
//! ```rust //! # Examples
//! use std::str::FromStr;
//!
//! use keyforkd_client::Client;
//! use keyfork_derive_util::{DerivationIndex, DerivationPath};
//! # use keyfork_derive_util::private_key::TestPrivateKey as PrivateKey;
//! // use k256::SecretKey as PrivateKey;
//! // use ed25519_dalek::SigningKey as PrivateKey;
//! # fn check_wallet<T>(_: T) {}
//!
//! #[derive(Debug, thiserror::Error)]
//! enum Error {
//! #[error(transparent)]
//! Index(#[from] keyfork_derive_util::IndexError),
//!
//! #[error(transparent)]
//! Path(#[from] keyfork_derive_util::PathError),
//!
//! #[error(transparent)]
//! PrivateKey(#[from] keyfork_derive_util::PrivateKeyError),
//!
//! #[error(transparent)]
//! Keyforkd(#[from] keyforkd_client::Error),
//! }
//!
//! fn main() -> Result<(), Error> {
//! # let seed = b"funky accordion noises";
//! # keyforkd::test_util::run_test(seed, |socket_path| {
//! let derivation_path = DerivationPath::from_str("m/44'/0'/0'/0")?;
//! let mut client = Client::discover_socket()?;
//! let xprv = client.request_xprv::<PrivateKey>(&derivation_path)?;
//! // scan first 20 wallets
//! for index in 0..20 {
//! // use non-hardened derivation
//! let new_xprv = xprv.derive_child(&DerivationIndex::new(index, false)?);
//! check_wallet(new_xprv)
//! }
//! # Ok::<_, Error>(())
//! # })?;
//! Ok(())
//! }
//! ```
//!
//! ## Testing Infrastructure
//!
//! In tests, the `keyforkd::test_util` module and TestPrivateKeys can be used. These provide
//! useful utilities for writing tests that interact with the Keyfork Server without needing to
//! manually create the server for the purpose of the test. The `run_test` method can be used to
//! run a test, which can handle both returning errors and correctly translating panics (though,
//! the panics definitely won't look tidy).
//!
//! ```rust //! ```rust
//! use std::str::FromStr; //! use std::str::FromStr;
//! //!
//! use keyforkd_client::Client; //! use keyforkd_client::Client;
//! use keyfork_derive_util::DerivationPath; //! use keyfork_derive_util::DerivationPath;
//! use keyfork_derive_util::private_key::TestPrivateKey as PrivateKey; //! # use keyfork_derive_util::private_key::TestPrivateKey as PrivateKey;
//! // use k256::SecretKey as PrivateKey;
//! // use ed25519_dalek::SigningKey as PrivateKey;
//! //!
//! #[derive(Debug, thiserror::Error)] //! # let seed = b"funky accordion noises";
//! enum Error { //! # keyforkd::test_util::run_test(seed, |socket_path| {
//! #[error(transparent)] //! # std::env::set_var("KEYFORKD_SOCKET_PATH", socket_path);
//! Path(#[from] keyfork_derive_util::PathError), //! let derivation_path = DerivationPath::from_str("m/44'/0'").unwrap();
//! //! let mut client = Client::discover_socket().unwrap();
//! #[error(transparent)] //! let xprv = client.request_xprv::<PrivateKey>(&derivation_path).unwrap();
//! Keyforkd(#[from] keyforkd_client::Error), //! # keyforkd::test_util::Infallible::Ok(())
//! } //! # }).unwrap();
//!
//! fn main() -> Result<(), Error> {
//! let seed = b"funky accordion noises";
//! keyforkd::test_util::run_test(seed, |socket_path| {
//! let derivation_path = DerivationPath::from_str("m/44'/0'")?;
//! let mut client = Client::discover_socket()?;
//! let xprv = client.request_xprv::<PrivateKey>(&derivation_path)?;
//! Ok::<_, Error>(())
//! })?;
//! Ok(())
//! }
//! ``` //! ```
//! //!
//! If you would rather write tests to panic rather than error, or would rather not deal with error //! In tests, the Keyforkd test_util module and TestPrivateKeys can be used.
//! types, the Panicable type should be used, which will handle the Error type for the closure.
//! //!
//! ```rust //! ```rust
//! use std::str::FromStr; //! use std::str::FromStr;
@ -163,10 +52,11 @@
//! //!
//! let seed = b"funky accordion noises"; //! let seed = b"funky accordion noises";
//! keyforkd::test_util::run_test(seed, |socket_path| { //! keyforkd::test_util::run_test(seed, |socket_path| {
//! std::env::set_var("KEYFORKD_SOCKET_PATH", socket_path);
//! let derivation_path = DerivationPath::from_str("m/44'/0'").unwrap(); //! let derivation_path = DerivationPath::from_str("m/44'/0'").unwrap();
//! let mut client = Client::discover_socket().unwrap(); //! let mut client = Client::discover_socket().unwrap();
//! let xprv = client.request_xprv::<PrivateKey>(&derivation_path).unwrap(); //! let xprv = client.request_xprv::<PrivateKey>(&derivation_path).unwrap();
//! keyforkd::test_util::Panicable::Ok(()) //! keyforkd::test_util::Infallible::Ok(())
//! }).unwrap(); //! }).unwrap();
//! ``` //! ```
@ -275,9 +165,10 @@ impl Client {
/// ///
/// # let seed = b"funky accordion noises"; /// # let seed = b"funky accordion noises";
/// # keyforkd::test_util::run_test(seed, |socket_path| { /// # keyforkd::test_util::run_test(seed, |socket_path| {
/// let mut socket = get_socket()?; /// # std::env::set_var("KEYFORKD_SOCKET_PATH", socket_path);
/// let mut socket = get_socket().unwrap();
/// let mut client = Client::new(socket); /// let mut client = Client::new(socket);
/// # Ok::<_, keyforkd_client::Error>(()) /// # keyforkd::test_util::Infallible::Ok(())
/// # }).unwrap(); /// # }).unwrap();
/// ``` /// ```
pub fn new(socket: UnixStream) -> Self { pub fn new(socket: UnixStream) -> Self {
@ -296,8 +187,9 @@ impl Client {
/// ///
/// # let seed = b"funky accordion noises"; /// # let seed = b"funky accordion noises";
/// # keyforkd::test_util::run_test(seed, |socket_path| { /// # keyforkd::test_util::run_test(seed, |socket_path| {
/// let mut client = Client::discover_socket()?; /// # std::env::set_var("KEYFORKD_SOCKET_PATH", socket_path);
/// # Ok::<_, keyforkd_client::Error>(()) /// let mut client = Client::discover_socket().unwrap();
/// # keyforkd::test_util::Infallible::Ok(())
/// # }).unwrap(); /// # }).unwrap();
/// ``` /// ```
pub fn discover_socket() -> Result<Self> { pub fn discover_socket() -> Result<Self> {
@ -325,10 +217,11 @@ impl Client {
/// ///
/// # let seed = b"funky accordion noises"; /// # let seed = b"funky accordion noises";
/// # keyforkd::test_util::run_test(seed, |socket_path| { /// # keyforkd::test_util::run_test(seed, |socket_path| {
/// # std::env::set_var("KEYFORKD_SOCKET_PATH", socket_path);
/// let derivation_path = DerivationPath::from_str("m/44'/0'").unwrap(); /// let derivation_path = DerivationPath::from_str("m/44'/0'").unwrap();
/// let mut client = Client::discover_socket().unwrap(); /// let mut client = Client::discover_socket().unwrap();
/// let xprv = client.request_xprv::<PrivateKey>(&derivation_path).unwrap(); /// let xprv = client.request_xprv::<PrivateKey>(&derivation_path).unwrap();
/// # keyforkd::test_util::Panicable::Ok(()) /// # keyforkd::test_util::Infallible::Ok(())
/// # }).unwrap(); /// # }).unwrap();
/// ``` /// ```
pub fn request_xprv<K>(&mut self, path: &DerivationPath) -> Result<ExtendedPrivateKey<K>> pub fn request_xprv<K>(&mut self, path: &DerivationPath) -> Result<ExtendedPrivateKey<K>>
@ -351,20 +244,15 @@ impl Client {
_ => Err(Error::InvalidResponse), _ => Err(Error::InvalidResponse),
} }
} }
}
impl Client {
/// Serialize and send a [`Request`] to the server, awaiting a [`Result<Response>`]. /// Serialize and send a [`Request`] to the server, awaiting a [`Result<Response>`].
/// ///
/// This function does not properly assert the association between a request type and a
/// response type, and does not perform any serialization of native objects into Request or
/// Response types, and should only be used when absolutely necessary.
///
/// # Errors /// # Errors
/// An error may be returned if: /// An error may be returned if:
/// * Reading or writing from or to the socket encountered an error. /// * Reading or writing from or to the socket encountered an error.
/// * Bincode could not serialize the request or deserialize the response. /// * Bincode could not serialize the request or deserialize the response.
/// * An error occurred in Keyforkd. /// * An error occurred in Keyforkd.
#[doc(hidden)]
pub fn request(&mut self, req: &Request) -> Result<Response> { pub fn request(&mut self, req: &Request) -> Result<Response> {
try_encode_to(&bincode::serialize(&req)?, &mut self.socket)?; try_encode_to(&bincode::serialize(&req)?, &mut self.socket)?;
let resp = try_decode_from(&mut self.socket)?; let resp = try_decode_from(&mut self.socket)?;

View File

@ -1,7 +1,7 @@
use crate::Client; use crate::Client;
use keyfork_derive_util::{request::*, DerivationPath}; use keyfork_derive_util::{request::*, DerivationPath};
use keyfork_slip10_test_data::test_data; use keyfork_slip10_test_data::test_data;
use keyforkd::test_util::{run_test, Panicable}; use keyforkd::test_util::{run_test, Infallible};
use std::{os::unix::net::UnixStream, str::FromStr}; use std::{os::unix::net::UnixStream, str::FromStr};
#[test] #[test]
@ -109,7 +109,7 @@ fn ed25519_test_suite() {
DerivationResponse::try_from(client.request(&req.into()).unwrap()).unwrap(); DerivationResponse::try_from(client.request(&req.into()).unwrap()).unwrap();
assert_eq!(&response.data, test.private_key.as_slice()); assert_eq!(&response.data, test.private_key.as_slice());
} }
Panicable::Ok(()) Infallible::Ok(())
}) })
.unwrap(); .unwrap();
} }

View File

@ -12,21 +12,20 @@ use keyfork_bug::bug;
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
#[error("This error can never be instantiated")] #[error("This error can never be instantiated")]
#[doc(hidden)] #[doc(hidden)]
pub enum UninstantiableError {} pub struct InfallibleError {
protected: (),
}
/// A panicable result. This type can be used when a closure chooses to panic instead of /// An infallible result. This type can be used to represent a function that should never error.
/// returning an error. This doesn't necessarily mean a closure _has_ to panic, and its absence
/// doesn't imply a closure _can't_ panic, but this is a useful utility function for writing tests,
/// to avoid the necessity of making custom error types.
/// ///
/// ```rust /// ```rust
/// use keyforkd::test_util::Panicable; /// use keyforkd::test_util::Infallible;
/// let closure = || { /// let closure = || {
/// Panicable::Ok(()) /// Infallible::Ok(())
/// }; /// };
/// assert!(closure().is_ok()); /// assert!(closure().is_ok());
/// ``` /// ```
pub type Panicable<T> = std::result::Result<T, UninstantiableError>; pub type Infallible<T> = std::result::Result<T, InfallibleError>;
/// Run a test making use of a Keyforkd server. The test may use a seed (the first argument) from a /// Run a test making use of a Keyforkd server. The test may use a seed (the first argument) from a
/// test suite, or (as shown in the example below) a simple seed may be used solely to ensure /// test suite, or (as shown in the example below) a simple seed may be used solely to ensure
@ -40,8 +39,6 @@ pub type Panicable<T> = std::result::Result<T, UninstantiableError>;
/// runtime. /// runtime.
/// ///
/// # Examples /// # Examples
/// The test utility provides a socket that can be connected to for deriving keys.
///
/// ```rust /// ```rust
/// use std::os::unix::net::UnixStream; /// use std::os::unix::net::UnixStream;
/// let seed = b"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; /// let seed = b"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
@ -49,18 +46,6 @@ pub type Panicable<T> = std::result::Result<T, UninstantiableError>;
/// UnixStream::connect(&path).map(|_| ()) /// UnixStream::connect(&path).map(|_| ())
/// }).unwrap(); /// }).unwrap();
/// ``` /// ```
///
/// The `keyforkd-client` crate uses the `KEYFORKD_SOCKET_PATH` variable to determine the default
/// socket path. The test will export the environment variable so it may be used by default.
///
/// ```rust
/// use std::os::unix::net::UnixStream;
/// let seed = b"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
/// keyforkd::test_util::run_test(seed.as_slice(), |path| {
/// assert_eq!(std::env::var_os("KEYFORKD_SOCKET_PATH").unwrap(), path.as_os_str());
/// UnixStream::connect(&path).map(|_| ())
/// }).unwrap();
/// ```
#[allow(clippy::missing_errors_doc)] #[allow(clippy::missing_errors_doc)]
pub fn run_test<F, E>(seed: &[u8], closure: F) -> Result<(), E> pub fn run_test<F, E>(seed: &[u8], closure: F) -> Result<(), E>
where where
@ -97,7 +82,6 @@ where
rx.recv() rx.recv()
.await .await
.expect(bug!("can't receive server start signal from channel")); .expect(bug!("can't receive server start signal from channel"));
std::env::set_var("KEYFORKD_SOCKET_PATH", &socket_path);
let test_handle = tokio::task::spawn_blocking(move || closure(&socket_path)); let test_handle = tokio::task::spawn_blocking(move || closure(&socket_path));
let result = test_handle.await; let result = test_handle.await;
@ -105,8 +89,8 @@ where
result result
}); });
if let Err(e) = result { if let Err(e) = result {
if let Ok(reason) = e.try_into_panic() { if e.is_panic() {
std::panic::resume_unwind(reason); std::panic::resume_unwind(e.into_panic());
} }
} }
Ok(()) Ok(())
@ -119,6 +103,6 @@ mod tests {
#[test] #[test]
fn test_run_test() { fn test_run_test() {
let seed = b"beefbeef"; let seed = b"beefbeef";
run_test(seed, |_path| Panicable::Ok(())).expect("infallible"); run_test(seed, |_path| Infallible::Ok(())).expect("infallible");
} }
} }

View File

@ -108,7 +108,7 @@ pub trait PromptHandler {
/// Prompt the user to select a choice between multiple options. /// Prompt the user to select a choice between multiple options.
/// ///
/// # Errors /// # Errors
/// The method may return an error if the message was not able to be displayed or if a choice /// The metho may return an error if the message was not able to be displayed or if a choice
/// could not be received. /// could not be received.
fn prompt_choice<'a, T>( fn prompt_choice<'a, T>(
&mut self, &mut self,

View File

@ -510,6 +510,7 @@ where
return Err(Error::CtrlC); return Err(Error::CtrlC);
} }
KeyCode::Left => { KeyCode::Left => {
eprintln!("{active_choice}");
// prevent underflow // prevent underflow
// if 0, max is 1, -1 is 0, no underflow // if 0, max is 1, -1 is 0, no underflow
// if 1, max is 1, -1 is 0 // if 1, max is 1, -1 is 0
@ -517,6 +518,7 @@ where
active_choice = std::cmp::max(1, active_choice) - 1; active_choice = std::cmp::max(1, active_choice) - 1;
} }
KeyCode::Right => { KeyCode::Right => {
eprintln!("{active_choice}");
active_choice = std::cmp::min(choices.len() - 1, active_choice + 1); active_choice = std::cmp::min(choices.len() - 1, active_choice + 1);
} }
KeyCode::Enter => { KeyCode::Enter => {