diff --git a/participant/src/lib.rs b/participant/src/lib.rs index 3aa708e..5d9531b 100644 --- a/participant/src/lib.rs +++ b/participant/src/lib.rs @@ -1,12 +1,13 @@ -use frost::{Error, Identifier}; +use frost::{Error, Identifier, VerifyingKey}; use frost_ed25519 as frost; use hex::FromHex; use std::io::BufRead; #[derive(Debug, PartialEq)] pub struct Config { - identifier: Identifier, - public_key: [u8; 32], + pub identifier: Identifier, + pub public_key: [u8; 32], + pub group_public_key: VerifyingKey, } pub trait Logger { @@ -34,79 +35,18 @@ pub fn request_inputs(input: &mut impl BufRead, logger: &mut dyn Logger) -> Resu let public_key = <[u8; 32]>::from_hex(public_key_input.trim()).map_err(|_| Error::MalformedVerifyingKey)?; + logger.log("The group public key:".to_string()); + + let mut group_public_key_input = String::new(); + + input.read_line(&mut group_public_key_input).unwrap(); + + let group_public_key = VerifyingKey::from_hex(group_public_key_input.trim()) + .map_err(|_| Error::MalformedVerifyingKey)?; // TODO: Frost library needs to be updated with correct Error type + Ok(Config { identifier: Identifier::try_from(identifier)?, public_key, + group_public_key, }) } - -#[cfg(test)] -mod tests { - use frost::{Error, Identifier}; - use frost_ed25519 as frost; - use hex::FromHex; - - use crate::{request_inputs, Config, Logger}; - - pub struct TestLogger(Vec); - - impl Logger for TestLogger { - fn log(&mut self, value: String) { - self.0.push(value); - } - } - - #[test] - fn check_valid_inputs() { - let public_key = <[u8; 32]>::from_hex( - "929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509", - ) - .unwrap(); - let identifier = Identifier::try_from(1).unwrap(); - - let config = Config { - identifier, - public_key, - }; - - let mut test_logger = TestLogger(Vec::new()); - - let mut valid_input = - "1\n929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509\n".as_bytes(); - let expected = request_inputs(&mut valid_input, &mut test_logger).unwrap(); - - assert_eq!(expected, config); - } - - #[test] - fn check_0_input_for_identifier() { - let mut test_logger = TestLogger(Vec::new()); - - let mut invalid_input = - "0\n929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509\n".as_bytes(); - let expected = request_inputs(&mut invalid_input, &mut test_logger); - - assert!(expected.is_err()); - } - - #[test] - fn check_non_u16_input_for_identifier() { - let mut test_logger = TestLogger(Vec::new()); - - let mut invalid_input = "-1\n".as_bytes(); - let expected = request_inputs(&mut invalid_input, &mut test_logger); - - assert!(expected.is_err()); - } - - #[test] - fn check_invalid_length_public_key() { - let mut test_logger = TestLogger(Vec::new()); - - let mut invalid_input = "1\n123456\n".as_bytes(); - let expected = request_inputs(&mut invalid_input, &mut test_logger); - - assert!(expected.is_err()); - assert!(expected == Err(Error::MalformedVerifyingKey)) - } -} diff --git a/participant/src/tests/cli.rs b/participant/src/tests/cli.rs index 25b142d..6e598cb 100644 --- a/participant/src/tests/cli.rs +++ b/participant/src/tests/cli.rs @@ -11,8 +11,11 @@ impl Logger for TestLogger { #[test] fn check_cli() { - let mut reader = - "1\n929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509\n".as_bytes(); + let identifier = "1"; + let pub_key = "929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509"; + let group_pub_key = "15d21ccd7ee42959562fc8aa63224c8851fb3ec85a3faf66040d380fb9738673"; + let input = format!("{}\n{}\n{}\n", identifier, pub_key, group_pub_key); + let mut reader = input.as_bytes(); let mut test_logger = TestLogger(Vec::new()); cli(&mut reader, &mut test_logger); @@ -20,5 +23,6 @@ fn check_cli() { test_logger.0[0], format!("Your identifier (this should be an integer between 1 and 65535):") ); - assert_eq!(test_logger.0[1], format!("Your public key:")) + assert_eq!(test_logger.0[1], format!("Your public key:")); + assert_eq!(test_logger.0[2], format!("The group public key:")) } diff --git a/participant/src/tests/lib.rs b/participant/src/tests/lib.rs index 72d3b46..793b793 100644 --- a/participant/src/tests/lib.rs +++ b/participant/src/tests/lib.rs @@ -1,2 +1,102 @@ +use frost::VerifyingKey; +#[cfg(test)] +use frost::{Error, Identifier}; +use frost_ed25519 as frost; +use hex::FromHex; +use participant::{request_inputs, Config}; + +use crate::Logger; + +pub struct TestLogger(Vec); + +impl Logger for TestLogger { + fn log(&mut self, value: String) { + self.0.push(value); + } +} + #[test] -fn check_test() {} +fn check_valid_inputs() { + let public_key = + <[u8; 32]>::from_hex("929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509") + .unwrap(); + let identifier = Identifier::try_from(1).unwrap(); + let group_public_key = + VerifyingKey::from_hex("15d21ccd7ee42959562fc8aa63224c8851fb3ec85a3faf66040d380fb9738673") + .unwrap(); + + let config = Config { + identifier, + public_key, + group_public_key, + }; + + let mut test_logger = TestLogger(Vec::new()); + + let identifier = "1"; + let pub_key = "929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509"; + let group_pub_key = "15d21ccd7ee42959562fc8aa63224c8851fb3ec85a3faf66040d380fb9738673"; + let input = format!("{}\n{}\n{}\n", identifier, pub_key, group_pub_key); + let mut valid_input = input.as_bytes(); + + let expected = request_inputs(&mut valid_input, &mut test_logger).unwrap(); + + assert_eq!(expected, config); +} + +#[test] +fn check_0_input_for_identifier() { + let mut test_logger = TestLogger(Vec::new()); + + let identifier = "0"; + let pub_key = "929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509"; + let group_pub_key = "15d21ccd7ee42959562fc8aa63224c8851fb3ec85a3faf66040d380fb9738673"; + let input = format!("{}\n{}\n{}\n", identifier, pub_key, group_pub_key); + let mut invalid_input = input.as_bytes(); + + let expected = request_inputs(&mut invalid_input, &mut test_logger); + + assert!(expected.is_err()); +} + +#[test] +fn check_non_u16_input_for_identifier() { + let mut test_logger = TestLogger(Vec::new()); + + let mut invalid_input = "-1\n".as_bytes(); + let expected = request_inputs(&mut invalid_input, &mut test_logger); + + assert!(expected.is_err()); +} + +#[test] +fn check_invalid_length_public_key() { + let mut test_logger = TestLogger(Vec::new()); + + let identifier = "1"; + let pub_key = "123456"; + let group_pub_key = "15d21ccd7ee42959562fc8aa63224c8851fb3ec85a3faf66040d380fb9738673"; + let input = format!("{}\n{}\n{}\n", identifier, pub_key, group_pub_key); + let mut invalid_input = input.as_bytes(); + + let expected = request_inputs(&mut invalid_input, &mut test_logger); + + assert!(expected.is_err()); + assert!(expected == Err(Error::MalformedVerifyingKey)) +} + +#[test] +fn check_invalid_length_group_public_key() { + let mut test_logger = TestLogger(Vec::new()); + + let identifier = "1"; + let pub_key = "929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509"; + let group_pub_key = "123456"; + let input = format!("{}\n{}\n{}\n", identifier, pub_key, group_pub_key); + let mut invalid_input = input.as_bytes(); + + let expected = request_inputs(&mut invalid_input, &mut test_logger); + + assert!(expected.is_err()); + assert!(expected == Err(Error::MalformedVerifyingKey)) +}