Add Sapling Add/Have/Get to keystore
This commit is contained in:
@@ -93,6 +93,16 @@ bool CBasicKeyStore::AddSpendingKey(const libzcash::SproutSpendingKey &sk)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Sapling
|
||||||
|
bool CBasicKeyStore::AddSaplingSpendingKey(const libzcash::SaplingSpendingKey &sk)
|
||||||
|
{
|
||||||
|
LOCK(cs_SpendingKeyStore);
|
||||||
|
auto fvk = sk.full_viewing_key();
|
||||||
|
mapSaplingSpendingKeys[fvk] = sk;
|
||||||
|
//! TODO: Note decryptors for Sapling
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool CBasicKeyStore::AddViewingKey(const libzcash::SproutViewingKey &vk)
|
bool CBasicKeyStore::AddViewingKey(const libzcash::SproutViewingKey &vk)
|
||||||
{
|
{
|
||||||
LOCK(cs_SpendingKeyStore);
|
LOCK(cs_SpendingKeyStore);
|
||||||
|
|||||||
@@ -56,6 +56,13 @@ public:
|
|||||||
virtual bool GetSpendingKey(const libzcash::SproutPaymentAddress &address, libzcash::SproutSpendingKey& skOut) const =0;
|
virtual bool GetSpendingKey(const libzcash::SproutPaymentAddress &address, libzcash::SproutSpendingKey& skOut) const =0;
|
||||||
virtual void GetPaymentAddresses(std::set<libzcash::SproutPaymentAddress> &setAddress) const =0;
|
virtual void GetPaymentAddresses(std::set<libzcash::SproutPaymentAddress> &setAddress) const =0;
|
||||||
|
|
||||||
|
//! Add a Sapling spending key to the store.
|
||||||
|
virtual bool AddSaplingSpendingKey(const libzcash::SaplingSpendingKey &sk) =0;
|
||||||
|
|
||||||
|
//! Check whether a Sapling spending key corresponding to a given Sapling viewing key is present in the store.
|
||||||
|
virtual bool HaveSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk) const =0;
|
||||||
|
virtual bool GetSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk, libzcash::SaplingSpendingKey& skOut) const =0;
|
||||||
|
|
||||||
//! Support for viewing keys
|
//! Support for viewing keys
|
||||||
virtual bool AddViewingKey(const libzcash::SproutViewingKey &vk) =0;
|
virtual bool AddViewingKey(const libzcash::SproutViewingKey &vk) =0;
|
||||||
virtual bool RemoveViewingKey(const libzcash::SproutViewingKey &vk) =0;
|
virtual bool RemoveViewingKey(const libzcash::SproutViewingKey &vk) =0;
|
||||||
@@ -70,6 +77,9 @@ typedef std::map<libzcash::SproutPaymentAddress, libzcash::SproutSpendingKey> Sp
|
|||||||
typedef std::map<libzcash::SproutPaymentAddress, libzcash::SproutViewingKey> ViewingKeyMap;
|
typedef std::map<libzcash::SproutPaymentAddress, libzcash::SproutViewingKey> ViewingKeyMap;
|
||||||
typedef std::map<libzcash::SproutPaymentAddress, ZCNoteDecryption> NoteDecryptorMap;
|
typedef std::map<libzcash::SproutPaymentAddress, ZCNoteDecryption> NoteDecryptorMap;
|
||||||
|
|
||||||
|
typedef std::map<libzcash::SaplingFullViewingKey, libzcash::SaplingSpendingKey> SaplingSpendingKeyMap;
|
||||||
|
typedef std::map<libzcash::SaplingIncomingViewingKey, libzcash::SaplingFullViewingKey> SaplingFullViewingKeyMap;
|
||||||
|
|
||||||
/** Basic key store, that keeps keys in an address->secret map */
|
/** Basic key store, that keeps keys in an address->secret map */
|
||||||
class CBasicKeyStore : public CKeyStore
|
class CBasicKeyStore : public CKeyStore
|
||||||
{
|
{
|
||||||
@@ -81,6 +91,8 @@ protected:
|
|||||||
ViewingKeyMap mapViewingKeys;
|
ViewingKeyMap mapViewingKeys;
|
||||||
NoteDecryptorMap mapNoteDecryptors;
|
NoteDecryptorMap mapNoteDecryptors;
|
||||||
|
|
||||||
|
SaplingSpendingKeyMap mapSaplingSpendingKeys;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
|
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
|
||||||
bool HaveKey(const CKeyID &address) const
|
bool HaveKey(const CKeyID &address) const
|
||||||
@@ -183,6 +195,32 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Sapling
|
||||||
|
bool AddSaplingSpendingKey(const libzcash::SaplingSpendingKey &sk);
|
||||||
|
bool HaveSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk) const
|
||||||
|
{
|
||||||
|
bool result;
|
||||||
|
{
|
||||||
|
LOCK(cs_SpendingKeyStore);
|
||||||
|
result = (mapSaplingSpendingKeys.count(fvk) > 0);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
bool GetSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk, libzcash::SaplingSpendingKey &skOut) const
|
||||||
|
{
|
||||||
|
{
|
||||||
|
LOCK(cs_SpendingKeyStore);
|
||||||
|
|
||||||
|
SaplingSpendingKeyMap::const_iterator mi = mapSaplingSpendingKeys.find(fvk);
|
||||||
|
if (mi != mapSaplingSpendingKeys.end())
|
||||||
|
{
|
||||||
|
skOut = mi->second;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
virtual bool AddViewingKey(const libzcash::SproutViewingKey &vk);
|
virtual bool AddViewingKey(const libzcash::SproutViewingKey &vk);
|
||||||
virtual bool RemoveViewingKey(const libzcash::SproutViewingKey &vk);
|
virtual bool RemoveViewingKey(const libzcash::SproutViewingKey &vk);
|
||||||
virtual bool HaveViewingKey(const libzcash::SproutPaymentAddress &address) const;
|
virtual bool HaveViewingKey(const libzcash::SproutPaymentAddress &address) const;
|
||||||
|
|||||||
@@ -7,9 +7,42 @@
|
|||||||
|
|
||||||
#include <boost/filesystem.hpp>
|
#include <boost/filesystem.hpp>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This test covers Sapling methods on CWallet
|
||||||
|
* GenerateNewSaplingZKey()
|
||||||
|
*/
|
||||||
|
TEST(wallet_zkeys_tests, store_and_load_sapling_zkeys) {
|
||||||
|
CWallet wallet;
|
||||||
|
|
||||||
|
// wallet should be empty
|
||||||
|
// std::set<libzcash::SaplingPaymentAddress> addrs;
|
||||||
|
// wallet.GetSaplingPaymentAddresses(addrs);
|
||||||
|
// ASSERT_EQ(0, addrs.size());
|
||||||
|
|
||||||
|
// wallet should have one key
|
||||||
|
auto saplingAddr = wallet.GenerateNewSaplingZKey();
|
||||||
|
// ASSERT_NE(boost::get<libzcash::SaplingPaymentAddress>(&address), nullptr);
|
||||||
|
// auto sapling_addr = boost::get<libzcash::SaplingPaymentAddress>(saplingAddr);
|
||||||
|
// wallet.GetSaplingPaymentAddresses(addrs);
|
||||||
|
// ASSERT_EQ(1, addrs.size());
|
||||||
|
|
||||||
|
auto sk = libzcash::SaplingSpendingKey::random();
|
||||||
|
auto full_viewing_key = sk.full_viewing_key();
|
||||||
|
ASSERT_TRUE(wallet.AddSaplingSpendingKey(sk));
|
||||||
|
|
||||||
|
// verify wallet has spending key for the address
|
||||||
|
ASSERT_TRUE(wallet.HaveSaplingSpendingKey(full_viewing_key));
|
||||||
|
|
||||||
|
// check key is the same
|
||||||
|
libzcash::SaplingSpendingKey keyOut;
|
||||||
|
wallet.GetSaplingSpendingKey(full_viewing_key, keyOut);
|
||||||
|
ASSERT_EQ(sk, keyOut);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This test covers methods on CWallet
|
* This test covers methods on CWallet
|
||||||
* GenerateNewZKey()
|
* GenerateNewZKey()
|
||||||
|
* GenerateNewSaplingZKey()
|
||||||
* AddZKey()
|
* AddZKey()
|
||||||
* LoadZKey()
|
* LoadZKey()
|
||||||
* LoadZKeyMetadata()
|
* LoadZKeyMetadata()
|
||||||
|
|||||||
@@ -100,8 +100,64 @@ libzcash::PaymentAddress CWallet::GenerateNewZKey()
|
|||||||
return addr;
|
return addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! TODO: Should be Sapling address format, SaplingPaymentAddress
|
||||||
|
// Generate a new Sapling spending key and return its public payment address
|
||||||
|
SaplingPaymentAddress CWallet::GenerateNewSaplingZKey()
|
||||||
|
{
|
||||||
|
AssertLockHeld(cs_wallet); // mapZKeyMetadata
|
||||||
|
auto sk = SaplingSpendingKey::random();
|
||||||
|
auto fvk = sk.full_viewing_key();
|
||||||
|
auto addrOpt = sk.default_address();
|
||||||
|
if (addrOpt){
|
||||||
|
auto addr = addrOpt.value();
|
||||||
|
// Check for collision, even though it is unlikely to ever occur
|
||||||
|
if (CCryptoKeyStore::HaveSaplingSpendingKey(fvk))
|
||||||
|
throw std::runtime_error("CWallet::GenerateNewSaplingZKey(): Collision detected");
|
||||||
|
|
||||||
|
// Create new metadata
|
||||||
|
int64_t nCreationTime = GetTime();
|
||||||
|
mapSaplingZKeyMetadata[addr] = CKeyMetadata(nCreationTime);
|
||||||
|
|
||||||
|
if (!AddSaplingZKey(sk)) {
|
||||||
|
throw std::runtime_error("CWallet::GenerateNewSaplingZKey(): AddSaplingZKey failed");
|
||||||
|
}
|
||||||
|
// return default sapling payment address.
|
||||||
|
return addr;
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("CWallet::GenerateNewSaplingZKey(): default_address() did not return address");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add spending key to keystore
|
||||||
|
// TODO: persist to disk
|
||||||
|
bool CWallet::AddSaplingZKey(const libzcash::SaplingSpendingKey &sk)
|
||||||
|
{
|
||||||
|
AssertLockHeld(cs_wallet); // mapSaplingZKeyMetadata
|
||||||
|
auto addr = sk.default_address();
|
||||||
|
|
||||||
|
if (!CCryptoKeyStore::AddSaplingSpendingKey(sk)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// // check if we need to remove from viewing keys
|
||||||
|
// if (HaveViewingKey(addr)) {
|
||||||
|
// RemoveViewingKey(key.viewing_key());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (!fFileBacked) {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (!IsCrypted()) {
|
||||||
|
// return CWalletDB(strWalletFile).WriteSaplingZKey(addr,
|
||||||
|
// sk,
|
||||||
|
// mapSaplingZKeyMetadata[addr]);
|
||||||
|
// }
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Add spending key to keystore and persist to disk
|
// Add spending key to keystore and persist to disk
|
||||||
// TODO: Add Sapling support
|
|
||||||
bool CWallet::AddZKey(const libzcash::SproutSpendingKey &key)
|
bool CWallet::AddZKey(const libzcash::SproutSpendingKey &key)
|
||||||
{
|
{
|
||||||
AssertLockHeld(cs_wallet); // mapZKeyMetadata
|
AssertLockHeld(cs_wallet); // mapZKeyMetadata
|
||||||
|
|||||||
@@ -786,6 +786,7 @@ public:
|
|||||||
std::set<int64_t> setKeyPool;
|
std::set<int64_t> setKeyPool;
|
||||||
std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
|
std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
|
||||||
std::map<libzcash::SproutPaymentAddress, CKeyMetadata> mapZKeyMetadata;
|
std::map<libzcash::SproutPaymentAddress, CKeyMetadata> mapZKeyMetadata;
|
||||||
|
std::map<libzcash::SaplingPaymentAddress, CKeyMetadata> mapSaplingZKeyMetadata;
|
||||||
|
|
||||||
typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
|
typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
|
||||||
MasterKeyMap mapMasterKeys;
|
MasterKeyMap mapMasterKeys;
|
||||||
@@ -978,6 +979,14 @@ public:
|
|||||||
//! Adds a viewing key to the store, without saving it to disk (used by LoadWallet)
|
//! Adds a viewing key to the store, without saving it to disk (used by LoadWallet)
|
||||||
bool LoadViewingKey(const libzcash::SproutViewingKey &dest);
|
bool LoadViewingKey(const libzcash::SproutViewingKey &dest);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sapling ZKeys
|
||||||
|
*/
|
||||||
|
//! Generates new Sapling key
|
||||||
|
libzcash::SaplingPaymentAddress GenerateNewSaplingZKey();
|
||||||
|
//! Adds Sapling spending key to the store, and saves it to disk
|
||||||
|
bool AddSaplingZKey(const libzcash::SaplingSpendingKey &key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Increment the next transaction order id
|
* Increment the next transaction order id
|
||||||
* @return next transaction order id
|
* @return next transaction order id
|
||||||
|
|||||||
Reference in New Issue
Block a user