Skip to content

Commit

Permalink
Final migration from CBitcoinAddress to the destionation wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Jun 29, 2020
1 parent 0724bbb commit 548f828
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 56 deletions.
11 changes: 4 additions & 7 deletions src/qt/paymentserver.cpp
Expand Up @@ -193,11 +193,9 @@ void PaymentServer::ipcParseCommandLine(int argc, char* argv[])

SendCoinsRecipient r;
if (GUIUtil::parseBitcoinURI(arg, &r) && !r.address.isEmpty()) {
CBitcoinAddress address(r.address.toStdString());

if (address.IsValid(Params(CBaseChainParams::MAIN))) {
if (IsValidDestinationString(r.address.toStdString(), false, Params(CBaseChainParams::MAIN))) {
SelectParams(CBaseChainParams::MAIN);
} else if (address.IsValid(Params(CBaseChainParams::TESTNET))) {
} else if (IsValidDestinationString(r.address.toStdString(), false, Params(CBaseChainParams::TESTNET))) {
SelectParams(CBaseChainParams::TESTNET);
}
}
Expand Down Expand Up @@ -385,8 +383,7 @@ void PaymentServer::handleURIOrFile(const QString& s)
{
SendCoinsRecipient recipient;
if (GUIUtil::parseBitcoinURI(s, &recipient)) {
CBitcoinAddress address(recipient.address.toStdString());
if (!address.IsValid()) {
if (!IsValidDestinationString(recipient.address.toStdString())) {
Q_EMIT message(tr("URI handling"), tr("Invalid payment address %1").arg(recipient.address),
CClientUIInterface::MSG_ERROR);
} else
Expand Down Expand Up @@ -505,7 +502,7 @@ bool PaymentServer::processPaymentRequest(PaymentRequestPlus& request, SendCoins
CTxDestination dest;
if (ExtractDestination(sendingTo.first, dest)) {
// Append destination address
addresses.append(QString::fromStdString(CBitcoinAddress(dest).ToString()));
addresses.append(QString::fromStdString(EncodeDestination(dest)));
} else if (!recipient.authenticatedMerchant.isEmpty()) {
// Insecure payments to custom pivx addresses are not supported
// (there is no good way to tell the user where they are paying in a way
Expand Down
33 changes: 18 additions & 15 deletions src/qt/walletmodel.cpp
Expand Up @@ -578,10 +578,11 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(WalletModelTransaction& tran
Q_FOREACH (const SendCoinsRecipient& rcp, transaction.getRecipients()) {
// Don't touch the address book when we have a payment request
if (!rcp.paymentRequest.IsInitialized()) {
CBitcoinAddress address = CBitcoinAddress(rcp.address.toStdString());
std::string purpose = address.IsStakingAddress() ? AddressBook::AddressBookPurpose::COLD_STAKING_SEND : AddressBook::AddressBookPurpose::SEND;
bool isStaking = false;
CTxDestination address = DecodeDestination(rcp.address.toStdString(), isStaking);
std::string purpose = isStaking ? AddressBook::AddressBookPurpose::COLD_STAKING_SEND : AddressBook::AddressBookPurpose::SEND;
std::string strLabel = rcp.label.toStdString();
updateAddressBookLabels(address.Get(), strLabel, purpose);
updateAddressBookLabels(address, strLabel, purpose);
}
Q_EMIT coinsSent(wallet, rcp, transaction_array);
}
Expand Down Expand Up @@ -699,7 +700,7 @@ static void NotifyKeyStoreStatusChanged(WalletModel* walletmodel, CCryptoKeyStor

static void NotifyAddressBookChanged(WalletModel* walletmodel, CWallet* wallet, const CTxDestination& address, const std::string& label, bool isMine, const std::string& purpose, ChangeType status)
{
QString strAddress = QString::fromStdString(pwalletMain->ParseIntoAddress(address, purpose).ToString());
QString strAddress = QString::fromStdString(pwalletMain->ParseIntoAddress(address, purpose));
QString strLabel = QString::fromStdString(label);
QString strPurpose = QString::fromStdString(purpose);

Expand Down Expand Up @@ -879,32 +880,35 @@ bool WalletModel::blacklistAddressFromColdStaking(const QString &addressStr)

bool WalletModel::updateAddressBookPurpose(const QString &addressStr, const std::string& purpose)
{
CBitcoinAddress address(addressStr.toStdString());
if (address.IsStakingAddress())
bool isStaking = false;
CTxDestination address = DecodeDestination(addressStr.toStdString(), isStaking);
if (isStaking)
return error("Invalid PIVX address, cold staking address");
CKeyID keyID;
if (!getKeyId(address, keyID))
return false;
return pwalletMain->SetAddressBook(keyID, getLabelForAddress(address), purpose);
}

bool WalletModel::getKeyId(const CBitcoinAddress& address, CKeyID& keyID)
bool WalletModel::getKeyId(const CTxDestination& address, CKeyID& keyID)
{
if (!address.IsValid())
if (!IsValidDestination(address))
return error("Invalid PIVX address");

if (!address.GetKeyID(keyID))
const CKeyID* inKeyID = boost::get<CKeyID>(&address);
if (!inKeyID)
return error("Unable to get KeyID from PIVX address");

keyID = *inKeyID;
return true;
}

std::string WalletModel::getLabelForAddress(const CBitcoinAddress& address)
std::string WalletModel::getLabelForAddress(const CTxDestination& address)
{
std::string label = "";
{
LOCK(wallet->cs_wallet);
std::map<CTxDestination, AddressBook::CAddressBookData>::iterator mi = wallet->mapAddressBook.find(address.Get());
std::map<CTxDestination, AddressBook::CAddressBookData>::iterator mi = wallet->mapAddressBook.find(address);
if (mi != wallet->mapAddressBook.end()) {
label = mi->second.name;
}
Expand Down Expand Up @@ -980,7 +984,7 @@ void WalletModel::listCoins(std::map<QString, std::vector<COutput> >& mapCoins)
CTxDestination address;
if (!out.fSpendable || !ExtractDestination(cout.tx->vout[cout.i].scriptPubKey, address))
continue;
mapCoins[QString::fromStdString(CBitcoinAddress(address).ToString())].push_back(out);
mapCoins[QString::fromStdString(EncodeDestination(address))].push_back(out);
}
}

Expand Down Expand Up @@ -1039,11 +1043,10 @@ bool WalletModel::isMine(const CTxDestination& address)

bool WalletModel::isMine(const QString& addressStr)
{
CBitcoinAddress address(addressStr.toStdString());
return IsMine(*wallet, address.Get());
return IsMine(*wallet, DecodeDestination(addressStr.toStdString()));
}

bool WalletModel::isUsed(CBitcoinAddress address)
bool WalletModel::isUsed(CTxDestination address)
{
return wallet->IsUsed(address);
}
6 changes: 3 additions & 3 deletions src/qt/walletmodel.h
Expand Up @@ -263,12 +263,12 @@ class WalletModel : public QObject
bool whitelistAddressFromColdStaking(const QString &addressStr);
bool blacklistAddressFromColdStaking(const QString &address);
bool updateAddressBookPurpose(const QString &addressStr, const std::string& purpose);
std::string getLabelForAddress(const CBitcoinAddress& address);
bool getKeyId(const CBitcoinAddress& address, CKeyID& keyID);
std::string getLabelForAddress(const CTxDestination& address);
bool getKeyId(const CTxDestination& address, CKeyID& keyID);

bool isMine(const CTxDestination& address);
bool isMine(const QString& addressStr);
bool isUsed(CBitcoinAddress address);
bool isUsed(CTxDestination address);
void getOutputs(const std::vector<COutPoint>& vOutpoints, std::vector<COutput>& vOutputs);
bool getMNCollateralCandidate(COutPoint& outPoint);
bool isSpent(const COutPoint& outpoint) const;
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/blockchain.cpp
Expand Up @@ -1238,7 +1238,7 @@ UniValue getserials(const UniValue& params, bool fHelp) {
if (!ExtractDestinations(tx.vout[0].scriptPubKey, type, addresses, nRequired)) {
spentTo = strprintf("type: %d", GetTxnOutputType(type));
} else {
spentTo = CBitcoinAddress(addresses[0]).ToString();
spentTo = EncodeDestination(addresses[0]);
}
}
}
Expand Down
41 changes: 20 additions & 21 deletions src/wallet/wallet.cpp
Expand Up @@ -141,12 +141,12 @@ int64_t CWallet::GetKeyCreationTime(CPubKey pubkey)
return mapKeyMetadata[pubkey.GetID()].nCreateTime;
}

int64_t CWallet::GetKeyCreationTime(const CBitcoinAddress& address)
int64_t CWallet::GetKeyCreationTime(const CTxDestination& address)
{
CKeyID keyID;
if (address.GetKeyID(keyID)) {
const CKeyID* keyID = boost::get<CKeyID>(&address);
if (keyID) {
CPubKey keyRet;
if (GetPubKey(keyID, keyRet)) {
if (GetPubKey(*keyID, keyRet)) {
return GetKeyCreationTime(keyRet);
}
}
Expand Down Expand Up @@ -681,15 +681,14 @@ bool CWallet::GetVinAndKeysFromOutput(COutput out, CTxIn& txinRet, CPubKey& pubK

CTxDestination address1;
ExtractDestination(pubScript, address1, fColdStake);
CBitcoinAddress address2(address1);

CKeyID keyID;
if (!address2.GetKeyID(keyID)) {
CKeyID* keyID = boost::get<CKeyID>(&address1);
if (!keyID) {
LogPrintf("CWallet::GetVinAndKeysFromOutput -- Address does not refer to a key\n");
return false;
}

if (!GetKey(keyID, keyRet)) {
if (!GetKey(*keyID, keyRet)) {
LogPrintf("CWallet::GetVinAndKeysFromOutput -- Private key for address is not known\n");
return false;
}
Expand Down Expand Up @@ -1098,10 +1097,10 @@ isminetype CWallet::IsMine(const CTxIn& txin) const
return ISMINE_NO;
}

bool CWallet::IsUsed(const CBitcoinAddress address) const
bool CWallet::IsUsed(const CTxDestination address) const
{
LOCK(cs_wallet);
CScript scriptPubKey = GetScriptForDestination(address.Get());
CScript scriptPubKey = GetScriptForDestination(address);
if (!::IsMine(*this, scriptPubKey)) {
return false;
}
Expand Down Expand Up @@ -2091,7 +2090,7 @@ bool CWallet::AvailableCoins(std::vector<COutput>* pCoins, // --> populates
}
}

std::map<CBitcoinAddress, std::vector<COutput> > CWallet::AvailableCoinsByAddress(bool fConfirmed, CAmount maxCoinValue)
std::map<CTxDestination , std::vector<COutput> > CWallet::AvailableCoinsByAddress(bool fConfirmed, CAmount maxCoinValue)
{
std::vector<COutput> vCoins;
// include cold
Expand All @@ -2102,8 +2101,8 @@ std::map<CBitcoinAddress, std::vector<COutput> > CWallet::AvailableCoinsByAddres
ALL_COINS, // coin type
fConfirmed); // only confirmed

std::map<CBitcoinAddress, std::vector<COutput> > mapCoins;
for (COutput out : vCoins) {
std::map<CTxDestination, std::vector<COutput> > mapCoins;
for (COutput& out : vCoins) {
if (maxCoinValue > 0 && out.tx->vout[out.i].nValue > maxCoinValue)
continue;

Expand All @@ -2117,7 +2116,7 @@ std::map<CBitcoinAddress, std::vector<COutput> > CWallet::AvailableCoinsByAddres
continue;
}

mapCoins[CBitcoinAddress(address, fColdStakeAddr ? CChainParams::STAKING_ADDRESS : CChainParams::PUBKEY_ADDRESS)].push_back(out);
mapCoins[address].push_back(out);
}

return mapCoins;
Expand Down Expand Up @@ -3026,7 +3025,7 @@ DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx>& vWtx)
return DB_LOAD_OK;
}

CBitcoinAddress CWallet::ParseIntoAddress(const CTxDestination& dest, const std::string& purpose) {
std::string CWallet::ParseIntoAddress(const CTxDestination& dest, const std::string& purpose) {
const CChainParams::Base58Type addrType =
AddressBook::IsColdStakingPurpose(purpose) ?
CChainParams::STAKING_ADDRESS : CChainParams::PUBKEY_ADDRESS;
Expand All @@ -3046,7 +3045,7 @@ bool CWallet::SetAddressBook(const CTxDestination& address, const std::string& s
mapAddressBook.at(address).purpose, (fUpdated ? CT_UPDATED : CT_NEW));
if (!fFileBacked)
return false;
std::string addressStr = ParseIntoAddress(address, mapAddressBook.at(address).purpose).ToString();
std::string addressStr = ParseIntoAddress(address, mapAddressBook.at(address).purpose);
if (!strPurpose.empty() && !CWalletDB(strWalletFile).WritePurpose(addressStr, strPurpose))
return false;
return CWalletDB(strWalletFile).WriteName(addressStr, strName);
Expand Down Expand Up @@ -3488,7 +3487,7 @@ bool CWallet::AddDestData(const CTxDestination& dest, const std::string& key, co
mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
if (!fFileBacked)
return true;
return CWalletDB(strWalletFile).WriteDestData(CBitcoinAddress(dest).ToString(), key, value);
return CWalletDB(strWalletFile).WriteDestData(EncodeDestination(dest), key, value);
}

bool CWallet::EraseDestData(const CTxDestination& dest, const std::string& key)
Expand All @@ -3497,7 +3496,7 @@ bool CWallet::EraseDestData(const CTxDestination& dest, const std::string& key)
return false;
if (!fFileBacked)
return true;
return CWalletDB(strWalletFile).EraseDestData(CBitcoinAddress(dest).ToString(), key);
return CWalletDB(strWalletFile).EraseDestData(EncodeDestination(dest), key);
}

bool CWallet::LoadDestData(const CTxDestination& dest, const std::string& key, const std::string& value)
Expand All @@ -3514,10 +3513,10 @@ void CWallet::AutoCombineDust()
return;
}

std::map<CBitcoinAddress, std::vector<COutput> > mapCoinsByAddress = AvailableCoinsByAddress(true, nAutoCombineThreshold * COIN);
std::map<CTxDestination, std::vector<COutput> > mapCoinsByAddress = AvailableCoinsByAddress(true, nAutoCombineThreshold * COIN);

//coins are sectioned by address. This combination code only wants to combine inputs that belong to the same address
for (std::map<CBitcoinAddress, std::vector<COutput> >::iterator it = mapCoinsByAddress.begin(); it != mapCoinsByAddress.end(); it++) {
for (std::map<CTxDestination, std::vector<COutput> >::iterator it = mapCoinsByAddress.begin(); it != mapCoinsByAddress.end(); it++) {
std::vector<COutput> vCoins, vRewardCoins;
bool maxSize = false;
vCoins = it->second;
Expand Down Expand Up @@ -3566,7 +3565,7 @@ void CWallet::AutoCombineDust()
continue;

std::vector<CRecipient> vecSend;
CScript scriptPubKey = GetScriptForDestination(it->first.Get());
CScript scriptPubKey = GetScriptForDestination(it->first);
vecSend.push_back(CRecipient{scriptPubKey, nTotalRewardsValue, false});

//Send change to same address
Expand Down
12 changes: 6 additions & 6 deletions src/wallet/wallet.h
Expand Up @@ -362,7 +362,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
//! >> Available coins (P2CS)
void GetAvailableP2CSCoins(std::vector<COutput>& vCoins) const;

std::map<CBitcoinAddress, std::vector<COutput> > AvailableCoinsByAddress(bool fConfirmed = true, CAmount maxCoinValue = 0);
std::map<CTxDestination, std::vector<COutput> > AvailableCoinsByAddress(bool fConfirmed = true, CAmount maxCoinValue = 0);

/// Get 10000 PIV output and keys which can be used for the Masternode
bool GetMasternodeVinAndKeys(CTxIn& txinRet, CPubKey& pubKeyRet, CKey& keyRet, std::string strTxHash = "", std::string strOutputIndex = "");
Expand All @@ -383,7 +383,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
PairResult getNewAddress(CTxDestination& ret, std::string label);
PairResult getNewStakingAddress(CTxDestination& ret, std::string label);
int64_t GetKeyCreationTime(CPubKey pubkey);
int64_t GetKeyCreationTime(const CBitcoinAddress& address);
int64_t GetKeyCreationTime(const CTxDestination& address);

//! Adds a key to the store, and saves it to disk.
bool AddKeyPubKey(const CKey& key, const CPubKey& pubkey);
Expand Down Expand Up @@ -529,7 +529,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
bool GetBudgetSystemCollateralTX(CWalletTx& tx, uint256 hash, bool useIX);
bool GetBudgetFinalizationCollateralTX(CWalletTx& tx, uint256 hash, bool useIX); // Only used for budget finalization

bool IsUsed(const CBitcoinAddress address) const;
bool IsUsed(const CTxDestination address) const;

isminetype IsMine(const CTxIn& txin) const;
CAmount GetDebit(const CTxIn& txin, const isminefilter& filter) const;
Expand All @@ -548,7 +548,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
DBErrors LoadWallet(bool& fFirstRunRet);
DBErrors ZapWalletTx(std::vector<CWalletTx>& vWtx);

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

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);
Expand Down Expand Up @@ -618,7 +618,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
const CCoinControl* coinControl = NULL);

// - ZC PublicSpends
bool SpendZerocoin(CAmount nAmount, CWalletTx& wtxNew, CZerocoinSpendReceipt& receipt, std::vector<CZerocoinMint>& vMintsSelected, std::list<std::pair<CTxDestination,CAmount>> addressesTo, CBitcoinAddress* changeAddress = nullptr);
bool SpendZerocoin(CAmount nAmount, CWalletTx& wtxNew, CZerocoinSpendReceipt& receipt, std::vector<CZerocoinMint>& vMintsSelected, std::list<std::pair<CTxDestination,CAmount>> addressesTo, CTxDestination* changeAddress = nullptr);
bool MintsToInputVectorPublicSpend(std::map<CBigNum, CZerocoinMint>& mapMintsSelected, const uint256& hashTxOut, std::vector<CTxIn>& vin, CZerocoinSpendReceipt& receipt, libzerocoin::SpendType spendType, CBlockIndex* pindexCheckpoint = nullptr);
bool CreateZCPublicSpendTransaction(
CAmount nValue,
Expand All @@ -628,7 +628,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
std::vector<CZerocoinMint>& vSelectedMints,
std::vector<CDeterministicMint>& vNewMints,
std::list<std::pair<CTxDestination,CAmount>> addressesTo,
CBitcoinAddress* changeAddress = nullptr);
CTxDestination* changeAddress = nullptr);

// - ZC Balances
CAmount GetZerocoinBalance(bool fMatureOnly) const;
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/wallet_zerocoin.cpp
Expand Up @@ -293,7 +293,7 @@ bool CWallet::CreateZerocoinMintTransaction(const CAmount nValue,
// - ZC PublicSpends

bool CWallet::SpendZerocoin(CAmount nAmount, CWalletTx& wtxNew, CZerocoinSpendReceipt& receipt, std::vector<CZerocoinMint>& vMintsSelected,
std::list<std::pair<CTxDestination, CAmount>> addressesTo, CBitcoinAddress* changeAddress)
std::list<std::pair<CTxDestination, CAmount>> addressesTo, CTxDestination* changeAddress)
{
// Default: assume something goes wrong. Depending on the problem this gets more specific below
int nStatus = ZPIV_SPEND_ERROR;
Expand Down Expand Up @@ -454,7 +454,7 @@ bool CWallet::CreateZCPublicSpendTransaction(
std::vector<CZerocoinMint>& vSelectedMints,
std::vector<CDeterministicMint>& vNewMints,
std::list<std::pair<CTxDestination,CAmount>> addressesTo,
CBitcoinAddress* changeAddress)
CTxDestination * changeAddress)
{
// Check available funds
int nStatus = ZPIV_TRX_FUNDS_PROBLEMS;
Expand Down Expand Up @@ -622,7 +622,7 @@ bool CWallet::CreateZCPublicSpendTransaction(
CScript scriptChange;
// Change address
if(changeAddress){
scriptChange = GetScriptForDestination(changeAddress->Get());
scriptChange = GetScriptForDestination(*changeAddress);
} else {
// Reserve a new key pair from key pool
CPubKey vchPubKey;
Expand Down

0 comments on commit 548f828

Please sign in to comment.