added `from_bytes` for `Wallet` type (#1983)

* added `from_bytes` for `Wallet` type

* added key_from_bytes test for Wallet::from_bytes

* updated changelog
This commit is contained in:
0xKitsune 2022-12-30 07:45:19 -05:00 committed by GitHub
parent 69e0ff7301
commit f94e6f549f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -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

View File

@ -90,6 +90,13 @@ impl Wallet<SigningKey> {
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<Self, WalletError> {
let signer = SigningKey::from_bytes(bytes)?;
let address = secret_key_to_address(&signer);
Ok(Self { signer, address, chain_id: 1 })
}
}
impl PartialEq for Wallet<SigningKey> {
@ -305,4 +312,17 @@ mod tests {
Address::from_str("6813Eb9362372EEF6200f3b1dbC3f819671cBA69").expect("Decoding failed")
);
}
#[test]
fn key_from_bytes() {
let wallet: Wallet<SigningKey> =
"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);
}
}