Skip to content

Commit 2397dbe

Browse files
committed
gui: Use wallet name for wallet migration rather than WalletModel
To prepare for migrating wallets that are not loaded, when migration occurs in the GUI, it should not rely on a WalletModel existing.
1 parent 377dca8 commit 2397dbe

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

src/qt/askpassphrasedialog.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ AskPassphraseDialog::AskPassphraseDialog(Mode _mode, QWidget *parent, SecureStri
4444
ui->passEdit1->hide();
4545
setWindowTitle(tr("Encrypt wallet"));
4646
break;
47+
case UnlockMigration:
4748
case Unlock: // Ask passphrase
4849
ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
4950
ui->passLabel2->hide();
@@ -80,7 +81,7 @@ void AskPassphraseDialog::setModel(WalletModel *_model)
8081
void AskPassphraseDialog::accept()
8182
{
8283
SecureString oldpass, newpass1, newpass2;
83-
if (!model && mode != Encrypt)
84+
if (!model && mode != Encrypt && mode != UnlockMigration)
8485
return;
8586
oldpass.reserve(MAX_PASSPHRASE_SIZE);
8687
newpass1.reserve(MAX_PASSPHRASE_SIZE);
@@ -181,6 +182,10 @@ void AskPassphraseDialog::accept()
181182
QMessageBox::critical(this, tr("Wallet unlock failed"), e.what());
182183
}
183184
break;
185+
case UnlockMigration:
186+
Assume(m_passphrase_out)->assign(oldpass);
187+
QDialog::accept();
188+
break;
184189
case ChangePass:
185190
if(newpass1 == newpass2)
186191
{
@@ -224,6 +229,7 @@ void AskPassphraseDialog::textChanged()
224229
case Encrypt: // New passphrase x2
225230
acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
226231
break;
232+
case UnlockMigration:
227233
case Unlock: // Old passphrase x1
228234
acceptable = !ui->passEdit1->text().isEmpty();
229235
break;

src/qt/askpassphrasedialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class AskPassphraseDialog : public QDialog
2626
Encrypt, /**< Ask passphrase twice and encrypt */
2727
Unlock, /**< Ask passphrase and unlock */
2828
ChangePass, /**< Ask old passphrase + new passphrase twice */
29+
UnlockMigration, // Ask passphrase for unlocking during migration
2930
};
3031

3132
explicit AskPassphraseDialog(Mode mode, QWidget *parent, SecureString* passphrase_out = nullptr);

src/qt/bitcoingui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ void BitcoinGUI::createActions()
459459
connect(m_migrate_wallet_action, &QAction::triggered, [this] {
460460
auto activity = new MigrateWalletActivity(m_wallet_controller, this);
461461
connect(activity, &MigrateWalletActivity::migrated, this, &BitcoinGUI::setCurrentWallet);
462-
activity->migrate(walletFrame->currentWalletModel());
462+
activity->migrate(walletFrame->currentWalletModel()->wallet().getWalletName());
463463
});
464464
connect(m_mask_values_action, &QAction::toggled, this, &BitcoinGUI::setPrivacy);
465465
connect(m_mask_values_action, &QAction::toggled, this, &BitcoinGUI::enableHistoryAction);

src/qt/walletcontroller.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wal
170170
return wallet_model;
171171
}
172172

173+
WalletModel* WalletController::getWallet(const std::string& name)
174+
{
175+
auto it = std::find_if(m_wallets.begin(), m_wallets.end(),
176+
[&name](WalletModel* model) {
177+
return name == model->wallet().getWalletName();
178+
});
179+
return it == m_wallets.end() ? nullptr : *it;
180+
}
181+
173182
void WalletController::removeAndDeleteWallet(WalletModel* wallet_model)
174183
{
175184
// Unregister wallet model.
@@ -435,34 +444,37 @@ void RestoreWalletActivity::finish()
435444
Q_EMIT finished();
436445
}
437446

