Add StoreAndRetrieveSaplingSpendingKey test
This commit is contained in:
@@ -7,42 +7,9 @@
|
||||
|
||||
#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
|
||||
* GenerateNewZKey()
|
||||
* GenerateNewSaplingZKey()
|
||||
* AddZKey()
|
||||
* LoadZKey()
|
||||
* LoadZKeyMetadata()
|
||||
|
||||
@@ -100,40 +100,40 @@ libzcash::PaymentAddress CWallet::GenerateNewZKey()
|
||||
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");
|
||||
|
||||
SaplingSpendingKey sk;
|
||||
boost::optional<SaplingPaymentAddress> addrOpt;
|
||||
while (!addrOpt){
|
||||
sk = SaplingSpendingKey::random();
|
||||
addrOpt = sk.default_address();
|
||||
}
|
||||
|
||||
auto addr = addrOpt.value();
|
||||
auto fvk = sk.full_viewing_key();
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
@@ -144,11 +144,7 @@ bool CWallet::AddSaplingZKey(const libzcash::SaplingSpendingKey &sk)
|
||||
}
|
||||
|
||||
// TODO: Persist to disk
|
||||
// if (!IsCrypted()) {
|
||||
// return CWalletDB(strWalletFile).WriteSaplingZKey(addr,
|
||||
// sk,
|
||||
// mapSaplingZKeyMetadata[addr]);
|
||||
// }
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -141,7 +141,7 @@ public:
|
||||
|
||||
bool WriteViewingKey(const libzcash::SproutViewingKey &vk);
|
||||
bool EraseViewingKey(const libzcash::SproutViewingKey &vk);
|
||||
|
||||
|
||||
private:
|
||||
CWalletDB(const CWalletDB&);
|
||||
void operator=(const CWalletDB&);
|
||||
|
||||
Reference in New Issue
Block a user