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());
|
||||
}
|
||||
|
||||
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);
|
||||
ss << addr;
|
||||
std::vector<unsigned char> addrSerialized(ss.begin(), ss.end());
|
||||
assert(addrSerialized.size() == libzcash::SerializedPaymentAddressSize);
|
||||
SetData(Params().Base58Prefix(CChainParams::ZCPAYMENT_ADDRRESS), &addrSerialized[0], libzcash::SerializedPaymentAddressSize);
|
||||
assert(addrSerialized.size() == SER_SIZE);
|
||||
SetData(Params().Base58Prefix(PREFIX), &addrSerialized[0], SER_SIZE);
|
||||
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(
|
||||
"payment address is invalid"
|
||||
PrependName(" is invalid")
|
||||
);
|
||||
}
|
||||
|
||||
if (vchVersion != Params().Base58Prefix(CChainParams::ZCPAYMENT_ADDRRESS)) {
|
||||
if (vchVersion != Params().Base58Prefix(PREFIX)) {
|
||||
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());
|
||||
|
||||
CDataStream ss(serialized, SER_NETWORK, PROTOCOL_VERSION);
|
||||
libzcash::PaymentAddress ret;
|
||||
DATA_TYPE ret;
|
||||
ss >> ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool CZCSpendingKey::Set(const libzcash::SpendingKey& addr)
|
||||
{
|
||||
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
||||
ss << addr;
|
||||
std::vector<unsigned char> addrSerialized(ss.begin(), ss.end());
|
||||
assert(addrSerialized.size() == libzcash::SerializedSpendingKeySize);
|
||||
SetData(Params().Base58Prefix(CChainParams::ZCSPENDING_KEY), &addrSerialized[0], libzcash::SerializedSpendingKeySize);
|
||||
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::PaymentAddress
|
||||
template bool CZCEncoding<libzcash::PaymentAddress,
|
||||
CChainParams::ZCPAYMENT_ADDRRESS,
|
||||
libzcash::SerializedPaymentAddressSize>::Set(const libzcash::PaymentAddress& addr);
|
||||
template libzcash::PaymentAddress CZCEncoding<libzcash::PaymentAddress,
|
||||
CChainParams::ZCPAYMENT_ADDRRESS,
|
||||
libzcash::SerializedPaymentAddressSize>::Get() const;
|
||||
|
||||
// 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;
|
||||
|
||||
Reference in New Issue
Block a user