Browse Source

Rename *ViewingKey* -> *SproutViewingKey*

pull/4/head
Jack Grigg 6 years ago
parent
commit
4c77517772
No known key found for this signature in database GPG Key ID: 1B8D649257DB0829
  1. 16
      src/gtest/test_keystore.cpp
  2. 21
      src/keystore.cpp
  3. 32
      src/keystore.h
  4. 32
      src/wallet/gtest/test_wallet_zkeys.cpp
  5. 6
      src/wallet/rpcdump.cpp
  6. 4
      src/wallet/rpcwallet.cpp
  7. 20
      src/wallet/wallet.cpp
  8. 10
      src/wallet/wallet.h
  9. 6
      src/wallet/walletdb.cpp
  10. 4
      src/wallet/walletdb.h

16
src/gtest/test_keystore.cpp

@ -117,8 +117,8 @@ TEST(keystore_tests, StoreAndRetrieveViewingKey) {
auto addr = sk.address();
// Sanity-check: we can't get a viewing key we haven't added
EXPECT_FALSE(keyStore.HaveViewingKey(addr));
EXPECT_FALSE(keyStore.GetViewingKey(addr, vkOut));
EXPECT_FALSE(keyStore.HaveSproutViewingKey(addr));
EXPECT_FALSE(keyStore.GetSproutViewingKey(addr, vkOut));
// and we shouldn't have a spending key or decryptor either
EXPECT_FALSE(keyStore.HaveSpendingKey(addr));
@ -130,9 +130,9 @@ TEST(keystore_tests, StoreAndRetrieveViewingKey) {
keyStore.GetPaymentAddresses(addresses);
EXPECT_FALSE(addresses.count(addr));
keyStore.AddViewingKey(vk);
EXPECT_TRUE(keyStore.HaveViewingKey(addr));
EXPECT_TRUE(keyStore.GetViewingKey(addr, vkOut));
keyStore.AddSproutViewingKey(vk);
EXPECT_TRUE(keyStore.HaveSproutViewingKey(addr));
EXPECT_TRUE(keyStore.GetSproutViewingKey(addr, vkOut));
EXPECT_EQ(vk, vkOut);
// We should still not have the spending key...
@ -148,9 +148,9 @@ TEST(keystore_tests, StoreAndRetrieveViewingKey) {
keyStore.GetPaymentAddresses(addresses);
EXPECT_TRUE(addresses.count(addr));
keyStore.RemoveViewingKey(vk);
EXPECT_FALSE(keyStore.HaveViewingKey(addr));
EXPECT_FALSE(keyStore.GetViewingKey(addr, vkOut));
keyStore.RemoveSproutViewingKey(vk);
EXPECT_FALSE(keyStore.HaveSproutViewingKey(addr));
EXPECT_FALSE(keyStore.GetSproutViewingKey(addr, vkOut));
EXPECT_FALSE(keyStore.HaveSpendingKey(addr));
EXPECT_FALSE(keyStore.GetSpendingKey(addr, skOut));
addresses.clear();

21
src/keystore.cpp

@ -111,11 +111,11 @@ bool CBasicKeyStore::AddSaplingSpendingKey(
return true;
}
bool CBasicKeyStore::AddViewingKey(const libzcash::SproutViewingKey &vk)
bool CBasicKeyStore::AddSproutViewingKey(const libzcash::SproutViewingKey &vk)
{
LOCK(cs_SpendingKeyStore);
auto address = vk.address();
mapViewingKeys[address] = vk;
mapSproutViewingKeys[address] = vk;
mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(vk.sk_enc)));
return true;
}
@ -136,17 +136,17 @@ bool CBasicKeyStore::AddSaplingFullViewingKey(
return true;
}
bool CBasicKeyStore::RemoveViewingKey(const libzcash::SproutViewingKey &vk)
bool CBasicKeyStore::RemoveSproutViewingKey(const libzcash::SproutViewingKey &vk)
{
LOCK(cs_SpendingKeyStore);
mapViewingKeys.erase(vk.address());
mapSproutViewingKeys.erase(vk.address());
return true;
}
bool CBasicKeyStore::HaveViewingKey(const libzcash::SproutPaymentAddress &address) const
bool CBasicKeyStore::HaveSproutViewingKey(const libzcash::SproutPaymentAddress &address) const
{
LOCK(cs_SpendingKeyStore);
return mapViewingKeys.count(address) > 0;
return mapSproutViewingKeys.count(address) > 0;
}
bool CBasicKeyStore::HaveSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk) const
@ -161,12 +161,13 @@ bool CBasicKeyStore::HaveSaplingIncomingViewingKey(const libzcash::SaplingPaymen
return mapSaplingIncomingViewingKeys.count(addr) > 0;
}
bool CBasicKeyStore::GetViewingKey(const libzcash::SproutPaymentAddress &address,
libzcash::SproutViewingKey &vkOut) const
bool CBasicKeyStore::GetSproutViewingKey(
const libzcash::SproutPaymentAddress &address,
libzcash::SproutViewingKey &vkOut) const
{
LOCK(cs_SpendingKeyStore);
ViewingKeyMap::const_iterator mi = mapViewingKeys.find(address);
if (mi != mapViewingKeys.end()) {
SproutViewingKeyMap::const_iterator mi = mapSproutViewingKeys.find(address);
if (mi != mapSproutViewingKeys.end()) {
vkOut = mi->second;
return true;
}

32
src/keystore.h

@ -81,18 +81,20 @@ public:
libzcash::SaplingIncomingViewingKey& ivkOut) const =0;
virtual void GetSaplingPaymentAddresses(std::set<libzcash::SaplingPaymentAddress> &setAddress) const =0;
//! Support for viewing keys
virtual bool AddViewingKey(const libzcash::SproutViewingKey &vk) =0;
virtual bool RemoveViewingKey(const libzcash::SproutViewingKey &vk) =0;
virtual bool HaveViewingKey(const libzcash::SproutPaymentAddress &address) const =0;
virtual bool GetViewingKey(const libzcash::SproutPaymentAddress &address, libzcash::SproutViewingKey& vkOut) const =0;
//! Support for Sprout viewing keys
virtual bool AddSproutViewingKey(const libzcash::SproutViewingKey &vk) =0;
virtual bool RemoveSproutViewingKey(const libzcash::SproutViewingKey &vk) =0;
virtual bool HaveSproutViewingKey(const libzcash::SproutPaymentAddress &address) const =0;
virtual bool GetSproutViewingKey(
const libzcash::SproutPaymentAddress &address,
libzcash::SproutViewingKey& vkOut) const =0;
};
typedef std::map<CKeyID, CKey> KeyMap;
typedef std::map<CScriptID, CScript > ScriptMap;
typedef std::set<CScript> WatchOnlySet;
typedef std::map<libzcash::SproutPaymentAddress, libzcash::SproutSpendingKey> SproutSpendingKeyMap;
typedef std::map<libzcash::SproutPaymentAddress, libzcash::SproutViewingKey> ViewingKeyMap;
typedef std::map<libzcash::SproutPaymentAddress, libzcash::SproutViewingKey> SproutViewingKeyMap;
typedef std::map<libzcash::SproutPaymentAddress, ZCNoteDecryption> NoteDecryptorMap;
// Full viewing key has equivalent functionality to a transparent address
@ -111,9 +113,9 @@ protected:
ScriptMap mapScripts;
WatchOnlySet setWatchOnly;
SproutSpendingKeyMap mapSproutSpendingKeys;
ViewingKeyMap mapViewingKeys;
SproutViewingKeyMap mapSproutViewingKeys;
NoteDecryptorMap mapNoteDecryptors;
SaplingSpendingKeyMap mapSaplingSpendingKeys;
SaplingFullViewingKeyMap mapSaplingFullViewingKeys;
SaplingIncomingViewingKeyMap mapSaplingIncomingViewingKeys;
@ -211,8 +213,8 @@ public:
setAddress.insert((*mi).first);
mi++;
}
ViewingKeyMap::const_iterator mvi = mapViewingKeys.begin();
while (mvi != mapViewingKeys.end())
SproutViewingKeyMap::const_iterator mvi = mapSproutViewingKeys.begin();
while (mvi != mapSproutViewingKeys.end())
{
setAddress.insert((*mvi).first);
mvi++;
@ -274,10 +276,12 @@ public:
}
}
virtual bool AddViewingKey(const libzcash::SproutViewingKey &vk);
virtual bool RemoveViewingKey(const libzcash::SproutViewingKey &vk);
virtual bool HaveViewingKey(const libzcash::SproutPaymentAddress &address) const;
virtual bool GetViewingKey(const libzcash::SproutPaymentAddress &address, libzcash::SproutViewingKey& vkOut) const;
virtual bool AddSproutViewingKey(const libzcash::SproutViewingKey &vk);
virtual bool RemoveSproutViewingKey(const libzcash::SproutViewingKey &vk);
virtual bool HaveSproutViewingKey(const libzcash::SproutPaymentAddress &address) const;
virtual bool GetSproutViewingKey(
const libzcash::SproutPaymentAddress &address,
libzcash::SproutViewingKey& vkOut) const;
};
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;

