Skip to content

Commit

Permalink
chore(cli): validate hex key provided to wallet create --key
Browse files Browse the repository at this point in the history
  • Loading branch information
mickvandijke authored and joshuef committed Jul 12, 2024
1 parent 0677189 commit 1379c33
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions sn_cli/src/bin/subcommands/wallet/hot_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::{
};
use crate::get_stdin_response;

use autonomi::utils::is_valid_key_hex;
use bls::SecretKey;
use clap::Parser;
use color_eyre::{
Expand Down Expand Up @@ -176,6 +177,13 @@ pub(crate) async fn wallet_cmds_without_client(cmds: &WalletCmds, root_dir: &Pat
"Only one of `--key` or `--derivation` may be specified"
));
}
if let Some(key) = key {
// Check if key is valid
// Doing this early to avoid stashing an existing wallet while the provided key is invalid
if !is_valid_key_hex(key) {
return Err(eyre!("Please provide a valid secret key in hex format. It must be 64 characters long."));
}
}
// Check for existing wallet
if let Ok(existing_wallet) = WalletApiHelper::load_from(root_dir) {
let balance = existing_wallet.balance();
Expand Down
1 change: 1 addition & 0 deletions sn_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

mod acc_packet;
mod files;
pub mod utils;

pub use acc_packet::AccountPacket;
pub use files::{
Expand Down
4 changes: 4 additions & 0 deletions sn_cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// Returns whether a hex string is a valid secret key in hex format.
pub fn is_valid_key_hex(hex: &str) -> bool {
hex.len() == 64 && hex.chars().all(|c| c.is_ascii_hexdigit())
}

0 comments on commit 1379c33

Please sign in to comment.