diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index c2f4f510f..e9ce51bcb 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -563,67 +563,6 @@ UniValue dumpwallet_impl(const UniValue& params, bool fHelp, bool fDumpZKeys) return exportfilepath.string(); } -class AddSpendingKeyToWallet : public boost::static_visitor -{ -private: - CWallet *m_wallet; - const Consensus::Params ¶ms; -public: - AddSpendingKeyToWallet(CWallet *wallet, const Consensus::Params ¶ms) : - m_wallet(wallet), params(params) {} - - bool operator()(const libzcash::SproutSpendingKey &sk) const { - auto addr = sk.address(); - // Don't throw error in case a key is already there - if (m_wallet->HaveSproutSpendingKey(addr)) { - return true; - } else { - m_wallet->MarkDirty(); - - if (!m_wallet-> AddSproutZKey(sk)) { - throw JSONRPCError(RPC_WALLET_ERROR, "Error adding spending key to wallet"); - } - - m_wallet->mapSproutZKeyMetadata[addr].nCreateTime = 1; - - return false; - } - } - - bool operator()(const libzcash::SaplingExtendedSpendingKey &sk) const { - auto fvk = sk.expsk.full_viewing_key(); - auto ivk = fvk.in_viewing_key(); - auto addr = sk.DefaultAddress(); - { - // Don't throw error in case a key is already there - if (m_wallet->HaveSaplingSpendingKey(fvk)) { - return true; - } else { - m_wallet->MarkDirty(); - - if (!m_wallet-> AddSaplingZKey(sk, addr)) { - throw JSONRPCError(RPC_WALLET_ERROR, "Error adding spending key to wallet"); - } - - // Sapling addresses can't have been used in transactions prior to activation. - if (params.vUpgrades[Consensus::UPGRADE_SAPLING].nActivationHeight == Consensus::NetworkUpgrade::ALWAYS_ACTIVE) { - m_wallet->mapSaplingZKeyMetadata[ivk].nCreateTime = 1; - } else { - // Friday, 26 October 2018 00:00:00 GMT - definitely before Sapling activates - m_wallet->mapSaplingZKeyMetadata[ivk].nCreateTime = 1540512000; - } - - return false; - } - } - } - - bool operator()(const libzcash::InvalidEncoding& no) const { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid spending key"); - } - -}; - UniValue z_importkey(const UniValue& params, bool fHelp) { diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index e39c687c3..dc2670f9d 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -14,6 +14,7 @@ #include "key_io.h" #include "main.h" #include "net.h" +#include "rpc/protocol.h" #include "script/script.h" #include "script/sign.h" #include "timedata.h" @@ -4567,3 +4568,53 @@ boost::optional GetSpendingKeyForPaymentAddress::operator // Defaults to InvalidEncoding return libzcash::SpendingKey(); } + +bool AddSpendingKeyToWallet::operator()(const libzcash::SproutSpendingKey &sk) const { + auto addr = sk.address(); + // Don't throw error in case a key is already there + if (m_wallet->HaveSproutSpendingKey(addr)) { + return true; + } else { + m_wallet->MarkDirty(); + + if (!m_wallet-> AddSproutZKey(sk)) { + throw JSONRPCError(RPC_WALLET_ERROR, "Error adding spending key to wallet"); + } + + m_wallet->mapSproutZKeyMetadata[addr].nCreateTime = 1; + + return false; + } +} + +bool AddSpendingKeyToWallet::operator()(const libzcash::SaplingExtendedSpendingKey &sk) const { + auto fvk = sk.expsk.full_viewing_key(); + auto ivk = fvk.in_viewing_key(); + auto addr = sk.DefaultAddress(); + { + // Don't throw error in case a key is already there + if (m_wallet->HaveSaplingSpendingKey(fvk)) { + return true; + } else { + m_wallet->MarkDirty(); + + if (!m_wallet-> AddSaplingZKey(sk, addr)) { + throw JSONRPCError(RPC_WALLET_ERROR, "Error adding spending key to wallet"); + } + + // Sapling addresses can't have been used in transactions prior to activation. + if (params.vUpgrades[Consensus::UPGRADE_SAPLING].nActivationHeight == Consensus::NetworkUpgrade::ALWAYS_ACTIVE) { + m_wallet->mapSaplingZKeyMetadata[ivk].nCreateTime = 1; + } else { + // Friday, 26 October 2018 00:00:00 GMT - definitely before Sapling activates + m_wallet->mapSaplingZKeyMetadata[ivk].nCreateTime = 1540512000; + } + + return false; + } + } +} + +bool AddSpendingKeyToWallet::operator()(const libzcash::InvalidEncoding& no) const { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid spending key"); +} diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 1b7331d0c..c0b36d88d 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1376,4 +1376,19 @@ public: boost::optional operator()(const libzcash::InvalidEncoding& no) const; }; +class AddSpendingKeyToWallet : public boost::static_visitor +{ +private: + CWallet *m_wallet; + const Consensus::Params ¶ms; +public: + AddSpendingKeyToWallet(CWallet *wallet, const Consensus::Params ¶ms) : + m_wallet(wallet), params(params) {} + + bool operator()(const libzcash::SproutSpendingKey &sk) const; + bool operator()(const libzcash::SaplingExtendedSpendingKey &sk) const; + bool operator()(const libzcash::InvalidEncoding& no) const; +}; + + #endif // BITCOIN_WALLET_WALLET_H