您还可以使用JSON 钱包 来管理一个钱包,该钱包被安全加密并存储在磁盘上。这样可以更轻松地管理多个钱包,特别是用于测试目的。
您可以创建一个随机钱包,并在同一时间加密和存储它。然后,稍后,如果您知道主密码,可以恢复钱包:
use fuels::prelude::*;
let dir = std::env::temp_dir();
let mut rng = rand::thread_rng();
// Use the test helper to setup a test provider.
let provider = setup_test_provider(vec![], vec![], None, None).await?;
let password = "my_master_password";
// Create a wallet to be stored in the keystore.
let (_wallet, uuid) =
WalletUnlocked::new_from_keystore(&dir, &mut rng, password, Some(provider.clone()))?;
let path = dir.join(uuid);
let _recovered_wallet = WalletUnlocked::load_keystore(path, password, Some(provider))?;
如果您已经使用助记词短语或私钥创建了钱包,您也可以将其加密并保存到磁盘上:
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(&dir, password)?;