From 16e0a4b4127d99a5a038b6196e0385975b4a3d94 Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Thu, 18 Feb 2021 00:07:14 -0700 Subject: [PATCH] CLI: Make derived address seed.len() check a clap validator --- clap-utils/src/input_validators.rs | 17 ++++++++++++++++- cli/src/cli.rs | 9 ++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/clap-utils/src/input_validators.rs b/clap-utils/src/input_validators.rs index 5727200170..dceaccacb4 100644 --- a/clap-utils/src/input_validators.rs +++ b/clap-utils/src/input_validators.rs @@ -3,7 +3,7 @@ use chrono::DateTime; use solana_sdk::{ clock::{Epoch, Slot}, hash::Hash, - pubkey::Pubkey, + pubkey::{Pubkey, MAX_SEED_LEN}, signature::{read_keypair_file, Signature}, }; use std::fmt::Display; @@ -291,6 +291,21 @@ where .map(|_| ()) } +pub fn is_derived_address_seed(value: T) -> Result<(), String> +where + T: AsRef + Display, +{ + let value = value.as_ref(); + if value.len() > MAX_SEED_LEN { + Err(format!( + "Address seed must not be longer than {} bytes", + MAX_SEED_LEN + )) + } else { + Ok(()) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/cli/src/cli.rs b/cli/src/cli.rs index 179bb4757f..a97ff779fe 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -40,7 +40,7 @@ use solana_sdk::{ hash::Hash, instruction::InstructionError, message::Message, - pubkey::{Pubkey, MAX_SEED_LEN}, + pubkey::Pubkey, signature::{Signature, Signer, SignerError}, system_instruction::{self, SystemError}, system_program, @@ -923,12 +923,6 @@ pub fn parse_create_address_with_seed( let seed = matches.value_of("seed").unwrap().to_string(); - if seed.len() > MAX_SEED_LEN { - return Err(CliError::BadParameter( - "Address seed must not be longer than 32 bytes".to_string(), - )); - } - Ok(CliCommandInfo { command: CliCommand::CreateAddressWithSeed { from_pubkey, @@ -1971,6 +1965,7 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, ' .value_name("SEED_STRING") .takes_value(true) .required(true) + .validator(is_derived_address_seed) .help("The seed. Must not take more than 32 bytes to encode as utf-8"), ) .arg(