Add Sapling support to z_importkey

This commit is contained in:
Jay Graber
2018-07-11 13:46:41 -07:00
committed by Simon
parent 2afc8eb0ec
commit dd4c8a3c70

View File

@@ -620,35 +620,59 @@ UniValue z_importkey(const UniValue& params, bool fHelp)
if (!IsValidSpendingKey(spendingkey)) { if (!IsValidSpendingKey(spendingkey)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid spending key"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid spending key");
} }
// TODO: Add Sapling support. For now, ensure we can freely convert. // Sapling support
assert(boost::get<libzcash::SproutSpendingKey>(&spendingkey) != nullptr); bool isValidKey = boost::get<libzcash::SproutSpendingKey>(&spendingkey) != nullptr || boost::get<libzcash::SaplingSpendingKey>(&spendingkey) != nullptr;
auto key = boost::get<libzcash::SproutSpendingKey>(spendingkey); assert(isValidKey);
auto addr = key.address();
{ if (boost::get<libzcash::SproutSpendingKey>(&spendingkey) != nullptr) {
// Don't throw error in case a key is already there auto key = boost::get<libzcash::SproutSpendingKey>(spendingkey);
if (pwalletMain->HaveSpendingKey(addr)) { auto addr = key.address();
if (fIgnoreExistingKey) { {
return NullUniValue; // 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;
} }
} else {
auto sk = boost::get<libzcash::SaplingSpendingKey>(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();
// whenever a key is imported, we need to scan the whole chain if (!pwalletMain-> AddSaplingZKey(sk)) {
pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value' throw JSONRPCError(RPC_WALLET_ERROR, "Error adding spending key to wallet");
}
// We want to scan for transactions and notes pwalletMain->mapSaplingZKeyMetadata[addr].nCreateTime = 1;
if (fRescan) { }
pwalletMain->ScanForWalletTransactions(chainActive[nRescanHeight], true);
} }
} }
// 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; return NullUniValue;
} }
@@ -776,7 +800,7 @@ UniValue z_exportkey(const UniValue& params, bool fHelp)
if (!IsValidPaymentAddress(address)) { if (!IsValidPaymentAddress(address)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid zaddr"); 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<libzcash::SproutPaymentAddress>(&address) != nullptr || boost::get<libzcash::SaplingPaymentAddress>(&address) != nullptr; bool isValidAddr = boost::get<libzcash::SproutPaymentAddress>(&address) != nullptr || boost::get<libzcash::SaplingPaymentAddress>(&address) != nullptr;
assert(isValidAddr); assert(isValidAddr);