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 socket;
mod client;
#[cfg(test)]
mod tests;
@ -35,18 +36,18 @@ pub enum Error {
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 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 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)?;
let request = DerivationRequest::new(algo.clone(), path.clone());
let response = client.request(&request)?;
dbg!(&response);
Ok(())
}

View File

@ -1,37 +1,47 @@
use crate::client::Client;
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;
use std::sync::mpsc::channel;
#[test]
fn it_works() {
// Test literals taken from keyfork-derive-util.
// Setup
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 (tx, rx) = channel();
let handle = rt.spawn({
let socket_path = socket_path.clone();
async move {
keyforkd::start_and_run_server_on(mnemonic, &socket_path)
.await
.unwrap();
let mut server = keyforkd::UnixServer::bind(&socket_path).unwrap();
// Connections can be pending for a few seconds, so signal to the main
// 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.
std::thread::sleep(std::time::Duration::from_secs(2));
let mut socket = UnixStream::connect(&socket_path).unwrap();
rx.recv().unwrap();
let socket = UnixStream::connect(&socket_path).unwrap();
let mut client = Client::new(socket);
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"));
let response = client.request(&req).unwrap();
assert_eq!(
response.data,
hex!("8f94d394a8e8fd6b1bc2f3f49f5c47e385281d5c17e65324b0f62483e37e8793")
);
handle.abort();
}