32
src/wallet/gtest/test_wallet_zkeys.cpp

@ -111,9 +111,9 @@ TEST(wallet_zkeys_tests, store_and_load_zkeys) {
/**
* This test covers methods on CWallet
* AddViewingKey()
* RemoveViewingKey()
* LoadViewingKey()
* AddSproutViewingKey()
* RemoveSproutViewingKey()
* LoadSproutViewingKey()
*/
TEST(wallet_zkeys_tests, StoreAndLoadViewingKeys) {
SelectParams(CBaseChainParams::MAIN);
@ -128,32 +128,32 @@ TEST(wallet_zkeys_tests, StoreAndLoadViewingKeys) {
// manually add new viewing key to wallet
auto sk = libzcash::SproutSpendingKey::random();
auto vk = sk.viewing_key();
ASSERT_TRUE(wallet.AddViewingKey(vk));
ASSERT_TRUE(wallet.AddSproutViewingKey(vk));
// verify wallet did add it
auto addr = sk.address();
ASSERT_TRUE(wallet.HaveViewingKey(addr));
ASSERT_TRUE(wallet.HaveSproutViewingKey(addr));
// and that we don't have the corresponding spending key
ASSERT_FALSE(wallet.HaveSpendingKey(addr));
// verify viewing key stored correctly
libzcash::SproutViewingKey vkOut;
wallet.GetViewingKey(addr, vkOut);
wallet.GetSproutViewingKey(addr, vkOut);
ASSERT_EQ(vk, vkOut);
// Load a second viewing key into the wallet
auto sk2 = libzcash::SproutSpendingKey::random();
ASSERT_TRUE(wallet.LoadViewingKey(sk2.viewing_key()));
ASSERT_TRUE(wallet.LoadSproutViewingKey(sk2.viewing_key()));
// verify wallet did add it
auto addr2 = sk2.address();
ASSERT_TRUE(wallet.HaveViewingKey(addr2));
ASSERT_TRUE(wallet.HaveSproutViewingKey(addr2));
ASSERT_FALSE(wallet.HaveSpendingKey(addr2));
// Remove the first viewing key
ASSERT_TRUE(wallet.RemoveViewingKey(vk));
ASSERT_FALSE(wallet.HaveViewingKey(addr));
ASSERT_TRUE(wallet.HaveViewingKey(addr2));
ASSERT_TRUE(wallet.RemoveSproutViewingKey(vk));
ASSERT_FALSE(wallet.HaveSproutViewingKey(addr));
ASSERT_TRUE(wallet.HaveSproutViewingKey(addr2));
}
/**
@ -230,7 +230,7 @@ TEST(wallet_zkeys_tests, write_zkey_direct_to_db) {
/**
* This test covers methods on CWalletDB
* WriteViewingKey()
* WriteSproutViewingKey()
*/
TEST(wallet_zkeys_tests, WriteViewingKeyDirectToDB) {
SelectParams(CBaseChainParams::TESTNET);
@ -255,20 +255,20 @@ TEST(wallet_zkeys_tests, WriteViewingKeyDirectToDB) {
int64_t now = GetTime();
CKeyMetadata meta(now);
CWalletDB db("wallet-vkey.dat");
db.WriteViewingKey(vk);
db.WriteSproutViewingKey(vk);
// wallet should not be aware of viewing key
ASSERT_FALSE(wallet.HaveViewingKey(addr));
ASSERT_FALSE(wallet.HaveSproutViewingKey(addr));
// load the wallet again
ASSERT_EQ(DB_LOAD_OK, wallet.LoadWallet(fFirstRun));
// wallet can now see the viewing key
ASSERT_TRUE(wallet.HaveViewingKey(addr));
ASSERT_TRUE(wallet.HaveSproutViewingKey(addr));
// check key is the same
libzcash::SproutViewingKey vkOut;
wallet.GetViewingKey(addr, vkOut);
wallet.GetSproutViewingKey(addr, vkOut);
ASSERT_EQ(vk, vkOut);
}

6
src/wallet/rpcdump.cpp

@ -763,14 +763,14 @@ UniValue z_importviewingkey(const UniValue& params, bool fHelp)
}
// Don't throw error in case a viewing key is already there
if (pwalletMain->HaveViewingKey(addr)) {
if (pwalletMain->HaveSproutViewingKey(addr)) {
if (fIgnoreExistingKey) {
return NullUniValue;
}
} else {
pwalletMain->MarkDirty();
if (!pwalletMain->AddViewingKey(vkey)) {
if (!pwalletMain->AddSproutViewingKey(vkey)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding viewing key to wallet");
}
}
@ -889,7 +889,7 @@ UniValue z_exportviewingkey(const UniValue& params, bool fHelp)
auto addr = boost::get<libzcash::SproutPaymentAddress>(address);
libzcash::SproutViewingKey vk;
if (!pwalletMain->GetViewingKey(addr, vk)) {
if (!pwalletMain->GetSproutViewingKey(addr, vk)) {
libzcash::SproutSpendingKey k;
if (!pwalletMain->GetSpendingKey(addr, k)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet does not hold private key or viewing key for this zaddr");

4
src/wallet/rpcwallet.cpp

@ -3296,7 +3296,7 @@ UniValue z_listreceivedbyaddress(const UniValue& params, bool fHelp)
assert(boost::get<libzcash::SproutPaymentAddress>(&zaddr) != nullptr);
auto sproutzaddr = boost::get<libzcash::SproutPaymentAddress>(zaddr);
if (!(pwalletMain->HaveSpendingKey(sproutzaddr) || pwalletMain->HaveViewingKey(sproutzaddr))) {
if (!(pwalletMain->HaveSpendingKey(sproutzaddr) || pwalletMain->HaveSproutViewingKey(sproutzaddr))) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "From address does not belong to this node, zaddr spending key or viewing key not found.");
}
@ -3368,7 +3368,7 @@ UniValue z_getbalance(const UniValue& params, bool fHelp)
// TODO: Add Sapling support. For now, ensure we can freely convert.
assert(boost::get<libzcash::SproutPaymentAddress>(&res) != nullptr);
auto zaddr = boost::get<libzcash::SproutPaymentAddress>(res);
if (!(pwalletMain->HaveSpendingKey(zaddr) || pwalletMain->HaveViewingKey(zaddr))) {
if (!(pwalletMain->HaveSpendingKey(zaddr) || pwalletMain->HaveSproutViewingKey(zaddr))) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "From address does not belong to this node, zaddr spending key or viewing key not found.");
}
}

20
src/wallet/wallet.cpp

@ -156,8 +156,8 @@ bool CWallet::AddZKey(const libzcash::SproutSpendingKey &key)
return false;
// check if we need to remove from viewing keys
if (HaveViewingKey(addr))
RemoveViewingKey(key.viewing_key());
if (HaveSproutViewingKey(addr))
RemoveSproutViewingKey(key.viewing_key());
if (!fFileBacked)
return true;
@ -312,26 +312,26 @@ bool CWallet::LoadZKey(const libzcash::SproutSpendingKey &key)
return CCryptoKeyStore::AddSpendingKey(key);
}
bool CWallet::AddViewingKey(const libzcash::SproutViewingKey &vk)
bool CWallet::AddSproutViewingKey(const libzcash::SproutViewingKey &vk)
{
if (!CCryptoKeyStore::AddViewingKey(vk)) {
if (!CCryptoKeyStore::AddSproutViewingKey(vk)) {
return false;
}
nTimeFirstKey = 1; // No birthday information for viewing keys.
if (!fFileBacked) {
return true;
}
return CWalletDB(strWalletFile).WriteViewingKey(vk);
return CWalletDB(strWalletFile).WriteSproutViewingKey(vk);
}
bool CWallet::RemoveViewingKey(const libzcash::SproutViewingKey &vk)
bool CWallet::RemoveSproutViewingKey(const libzcash::SproutViewingKey &vk)
{
AssertLockHeld(cs_wallet);
if (!CCryptoKeyStore::RemoveViewingKey(vk)) {
if (!CCryptoKeyStore::RemoveSproutViewingKey(vk)) {
return false;
}
if (fFileBacked) {
if (!CWalletDB(strWalletFile).EraseViewingKey(vk)) {
if (!CWalletDB(strWalletFile).EraseSproutViewingKey(vk)) {
return false;
}
}
@ -339,9 +339,9 @@ bool CWallet::RemoveViewingKey(const libzcash::SproutViewingKey &vk)
return true;
}
bool CWallet::LoadViewingKey(const libzcash::SproutViewingKey &vk)
bool CWallet::LoadSproutViewingKey(const libzcash::SproutViewingKey &vk)
{
return CCryptoKeyStore::AddViewingKey(vk);
return CCryptoKeyStore::AddSproutViewingKey(vk);
}
bool CWallet::AddCScript(const CScript& redeemScript)

10
src/wallet/wallet.h

@ -994,11 +994,11 @@ public:
//! Adds an encrypted spending key to the store, and saves it to disk (virtual method, declared in crypter.h)
bool AddCryptedSpendingKey(const libzcash::SproutPaymentAddress &address, const libzcash::ReceivingKey &rk, const std::vector<unsigned char> &vchCryptedSecret);
//! Adds a viewing key to the store, and saves it to disk.
bool AddViewingKey(const libzcash::SproutViewingKey &vk);
bool RemoveViewingKey(const libzcash::SproutViewingKey &vk);
//! Adds a viewing key to the store, without saving it to disk (used by LoadWallet)
bool LoadViewingKey(const libzcash::SproutViewingKey &dest);
//! Adds a Sprout viewing key to the store, and saves it to disk.
bool AddSproutViewingKey(const libzcash::SproutViewingKey &vk);
bool RemoveSproutViewingKey(const libzcash::SproutViewingKey &vk);
//! Adds a Sprout viewing key to the store, without saving it to disk (used by LoadWallet)
bool LoadSproutViewingKey(const libzcash::SproutViewingKey &dest);
/**
* Sapling ZKeys

6
src/wallet/walletdb.cpp

@ -142,13 +142,13 @@ bool CWalletDB::WriteZKey(const libzcash::SproutPaymentAddress& addr, const libz
return Write(std::make_pair(std::string("zkey"), addr), key, false);
}
bool CWalletDB::WriteViewingKey(const libzcash::SproutViewingKey &vk)
bool CWalletDB::WriteSproutViewingKey(const libzcash::SproutViewingKey &vk)
{
nWalletDBUpdated++;
return Write(std::make_pair(std::string("vkey"), vk), '1');
}
bool CWalletDB::EraseViewingKey(const libzcash::SproutViewingKey &vk)
bool CWalletDB::EraseSproutViewingKey(const libzcash::SproutViewingKey &vk)
{
nWalletDBUpdated++;
return Erase(std::make_pair(std::string("vkey"), vk));
@ -490,7 +490,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
char fYes;
ssValue >> fYes;
if (fYes == '1')
pwallet->LoadViewingKey(vk);
pwallet->LoadSproutViewingKey(vk);
// Viewing keys have no birthday information for now,
// so set the wallet birthday to the beginning of time.

4
src/wallet/walletdb.h

@ -139,8 +139,8 @@ public:
const std::vector<unsigned char>& vchCryptedSecret,
const CKeyMetadata &keyMeta);
bool WriteViewingKey(const libzcash::SproutViewingKey &vk);
bool EraseViewingKey(const libzcash::SproutViewingKey &vk);
bool WriteSproutViewingKey(const libzcash::SproutViewingKey &vk);
bool EraseSproutViewingKey(const libzcash::SproutViewingKey &vk);
private:
CWalletDB(const CWalletDB&);

Loading…
Cancel
Save