Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ BITCOIN_CORE_H = \
evo/evonotificationinterface.h \
evo/providertx.h \
evo/specialtx.h \
pairresult.h \
addressbook.h \
wallet/db.h \
flatfile.h \
Expand Down
23 changes: 23 additions & 0 deletions src/destination_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,26 @@ namespace Standard {

} // End Standard namespace

Destination& Destination::operator=(const Destination& from)
{
this->dest = from.dest;
this->isP2CS = from.isP2CS;
return *this;
}

// Returns the key ID if Destination is a transparent "regular" destination
const CKeyID* Destination::getKeyID()
{
const CTxDestination* regDest = Standard::GetTransparentDestination(dest);
return (regDest) ? boost::get<CKeyID>(regDest) : nullptr;
}

std::string Destination::ToString() const
{
if (!Standard::IsValidDestination(dest)) {
// Invalid address
return "";
}
return Standard::EncodeDestination(dest, isP2CS ? CChainParams::STAKING_ADDRESS : CChainParams::PUBKEY_ADDRESS);
}

19 changes: 19 additions & 0 deletions src/destination_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,23 @@ namespace Standard {

} // End Standard namespace

/**
* Wrapper class for every supported address
*/
class Destination {
public:
explicit Destination() {}
explicit Destination(const CTxDestination& _dest, bool _isP2CS) : dest(_dest), isP2CS(_isP2CS) {}
explicit Destination(const libzcash::SaplingPaymentAddress& _dest) : dest(_dest) {}

CWDestination dest{CNoDestination()};
bool isP2CS{false};

Destination& operator=(const Destination& from);
// Returns the key ID if Destination is a transparent "regular" destination
const CKeyID* getKeyID();
// Returns the encoded string address
std::string ToString() const;
};

#endif //DESTINATION_IO_H
28 changes: 0 additions & 28 deletions src/key_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,4 @@ namespace KeyIO {

}

/**
* Wrapper class for every supported address
*/
struct Destination {
public:
explicit Destination() {}
explicit Destination(const CTxDestination& _dest, bool _isP2CS) : dest(_dest), isP2CS(_isP2CS) {}

CTxDestination dest{CNoDestination()};
bool isP2CS{false};

Destination& operator=(const Destination& from)
{
this->dest = from.dest;
this->isP2CS = from.isP2CS;
return *this;
}

std::string ToString()
{
if (!IsValidDestination(dest)) {
// Invalid address
return "";
}
return EncodeDestination(dest, isP2CS ? CChainParams::STAKING_ADDRESS : CChainParams::PUBKEY_ADDRESS);
}
};

#endif //PIVX_KEY_IO_H
14 changes: 14 additions & 0 deletions src/operationresult.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,18 @@ inline OperationResult errorOut(const std::string& errorStr)
return OperationResult(false, errorStr);
}


template <class T>
class CallResult : public OperationResult
{
private:
Optional<T> m_obj_res{nullopt};
public:
CallResult() : OperationResult(false) {}
CallResult(T _obj) : OperationResult(true), m_obj_res(_obj) { }
CallResult(const std::string& error) : OperationResult(false, error) { }
Comment thread
random-zebra marked this conversation as resolved.
Outdated
const Optional<T>& getObjResult() const { return m_obj_res; }
Comment thread
furszy marked this conversation as resolved.
Outdated
};


#endif //OPERATIONRESULT_H
20 changes: 0 additions & 20 deletions src/pairresult.h

This file was deleted.

15 changes: 3 additions & 12 deletions src/qt/addresstablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,18 +631,9 @@ QString AddressTableModel::getAddressToShow(bool isShielded) const
}

// For some reason we don't have any address in our address book, let's create one
PairResult res(false);
QString addressStr;
if (!isShielded) {
Destination newAddress;
res = walletModel->getNewAddress(newAddress, "Default");
if (res.result) {
addressStr = QString::fromStdString(newAddress.ToString());
}
} else {
res = walletModel->getNewShieldedAddress(addressStr, "default shielded");
}
return addressStr;
CallResult<Destination> res = !isShielded ? walletModel->getNewAddress("Default") :
walletModel->getNewShieldedAddress("default shielded");
return (res) ? QString::fromStdString(res.getObjResult()->ToString()) : "";;
}

