Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly decrypt imported faux tx when reading from db #3754

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
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));
}