From 5d31fb3bf490664612f1647f29f18b496f6aae21 Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Thu, 29 Dec 2022 14:21:26 -0500 Subject: [PATCH 1/3] added `from_bytes` for `Wallet` type --- ethers-signers/src/wallet/private_key.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ethers-signers/src/wallet/private_key.rs b/ethers-signers/src/wallet/private_key.rs index cdddf00a4..4926bbc4d 100644 --- a/ethers-signers/src/wallet/private_key.rs +++ b/ethers-signers/src/wallet/private_key.rs @@ -90,13 +90,20 @@ impl Wallet { let address = secret_key_to_address(&signer); Self { signer, address, chain_id: 1 } } + + /// Creates a new Wallet instance from a raw scalar value (big endian). + pub fn from_bytes(bytes: &[u8]) -> Result { + let signer = SigningKey::from_bytes(bytes)?; + let address = secret_key_to_address(&signer); + Ok(Self { signer, address, chain_id: 1 }) + } } impl PartialEq for Wallet { fn eq(&self, other: &Self) -> bool { - self.signer.to_bytes().eq(&other.signer.to_bytes()) && - self.address == other.address && - self.chain_id == other.chain_id + self.signer.to_bytes().eq(&other.signer.to_bytes()) + && self.address == other.address + && self.chain_id == other.chain_id } } From e2a61dac4adaf95d6ded5ebf18d4d7876f4069ab Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Thu, 29 Dec 2022 14:57:54 -0500 Subject: [PATCH 2/3] added key_from_bytes test for Wallet::from_bytes --- ethers-signers/src/wallet/private_key.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ethers-signers/src/wallet/private_key.rs b/ethers-signers/src/wallet/private_key.rs index 4926bbc4d..ed10949c3 100644 --- a/ethers-signers/src/wallet/private_key.rs +++ b/ethers-signers/src/wallet/private_key.rs @@ -101,9 +101,9 @@ impl Wallet { impl PartialEq for Wallet { fn eq(&self, other: &Self) -> bool { - self.signer.to_bytes().eq(&other.signer.to_bytes()) - && self.address == other.address - && self.chain_id == other.chain_id + self.signer.to_bytes().eq(&other.signer.to_bytes()) && + self.address == other.address && + self.chain_id == other.chain_id } } @@ -312,4 +312,17 @@ mod tests { Address::from_str("6813Eb9362372EEF6200f3b1dbC3f819671cBA69").expect("Decoding failed") ); } + + #[test] + fn key_from_bytes() { + let wallet: Wallet = + "0000000000000000000000000000000000000000000000000000000000000001".parse().unwrap(); + + let key_as_bytes = wallet.signer.to_bytes(); + let wallet_from_bytes = Wallet::from_bytes(&key_as_bytes).unwrap(); + + assert_eq!(wallet.address, wallet_from_bytes.address); + assert_eq!(wallet.chain_id, wallet_from_bytes.chain_id); + assert_eq!(wallet.signer, wallet_from_bytes.signer); + } } From 2775fe72a05fae41af464fbffa7ab944ba4064cd Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Thu, 29 Dec 2022 15:02:29 -0500 Subject: [PATCH 3/3] updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a6233634..c5a0d9962 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -275,6 +275,7 @@ - `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) +- [1983](https://github.com/gakonst/ethers-rs/pull/1983) Added a `from_bytes` function for the `Wallet` type. ### 0.6.0