return address info in z_importviewingkey

This commit is contained in:
Alfredo Garcia 2020-04-06 17:11:50 -03:00
parent 1be7250db9
commit 0a6ab8a5ab
5 changed files with 41 additions and 8 deletions

View File

@ -122,10 +122,14 @@ class WalletNullifiersTest (BitcoinTestFramework):
'total': node3mined + zsendmany2notevalue,
})
# add node 1 address and node 2 viewing key to node 3
# Add node 1 address and node 2 viewing key to node 3
myzvkey = self.nodes[2].z_exportviewingkey(myzaddr)
self.nodes[3].importaddress(mytaddr1)
self.nodes[3].z_importviewingkey(myzvkey, 'whenkeyisnew', 1)
importvk_result = self.nodes[3].z_importviewingkey(myzvkey, 'whenkeyisnew', 1)
# Check results of z_importviewingkey
assert_equal(importvk_result["type"], "sprout")
assert_equal(importvk_result["address"], myzaddr)
# Check the address has been imported
assert_equal(myzaddr in self.nodes[3].z_listaddresses(), False)

View File

@ -144,11 +144,13 @@ class WalletSaplingTest(BitcoinTestFramework):
# Verify importing a viewing key will update the nullifiers and witnesses correctly
extfvk0 = self.nodes[0].z_exportviewingkey(saplingAddr0)
self.nodes[3].z_importviewingkey(extfvk0, "yes")
assert_equal(self.nodes[3].z_getbalance(saplingAddr0), Decimal('10'))
saplingAddrInfo0 = self.nodes[3].z_importviewingkey(extfvk0, "yes")
assert_equal(saplingAddrInfo0["type"], "sapling")
assert_equal(self.nodes[3].z_getbalance(saplingAddrInfo0["address"]), Decimal('10'))
extfvk1 = self.nodes[1].z_exportviewingkey(saplingAddr1)
self.nodes[3].z_importviewingkey(extfvk1, "yes")
assert_equal(self.nodes[3].z_getbalance(saplingAddr1), Decimal('5'))
saplingAddrInfo1 = self.nodes[3].z_importviewingkey(extfvk1, "yes")
assert_equal(saplingAddrInfo0["type"], "sapling")
assert_equal(self.nodes[3].z_getbalance(saplingAddrInfo1["address"]), Decimal('5'))
# Verify that z_gettotalbalance only includes watch-only addresses when requested
assert_equal(self.nodes[3].z_gettotalbalance()['private'], '0.00')

View File

@ -783,6 +783,11 @@ UniValue z_importviewingkey(const UniValue& params, bool fHelp)
"2. rescan (string, optional, default=\"whenkeyisnew\") Rescan the wallet for transactions - can be \"yes\", \"no\" or \"whenkeyisnew\"\n"
"3. startHeight (numeric, optional, default=0) Block height to start rescan from\n"
"\nNote: This call can take minutes to complete if rescan is true.\n"
"\nResult:\n"
"{\n"
" \"type\" : \"xxxx\", (string) \"sprout\" or \"sapling\"\n"
" \"address\" : \"address|DefaultAddress\", (string) The address(sprout) or the DefaultAddress(sapling)\n"
"}\n"
"\nExamples:\n"
"\nImport a viewing key\n"
+ HelpExampleCli("z_importviewingkey", "\"vkey\"") +
@ -832,13 +837,18 @@ UniValue z_importviewingkey(const UniValue& params, bool fHelp)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid viewing key");
}
auto addrInfo = boost::apply_visitor(libzcash::AddressInfoFromViewingKey{}, viewingkey);
UniValue result(UniValue::VOBJ);
result.pushKV("type", addrInfo.first);
result.pushKV("address", EncodePaymentAddress(addrInfo.second));
auto addResult = boost::apply_visitor(AddViewingKeyToWallet(pwalletMain), viewingkey);
if (addResult == SpendingKeyExists) {
throw JSONRPCError(
RPC_WALLET_ERROR,
"The wallet already contains the private key for this viewing key");
} else if (addResult == KeyAlreadyExists && fIgnoreExistingKey) {
return NullUniValue;
return result;
}
pwalletMain->MarkDirty();
if (addResult == KeyNotAdded) {
@ -850,7 +860,7 @@ UniValue z_importviewingkey(const UniValue& params, bool fHelp)
pwalletMain->ScanForWalletTransactions(chainActive[nRescanHeight], true);
}
return NullUniValue;
return result;
}
UniValue z_exportkey(const UniValue& params, bool fHelp)

View File

@ -12,6 +12,16 @@ std::pair<std::string, PaymentAddress> AddressInfoFromSpendingKey::operator()(co
throw std::invalid_argument("Cannot derive default address from invalid spending key");
}
std::pair<std::string, PaymentAddress> AddressInfoFromViewingKey::operator()(const SproutViewingKey &sk) const {
return std::make_pair("sprout", sk.address());
}
std::pair<std::string, PaymentAddress> AddressInfoFromViewingKey::operator()(const SaplingExtendedFullViewingKey &sk) const {
return std::make_pair("sapling", sk.DefaultAddress());
}
std::pair<std::string, PaymentAddress> AddressInfoFromViewingKey::operator()(const InvalidEncoding&) const {
throw std::invalid_argument("Cannot derive default address from invalid viewing key");
}
}
bool IsValidPaymentAddress(const libzcash::PaymentAddress& zaddr) {

View File

@ -25,6 +25,13 @@ public:
std::pair<std::string, PaymentAddress> operator()(const InvalidEncoding&) const;
};
class AddressInfoFromViewingKey : public boost::static_visitor<std::pair<std::string, PaymentAddress>> {
public:
std::pair<std::string, PaymentAddress> operator()(const SproutViewingKey&) const;
std::pair<std::string, PaymentAddress> operator()(const struct SaplingExtendedFullViewingKey&) const;
std::pair<std::string, PaymentAddress> operator()(const InvalidEncoding&) const;
};
}
/** Check whether a PaymentAddress is not an InvalidEncoding. */