From 6cd5625787f64b5fab9fc79bd1163fc79eb3a793 Mon Sep 17 00:00:00 2001 From: Rohit Narurkar Date: Fri, 5 Nov 2021 11:28:16 +0100 Subject: [PATCH] (fix): new_keystore returns uuid as well (#559) * (fix): new_keystore returns uuid as well * (chore): add changelog for the changes * (chore): revert fmt change pushed accidentally * (chore): remove 2nd fmt change pushed accidentally --- CHANGELOG.md | 1 + ethers-signers/src/wallet/private_key.rs | 29 ++++++++++++------------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34933a34..2abe6bfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - `abigen!` now supports multiple contracts [#498](https://github.com/gakonst/ethers-rs/pull/498) - Use rust types as contract function inputs for human readable abi [#482](https://github.com/gakonst/ethers-rs/pull/482) - Add EIP-712 `sign_typed_data` signer method; add ethers-core type `Eip712` trait and derive macro in ethers-derive-eip712 [#481](https://github.com/gakonst/ethers-rs/pull/481) +- `LocalWallet::new_keystore` now returns a tuple `(LocalWallet, String)` instead of `LocalWallet`, where the string represents the UUID of the newly created encrypted JSON keystore. The JSON keystore is stored as a file `/dir/uuid`. The issue [#557](https://github.com/gakonst/ethers-rs/issues/557) is addressed [#559](https://github.com/gakonst/ethers-rs/pull/559) ### 0.5.3 diff --git a/ethers-signers/src/wallet/private_key.rs b/ethers-signers/src/wallet/private_key.rs index 88b3b6a6..08a0da93 100644 --- a/ethers-signers/src/wallet/private_key.rs +++ b/ethers-signers/src/wallet/private_key.rs @@ -61,18 +61,23 @@ impl Clone for Wallet { impl Wallet { /// Creates a new random encrypted JSON with the provided password and stores it in the - /// provided directory + /// provided directory. Returns a tuple (Wallet, String) of the wallet instance for the + /// keystore with its random UUID. #[cfg(not(target_arch = "wasm32"))] - pub fn new_keystore(dir: P, rng: &mut R, password: S) -> Result + pub fn new_keystore( + dir: P, + rng: &mut R, + password: S, + ) -> Result<(Self, String), WalletError> where P: AsRef, R: Rng + CryptoRng + rand_core::CryptoRng, S: AsRef<[u8]>, { - let (secret, _) = eth_keystore::new(dir, rng, password)?; + let (secret, uuid) = eth_keystore::new(dir, rng, password)?; let signer = SigningKey::from_bytes(secret.as_slice())?; let address = secret_key_to_address(&signer); - Ok(Self { signer, address, chain_id: 1 }) + Ok((Self { signer, address, chain_id: 1 }, uuid)) } /// Decrypts an encrypted JSON from the provided path to construct a Wallet instance @@ -140,7 +145,6 @@ mod tests { use super::*; use crate::Signer; use ethers_core::types::Address; - use std::fs; use tempfile::tempdir; #[tokio::test] @@ -148,7 +152,7 @@ mod tests { // create and store a random encrypted JSON keystore in this directory let dir = tempdir().unwrap(); let mut rng = rand::thread_rng(); - let key = Wallet::::new_keystore(&dir, &mut rng, "randpsswd").unwrap(); + let (key, uuid) = Wallet::::new_keystore(&dir, &mut rng, "randpsswd").unwrap(); // sign a message using the above key let message = "Some data"; @@ -156,14 +160,11 @@ mod tests { // read from the encrypted JSON keystore and decrypt it, while validating that the // signatures produced by both the keys should match - let paths = fs::read_dir(dir).unwrap(); - for path in paths { - let path = path.unwrap().path(); - let key2 = Wallet::::decrypt_keystore(&path.clone(), "randpsswd").unwrap(); - let signature2 = key2.sign_message(message).await.unwrap(); - assert_eq!(signature, signature2); - assert!(std::fs::remove_file(&path).is_ok()); - } + let path = Path::new(dir.path()).join(uuid); + let key2 = Wallet::::decrypt_keystore(&path.clone(), "randpsswd").unwrap(); + let signature2 = key2.sign_message(message).await.unwrap(); + assert_eq!(signature, signature2); + assert!(std::fs::remove_file(&path).is_ok()); } #[tokio::test]