Auto merge of #3579 - Eirik0:3577-generate-sprout-zkey, r=bitcartel

Make it clear that CWallet::GenerateNewZKey is Sprout specific

Closes https://github.com/zcash/zcash/issues/3577

When adding sapling support we had considered making the method `GenerateNewZKey` generic, but it ended up making more sense to add a second method `GenerateNewSaplingZKey` for sapling support.

This PR changes the name of `GenerateNewZKey` to `GenerateNewSproutZKey` and changes the return type from `libzcash::PaymentAddress` to `libzcash::SproutPaymentAddress` to make it more clear that this is what the method does.
This commit is contained in:
Homu
2018-10-18 19:47:46 -07:00
5 changed files with 21 additions and 30 deletions

View File

@@ -105,7 +105,7 @@ TEST(wallet_zkeys_tests, StoreAndLoadSaplingZkeys) {
/**
* This test covers methods on CWallet
* GenerateNewZKey()
* GenerateNewSproutZKey()
* AddSproutZKey()
* LoadZKey()
* LoadZKeyMetadata()
@@ -121,9 +121,7 @@ TEST(wallet_zkeys_tests, store_and_load_zkeys) {
ASSERT_EQ(0, addrs.size());
// wallet should have one key
auto address = wallet.GenerateNewZKey();
ASSERT_NE(boost::get<libzcash::SproutPaymentAddress>(&address), nullptr);
auto addr = boost::get<libzcash::SproutPaymentAddress>(address);
auto addr = wallet.GenerateNewSproutZKey();
wallet.GetSproutPaymentAddresses(addrs);
ASSERT_EQ(1, addrs.size());
@@ -236,7 +234,7 @@ TEST(wallet_zkeys_tests, write_zkey_direct_to_db) {
ASSERT_EQ(0, addrs.size());
// Add random key to the wallet
auto paymentAddress = wallet.GenerateNewZKey();
auto paymentAddress = wallet.GenerateNewSproutZKey();
// wallet should have one key
wallet.GetSproutPaymentAddresses(addrs);
@@ -353,9 +351,7 @@ TEST(wallet_zkeys_tests, write_cryptedzkey_direct_to_db) {
ASSERT_EQ(0, addrs.size());
// Add random key to the wallet
auto address = wallet.GenerateNewZKey();
ASSERT_NE(boost::get<libzcash::SproutPaymentAddress>(&address), nullptr);
auto paymentAddress = boost::get<libzcash::SproutPaymentAddress>(address);
auto paymentAddress = wallet.GenerateNewSproutZKey();
// wallet should have one key
wallet.GetSproutPaymentAddresses(addrs);
@@ -368,13 +364,11 @@ TEST(wallet_zkeys_tests, write_cryptedzkey_direct_to_db) {
ASSERT_TRUE(wallet.EncryptWallet(strWalletPass));
// adding a new key will fail as the wallet is locked
EXPECT_ANY_THROW(wallet.GenerateNewZKey());
EXPECT_ANY_THROW(wallet.GenerateNewSproutZKey());
// unlock wallet and then add
wallet.Unlock(strWalletPass);
auto address2 = wallet.GenerateNewZKey();
ASSERT_NE(boost::get<libzcash::SproutPaymentAddress>(&address2), nullptr);
auto paymentAddress2 = boost::get<libzcash::SproutPaymentAddress>(address2);
auto paymentAddress2 = wallet.GenerateNewSproutZKey();
// Create a new wallet from the existing wallet path
CWallet wallet2("wallet_crypted.dat");

View File

@@ -3166,7 +3166,7 @@ UniValue z_getnewaddress(const UniValue& params, bool fHelp)
}
if (addrType == ADDR_TYPE_SPROUT) {
return EncodePaymentAddress(pwalletMain->GenerateNewZKey());
return EncodePaymentAddress(pwalletMain->GenerateNewSproutZKey());
} else if (addrType == ADDR_TYPE_SAPLING) {
return EncodePaymentAddress(pwalletMain->GenerateNewSaplingZKey());
} else {

View File

@@ -82,23 +82,23 @@ const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const
}
// Generate a new spending key and return its public payment address
libzcash::PaymentAddress CWallet::GenerateNewZKey()
libzcash::SproutPaymentAddress CWallet::GenerateNewSproutZKey()
{
AssertLockHeld(cs_wallet); // mapSproutZKeyMetadata
// TODO: Add Sapling support
auto k = SproutSpendingKey::random();
auto addr = k.address();
// Check for collision, even though it is unlikely to ever occur
if (CCryptoKeyStore::HaveSproutSpendingKey(addr))
throw std::runtime_error("CWallet::GenerateNewZKey(): Collision detected");
throw std::runtime_error("CWallet::GenerateNewSproutZKey(): Collision detected");
// Create new metadata
int64_t nCreationTime = GetTime();
mapSproutZKeyMetadata[addr] = CKeyMetadata(nCreationTime);
if (!AddSproutZKey(k))
throw std::runtime_error("CWallet::GenerateNewZKey(): AddSproutZKey failed");
throw std::runtime_error("CWallet::GenerateNewSproutZKey(): AddSproutZKey failed");
return addr;
}

View File

@@ -1042,10 +1042,10 @@ public:
void GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const;
/**
* ZKeys
* Sprout ZKeys
*/
//! Generates a new zaddr
libzcash::PaymentAddress GenerateNewZKey();
//! Generates a new Sprout zaddr
libzcash::SproutPaymentAddress GenerateNewSproutZKey();
//! Adds spending key to the store, and saves it to disk
bool AddSproutZKey(const libzcash::SproutSpendingKey &key);
//! Adds spending key to the store, without saving it to disk (used by LoadWallet)