Skip to content

Commit

Permalink
qt: update address warnings when wallet is encrypted
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Apr 4, 2023
1 parent 7f799b5 commit e4e7466
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 31 deletions.
9 changes: 5 additions & 4 deletions src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,11 @@ class Wallet

//! Register handler for address book changed messages.
using AddressBookChangedFn = std::function<void(const CTxDestination& address,
const std::string& label,
bool is_mine,
const std::string& purpose,
ChangeType status)>;
const std::string& label,
bool is_mine,
const std::string& purpose,
ChangeType status,
bool is_active)>;
virtual std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) = 0;

//! Register handler for transaction changed messages.
Expand Down
14 changes: 9 additions & 5 deletions src/qt/addresstablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class AddressTablePriv
std::sort(cachedAddressTable.begin(), cachedAddressTable.end(), AddressTableEntryLessThan());
}

void updateEntry(const QString &address, const QString &label, bool isMine, const QString &purpose, int status)
void updateEntry(const QString& address, const QString& label, bool isMine, const QString& purpose, int status, bool isActive)
{
// Find address / label in model
QList<AddressTableEntry>::iterator lower = std::lower_bound(
Expand All @@ -130,7 +130,7 @@ class AddressTablePriv
break;
}
parent->beginInsertRows(QModelIndex(), lowerIndex, lowerIndex);
cachedAddressTable.insert(lowerIndex, AddressTableEntry(newEntryType, label, address, /*TODO:*/ false));
cachedAddressTable.insert(lowerIndex, AddressTableEntry(newEntryType, label, address, isActive));
parent->endInsertRows();
break;
case CT_UPDATED:
Expand Down Expand Up @@ -357,11 +357,15 @@ QModelIndex AddressTableModel::index(int row, int column, const QModelIndex &par
}
}

void AddressTableModel::updateEntry(const QString &address,
const QString &label, bool isMine, const QString &purpose, int status)
void AddressTableModel::updateEntry(const QString& address,
const QString& label,
bool isMine,
const QString& purpose,
int status,
bool isActive)
{
// Update address book model from Bitcoin core
priv->updateEntry(address, label, isMine, purpose, status);
priv->updateEntry(address, label, isMine, purpose, status, isActive);
}

QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address, const OutputType address_type)
Expand Down
2 changes: 1 addition & 1 deletion src/qt/addresstablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class AddressTableModel : public QAbstractTableModel
public Q_SLOTS:
/* Update address list from core.
*/
void updateEntry(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
void updateEntry(const QString& address, const QString& label, bool isMine, const QString& purpose, int status, bool isActive);

friend class AddressTablePriv;
};
Expand Down
3 changes: 3 additions & 0 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,9 @@ void BitcoinGUI::updateWalletStatus()
WalletModel * const walletModel = walletView->getWalletModel();
setEncryptionStatus(walletModel->getEncryptionStatus());
setHDStatus(walletModel->wallet().privateKeysDisabled(), walletModel->wallet().hdEnabled());

// Refresh requests table and address book to show warnings on old unencrypted keys
walletView->refreshAddressTables();
}
#endif // ENABLE_WALLET

Expand Down
2 changes: 1 addition & 1 deletion src/qt/signverifymessagedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void SignVerifyMessageDialog::on_addressBookButton_SM_clicked()
{
if (model && model->getAddressTableModel())
{
model->refresh(platformStyle, /*pk_hash_only=*/true);
model->RefreshAddressTableModel(platformStyle, /*pk_hash_only=*/true);
AddressBookPage dlg(platformStyle, AddressBookPage::ForSelection, AddressBookPage::ReceivingTab, this);
dlg.setModel(model->getAddressTableModel());
if (dlg.exec())
Expand Down
42 changes: 28 additions & 14 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,15 @@ void WalletModel::updateTransaction()
fForceCheckBalanceChanged = true;
}

void WalletModel::updateAddressBook(const QString &address, const QString &label,
bool isMine, const QString &purpose, int status)
void WalletModel::updateAddressBook(const QString& address,
const QString& label,
bool isMine,
const QString& purpose,
int status,
bool isActive)
{
if(addressTableModel)
addressTableModel->updateEntry(address, label, isMine, purpose, status);
addressTableModel->updateEntry(address, label, isMine, purpose, status, isActive);
}

void WalletModel::updateWatchOnlyFlag(bool fHaveWatchonly)
Expand Down Expand Up @@ -375,21 +379,26 @@ static void NotifyKeyStoreStatusChanged(WalletModel *walletmodel)
assert(invoked);
}

