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,34 +620,58 @@ 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) {
{ auto key = boost::get<libzcash::SproutSpendingKey>(spendingkey);
// Don't throw error in case a key is already there auto addr = key.address();
if (pwalletMain->HaveSpendingKey(addr)) { {
if (fIgnoreExistingKey) { // Don't throw error in case a key is already there
return NullUniValue; 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 {
// whenever a key is imported, we need to scan the whole chain auto sk = boost::get<libzcash::SaplingSpendingKey>(spendingkey);
pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value' auto fvk = sk.full_viewing_key();
auto addr = sk.default_address();
// We want to scan for transactions and notes {
if (fRescan) { // Don't throw error in case a key is already there
pwalletMain->ScanForWalletTransactions(chainActive[nRescanHeight], true); 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; 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);