Skip to content

Commit

Permalink
fix: properly decrypt imported faux tx when reading from db (#3754)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philipr-za authored Jan 26, 2022
1 parent dfdaf4b commit 997b74b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Result<Vec<CompletedTransaction>, TransactionStorageError>>()
}
}
Expand Down
47 changes: 46 additions & 1 deletion base_layer/wallet/tests/transaction_service_tests/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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));
}

0 comments on commit 997b74b

Please sign in to comment.