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
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'

View File

@ -4569,37 +4569,30 @@ boost::optional<libzcash::SpendingKey> 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");
}

View File

@ -1376,7 +1376,13 @@ public:
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:
CWallet *m_wallet;
@ -1385,9 +1391,9 @@ public:
AddSpendingKeyToWallet(CWallet *wallet, const Consensus::Params &params) :
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;
};