Skip to content

Commit c751d88

Browse files
committed
Wallet: Avoid treating change-in-the-addressbook as non-change everywhere
1 parent 8e64b8c commit c751d88

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

src/interfaces/wallet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class WalletImpl : public Wallet
152152
{
153153
LOCK(m_wallet->cs_wallet);
154154
auto it = m_wallet->m_address_book.find(dest);
155-
if (it == m_wallet->m_address_book.end()) {
155+
if (it == m_wallet->m_address_book.end() || it->second.IsChange()) {
156156
return false;
157157
}
158158
if (name) {
@@ -171,6 +171,7 @@ class WalletImpl : public Wallet
171171
LOCK(m_wallet->cs_wallet);
172172
std::vector<WalletAddress> result;
173173
for (const auto& item : m_wallet->m_address_book) {
174+
if (item.second.IsChange()) continue;
174175
result.emplace_back(item.first, m_wallet->IsMine(item.first), item.second.name, item.second.purpose);
175176
}
176177
return result;

src/qt/addresstablemodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct AddressTableEntryLessThan
5555
static AddressTableEntry::Type translateTransactionType(const QString &strPurpose, bool isMine)
5656
{
5757
AddressTableEntry::Type addressType = AddressTableEntry::Hidden;
58-
// "refund" addresses aren't shown, and change addresses aren't in m_address_book at all.
58+
// "refund" addresses aren't shown, and change addresses aren't returned by getAddresses at all.
5959
if (strPurpose == "send")
6060
addressType = AddressTableEntry::Sending;
6161
else if (strPurpose == "receive")

src/wallet/rpcdump.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ static bool GetWalletAddressesForKey(LegacyScriptPubKeyMan* spk_man, const CWall
6060
CKey key;
6161
spk_man->GetKey(keyid, key);
6262
for (const auto& dest : GetAllDestinationsForKey(key.GetPubKey())) {
63-
if (pwallet->m_address_book.count(dest)) {
63+
const auto* address_book_entry = pwallet->FindAddressBookEntry(dest);
64+
if (address_book_entry) {
6465
if (!strAddr.empty()) {
6566
strAddr += ",";
6667
}
@@ -168,7 +169,7 @@ UniValue importprivkey(const JSONRPCRequest& request)
168169
// label all new addresses, and label existing addresses if a
169170
// label was passed.
170171
for (const auto& dest : GetAllDestinationsForKey(pubkey)) {
171-
if (!request.params[1].isNull() || pwallet->m_address_book.count(dest) == 0) {
172+
if (!request.params[1].isNull() || !pwallet->FindAddressBookEntry(dest)) {
172173
pwallet->SetAddressBook(dest, strLabel, "receive");
173174
}
174175
}

src/wallet/rpcwallet.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,8 @@ static UniValue listaddressgroupings(const JSONRPCRequest& request)
505505
addressInfo.push_back(EncodeDestination(address));
506506
addressInfo.push_back(ValueFromAmount(balances[address]));
507507
{
508-
if (pwallet->m_address_book.find(address) != pwallet->m_address_book.end()) {
508+
const auto* address_book_entry = pwallet->FindAddressBookEntry(address);
509+
if (address_book_entry) {
509510
addressInfo.push_back(pwallet->m_address_book.find(address)->second.name);
510511
}
511512
}
@@ -1112,6 +1113,7 @@ static UniValue ListReceived(interfaces::Chain::Lock& locked_chain, const CWalle
11121113

11131114
for (auto item_it = start; item_it != end; ++item_it)
11141115
{
1116+
if (item_it->second.IsChange()) continue;
11151117
const CTxDestination& address = item_it->first;
11161118
const std::string& label = item_it->second.name;
11171119
auto it = mapTally.find(address);
@@ -1313,7 +1315,8 @@ static void ListTransactions(interfaces::Chain::Lock& locked_chain, const CWalle
13131315
MaybePushAddress(entry, s.destination);
13141316
entry.pushKV("category", "send");
13151317
entry.pushKV("amount", ValueFromAmount(-s.amount));
1316-
if (pwallet->m_address_book.count(s.destination)) {
1318+
const auto* address_book_entry = pwallet->FindAddressBookEntry(s.destination);
1319+
if (address_book_entry) {
13171320
entry.pushKV("label", pwallet->m_address_book.at(s.destination).name);
13181321
}
13191322
entry.pushKV("vout", s.vout);
@@ -1330,7 +1333,8 @@ static void ListTransactions(interfaces::Chain::Lock& locked_chain, const CWalle
13301333
for (const COutputEntry& r : listReceived)
13311334
{
13321335
std::string label;
1333-
if (pwallet->m_address_book.count(r.destination)) {
1336+
const auto* address_book_entry = pwallet->FindAddressBookEntry(r.destination);
1337+
if (address_book_entry) {
13341338
label = pwallet->m_address_book.at(r.destination).name;
13351339
}
13361340
if (filter_label && label != *filter_label) {
@@ -1355,7 +1359,7 @@ static void ListTransactions(interfaces::Chain::Lock& locked_chain, const CWalle
13551359
entry.pushKV("category", "receive");
13561360
}
13571361
entry.pushKV("amount", ValueFromAmount(r.amount));
1358-
if (pwallet->m_address_book.count(r.destination)) {
1362+
if (address_book_entry) {
13591363
entry.pushKV("label", label);
13601364
}
13611365
entry.pushKV("vout", r.vout);
@@ -2955,9 +2959,9 @@ static UniValue listunspent(const JSONRPCRequest& request)
29552959
if (fValidAddress) {
29562960
entry.pushKV("address", EncodeDestination(address));
29572961

2958-
auto i = pwallet->m_address_book.find(address);
2959-
if (i != pwallet->m_address_book.end()) {
2960-
entry.pushKV("label", i->second.name);
2962+
const auto* address_book_entry = pwallet->FindAddressBookEntry(address);
2963+
if (address_book_entry) {
2964+
entry.pushKV("label", address_book_entry->name);
29612965
}
29622966

29632967
std::unique_ptr<SigningProvider> provider = pwallet->GetSolvingProvider(scriptPubKey);
@@ -3814,7 +3818,8 @@ UniValue getaddressinfo(const JSONRPCRequest& request)
38143818
// DEPRECATED: Return label field if existing. Currently only one label can
38153819
// be associated with an address, so the label should be equivalent to the
38163820
// value of the name key/value pair in the labels array below.
3817-
if ((pwallet->chain().rpcEnableDeprecated("label")) && (pwallet->m_address_book.count(dest))) {
3821+
const auto* address_book_entry = pwallet->FindAddressBookEntry(dest);
3822+
if (pwallet->chain().rpcEnableDeprecated("label") && address_book_entry) {
38183823
ret.pushKV("label", pwallet->m_address_book.at(dest).name);
38193824
}
38203825

@@ -3838,14 +3843,13 @@ UniValue getaddressinfo(const JSONRPCRequest& request)
38383843
// stable if we allow multiple labels to be associated with an address in
38393844
// the future.
38403845
UniValue labels(UniValue::VARR);
3841-
std::map<CTxDestination, CAddressBookData>::const_iterator mi = pwallet->m_address_book.find(dest);
3842-
if (mi != pwallet->m_address_book.end()) {
3846+
if (address_book_entry) {
38433847
// DEPRECATED: The previous behavior of returning an array containing a
38443848
// JSON object of `name` and `purpose` key/value pairs is deprecated.
38453849
if (pwallet->chain().rpcEnableDeprecated("labelspurpose")) {
3846-
labels.push_back(AddressBookDataToJSON(mi->second, true));
3850+
labels.push_back(AddressBookDataToJSON(*address_book_entry, true));
38473851
} else {
3848-
labels.push_back(mi->second.name);
3852+
labels.push_back(address_book_entry->name);
38493853
}
38503854
}
38513855
ret.pushKV("labels", std::move(labels));
@@ -3890,6 +3894,7 @@ static UniValue getaddressesbylabel(const JSONRPCRequest& request)
38903894
UniValue ret(UniValue::VOBJ);
38913895
std::set<std::string> addresses;
38923896
for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->m_address_book) {
3897+
if (item.second.IsChange()) continue;
38933898
if (item.second.name == label) {
38943899
std::string address = EncodeDestination(item.first);
38953900
// CWallet::m_address_book is not expected to contain duplicate
@@ -3954,6 +3959,7 @@ static UniValue listlabels(const JSONRPCRequest& request)
39543959
// Add to a set to sort by label name, then insert into Univalue array
39553960
std::set<std::string> label_set;
39563961
for (const std::pair<const CTxDestination, CAddressBookData>& entry : pwallet->m_address_book) {
3962+
if (entry.second.IsChange()) continue;
39573963
if (purpose.empty() || entry.second.purpose == purpose) {
39583964
label_set.insert(entry.second.name);
39593965
}

src/wallet/wallet.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,8 +1237,9 @@ bool CWallet::IsChange(const CScript& script) const
12371237
return true;
12381238

12391239
LOCK(cs_wallet);
1240-
if (!m_address_book.count(address))
1240+
if (!FindAddressBookEntry(address)) {
12411241
return true;
1242+
}
12421243
}
12431244
return false;
12441245
}
@@ -3192,7 +3193,7 @@ bool CWallet::SetAddressBookWithDB(WalletBatch& batch, const CTxDestination& add
31923193
{
31933194
LOCK(cs_wallet);
31943195
std::map<CTxDestination, CAddressBookData>::iterator mi = m_address_book.find(address);
3195-
fUpdated = mi != m_address_book.end();
3196+
fUpdated = (mi != m_address_book.end() && !mi->second.IsChange());
31963197
m_address_book[address].SetLabel(strName);
31973198
if (!strPurpose.empty()) /* update purpose only if requested */
31983199
m_address_book[address].purpose = strPurpose;
@@ -3459,6 +3460,7 @@ std::set<CTxDestination> CWallet::GetLabelAddresses(const std::string& label) co
34593460
std::set<CTxDestination> result;
34603461
for (const std::pair<const CTxDestination, CAddressBookData>& item : m_address_book)
34613462
{
3463+
if (item.second.IsChange()) continue;
34623464
const CTxDestination& address = item.first;
34633465
const std::string& strName = item.second.name;
34643466
if (strName == label)

0 commit comments

Comments
 (0)