diff --git a/nym-wallet/src-tauri/src/wallet_storage/mod.rs b/nym-wallet/src-tauri/src/wallet_storage/mod.rs index 092fe6dc83..862e23e682 100644 --- a/nym-wallet/src-tauri/src/wallet_storage/mod.rs +++ b/nym-wallet/src-tauri/src/wallet_storage/mod.rs @@ -153,6 +153,7 @@ pub(crate) fn remove_wallet_login_information_at_file( mod tests { use super::*; use config::defaults::COSMOS_DERIVATION_PATH; + use std::str::FromStr; use tempfile::tempdir; // I'm not 100% sure how to feel about having to touch the file system at all @@ -308,4 +309,38 @@ mod tests { // The file should now be removed assert!(!wallet_file.exists()); } + + #[test] + fn decrypt_stored_wallet() { + const SAVED_WALLET: &str = "src/wallet_storage/test-data/saved-wallet.json"; + let wallet_file = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(SAVED_WALLET); + + let wallet = load_existing_wallet_at_file(wallet_file).unwrap(); + + let cosmos_hd_path: DerivationPath = COSMOS_DERIVATION_PATH.parse().unwrap(); + let password = UserPassword::new("password".to_string()); + let bad_password = UserPassword::new("bad-password".to_string()); + let id1 = WalletAccountId::new("first".to_string()); + let id2 = WalletAccountId::new("second".to_string()); + + assert!(!wallet.password_can_decrypt_all(&bad_password)); + assert!(wallet.password_can_decrypt_all(&password)); + + let account1 = wallet.decrypt_account(&id1, &password).unwrap(); + let account2 = wallet.decrypt_account(&id2, &password).unwrap(); + + let expected_account1 = bip39::Mnemonic::from_str("country mean universe text phone begin deputy reject result good cram illness common cluster proud swamp digital patrol spread bar face december base kick").unwrap(); + let expected_account2 = bip39::Mnemonic::from_str("home mansion start quiz dress decide hint second dragon sunny juice always steak real minimum art rival skin draw total pulp foot goddess agent").unwrap(); + + assert_eq!(account1.mnemonic(), &expected_account1); + assert_eq!(account2.mnemonic(), &expected_account2); + + let StoredAccount::Mnemonic(ref mnemonic1) = account1; + assert_eq!(mnemonic1.mnemonic(), &expected_account1); + assert_eq!(mnemonic1.hd_path(), &cosmos_hd_path); + + let StoredAccount::Mnemonic(ref mnemonic2) = account2; + assert_eq!(mnemonic2.mnemonic(), &expected_account2); + assert_eq!(mnemonic2.hd_path(), &cosmos_hd_path); + } } diff --git a/nym-wallet/src-tauri/src/wallet_storage/test-data/saved-wallet.json b/nym-wallet/src-tauri/src/wallet_storage/test-data/saved-wallet.json new file mode 100644 index 0000000000..2cf0288fbc --- /dev/null +++ b/nym-wallet/src-tauri/src/wallet_storage/test-data/saved-wallet.json @@ -0,0 +1,21 @@ +{ + "version": 1, + "accounts": [ + { + "id": "first", + "account": { + "ciphertext": "icnpxLmr/H7FIIOaEf7DYNLuM6uhh7poEppXpYCllQD33TjY+8eLtVvhEQmjX60IQeFOd+1JCcrHa2B12vlBAYlfM4gBxA6d2ZJ8+Dw/vNvBNyChiyUx2euV3vPGOs22r/XDBsmEeF40XZcXftQZa2kzYaPnkbP+eiMOIWkcY4FYOEHwx5SxT4VBPZIrVTC3iDalJLWybVbbw/Bc2zbzEXI1ckg4Ccydj95SMil9BiyDpALfZqwlai7I97S+BjmcVxSCsYqFjTkRUHVMjrEr7fWHKU4DIOM=", + "salt": "CtnbfkxTybqz0U4cPHW2jQ==", + "iv": "77ZROU6dAMttEWwS" + } + }, + { + "id": "second", + "account": { + "ciphertext": "nsqZHdQFlskglc5izKgnr8sBwdMmd82h2Rnjdos9EUca3cqkUdFYEjZDsK8OGR3GZ9alLTNt/1U97Rvvr2HPAWbzl23FW2YXaLTA6yj6ZwQK5w0MYE061NYbcxNHuzT9f5aQWkGULAk4RWb5t8eUX7y/NdJr3tA5xuGOLhooTfBB98/4RpupDsYGZp1DPC/GMFppOA3GmKs9bacZm805Bhfq5mwhXab1SjJQpFHMHisCMhxo/oLqulKML1tQMetBdqDTjJmPpdUnd1mi", + "salt": "J2TMLjKv4dkZ/kXso9FGhg==", + "iv": "CTqqoMa4LetvBKCP" + } + } + ] +} \ No newline at end of file