Skip to content

Commit

Permalink
[GUI][Bug] Fix editing of CS address labels
Browse files Browse the repository at this point in the history
Fixes a bug that prevented the editing of Cold Staking address labels in
the address book due to the passing of an empty purpose, which
`CWallet::ParseIntoAddress()` needs in order to determine if an address
is a cold staking type address.
  • Loading branch information
Fuzzbawls committed May 3, 2020
1 parent e99a18e commit ef83a3d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
4 changes: 1 addition & 3 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,8 @@ bool WalletModel::updateAddressBookLabels(const CTxDestination& dest, const std:
std::map<CTxDestination, AddressBook::CAddressBookData>::iterator mi = wallet->mapAddressBook.find(dest);

// Check if we have a new address or an updated label
if (mi == wallet->mapAddressBook.end()) {
if (mi == wallet->mapAddressBook.end() || mi->second.name != strName) {
return wallet->SetAddressBook(dest, strName, strPurpose);
} else if (mi->second.name != strName) {
return wallet->SetAddressBook(dest, strName, ""); // "" means don't change purpose
}
return false;
}
Expand Down
18 changes: 14 additions & 4 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2880,21 +2880,31 @@ CBitcoinAddress CWallet::ParseIntoAddress(const CTxDestination& dest, const std:
return CBitcoinAddress(dest, addrType);
}

/**
* TODO: If we ever add new functionality that modifies the purpose of an address book entry
* outside of the below situations, we need to also ensure those situations are considered here
*/
bool CWallet::UpdatablePurpose(const std::string& strPurpose1, const std::string& strPurpose2)
{
return (strPurpose1 == AddressBook::AddressBookPurpose::DELEGABLE && strPurpose2 == AddressBook::AddressBookPurpose::DELEGATOR) ||
(strPurpose1 == AddressBook::AddressBookPurpose::DELEGATOR && strPurpose2 == AddressBook::AddressBookPurpose::DELEGABLE);
}

bool CWallet::SetAddressBook(const CTxDestination& address, const std::string& strName, const std::string& strPurpose)
{
bool fUpdated = HasAddressBook(address);
const bool fUpdatePurpose = !fUpdated || UpdatablePurpose(mapAddressBook.at(address).purpose, strPurpose);
{
LOCK(cs_wallet); // mapAddressBook
mapAddressBook[address].name = strName;
if (!strPurpose.empty()) /* update purpose only if requested */
mapAddressBook[address].purpose = strPurpose;
if (!strPurpose.empty() || fUpdatePurpose) mapAddressBook[address].purpose = strPurpose;
}
NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address) != ISMINE_NO,
strPurpose, (fUpdated ? CT_UPDATED : CT_NEW));
if (!fFileBacked)
return false;
std::string addressStr = ParseIntoAddress(address, strPurpose).ToString();
if (!strPurpose.empty() && !CWalletDB(strWalletFile).WritePurpose(addressStr, strPurpose))
std::string addressStr = ParseIntoAddress(address, (strPurpose.empty() && fUpdated) ? mapAddressBook.at(address).purpose : strPurpose).ToString();
if ((!fUpdated || fUpdatePurpose) && !CWalletDB(strWalletFile).WritePurpose(addressStr, strPurpose))
return false;
return CWalletDB(strWalletFile).WriteName(addressStr, strName);
}
Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface

static CBitcoinAddress ParseIntoAddress(const CTxDestination& dest, const std::string& purpose);

bool UpdatablePurpose(const std::string& strPurpose1, const std::string& strPurpose2);
bool SetAddressBook(const CTxDestination& address, const std::string& strName, const std::string& purpose);
bool DelAddressBook(const CTxDestination& address, const CChainParams::Base58Type addrType = CChainParams::PUBKEY_ADDRESS);
bool HasAddressBook(const CTxDestination& address) const;
Expand Down

0 comments on commit ef83a3d

Please sign in to comment.