Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions mm2src/crypto/src/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn encrypt_mnemonic(mnemonic: &str, password: &str) -> MmResult<EncryptedDat
///
/// # Errors
/// This function can return various errors related to decoding, key derivation, encryption, and HMAC verification.
pub fn decrypt_mnemonic(encrypted_data: &EncryptedData, password: &str) -> MmResult<Mnemonic, MnemonicError> {
pub fn decrypt_mnemonic(encrypted_data: &EncryptedData, password: &str) -> MmResult<String, MnemonicError> {
// Re-create the salts from Base64-encoded strings
let (salt_aes, salt_hmac) = match &encrypted_data.key_derivation_details {
KeyDerivationDetails::Argon2 {
Expand All @@ -126,8 +126,7 @@ pub fn decrypt_mnemonic(encrypted_data: &EncryptedData, password: &str) -> MmRes

// Convert decrypted data back to a string
let mnemonic_str = String::from_utf8(decrypted_data).map_to_mm(|e| MnemonicError::DecodeError(e.to_string()))?;
let mnemonic = Mnemonic::parse_normalized(&mnemonic_str)?;
Ok(mnemonic)
Ok(mnemonic_str)
}

#[cfg(any(test, target_arch = "wasm32"))]
Expand All @@ -144,10 +143,23 @@ mod tests {
let mnemonic = "tank abandon bind salon remove wisdom net size aspect direct source fossil";
let password = "password";

// Verify that the mnemonic is valid
let parsed_mnemonic = Mnemonic::parse_normalized(mnemonic);
assert!(parsed_mnemonic.is_ok());
let parsed_mnemonic = parsed_mnemonic.unwrap();
// Encrypt the mnemonic
let encrypted_data = encrypt_mnemonic(mnemonic, password);
assert!(encrypted_data.is_ok());
let encrypted_data = encrypted_data.unwrap();

// Decrypt the mnemonic
let decrypted_mnemonic = decrypt_mnemonic(&encrypted_data, password);
assert!(decrypted_mnemonic.is_ok());
let decrypted_mnemonic = decrypted_mnemonic.unwrap();

// Verify if decrypted mnemonic matches the original
assert_eq!(decrypted_mnemonic, mnemonic);
});

cross_test!(test_encrypt_decrypt_non_bip39_mnemonic, {
let mnemonic = "Helloworld";
let password = "Helloworld";

// Encrypt the mnemonic
let encrypted_data = encrypt_mnemonic(mnemonic, password);
Expand All @@ -160,7 +172,7 @@ mod tests {
let decrypted_mnemonic = decrypted_mnemonic.unwrap();

// Verify if decrypted mnemonic matches the original
assert_eq!(decrypted_mnemonic, parsed_mnemonic);
assert_eq!(decrypted_mnemonic, mnemonic);
});

cross_test!(test_mnemonic_with_last_byte_zero, {
Expand All @@ -173,7 +185,9 @@ mod tests {
let encrypted_data = encrypted_data.unwrap();

// Decrypt the mnemonic
let decrypted_mnemonic = decrypt_mnemonic(&encrypted_data, password);
let decrypted_mnemonic_str = decrypt_mnemonic(&encrypted_data, password);
assert!(decrypted_mnemonic_str.is_ok());
let decrypted_mnemonic = Mnemonic::parse_normalized(&decrypted_mnemonic_str.unwrap());
assert!(decrypted_mnemonic.is_err());

// Verify that the error is due to parsing and not padding
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/src/lp_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ async fn read_and_decrypt_passphrase_if_available(
Some(encrypted_passphrase) => {
let mnemonic = decrypt_mnemonic(&encrypted_passphrase, wallet_password)
.mm_err(|e| ReadPassphraseError::DecryptionError(e.to_string()))?;
Ok(Some(mnemonic.to_string()))
Ok(Some(mnemonic))
},
None => Ok(None),
}
Expand Down Expand Up @@ -214,7 +214,7 @@ async fn decrypt_validate_or_save_passphrase(
wallet_password: &str,
) -> WalletInitResult<Option<String>> {
// Decrypt the provided encrypted passphrase
let decrypted_passphrase = decrypt_mnemonic(&encrypted_passphrase_data, wallet_password)?.to_string();
let decrypted_passphrase = decrypt_mnemonic(&encrypted_passphrase_data, wallet_password)?;

match read_and_decrypt_passphrase_if_available(ctx, wallet_password).await? {
Some(passphrase_from_file) if decrypted_passphrase == passphrase_from_file => {
Expand Down