use crate::Client; use keyfork_derive_util::{request::*, DerivationPath}; use keyfork_slip10_test_data::test_data; use std::sync::mpsc::channel; use std::{os::unix::net::UnixStream, str::FromStr}; use tempdir::TempDir; use tokio::runtime::Builder; #[test] fn secp256k1() { let tests = test_data() .unwrap() .remove(&"secp256k1".to_string()) .unwrap(); // note: since client is non async, can't be single threaded let rt = Builder::new_multi_thread().enable_io().build().unwrap(); let tempdir = TempDir::new("keyfork-seed").unwrap(); 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(); 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(), ); let response = DerivationResponse::try_from(client.request(&req.into()).unwrap()).unwrap(); assert_eq!(response.data, test.private_key); } handle.abort(); } } #[test] fn ed25519() { let tests = test_data().unwrap().remove(&"ed25519".to_string()).unwrap(); let rt = Builder::new_multi_thread().enable_io().build().unwrap(); let tempdir = TempDir::new("keyfork-seed").unwrap(); 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(); 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(), ); let response = DerivationResponse::try_from(client.request(&req.into()).unwrap()).unwrap(); assert_eq!(response.data, test.private_key); } handle.abort(); } }