static void NotifyAddressBookChanged(WalletModel *walletmodel,
const CTxDestination &address, const std::string &label, bool isMine,
const std::string &purpose, ChangeType status)
static void NotifyAddressBookChanged(WalletModel* walletmodel,
const CTxDestination& address,
const std::string& label,
bool isMine,
const std::string& purpose,
ChangeType status,
bool isActive)
{
QString strAddress = QString::fromStdString(EncodeDestination(address));
QString strLabel = QString::fromStdString(label);
QString strPurpose = QString::fromStdString(purpose);

qDebug() << "NotifyAddressBookChanged: " + strAddress + " " + strLabel + " isMine=" + QString::number(isMine) + " purpose=" + strPurpose + " status=" + QString::number(status);
qDebug() << "NotifyAddressBookChanged: " + strAddress + " " + strLabel + " isMine=" + QString::number(isMine) + " purpose=" + strPurpose + " status=" + QString::number(status) + " isActive=" + QString::number(isActive);
bool invoked = QMetaObject::invokeMethod(walletmodel, "updateAddressBook",
Q_ARG(QString, strAddress),
Q_ARG(QString, strLabel),
Q_ARG(bool, isMine),
Q_ARG(QString, strPurpose),
Q_ARG(int, status));
Q_ARG(QString, strAddress),
Q_ARG(QString, strLabel),
Q_ARG(bool, isMine),
Q_ARG(QString, strPurpose),
Q_ARG(int, status),
Q_ARG(bool, isActive));
assert(invoked);
}

Expand Down Expand Up @@ -428,7 +437,7 @@ void WalletModel::subscribeToCoreSignals()
// Connect signals to wallet
m_handler_unload = m_wallet->handleUnload(std::bind(&NotifyUnload, this));
m_handler_status_changed = m_wallet->handleStatusChanged(std::bind(&NotifyKeyStoreStatusChanged, this));
m_handler_address_book_changed = m_wallet->handleAddressBookChanged(std::bind(NotifyAddressBookChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
m_handler_address_book_changed = m_wallet->handleAddressBookChanged(std::bind(NotifyAddressBookChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6));
m_handler_transaction_changed = m_wallet->handleTransactionChanged(std::bind(NotifyTransactionChanged, this, std::placeholders::_1, std::placeholders::_2));
m_handler_show_progress = m_wallet->handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2));
m_handler_watch_only_changed = m_wallet->handleWatchOnlyChanged(std::bind(NotifyWatchonlyChanged, this, std::placeholders::_1));
Expand Down Expand Up @@ -601,11 +610,16 @@ bool WalletModel::isMultiwallet() const
return m_node.walletLoader().getWallets().size() > 1;
}

void WalletModel::refresh(const PlatformStyle* platformStyle, bool pk_hash_only)
void WalletModel::RefreshAddressTableModel(const PlatformStyle* platformStyle, bool pk_hash_only)
{
addressTableModel = new AddressTableModel(platformStyle, this, pk_hash_only);
}

void WalletModel::RefreshRecentRequestsTableModel(const PlatformStyle* platformStyle)
{
recentRequestsTableModel = new RecentRequestsTableModel(platformStyle, this);
}

