Change default_address to return SaplingPaymentAddr and not boost::optional

This commit is contained in:
Jay Graber 2018-06-30 12:57:04 -07:00
parent f82a864dc1
commit 8e91ebf76c
8 changed files with 24 additions and 28 deletions

View File

@ -55,9 +55,7 @@ TEST(keystore_tests, sapling_keys) {
EXPECT_EQ(in_viewing_key, in_viewing_key_2); EXPECT_EQ(in_viewing_key, in_viewing_key_2);
// Check that the default address from primitives and from sk method are the same // Check that the default address from primitives and from sk method are the same
auto addrOpt = sk.default_address(); auto default_addr = sk.default_address();
EXPECT_TRUE(addrOpt);
auto default_addr = addrOpt.value();
auto addrOpt2 = in_viewing_key.address(default_d); auto addrOpt2 = in_viewing_key.address(default_d);
EXPECT_TRUE(addrOpt2); EXPECT_TRUE(addrOpt2);
auto default_addr_2 = addrOpt2.value(); auto default_addr_2 = addrOpt2.value();
@ -175,9 +173,7 @@ TEST(keystore_tests, StoreAndRetrieveSaplingSpendingKey) {
auto sk = libzcash::SaplingSpendingKey::random(); auto sk = libzcash::SaplingSpendingKey::random();
auto fvk = sk.full_viewing_key(); auto fvk = sk.full_viewing_key();
auto ivk = fvk.in_viewing_key(); auto ivk = fvk.in_viewing_key();
auto addrOpt = sk.default_address(); auto addr = sk.default_address();
EXPECT_TRUE(addrOpt);
auto addr = addrOpt.value();
// Sanity-check: we can't get a key we haven't added // Sanity-check: we can't get a key we haven't added
EXPECT_FALSE(keyStore.HaveSaplingSpendingKey(fvk)); EXPECT_FALSE(keyStore.HaveSaplingSpendingKey(fvk));

View File

@ -56,7 +56,7 @@ TEST(SaplingNote, TestVectors)
TEST(SaplingNote, Random) TEST(SaplingNote, Random)
{ {
// Test creating random notes using the same spending key // Test creating random notes using the same spending key
auto address = SaplingSpendingKey::random().default_address().get(); auto address = SaplingSpendingKey::random().default_address();
SaplingNote note1(address, GetRand(MAX_MONEY)); SaplingNote note1(address, GetRand(MAX_MONEY));
SaplingNote note2(address, GetRand(MAX_MONEY)); SaplingNote note2(address, GetRand(MAX_MONEY));
@ -66,7 +66,7 @@ TEST(SaplingNote, Random)
ASSERT_NE(note1.r, note2.r); ASSERT_NE(note1.r, note2.r);
// Test diversifier and pk_d are not the same for different spending keys // Test diversifier and pk_d are not the same for different spending keys
SaplingNote note3(SaplingSpendingKey::random().default_address().get(), GetRand(MAX_MONEY)); SaplingNote note3(SaplingSpendingKey::random().default_address(), GetRand(MAX_MONEY));
ASSERT_NE(note1.d, note3.d); ASSERT_NE(note1.d, note3.d);
ASSERT_NE(note1.pk_d, note3.pk_d); ASSERT_NE(note1.pk_d, note3.pk_d);
} }

View File

@ -98,16 +98,17 @@ bool CBasicKeyStore::AddSaplingSpendingKey(const libzcash::SaplingSpendingKey &s
{ {
LOCK(cs_SpendingKeyStore); LOCK(cs_SpendingKeyStore);
auto fvk = sk.full_viewing_key(); auto fvk = sk.full_viewing_key();
mapSaplingSpendingKeys[fvk] = sk;
// if SaplingFullViewingKey is not in SaplingFullViewingKeyMap, add it // if SaplingFullViewingKey is not in SaplingFullViewingKeyMap, add it
AddSaplingFullViewingKey(fvk); if (!AddSaplingFullViewingKey(fvk)){
return false;
}
mapSaplingSpendingKeys[fvk] = sk;
// Add addr -> SaplingIncomingViewing to SaplingIncomingViewingKeyMap // Add addr -> SaplingIncomingViewing to SaplingIncomingViewingKeyMap
auto ivk = fvk.in_viewing_key(); auto ivk = fvk.in_viewing_key();
auto addrOpt = sk.default_address(); auto addr = sk.default_address();
assert(addrOpt != boost::none);
auto addr = addrOpt.value();
mapSaplingIncomingViewingKeys[addr] = ivk; mapSaplingIncomingViewingKeys[addr] = ivk;
return true; return true;

View File

@ -92,6 +92,7 @@ typedef std::map<libzcash::SproutPaymentAddress, ZCNoteDecryption> NoteDecryptor
// Full viewing key has equivalent functionality to a transparent address // Full viewing key has equivalent functionality to a transparent address
// When encrypting wallet, encrypt SaplingSpendingKeyMap, while leaving SaplingFullViewingKeyMap unencrypted // When encrypting wallet, encrypt SaplingSpendingKeyMap, while leaving SaplingFullViewingKeyMap unencrypted
// When implementing ZIP 32, add another map from SaplingFullViewingKey -> SaplingExpandedSpendingKey
typedef std::map<libzcash::SaplingFullViewingKey, libzcash::SaplingSpendingKey> SaplingSpendingKeyMap; typedef std::map<libzcash::SaplingFullViewingKey, libzcash::SaplingSpendingKey> SaplingSpendingKeyMap;
typedef std::map<libzcash::SaplingIncomingViewingKey, libzcash::SaplingFullViewingKey> SaplingFullViewingKeyMap; typedef std::map<libzcash::SaplingIncomingViewingKey, libzcash::SaplingFullViewingKey> SaplingFullViewingKeyMap;
// Only maps from default addresses to ivk, may need to be reworked when adding diversified addresses. // Only maps from default addresses to ivk, may need to be reworked when adding diversified addresses.

View File

@ -103,21 +103,16 @@ libzcash::PaymentAddress CWallet::GenerateNewZKey()
// Generate a new Sapling spending key and return its public payment address // Generate a new Sapling spending key and return its public payment address
SaplingPaymentAddress CWallet::GenerateNewSaplingZKey() SaplingPaymentAddress CWallet::GenerateNewSaplingZKey()
{ {
AssertLockHeld(cs_wallet); // mapZKeyMetadata AssertLockHeld(cs_wallet); // mapSaplingZKeyMetadata
SaplingSpendingKey sk; auto sk = SaplingSpendingKey::random();
boost::optional<SaplingPaymentAddress> addrOpt;
while (!addrOpt){
sk = SaplingSpendingKey::random();
addrOpt = sk.default_address();
}
auto addr = addrOpt.value();
auto fvk = sk.full_viewing_key(); auto fvk = sk.full_viewing_key();
auto addr = sk.default_address();
// Check for collision, even though it is unlikely to ever occur // Check for collision, even though it is unlikely to ever occur
if (CCryptoKeyStore::HaveSaplingSpendingKey(fvk)) if (CCryptoKeyStore::HaveSaplingSpendingKey(fvk)) {
throw std::runtime_error("CWallet::GenerateNewSaplingZKey(): Collision detected"); throw std::runtime_error("CWallet::GenerateNewSaplingZKey(): Collision detected");
}
// Create new metadata // Create new metadata
int64_t nCreationTime = GetTime(); int64_t nCreationTime = GetTime();

View File

@ -141,7 +141,7 @@ public:
bool WriteViewingKey(const libzcash::SproutViewingKey &vk); bool WriteViewingKey(const libzcash::SproutViewingKey &vk);
bool EraseViewingKey(const libzcash::SproutViewingKey &vk); bool EraseViewingKey(const libzcash::SproutViewingKey &vk);
private: private:
CWalletDB(const CWalletDB&); CWalletDB(const CWalletDB&);
void operator=(const CWalletDB&); void operator=(const CWalletDB&);

View File

@ -75,8 +75,11 @@ boost::optional<SaplingPaymentAddress> SaplingIncomingViewingKey::address(divers
} }
} }
boost::optional<SaplingPaymentAddress> SaplingSpendingKey::default_address() const { SaplingPaymentAddress SaplingSpendingKey::default_address() const {
return full_viewing_key().in_viewing_key().address(default_diversifier(*this)); // Iterates within default_diversifier to ensure a valid address is returned
auto addrOpt = full_viewing_key().in_viewing_key().address(default_diversifier(*this));
assert(addrOpt != boost::none);
return addrOpt.value();
} }
} }

View File

@ -202,7 +202,7 @@ public:
SaplingFullViewingKey full_viewing_key() const; SaplingFullViewingKey full_viewing_key() const;
// Can derive Sapling addr from default diversifier // Can derive Sapling addr from default diversifier
boost::optional<SaplingPaymentAddress> default_address() const; SaplingPaymentAddress default_address() const;
}; };
typedef boost::variant<InvalidEncoding, SproutPaymentAddress, SaplingPaymentAddress> PaymentAddress; typedef boost::variant<InvalidEncoding, SproutPaymentAddress, SaplingPaymentAddress> PaymentAddress;