void AddressTableModel::emitDataChanged(int idx)
Expand Down
11 changes: 4 additions & 7 deletions src/qt/pivx/masternodewizarddialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "clientmodel.h"
#include "key_io.h"
#include "optionsmodel.h"
#include "pairresult.h"
#include "qt/pivx/mnmodel.h"
#include "qt/pivx/guitransactionsutils.h"
#include "qt/pivx/qtutils.h"
Expand Down Expand Up @@ -221,18 +220,16 @@ bool MasterNodeWizardDialog::createMN()
// If not found create a new collateral tx
if (!walletModel->getMNCollateralCandidate(collateralOut)) {
// New receive address
Destination dest;
PairResult r = walletModel->getNewAddress(dest, alias);

if (!r.result) {
auto r = walletModel->getNewAddress(alias);
if (!r) {
// generate address fail
inform(tr(r.status->c_str()));
inform(tr(r.getError().c_str()));
return false;
}

// const QString& addr, const QString& label, const CAmount& amount, const QString& message
SendCoinsRecipient sendCoinsRecipient(
QString::fromStdString(dest.ToString()),
QString::fromStdString(r.getObjResult()->ToString()),
QString::fromStdString(alias),
clientModel->getMNCollateralRequiredAmount(),
"");
Expand Down
18 changes: 5 additions & 13 deletions src/qt/pivx/receivewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "qt/pivx/addressholder.h"
#include "walletmodel.h"
#include "guiutil.h"
#include "pairresult.h"

#include <QModelIndex>
#include <QColor>
Expand Down Expand Up @@ -251,23 +250,16 @@ void ReceiveWidget::onNewAddressClicked()
return;
}

QString strAddress;
PairResult r(false);
if (!shieldedMode) {
Destination address;
r = walletModel->getNewAddress(address, "");
strAddress = QString::fromStdString(address.ToString());
} else {
r = walletModel->getNewShieldedAddress(strAddress, "");
}
CallResult<Destination> r = !shieldedMode ? walletModel->getNewAddress("") :
walletModel->getNewShieldedAddress("");

// Check validity
if (!r.result) {
inform(r.status->c_str());
if (!r) {
inform(r.getError().c_str());
return;
}

refreshView(strAddress);
refreshView(QString::fromStdString(r.getObjResult()->ToString()));
inform(tr("New address created"));
} catch (const std::runtime_error& error) {
// Error generating address
Expand Down
12 changes: 5 additions & 7 deletions src/qt/pivx/requestdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "qt/pivx/qtutils.h"
#include "guiutil.h"
#include "amount.h"
#include "pairresult.h"
#include "optionsmodel.h"

RequestDialog::RequestDialog(QWidget *parent) :
Expand Down Expand Up @@ -114,23 +113,22 @@ void RequestDialog::accept()
std::string label = info->label.isEmpty() ? "" : info->label.toStdString();
QString title;

Destination address;
PairResult r(false);
CallResult<Destination> r;
if (this->isPaymentRequest) {
r = walletModel->getNewAddress(address, label);
r = walletModel->getNewAddress(label);
title = tr("Request for ") + BitcoinUnits::format(displayUnit, value, false, BitcoinUnits::separatorAlways) + " " + QString(CURRENCY_UNIT.c_str());
} else {
r = walletModel->getNewStakingAddress(address, label);
r = walletModel->getNewStakingAddress(label);
title = tr("Cold Staking Address Generated");
}

if (!r.result) {
if (!r) {
// TODO: notify user about this error
close();
return;
}

info->address = QString::fromStdString(address.ToString());
info->address = QString::fromStdString(r.getObjResult()->ToString());
ui->labelTitle->setText(title);

updateQr(info->address);
Expand Down
8 changes: 3 additions & 5 deletions src/qt/pivx/send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,14 +743,12 @@ void SendWidget::onShieldCoinsClicked()

// Process spending
ProcessSend(recipients, true, [this](QList<SendCoinsRecipient>& recipients) {
QString strAddress;
auto res = walletModel->getNewShieldedAddress(strAddress, "");
// Check for generation errors
if (!res.result) {
auto res = walletModel->getNewShieldedAddress("");
if (!res) {
inform(tr("Error generating address to shield PIVs"));
return false;
}
recipients.back().address = strAddress;
recipients.back().address = QString::fromStdString(res.getObjResult()->ToString());
resetCoinControl();
return true;
});
Expand Down
32 changes: 15 additions & 17 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <QtConcurrent/QtConcurrent>
#include <QSet>
#include <QTimer>
#include <utility>


WalletModel::WalletModel(CWallet* wallet, OptionsModel* optionsModel, QObject* parent) : QObject(parent), wallet(wallet), walletWrapper(*wallet),
Expand Down Expand Up @@ -475,14 +476,15 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
Destination ownerAdd;
if (rcp.ownerAddress.isEmpty()) {
// Create new internal owner address
if (!getNewAddress(ownerAdd).result)
return CannotCreateInternalAddress;
auto res = getNewAddress();
if (!res) return CannotCreateInternalAddress;
ownerAdd = *res.getObjResult();
} else {
ownerAdd = Destination(DecodeDestination(rcp.ownerAddress.toStdString()), false);
}

const CKeyID* stakerId = boost::get<CKeyID>(&out);
const CKeyID* ownerId = boost::get<CKeyID>(&ownerAdd.dest);
const CKeyID* ownerId = ownerAdd.getKeyID();
if (!stakerId || !ownerId) {
return InvalidAddress;
}
Expand Down Expand Up @@ -926,27 +928,23 @@ int64_t WalletModel::getKeyCreationTime(const libzcash::SaplingPaymentAddress& a
return 0;
}

PairResult WalletModel::getNewAddress(Destination& ret, std::string label) const
CallResult<Destination> WalletModel::getNewAddress(const std::string& label) const
{
CTxDestination dest;
PairResult res = wallet->getNewAddress(dest, label);
if (res.result) ret = Destination(dest, false);
return res;
auto res = wallet->getNewAddress(label);
return res ? CallResult<Destination>(Destination(*res.getObjResult(), false)) :
CallResult<Destination>(res.getError());
}

PairResult WalletModel::getNewStakingAddress(Destination& ret,std::string label) const
CallResult<Destination> WalletModel::getNewStakingAddress(const std::string& label) const
{
CTxDestination dest;
PairResult res = wallet->getNewStakingAddress(dest, label);
if (res.result) ret = Destination(dest, true);
return res;
auto res = wallet->getNewStakingAddress(label);
return res ? CallResult<Destination>(Destination(*res.getObjResult(), true)) :
CallResult<Destination>(res.getError());
}

PairResult WalletModel::getNewShieldedAddress(QString& shieldedAddrRet, std::string strLabel)
CallResult<Destination> WalletModel::getNewShieldedAddress(std::string strLabel)
{
shieldedAddrRet = QString::fromStdString(
KeyIO::EncodePaymentAddress(wallet->GenerateNewSaplingZKey(strLabel)));
return PairResult(true);
return CallResult<Destination>(Destination(wallet->GenerateNewSaplingZKey(std::move(strLabel))));
}

bool WalletModel::whitelistAddressFromColdStaking(const QString &addressStr)
Expand Down
8 changes: 3 additions & 5 deletions src/qt/walletmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
#include "interfaces/wallet.h"

#include "key.h"
#include "key_io.h"
#include "operationresult.h"
#include "support/allocators/zeroafterfree.h"
#include "pairresult.h"

#include <map>
#include <vector>
Expand Down Expand Up @@ -273,14 +271,14 @@ class WalletModel : public QObject
int64_t getKeyCreationTime(const CTxDestination& address);
int64_t getKeyCreationTime(const std::string& address);
int64_t getKeyCreationTime(const libzcash::SaplingPaymentAddress& address);
PairResult getNewAddress(Destination& ret, std::string label = "") const;
CallResult<Destination> getNewAddress(const std::string& label = "") const;
/**
* Return a new address used to receive for delegated cold stake purpose.
*/
PairResult getNewStakingAddress(Destination& ret, std::string label = "") const;
CallResult<Destination> getNewStakingAddress(const std::string& label = "") const;

//! Return a new shielded address.
PairResult getNewShieldedAddress(QString& shieldedAddrRet, std::string strLabel = "");
CallResult<Destination> getNewShieldedAddress(std::string strLabel = "");

//! Return new wallet rescan reserver
WalletRescanReserver getRescanReserver() const { return WalletRescanReserver(wallet); }
Expand Down
5 changes: 3 additions & 2 deletions src/test/librust/sapling_rpc_wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,9 @@ BOOST_AUTO_TEST_CASE(rpc_shieldsendmany_taddr_to_sapling)
UniValue retValue;

// add keys manually
CTxDestination taddr;
m_wallet.getNewAddress(taddr, "");
auto res = m_wallet.getNewAddress("");
BOOST_CHECK(res);
CTxDestination taddr = *res.getObjResult();
std::string taddr1 = EncodeDestination(taddr);
auto zaddr1 = m_wallet.GenerateNewSaplingZKey();

Expand Down
Loading