Address need not be optional when adding sapling keys

This commit is contained in:
Eirik Ogilvie-Wigley
2018-09-27 15:44:04 -06:00
parent 379c7439a0
commit 83c4e360da
8 changed files with 28 additions and 36 deletions

View File

@@ -215,12 +215,6 @@ TEST(keystore_tests, StoreAndRetrieveSaplingSpendingKey) {
EXPECT_FALSE(keyStore.HaveSaplingIncomingViewingKey(addr)); EXPECT_FALSE(keyStore.HaveSaplingIncomingViewingKey(addr));
EXPECT_FALSE(keyStore.GetSaplingIncomingViewingKey(addr, ivkOut)); EXPECT_FALSE(keyStore.GetSaplingIncomingViewingKey(addr, ivkOut));
// If we don't specify the default address, that mapping isn't created
keyStore.AddSaplingSpendingKey(sk);
EXPECT_TRUE(keyStore.HaveSaplingSpendingKey(fvk));
EXPECT_TRUE(keyStore.HaveSaplingFullViewingKey(ivk));
EXPECT_FALSE(keyStore.HaveSaplingIncomingViewingKey(addr));
// When we specify the default address, we get the full mapping // When we specify the default address, we get the full mapping
keyStore.AddSaplingSpendingKey(sk, addr); keyStore.AddSaplingSpendingKey(sk, addr);
EXPECT_TRUE(keyStore.HaveSaplingSpendingKey(fvk)); EXPECT_TRUE(keyStore.HaveSaplingSpendingKey(fvk));

View File

@@ -125,13 +125,13 @@ bool CBasicKeyStore::AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk)
//! Sapling //! Sapling
bool CBasicKeyStore::AddSaplingSpendingKey( bool CBasicKeyStore::AddSaplingSpendingKey(
const libzcash::SaplingExtendedSpendingKey &sk, const libzcash::SaplingExtendedSpendingKey &sk,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr) const libzcash::SaplingPaymentAddress &defaultAddr)
{ {
LOCK(cs_SpendingKeyStore); LOCK(cs_SpendingKeyStore);
auto fvk = sk.expsk.full_viewing_key(); auto fvk = sk.expsk.full_viewing_key();
// if SaplingFullViewingKey is not in SaplingFullViewingKeyMap, add it // if SaplingFullViewingKey is not in SaplingFullViewingKeyMap, add it
if (!AddSaplingFullViewingKey(fvk, defaultAddr)){ if (!AddSaplingFullViewingKey(fvk, defaultAddr)) {
return false; return false;
} }
@@ -151,17 +151,15 @@ bool CBasicKeyStore::AddSproutViewingKey(const libzcash::SproutViewingKey &vk)
bool CBasicKeyStore::AddSaplingFullViewingKey( bool CBasicKeyStore::AddSaplingFullViewingKey(
const libzcash::SaplingFullViewingKey &fvk, const libzcash::SaplingFullViewingKey &fvk,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr) const libzcash::SaplingPaymentAddress &defaultAddr)
{ {
LOCK(cs_SpendingKeyStore); LOCK(cs_SpendingKeyStore);
auto ivk = fvk.in_viewing_key(); auto ivk = fvk.in_viewing_key();
mapSaplingFullViewingKeys[ivk] = fvk; mapSaplingFullViewingKeys[ivk] = fvk;
if (defaultAddr) {
// Add defaultAddr -> SaplingIncomingViewing to SaplingIncomingViewingKeyMap
mapSaplingIncomingViewingKeys[defaultAddr.get()] = ivk;
}
// Add defaultAddr -> SaplingIncomingViewing to SaplingIncomingViewingKeyMap
mapSaplingIncomingViewingKeys[defaultAddr] = ivk;
return true; return true;
} }

View File

@@ -66,7 +66,7 @@ public:
//! Add a Sapling spending key to the store. //! Add a Sapling spending key to the store.
virtual bool AddSaplingSpendingKey( virtual bool AddSaplingSpendingKey(
const libzcash::SaplingExtendedSpendingKey &sk, const libzcash::SaplingExtendedSpendingKey &sk,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr = boost::none) =0; const libzcash::SaplingPaymentAddress &defaultAddr) =0;
//! Check whether a Sapling spending key corresponding to a given Sapling viewing key is present in the store. //! 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 HaveSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk) const =0;
@@ -75,7 +75,7 @@ public:
//! Support for Sapling full viewing keys //! Support for Sapling full viewing keys
virtual bool AddSaplingFullViewingKey( virtual bool AddSaplingFullViewingKey(
const libzcash::SaplingFullViewingKey &fvk, const libzcash::SaplingFullViewingKey &fvk,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr = boost::none) =0; const libzcash::SaplingPaymentAddress &defaultAddr) =0;
virtual bool HaveSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk) const =0; virtual bool HaveSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk) const =0;
virtual bool GetSaplingFullViewingKey( virtual bool GetSaplingFullViewingKey(
const libzcash::SaplingIncomingViewingKey &ivk, const libzcash::SaplingIncomingViewingKey &ivk,
@@ -236,7 +236,7 @@ public:
//! Sapling //! Sapling
bool AddSaplingSpendingKey( bool AddSaplingSpendingKey(
const libzcash::SaplingExtendedSpendingKey &sk, const libzcash::SaplingExtendedSpendingKey &sk,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr = boost::none); const libzcash::SaplingPaymentAddress &defaultAddr);
bool HaveSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk) const bool HaveSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk) const
{ {
bool result; bool result;
@@ -263,7 +263,7 @@ public:
virtual bool AddSaplingFullViewingKey( virtual bool AddSaplingFullViewingKey(
const libzcash::SaplingFullViewingKey &fvk, const libzcash::SaplingFullViewingKey &fvk,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr = boost::none); const libzcash::SaplingPaymentAddress &defaultAddr);
virtual bool HaveSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk) const; virtual bool HaveSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk) const;
virtual bool GetSaplingFullViewingKey( virtual bool GetSaplingFullViewingKey(
const libzcash::SaplingIncomingViewingKey &ivk, const libzcash::SaplingIncomingViewingKey &ivk,

View File

@@ -449,7 +449,7 @@ bool CCryptoKeyStore::AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk
bool CCryptoKeyStore::AddSaplingSpendingKey( bool CCryptoKeyStore::AddSaplingSpendingKey(
const libzcash::SaplingExtendedSpendingKey &sk, const libzcash::SaplingExtendedSpendingKey &sk,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr) const libzcash::SaplingPaymentAddress &defaultAddr)
{ {
{ {
LOCK(cs_SpendingKeyStore); LOCK(cs_SpendingKeyStore);
@@ -496,7 +496,7 @@ bool CCryptoKeyStore::AddCryptedSproutSpendingKey(
bool CCryptoKeyStore::AddCryptedSaplingSpendingKey( bool CCryptoKeyStore::AddCryptedSaplingSpendingKey(
const libzcash::SaplingFullViewingKey &fvk, const libzcash::SaplingFullViewingKey &fvk,
const std::vector<unsigned char> &vchCryptedSecret, const std::vector<unsigned char> &vchCryptedSecret,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr) const libzcash::SaplingPaymentAddress &defaultAddr)
{ {
{ {
LOCK(cs_SpendingKeyStore); LOCK(cs_SpendingKeyStore);
@@ -505,7 +505,7 @@ bool CCryptoKeyStore::AddCryptedSaplingSpendingKey(
} }
// if SaplingFullViewingKey is not in SaplingFullViewingKeyMap, add it // if SaplingFullViewingKey is not in SaplingFullViewingKeyMap, add it
if (!AddSaplingFullViewingKey(fvk, defaultAddr)){ if (!AddSaplingFullViewingKey(fvk, defaultAddr)) {
return false; return false;
} }
@@ -614,7 +614,7 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
if (!EncryptSecret(vMasterKeyIn, vchSecret, fvk.GetFingerprint(), vchCryptedSecret)) { if (!EncryptSecret(vMasterKeyIn, vchSecret, fvk.GetFingerprint(), vchCryptedSecret)) {
return false; return false;
} }
if (!AddCryptedSaplingSpendingKey(fvk, vchCryptedSecret)) { if (!AddCryptedSaplingSpendingKey(fvk, vchCryptedSecret, sk.DefaultAddress())) {
return false; return false;
} }
} }

View File

@@ -243,10 +243,10 @@ public:
virtual bool AddCryptedSaplingSpendingKey( virtual bool AddCryptedSaplingSpendingKey(
const libzcash::SaplingFullViewingKey &fvk, const libzcash::SaplingFullViewingKey &fvk,
const std::vector<unsigned char> &vchCryptedSecret, const std::vector<unsigned char> &vchCryptedSecret,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr = boost::none); const libzcash::SaplingPaymentAddress &defaultAddr);
bool AddSaplingSpendingKey( bool AddSaplingSpendingKey(
const libzcash::SaplingExtendedSpendingKey &sk, const libzcash::SaplingExtendedSpendingKey &sk,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr = boost::none); const libzcash::SaplingPaymentAddress &defaultAddr);
bool HaveSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk) const bool HaveSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk) const
{ {
{ {

View File

@@ -516,7 +516,7 @@ TEST(WalletTests, FindMySaplingNotes) {
EXPECT_EQ(0, noteMap.size()); EXPECT_EQ(0, noteMap.size());
// Add spending key to wallet, so Sapling notes can be found // Add spending key to wallet, so Sapling notes can be found
ASSERT_TRUE(wallet.AddSaplingZKey(sk)); ASSERT_TRUE(wallet.AddSaplingZKey(sk, pk));
ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk)); ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk));
noteMap = wallet.FindMySaplingNotes(wtx); noteMap = wallet.FindMySaplingNotes(wtx);
EXPECT_EQ(2, noteMap.size()); EXPECT_EQ(2, noteMap.size());
@@ -630,7 +630,7 @@ TEST(WalletTests, GetConflictedSaplingNotes) {
auto ivk = fvk.in_viewing_key(); auto ivk = fvk.in_viewing_key();
auto pk = sk.DefaultAddress(); auto pk = sk.DefaultAddress();
ASSERT_TRUE(wallet.AddSaplingZKey(sk)); ASSERT_TRUE(wallet.AddSaplingZKey(sk, pk));
ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk)); ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk));
// Generate note A // Generate note A
@@ -815,7 +815,7 @@ TEST(WalletTests, SaplingNullifierIsSpent) {
auto tx = maybe_tx.get(); auto tx = maybe_tx.get();
CWalletTx wtx {&wallet, tx}; CWalletTx wtx {&wallet, tx};
ASSERT_TRUE(wallet.AddSaplingZKey(sk)); ASSERT_TRUE(wallet.AddSaplingZKey(sk, pk));
ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk)); ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk));
// Manually compute the nullifier based on the known position // Manually compute the nullifier based on the known position
@@ -912,7 +912,7 @@ TEST(WalletTests, NavigateFromSaplingNullifierToNote) {
auto tx = maybe_tx.get(); auto tx = maybe_tx.get();
CWalletTx wtx {&wallet, tx}; CWalletTx wtx {&wallet, tx};
ASSERT_TRUE(wallet.AddSaplingZKey(sk)); ASSERT_TRUE(wallet.AddSaplingZKey(sk, pk));
ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk)); ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk));
// Manually compute the nullifier based on the expected position // Manually compute the nullifier based on the expected position
@@ -1048,7 +1048,7 @@ TEST(WalletTests, SpentSaplingNoteIsFromMe) {
auto tx = maybe_tx.get(); auto tx = maybe_tx.get();
CWalletTx wtx {&wallet, tx}; CWalletTx wtx {&wallet, tx};
ASSERT_TRUE(wallet.AddSaplingZKey(sk)); ASSERT_TRUE(wallet.AddSaplingZKey(sk, pk));
ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk)); ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk));
// Fake-mine the transaction // Fake-mine the transaction
@@ -1751,7 +1751,7 @@ TEST(WalletTests, UpdatedSaplingNoteData) {
// Wallet contains fvk1 but not fvk2 // Wallet contains fvk1 but not fvk2
CWalletTx wtx {&wallet, tx}; CWalletTx wtx {&wallet, tx};
ASSERT_TRUE(wallet.AddSaplingZKey(sk)); ASSERT_TRUE(wallet.AddSaplingZKey(sk, pk));
ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk)); ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk));
ASSERT_FALSE(wallet.HaveSaplingSpendingKey(fvk2)); ASSERT_FALSE(wallet.HaveSaplingSpendingKey(fvk2));
@@ -1784,7 +1784,7 @@ TEST(WalletTests, UpdatedSaplingNoteData) {
wtx = wallet.mapWallet[hash]; wtx = wallet.mapWallet[hash];
// Now lets add key fvk2 so wallet can find the payment note sent to pk2 // Now lets add key fvk2 so wallet can find the payment note sent to pk2
ASSERT_TRUE(wallet.AddSaplingZKey(sk2)); ASSERT_TRUE(wallet.AddSaplingZKey(sk2, pk2));
ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk2)); ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk2));
CWalletTx wtx2 = wtx; CWalletTx wtx2 = wtx;
auto saplingNoteData2 = wallet.FindMySaplingNotes(wtx2); auto saplingNoteData2 = wallet.FindMySaplingNotes(wtx2);
@@ -1881,7 +1881,7 @@ TEST(WalletTests, MarkAffectedSaplingTransactionsDirty) {
auto ivk = fvk.in_viewing_key(); auto ivk = fvk.in_viewing_key();
auto pk = sk.DefaultAddress(); auto pk = sk.DefaultAddress();
ASSERT_TRUE(wallet.AddSaplingZKey(sk)); ASSERT_TRUE(wallet.AddSaplingZKey(sk, pk));
ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk)); ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk));
// Set up transparent address // Set up transparent address

