From dd4c8a3c7057b2ae3a498b1a5a088194aa9cdb42 Mon Sep 17 00:00:00 2001 From: Jay Graber Date: Wed, 11 Jul 2018 13:46:41 -0700 Subject: [PATCH] Add Sapling support to z_importkey --- src/wallet/rpcdump.cpp | 74 ++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index fe4b66ec5..c10b0af95 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -620,34 +620,58 @@ UniValue z_importkey(const UniValue& params, bool fHelp) if (!IsValidSpendingKey(spendingkey)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid spending key"); } - // TODO: Add Sapling support. For now, ensure we can freely convert. - assert(boost::get(&spendingkey) != nullptr); - auto key = boost::get(spendingkey); - auto addr = key.address(); - - { - // Don't throw error in case a key is already there - if (pwalletMain->HaveSpendingKey(addr)) { - if (fIgnoreExistingKey) { - return NullUniValue; + // Sapling support + bool isValidKey = boost::get(&spendingkey) != nullptr || boost::get(&spendingkey) != nullptr; + assert(isValidKey); + + if (boost::get(&spendingkey) != nullptr) { + auto key = boost::get(spendingkey); + auto addr = key.address(); + { + // Don't throw error in case a key is already there + if (pwalletMain->HaveSpendingKey(addr)) { + if (fIgnoreExistingKey) { + return NullUniValue; + } + } else { + pwalletMain->MarkDirty(); + + if (!pwalletMain-> AddZKey(key)) { + throw JSONRPCError(RPC_WALLET_ERROR, "Error adding spending key to wallet"); + } + + pwalletMain->mapZKeyMetadata[addr].nCreateTime = 1; } - } else { - pwalletMain->MarkDirty(); - - if (!pwalletMain-> AddZKey(key)) - throw JSONRPCError(RPC_WALLET_ERROR, "Error adding spending key to wallet"); - - pwalletMain->mapZKeyMetadata[addr].nCreateTime = 1; } - - // whenever a key is imported, we need to scan the whole chain - pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value' - - // We want to scan for transactions and notes - if (fRescan) { - pwalletMain->ScanForWalletTransactions(chainActive[nRescanHeight], true); + } else { + auto sk = boost::get(spendingkey); + auto fvk = sk.full_viewing_key(); + auto addr = sk.default_address(); + { + // Don't throw error in case a key is already there + if (pwalletMain->HaveSaplingSpendingKey(fvk)) { + if (fIgnoreExistingKey) { + return NullUniValue; + } + } else { + pwalletMain->MarkDirty(); + + if (!pwalletMain-> AddSaplingZKey(sk)) { + throw JSONRPCError(RPC_WALLET_ERROR, "Error adding spending key to wallet"); + } + + pwalletMain->mapSaplingZKeyMetadata[addr].nCreateTime = 1; + } } } + + // whenever a key is imported, we need to scan the whole chain + pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value' + + // We want to scan for transactions and notes + if (fRescan) { + pwalletMain->ScanForWalletTransactions(chainActive[nRescanHeight], true); + } return NullUniValue; } @@ -776,7 +800,7 @@ UniValue z_exportkey(const UniValue& params, bool fHelp) if (!IsValidPaymentAddress(address)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid zaddr"); } - // TODO: Add Sapling support. For now, ensure we can freely convert. + // Sapling support bool isValidAddr = boost::get(&address) != nullptr || boost::get(&address) != nullptr; assert(isValidAddr);