438-
void MigrateWalletActivity::migrate(WalletModel* wallet_model)
447+
void MigrateWalletActivity::migrate(const std::string& name)
439448
{
440449
// Warn the user about migration
441450
QMessageBox box(m_parent_widget);
442451
box.setWindowTitle(tr("Migrate wallet"));
443-
box.setText(tr("Are you sure you wish to migrate the wallet <i>%1</i>?").arg(GUIUtil::HtmlEscape(wallet_model->getDisplayName())));
452+
box.setText(tr("Are you sure you wish to migrate the wallet <i>%1</i>?").arg(GUIUtil::HtmlEscape(GUIUtil::WalletDisplayName(name))));
444453
box.setInformativeText(tr("Migrating the wallet will convert this wallet to one or more descriptor wallets. A new wallet backup will need to be made.\n"
445454
"If this wallet contains any watchonly scripts, a new wallet will be created which contains those watchonly scripts.\n"
446455
"If this wallet contains any solvable but not watched scripts, a different and new wallet will be created which contains those scripts.\n\n"
447456
"The migration process will create a backup of the wallet before migrating. This backup file will be named "
448457
"<wallet name>-<timestamp>.legacy.bak and can be found in the directory for this wallet. In the event of "
449-
"an incorrect migration, the backup can be restored with the \"Restore Wallet\" functionality."));
450-
box.setStandardButtons(QMessageBox::Yes|QMessageBox::Cancel);
451-
box.setDefaultButton(QMessageBox::Yes);
452-
if (box.exec() != QMessageBox::Yes) return;
458+
"an incorrect migration, the backup can be restored with the \"Restore Wallet\" functionality.\n\n"
459+
"If your wallet is encrypted, please provide the passphrase now as by choosing \"Yes, encrypted\"."));
460+
QPushButton* yes_enc_button = box.addButton(tr("Yes, encrypted"), QMessageBox::YesRole);
461+
QPushButton* yes_button = box.addButton(QMessageBox::Yes);
462+
QPushButton* cancel_button = box.addButton(QMessageBox::Cancel);
463+
box.setDefaultButton(yes_button);
464+
box.exec();
465+
if (!box.clickedButton() || box.clickedButton() == (QAbstractButton*)cancel_button) return;
453466

454-
// Get the passphrase if it is encrypted regardless of it is locked or unlocked. We need the passphrase itself.
455467
SecureString passphrase;
456-
WalletModel::EncryptionStatus enc_status = wallet_model->getEncryptionStatus();
457-
if (enc_status == WalletModel::EncryptionStatus::Locked || enc_status == WalletModel::EncryptionStatus::Unlocked) {
458-
AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, m_parent_widget, &passphrase);
459-
dlg.setModel(wallet_model);
460-
dlg.exec();
468+
if (box.clickedButton() == (QAbstractButton*)yes_enc_button) {
469+
// Get the passphrase for the wallet
470+
AskPassphraseDialog dlg(AskPassphraseDialog::UnlockMigration, m_parent_widget, &passphrase);
471+
if (dlg.exec() == QDialog::Rejected) return;
461472
}
462473

463474
// GUI needs to remove the wallet so that it can actually be unloaded by migration
464-
const std::string name = wallet_model->wallet().getWalletName();
465-
m_wallet_controller->removeAndDeleteWallet(wallet_model);
475+
if (WalletModel* wallet = m_wallet_controller->getWallet(name)) {
476+
m_wallet_controller->removeAndDeleteWallet(wallet);
477+
};
466478

467479
showProgressDialog(tr("Migrate Wallet"), tr("Migrating Wallet <b>%1</b>…").arg(GUIUtil::HtmlEscape(name)));
468480

src/qt/walletcontroller.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class WalletController : public QObject
5858
~WalletController();
5959

6060
WalletModel* getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet);
61+
WalletModel* getWallet(const std::string& name);
6162

6263
//! Returns all wallet names in the wallet dir mapped to whether the wallet
6364
//! is loaded.
@@ -66,8 +67,6 @@ class WalletController : public QObject
6667
void closeWallet(WalletModel* wallet_model, QWidget* parent = nullptr);
6768
void closeAllWallets(QWidget* parent = nullptr);
6869

69-
void migrateWallet(WalletModel* wallet_model, QWidget* parent = nullptr);
70-
7170
Q_SIGNALS:
7271
void walletAdded(WalletModel* wallet_model);
7372
void walletRemoved(WalletModel* wallet_model);
@@ -186,7 +185,7 @@ class MigrateWalletActivity : public WalletControllerActivity
186185
public:
187186
MigrateWalletActivity(WalletController* wallet_controller, QWidget* parent) : WalletControllerActivity(wallet_controller, parent) {}
188187

189-
void migrate(WalletModel* wallet_model);
188+
void migrate(const std::string& path);
190189

191190
Q_SIGNALS:
192191
void migrated(WalletModel* wallet_model);

0 commit comments

Comments
 (0)