Skip to content

Commit

Permalink
[Refactoring][GUI] Remove last remaining pwalletMain calls in the GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Apr 3, 2021
1 parent 93f41dc commit b24260b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
15 changes: 4 additions & 11 deletions src/qt/pivx/settings/settingsbittoolwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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; }");
Expand Down
2 changes: 1 addition & 1 deletion src/qt/pivx/settings/settingssignmessagewidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/qt/walletmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b24260b

Please sign in to comment.