From d869d7f4a30ba227ea45e9a841849d2634947575 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Thu, 9 Dec 2021 12:05:59 -0700 Subject: [PATCH] Load unified full viewing keys from the walletdb. --- src/wallet/wallet.cpp | 98 ++++++++++++++++++++--------------------- src/wallet/wallet.h | 10 +---- src/wallet/walletdb.cpp | 24 ++++++++++ 3 files changed, 72 insertions(+), 60 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index e3d4d7c72..851ad6a59 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -430,6 +430,7 @@ std::pair CWallet::GenerateN throw std::runtime_error( "CWallet::GenerateNewUnifiedSpendingKey(): Wallet is missing mnemonic seed metadata."); } + CHDChain& hdChain = mnemonicHDChain.value(); while (true) { auto usk = GenerateUnifiedSpendingKeyForAccount(hdChain.GetAccountCounter()); @@ -449,6 +450,8 @@ std::pair CWallet::GenerateN std::optional> CWallet::GenerateUnifiedSpendingKeyForAccount(libzcash::AccountId accountId) { + AssertLockHeld(cs_wallet); // mapUnifiedKeyMetadata + auto seed = GetMnemonicSeed(); if (!seed.has_value()) { throw std::runtime_error(std::string(__func__) + ": Wallet has no mnemonic HD seed."); @@ -456,8 +459,48 @@ std::optional> GenerateUnifiedSpendingKeyForAccount(libzcash::AccountId accountId); - //! Add the specified unified spending key to the wallet with the provided key - //! metadata. -- TODO, this should probably not be part of the public API? - bool AddUnifiedSpendingKey( - const libzcash::ZcashdUnifiedSpendingKey& sk, - const libzcash::ZcashdUnifiedKeyMetadata& metadata); - bool AddUnifiedFullViewingKey(const libzcash::UnifiedFullViewingKey &ufvk); - bool LoadUnifiedFullViewingKey( - const libzcash::UFVKId& keyId, - const libzcash::UnifiedFullViewingKey &key); + bool LoadUnifiedFullViewingKey(const libzcash::UnifiedFullViewingKey &key); void LoadUnifiedKeyMetadata(const libzcash::ZcashdUnifiedKeyMetadata &meta); diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 852c50438..041c9445d 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -649,6 +649,30 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, { ssValue >> pwallet->vchDefaultKey; } + else if (strType == "unifiedfvk") + { + libzcash::UFVKId fp; + ssKey >> fp; + + std::string ufvkenc; + ssValue >> ufvkenc; + + auto ufvkopt = libzcash::UnifiedFullViewingKey::Decode(ufvkenc, Params()); + if (ufvkopt.has_value()) { + auto ufvk = ufvkopt.value(); + if (fp != ufvk.GetKeyID(Params())) { + strErr = "Error reading wallet database: key fingerprint did not match decoded key"; + return false; + } + if (!pwallet->LoadUnifiedFullViewingKey(ufvk)) { + strErr = "Error reading wallet database: LoadUnifiedFullViewingKey failed."; + return false; + } + } else { + strErr = "Error reading wallet database: failed to decode unified full viewing key."; + return false; + } + } else if (strType == "pool") { int64_t nIndex;