diff --git a/processes/src/authenticator/process.rs b/processes/src/authenticator/process.rs deleted file mode 100644 index 71f0352..0000000 --- a/processes/src/authenticator/process.rs +++ /dev/null @@ -1,65 +0,0 @@ -use std::process::Stdio; - -use super::{Processor, Request}; -use nacho_data_structures::{ByteConversion, Transaction}; -use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader}; -use tokio::process::{ChildStdin, ChildStdout, Command}; -use tokio::sync::mpsc; - -pub fn process(path: &str) -> Processor { - let (sender, mut receiver) = mpsc::channel::(1000); - - let (stdin, stdout) = spawn_authenticator_script(path); - - let mut stdout = BufReader::new(stdout); - - tokio::spawn(async move { - while let Some(request) = receiver.recv().await { - match request { - Request::CheckSignature { sender, tx } => { - let is_valid = check_signature(stdin, &mut stdout, tx).await; - sender.send(is_valid).unwrap(); - } - } - } - }); - - Processor { - sender: Box::leak(Box::new(sender)), - } -} - -pub fn spawn_authenticator_script( - path: &str, -) -> (&'static mut ChildStdin, &'static mut ChildStdout) { - // Spawn a Node.js process - let mut node_process = Command::new("node") - .arg(path) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::inherit()) - .kill_on_drop(true) - .spawn() - .unwrap(); - - let stdin = node_process.stdin.take().unwrap(); - let stdout = node_process.stdout.take().unwrap(); - - (Box::leak(Box::new(stdin)), Box::leak(Box::new(stdout))) -} - -pub async fn check_signature( - stdin: &mut ChildStdin, - stdout: &mut BufReader<&mut ChildStdout>, - tx: Transaction, -) -> Option { - let tx_buf = tx.to_bytes(); - - stdin.write_all(&tx_buf).await.ok()?; - - let is_valid = stdout.read_u8().await.ok()?; - - let is_valid = is_valid != 0; - - Some(is_valid) -} diff --git a/processes/src/authenticator/mod.rs b/processes/src/verifier/mod.rs similarity index 100% rename from processes/src/authenticator/mod.rs rename to processes/src/verifier/mod.rs diff --git a/processes/src/verifier/process.rs b/processes/src/verifier/process.rs new file mode 100644 index 0000000..fa6a2a1 --- /dev/null +++ b/processes/src/verifier/process.rs @@ -0,0 +1,32 @@ +use super::{Processor, Request}; +use nacho_data_structures::ByteConversion; +use tokio::sync::mpsc; + +pub fn process(path: &str) -> Processor { + let (sender, mut receiver) = mpsc::channel::(1000); + + let (stdin, stdout) = nacho_js_process::spawn(&[path]).unwrap(); + + tokio::spawn(async move { + while let Some(request) = receiver.recv().await { + match request { + Request::CheckSignature { sender, tx } => { + let input = tx.to_bytes(); + let mut output = [0u8; 1]; + + nacho_js_process::interact(stdin, stdout, &input, &mut output) + .await + .ok(); + + let is_valid = output[0] != 0; + + sender.send(Some(is_valid)).unwrap(); + } + } + } + }); + + Processor { + sender: Box::leak(Box::new(sender)), + } +} diff --git a/processes/src/authenticator/processor.rs b/processes/src/verifier/processor.rs similarity index 100% rename from processes/src/authenticator/processor.rs rename to processes/src/verifier/processor.rs diff --git a/processes/src/authenticator/request.rs b/processes/src/verifier/request.rs similarity index 100% rename from processes/src/authenticator/request.rs rename to processes/src/verifier/request.rs