Skip to content

Commit

Permalink
CBitcoinAddress to wrapper migration
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Jun 12, 2020
1 parent 7f41c09 commit ad77fdc
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 61 deletions.
5 changes: 5 additions & 0 deletions src/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ CTxDestination DestinationFor(const CKeyID& keyID, const CChainParams::Base58Typ
return addr.Get();
}

std::string EncodeDestination(const CTxDestination& dest, bool isStaking)
{
return EncodeDestination(dest, isStaking ? CChainParams::STAKING_ADDRESS : CChainParams::PUBKEY_ADDRESS);
}

std::string EncodeDestination(const CTxDestination& dest, const CChainParams::Base58Type addrType)
{
CBitcoinAddress addr(dest, addrType);
Expand Down
1 change: 1 addition & 0 deletions src/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ typedef CBitcoinExtKeyBase<CExtPubKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_PUBL


CTxDestination DestinationFor(const CKeyID& keyID, const CChainParams::Base58Type addrType);
std::string EncodeDestination(const CTxDestination& dest, bool isStaking);
std::string EncodeDestination(const CTxDestination& dest, const CChainParams::Base58Type addrType = CChainParams::PUBKEY_ADDRESS);
// DecodeDestinationisStaking flag is set to true when the string arg is from an staking address
CTxDestination DecodeDestination(const std::string& str, bool& isStaking);
Expand Down
2 changes: 1 addition & 1 deletion src/qt/pivx/sendconfirmdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ void TxDetailDialog::onOutputsClicked()
CTxDestination dest;
bool isCsAddress = out.scriptPubKey.IsPayToColdStaking();
if (ExtractDestination(out.scriptPubKey, dest, isCsAddress)) {
std::string address = ((isCsAddress) ? CBitcoinAddress::newCSInstance(dest) : CBitcoinAddress::newInstance(dest)).ToString();
std::string address = EncodeDestination(dest, isCsAddress);
labelRes = QString::fromStdString(address);
labelRes = labelRes.left(16) + "..." + labelRes.right(16);
} else {
Expand Down
17 changes: 8 additions & 9 deletions src/qt/pivx/settings/settingsbittoolwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ void SettingsBitToolWidget::onEncryptKeyButtonENCClicked()
return;
}

CBitcoinAddress addr(ui->addressIn_ENC->text().toStdString());
if (!addr.IsValid()) {
CTxDestination dest = DecodeDestination(ui->addressIn_ENC->text().toStdString());
if (!IsValidDestination(dest)) {
ui->statusLabel_ENC->setStyleSheet("QLabel { color: red; }");
ui->statusLabel_ENC->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
return;
}

CKeyID keyID;
if (!addr.GetKeyID(keyID)) {
CKeyID keyID = *boost::get<CKeyID>(&dest);
if (!keyID) {
//ui->addressIn_ENC->setValid(false);
ui->statusLabel_ENC->setStyleSheet("QLabel { color: red; }");
ui->statusLabel_ENC->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again."));
Expand All @@ -172,7 +172,7 @@ void SettingsBitToolWidget::onEncryptKeyButtonENCClicked()
return;
}

std::string encryptedKey = BIP38_Encrypt(addr.ToString(), qstrPassphrase.toStdString(), key.GetPrivKey_256(), key.IsCompressed());
std::string encryptedKey = BIP38_Encrypt(EncodeDestination(dest), qstrPassphrase.toStdString(), key.GetPrivKey_256(), key.IsCompressed());
ui->encryptedKeyOut_ENC->setText(QString::fromStdString(encryptedKey));

ui->statusLabel_ENC->setStyleSheet("QLabel { color: green; }");
Expand Down Expand Up @@ -265,8 +265,7 @@ void SettingsBitToolWidget::onDecryptClicked()

key.Set(privKey.begin(), privKey.end(), fCompressed);
CPubKey pubKey = key.GetPubKey();
CBitcoinAddress address(pubKey.GetID());
ui->lineEditDecryptResult->setText(QString::fromStdString(address.ToString()));
ui->lineEditDecryptResult->setText(QString::fromStdString(EncodeDestination(pubKey.GetID())));
ui->pushButtonImport->setVisible(true);
}

Expand All @@ -279,10 +278,10 @@ void SettingsBitToolWidget::importAddressFromDecKey()
return;
}

CBitcoinAddress address(ui->lineEditDecryptResult->text().toStdString());
CTxDestination dest = DecodeDestination(ui->lineEditDecryptResult->text().toStdString());
CPubKey pubkey = key.GetPubKey();

if (!address.IsValid() || !key.IsValid() || CBitcoinAddress(pubkey.GetID()).ToString() != address.ToString()) {
if (!IsValidDestination(dest) || !key.IsValid() || EncodeDestination(pubkey.GetID()) != EncodeDestination(dest)) {
ui->statusLabel_DEC->setStyleSheet("QLabel { color: red; }");
ui->statusLabel_DEC->setText(tr("Data Not Valid.") + QString(" ") + tr("Please try again."));
return;
Expand Down
7 changes: 3 additions & 4 deletions src/qt/pivx/settings/settingsmultisendwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ void SettingsMultisendWidget::onAddRecipientClicked()
void SettingsMultisendWidget::addMultiSend(QString address, int percentage, QString addressLabel)
{
std::string strAddress = address.toStdString();
if (!CBitcoinAddress(strAddress).IsValid()) {
if (!IsValidDestinationString(strAddress)) {
inform(tr("The entered address: %1 is invalid.\nPlease check the address and try again.").arg(address));
return;
}
Expand All @@ -311,9 +311,8 @@ void SettingsMultisendWidget::addMultiSend(QString address, int percentage, QStr

if (walletModel && walletModel->getAddressTableModel()) {
// update the address book with the label given or no label if none was given.
CBitcoinAddress address(strAddress);
std::string userInputLabel = addressLabel.toStdString();
walletModel->updateAddressBookLabels(address.Get(), (userInputLabel.empty()) ? "(no label)" : userInputLabel,
walletModel->updateAddressBookLabels(DecodeDestination(strAddress), (userInputLabel.empty()) ? "(no label)" : userInputLabel,
AddressBook::AddressBookPurpose::SEND);
}

Expand All @@ -337,7 +336,7 @@ void SettingsMultisendWidget::activate()
strRet = tr("Unable to activate MultiSend, no available recipients");
else if (!(ui->checkBoxStake->isChecked() || ui->checkBoxRewards->isChecked())) {
strRet = tr("Unable to activate MultiSend\nCheck one or both of the check boxes to send on stake and/or masternode rewards");
} else if (CBitcoinAddress(pwalletMain->vMultiSend[0].first).IsValid()) {
} else if (IsValidDestinationString(pwalletMain->vMultiSend[0].first)) {
pwalletMain->fMultiSendStake = ui->checkBoxStake->isChecked();
pwalletMain->fMultiSendMasternodeReward = ui->checkBoxRewards->isChecked();

Expand Down
99 changes: 52 additions & 47 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,11 +734,11 @@ UniValue getaddressesbyaccount(const UniValue& params, bool fHelp)

// Find all addresses that have the given account
UniValue ret(UniValue::VARR);
for (const PAIRTYPE(CBitcoinAddress, AddressBook::CAddressBookData) & item : pwalletMain->mapAddressBook) {
const CBitcoinAddress& address = item.first;
for (const PAIRTYPE(CTxDestination, AddressBook::CAddressBookData) & item : pwalletMain->mapAddressBook) {
const CTxDestination& address = item.first;
const std::string& strName = item.second.name;
if (strName == strAccount)
ret.push_back(address.ToString());
ret.push_back(EncodeDestination(address));
}
return ret;
}
Expand Down Expand Up @@ -802,8 +802,9 @@ UniValue sendtoaddress(const UniValue& params, bool fHelp)

LOCK2(cs_main, pwalletMain->cs_wallet);

CBitcoinAddress address(params[0].get_str());
if (!address.IsValid() || address.IsStakingAddress())
bool isStaking = false;
CTxDestination address = DecodeDestination(params[0].get_str(), isStaking);
if (!IsValidDestination(address) || isStaking)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid PIVX address");

// Amount
Expand All @@ -818,7 +819,7 @@ UniValue sendtoaddress(const UniValue& params, bool fHelp)

EnsureWalletIsUnlocked();

SendMoney(address.Get(), nAmount, wtx);
SendMoney(address, nAmount, wtx);

return wtx.GetHash().GetHex();
}
Expand All @@ -840,11 +841,13 @@ UniValue CreateColdStakeDelegation(const UniValue& params, CWalletTx& wtxNew, CR
}

// Get Staking Address
CBitcoinAddress stakeAddr(params[0].get_str());
CKeyID stakeKey;
if (!stakeAddr.IsValid() || !stakeAddr.IsStakingAddress())
bool isStaking = false;
CTxDestination stakeAddr = DecodeDestination(params[0].get_str(), isStaking);
if (!IsValidDestination(stakeAddr) || !isStaking)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid PIVX staking address");
if (!stakeAddr.GetKeyID(stakeKey))

CKeyID* stakeKey = boost::get<CKeyID>(&stakeAddr);
if (!stakeKey)
throw JSONRPCError(RPC_WALLET_ERROR, "Unable to get stake pubkey hash from stakingaddress");

// Get Amount
Expand Down Expand Up @@ -899,7 +902,7 @@ UniValue CreateColdStakeDelegation(const UniValue& params, CWalletTx& wtxNew, CR
}

// Get P2CS script for addresses
CScript scriptPubKey = GetScriptForStakeDelegation(stakeKey, ownerKey);
CScript scriptPubKey = GetScriptForStakeDelegation(*stakeKey, ownerKey);

// Create the transaction
CAmount nFeeRequired;
Expand All @@ -912,7 +915,7 @@ UniValue CreateColdStakeDelegation(const UniValue& params, CWalletTx& wtxNew, CR

UniValue result(UniValue::VOBJ);
result.push_back(Pair("owner_address", ownerAddressStr));
result.push_back(Pair("staker_address", stakeAddr.ToString()));
result.push_back(Pair("staker_address", EncodeDestination(stakeAddr, true)));
return result;
}

Expand Down Expand Up @@ -1059,8 +1062,9 @@ UniValue sendtoaddressix(const UniValue& params, bool fHelp)

LOCK2(cs_main, pwalletMain->cs_wallet);

CBitcoinAddress address(params[0].get_str());
if (!address.IsValid() || address.IsStakingAddress())
bool isStaking = false;
CTxDestination address = DecodeDestination(params[0].get_str(), isStaking);
if (!IsValidDestination(address) || isStaking)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid PIVX address");

// Amount
Expand All @@ -1075,7 +1079,7 @@ UniValue sendtoaddressix(const UniValue& params, bool fHelp)

EnsureWalletIsUnlocked();

SendMoney(address.Get(), nAmount, wtx, true);
SendMoney(address, nAmount, wtx, true);

return wtx.GetHash().GetHex();
}
Expand Down Expand Up @@ -1113,11 +1117,11 @@ UniValue listaddressgroupings(const UniValue& params, bool fHelp)
UniValue jsonGrouping(UniValue::VARR);
for (CTxDestination address : grouping) {
UniValue addressInfo(UniValue::VARR);
addressInfo.push_back(CBitcoinAddress(address).ToString());
addressInfo.push_back(EncodeDestination(address));
addressInfo.push_back(ValueFromAmount(balances[address]));
{
if (pwalletMain->mapAddressBook.find(CBitcoinAddress(address).Get()) != pwalletMain->mapAddressBook.end())
addressInfo.push_back(pwalletMain->mapAddressBook.find(CBitcoinAddress(address).Get())->second.name);
if (pwalletMain->mapAddressBook.find(address) != pwalletMain->mapAddressBook.end())
addressInfo.push_back(pwalletMain->mapAddressBook.find(address)->second.name);
}
jsonGrouping.push_back(addressInfo);
}
Expand Down Expand Up @@ -1158,12 +1162,12 @@ UniValue signmessage(const UniValue& params, bool fHelp)
std::string strAddress = params[0].get_str();
std::string strMessage = params[1].get_str();

CBitcoinAddress addr(strAddress);
if (!addr.IsValid())
CTxDestination dest = DecodeDestination(strAddress);
if (!IsValidDestination(dest))
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid address");

CKeyID keyID;
if (!addr.GetKeyID(keyID))
CKeyID keyID = *boost::get<CKeyID>(&dest);
if (!keyID)
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");

CKey key;
Expand Down Expand Up @@ -1208,10 +1212,10 @@ UniValue getreceivedbyaddress(const UniValue& params, bool fHelp)
LOCK2(cs_main, pwalletMain->cs_wallet);

// pivx address
CBitcoinAddress address = CBitcoinAddress(params[0].get_str());
if (!address.IsValid())
CTxDestination address = DecodeDestination(params[0].get_str());
if (!IsValidDestination(address))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid PIVX address");
CScript scriptPubKey = GetScriptForDestination(address.Get());
CScript scriptPubKey = GetScriptForDestination(address);
if (!IsMine(*pwalletMain, scriptPubKey))
throw JSONRPCError(RPC_WALLET_ERROR, "Address not found in wallet");

Expand Down Expand Up @@ -1571,8 +1575,9 @@ UniValue sendfrom(const UniValue& params, bool fHelp)
LOCK2(cs_main, pwalletMain->cs_wallet);

std::string strAccount = AccountFromValue(params[0]);
CBitcoinAddress address(params[1].get_str());
if (!address.IsValid() || address.IsStakingAddress())
bool isStaking = false;
CTxDestination address = DecodeDestination(params[1].get_str(), isStaking);
if (!IsValidDestination(address) || isStaking)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid PIVX address");
CAmount nAmount = AmountFromValue(params[2]);
int nMinDepth = 1;
Expand All @@ -1597,7 +1602,7 @@ UniValue sendfrom(const UniValue& params, bool fHelp)
if (nAmount > nBalance)
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");

SendMoney(address.Get(), nAmount, wtx);
SendMoney(address, nAmount, wtx);

return wtx.GetHash().GetHex();
}
Expand Down Expand Up @@ -1647,21 +1652,22 @@ UniValue sendmany(const UniValue& params, bool fHelp)
if (params.size() > 3 && !params[3].isNull() && !params[3].get_str().empty())
wtx.mapValue["comment"] = params[3].get_str();

std::set<CBitcoinAddress> setAddress;
std::set<CTxDestination> setAddress;
std::vector<std::pair<CScript, CAmount> > vecSend;

CAmount totalAmount = 0;
std::vector<std::string> keys = sendTo.getKeys();
for (const std::string& name_ : keys) {
CBitcoinAddress address(name_);
if (!address.IsValid() || address.IsStakingAddress())
bool isStaking = false;
CTxDestination dest = DecodeDestination(name_,isStaking);
if (!IsValidDestination(dest) || isStaking)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid PIVX address: ")+name_);

if (setAddress.count(address))
if (setAddress.count(dest))
throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ")+name_);
setAddress.insert(address);
setAddress.insert(dest);

CScript scriptPubKey = GetScriptForDestination(address.Get());
CScript scriptPubKey = GetScriptForDestination(dest);
CAmount nAmount = AmountFromValue(sendTo[name_]);
totalAmount += nAmount;

Expand Down Expand Up @@ -1734,7 +1740,7 @@ UniValue addmultisigaddress(const UniValue& params, bool fHelp)
pwalletMain->AddCScript(inner);

pwalletMain->SetAddressBook(innerID, strAccount, AddressBook::AddressBookPurpose::SEND);
return CBitcoinAddress(innerID).ToString();
return EncodeDestination(innerID);
}


Expand Down Expand Up @@ -1771,7 +1777,7 @@ UniValue ListReceived(const UniValue& params, bool fByAccounts)
filter = filter | ISMINE_WATCH_ONLY;

// Tally
std::map<CBitcoinAddress, tallyitem> mapTally;
std::map<CTxDestination, tallyitem> mapTally;
for (std::map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) {
const CWalletTx& wtx = (*it).second;

Expand Down Expand Up @@ -1805,10 +1811,10 @@ UniValue ListReceived(const UniValue& params, bool fByAccounts)
// Reply
UniValue ret(UniValue::VARR);
std::map<std::string, tallyitem> mapAccountTally;
for (const PAIRTYPE(CBitcoinAddress, AddressBook::CAddressBookData) & item : pwalletMain->mapAddressBook) {
const CBitcoinAddress& address = item.first;
for (const PAIRTYPE(CTxDestination, AddressBook::CAddressBookData) & item : pwalletMain->mapAddressBook) {
const CTxDestination& address = item.first;
const std::string& strAccount = item.second.name;
std::map<CBitcoinAddress, tallyitem>::iterator it = mapTally.find(address);
std::map<CTxDestination, tallyitem>::iterator it = mapTally.find(address);
if (it == mapTally.end() && !fIncludeEmpty)
continue;

Expand All @@ -1833,7 +1839,7 @@ UniValue ListReceived(const UniValue& params, bool fByAccounts)
UniValue obj(UniValue::VOBJ);
if (fIsWatchonly)
obj.push_back(Pair("involvesWatchonly", true));
obj.push_back(Pair("address", address.ToString()));
obj.push_back(Pair("address", EncodeDestination(address, AddressBook::IsColdStakingPurpose(strAccount))));
obj.push_back(Pair("account", strAccount));
obj.push_back(Pair("amount", ValueFromAmount(nAmount)));
obj.push_back(Pair("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf)));
Expand Down Expand Up @@ -1996,8 +2002,8 @@ UniValue listcoldutxos(const UniValue& params, bool fHelp)
entry.push_back(Pair("txidn", (int)i));
entry.push_back(Pair("amount", ValueFromAmount(out.nValue)));
entry.push_back(Pair("confirmations", pcoin->GetDepthInMainChain(false)));
entry.push_back(Pair("cold-staker", CBitcoinAddress(addresses[0], CChainParams::STAKING_ADDRESS).ToString()));
entry.push_back(Pair("coin-owner", CBitcoinAddress(addresses[1]).ToString()));
entry.push_back(Pair("cold-staker", EncodeDestination(addresses[0], CChainParams::STAKING_ADDRESS)));
entry.push_back(Pair("coin-owner", EncodeDestination(addresses[1])));
entry.push_back(Pair("whitelisted", fWhitelisted ? "true" : "false"));
results.push_back(entry);
}
Expand All @@ -2008,9 +2014,8 @@ UniValue listcoldutxos(const UniValue& params, bool fHelp)

static void MaybePushAddress(UniValue & entry, const CTxDestination &dest)
{
CBitcoinAddress addr;
if (addr.Set(dest))
entry.push_back(Pair("address", addr.ToString()));
if (IsValidDestination(dest))
entry.push_back(Pair("address", EncodeDestination(dest)));
}

void ListTransactions(const CWalletTx& wtx, const std::string& strAccount, int nMinDepth, bool fLong, UniValue& ret, const isminefilter& filter)
Expand Down Expand Up @@ -3158,7 +3163,7 @@ UniValue printAddresses()
for (const COutput& out : vCoins) {
CTxDestination utxoAddress;
ExtractDestination(out.tx->vout[out.i].scriptPubKey, utxoAddress);
std::string strAdd = CBitcoinAddress(utxoAddress).ToString();
std::string strAdd = EncodeDestination(utxoAddress);

if (mapAddresses.find(strAdd) == mapAddresses.end()) //if strAdd is not already part of the map
mapAddresses[strAdd] = (double)out.tx->vout[out.i].nValue / (double)COIN;
Expand Down Expand Up @@ -3829,7 +3834,7 @@ extern UniValue DoZpivSpend(const CAmount nAmount, std::vector<CZerocoinMint>& v
if(txout.IsZerocoinMint())
out.push_back(Pair("address", "zerocoinmint"));
else if(ExtractDestination(txout.scriptPubKey, dest))
out.push_back(Pair("address", CBitcoinAddress(dest).ToString()));
out.push_back(Pair("address", EncodeDestination(dest)));
vout.push_back(out);
}

Expand Down

0 comments on commit ad77fdc

Please sign in to comment.