From 70c8c25e4bafce18cd7114080f3a54bae115f904 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 3 Aug 2018 16:51:01 +0100 Subject: [PATCH] Store HDSeed in CBasicKeyStore --- src/gtest/test_keystore.cpp | 28 ++++++++++++++++++++++++++++ src/keystore.cpp | 24 ++++++++++++++++++++++++ src/keystore.h | 12 ++++++++++++ 3 files changed, 64 insertions(+) diff --git a/src/gtest/test_keystore.cpp b/src/gtest/test_keystore.cpp index 4b4baa1a4..4f2d0ea44 100644 --- a/src/gtest/test_keystore.cpp +++ b/src/gtest/test_keystore.cpp @@ -8,11 +8,39 @@ #include "wallet/crypter.h" #endif #include "zcash/Address.hpp" +#include "zcash/zip32.h" #include "json_test_vectors.h" #define MAKE_STRING(x) std::string((x), (x)+sizeof(x)) +TEST(keystore_tests, StoreAndRetrieveHDSeed) { + CBasicKeyStore keyStore; + HDSeed seedOut; + + // When we haven't set a seed, we shouldn't get one + EXPECT_FALSE(keyStore.HaveHDSeed()); + EXPECT_FALSE(keyStore.GetHDSeed(seedOut)); + + // Generate a random seed + auto seed = HDSeed::Random(); + + // We should be able to set and retrieve the seed + ASSERT_TRUE(keyStore.SetHDSeed(seed)); + EXPECT_TRUE(keyStore.HaveHDSeed()); + ASSERT_TRUE(keyStore.GetHDSeed(seedOut)); + EXPECT_EQ(seed, seedOut); + + // Generate another random seed + auto seed2 = HDSeed::Random(); + EXPECT_NE(seed, seed2); + + // We should be able to set and retrieve a different seed + ASSERT_TRUE(keyStore.SetHDSeed(seed2)); + ASSERT_TRUE(keyStore.GetHDSeed(seedOut)); + EXPECT_EQ(seed2, seedOut); +} + TEST(keystore_tests, sapling_keys) { // ["sk, ask, nsk, ovk, ak, nk, ivk, default_d, default_pk_d, note_v, note_r, note_cm, note_pos, note_nf"], UniValue sapling_keys = read_json(MAKE_STRING(json_tests::sapling_key_components)); diff --git a/src/keystore.cpp b/src/keystore.cpp index 8e4310b38..efd084259 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -23,6 +23,30 @@ bool CKeyStore::AddKey(const CKey &key) { return AddKeyPubKey(key, key.GetPubKey()); } +bool CBasicKeyStore::SetHDSeed(const HDSeed& seed) +{ + LOCK(cs_SpendingKeyStore); + hdSeed = seed; + return true; +} + +bool CBasicKeyStore::HaveHDSeed() const +{ + LOCK(cs_SpendingKeyStore); + return !hdSeed.IsNull(); +} + +bool CBasicKeyStore::GetHDSeed(HDSeed& seedOut) const +{ + LOCK(cs_SpendingKeyStore); + if (hdSeed.IsNull()) { + return false; + } else { + seedOut = hdSeed; + return true; + } +} + bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey) { LOCK(cs_KeyStore); diff --git a/src/keystore.h b/src/keystore.h index 234f588b8..33ad8807e 100644 --- a/src/keystore.h +++ b/src/keystore.h @@ -13,6 +13,7 @@ #include "sync.h" #include "zcash/Address.hpp" #include "zcash/NoteEncryption.hpp" +#include "zcash/zip32.h" #include #include @@ -27,6 +28,12 @@ protected: public: virtual ~CKeyStore() {} + //! Set the HD seed for this keystore + virtual bool SetHDSeed(const HDSeed& seed) =0; + virtual bool HaveHDSeed() const =0; + //! Get the HD seed for this keystore + virtual bool GetHDSeed(HDSeed& seedOut) const =0; + //! Add a key to the store. virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0; virtual bool AddKey(const CKey &key); @@ -109,6 +116,7 @@ typedef std::map