View File

@@ -150,7 +150,7 @@ SaplingPaymentAddress CWallet::GenerateNewSaplingZKey()
// Add spending key to keystore // Add spending key to keystore
bool CWallet::AddSaplingZKey( bool CWallet::AddSaplingZKey(
const libzcash::SaplingExtendedSpendingKey &sk, const libzcash::SaplingExtendedSpendingKey &sk,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr) const libzcash::SaplingPaymentAddress &defaultAddr)
{ {
AssertLockHeld(cs_wallet); // mapSaplingZKeyMetadata AssertLockHeld(cs_wallet); // mapSaplingZKeyMetadata
@@ -291,7 +291,7 @@ bool CWallet::AddCryptedSproutSpendingKey(
bool CWallet::AddCryptedSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk, bool CWallet::AddCryptedSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk,
const std::vector<unsigned char> &vchCryptedSecret, const std::vector<unsigned char> &vchCryptedSecret,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr) const libzcash::SaplingPaymentAddress &defaultAddr)
{ {
if (!CCryptoKeyStore::AddCryptedSaplingSpendingKey(fvk, vchCryptedSecret, defaultAddr)) if (!CCryptoKeyStore::AddCryptedSaplingSpendingKey(fvk, vchCryptedSecret, defaultAddr))
return false; return false;

View File

@@ -1058,11 +1058,11 @@ public:
//! Adds Sapling spending key to the store, and saves it to disk //! Adds Sapling spending key to the store, and saves it to disk
bool AddSaplingZKey( bool AddSaplingZKey(
const libzcash::SaplingExtendedSpendingKey &key, const libzcash::SaplingExtendedSpendingKey &key,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr = boost::none); const libzcash::SaplingPaymentAddress &defaultAddr);
bool AddCryptedSaplingSpendingKey( bool AddCryptedSaplingSpendingKey(
const libzcash::SaplingFullViewingKey &fvk, const libzcash::SaplingFullViewingKey &fvk,
const std::vector<unsigned char> &vchCryptedSecret, const std::vector<unsigned char> &vchCryptedSecret,
const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr = boost::none); const libzcash::SaplingPaymentAddress &defaultAddr);
/** /**
* Increment the next transaction order id * Increment the next transaction order id