Return more information when adding a spending key

This commit is contained in:
Eirik Ogilvie-Wigley
2018-09-12 05:03:13 -06:00
parent fcab001b1e
commit 0f03de5536
3 changed files with 27 additions and 24 deletions

View File

@@ -635,11 +635,15 @@ UniValue z_importkey(const UniValue& params, bool fHelp)
} }
// Sapling support // Sapling support
auto keyAlreadyExists = boost::apply_visitor( auto addResult = boost::apply_visitor(
AddSpendingKeyToWallet(pwalletMain, Params().GetConsensus()), spendingkey); AddSpendingKeyToWallet(pwalletMain, Params().GetConsensus()), spendingkey);
if (keyAlreadyExists && fIgnoreExistingKey) { if (addResult == KeyAlreadyExists && fIgnoreExistingKey) {
return NullUniValue; 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 // whenever a key is imported, we need to scan the whole chain
pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value' pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'

View File

@@ -4569,37 +4569,30 @@ boost::optional<libzcash::SpendingKey> GetSpendingKeyForPaymentAddress::operator
return libzcash::SpendingKey(); return libzcash::SpendingKey();
} }
bool AddSpendingKeyToWallet::operator()(const libzcash::SproutSpendingKey &sk) const { SpendingKeyAddResult AddSpendingKeyToWallet::operator()(const libzcash::SproutSpendingKey &sk) const {
auto addr = sk.address(); auto addr = sk.address();
// Don't throw error in case a key is already there // Don't throw error in case a key is already there
if (m_wallet->HaveSproutSpendingKey(addr)) { if (m_wallet->HaveSproutSpendingKey(addr)) {
return true; return KeyAlreadyExists;
} else { } else if (m_wallet-> AddSproutZKey(sk)) {
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; m_wallet->mapSproutZKeyMetadata[addr].nCreateTime = 1;
return KeyAdded;
return false; } 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 fvk = sk.expsk.full_viewing_key();
auto ivk = fvk.in_viewing_key(); auto ivk = fvk.in_viewing_key();
auto addr = sk.DefaultAddress(); auto addr = sk.DefaultAddress();
{ {
// Don't throw error in case a key is already there // Don't throw error in case a key is already there
if (m_wallet->HaveSaplingSpendingKey(fvk)) { if (m_wallet->HaveSaplingSpendingKey(fvk)) {
return true; return KeyAlreadyExists;
} else { } else {
m_wallet->MarkDirty();
if (!m_wallet-> AddSaplingZKey(sk, addr)) { 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. // 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; 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"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid spending key");
} }

View File

@@ -1376,7 +1376,13 @@ public:
boost::optional<libzcash::SpendingKey> operator()(const libzcash::InvalidEncoding& no) const; boost::optional<libzcash::SpendingKey> operator()(const libzcash::InvalidEncoding& no) const;
}; };
class AddSpendingKeyToWallet : public boost::static_visitor<bool> enum SpendingKeyAddResult {
KeyAlreadyExists,
KeyAdded,
KeyNotAdded,
};
class AddSpendingKeyToWallet : public boost::static_visitor<SpendingKeyAddResult>
{ {
private: private:
CWallet *m_wallet; CWallet *m_wallet;
@@ -1385,9 +1391,9 @@ public:
AddSpendingKeyToWallet(CWallet *wallet, const Consensus::Params &params) : AddSpendingKeyToWallet(CWallet *wallet, const Consensus::Params &params) :
m_wallet(wallet), params(params) {} m_wallet(wallet), params(params) {}
bool operator()(const libzcash::SproutSpendingKey &sk) const; SpendingKeyAddResult operator()(const libzcash::SproutSpendingKey &sk) const;
bool operator()(const libzcash::SaplingExtendedSpendingKey &sk) const; SpendingKeyAddResult operator()(const libzcash::SaplingExtendedSpendingKey &sk) const;
bool operator()(const libzcash::InvalidEncoding& no) const; SpendingKeyAddResult operator()(const libzcash::InvalidEncoding& no) const;
}; };