Factor out common logic from CZCPaymentAddress and CZCSpendingKey
This commit is contained in:
@@ -323,67 +323,52 @@ bool CBitcoinSecret::SetString(const std::string& strSecret)
|
|||||||
return SetString(strSecret.c_str());
|
return SetString(strSecret.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CZCPaymentAddress::Set(const libzcash::PaymentAddress& addr)
|
template<class DATA_TYPE, CChainParams::Base58Type PREFIX, size_t SER_SIZE>
|
||||||
|
bool CZCEncoding<DATA_TYPE, PREFIX, SER_SIZE>::Set(const DATA_TYPE& addr)
|
||||||
{
|
{
|
||||||
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
||||||
ss << addr;
|
ss << addr;
|
||||||
std::vector<unsigned char> addrSerialized(ss.begin(), ss.end());
|
std::vector<unsigned char> addrSerialized(ss.begin(), ss.end());
|
||||||
assert(addrSerialized.size() == libzcash::SerializedPaymentAddressSize);
|
assert(addrSerialized.size() == SER_SIZE);
|
||||||
SetData(Params().Base58Prefix(CChainParams::ZCPAYMENT_ADDRRESS), &addrSerialized[0], libzcash::SerializedPaymentAddressSize);
|
SetData(Params().Base58Prefix(PREFIX), &addrSerialized[0], SER_SIZE);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
libzcash::PaymentAddress CZCPaymentAddress::Get() const
|
template<class DATA_TYPE, CChainParams::Base58Type PREFIX, size_t SER_SIZE>
|
||||||
|
DATA_TYPE CZCEncoding<DATA_TYPE, PREFIX, SER_SIZE>::Get() const
|
||||||
{
|
{
|
||||||
if (vchData.size() != libzcash::SerializedPaymentAddressSize) {
|
if (vchData.size() != SER_SIZE) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"payment address is invalid"
|
PrependName(" is invalid")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (vchVersion != Params().Base58Prefix(CChainParams::ZCPAYMENT_ADDRRESS)) {
|
if (vchVersion != Params().Base58Prefix(PREFIX)) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"payment address is for wrong network type"
|
PrependName(" is for wrong network type")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> serialized(vchData.begin(), vchData.end());
|
std::vector<unsigned char> serialized(vchData.begin(), vchData.end());
|
||||||
|
|
||||||
CDataStream ss(serialized, SER_NETWORK, PROTOCOL_VERSION);
|
CDataStream ss(serialized, SER_NETWORK, PROTOCOL_VERSION);
|
||||||
libzcash::PaymentAddress ret;
|
DATA_TYPE ret;
|
||||||
ss >> ret;
|
ss >> ret;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CZCSpendingKey::Set(const libzcash::SpendingKey& addr)
|
// Explicit instantiations for libzcash::PaymentAddress
|
||||||
{
|
template bool CZCEncoding<libzcash::PaymentAddress,
|
||||||
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
CChainParams::ZCPAYMENT_ADDRRESS,
|
||||||
ss << addr;
|
libzcash::SerializedPaymentAddressSize>::Set(const libzcash::PaymentAddress& addr);
|
||||||
std::vector<unsigned char> addrSerialized(ss.begin(), ss.end());
|
template libzcash::PaymentAddress CZCEncoding<libzcash::PaymentAddress,
|
||||||
assert(addrSerialized.size() == libzcash::SerializedSpendingKeySize);
|
CChainParams::ZCPAYMENT_ADDRRESS,
|
||||||
SetData(Params().Base58Prefix(CChainParams::ZCSPENDING_KEY), &addrSerialized[0], libzcash::SerializedSpendingKeySize);
|
libzcash::SerializedPaymentAddressSize>::Get() const;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
libzcash::SpendingKey CZCSpendingKey::Get() const
|
|
||||||
{
|
|
||||||
if (vchData.size() != libzcash::SerializedSpendingKeySize) {
|
|
||||||
throw std::runtime_error(
|
|
||||||
"spending key is invalid"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (vchVersion != Params().Base58Prefix(CChainParams::ZCSPENDING_KEY)) {
|
|
||||||
throw std::runtime_error(
|
|
||||||
"spending key is for wrong network type"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<unsigned char> serialized(vchData.begin(), vchData.end());
|
|
||||||
|
|
||||||
CDataStream ss(serialized, SER_NETWORK, PROTOCOL_VERSION);
|
|
||||||
libzcash::SpendingKey ret;
|
|
||||||
ss >> ret;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Explicit instantiations for libzcash::SpendingKey
|
||||||
|
template bool CZCEncoding<libzcash::SpendingKey,
|
||||||
|
CChainParams::ZCSPENDING_KEY,
|
||||||
|
libzcash::SerializedSpendingKeySize>::Set(const libzcash::SpendingKey& sk);
|
||||||
|
template libzcash::SpendingKey CZCEncoding<libzcash::SpendingKey,
|
||||||
|
CChainParams::ZCSPENDING_KEY,
|
||||||
|
libzcash::SerializedSpendingKeySize>::Get() const;
|
||||||
|
|||||||
27
src/base58.h
27
src/base58.h
@@ -96,26 +96,37 @@ public:
|
|||||||
bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
|
bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class CZCPaymentAddress : public CBase58Data {
|
template<class DATA_TYPE, CChainParams::Base58Type PREFIX, size_t SER_SIZE>
|
||||||
|
class CZCEncoding : public CBase58Data {
|
||||||
|
protected:
|
||||||
|
virtual std::string PrependName(const std::string& s) const = 0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
bool Set(const DATA_TYPE& addr);
|
||||||
|
|
||||||
|
DATA_TYPE Get() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
class CZCPaymentAddress : public CZCEncoding<libzcash::PaymentAddress, CChainParams::ZCPAYMENT_ADDRRESS, libzcash::SerializedPaymentAddressSize> {
|
||||||
|
protected:
|
||||||
|
std::string PrependName(const std::string& s) const { return "payment address" + s; }
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool Set(const libzcash::PaymentAddress& addr);
|
|
||||||
CZCPaymentAddress() {}
|
CZCPaymentAddress() {}
|
||||||
|
|
||||||
CZCPaymentAddress(const std::string& strAddress) { SetString(strAddress.c_str(), 2); }
|
CZCPaymentAddress(const std::string& strAddress) { SetString(strAddress.c_str(), 2); }
|
||||||
CZCPaymentAddress(const libzcash::PaymentAddress& addr) { Set(addr); }
|
CZCPaymentAddress(const libzcash::PaymentAddress& addr) { Set(addr); }
|
||||||
|
|
||||||
libzcash::PaymentAddress Get() const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class CZCSpendingKey : public CBase58Data {
|
class CZCSpendingKey : public CZCEncoding<libzcash::SpendingKey, CChainParams::ZCSPENDING_KEY, libzcash::SerializedSpendingKeySize> {
|
||||||
|
protected:
|
||||||
|
std::string PrependName(const std::string& s) const { return "spending key" + s; }
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool Set(const libzcash::SpendingKey& addr);
|
|
||||||
CZCSpendingKey() {}
|
CZCSpendingKey() {}
|
||||||
|
|
||||||
CZCSpendingKey(const std::string& strAddress) { SetString(strAddress.c_str(), 2); }
|
CZCSpendingKey(const std::string& strAddress) { SetString(strAddress.c_str(), 2); }
|
||||||
CZCSpendingKey(const libzcash::SpendingKey& addr) { Set(addr); }
|
CZCSpendingKey(const libzcash::SpendingKey& addr) { Set(addr); }
|
||||||
|
|
||||||
libzcash::SpendingKey Get() const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** base58-encoded Bitcoin addresses.
|
/** base58-encoded Bitcoin addresses.
|
||||||
|
|||||||
Reference in New Issue
Block a user