feat(signers): Allow parsing of private key that has `0x` prefix (#2037)

This commit is contained in:
Yiannis M 2023-01-12 05:41:41 +02:00 committed by GitHub
parent 927d960829
commit 0a7e7423e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 0 deletions

View File

@ -278,6 +278,8 @@
- `eth-keystore-rs` crate updated. Allow an optional name for the to-be-generated - `eth-keystore-rs` crate updated. Allow an optional name for the to-be-generated
keystore file [#910](https://github.com/gakonst/ethers-rs/pull/910) keystore file [#910](https://github.com/gakonst/ethers-rs/pull/910)
- [1983](https://github.com/gakonst/ethers-rs/pull/1983) Added a `from_bytes` function for the `Wallet` type. - [1983](https://github.com/gakonst/ethers-rs/pull/1983) Added a `from_bytes` function for the `Wallet` type.
- Allow parsing of private key that has `0x` prefix
[#2037](https://github.com/gakonst/ethers-rs/pull/2037)
### 0.6.0 ### 0.6.0

View File

@ -130,12 +130,29 @@ impl FromStr for Wallet<SigningKey> {
type Err = WalletError; type Err = WalletError;
fn from_str(src: &str) -> Result<Self, Self::Err> { 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)?; let src = hex::decode(src)?;
let sk = SigningKey::from_bytes(&src)?; let sk = SigningKey::from_bytes(&src)?;
Ok(sk.into()) Ok(sk.into())
} }
} }
impl TryFrom<&str> for Wallet<SigningKey> {
type Error = WalletError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
value.parse()
}
}
impl TryFrom<String> for Wallet<SigningKey> {
type Error = WalletError;
fn try_from(value: String) -> Result<Self, Self::Error> {
value.parse()
}
}
#[cfg(test)] #[cfg(test)]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
mod tests { mod tests {
@ -325,4 +342,48 @@ mod tests {
assert_eq!(wallet.chain_id, wallet_from_bytes.chain_id); assert_eq!(wallet.chain_id, wallet_from_bytes.chain_id);
assert_eq!(wallet.signer, wallet_from_bytes.signer); assert_eq!(wallet.signer, wallet_from_bytes.signer);
} }
#[test]
fn key_from_str() {
let wallet: Wallet<SigningKey> =
"0000000000000000000000000000000000000000000000000000000000000001".parse().unwrap();
// Check FromStr and `0x`
let wallet_0x: Wallet<SigningKey> =
"0x0000000000000000000000000000000000000000000000000000000000000001".parse().unwrap();
assert_eq!(wallet.address, wallet_0x.address);
assert_eq!(wallet.chain_id, wallet_0x.chain_id);
assert_eq!(wallet.signer, wallet_0x.signer);
// Check FromStr and `0X`
let wallet_0x_cap: Wallet<SigningKey> =
"0X0000000000000000000000000000000000000000000000000000000000000001".parse().unwrap();
assert_eq!(wallet.address, wallet_0x_cap.address);
assert_eq!(wallet.chain_id, wallet_0x_cap.chain_id);
assert_eq!(wallet.signer, wallet_0x_cap.signer);
// Check TryFrom<&str>
let wallet_0x_tryfrom_str: Wallet<SigningKey> =
"0x0000000000000000000000000000000000000000000000000000000000000001"
.try_into()
.unwrap();
assert_eq!(wallet.address, wallet_0x_tryfrom_str.address);
assert_eq!(wallet.chain_id, wallet_0x_tryfrom_str.chain_id);
assert_eq!(wallet.signer, wallet_0x_tryfrom_str.signer);
// Check TryFrom<String>
let wallet_0x_tryfrom_string: Wallet<SigningKey> =
"0x0000000000000000000000000000000000000000000000000000000000000001"
.to_string()
.try_into()
.unwrap();
assert_eq!(wallet.address, wallet_0x_tryfrom_string.address);
assert_eq!(wallet.chain_id, wallet_0x_tryfrom_string.chain_id);
assert_eq!(wallet.signer, wallet_0x_tryfrom_string.signer);
// Must fail because of `0z`
"0z0000000000000000000000000000000000000000000000000000000000000001"
.parse::<Wallet<SigningKey>>()
.unwrap_err();
}
} }