Validate inputs for number of signers (#283)
Prompt for input and handle errors
This commit is contained in:
parent
5b6f66e29c
commit
5d4f19b0ed
File diff suppressed because it is too large
Load Diff
|
@ -6,3 +6,5 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
frost-ed25519 = "0.1.0"
|
||||||
|
thiserror = "1.0"
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
# Frost Trusted Dealer Demo
|
# Frost Trusted Dealer Demo
|
||||||
|
|
||||||
A CLI demo for running trusted dealer key generation with FROST
|
A CLI demo for running trusted dealer key generation with FROST
|
||||||
|
|
||||||
|
This demo uses ed25519
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
||||||
|
pub struct Config {
|
||||||
|
pub min_signers: u16,
|
||||||
|
pub max_signers: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validate_inputs(config: &Config) -> Result<Config, frost_ed25519::Error> {
|
||||||
|
if config.min_signers < 2 {
|
||||||
|
return Err(frost_ed25519::Error::InvalidMinSigners);
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.max_signers < 2 {
|
||||||
|
return Err(frost_ed25519::Error::InvalidMaxSigners);
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.min_signers > config.max_signers {
|
||||||
|
return Err(frost_ed25519::Error::InvalidMinSigners);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(*config)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn request_inputs() -> Config {
|
||||||
|
let mut min = "".to_string();
|
||||||
|
|
||||||
|
println!("The minimum number of signers:");
|
||||||
|
io::stdin().read_line(&mut min);
|
||||||
|
let min_signers = min.trim().parse::<u16>().unwrap();
|
||||||
|
|
||||||
|
let mut max = "".to_string();
|
||||||
|
|
||||||
|
println!("The maximum number of signers:");
|
||||||
|
io::stdin().read_line(&mut max);
|
||||||
|
let max_signers = max.trim().parse::<u16>().unwrap();
|
||||||
|
|
||||||
|
Config {
|
||||||
|
min_signers,
|
||||||
|
max_signers,
|
||||||
|
}
|
||||||
|
}
|
18
src/main.rs
18
src/main.rs
|
@ -1,6 +1,20 @@
|
||||||
|
mod inputs;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
fn main() {
|
use std::io;
|
||||||
println!("Hello, world!");
|
|
||||||
|
use crate::inputs::{request_inputs, validate_inputs};
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let config = request_inputs();
|
||||||
|
|
||||||
|
let out = validate_inputs(&config);
|
||||||
|
|
||||||
|
match out {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(e) => println!("An error occurred: {e}"),
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
48
src/tests.rs
48
src/tests.rs
|
@ -1,4 +1,48 @@
|
||||||
|
use crate::inputs::{validate_inputs, Config};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn check_test() {
|
fn check_valid_input_for_signers() {
|
||||||
assert!(1 == 1);
|
let config = Config {
|
||||||
|
min_signers: 2,
|
||||||
|
max_signers: 3,
|
||||||
|
};
|
||||||
|
|
||||||
|
let expected = validate_inputs(&config);
|
||||||
|
|
||||||
|
assert_eq!(expected, Ok(config));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn return_error_if_min_participant_greater_than_max_participant() {
|
||||||
|
let config = Config {
|
||||||
|
min_signers: 4,
|
||||||
|
max_signers: 3,
|
||||||
|
};
|
||||||
|
|
||||||
|
let expected = validate_inputs(&config);
|
||||||
|
|
||||||
|
assert_eq!(expected, Err(frost_ed25519::Error::InvalidMinSigners));
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn return_error_if_min_participant_is_less_than_2() {
|
||||||
|
let config = Config {
|
||||||
|
min_signers: 1,
|
||||||
|
max_signers: 3,
|
||||||
|
};
|
||||||
|
|
||||||
|
let expected = validate_inputs(&config);
|
||||||
|
|
||||||
|
assert_eq!(expected, Err(frost_ed25519::Error::InvalidMinSigners));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn return_error_if_max_participant_is_less_than_2() {
|
||||||
|
let config = Config {
|
||||||
|
min_signers: 2,
|
||||||
|
max_signers: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
let expected = validate_inputs(&config);
|
||||||
|
|
||||||
|
assert_eq!(expected, Err(frost_ed25519::Error::InvalidMaxSigners));
|
||||||
}
|
}
|
Loading…
Reference in New Issue