diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index dea3ea245..e4a6e179d 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -784,41 +784,6 @@ UniValue z_importviewingkey(const UniValue& params, bool fHelp) return NullUniValue; } -class GetSpendingKeyForPaymentAddress : public boost::static_visitor -{ -private: - CWallet *m_wallet; -public: - GetSpendingKeyForPaymentAddress(CWallet *wallet) : m_wallet(wallet) {} - - libzcash::SpendingKey operator()(const libzcash::SproutPaymentAddress &zaddr) const - { - libzcash::SproutSpendingKey k; - if (!pwalletMain->GetSproutSpendingKey(zaddr, k)) { - throw JSONRPCError(RPC_WALLET_ERROR, "Wallet does not hold private zkey for this zaddr"); - } - return k; - } - - libzcash::SpendingKey operator()(const libzcash::SaplingPaymentAddress &zaddr) const - { - libzcash::SaplingIncomingViewingKey ivk; - libzcash::SaplingFullViewingKey fvk; - libzcash::SaplingSpendingKey sk; - - if (!pwalletMain->GetSaplingIncomingViewingKey(zaddr, ivk) || - !pwalletMain->GetSaplingFullViewingKey(ivk, fvk) || - !pwalletMain->GetSaplingSpendingKey(fvk, sk)) { - throw JSONRPCError(RPC_WALLET_ERROR, "Wallet does not hold private zkey for this zaddr"); - } - return sk; - } - - libzcash::SpendingKey operator()(const libzcash::InvalidEncoding& no) const { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid zaddr"); - } -}; - UniValue z_exportkey(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) @@ -852,7 +817,10 @@ UniValue z_exportkey(const UniValue& params, bool fHelp) // Sapling support auto sk = boost::apply_visitor(GetSpendingKeyForPaymentAddress(pwalletMain), address); - return EncodeSpendingKey(sk); + if (!sk) { + throw JSONRPCError(RPC_WALLET_ERROR, "Wallet does not hold private zkey for this zaddr"); + } + return EncodeSpendingKey(sk.get()); } UniValue z_exportviewingkey(const UniValue& params, bool fHelp) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 04a61ed13..e016cc476 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4373,3 +4373,37 @@ bool PaymentAddressBelongsToWallet::operator()(const libzcash::InvalidEncoding& { return false; } + +boost::optional GetSpendingKeyForPaymentAddress::operator()( + const libzcash::SproutPaymentAddress &zaddr) const +{ + libzcash::SproutSpendingKey k; + if (m_wallet->GetSproutSpendingKey(zaddr, k)) { + return libzcash::SpendingKey(k); + } else { + return boost::none; + } +} + +boost::optional GetSpendingKeyForPaymentAddress::operator()( + const libzcash::SaplingPaymentAddress &zaddr) const +{ + libzcash::SaplingIncomingViewingKey ivk; + libzcash::SaplingFullViewingKey fvk; + libzcash::SaplingSpendingKey sk; + + if (m_wallet->GetSaplingIncomingViewingKey(zaddr, ivk) && + m_wallet->GetSaplingFullViewingKey(ivk, fvk) && + m_wallet->GetSaplingSpendingKey(fvk, sk)) { + return libzcash::SpendingKey(sk); + } else { + return boost::none; + } +} + +boost::optional GetSpendingKeyForPaymentAddress::operator()( + const libzcash::InvalidEncoding& no) const +{ + // Defaults to InvalidEncoding + return libzcash::SpendingKey(); +} diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index acefa01d8..2e23b2169 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1319,4 +1319,16 @@ public: bool operator()(const libzcash::InvalidEncoding& no) const; }; +class GetSpendingKeyForPaymentAddress : public boost::static_visitor> +{ +private: + CWallet *m_wallet; +public: + GetSpendingKeyForPaymentAddress(CWallet *wallet) : m_wallet(wallet) {} + + boost::optional operator()(const libzcash::SproutPaymentAddress &zaddr) const; + boost::optional operator()(const libzcash::SaplingPaymentAddress &zaddr) const; + boost::optional operator()(const libzcash::InvalidEncoding& no) const; +}; + #endif // BITCOIN_WALLET_WALLET_H