From f7824e8cecda1048c951956893d415512cc6145c Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Tue, 1 Mar 2022 10:06:17 -0700 Subject: [PATCH] wallet: restore Orchard secret keys from mnemonic seed --- src/wallet/wallet.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index f24bb1676..cc69f26bd 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -850,12 +850,39 @@ bool CWallet::LoadUnifiedCaches() { AssertLockHeld(cs_wallet); + auto seed = GetMnemonicSeed(); + for (auto account = mapUnifiedAccountKeys.begin(); account != mapUnifiedAccountKeys.end(); ++account) { auto ufvkId = account->second; auto ufvk = GetUnifiedFullViewingKey(ufvkId); + + if (!seed.has_value()) { + // We loaded an account but we didn't load the mnemonic seed, so + // we cannot recover the secret keys. + return false; + } + if (ufvk.has_value()) { auto metadata = mapUfvkAddressMetadata.find(ufvkId); if (metadata != mapUfvkAddressMetadata.end()) { + auto accountId = metadata->second.GetAccountId(); + if (!accountId.has_value()) { + // The account record was not loaded and yet there were + // address records loaded. + return false; + } + auto usk = ZcashdUnifiedSpendingKey::ForAccount(seed.value(), BIP44CoinType(), accountId.value()); + if (!usk.has_value()) { + // Unable to generate a unified spending key for this account ID. + return false; + } + + // add Orchard spending key to the orchard wallet + { + auto orchardSk = usk.value().GetOrchardKey(); + orchardWallet.AddSpendingKey(orchardSk); + } + // restore unified addresses that have been previously generated to the // keystore for (const auto &[j, receiverTypes] : metadata->second.GetKnownReceiverSetsByDiversifierIndex()) {