2023-09-28 02:30:56 +00:00
|
|
|
use crate::Client;
|
2023-09-26 00:40:37 +00:00
|
|
|
use keyfork_derive_util::{request::*, DerivationPath};
|
|
|
|
use keyfork_slip10_test_data::test_data;
|
2023-09-12 06:36:05 +00:00
|
|
|
use std::sync::mpsc::channel;
|
2023-09-12 03:41:55 +00:00
|
|
|
use std::{os::unix::net::UnixStream, str::FromStr};
|
|
|
|
use tempdir::TempDir;
|
|
|
|
use tokio::runtime::Builder;
|
|
|
|
|
|
|
|
#[test]
|
2023-09-26 00:40:37 +00:00
|
|
|
fn secp256k1() {
|
|
|
|
let tests = test_data()
|
|
|
|
.unwrap()
|
|
|
|
.remove(&"secp256k1".to_string())
|
|
|
|
.unwrap();
|
|
|
|
|
2023-09-28 02:51:01 +00:00
|
|
|
// note: since client is non async, can't be single threaded
|
2023-09-12 03:41:55 +00:00
|
|
|
let rt = Builder::new_multi_thread().enable_io().build().unwrap();
|
|
|
|
let tempdir = TempDir::new("keyfork-seed").unwrap();
|
2023-09-26 00:40:37 +00:00
|
|
|
for (i, per_seed) in tests.into_iter().enumerate() {
|
|
|
|
let mut socket_name = i.to_string();
|
|
|
|
socket_name.push_str("-keyforkd.sock");
|
|
|
|
let socket_path = tempdir.path().join(socket_name);
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
let handle = rt.spawn({
|
|
|
|
let socket_path = socket_path.clone();
|
|
|
|
async move {
|
|
|
|
let seed = per_seed.seed.clone();
|
|
|
|
let mut server = keyforkd::UnixServer::bind(&socket_path).unwrap();
|
|
|
|
tx.send(()).unwrap();
|
|
|
|
let service = keyforkd::ServiceBuilder::new()
|
|
|
|
.layer(keyforkd::middleware::BincodeLayer::new())
|
|
|
|
.service(keyforkd::Keyforkd::new(seed));
|
|
|
|
server.run(service).await.unwrap();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
rx.recv().unwrap();
|
2023-09-12 04:00:30 +00:00
|
|
|
|
2023-09-26 00:40:37 +00:00
|
|
|
for test in &per_seed.tests {
|
|
|
|
let socket = UnixStream::connect(&socket_path).unwrap();
|
|
|
|
let mut client = Client::new(socket);
|
|
|
|
let chain = DerivationPath::from_str(test.chain).unwrap();
|
|
|
|
if chain.len() < 2 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let req = DerivationRequest::new(
|
|
|
|
DerivationAlgorithm::Secp256k1,
|
|
|
|
&DerivationPath::from_str(test.chain).unwrap(),
|
|
|
|
);
|
2023-11-06 04:51:40 +00:00
|
|
|
let response =
|
|
|
|
DerivationResponse::try_from(client.request(&req.into()).unwrap()).unwrap();
|
2023-09-26 00:40:37 +00:00
|
|
|
assert_eq!(response.data, test.private_key);
|
2023-09-12 03:41:55 +00:00
|
|
|
}
|
2023-09-12 04:00:30 +00:00
|
|
|
|
2023-09-26 00:40:37 +00:00
|
|
|
handle.abort();
|
|
|
|
}
|
2023-09-12 03:41:55 +00:00
|
|
|
}
|
2023-09-12 06:36:05 +00:00
|
|
|
|
|
|
|
#[test]
|
2023-09-26 00:40:37 +00:00
|
|
|
fn ed25519() {
|
2023-11-05 06:29:10 +00:00
|
|
|
let tests = test_data().unwrap().remove(&"ed25519".to_string()).unwrap();
|
2023-09-26 00:40:37 +00:00
|
|
|
|
2023-09-12 06:36:05 +00:00
|
|
|
let rt = Builder::new_multi_thread().enable_io().build().unwrap();
|
|
|
|
let tempdir = TempDir::new("keyfork-seed").unwrap();
|
2023-09-26 00:40:37 +00:00
|
|
|
for (i, per_seed) in tests.into_iter().enumerate() {
|
|
|
|
let mut socket_name = i.to_string();
|
|
|
|
socket_name.push_str("-keyforkd.sock");
|
|
|
|
let socket_path = tempdir.path().join(socket_name);
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
let handle = rt.spawn({
|
|
|
|
let socket_path = socket_path.clone();
|
|
|
|
async move {
|
|
|
|
let seed = per_seed.seed.clone();
|
|
|
|
let mut server = keyforkd::UnixServer::bind(&socket_path).unwrap();
|
|
|
|
tx.send(()).unwrap();
|
|
|
|
let service = keyforkd::ServiceBuilder::new()
|
|
|
|
.layer(keyforkd::middleware::BincodeLayer::new())
|
|
|
|
.service(keyforkd::Keyforkd::new(seed));
|
|
|
|
server.run(service).await.unwrap();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
rx.recv().unwrap();
|
2023-09-12 06:36:05 +00:00
|
|
|
|
2023-09-26 00:40:37 +00:00
|
|
|
for test in &per_seed.tests {
|
|
|
|
let socket = UnixStream::connect(&socket_path).unwrap();
|
|
|
|
let mut client = Client::new(socket);
|
|
|
|
let chain = DerivationPath::from_str(test.chain).unwrap();
|
|
|
|
if chain.len() < 2 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let req = DerivationRequest::new(
|
|
|
|
DerivationAlgorithm::Ed25519,
|
|
|
|
&DerivationPath::from_str(test.chain).unwrap(),
|
|
|
|
);
|
2023-11-06 04:51:40 +00:00
|
|
|
let response =
|
|
|
|
DerivationResponse::try_from(client.request(&req.into()).unwrap()).unwrap();
|
2023-09-26 00:40:37 +00:00
|
|
|
assert_eq!(response.data, test.private_key);
|
|
|
|
}
|
2023-09-12 06:36:05 +00:00
|
|
|
|
2023-09-26 00:40:37 +00:00
|
|
|
handle.abort();
|
2023-09-12 06:36:05 +00:00
|
|
|
}
|
|
|
|
}
|