From 997b74b36221abbcb7f107eca0b78eaccb6aea87 Mon Sep 17 00:00:00 2001 From: Philip Robinson Date: Wed, 26 Jan 2022 16:40:00 +0200 Subject: [PATCH] fix: properly decrypt imported faux tx when reading from db (#3754) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description --- The function that read the imported transaction out of the database didn’t check if the data needed to be decrypted before deserialization. This PR fixes that. How Has This Been Tested? --- test provided --- .../transaction_service/storage/sqlite_db.rs | 7 ++- .../transaction_service_tests/storage.rs | 47 ++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/base_layer/wallet/src/transaction_service/storage/sqlite_db.rs b/base_layer/wallet/src/transaction_service/storage/sqlite_db.rs index ade8345f95..7eff7dbedd 100644 --- a/base_layer/wallet/src/transaction_service/storage/sqlite_db.rs +++ b/base_layer/wallet/src/transaction_service/storage/sqlite_db.rs @@ -1217,7 +1217,12 @@ impl TransactionBackend for TransactionServiceSqliteDatabase { let conn = self.database_connection.get_pooled_connection()?; CompletedTransactionSql::index_by_status_and_cancelled(TransactionStatus::Imported, false, &conn)? .into_iter() - .map(|ct| CompletedTransaction::try_from(ct).map_err(TransactionStorageError::from)) + .map(|mut ct: CompletedTransactionSql| { + if let Err(e) = self.decrypt_if_necessary(&mut ct) { + return Err(e); + } + CompletedTransaction::try_from(ct).map_err(TransactionStorageError::from) + }) .collect::, TransactionStorageError>>() } } diff --git a/base_layer/wallet/tests/transaction_service_tests/storage.rs b/base_layer/wallet/tests/transaction_service_tests/storage.rs index 601461c956..d9e616dac0 100644 --- a/base_layer/wallet/tests/transaction_service_tests/storage.rs +++ b/base_layer/wallet/tests/transaction_service_tests/storage.rs @@ -52,7 +52,7 @@ use tari_wallet::{ storage::sqlite_utilities::run_migration_and_create_sqlite_connection, test_utils::create_consensus_constants, transaction_service::storage::{ - database::{TransactionBackend, TransactionDatabase}, + database::{DbKeyValuePair, TransactionBackend, TransactionDatabase, WriteOperation}, models::{CompletedTransaction, InboundTransaction, OutboundTransaction, WalletTransaction}, sqlite_db::TransactionServiceSqliteDatabase, }, @@ -583,3 +583,48 @@ pub fn test_transaction_service_sqlite_db_encrypted() { test_db_backend(TransactionServiceSqliteDatabase::new(connection, Some(cipher))); } + +#[tokio::test] +async fn import_tx_and_read_it_from_db() { + let db_name = format!("{}.sqlite3", random::string(8)); + let db_tempdir = tempdir().unwrap(); + let db_folder = db_tempdir.path().to_str().unwrap().to_string(); + let db_path = format!("{}/{}", db_folder, db_name); + let connection = run_migration_and_create_sqlite_connection(&db_path, 16).unwrap(); + + let key = GenericArray::from_slice(b"an example very very secret key."); + let cipher = Aes256Gcm::new(key); + let sqlite_db = TransactionServiceSqliteDatabase::new(connection, Some(cipher)); + + let transaction = CompletedTransaction::new( + TxId::from(1), + PublicKey::default(), + PublicKey::default(), + MicroTari::from(100000), + MicroTari::from(0), + Transaction::new( + Vec::new(), + Vec::new(), + Vec::new(), + PrivateKey::default(), + PrivateKey::default(), + ), + TransactionStatus::Imported, + "message".to_string(), + Utc::now().naive_utc(), + TransactionDirection::Inbound, + Some(0), + ); + + sqlite_db + .write(WriteOperation::Insert(DbKeyValuePair::CompletedTransaction( + TxId::from(1), + Box::new(transaction), + ))) + .unwrap(); + + let db_tx = sqlite_db.fetch_imported_transactions().unwrap(); + + assert_eq!(db_tx.len(), 1); + assert_eq!(db_tx.first().unwrap().tx_id, TxId::from(1)); +}