From 8bf3a3d7006425cdf52dd5d1ca1f1766a9b52db1 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 24 Apr 2018 22:53:54 +0100 Subject: [PATCH] Introduce wrappers around CZCViewingKey --- src/base58.cpp | 15 +++++++++++++++ src/base58.h | 3 +++ src/wallet/rpcdump.cpp | 10 ++++++---- src/wallet/rpcwallet.cpp | 4 +--- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/base58.cpp b/src/base58.cpp index 90982cf95..46a03e0ab 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -379,6 +379,21 @@ boost::optional DecodePaymentAddress(const std::string } } +std::string EncodeViewingKey(const libzcash::ViewingKey& vk) +{ + return CZCViewingKey(vk).ToString(); +} + +boost::optional 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(); diff --git a/src/base58.h b/src/base58.h index da9b7dd1e..0cc62b456 100644 --- a/src/base58.h +++ b/src/base58.h @@ -182,6 +182,9 @@ bool IsValidDestinationString(const std::string& str, const CChainParams& params std::string EncodePaymentAddress(const libzcash::PaymentAddress& zaddr); boost::optional DecodePaymentAddress(const std::string& str); +std::string EncodeViewingKey(const libzcash::ViewingKey& vk); +boost::optional DecodeViewingKey(const std::string& str); + std::string EncodeSpendingKey(const libzcash::SpendingKey& zkey); boost::optional DecodeSpendingKey(const std::string& str); diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index e9354d179..8d61c6170 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -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); } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 7f433bb3c..ed2a120f2 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -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; }