Introduce wrappers around CZCViewingKey

This commit is contained in:
Jack Grigg 2018-04-24 22:53:54 +01:00
parent 472f75bc2d
commit 8bf3a3d700
No known key found for this signature in database
GPG Key ID: 665DBCD284F7DAFF
4 changed files with 25 additions and 7 deletions

View File

@ -379,6 +379,21 @@ boost::optional<libzcash::PaymentAddress> DecodePaymentAddress(const std::string
}
}
std::string EncodeViewingKey(const libzcash::ViewingKey& vk)
{
return CZCViewingKey(vk).ToString();
}
boost::optional<libzcash::ViewingKey> DecodeViewingKey(const std::string& str)
{
CZCViewingKey vk(str);
try {
return vk.Get();
} catch (const std::runtime_error&) {
return boost::none;
}
}
std::string EncodeSpendingKey(const libzcash::SpendingKey& zkey)
{
return CZCSpendingKey(zkey).ToString();

View File

@ -182,6 +182,9 @@ bool IsValidDestinationString(const std::string& str, const CChainParams& params
std::string EncodePaymentAddress(const libzcash::PaymentAddress& zaddr);
boost::optional<libzcash::PaymentAddress> DecodePaymentAddress(const std::string& str);
std::string EncodeViewingKey(const libzcash::ViewingKey& vk);
boost::optional<libzcash::ViewingKey> DecodeViewingKey(const std::string& str);
std::string EncodeSpendingKey(const libzcash::SpendingKey& zkey);
boost::optional<libzcash::SpendingKey> DecodeSpendingKey(const std::string& str);

View File

@ -705,8 +705,11 @@ UniValue z_importviewingkey(const UniValue& params, bool fHelp)
}
string strVKey = params[0].get_str();
CZCViewingKey viewingkey(strVKey);
auto vkey = viewingkey.Get();
auto viewingkey = DecodeViewingKey(strVKey);
if (!viewingkey) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid viewing key");
}
auto vkey = *viewingkey;
auto addr = vkey.address();
{
@ -815,6 +818,5 @@ UniValue z_exportviewingkey(const UniValue& params, bool fHelp)
vk = k.viewing_key();
}
CZCViewingKey viewingkey(vk);
return viewingkey.ToString();
return EncodeViewingKey(vk);
}

View File

@ -3067,12 +3067,10 @@ UniValue zc_raw_keygen(const UniValue& params, bool fHelp)
auto addr = k.address();
auto viewing_key = k.viewing_key();
CZCViewingKey viewingkey(viewing_key);
UniValue result(UniValue::VOBJ);
result.push_back(Pair("zcaddress", EncodePaymentAddress(addr)));
result.push_back(Pair("zcsecretkey", EncodeSpendingKey(k)));
result.push_back(Pair("zcviewingkey", viewingkey.ToString()));
result.push_back(Pair("zcviewingkey", EncodeViewingKey(viewing_key)));
return result;
}