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

Commit 0a7e742

Browse files
authored
feat(signers): Allow parsing of private key that has 0x prefix (#2037)
1 parent 927d960 commit 0a7e742

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

Diff for: CHANGELOG.md

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

282284
### 0.6.0
283285

Diff for: ethers-signers/src/wallet/private_key.rs

+61
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,29 @@ impl FromStr for Wallet<SigningKey> {
130130
type Err = WalletError;
131131

132132
fn from_str(src: &str) -> Result<Self, Self::Err> {
133+
let src = src.strip_prefix("0x").or_else(|| src.strip_prefix("0X")).unwrap_or(src);
133134
let src = hex::decode(src)?;
134135
let sk = SigningKey::from_bytes(&src)?;
135136
Ok(sk.into())
136137
}
137138
}
138139

140+
impl TryFrom<&str> for Wallet<SigningKey> {
141+
type Error = WalletError;
142+
143+
fn try_from(value: &str) -> Result<Self, Self::Error> {
144+
value.parse()
145+
}
146+
}
147+
148+
impl TryFrom<String> for Wallet<SigningKey> {
149+
type Error = WalletError;
150+
151+
fn try_from(value: String) -> Result<Self, Self::Error> {
152+
value.parse()
153+
}
154+
}
155+
139156
#[cfg(test)]
140157
#[cfg(not(target_arch = "wasm32"))]
141158
mod tests {
@@ -325,4 +342,48 @@ mod tests {
325342
assert_eq!(wallet.chain_id, wallet_from_bytes.chain_id);
326343
assert_eq!(wallet.signer, wallet_from_bytes.signer);
327344
}
345+
346+
#[test]
347+
fn key_from_str() {
348+
let wallet: Wallet<SigningKey> =
349+
"0000000000000000000000000000000000000000000000000000000000000001".parse().unwrap();
350+
351+
// Check FromStr and `0x`
352+
let wallet_0x: Wallet<SigningKey> =
353+
"0x0000000000000000000000000000000000000000000000000000000000000001".parse().unwrap();
354+
assert_eq!(wallet.address, wallet_0x.address);
355+
assert_eq!(wallet.chain_id, wallet_0x.chain_id);
356+
assert_eq!(wallet.signer, wallet_0x.signer);
357+
358+
// Check FromStr and `0X`
359+
let wallet_0x_cap: Wallet<SigningKey> =
360+
"0X0000000000000000000000000000000000000000000000000000000000000001".parse().unwrap();
361+
assert_eq!(wallet.address, wallet_0x_cap.address);
362+
assert_eq!(wallet.chain_id, wallet_0x_cap.chain_id);
363+
assert_eq!(wallet.signer, wallet_0x_cap.signer);
364+
365+
// Check TryFrom<&str>
366+
let wallet_0x_tryfrom_str: Wallet<SigningKey> =
367+
"0x0000000000000000000000000000000000000000000000000000000000000001"
368+
.try_into()
369+
.unwrap();
370+
assert_eq!(wallet.address, wallet_0x_tryfrom_str.address);
371+
assert_eq!(wallet.chain_id, wallet_0x_tryfrom_str.chain_id);
372+
assert_eq!(wallet.signer, wallet_0x_tryfrom_str.signer);
373+
374+
// Check TryFrom<String>
375+
let wallet_0x_tryfrom_string: Wallet<SigningKey> =
376+
"0x0000000000000000000000000000000000000000000000000000000000000001"
377+
.to_string()
378+
.try_into()
379+
.unwrap();
380+
assert_eq!(wallet.address, wallet_0x_tryfrom_string.address);
381+
assert_eq!(wallet.chain_id, wallet_0x_tryfrom_string.chain_id);
382+
assert_eq!(wallet.signer, wallet_0x_tryfrom_string.signer);
383+
384+
// Must fail because of `0z`
385+
"0z0000000000000000000000000000000000000000000000000000000000000001"
386+
.parse::<Wallet<SigningKey>>()
387+
.unwrap_err();
388+
}
328389
}

0 commit comments

Comments
 (0)