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

fix: don't panic if hex str too short #2363

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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