Skip to content

Commit

Permalink
Make initial encryption setup atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFeickert committed Feb 11, 2023
1 parent f45a443 commit 5fd8c25
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions base_layer/wallet/src/storage/sqlite_db/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,9 +593,23 @@ fn get_db_cipher(
let encrypted_main_key = encrypt_main_key(&secondary_key, &main_key, argon2_params.id)?;

// Store the secondary key version, secondary key salt, and encrypted main key
WalletSettingSql::new(DbKey::SecondaryKeyVersion, argon2_params.id.to_string()).set(&conn)?;
WalletSettingSql::new(DbKey::SecondaryKeySalt, secondary_key_salt.to_string()).set(&conn)?;
WalletSettingSql::new(DbKey::EncryptedMainKey, encrypted_main_key.to_hex()).set(&conn)?;
conn.transaction::<_, Error, _>(|| {
// If any operation fails, trigger a rollback
WalletSettingSql::new(DbKey::SecondaryKeyVersion, argon2_params.id.to_string())
.set(&conn)
.map_err(|_| Error::RollbackTransaction)?;
WalletSettingSql::new(DbKey::SecondaryKeySalt, secondary_key_salt.to_string())
.set(&conn)
.map_err(|_| Error::RollbackTransaction)?;
WalletSettingSql::new(DbKey::EncryptedMainKey, encrypted_main_key.to_hex())
.set(&conn)
.map_err(|_| Error::RollbackTransaction)?;

Ok(())
})
.map_err(|_| {
WalletStorageError::UnexpectedResult("Unable to update database for new password".into())
})?;

// Return the unencrypted main key
main_key
Expand Down

0 comments on commit 5fd8c25

Please sign in to comment.