Merge pull request #4 from ZcashFoundation/handle_errors_326
Handle errors 326
This commit is contained in:
commit
4695467a8a
|
@ -44,4 +44,4 @@ jobs:
|
|||
run: cargo llvm-cov report --lcov --ignore-filename-regex 'tests.rs' --output-path lcov.info
|
||||
|
||||
- name: Upload coverage report to Codecov
|
||||
uses: codecov/codecov-action@v3.1.2
|
||||
uses: codecov/codecov-action@v3.1.3
|
||||
|
|
|
@ -290,6 +290,12 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "exitcode"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "de853764b47027c2e862a995c34978ffa63c1501f2e15f987ba11bd4f9bba193"
|
||||
|
||||
[[package]]
|
||||
name = "fastrand"
|
||||
version = "1.9.0"
|
||||
|
@ -347,6 +353,7 @@ dependencies = [
|
|||
name = "frost-trusted-dealer-demo"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"exitcode",
|
||||
"frost-ed25519",
|
||||
"hex",
|
||||
"itertools",
|
||||
|
|
|
@ -11,3 +11,4 @@ thiserror = "1.0"
|
|||
rand = "0.8"
|
||||
hex = "0.4"
|
||||
itertools = "0.10.5"
|
||||
exitcode = "1.1.2"
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use std::io;
|
||||
use frost_ed25519::Error;
|
||||
use std::io::BufRead;
|
||||
|
||||
#[derive(Debug, PartialEq, Copy, Clone)]
|
||||
pub struct Config {
|
||||
|
@ -6,38 +7,44 @@ pub struct Config {
|
|||
pub max_signers: u16,
|
||||
}
|
||||
|
||||
pub fn validate_inputs(config: &Config) -> Result<Config, frost_ed25519::Error> {
|
||||
pub fn validate_inputs(config: &Config) -> Result<(), Error> {
|
||||
if config.min_signers < 2 {
|
||||
return Err(frost_ed25519::Error::InvalidMinSigners);
|
||||
return Err(Error::InvalidMinSigners);
|
||||
}
|
||||
|
||||
if config.max_signers < 2 {
|
||||
return Err(frost_ed25519::Error::InvalidMaxSigners);
|
||||
return Err(Error::InvalidMaxSigners);
|
||||
}
|
||||
|
||||
if config.min_signers > config.max_signers {
|
||||
return Err(frost_ed25519::Error::InvalidMinSigners);
|
||||
return Err(Error::InvalidMinSigners);
|
||||
}
|
||||
|
||||
Ok(*config)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn request_inputs() -> Config {
|
||||
pub fn request_inputs(input: &mut impl BufRead) -> Result<Config, Error> {
|
||||
println!("The minimum number of signers: (2 or more)");
|
||||
|
||||
let mut min = String::new();
|
||||
io::stdin().read_line(&mut min).expect("invalid input");
|
||||
input.read_line(&mut min).unwrap();
|
||||
|
||||
let min_signers = min.trim().parse::<u16>().expect("Invalid input");
|
||||
let min_signers = min
|
||||
.trim()
|
||||
.parse::<u16>()
|
||||
.map_err(|_| Error::InvalidMinSigners)?;
|
||||
|
||||
println!("The maximum number of signers: (must be greater than minimum number of signers)");
|
||||
println!("The maximum number of signers: ");
|
||||
|
||||
let mut max = String::new();
|
||||
io::stdin().read_line(&mut max).expect("invalid input");
|
||||
let max_signers = max.trim().parse::<u16>().expect("invalid input");
|
||||
input.read_line(&mut max).unwrap();
|
||||
let max_signers = max
|
||||
.trim()
|
||||
.parse::<u16>()
|
||||
.map_err(|_| Error::InvalidMaxSigners)?;
|
||||
|
||||
Config {
|
||||
Ok(Config {
|
||||
min_signers,
|
||||
max_signers,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -13,13 +13,31 @@ use crate::inputs::{request_inputs, validate_inputs};
|
|||
use crate::trusted_dealer_keygen::trusted_dealer_keygen;
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let config = request_inputs();
|
||||
let mut reader = Box::new(io::stdin().lock());
|
||||
let config = request_inputs(&mut reader);
|
||||
if let Err(e) = config {
|
||||
eprintln!("Error: {}", e);
|
||||
std::process::exit(exitcode::DATAERR)
|
||||
}
|
||||
|
||||
let config = config.unwrap();
|
||||
|
||||
let mut rng = thread_rng();
|
||||
|
||||
validate_inputs(&config).expect("An error occurred");
|
||||
let valid = validate_inputs(&config);
|
||||
if let Err(e) = valid {
|
||||
eprintln!("Error: {}", e);
|
||||
std::process::exit(exitcode::DATAERR)
|
||||
}
|
||||
|
||||
// Print outputs
|
||||
let (key_packages, pubkeys) = trusted_dealer_keygen(config, &mut rng);
|
||||
let out = trusted_dealer_keygen(config, &mut rng);
|
||||
if let Err(e) = out {
|
||||
eprintln!("Error: {}", e);
|
||||
std::process::exit(1)
|
||||
}
|
||||
|
||||
let (key_packages, pubkeys) = out.unwrap();
|
||||
|
||||
let mut console_logger = ConsoleLogger::default();
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::inputs::{validate_inputs, Config};
|
||||
use crate::inputs::{request_inputs, validate_inputs, Config};
|
||||
|
||||
#[test]
|
||||
fn check_valid_input_for_signers() {
|
||||
|
@ -9,7 +9,7 @@ fn check_valid_input_for_signers() {
|
|||
|
||||
let expected = validate_inputs(&config);
|
||||
|
||||
assert_eq!(expected, Ok(config));
|
||||
assert_eq!(expected, Ok(()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -47,3 +47,31 @@ fn return_error_if_max_participant_is_less_than_2() {
|
|||
|
||||
assert_eq!(expected, Err(frost_ed25519::Error::InvalidMaxSigners));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn return_config_if_valid_input() {
|
||||
let mut valid_input = "3\n6\n".as_bytes();
|
||||
let config = request_inputs(&mut valid_input).unwrap();
|
||||
let expected = Config {
|
||||
min_signers: 3,
|
||||
max_signers: 6,
|
||||
};
|
||||
|
||||
assert_eq!(expected, config)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn return_error_if_invalid_min_signers_input() {
|
||||
let mut invalid_input = "hello\n6\n".as_bytes();
|
||||
let expected = request_inputs(&mut invalid_input);
|
||||
|
||||
assert_eq!(expected, Err(frost_ed25519::Error::InvalidMinSigners))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn return_error_if_invalid_max_signers_input() {
|
||||
let mut invalid_input = "4\nworld\n".as_bytes();
|
||||
let expected = request_inputs(&mut invalid_input);
|
||||
|
||||
assert_eq!(expected, Err(frost_ed25519::Error::InvalidMaxSigners))
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ fn check_keygen() {
|
|||
min_signers: 2,
|
||||
max_signers: 3,
|
||||
};
|
||||
let (key_packages, pubkeys) = trusted_dealer_keygen(config, &mut rng);
|
||||
let (key_packages, pubkeys) = trusted_dealer_keygen(config, &mut rng).unwrap();
|
||||
let (nonces, commitments) =
|
||||
signature_gen::generate_nonces_and_commitments(config.min_signers, &key_packages, &mut rng);
|
||||
let message = "message to sign".as_bytes();
|
||||
|
|
|
@ -22,7 +22,7 @@ fn check_output() {
|
|||
min_signers: 2,
|
||||
max_signers: 3,
|
||||
};
|
||||
let (key_packages, pubkeys) = trusted_dealer_keygen(config, &mut rng);
|
||||
let (key_packages, pubkeys) = trusted_dealer_keygen(config, &mut rng).unwrap();
|
||||
|
||||
print_values(&key_packages, pubkeys, &mut test_logger);
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use frost::keys::{KeyPackage, PublicKeyPackage};
|
||||
use frost::Identifier;
|
||||
use frost::{Error, Identifier};
|
||||
use frost_ed25519 as frost;
|
||||
use rand::rngs::ThreadRng;
|
||||
use std::collections::HashMap;
|
||||
|
@ -9,17 +9,16 @@ use crate::inputs::Config;
|
|||
pub fn trusted_dealer_keygen(
|
||||
config: Config,
|
||||
rng: &mut ThreadRng,
|
||||
) -> (HashMap<Identifier, KeyPackage>, PublicKeyPackage) {
|
||||
) -> Result<(HashMap<Identifier, KeyPackage>, PublicKeyPackage), Error> {
|
||||
let (shares, pubkeys) =
|
||||
frost::keys::keygen_with_dealer(config.max_signers, config.min_signers, rng)
|
||||
.expect("Error generating keys"); // TODO: handle error
|
||||
frost::keys::keygen_with_dealer(config.max_signers, config.min_signers, rng)?;
|
||||
|
||||
let mut key_packages: HashMap<_, _> = HashMap::new();
|
||||
|
||||
for (k, v) in shares {
|
||||
let key_package = frost::keys::KeyPackage::try_from(v).unwrap(); // TODO: handle error
|
||||
let key_package = frost::keys::KeyPackage::try_from(v)?;
|
||||
key_packages.insert(k, key_package);
|
||||
}
|
||||
|
||||
(key_packages, pubkeys)
|
||||
Ok((key_packages, pubkeys))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue