Add public key to Participant project (#28)

* Add text for requesting public key for participant (#17)

* Add public key to Participant Config (#17)

* Add error handling for incorrect pub key length (#17)
This commit is contained in:
natalie 2023-06-19 19:58:27 +01:00 committed by GitHub
parent 2092ccca0d
commit 7936c55e1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 11 deletions

9
Cargo.lock generated
View File

@ -109,18 +109,18 @@ dependencies = [
[[package]]
name = "clap"
version = "4.3.3"
version = "4.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca8f255e4b8027970e78db75e78831229c9815fdbfa67eb1a1b777a62e24b4a0"
checksum = "80672091db20273a15cf9fdd4e47ed43b5091ec9841bf4c6145c9dfbbcae09ed"
dependencies = [
"clap_builder",
]
[[package]]
name = "clap_builder"
version = "4.3.3"
version = "4.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acd4f3c17c83b0ba34ffbc4f8bbd74f079413f747f84a6f89292f138057e36ab"
checksum = "c1458a1df40e1e2afebb7ab60ce55c1fa8f431146205aa5f4887e0b111c27636"
dependencies = [
"anstyle",
"bitflags",
@ -549,6 +549,7 @@ name = "participant"
version = "0.1.0"
dependencies = [
"frost-ed25519",
"hex",
]
[[package]]

View File

@ -7,3 +7,4 @@ edition = "2021"
[dependencies]
frost-ed25519 = "0.4.0"
hex = "0.4"

View File

@ -1,10 +1,12 @@
use frost::{Error, Identifier};
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 trait Logger {
@ -23,15 +25,26 @@ pub fn request_inputs(input: &mut impl BufRead, logger: &mut dyn Logger) -> Resu
.parse::<u16>()
.map_err(|_| Error::MalformedIdentifier)?;
logger.log("Your public key:".to_string());
let mut public_key_input = String::new();
input.read_line(&mut public_key_input).unwrap();
let public_key =
<[u8; 32]>::from_hex(public_key_input.trim()).map_err(|_| Error::MalformedVerifyingKey)?;
Ok(Config {
identifier: Identifier::try_from(identifier)?,
public_key,
})
}
#[cfg(test)]
mod tests {
use frost::Identifier;
use frost::{Error, Identifier};
use frost_ed25519 as frost;
use hex::FromHex;
use crate::{request_inputs, Config, Logger};
@ -44,14 +57,22 @@ mod tests {
}
#[test]
fn check_valid_input_for_identifier() {
fn check_valid_inputs() {
let public_key = <[u8; 32]>::from_hex(
"929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509",
)
.unwrap();
let identifier = Identifier::try_from(1).unwrap();
let config = Config {
identifier: Identifier::try_from(1).unwrap(),
identifier,
public_key,
};
let mut test_logger = TestLogger(Vec::new());
let mut valid_input = "1\n".as_bytes();
let mut valid_input =
"1\n929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509\n".as_bytes();
let expected = request_inputs(&mut valid_input, &mut test_logger).unwrap();
assert_eq!(expected, config);
@ -61,7 +82,8 @@ mod tests {
fn check_0_input_for_identifier() {
let mut test_logger = TestLogger(Vec::new());
let mut invalid_input = "0\n".as_bytes();
let mut invalid_input =
"0\n929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509\n".as_bytes();
let expected = request_inputs(&mut invalid_input, &mut test_logger);
assert!(expected.is_err());
@ -76,4 +98,15 @@ mod tests {
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))
}
}

View File

@ -11,12 +11,14 @@ impl Logger for TestLogger {
#[test]
fn check_cli() {
let mut reader = "1\n".as_bytes();
let mut reader =
"1\n929dcc590407aae7d388761cddb0c0db6f5627aea8e217f4a033f2ec83d93509\n".as_bytes();
let mut test_logger = TestLogger(Vec::new());
cli(&mut reader, &mut test_logger);
assert_eq!(
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:"))
}