Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix: don't panic if hex str too short (#2363)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Apr 24, 2023
1 parent fdec0bc commit d64e630
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion ethers-signers/src/wallet/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ impl FromStr for Wallet<SigningKey> {
fn from_str(src: &str) -> Result<Self, Self::Err> {
let src = src.strip_prefix("0x").or_else(|| src.strip_prefix("0X")).unwrap_or(src);
let src = hex::decode(src)?;

if src.len() != 32 {
return Err(WalletError::HexError(hex::FromHexError::InvalidStringLength))
}

let sk = SigningKey::from_bytes(src.as_slice().into())?;
Ok(sk.into())
}
Expand All @@ -157,7 +162,7 @@ impl TryFrom<String> for Wallet<SigningKey> {
#[cfg(not(target_arch = "wasm32"))]
mod tests {
use super::*;
use crate::Signer;
use crate::{LocalWallet, Signer};
use ethers_core::types::Address;
use tempfile::tempdir;

Expand All @@ -167,6 +172,17 @@ mod tests {
let _pk: Wallet<SigningKey> = s.parse().unwrap();
}

#[test]
fn parse_short_key() {
let s = "6f142508b4eea641e33cb2a0161221105086a84584c74245ca463a49effea3";
assert!(s.len() < 64);
let pk = s.parse::<LocalWallet>().unwrap_err();
match pk {
WalletError::HexError(hex::FromHexError::InvalidStringLength) => {}
_ => panic!("Unexpected error"),
}
}

#[tokio::test]
async fn encrypted_json_keystore() {
// create and store a random encrypted JSON keystore in this directory
Expand Down

0 comments on commit d64e630

Please sign in to comment.