keyfork-seed: fixup tests, add Client

This commit is contained in:
Ryan Heywood 2023-09-11 23:00:30 -05:00
parent 0ef93fced5
commit 2e9bfde9b4
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
3 changed files with 51 additions and 19 deletions

View File

@ -0,0 +1,21 @@
use keyfork_frame::*;
use crate::Result;
use std::os::unix::net::UnixStream;
use keyfork_derive_util::request::*;
#[derive(Debug)]
pub struct Client {
socket: UnixStream,
}
impl Client {
pub fn new(socket: UnixStream) -> Self {
Self { socket }
}
pub fn request(&mut self, req: &DerivationRequest) -> Result<DerivationResponse> {
try_encode_to(&bincode::serialize(&req)?, &mut self.socket)?;
let resp = try_decode_from(&mut self.socket)?;
bincode::deserialize(&resp).map_err(From::from)
}
}

View File

@ -4,6 +4,7 @@ use std::path::PathBuf;
mod cli; mod cli;
mod socket; mod socket;
mod client;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
@ -35,18 +36,18 @@ pub enum Error {
FrameDec(#[from] DecodeError), FrameDec(#[from] DecodeError),
} }
fn main() -> Result<(), Error> { pub type Result<T, E = Error> = std::result::Result<T, E>;
fn main() -> Result<()> {
let args = cli::get_args(); let args = cli::get_args();
let mut socket = socket::get_socket()?; let socket = socket::get_socket()?;
let mut client = client::Client::new(socket);
let path = args.get_one::<DerivationPath>("path").expect("required"); let path = args.get_one::<DerivationPath>("path").expect("required");
let algo = args let algo = args
.get_one::<DerivationAlgorithm>("algorithm") .get_one::<DerivationAlgorithm>("algorithm")
.expect("required"); .expect("required");
let req = DerivationRequest::new(algo.clone(), path.clone()); let request = DerivationRequest::new(algo.clone(), path.clone());
let ser_req = bincode::serialize(&req)?; let response = client.request(&request)?;
try_encode_to(&ser_req, &mut socket)?;
let ser_response = try_decode_from(&mut socket)?;
let response: DerivationResponse = bincode::deserialize(&ser_response)?;
dbg!(&response); dbg!(&response);
Ok(()) Ok(())
} }

View File

@ -1,37 +1,47 @@
use crate::client::Client;
use hex_literal::hex; use hex_literal::hex;
use keyfork_derive_util::{request::*, DerivationPath}; use keyfork_derive_util::{request::*, DerivationPath};
use keyfork_frame::*;
use std::{os::unix::net::UnixStream, str::FromStr}; use std::{os::unix::net::UnixStream, str::FromStr};
use tempdir::TempDir; use tempdir::TempDir;
use tokio::runtime::Builder; use tokio::runtime::Builder;
use std::sync::mpsc::channel;
#[test] #[test]
fn it_works() { fn it_works() {
// Test literals taken from keyfork-derive-util. // Test literals taken from keyfork-derive-util.
// Setup
let entropy = &hex!("000102030405060708090a0b0c0d0e0f")[..]; let entropy = &hex!("000102030405060708090a0b0c0d0e0f")[..];
let mnemonic = keyforkd::Mnemonic::from_entropy(entropy, Default::default()).unwrap(); let mnemonic = keyforkd::Mnemonic::from_entropy(entropy, Default::default()).unwrap();
let rt = Builder::new_multi_thread().enable_io().build().unwrap(); let rt = Builder::new_multi_thread().enable_io().build().unwrap();
let tempdir = TempDir::new("keyfork-seed").unwrap(); let tempdir = TempDir::new("keyfork-seed").unwrap();
let socket_path = tempdir.path().join("keyforkd.sock"); let socket_path = tempdir.path().join("keyforkd.sock");
let (tx, rx) = channel();
let handle = rt.spawn({ let handle = rt.spawn({
let socket_path = socket_path.clone(); let socket_path = socket_path.clone();
async move { async move {
keyforkd::start_and_run_server_on(mnemonic, &socket_path) let mut server = keyforkd::UnixServer::bind(&socket_path).unwrap();
.await // Connections can be pending for a few seconds, so signal to the main
.unwrap(); // test we're ready to start accepting.
tx.send(()).unwrap();
let service = keyforkd::ServiceBuilder::new()
.layer(keyforkd::middleware::BincodeLayer::new())
.service(keyforkd::Keyforkd::new(mnemonic));
server.run(service).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. rx.recv().unwrap();
std::thread::sleep(std::time::Duration::from_secs(2)); let socket = UnixStream::connect(&socket_path).unwrap();
let mut socket = UnixStream::connect(&socket_path).unwrap(); let mut client = Client::new(socket);
let req = DerivationRequest::new( let req = DerivationRequest::new(
DerivationAlgorithm::Ed25519, DerivationAlgorithm::Ed25519,
DerivationPath::from_str("m/0'/1'/2'/2'/1000000000'").unwrap(), DerivationPath::from_str("m/0'/1'/2'/2'/1000000000'").unwrap(),
); );
try_encode_to(&bincode::serialize(&req).unwrap(), &mut socket).unwrap(); let response = client.request(&req).unwrap();
let response: DerivationResponse = assert_eq!(
bincode::deserialize(&try_decode_from(&mut socket).unwrap()).unwrap(); response.data,
assert_eq!(response.data, hex!("8f94d394a8e8fd6b1bc2f3f49f5c47e385281d5c17e65324b0f62483e37e8793")); hex!("8f94d394a8e8fd6b1bc2f3f49f5c47e385281d5c17e65324b0f62483e37e8793")
);
handle.abort(); handle.abort();
} }