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")
.takes_value(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();
@ -55,8 +63,53 @@ fn main() {
"data_dir" => &config.data_dir.to_str());
match matches.subcommand() {
("generate", Some(_gen_m)) => {
let keypair = Keypair::random();
("generate", Some(_)) => generate_random(&config, &log),
("generate_deterministic", Some(m)) => {
if let Some(string) = m.value_of("validator index") {
let i: usize = string.parse().expect("Invalid validator index");
if let Some(string) = m.value_of("end validator index") {
let j: usize = string.parse().expect("Invalid end validator index");
let indices: Vec<usize> = (i..j).collect();
generate_deterministic_multiple(&indices, &config, &log)
} else {
generate_deterministic(i, &config, &log)
}
}
}
_ => panic!(
"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.");
@ -66,26 +119,4 @@ fn main() {
keypair.identifier(),
key_path.to_string_lossy()
);
}
("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!(
"The account manager must be run with a subcommand. See help for more information."
),
}
}