From b24260b857368fd3ff6e2dc28fc13c4de3ff8785 Mon Sep 17 00:00:00 2001 From: random-zebra Date: Sat, 3 Apr 2021 03:15:04 +0200 Subject: [PATCH] [Refactoring][GUI] Remove last remaining pwalletMain calls in the GUI --- src/qt/pivx/settings/settingsbittoolwidget.cpp | 15 ++++----------- .../settings/settingssignmessagewidgets.cpp | 2 +- src/qt/walletmodel.cpp | 18 ++++++++++++++++++ src/qt/walletmodel.h | 3 +++ 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/qt/pivx/settings/settingsbittoolwidget.cpp b/src/qt/pivx/settings/settingsbittoolwidget.cpp index ea192449e0e4d0..bae73e0fc5a11c 100644 --- a/src/qt/pivx/settings/settingsbittoolwidget.cpp +++ b/src/qt/pivx/settings/settingsbittoolwidget.cpp @@ -166,7 +166,7 @@ void SettingsBitToolWidget::onEncryptKeyButtonENCClicked() } CKey key; - if (!pwalletMain->GetKey(*keyID, key)) { + if (!walletModel->getKey(*keyID, key)) { ui->statusLabel_ENC->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_ENC->setText(tr("Private key for the entered address is not available.")); return; @@ -292,27 +292,20 @@ void SettingsBitToolWidget::importAddressFromDecKey() ui->statusLabel_DEC->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_DEC->setText(tr("Please wait while key is imported")); - pwalletMain->MarkDirty(); - pwalletMain->SetAddressBook(vchAddress, "", AddressBook::AddressBookPurpose::RECEIVE); + walletModel->updateAddressBookLabels(vchAddress, "", AddressBook::AddressBookPurpose::RECEIVE); // Don't throw error in case a key is already there - if (pwalletMain->HaveKey(vchAddress)) { + if (walletModel->haveKey(vchAddress)) { ui->statusLabel_DEC->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_DEC->setText(tr("Cannot import address, key already held by the wallet")); return; } - pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = 1; - - if (!pwalletMain->AddKeyPubKey(key, pubkey)) { + if (!walletModel->addKeys(key, pubkey)) { ui->statusLabel_DEC->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_DEC->setText(tr("Error adding key to the wallet")); return; } - - // whenever a key is imported, we need to scan the whole chain - pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value' - pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true); } ui->statusLabel_DEC->setStyleSheet("QLabel { color: green; }"); diff --git a/src/qt/pivx/settings/settingssignmessagewidgets.cpp b/src/qt/pivx/settings/settingssignmessagewidgets.cpp index 671f932851767c..bc3413a8399913 100644 --- a/src/qt/pivx/settings/settingssignmessagewidgets.cpp +++ b/src/qt/pivx/settings/settingssignmessagewidgets.cpp @@ -181,7 +181,7 @@ void SettingsSignMessageWidgets::onSignMessageButtonSMClicked() } CKey key; - if (!pwalletMain->GetKey(*keyID, key)) { + if (!walletModel->getKey(*keyID, key)) { ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_SM->setText(tr("Private key for the entered address is not available.")); return; diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index 5deb5b24e0aae0..c3ebe6106bef07 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -427,6 +427,24 @@ bool WalletModel::updateAddressBookLabels(const CWDestination& dest, const std:: return false; } +bool WalletModel::addKeys(const CKey& key, const CPubKey& pubkey) +{ + { + LOCK(wallet->cs_wallet); + wallet->mapKeyMetadata[pubkey.GetID()].nCreateTime = 1; + + if (!wallet->AddKeyPubKey(key, pubkey)) { + return false; + } + + // whenever a key is imported, we need to scan the whole chain + wallet->nTimeFirstKey = 1; // 0 would be considered 'no value' + } + CBlockIndex* pindexGenesis = WITH_LOCK(cs_main, return chainActive.Genesis(); ); + wallet->ScanForWalletTransactions(pindexGenesis, true); + return true; +} + WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransaction* transaction, const CCoinControl* coinControl, bool fIncludeDelegations) { CAmount total = 0; diff --git a/src/qt/walletmodel.h b/src/qt/walletmodel.h index ba37df10b86801..63974c502fa37e 100644 --- a/src/qt/walletmodel.h +++ b/src/qt/walletmodel.h @@ -291,6 +291,9 @@ class WalletModel : public QObject std::string getLabelForAddress(const CTxDestination& address); QString getSaplingAddressString(const CWalletTx* wtx, const SaplingOutPoint& op) const; bool getKeyId(const CTxDestination& address, CKeyID& keyID); + bool getKey(const CKeyID& keyID, CKey& key) const { return wallet->GetKey(keyID, key); } + bool haveKey(const CKeyID& keyID) const { return wallet->HaveKey(keyID); } + bool addKeys(const CKey& key, const CPubKey& pubkey); bool isMine(const CWDestination& address); bool isMine(const QString& addressStr);