Add feat for account_manager to gen key range

This commit is contained in:
Paul Hauner 2019-05-28 14:32:32 +10:00
parent 3f27fd4edf
commit 5e435e7821
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF

View File

@ -43,6 +43,14 @@ fn main() {
.help("The index of the validator, for which the test key is generated") .help("The index of the validator, for which the test key is generated")
.takes_value(true) .takes_value(true)
.required(true), .required(true),
)
.arg(
Arg::with_name("end validator index")
.long("end_index")
.short("j")
.value_name("end_index")
.help("If supplied along with `index`, generates a range of keys.")
.takes_value(true),
), ),
) )
.get_matches(); .get_matches();
@ -55,37 +63,60 @@ fn main() {
"data_dir" => &config.data_dir.to_str()); "data_dir" => &config.data_dir.to_str());
match matches.subcommand() { match matches.subcommand() {
("generate", Some(_gen_m)) => { ("generate", Some(_)) => generate_random(&config, &log),
let keypair = Keypair::random(); ("generate_deterministic", Some(m)) => {
let key_path: PathBuf = config if let Some(string) = m.value_of("validator index") {
.save_key(&keypair) let i: usize = string.parse().expect("Invalid validator index");
.expect("Unable to save newly generated private key."); if let Some(string) = m.value_of("end validator index") {
debug!( let j: usize = string.parse().expect("Invalid end validator index");
log,
"Keypair generated {:?}, saved to: {:?}", let indices: Vec<usize> = (i..j).collect();
keypair.identifier(), generate_deterministic_multiple(&indices, &config, &log)
key_path.to_string_lossy() } else {
); generate_deterministic(i, &config, &log)
} }
("generate_deterministic", Some(gen_d_matches)) => { }
let validator_index = gen_d_matches
.value_of("validator index")
.expect("Validator index required.")
.parse::<u64>()
.expect("Invalid validator index.") as usize;
let keypair = generate_deterministic_keypair(validator_index);
let key_path: PathBuf = config
.save_key(&keypair)
.expect("Unable to save newly generated deterministic private key.");
debug!(
log,
"Deterministic Keypair generated {:?}, saved to: {:?}",
keypair.identifier(),
key_path.to_string_lossy()
);
} }
_ => panic!( _ => panic!(
"The account manager must be run with a subcommand. See help for more information." "The account manager must be run with a subcommand. See help for more information."
), ),
} }
} }
fn generate_random(config: &ValidatorClientConfig, log: &slog::Logger) {
save_key(&Keypair::random(), config, log)
}
fn generate_deterministic_multiple(
validator_indices: &[usize],
config: &ValidatorClientConfig,
log: &slog::Logger,
) {
for validator_index in validator_indices {
generate_deterministic(*validator_index, config, log)
}
}
fn generate_deterministic(
validator_index: usize,
config: &ValidatorClientConfig,
log: &slog::Logger,
) {
save_key(
&generate_deterministic_keypair(validator_index),
config,
log,
)
}
fn save_key(keypair: &Keypair, config: &ValidatorClientConfig, log: &slog::Logger) {
let key_path: PathBuf = config
.save_key(&keypair)
.expect("Unable to save newly generated private key.");
debug!(
log,
"Keypair generated {:?}, saved to: {:?}",
keypair.identifier(),
key_path.to_string_lossy()
);
}