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: add check for old db encryption and provide warning #3549

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1 +1,2 @@
-- This file should undo anything in `up.sql`
ALTER TABLE key_manager_states
RENAME COLUMN seed TO master_key;
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ALTER TABLE main.key_manager_states
ALTER TABLE key_manager_states
RENAME COLUMN master_key TO seed;
2 changes: 1 addition & 1 deletion base_layer/wallet/src/storage/sqlite_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ fn check_db_encryption_status(
/// A Sql version of the wallet setting key-value table
#[derive(Clone, Debug, Queryable, Insertable, PartialEq)]
#[table_name = "wallet_settings"]
struct WalletSettingSql {
pub(crate) struct WalletSettingSql {
key: String,
value: String,
}
Expand Down
25 changes: 24 additions & 1 deletion base_layer/wallet/src/storage/sqlite_utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
storage::{database::WalletDatabase, sqlite_db::WalletSqliteDatabase},
transaction_service::storage::sqlite_db::TransactionServiceSqliteDatabase,
};
use diesel::{Connection, SqliteConnection};
use diesel::{Connection, ExpressionMethods, QueryDsl, SqliteConnection};
use fs2::FileExt;
use log::*;
use std::{
Expand Down Expand Up @@ -69,6 +69,8 @@ pub fn run_migration_and_create_sqlite_connection<P: AsRef<Path>>(
let connection = SqliteConnection::establish(path_str)?;
connection.execute("PRAGMA foreign_keys = ON; PRAGMA busy_timeout = 60000;")?;

check_for_incompatible_db_encryption(&connection)?;

embed_migrations!("./migrations");
embedded_migrations::run(&connection)
.map_err(|err| WalletStorageError::DatabaseMigrationError(format!("Database migration failed {}", err)))?;
Expand Down Expand Up @@ -163,3 +165,24 @@ pub fn initialize_sqlite_database_backends(
contacts_backend,
))
}

/// This method detects if the database contains the old incompatable encryption data and errors rather than breaking
/// the DB
/// TODO remove at next testnet reset
fn check_for_incompatible_db_encryption(connection: &SqliteConnection) -> Result<(), WalletStorageError> {
use crate::{diesel::RunQueryDsl, schema::wallet_settings, storage::sqlite_db::WalletSettingSql};

if wallet_settings::table
.filter(wallet_settings::key.eq("MasterSecretKey".to_string()))
.first::<WalletSettingSql>(connection)
.is_ok()
{
return Err(WalletStorageError::AeadError(
"This wallet database is incompatible with the new form of encryption. Halting to preserve this database \
structure. Revert to a version of tari_console_wallet prior to 0.13.0"
.to_string(),
));
}

Ok(())
}