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 04e790c commit a10afb3
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 70 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
18 changes: 9 additions & 9 deletions src/qt/transactiondesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ QString TransactionDesc::toHTML(CWallet* wallet, CWalletTx& wtx, TransactionReco
// Offline transaction
if (nNet > 0) {
// Credit
if (CBitcoinAddress(rec->address).IsValid()) {
CTxDestination address = CBitcoinAddress(rec->address).Get();
if (wallet->mapAddressBook.count(address)) {
CTxDestination dest = DecodeDestination(rec->address);
if (IsValidDestination(dest)) {
if (wallet->mapAddressBook.count(dest)) {
strHTML += "<b>" + tr("From") + ":</b> " + tr("unknown") + "<br>";
strHTML += "<b>" + tr("To") + ":</b> ";
strHTML += GUIUtil::HtmlEscape(rec->address);
QString addressOwned = (::IsMine(*wallet, address) == ISMINE_SPENDABLE) ? tr("own address") : tr("watch-only");
if (!wallet->mapAddressBook[address].name.empty())
strHTML += " (" + addressOwned + ", " + tr("label") + ": " + GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + ")";
QString addressOwned = (::IsMine(*wallet, dest) == ISMINE_SPENDABLE) ? tr("own address") : tr("watch-only");
if (!wallet->mapAddressBook[dest].name.empty())
strHTML += " (" + addressOwned + ", " + tr("label") + ": " + GUIUtil::HtmlEscape(wallet->mapAddressBook[dest].name) + ")";
else
strHTML += " (" + addressOwned + ")";
strHTML += "<br>";
Expand All @@ -137,7 +137,7 @@ QString TransactionDesc::toHTML(CWallet* wallet, CWalletTx& wtx, TransactionReco
// Online transaction
std::string strAddress = wtx.mapValue["to"];
strHTML += "<b>" + tr("To") + ":</b> ";
CTxDestination dest = CBitcoinAddress(strAddress).Get();
CTxDestination dest = DecodeDestination(strAddress);
if (wallet->mapAddressBook.count(dest) && !wallet->mapAddressBook[dest].name.empty())
strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[dest].name) + " ";
strHTML += GUIUtil::HtmlEscape(strAddress) + "<br>";
Expand Down Expand Up @@ -197,7 +197,7 @@ QString TransactionDesc::toHTML(CWallet* wallet, CWalletTx& wtx, TransactionReco
strHTML += "<b>" + tr("To") + ":</b> ";
if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].name.empty())
strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + " ";
strHTML += GUIUtil::HtmlEscape(CBitcoinAddress(address).ToString());
strHTML += GUIUtil::HtmlEscape(EncodeDestination(address));
if (toSelf == ISMINE_SPENDABLE)
strHTML += " (own address)";
else if (toSelf == ISMINE_WATCH_ONLY)
Expand Down Expand Up @@ -301,7 +301,7 @@ QString TransactionDesc::toHTML(CWallet* wallet, CWalletTx& wtx, TransactionReco
if (ExtractDestination(vout.scriptPubKey, address)) {
if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].name.empty())
strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + " ";
strHTML += QString::fromStdString(CBitcoinAddress(address).ToString());
strHTML += QString::fromStdString(EncodeDestination(address));
}
strHTML = strHTML + " " + tr("Amount") + "=" + BitcoinUnits::formatHtmlWithUnit(unit, vout.nValue);
strHTML = strHTML + " IsMine=" + (wallet->IsMine(vout) & ISMINE_SPENDABLE ? tr("true") : tr("false"));
Expand Down
Loading

0 comments on commit a10afb3

Please sign in to comment.