diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index e9ce51bcb..9dc7f69fc 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -635,11 +635,15 @@ UniValue z_importkey(const UniValue& params, bool fHelp) } // Sapling support - auto keyAlreadyExists = boost::apply_visitor( + auto addResult = boost::apply_visitor( AddSpendingKeyToWallet(pwalletMain, Params().GetConsensus()), spendingkey); - if (keyAlreadyExists && fIgnoreExistingKey) { + if (addResult == KeyAlreadyExists && fIgnoreExistingKey) { return NullUniValue; } + pwalletMain->MarkDirty(); + if (addResult == KeyNotAdded) { + throw JSONRPCError(RPC_WALLET_ERROR, "Error adding spending key to wallet"); + } // whenever a key is imported, we need to scan the whole chain pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value' diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index dc2670f9d..e4695b301 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4569,37 +4569,30 @@ boost::optional GetSpendingKeyForPaymentAddress::operator return libzcash::SpendingKey(); } -bool AddSpendingKeyToWallet::operator()(const libzcash::SproutSpendingKey &sk) const { +SpendingKeyAddResult 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"); - } - + return KeyAlreadyExists; + } else if (m_wallet-> AddSproutZKey(sk)) { m_wallet->mapSproutZKeyMetadata[addr].nCreateTime = 1; - - return false; + return KeyAdded; + } else { + return KeyNotAdded; } } -bool AddSpendingKeyToWallet::operator()(const libzcash::SaplingExtendedSpendingKey &sk) const { +SpendingKeyAddResult 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; + return KeyAlreadyExists; } else { - m_wallet->MarkDirty(); - if (!m_wallet-> AddSaplingZKey(sk, addr)) { - throw JSONRPCError(RPC_WALLET_ERROR, "Error adding spending key to wallet"); + return KeyNotAdded; } // Sapling addresses can't have been used in transactions prior to activation. @@ -4610,11 +4603,11 @@ bool AddSpendingKeyToWallet::operator()(const libzcash::SaplingExtendedSpendingK m_wallet->mapSaplingZKeyMetadata[ivk].nCreateTime = 1540512000; } - return false; + return KeyAdded; } } } -bool AddSpendingKeyToWallet::operator()(const libzcash::InvalidEncoding& no) const { +SpendingKeyAddResult 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 c0b36d88d..0032138e1 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1376,7 +1376,13 @@ public: boost::optional operator()(const libzcash::InvalidEncoding& no) const; }; -class AddSpendingKeyToWallet : public boost::static_visitor +enum SpendingKeyAddResult { + KeyAlreadyExists, + KeyAdded, + KeyNotAdded, +}; + +class AddSpendingKeyToWallet : public boost::static_visitor { private: CWallet *m_wallet; @@ -1385,9 +1391,9 @@ 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; + SpendingKeyAddResult operator()(const libzcash::SproutSpendingKey &sk) const; + SpendingKeyAddResult operator()(const libzcash::SaplingExtendedSpendingKey &sk) const; + SpendingKeyAddResult operator()(const libzcash::InvalidEncoding& no) const; };