uint256 WalletModel::getLastBlockProcessed() const
{
return m_client_model ? m_client_model->getBestBlockHash() : uint256{};
Expand Down
5 changes: 3 additions & 2 deletions src/qt/walletmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ class WalletModel : public QObject

bool isMultiwallet() const;

void refresh(const PlatformStyle* platformStyle, bool pk_hash_only = false);
void RefreshAddressTableModel(const PlatformStyle* platformStyle, bool pk_hash_only = false);
void RefreshRecentRequestsTableModel(const PlatformStyle* platformStyle);

uint256 getLastBlockProcessed() const;

Expand Down Expand Up @@ -236,7 +237,7 @@ public Q_SLOTS:
/* New transaction, or transaction changed status */
void updateTransaction();
/* New, updated or removed address book entry */
void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
void updateAddressBook(const QString& address, const QString& label, bool isMine, const QString& purpose, int status, bool isActive);
/* Watch-only added */
void updateWatchOnlyFlag(bool fHaveWatchonly);
/* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
Expand Down
8 changes: 8 additions & 0 deletions src/qt/walletview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ void WalletView::showOutOfSyncWarning(bool fShow)
overviewPage->showOutOfSyncWarning(fShow);
}

void WalletView::refreshAddressTables()
{
walletModel->RefreshRecentRequestsTableModel(platformStyle);
receiveCoinsPage->setModel(walletModel);
walletModel->RefreshAddressTableModel(platformStyle);
usedReceivingAddressesPage->setModel(walletModel->getAddressTableModel());
}

void WalletView::encryptWallet()
{
auto dlg = new AskPassphraseDialog(AskPassphraseDialog::Encrypt, this);
Expand Down
1 change: 1 addition & 0 deletions src/qt/walletview.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class WalletView : public QStackedWidget
bool handlePaymentRequest(const SendCoinsRecipient& recipient);

void showOutOfSyncWarning(bool fShow);
void refreshAddressTables();

private:
ClientModel* clientModel{nullptr};
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ class WalletImpl : public Wallet
{
return MakeSignalHandler(m_wallet->NotifyAddressBookChanged.connect(
[fn](const CTxDestination& address, const std::string& label, bool is_mine,
const std::string& purpose, ChangeType status) { fn(address, label, is_mine, purpose, status); }));
const std::string& purpose, ChangeType status, bool is_active) { fn(address, label, is_mine, purpose, status, is_active); }));
}
std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) override
{
Expand Down
6 changes: 4 additions & 2 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2403,6 +2403,7 @@ bool CWallet::SetAddressBookWithDB(WalletBatch& batch, const CTxDestination& add
{
bool fUpdated = false;
bool is_mine;
bool is_active = false;
{
LOCK(cs_wallet);
std::map<CTxDestination, CAddressBookData>::iterator mi = m_address_book.find(address);
Expand All @@ -2411,9 +2412,10 @@ bool CWallet::SetAddressBookWithDB(WalletBatch& batch, const CTxDestination& add
if (!strPurpose.empty()) /* update purpose only if requested */
m_address_book[address].purpose = strPurpose;
is_mine = IsMine(address) != ISMINE_NO;
is_active = IsDestinationActive(address);
}
NotifyAddressBookChanged(address, strName, is_mine,
strPurpose, (fUpdated ? CT_UPDATED : CT_NEW));
strPurpose, (fUpdated ? CT_UPDATED : CT_NEW), is_active);
if (!strPurpose.empty() && !batch.WritePurpose(EncodeDestination(address), strPurpose))
return false;
return batch.WriteName(EncodeDestination(address), strName);
Expand Down Expand Up @@ -2448,7 +2450,7 @@ bool CWallet::DelAddressBook(const CTxDestination& address)
is_mine = IsMine(address) != ISMINE_NO;
}

NotifyAddressBookChanged(address, "", is_mine, "", CT_DELETED);
NotifyAddressBookChanged(address, "", is_mine, "", CT_DELETED, false);

batch.ErasePurpose(EncodeDestination(address));
return batch.EraseName(EncodeDestination(address));
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
*/
boost::signals2::signal<void(const CTxDestination& address,
const std::string& label, bool isMine,
const std::string& purpose, ChangeType status)>
const std::string& purpose, ChangeType status,
bool isActive)>
NotifyAddressBookChanged;

/**
Expand Down

0 comments on commit e4e7466

Please sign in to comment.