aboutsummaryrefslogtreecommitdiff
path: root/src/qt/addresstablemodel.cpp
diff options
context:
space:
mode:
authorAndrew Chow <github@achow101.com>2023-02-20 17:45:32 -0500
committerAndrew Chow <github@achow101.com>2023-04-11 15:55:31 -0400
commite83babe3b85b22e2360a99f9827b2b0d107ad0fa (patch)
tree67baf57399f7b1c386c64317164a0360976d1451 /src/qt/addresstablemodel.cpp
parent2f8000513675467e62bee9d0f836d2fefcc479af (diff)
downloadbitcoin-e83babe3b85b22e2360a99f9827b2b0d107ad0fa.tar.xz
wallet: Replace use of purpose strings with an enum
Instead of storing and passing around fixed strings for the purpose of an address, use an enum. This also rationalizes the CAddressBookData struct, documenting all fields and making them public, and simplifying the representation to avoid bugs like https://github.com/bitcoin/bitcoin/pull/26761#discussion_r1134615114 and make it not possible to invalid address data like change addresses with labels. Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
Diffstat (limited to 'src/qt/addresstablemodel.cpp')
-rw-r--r--src/qt/addresstablemodel.cpp42
1 files changed, 21 insertions, 21 deletions
diff --git a/src/qt/addresstablemodel.cpp b/src/qt/addresstablemodel.cpp
index e402c51ac4..0d0f1a4d15 100644
--- a/src/qt/addresstablemodel.cpp
+++ b/src/qt/addresstablemodel.cpp
@@ -8,6 +8,7 @@
#include <qt/walletmodel.h>
#include <key_io.h>
+#include <wallet/types.h>
#include <wallet/wallet.h>
#include <algorithm>
@@ -52,17 +53,16 @@ struct AddressTableEntryLessThan
};
/* Determine address type from address purpose */
-static AddressTableEntry::Type translateTransactionType(const QString &strPurpose, bool isMine)
+static AddressTableEntry::Type translateTransactionType(wallet::AddressPurpose purpose, bool isMine)
{
- AddressTableEntry::Type addressType = AddressTableEntry::Hidden;
// "refund" addresses aren't shown, and change addresses aren't returned by getAddresses at all.
- if (strPurpose == "send")
- addressType = AddressTableEntry::Sending;
- else if (strPurpose == "receive")
- addressType = AddressTableEntry::Receiving;
- else if (strPurpose == "unknown" || strPurpose == "") // if purpose not set, guess
- addressType = (isMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending);
- return addressType;
+ switch (purpose) {
+ case wallet::AddressPurpose::SEND: return AddressTableEntry::Sending;
+ case wallet::AddressPurpose::RECEIVE: return AddressTableEntry::Receiving;
+ case wallet::AddressPurpose::REFUND: return AddressTableEntry::Hidden;
+ // No default case to allow for compiler to warn
+ }
+ assert(false);
}
// Private implementation
@@ -85,7 +85,7 @@ public:
continue;
}
AddressTableEntry::Type addressType = translateTransactionType(
- QString::fromStdString(address.purpose), address.is_mine);
+ address.purpose, address.is_mine);
cachedAddressTable.append(AddressTableEntry(addressType,
QString::fromStdString(address.name),
QString::fromStdString(EncodeDestination(address.dest))));
@@ -97,7 +97,7 @@ public:
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, wallet::AddressPurpose purpose, int status)
{
// Find address / label in model
QList<AddressTableEntry>::iterator lower = std::lower_bound(
@@ -239,7 +239,7 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
if(!index.isValid())
return false;
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
- std::string strPurpose = (rec->type == AddressTableEntry::Sending ? "send" : "receive");
+ wallet::AddressPurpose purpose = rec->type == AddressTableEntry::Sending ? wallet::AddressPurpose::SEND : wallet::AddressPurpose::RECEIVE;
editStatus = OK;
if(role == Qt::EditRole)
@@ -253,7 +253,7 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
editStatus = NO_CHANGES;
return false;
}
- walletModel->wallet().setAddressBook(curAddress, value.toString().toStdString(), strPurpose);
+ walletModel->wallet().setAddressBook(curAddress, value.toString().toStdString(), purpose);
} else if(index.column() == Address) {
CTxDestination newAddress = DecodeDestination(value.toString().toStdString());
// Refuse to set invalid address, set error status and return false
@@ -282,7 +282,7 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
// Remove old entry
walletModel->wallet().delAddressBook(curAddress);
// Add new entry with new address
- walletModel->wallet().setAddressBook(newAddress, value.toString().toStdString(), strPurpose);
+ walletModel->wallet().setAddressBook(newAddress, value.toString().toStdString(), purpose);
}
}
return true;
@@ -334,7 +334,7 @@ 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)
+ const QString &label, bool isMine, wallet::AddressPurpose purpose, int status)
{
// Update address book model from Bitcoin core
priv->updateEntry(address, label, isMine, purpose, status);
@@ -365,7 +365,7 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
}
// Add entry
- walletModel->wallet().setAddressBook(DecodeDestination(strAddress), strLabel, "send");
+ walletModel->wallet().setAddressBook(DecodeDestination(strAddress), strLabel, wallet::AddressPurpose::SEND);
}
else if(type == Receive)
{
@@ -416,18 +416,18 @@ QString AddressTableModel::labelForAddress(const QString &address) const
return QString();
}
-QString AddressTableModel::purposeForAddress(const QString &address) const
+std::optional<wallet::AddressPurpose> AddressTableModel::purposeForAddress(const QString &address) const
{
- std::string purpose;
+ wallet::AddressPurpose purpose;
if (getAddressData(address, /* name= */ nullptr, &purpose)) {
- return QString::fromStdString(purpose);
+ return purpose;
}
- return QString();
+ return std::nullopt;
}
bool AddressTableModel::getAddressData(const QString &address,
std::string* name,
- std::string* purpose) const {
+ wallet::AddressPurpose* purpose) const {
CTxDestination destination = DecodeDestination(address.toStdString());
return walletModel->wallet().getAddress(destination, name, /* is_mine= */ nullptr, purpose);
}