From 9eb16942e2dad7fe33cb017ad93b5029a1488857 Mon Sep 17 00:00:00 2001
From: MAOYM <30402475+mtroym@users.noreply.github.com>
Date: Sun, 3 Mar 2024 09:38:49 +0000
Subject: [PATCH 1/2] encryp wallet with name api.
---
packages/fuels-accounts/src/wallet.rs | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/packages/fuels-accounts/src/wallet.rs b/packages/fuels-accounts/src/wallet.rs
index c8f2618834..b2fc1e7a41 100644
--- a/packages/fuels-accounts/src/wallet.rs
+++ b/packages/fuels-accounts/src/wallet.rs
@@ -171,6 +171,20 @@ impl WalletUnlocked {
.map_err(|e| error!(Other, "{e}"))
}
+ /// Encrypts the wallet's private key with the given password and saves it
+ /// to the given path named with given name.
+ pub fn encrypt_with_name
(&self, dir: P, password: S, name: &str) -> Result
+ where
+ P: AsRef,
+ S: AsRef<[u8]>,
+ {
+ let mut rng = rand::thread_rng();
+
+ eth_keystore::encrypt_key(dir, &mut rng, *self.private_key, password, name)
+ .map_err(|e| error!(Other, "{e}"))
+ }
+
+
/// Recreates a wallet from an encrypted JSON wallet given the provided path and password.
pub fn load_keystore(keypath: P, password: S, provider: Option) -> Result
where
From 473b2fc5c0b5731cf3a0dc62e24c413810505e67 Mon Sep 17 00:00:00 2001
From: MAOYM <30402475+mtroym@users.noreply.github.com>
Date: Sun, 3 Mar 2024 10:00:14 +0000
Subject: [PATCH 2/2] add unitest
---
examples/wallets/src/lib.rs | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/examples/wallets/src/lib.rs b/examples/wallets/src/lib.rs
index e12905797f..9fa345ced3 100644
--- a/examples/wallets/src/lib.rs
+++ b/examples/wallets/src/lib.rs
@@ -115,6 +115,31 @@ mod tests {
Ok(())
}
+
+ #[tokio::test]
+ async fn create_and_store_mnemonic_wallet_with_address_named_keystore() -> Result<()> {
+ // ANCHOR: create_and_store_mnemonic_wallet_with_address_named_keystore
+ use fuels::prelude::*;
+
+ let dir = std::env::temp_dir();
+
+ let phrase =
+ "oblige salon price punch saddle immune slogan rare snap desert retire surprise";
+
+ // Use the test helper to setup a test provider.
+ let provider = setup_test_provider(vec![], vec![], None, None).await?;
+
+ // Create first account from mnemonic phrase.
+ let wallet = WalletUnlocked::new_from_mnemonic_phrase(phrase, Some(provider))?;
+
+ let password = "my_master_password";
+
+ // Encrypts and stores it on disk. Can be recovered using `Wallet::load_keystore`.
+ let _uuid = wallet.encrypt_with_name(&dir, password, wallet.clone().address())?;
+ // ANCHOR_END: create_and_store_mnemonic_wallet_with_address_named_keystore
+ Ok(())
+ }
+
#[tokio::test]
async fn wallet_transfer() -> Result<()> {
// ANCHOR: wallet_transfer