Skip to content

Commit

Permalink
[Refactor] Remove direct constructor for public spends
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Dec 2, 2020
1 parent 8a30aac commit e99388e
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 84 deletions.
76 changes: 0 additions & 76 deletions src/zpiv/zpivmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,6 @@
#include "validation.h"
#include "zpivchain.h"

PublicCoinSpend::PublicCoinSpend(libzerocoin::ZerocoinParams* params, const uint8_t version,
const CBigNum& serial, const CBigNum& randomness, const uint256& ptxHash, CPubKey* pubkey):
pubCoin(params)
{
this->coinSerialNumber = serial;
this->version = version;
this->spendType = libzerocoin::SpendType::SPEND;
this->ptxHash = ptxHash;
this->coinVersion = libzerocoin::ExtractVersionFromSerial(coinSerialNumber);

if (!isAllowed()) {
// v1 coins need at least version 4 spends
std::string errMsg = strprintf("Unable to create PublicCoinSpend version %d with coin version 1. "
"Minimum spend version required: %d", version, PUBSPEND_SCHNORR);
// this should be unreachable code (already checked in createInput)
// throw runtime error
throw std::runtime_error(errMsg);
}

if (pubkey && getCoinVersion() >= libzerocoin::PrivateCoin::PUBKEY_VERSION) {
// pubkey available only from v2 coins onwards
this->pubkey = *pubkey;
}

if (version < PUBSPEND_SCHNORR)
this->randomness = randomness;
else
this->schnorrSig = libzerocoin::CoinRandomnessSchnorrSignature(params, randomness, ptxHash);

}

template <typename Stream>
PublicCoinSpend::PublicCoinSpend(libzerocoin::ZerocoinParams* params, Stream& strm): pubCoin(params) {
strm >> *this;
Expand Down Expand Up @@ -136,51 +105,6 @@ namespace ZPIVModule {
return CDataStream(data, SER_NETWORK, PROTOCOL_VERSION);
}

bool createInput(CTxIn &in, CZerocoinMint &mint, uint256 hashTxOut, const int spendVersion) {
// check that this spend is allowed
const bool fUseV1Params = mint.GetVersion() < libzerocoin::PrivateCoin::PUBKEY_VERSION;
if (!PublicCoinSpend::isAllowed(fUseV1Params, spendVersion)) {
// v1 coins need at least version 4 spends
std::string errMsg = strprintf("Unable to create PublicCoinSpend version %d with coin version 1. "
"Minimum spend version required: %d", spendVersion, PUBSPEND_SCHNORR);
return error("%s: %s", __func__, errMsg);
}

// create the PublicCoinSpend
libzerocoin::ZerocoinParams *params = Params().GetConsensus().Zerocoin_Params(fUseV1Params);
PublicCoinSpend spend(params, spendVersion, mint.GetSerialNumber(), mint.GetRandomness(), hashTxOut, nullptr);

spend.outputIndex = mint.GetOutputIndex();
spend.txHash = mint.GetTxHash();
spend.setDenom(mint.GetDenomination());

// add public key and signature
if (!fUseV1Params) {
CKey key;
if (!mint.GetKeyPair(key))
return error("%s: failed to set zPIV privkey mint.", __func__);
spend.setPubKey(key.GetPubKey(), true);

std::vector<unsigned char> vchSig;
if (!key.Sign(spend.signatureHash(), vchSig))
return error("%s: ZPIVModule failed to sign signatureHash.", __func__);
spend.setVchSig(vchSig);

}

// serialize the PublicCoinSpend and add it to the input scriptSig
CDataStream ser(SER_NETWORK, PROTOCOL_VERSION);
ser << spend;
std::vector<unsigned char> data(ser.begin(), ser.end());
CScript scriptSigIn = CScript() << OP_ZEROCOINPUBLICSPEND << data.size();
scriptSigIn.insert(scriptSigIn.end(), data.begin(), data.end());

// create the tx input
in = CTxIn(mint.GetTxHash(), mint.GetOutputIndex(), scriptSigIn, mint.GetDenomination());
in.nSequence = mint.GetDenomination();
return true;
}

PublicCoinSpend parseCoinSpend(const CTxIn &in)
{
libzerocoin::ZerocoinParams *params = Params().GetConsensus().Zerocoin_Params(false);
Expand Down
8 changes: 0 additions & 8 deletions src/zpiv/zpivmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,13 @@ class PublicCoinSpend : public libzerocoin::CoinSpend {
public:

PublicCoinSpend(libzerocoin::ZerocoinParams* params): pubCoin(params) {};
PublicCoinSpend(libzerocoin::ZerocoinParams* params, const uint8_t version, const CBigNum& serial, const CBigNum& randomness, const uint256& ptxHash, CPubKey* pubkey);
template <typename Stream> PublicCoinSpend(libzerocoin::ZerocoinParams* params, Stream& strm);

~PublicCoinSpend(){};

const uint256 signatureHash() const override;
void setVchSig(std::vector<unsigned char> vchSig) { this->vchSig = vchSig; };
bool HasValidSignature() const;
bool Verify() const;
static bool isAllowed(const bool fUseV1Params, const int spendVersion) { return !fUseV1Params || spendVersion >= PUBSPEND_SCHNORR; }
bool isAllowed() const {
const bool fUseV1Params = getCoinVersion() < libzerocoin::PrivateCoin::PUBKEY_VERSION;
return isAllowed(fUseV1Params, version);
}
int getCoinVersion() const { return this->coinVersion; }

// Members
Expand Down Expand Up @@ -83,7 +76,6 @@ class CValidationState;

namespace ZPIVModule {
CDataStream ScriptSigToSerializedSpend(const CScript& scriptSig);
bool createInput(CTxIn &in, CZerocoinMint& mint, uint256 hashTxOut, const int spendVersion);
PublicCoinSpend parseCoinSpend(const CTxIn &in);
bool parseCoinSpend(const CTxIn &in, const CTransaction& tx, const CTxOut &prevOut, PublicCoinSpend& publicCoinSpend);
bool validateInput(const CTxIn &in, const CTxOut &prevOut, const CTransaction& tx, PublicCoinSpend& ret);
Expand Down

0 comments on commit e99388e

Please sign in to comment.