LCOV - code coverage report
Current view: top level - src/wallet - scriptpubkeyman.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 68.3 % 941 643
Test Date: 2026-06-01 07:10:42 Functions: 71.4 % 84 60
Branches: 39.6 % 1553 615

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2019-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <wallet/scriptpubkeyman.h>
       6                 :             : 
       7                 :             : #include <coins.h>
       8                 :             : #include <hash.h>
       9                 :             : #include <key_io.h>
      10                 :             : #include <node/types.h>
      11                 :             : #include <outputtype.h>
      12                 :             : #include <script/descriptor.h>
      13                 :             : #include <script/script.h>
      14                 :             : #include <script/sign.h>
      15                 :             : #include <script/solver.h>
      16                 :             : #include <util/bip32.h>
      17                 :             : #include <util/check.h>
      18                 :             : #include <util/log.h>
      19                 :             : #include <util/strencodings.h>
      20                 :             : #include <util/string.h>
      21                 :             : #include <util/time.h>
      22                 :             : #include <util/translation.h>
      23                 :             : 
      24                 :             : #include <optional>
      25                 :             : 
      26                 :             : using common::PSBTError;
      27                 :             : using util::ToString;
      28                 :             : 
      29                 :             : namespace wallet {
      30                 :             : 
      31                 :             : typedef std::vector<unsigned char> valtype;
      32                 :             : 
      33                 :             : // Legacy wallet IsMine(). Used only in migration
      34                 :             : // DO NOT USE ANYTHING IN THIS NAMESPACE OUTSIDE OF MIGRATION
      35                 :             : namespace {
      36                 :             : 
      37                 :             : /**
      38                 :             :  * This is an enum that tracks the execution context of a script, similar to
      39                 :             :  * SigVersion in script/interpreter. It is separate however because we want to
      40                 :             :  * distinguish between top-level scriptPubKey execution and P2SH redeemScript
      41                 :             :  * execution (a distinction that has no impact on consensus rules).
      42                 :             :  */
      43                 :             : enum class IsMineSigVersion
      44                 :             : {
      45                 :             :     TOP = 0,        //!< scriptPubKey execution
      46                 :             :     P2SH = 1,       //!< P2SH redeemScript
      47                 :             :     WITNESS_V0 = 2, //!< P2WSH witness script execution
      48                 :             : };
      49                 :             : 
      50                 :             : /**
      51                 :             :  * This is an internal representation of isminetype + invalidity.
      52                 :             :  * Its order is significant, as we return the max of all explored
      53                 :             :  * possibilities.
      54                 :             :  */
      55                 :             : enum class IsMineResult
      56                 :             : {
      57                 :             :     NO = 0,         //!< Not ours
      58                 :             :     WATCH_ONLY = 1, //!< Included in watch-only balance
      59                 :             :     SPENDABLE = 2,  //!< Included in all balances
      60                 :             :     INVALID = 3,    //!< Not spendable by anyone (uncompressed pubkey in segwit, P2SH inside P2SH or witness, witness inside witness)
      61                 :             : };
      62                 :             : 
      63                 :       27098 : bool PermitsUncompressed(IsMineSigVersion sigversion)
      64                 :             : {
      65                 :       27098 :     return sigversion == IsMineSigVersion::TOP || sigversion == IsMineSigVersion::P2SH;
      66                 :             : }
      67                 :             : 
      68                 :        8106 : bool HaveKeys(const std::vector<valtype>& pubkeys, const LegacyDataSPKM& keystore)
      69                 :             : {
      70         [ +  + ]:       56140 :     for (const valtype& pubkey : pubkeys) {
      71         [ -  + ]:       50752 :         CKeyID keyID = CPubKey(pubkey).GetID();
      72         [ +  + ]:       50752 :         if (!keystore.HaveKey(keyID)) return false;
      73                 :             :     }
      74                 :             :     return true;
      75                 :             : }
      76                 :             : 
      77                 :             : //! Recursively solve script and return spendable/watchonly/invalid status.
      78                 :             : //!
      79                 :             : //! @param keystore            legacy key and script store
      80                 :             : //! @param scriptPubKey        script to solve
      81                 :             : //! @param sigversion          script type (top-level / redeemscript / witnessscript)
      82                 :             : //! @param recurse_scripthash  whether to recurse into nested p2sh and p2wsh
      83                 :             : //!                            scripts or simply treat any script that has been
      84                 :             : //!                            stored in the keystore as spendable
      85                 :             : // NOLINTNEXTLINE(misc-no-recursion)
      86                 :      105860 : IsMineResult LegacyWalletIsMineInnerDONOTUSE(const LegacyDataSPKM& keystore, const CScript& scriptPubKey, IsMineSigVersion sigversion, bool recurse_scripthash=true)
      87                 :             : {
      88                 :      105860 :     IsMineResult ret = IsMineResult::NO;
      89                 :             : 
      90                 :      105860 :     std::vector<valtype> vSolutions;
      91         [ +  - ]:      105860 :     TxoutType whichType = Solver(scriptPubKey, vSolutions);
      92                 :             : 
      93   [ +  +  +  +  :      105860 :     CKeyID keyID;
                +  +  + ]
      94   [ +  +  +  +  :      105860 :     switch (whichType) {
                +  +  + ]
      95                 :             :     case TxoutType::NONSTANDARD:
      96                 :             :     case TxoutType::NULL_DATA:
      97                 :             :     case TxoutType::WITNESS_UNKNOWN:
      98                 :             :     case TxoutType::WITNESS_V1_TAPROOT:
      99                 :             :     case TxoutType::ANCHOR:
     100                 :             :         break;
     101                 :        4082 :     case TxoutType::PUBKEY:
     102   [ -  +  +  - ]:        4082 :         keyID = CPubKey(vSolutions[0]).GetID();
     103   [ -  +  -  -  :        4082 :         if (!PermitsUncompressed(sigversion) && vSolutions[0].size() != 33) {
                   -  - ]
     104                 :             :             return IsMineResult::INVALID;
     105                 :             :         }
     106   [ +  -  +  - ]:        4082 :         if (keystore.HaveKey(keyID)) {
     107         [ -  + ]:        4082 :             ret = std::max(ret, IsMineResult::SPENDABLE);
     108                 :             :         }
     109                 :             :         break;
     110                 :       10245 :     case TxoutType::WITNESS_V0_KEYHASH:
     111                 :       10245 :     {
     112         [ +  - ]:       10245 :         if (sigversion == IsMineSigVersion::WITNESS_V0) {
     113                 :             :             // P2WPKH inside P2WSH is invalid.
     114                 :             :             return IsMineResult::INVALID;
     115                 :             :         }
     116   [ +  +  +  -  :       14786 :         if (sigversion == IsMineSigVersion::TOP && !keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
          -  +  +  -  +  
             -  +  +  +  
                      + ]
     117                 :             :             // We do not support bare witness outputs unless the P2SH version of it would be
     118                 :             :             // acceptable as well. This protects against matching before segwit activates.
     119                 :             :             // This also applies to the P2WSH case.
     120                 :             :             break;
     121                 :             :         }
     122   [ -  +  +  -  :        9644 :         ret = std::max(ret, LegacyWalletIsMineInnerDONOTUSE(keystore, GetScriptForDestination(PKHash(uint160(vSolutions[0]))), IsMineSigVersion::WITNESS_V0));
             +  -  +  + ]
     123                 :        9488 :         break;
     124                 :             :     }
     125                 :       14910 :     case TxoutType::PUBKEYHASH:
     126   [ -  +  +  + ]:       14910 :         keyID = CKeyID(uint160(vSolutions[0]));
     127         [ +  + ]:       14910 :         if (!PermitsUncompressed(sigversion)) {
     128         [ +  - ]:        9488 :             CPubKey pubkey;
     129   [ +  -  +  +  :        9488 :             if (keystore.GetPubKey(keyID, pubkey) && !pubkey.IsCompressed()) {
                   +  + ]
     130                 :             :                 return IsMineResult::INVALID;
     131                 :             :             }
     132                 :             :         }
     133   [ +  -  +  + ]:       14898 :         if (keystore.HaveKey(keyID)) {
     134         [ -  + ]:       13726 :             ret = std::max(ret, IsMineResult::SPENDABLE);
     135                 :             :         }
     136                 :             :         break;
     137                 :       43294 :     case TxoutType::SCRIPTHASH:
     138                 :       43294 :     {
     139         [ +  + ]:       43294 :         if (sigversion != IsMineSigVersion::TOP) {
     140                 :             :             // P2SH inside P2WSH or P2SH is invalid.
     141                 :             :             return IsMineResult::INVALID;
     142                 :             :         }
     143   [ -  +  +  - ]:       42474 :         CScriptID scriptID = CScriptID(uint160(vSolutions[0]));
     144                 :       42474 :         CScript subscript;
     145   [ +  -  +  + ]:       42474 :         if (keystore.GetCScript(scriptID, subscript)) {
     146   [ +  +  +  -  :       22421 :             ret = std::max(ret, recurse_scripthash ? LegacyWalletIsMineInnerDONOTUSE(keystore, subscript, IsMineSigVersion::P2SH) : IsMineResult::SPENDABLE);
                   +  + ]
     147                 :             :         }
     148                 :       42474 :         break;
     149                 :       42474 :     }
     150                 :       20574 :     case TxoutType::WITNESS_V0_SCRIPTHASH:
     151                 :       20574 :     {
     152         [ +  - ]:       20574 :         if (sigversion == IsMineSigVersion::WITNESS_V0) {
     153                 :             :             // P2WSH inside P2WSH is invalid.
     154                 :             :             return IsMineResult::INVALID;
     155                 :             :         }
     156   [ +  -  +  -  :       41148 :         if (sigversion == IsMineSigVersion::TOP && !keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
          -  +  +  -  +  
             -  +  -  -  
                      + ]
     157                 :             :             break;
     158                 :             :         }
     159   [ #  #  #  # ]:           0 :         CScriptID scriptID{RIPEMD160(vSolutions[0])};
     160                 :           0 :         CScript subscript;
     161   [ #  #  #  # ]:           0 :         if (keystore.GetCScript(scriptID, subscript)) {
     162   [ #  #  #  #  :           0 :             ret = std::max(ret, recurse_scripthash ? LegacyWalletIsMineInnerDONOTUSE(keystore, subscript, IsMineSigVersion::WITNESS_V0) : IsMineResult::SPENDABLE);
                   #  # ]
     163                 :             :         }
     164                 :           0 :         break;
     165                 :           0 :     }
     166                 :             : 
     167                 :       11261 :     case TxoutType::MULTISIG:
     168                 :       11261 :     {
     169                 :             :         // Never treat bare multisig outputs as ours (they can still be made watchonly-though)
     170         [ +  + ]:       11261 :         if (sigversion == IsMineSigVersion::TOP) {
     171                 :             :             break;
     172                 :             :         }
     173                 :             : 
     174                 :             :         // Only consider transactions "mine" if we own ALL the
     175                 :             :         // keys involved. Multi-signature transactions that are
     176                 :             :         // partially owned (somebody else has a key that can spend
     177                 :             :         // them) enable spend-out-from-under-you attacks, especially
     178                 :             :         // in shared-wallet situations.
     179   [ -  +  +  - ]:        8106 :         std::vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
     180         [ -  + ]:        8106 :         if (!PermitsUncompressed(sigversion)) {
     181   [ #  #  #  # ]:           0 :             for (size_t i = 0; i < keys.size(); i++) {
     182   [ #  #  #  # ]:           0 :                 if (keys[i].size() != 33) {
     183                 :           0 :                     return IsMineResult::INVALID;
     184                 :             :                 }
     185                 :             :             }
     186                 :             :         }
     187   [ +  -  +  + ]:        8106 :         if (HaveKeys(keys, keystore)) {
     188         [ -  + ]:        5388 :             ret = std::max(ret, IsMineResult::SPENDABLE);
     189                 :             :         }
     190                 :        8106 :         break;
     191                 :        8106 :     }
     192                 :             :     } // no default case, so the compiler can warn about missing cases
     193                 :             : 
     194   [ +  +  +  -  :      105028 :     if (ret == IsMineResult::NO && keystore.HaveWatchOnly(scriptPubKey)) {
                   +  + ]
     195         [ -  + ]:        1402 :         ret = std::max(ret, IsMineResult::WATCH_ONLY);
     196                 :             :     }
     197                 :      105028 :     return ret;
     198                 :      105860 : }
     199                 :             : 
     200                 :             : } // namespace
     201                 :             : 
     202                 :       63042 : bool LegacyDataSPKM::IsMine(const CScript& script) const
     203                 :             : {
     204      [ +  -  + ]:       63042 :     switch (LegacyWalletIsMineInnerDONOTUSE(*this, script, IsMineSigVersion::TOP)) {
     205                 :             :     case IsMineResult::INVALID:
     206                 :             :     case IsMineResult::NO:
     207                 :             :         return false;
     208                 :       24598 :     case IsMineResult::WATCH_ONLY:
     209                 :       24598 :     case IsMineResult::SPENDABLE:
     210                 :       24598 :         return true;
     211                 :             :     }
     212                 :           0 :     assert(false);
     213                 :             : }
     214                 :             : 
     215                 :           0 : bool LegacyDataSPKM::CheckDecryptionKey(const CKeyingMaterial& master_key)
     216                 :             : {
     217                 :           0 :     {
     218                 :           0 :         LOCK(cs_KeyStore);
     219         [ #  # ]:           0 :         assert(mapKeys.empty());
     220                 :             : 
     221         [ #  # ]:           0 :         bool keyPass = mapCryptedKeys.empty(); // Always pass when there are no encrypted keys
     222                 :           0 :         bool keyFail = false;
     223         [ #  # ]:           0 :         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
     224   [ #  #  #  # ]:           0 :         WalletBatch batch(m_storage.GetDatabase());
     225         [ #  # ]:           0 :         for (; mi != mapCryptedKeys.end(); ++mi)
     226                 :             :         {
     227         [ #  # ]:           0 :             const CPubKey &vchPubKey = (*mi).second.first;
     228                 :           0 :             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
     229                 :           0 :             CKey key;
     230   [ #  #  #  #  :           0 :             if (!DecryptKey(master_key, vchCryptedSecret, vchPubKey, key))
                   #  # ]
     231                 :             :             {
     232                 :             :                 keyFail = true;
     233                 :             :                 break;
     234                 :             :             }
     235                 :           0 :             keyPass = true;
     236         [ #  # ]:           0 :             if (fDecryptionThoroughlyChecked)
     237                 :             :                 break;
     238                 :             :             else {
     239                 :             :                 // Rewrite these encrypted keys with checksums
     240   [ #  #  #  #  :           0 :                 batch.WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]);
                   #  # ]
     241                 :             :             }
     242                 :           0 :         }
     243         [ #  # ]:           0 :         if (keyPass && keyFail)
     244                 :             :         {
     245         [ #  # ]:           0 :             LogWarning("The wallet is probably corrupted: Some keys decrypt but not all.");
     246         [ #  # ]:           0 :             throw std::runtime_error("Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
     247                 :             :         }
     248         [ #  # ]:           0 :         if (keyFail || !keyPass)
     249                 :           0 :             return false;
     250                 :           0 :         fDecryptionThoroughlyChecked = true;
     251   [ #  #  #  # ]:           0 :     }
     252                 :           0 :     return true;
     253                 :             : }
     254                 :             : 
     255                 :        7685 : std::unique_ptr<SigningProvider> LegacyDataSPKM::GetSolvingProvider(const CScript& script) const
     256                 :             : {
     257                 :        7685 :     return std::make_unique<LegacySigningProvider>(*this);
     258                 :             : }
     259                 :             : 
     260                 :       17248 : bool LegacyDataSPKM::CanProvide(const CScript& script, SignatureData& sigdata)
     261                 :             : {
     262                 :       17248 :     IsMineResult ismine = LegacyWalletIsMineInnerDONOTUSE(*this, script, IsMineSigVersion::TOP, /* recurse_scripthash= */ false);
     263         [ +  + ]:       17248 :     if (ismine == IsMineResult::SPENDABLE || ismine == IsMineResult::WATCH_ONLY) {
     264                 :             :         // If ismine, it means we recognize keys or script ids in the script, or
     265                 :             :         // are watching the script itself, and we can at least provide metadata
     266                 :             :         // or solving information, even if not able to sign fully.
     267                 :             :         return true;
     268                 :             :     } else {
     269                 :             :         // If, given the stuff in sigdata, we could make a valid signature, then we can provide for this script
     270                 :       14859 :         ProduceSignature(*this, DUMMY_SIGNATURE_CREATOR, script, sigdata);
     271         [ +  + ]:       14859 :         if (!sigdata.signatures.empty()) {
     272                 :             :             // If we could make signatures, make sure we have a private key to actually make a signature
     273                 :        3263 :             bool has_privkeys = false;
     274         [ +  + ]:        7790 :             for (const auto& key_sig_pair : sigdata.signatures) {
     275                 :        4527 :                 has_privkeys |= HaveKey(key_sig_pair.first);
     276                 :             :             }
     277                 :             :             return has_privkeys;
     278                 :             :         }
     279                 :             :         return false;
     280                 :             :     }
     281                 :             : }
     282                 :             : 
     283                 :        2827 : bool LegacyDataSPKM::LoadKey(const CKey& key, const CPubKey &pubkey)
     284                 :             : {
     285                 :        2827 :     return AddKeyPubKeyInner(key, pubkey);
     286                 :             : }
     287                 :             : 
     288                 :           0 : bool LegacyDataSPKM::LoadCScript(const CScript& redeemScript)
     289                 :             : {
     290                 :             :     /* A sanity check was added in pull #3843 to avoid adding redeemScripts
     291                 :             :      * that never can be redeemed. However, old wallets may still contain
     292                 :             :      * these. Do not add them to the wallet and warn. */
     293   [ #  #  #  # ]:           0 :     if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
     294                 :             :     {
     295         [ #  # ]:           0 :         std::string strAddr = EncodeDestination(ScriptHash(redeemScript));
     296   [ #  #  #  # ]:           0 :         WalletLogPrintf("%s: Warning: This wallet contains a redeemScript of size %i which exceeds maximum size %i thus can never be redeemed. Do not use address %s.\n", __func__, redeemScript.size(), MAX_SCRIPT_ELEMENT_SIZE, strAddr);
     297                 :           0 :         return true;
     298                 :           0 :     }
     299                 :             : 
     300                 :           0 :     return FillableSigningProvider::AddCScript(redeemScript);
     301                 :             : }
     302                 :             : 
     303                 :           0 : void LegacyDataSPKM::LoadKeyMetadata(const CKeyID& keyID, const CKeyMetadata& meta)
     304                 :             : {
     305                 :           0 :     LOCK(cs_KeyStore);
     306   [ #  #  #  # ]:           0 :     mapKeyMetadata[keyID] = meta;
     307                 :           0 : }
     308                 :             : 
     309                 :           0 : void LegacyDataSPKM::LoadScriptMetadata(const CScriptID& script_id, const CKeyMetadata& meta)
     310                 :             : {
     311                 :           0 :     LOCK(cs_KeyStore);
     312   [ #  #  #  # ]:           0 :     m_script_metadata[script_id] = meta;
     313                 :           0 : }
     314                 :             : 
     315                 :        2827 : bool LegacyDataSPKM::AddKeyPubKeyInner(const CKey& key, const CPubKey& pubkey)
     316                 :             : {
     317                 :        2827 :     LOCK(cs_KeyStore);
     318   [ +  -  +  - ]:        2827 :     return FillableSigningProvider::AddKeyPubKey(key, pubkey);
     319                 :        2827 : }
     320                 :             : 
     321                 :           0 : bool LegacyDataSPKM::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret, bool checksum_valid)
     322                 :             : {
     323                 :             :     // Set fDecryptionThoroughlyChecked to false when the checksum is invalid
     324         [ #  # ]:           0 :     if (!checksum_valid) {
     325                 :           0 :         fDecryptionThoroughlyChecked = false;
     326                 :             :     }
     327                 :             : 
     328                 :           0 :     return AddCryptedKeyInner(vchPubKey, vchCryptedSecret);
     329                 :             : }
     330                 :             : 
     331                 :           0 : bool LegacyDataSPKM::AddCryptedKeyInner(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
     332                 :             : {
     333                 :           0 :     LOCK(cs_KeyStore);
     334         [ #  # ]:           0 :     assert(mapKeys.empty());
     335                 :             : 
     336   [ #  #  #  #  :           0 :     mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
                   #  # ]
     337         [ #  # ]:           0 :     ImplicitlyLearnRelatedKeyScripts(vchPubKey);
     338         [ #  # ]:           0 :     return true;
     339                 :           0 : }
     340                 :             : 
     341                 :       57979 : bool LegacyDataSPKM::HaveWatchOnly(const CScript &dest) const
     342                 :             : {
     343                 :       57979 :     LOCK(cs_KeyStore);
     344         [ +  - ]:       57979 :     return setWatchOnly.contains(dest);
     345                 :       57979 : }
     346                 :             : 
     347                 :         701 : bool LegacyDataSPKM::LoadWatchOnly(const CScript &dest)
     348                 :             : {
     349                 :         701 :     return AddWatchOnlyInMem(dest);
     350                 :             : }
     351                 :             : 
     352                 :         701 : static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
     353                 :             : {
     354                 :         701 :     std::vector<std::vector<unsigned char>> solutions;
     355   [ +  -  -  +  :         701 :     return Solver(dest, solutions) == TxoutType::PUBKEY &&
                   -  - ]
     356   [ -  -  -  - ]:         701 :         (pubKeyOut = CPubKey(solutions[0])).IsFullyValid();
     357                 :         701 : }
     358                 :             : 
     359                 :         701 : bool LegacyDataSPKM::AddWatchOnlyInMem(const CScript &dest)
     360                 :             : {
     361                 :         701 :     LOCK(cs_KeyStore);
     362         [ +  - ]:         701 :     setWatchOnly.insert(dest);
     363         [ +  - ]:         701 :     CPubKey pubKey;
     364   [ +  -  -  + ]:         701 :     if (ExtractPubKey(dest, pubKey)) {
     365   [ #  #  #  # ]:           0 :         mapWatchKeys[pubKey.GetID()] = pubKey;
     366         [ #  # ]:           0 :         ImplicitlyLearnRelatedKeyScripts(pubKey);
     367                 :             :     }
     368         [ +  - ]:         701 :     return true;
     369                 :         701 : }
     370                 :             : 
     371                 :         263 : void LegacyDataSPKM::LoadHDChain(const CHDChain& chain)
     372                 :             : {
     373                 :         263 :     LOCK(cs_KeyStore);
     374         [ +  - ]:         263 :     m_hd_chain = chain;
     375                 :         263 : }
     376                 :             : 
     377                 :         262 : void LegacyDataSPKM::AddInactiveHDChain(const CHDChain& chain)
     378                 :             : {
     379                 :         262 :     LOCK(cs_KeyStore);
     380         [ -  + ]:         524 :     assert(!chain.seed_id.IsNull());
     381   [ +  -  +  - ]:         262 :     m_inactive_hd_chains[chain.seed_id] = chain;
     382                 :         262 : }
     383                 :             : 
     384                 :       74259 : bool LegacyDataSPKM::HaveKey(const CKeyID &address) const
     385                 :             : {
     386                 :       74259 :     LOCK(cs_KeyStore);
     387   [ +  -  +  - ]:       74259 :     if (!m_storage.HasEncryptionKeys()) {
     388         [ +  - ]:       74259 :         return FillableSigningProvider::HaveKey(address);
     389                 :             :     }
     390                 :           0 :     return mapCryptedKeys.contains(address);
     391                 :       74259 : }
     392                 :             : 
     393                 :       29422 : bool LegacyDataSPKM::GetKey(const CKeyID &address, CKey& keyOut) const
     394                 :             : {
     395                 :       29422 :     LOCK(cs_KeyStore);
     396   [ +  -  +  - ]:       29422 :     if (!m_storage.HasEncryptionKeys()) {
     397         [ +  - ]:       29422 :         return FillableSigningProvider::GetKey(address, keyOut);
     398                 :             :     }
     399                 :             : 
     400                 :           0 :     CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
     401         [ #  # ]:           0 :     if (mi != mapCryptedKeys.end())
     402                 :             :     {
     403         [ #  # ]:           0 :         const CPubKey &vchPubKey = (*mi).second.first;
     404                 :           0 :         const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
     405   [ #  #  #  # ]:           0 :         return m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
     406         [ #  # ]:           0 :             return DecryptKey(encryption_key, vchCryptedSecret, vchPubKey, keyOut);
     407                 :             :         });
     408                 :             :     }
     409                 :             :     return false;
     410                 :       29422 : }
     411                 :             : 
     412                 :       52567 : bool LegacyDataSPKM::GetKeyOrigin(const CKeyID& keyID, KeyOriginInfo& info) const
     413                 :             : {
     414                 :       52567 :     CKeyMetadata meta;
     415                 :       52567 :     {
     416         [ +  - ]:       52567 :         LOCK(cs_KeyStore);
     417                 :       52567 :         auto it = mapKeyMetadata.find(keyID);
     418         [ +  - ]:       52567 :         if (it == mapKeyMetadata.end()) {
     419         [ +  - ]:       52567 :             return false;
     420                 :             :         }
     421         [ #  # ]:           0 :         meta = it->second;
     422                 :       52567 :     }
     423         [ #  # ]:           0 :     if (meta.has_key_origin) {
     424                 :           0 :         std::copy(meta.key_origin.fingerprint, meta.key_origin.fingerprint + 4, info.fingerprint);
     425         [ #  # ]:           0 :         info.path = meta.key_origin.path;
     426                 :             :     } else { // Single pubkeys get the master fingerprint of themselves
     427                 :           0 :         std::copy(keyID.begin(), keyID.begin() + 4, info.fingerprint);
     428                 :             :     }
     429                 :             :     return true;
     430                 :       52567 : }
     431                 :             : 
     432                 :        1107 : bool LegacyDataSPKM::GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const
     433                 :             : {
     434                 :        1107 :     LOCK(cs_KeyStore);
     435                 :        1107 :     WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
     436         [ -  + ]:        1107 :     if (it != mapWatchKeys.end()) {
     437                 :           0 :         pubkey_out = it->second;
     438                 :           0 :         return true;
     439                 :             :     }
     440                 :             :     return false;
     441                 :        1107 : }
     442                 :             : 
     443                 :       10787 : bool LegacyDataSPKM::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
     444                 :             : {
     445                 :       10787 :     LOCK(cs_KeyStore);
     446   [ +  -  +  - ]:       10787 :     if (!m_storage.HasEncryptionKeys()) {
     447   [ +  -  +  + ]:       10787 :         if (!FillableSigningProvider::GetPubKey(address, vchPubKeyOut)) {
     448         [ +  - ]:        1107 :             return GetWatchPubKey(address, vchPubKeyOut);
     449                 :             :         }
     450                 :             :         return true;
     451                 :             :     }
     452                 :             : 
     453                 :           0 :     CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
     454         [ #  # ]:           0 :     if (mi != mapCryptedKeys.end())
     455                 :             :     {
     456                 :           0 :         vchPubKeyOut = (*mi).second.first;
     457                 :           0 :         return true;
     458                 :             :     }
     459                 :             :     // Check for watch-only pubkeys
     460         [ #  # ]:           0 :     return GetWatchPubKey(address, vchPubKeyOut);
     461                 :       10787 : }
     462                 :             : 
     463                 :        1402 : std::unordered_set<CScript, SaltedSipHasher> LegacyDataSPKM::GetCandidateScriptPubKeys() const
     464                 :             : {
     465                 :        1402 :     LOCK(cs_KeyStore);
     466         [ +  - ]:        1402 :     std::unordered_set<CScript, SaltedSipHasher> candidate_spks;
     467                 :             : 
     468                 :             :     // For every private key in the wallet, there should be a P2PK, P2PKH, P2WPKH, and P2SH-P2WPKH
     469                 :        5484 :     const auto& add_pubkey = [&candidate_spks](const CPubKey& pub) -> void {
     470         [ +  - ]:        4082 :         candidate_spks.insert(GetScriptForRawPubKey(pub));
     471         [ +  - ]:        8164 :         candidate_spks.insert(GetScriptForDestination(PKHash(pub)));
     472                 :             : 
     473         [ +  - ]:        4082 :         CScript wpkh = GetScriptForDestination(WitnessV0KeyHash(pub));
     474         [ +  - ]:        4082 :         candidate_spks.insert(wpkh);
     475   [ +  -  +  - ]:        8164 :         candidate_spks.insert(GetScriptForDestination(ScriptHash(wpkh)));
     476                 :        4082 :     };
     477   [ +  -  +  + ]:        5484 :     for (const auto& [_, key] : mapKeys) {
     478   [ +  -  +  - ]:        4082 :         add_pubkey(key.GetPubKey());
     479                 :             :     }
     480   [ -  -  -  + ]:        1402 :     for (const auto& [_, ckeypair] : mapCryptedKeys) {
     481         [ #  # ]:           0 :         add_pubkey(ckeypair.first);
     482                 :             :     }
     483                 :             : 
     484                 :             :     // mapScripts contains all redeemScripts and witnessScripts. Therefore each script in it has
     485                 :             :     // itself, P2SH, P2WSH, and P2SH-P2WSH as a candidate.
     486                 :             :     // Invalid scripts such as P2SH-P2SH and P2WSH-P2SH, among others, will be added as candidates.
     487                 :             :     // Callers of this function will need to remove such scripts.
     488                 :       15118 :     const auto& add_script = [&candidate_spks](const CScript& script) -> void {
     489                 :       13716 :         candidate_spks.insert(script);
     490         [ +  - ]:       27432 :         candidate_spks.insert(GetScriptForDestination(ScriptHash(script)));
     491                 :             : 
     492         [ +  - ]:       13716 :         CScript wsh = GetScriptForDestination(WitnessV0ScriptHash(script));
     493         [ +  - ]:       13716 :         candidate_spks.insert(wsh);
     494   [ +  -  +  - ]:       27432 :         candidate_spks.insert(GetScriptForDestination(ScriptHash(wsh)));
     495                 :       13716 :     };
     496   [ +  -  +  + ]:       13716 :     for (const auto& [_, script] : mapScripts) {
     497         [ +  - ]:       12314 :         add_script(script);
     498                 :             :     }
     499                 :             : 
     500                 :             :     // Although setWatchOnly should only contain output scripts, we will also include each script's
     501                 :             :     // P2SH, P2WSH, and P2SH-P2WSH as a precaution.
     502         [ +  + ]:        2804 :     for (const auto& script : setWatchOnly) {
     503         [ +  - ]:        1402 :         add_script(script);
     504                 :             :     }
     505                 :             : 
     506         [ +  - ]:        1402 :     return candidate_spks;
     507                 :        1402 : }
     508                 :             : 
     509                 :         701 : std::unordered_set<CScript, SaltedSipHasher> LegacyDataSPKM::GetScriptPubKeys() const
     510                 :             : {
     511                 :             :     // Run IsMine() on each candidate output script. Any script that IsMine is an output
     512                 :             :     // script to return.
     513                 :             :     // This both filters out things that are not watched by the wallet, and things that are invalid.
     514                 :         701 :     std::unordered_set<CScript, SaltedSipHasher> spks;
     515   [ +  -  +  +  :       32312 :     for (const CScript& script : GetCandidateScriptPubKeys()) {
                   +  - ]
     516   [ +  +  +  - ]:       31611 :         if (IsMine(script)) {
     517         [ +  - ]:       10415 :             spks.insert(script);
     518                 :             :         }
     519                 :             :     }
     520                 :             : 
     521                 :         701 :     return spks;
     522                 :           0 : }
     523                 :             : 
     524                 :           0 : std::unordered_set<CScript, SaltedSipHasher> LegacyDataSPKM::GetNotMineScriptPubKeys() const
     525                 :             : {
     526                 :           0 :     LOCK(cs_KeyStore);
     527         [ #  # ]:           0 :     std::unordered_set<CScript, SaltedSipHasher> spks;
     528         [ #  # ]:           0 :     for (const CScript& script : setWatchOnly) {
     529   [ #  #  #  #  :           0 :         if (!IsMine(script)) spks.insert(script);
                   #  # ]
     530                 :             :     }
     531         [ #  # ]:           0 :     return spks;
     532                 :           0 : }
     533                 :             : 
     534                 :         701 : std::optional<MigrationData> LegacyDataSPKM::MigrateToDescriptor()
     535                 :             : {
     536                 :         701 :     LOCK(cs_KeyStore);
     537   [ +  -  -  + ]:         701 :     if (m_storage.IsLocked()) {
     538                 :           0 :         return std::nullopt;
     539                 :             :     }
     540                 :             : 
     541                 :         701 :     MigrationData out;
     542                 :             : 
     543         [ +  - ]:         701 :     std::unordered_set<CScript, SaltedSipHasher> spks{GetScriptPubKeys()};
     544                 :             : 
     545                 :             :     // Get all key ids
     546                 :         701 :     std::set<CKeyID> keyids;
     547         [ +  + ]:        2742 :     for (const auto& key_pair : mapKeys) {
     548         [ +  - ]:        2041 :         keyids.insert(key_pair.first);
     549                 :             :     }
     550         [ -  + ]:         701 :     for (const auto& key_pair : mapCryptedKeys) {
     551         [ #  # ]:           0 :         keyids.insert(key_pair.first);
     552                 :             :     }
     553                 :             : 
     554                 :             :     // Get key metadata and figure out which keys don't have a seed
     555                 :             :     // Note that we do not ignore the seeds themselves because they are considered IsMine!
     556         [ +  + ]:        2742 :     for (auto keyid_it = keyids.begin(); keyid_it != keyids.end();) {
     557                 :        2041 :         const CKeyID& keyid = *keyid_it;
     558                 :        2041 :         const auto& it = mapKeyMetadata.find(keyid);
     559         [ -  + ]:        2041 :         if (it != mapKeyMetadata.end()) {
     560         [ #  # ]:           0 :             const CKeyMetadata& meta = it->second;
     561   [ #  #  #  # ]:           0 :             if (meta.hdKeypath == "s" || meta.hdKeypath == "m") {
     562                 :           0 :                 keyid_it++;
     563                 :           0 :                 continue;
     564                 :             :             }
     565   [ #  #  #  #  :           0 :             if (!meta.hd_seed_id.IsNull() && (m_hd_chain.seed_id == meta.hd_seed_id || m_inactive_hd_chains.contains(meta.hd_seed_id))) {
             #  #  #  # ]
     566                 :           0 :                 keyid_it = keyids.erase(keyid_it);
     567                 :           0 :                 continue;
     568                 :             :             }
     569                 :             :         }
     570                 :        2041 :         keyid_it++;
     571                 :             :     }
     572                 :             : 
     573   [ +  -  +  - ]:         701 :     WalletBatch batch(m_storage.GetDatabase());
     574   [ +  -  -  + ]:         701 :     if (!batch.TxnBegin()) {
     575         [ #  # ]:           0 :         LogWarning("Error generating descriptors for migration, cannot initialize db transaction");
     576                 :           0 :         return std::nullopt;
     577                 :             :     }
     578                 :             : 
     579                 :             :     // keyids is now all non-HD keys. Each key will have its own combo descriptor
     580         [ +  + ]:        2742 :     for (const CKeyID& keyid : keyids) {
     581                 :        2041 :         CKey key;
     582   [ +  -  -  + ]:        2041 :         if (!GetKey(keyid, key)) {
     583                 :           0 :             assert(false);
     584                 :             :         }
     585                 :             : 
     586                 :             :         // Get birthdate from key meta
     587                 :        2041 :         uint64_t creation_time = 0;
     588                 :        2041 :         const auto& it = mapKeyMetadata.find(keyid);
     589         [ -  + ]:        2041 :         if (it != mapKeyMetadata.end()) {
     590                 :           0 :             creation_time = it->second.nCreateTime;
     591                 :             :         }
     592                 :             : 
     593                 :             :         // Get the key origin
     594                 :             :         // Maybe this doesn't matter because floating keys here shouldn't have origins
     595         [ +  - ]:        2041 :         KeyOriginInfo info;
     596         [ +  - ]:        2041 :         bool has_info = GetKeyOrigin(keyid, info);
     597   [ -  +  -  -  :        2041 :         std::string origin_str = has_info ? "[" + HexStr(info.fingerprint) + FormatHDKeypath(info.path) + "]" : "";
          -  -  -  -  -  
          -  -  -  +  -  
          -  +  -  +  -  
          +  -  -  -  -  
             -  -  -  - ]
     598                 :             : 
     599                 :             :         // Construct the combo descriptor
     600   [ +  -  +  -  :        4082 :         std::string desc_str = "combo(" + origin_str + HexStr(key.GetPubKey()) + ")";
             +  -  +  - ]
     601                 :        2041 :         FlatSigningProvider provider;
     602         [ -  + ]:        2041 :         std::string error;
     603   [ -  +  +  - ]:        2041 :         std::vector<std::unique_ptr<Descriptor>> descs = Parse(desc_str, provider, error, false);
     604   [ -  +  +  - ]:        2041 :         CHECK_NONFATAL(descs.size() == 1); // It shouldn't be possible to have an invalid or multipath descriptor
     605   [ +  -  +  -  :        2041 :         WalletDescriptor w_desc(std::move(descs.at(0)), creation_time, 0, 0, 0);
                   +  - ]
     606                 :             : 
     607                 :             :         // Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
     608   [ +  -  +  -  :        2041 :         provider.keys.emplace(key.GetPubKey().GetID(), key);
                   +  - ]
     609         [ +  - ]:        2041 :         auto desc_spk_man = DescriptorScriptPubKeyMan::CreateFromMigration(m_storage, batch, w_desc, /*keypool_size=*/0, provider);
     610         [ +  - ]:        2041 :         auto desc_spks = desc_spk_man->GetScriptPubKeys();
     611                 :             : 
     612                 :             :         // Remove the scriptPubKeys from our current set
     613   [ +  +  +  - ]:        9851 :         for (const CScript& spk : desc_spks) {
     614         [ +  - ]:        7810 :             size_t erased = spks.erase(spk);
     615         [ -  + ]:        7810 :             assert(erased == 1);
     616   [ +  -  -  + ]:        7810 :             assert(IsMine(spk));
     617                 :             :         }
     618                 :             : 
     619         [ +  - ]:        2041 :         out.desc_spkms.push_back(std::move(desc_spk_man));
     620                 :        2041 :     }
     621                 :             : 
     622                 :             :     // Handle HD keys by using the CHDChains
     623         [ +  - ]:         701 :     std::set<CHDChain> chains;
     624         [ +  - ]:         701 :     chains.insert(m_hd_chain);
     625   [ +  +  +  - ]:         954 :     for (const auto& chain_pair : m_inactive_hd_chains) {
     626         [ +  - ]:         253 :         chains.insert(chain_pair.second);
     627                 :             :     }
     628                 :             : 
     629                 :         701 :     bool can_support_hd_split_feature = m_hd_chain.nVersion >= CHDChain::VERSION_HD_CHAIN_SPLIT;
     630                 :             : 
     631         [ +  + ]:        1545 :     for (const CHDChain& chain : chains) {
     632         [ +  + ]:        2532 :         for (int i = 0; i < 2; ++i) {
     633                 :             :             // Skip if doing internal chain and split chain is not supported
     634   [ +  +  +  +  :        3376 :             if (chain.seed_id.IsNull() || (i == 1 && !can_support_hd_split_feature)) {
                   +  + ]
     635                 :         937 :                 continue;
     636                 :             :             }
     637                 :             :             // Get the master xprv
     638                 :         751 :             CKey seed_key;
     639   [ +  -  -  + ]:         751 :             if (!GetKey(chain.seed_id, seed_key)) {
     640                 :           0 :                 assert(false);
     641                 :             :             }
     642         [ +  - ]:         751 :             CExtKey master_key;
     643   [ +  -  +  - ]:        1502 :             master_key.SetSeed(seed_key);
     644                 :             : 
     645                 :             :             // Make the combo descriptor
     646   [ +  -  +  - ]:         751 :             std::string xpub = EncodeExtPubKey(master_key.Neuter());
     647   [ +  -  +  -  :        2253 :             std::string desc_str = "combo(" + xpub + "/0h/" + ToString(i) + "h/*h)";
                   +  - ]
     648                 :         751 :             FlatSigningProvider provider;
     649         [ -  + ]:         751 :             std::string error;
     650   [ -  +  +  - ]:         751 :             std::vector<std::unique_ptr<Descriptor>> descs = Parse(desc_str, provider, error, false);
     651   [ -  +  +  - ]:         751 :             CHECK_NONFATAL(descs.size() == 1); // It shouldn't be possible to have an invalid or multipath descriptor
     652         [ +  + ]:         751 :             uint32_t chain_counter = std::max((i == 1 ? chain.nInternalChainCounter : chain.nExternalChainCounter), (uint32_t)0);
     653   [ +  -  +  -  :         751 :             WalletDescriptor w_desc(std::move(descs.at(0)), 0, 0, chain_counter, 0);
                   +  - ]
     654                 :             : 
     655                 :             :             // Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
     656   [ +  -  +  -  :         751 :             provider.keys.emplace(master_key.key.GetPubKey().GetID(), master_key.key);
                   +  - ]
     657         [ +  - ]:         751 :             auto desc_spk_man = DescriptorScriptPubKeyMan::CreateFromMigration(m_storage, batch, w_desc, /*keypool_size=*/0, provider);
     658         [ +  - ]:         751 :             auto desc_spks = desc_spk_man->GetScriptPubKeys();
     659                 :             : 
     660                 :             :             // Remove the scriptPubKeys from our current set
     661   [ -  +  -  - ]:         751 :             for (const CScript& spk : desc_spks) {
     662         [ #  # ]:           0 :                 size_t erased = spks.erase(spk);
     663         [ #  # ]:           0 :                 assert(erased == 1);
     664   [ #  #  #  # ]:           0 :                 assert(IsMine(spk));
     665                 :             :             }
     666                 :             : 
     667         [ +  - ]:         751 :             out.desc_spkms.push_back(std::move(desc_spk_man));
     668                 :         751 :         }
     669                 :             :     }
     670                 :             :     // Add the current master seed to the migration data
     671         [ +  + ]:        1402 :     if (!m_hd_chain.seed_id.IsNull()) {
     672                 :         255 :         CKey seed_key;
     673   [ +  -  -  + ]:         255 :         if (!GetKey(m_hd_chain.seed_id, seed_key)) {
     674                 :           0 :             assert(false);
     675                 :             :         }
     676   [ +  -  +  - ]:         510 :         out.master_key.SetSeed(seed_key);
     677                 :         255 :     }
     678                 :             : 
     679                 :             :     // Handle the rest of the scriptPubKeys which must be imports and may not have all info
     680         [ +  + ]:        3306 :     for (auto it = spks.begin(); it != spks.end();) {
     681         [ +  - ]:        2605 :         const CScript& spk = *it;
     682                 :             : 
     683                 :             :         // Get birthdate from script meta
     684                 :        2605 :         uint64_t creation_time = 0;
     685         [ +  - ]:        2605 :         const auto& mit = m_script_metadata.find(CScriptID(spk));
     686         [ -  + ]:        2605 :         if (mit != m_script_metadata.end()) {
     687                 :           0 :             creation_time = mit->second.nCreateTime;
     688                 :             :         }
     689                 :             : 
     690                 :             :         // InferDescriptor as that will get us all the solving info if it is there
     691   [ +  -  +  - ]:        5210 :         std::unique_ptr<Descriptor> desc = InferDescriptor(spk, *GetSolvingProvider(spk));
     692                 :             : 
     693                 :             :         // Past bugs in InferDescriptor have caused it to create descriptors which cannot be re-parsed.
     694                 :             :         // Re-parse the descriptors to detect that, and skip any that do not parse.
     695                 :        2605 :         {
     696         [ +  - ]:        2605 :             std::string desc_str = desc->ToString();
     697                 :        2605 :             FlatSigningProvider parsed_keys;
     698         [ -  + ]:        2605 :             std::string parse_error;
     699   [ -  +  +  - ]:        2605 :             std::vector<std::unique_ptr<Descriptor>> parsed_descs = Parse(desc_str, parsed_keys, parse_error);
     700         [ -  + ]:        2605 :             if (parsed_descs.empty()) {
     701                 :             :                 // Remove this scriptPubKey from the set
     702                 :           0 :                 it = spks.erase(it);
     703                 :           0 :                 continue;
     704                 :             :             }
     705                 :        2605 :         }
     706                 :             : 
     707                 :             :         // Get the private keys for this descriptor
     708                 :        2605 :         std::vector<CScript> scripts;
     709                 :        2605 :         FlatSigningProvider keys;
     710   [ +  -  -  + ]:        2605 :         if (!desc->Expand(0, DUMMY_SIGNING_PROVIDER, scripts, keys)) {
     711                 :           0 :             assert(false);
     712                 :             :         }
     713                 :        2605 :         std::set<CKeyID> privkeyids;
     714         [ +  + ]:        4659 :         for (const auto& key_orig_pair : keys.origins) {
     715         [ +  - ]:        2054 :             privkeyids.insert(key_orig_pair.first);
     716                 :             :         }
     717                 :             : 
     718                 :        2605 :         std::vector<CScript> desc_spks;
     719                 :             : 
     720                 :             :         // If we can't provide all private keys for this inferred descriptor,
     721                 :             :         // but this wallet is not watch-only, migrate it to the watch-only wallet.
     722   [ +  -  +  +  :        2605 :         if (!desc->HavePrivateKeys(*this) && !m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
             +  -  -  + ]
     723   [ +  -  +  - ]:         701 :             out.watch_descs.emplace_back(desc->ToString(), creation_time);
     724                 :             : 
     725                 :             :             // Get the scriptPubKeys without writing this to the wallet
     726                 :         701 :             FlatSigningProvider provider;
     727         [ +  - ]:         701 :             desc->Expand(0, provider, desc_spks, provider);
     728                 :         701 :         } else {
     729                 :             :             // Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
     730         [ +  + ]:        3958 :             for (const auto& keyid : privkeyids) {
     731                 :        2054 :                 CKey key;
     732   [ +  -  -  + ]:        2054 :                 if (!GetKey(keyid, key)) {
     733                 :           0 :                     continue;
     734                 :             :                 }
     735   [ +  -  +  -  :        2054 :                 keys.keys.emplace(key.GetPubKey().GetID(), key);
                   +  - ]
     736                 :        2054 :             }
     737   [ +  -  +  - ]:        1904 :             WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
     738         [ +  - ]:        1904 :             auto desc_spk_man = DescriptorScriptPubKeyMan::CreateFromMigration(m_storage, batch, w_desc, /*keypool_size=*/0, keys);
     739         [ +  - ]:        1904 :             auto desc_spks_set = desc_spk_man->GetScriptPubKeys();
     740         [ +  - ]:        1904 :             desc_spks.insert(desc_spks.end(), desc_spks_set.begin(), desc_spks_set.end());
     741                 :             : 
     742         [ +  - ]:        1904 :             out.desc_spkms.push_back(std::move(desc_spk_man));
     743                 :        1904 :         }
     744                 :             : 
     745                 :             :         // Remove the scriptPubKeys from our current set
     746         [ +  + ]:        5210 :         for (const CScript& desc_spk : desc_spks) {
     747         [ +  - ]:        2605 :             auto del_it = spks.find(desc_spk);
     748         [ +  - ]:        2605 :             assert(del_it != spks.end());
     749   [ +  -  -  + ]:        2605 :             assert(IsMine(desc_spk));
     750                 :        2605 :             it = spks.erase(del_it);
     751                 :             :         }
     752                 :        2605 :     }
     753                 :             : 
     754                 :             :     // Make sure that we have accounted for all scriptPubKeys
     755         [ +  - ]:         701 :     if (!Assume(spks.empty())) {
     756   [ #  #  #  # ]:           0 :         LogError("%s", STR_INTERNAL_BUG("Error: Some output scripts were not migrated."));
     757                 :           0 :         return std::nullopt;
     758                 :             :     }
     759                 :             : 
     760                 :             :     // Legacy wallets can also contain scripts whose P2SH, P2WSH, or P2SH-P2WSH it is not watching for
     761                 :             :     // but can provide script data to a PSBT spending them. These "solvable" output scripts will need to
     762                 :             :     // be put into the separate "solvables" wallet.
     763                 :             :     // These can be detected by going through the entire candidate output scripts, finding the not IsMine scripts,
     764                 :             :     // and checking CanProvide() which will dummy sign.
     765   [ +  -  +  +  :       32312 :     for (const CScript& script : GetCandidateScriptPubKeys()) {
                   +  - ]
     766                 :             :         // Since we only care about P2SH, P2WSH, and P2SH-P2WSH, filter out any scripts that are not those
     767   [ +  +  +  -  :       31611 :         if (!script.IsPayToScriptHash() && !script.IsPayToWitnessScriptHash()) {
             +  +  +  - ]
     768                 :       10595 :             continue;
     769                 :             :         }
     770   [ +  -  +  + ]:       21016 :         if (IsMine(script)) {
     771                 :        3768 :             continue;
     772                 :             :         }
     773                 :       17248 :         SignatureData dummy_sigdata;
     774   [ +  -  +  + ]:       17248 :         if (!CanProvide(script, dummy_sigdata)) {
     775                 :       12168 :             continue;
     776                 :             :         }
     777                 :             : 
     778                 :             :         // Get birthdate from script meta
     779                 :        5080 :         uint64_t creation_time = 0;
     780         [ +  - ]:        5080 :         const auto& it = m_script_metadata.find(CScriptID(script));
     781         [ -  + ]:        5080 :         if (it != m_script_metadata.end()) {
     782                 :           0 :             creation_time = it->second.nCreateTime;
     783                 :             :         }
     784                 :             : 
     785                 :             :         // InferDescriptor as that will get us all the solving info if it is there
     786   [ +  -  +  - ]:        5080 :         std::unique_ptr<Descriptor> desc = InferDescriptor(script, *GetSolvingProvider(script));
     787   [ +  -  +  + ]:        5080 :         if (!desc->IsSolvable()) {
     788                 :             :             // The wallet was able to provide some information, but not enough to make a descriptor that actually
     789                 :             :             // contains anything useful. This is probably because the script itself is actually unsignable (e.g. P2WSH-P2WSH).
     790                 :        1406 :             continue;
     791                 :             :         }
     792                 :             : 
     793                 :             :         // Past bugs in InferDescriptor have caused it to create descriptors which cannot be re-parsed
     794                 :             :         // Re-parse the descriptors to detect that, and skip any that do not parse.
     795                 :        3674 :         {
     796         [ +  - ]:        3674 :             std::string desc_str = desc->ToString();
     797                 :        3674 :             FlatSigningProvider parsed_keys;
     798         [ -  + ]:        3674 :             std::string parse_error;
     799   [ -  +  +  - ]:        3674 :             std::vector<std::unique_ptr<Descriptor>> parsed_descs = Parse(desc_str, parsed_keys, parse_error, false);
     800         [ -  + ]:        3674 :             if (parsed_descs.empty()) {
     801                 :           0 :                 continue;
     802                 :             :             }
     803                 :        3674 :         }
     804                 :             : 
     805   [ +  -  +  - ]:        3674 :         out.solvable_descs.emplace_back(desc->ToString(), creation_time);
     806                 :       17248 :     }
     807                 :             : 
     808                 :             :     // Finalize transaction
     809   [ +  -  -  + ]:         701 :     if (!batch.TxnCommit()) {
     810         [ #  # ]:           0 :         LogWarning("Error generating descriptors for migration, cannot commit db transaction");
     811                 :           0 :         return std::nullopt;
     812                 :             :     }
     813                 :             : 
     814                 :         701 :     return out;
     815                 :        2103 : }
     816                 :             : 
     817                 :           0 : bool LegacyDataSPKM::DeleteRecordsWithDB(WalletBatch& batch)
     818                 :             : {
     819                 :           0 :     LOCK(cs_KeyStore);
     820   [ #  #  #  # ]:           0 :     return batch.EraseRecords(DBKeys::LEGACY_TYPES);
     821                 :           0 : }
     822                 :             : 
     823                 :       17727 : std::unique_ptr<DescriptorScriptPubKeyMan> DescriptorScriptPubKeyMan::CreateFromImport(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const FlatSigningProvider& provider)
     824                 :             : {
     825   [ +  -  +  - ]:       17727 :     auto spkm = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(storage, descriptor, keypool_size));
     826         [ +  - ]:       17727 :     LOCK(spkm->cs_desc_man);
     827   [ +  -  +  - ]:       17727 :     WalletBatch batch(storage.GetDatabase());
     828         [ +  - ]:       17727 :     spkm->UpdateWithSigningProvider(batch, provider);
     829                 :       17727 :     return spkm;
     830         [ +  - ]:       35454 : }
     831                 :             : 
     832                 :        4696 : std::unique_ptr<DescriptorScriptPubKeyMan> DescriptorScriptPubKeyMan::CreateFromMigration(WalletStorage& storage, WalletBatch& batch, WalletDescriptor& descriptor, int64_t keypool_size, const FlatSigningProvider& provider)
     833                 :             : {
     834   [ +  -  +  - ]:        4696 :     auto spkm = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(storage, descriptor, keypool_size));
     835         [ +  - ]:        4696 :     LOCK(spkm->cs_desc_man);
     836         [ +  - ]:        4696 :     spkm->UpdateWithSigningProvider(batch, provider);
     837         [ +  - ]:        4696 :     return spkm;
     838                 :        4696 : }
     839                 :             : 
     840                 :           0 : DescriptorScriptPubKeyMan::DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys)
     841                 :             :     : ScriptPubKeyMan(storage),
     842         [ #  # ]:           0 :     m_map_keys(keys),
     843         [ #  # ]:           0 :     m_map_crypted_keys(ckeys),
     844                 :           0 :     m_keypool_size(keypool_size),
     845   [ #  #  #  #  :           0 :     m_wallet_descriptor(descriptor)
                   #  # ]
     846                 :             : {
     847   [ #  #  #  # ]:           0 :     if (!keys.empty() && !ckeys.empty()) {
     848         [ #  # ]:           0 :         throw std::runtime_error("Wallet contains both unencrypted and encrypted keys");
     849                 :             :     }
     850         [ #  # ]:           0 :     Load();
     851                 :           0 : }
     852                 :             : 
     853                 :           0 : std::unique_ptr<DescriptorScriptPubKeyMan> DescriptorScriptPubKeyMan::LoadFromStorage(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys)
     854                 :             : {
     855         [ #  # ]:           0 :     return std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(storage, descriptor, keypool_size, keys, ckeys));
     856                 :             : }
     857                 :             : 
     858                 :           0 : std::unique_ptr<DescriptorScriptPubKeyMan> DescriptorScriptPubKeyMan::GenerateNewSingleSig(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, const CExtKey& master_key, OutputType addr_type, bool internal)
     859                 :             : {
     860         [ #  # ]:           0 :     auto spkm = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(storage, keypool_size));
     861         [ #  # ]:           0 :     spkm->SetupDescriptorGeneration(batch, master_key, addr_type, internal);
     862                 :           0 :     return spkm;
     863                 :           0 : }
     864                 :             : 
     865                 :      202815 : util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type)
     866                 :             : {
     867                 :             :     // Returns true if this descriptor supports getting new addresses. Conditions where we may be unable to fetch them (e.g. locked) are caught later
     868         [ +  + ]:      202815 :     if (!CanGetAddresses()) {
     869                 :        1886 :         return util::Error{_("No addresses available")};
     870                 :             :     }
     871                 :      201872 :     {
     872                 :      201872 :         LOCK(cs_desc_man);
     873   [ +  -  -  + ]:      201872 :         assert(m_wallet_descriptor.descriptor->IsSingleType()); // This is a combo descriptor which should not be an active descriptor
     874         [ +  - ]:      201872 :         std::optional<OutputType> desc_addr_type = m_wallet_descriptor.descriptor->GetOutputType();
     875         [ -  + ]:      201872 :         assert(desc_addr_type);
     876         [ -  + ]:      201872 :         if (type != *desc_addr_type) {
     877   [ #  #  #  # ]:           0 :             throw std::runtime_error(std::string(__func__) + ": Types are inconsistent. Stored type does not match type of newly generated address");
     878                 :             :         }
     879                 :             : 
     880         [ +  - ]:      201872 :         TopUp();
     881                 :             : 
     882                 :             :         // Get the scriptPubKey from the descriptor
     883                 :      201872 :         FlatSigningProvider out_keys;
     884                 :      201872 :         std::vector<CScript> scripts_temp;
     885   [ -  +  -  -  :      201872 :         if (m_wallet_descriptor.range_end <= m_max_cached_index && !TopUp(1)) {
                   -  - ]
     886                 :             :             // We can't generate anymore keys
     887         [ #  # ]:           0 :             return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
     888                 :             :         }
     889   [ +  -  -  + ]:      201872 :         if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.next_index, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
     890                 :             :             // We can't generate anymore keys
     891         [ #  # ]:           0 :             return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
     892                 :             :         }
     893                 :             : 
     894                 :      201872 :         CTxDestination dest;
     895   [ +  -  -  + ]:      201872 :         if (!ExtractDestination(scripts_temp[0], dest)) {
     896         [ #  # ]:           0 :             return util::Error{_("Error: Cannot extract destination from the generated scriptpubkey")}; // shouldn't happen
     897                 :             :         }
     898                 :      201872 :         m_wallet_descriptor.next_index++;
     899   [ +  -  +  -  :      201872 :         WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);
             +  -  +  - ]
     900                 :      201872 :         return dest;
     901         [ +  - ]:      403744 :     }
     902                 :             : }
     903                 :             : 
     904                 :      172138 : bool DescriptorScriptPubKeyMan::IsMine(const CScript& script) const
     905                 :             : {
     906                 :      172138 :     LOCK(cs_desc_man);
     907         [ +  - ]:      172138 :     return m_map_script_pub_keys.contains(script);
     908                 :      172138 : }
     909                 :             : 
     910                 :           0 : bool DescriptorScriptPubKeyMan::CheckDecryptionKey(const CKeyingMaterial& master_key)
     911                 :             : {
     912                 :           0 :     LOCK(cs_desc_man);
     913         [ #  # ]:           0 :     if (!m_map_keys.empty()) {
     914                 :             :         return false;
     915                 :             :     }
     916                 :             : 
     917                 :           0 :     bool keyPass = m_map_crypted_keys.empty(); // Always pass when there are no encrypted keys
     918                 :           0 :     bool keyFail = false;
     919         [ #  # ]:           0 :     for (const auto& mi : m_map_crypted_keys) {
     920                 :           0 :         const CPubKey &pubkey = mi.second.first;
     921                 :           0 :         const std::vector<unsigned char> &crypted_secret = mi.second.second;
     922                 :           0 :         CKey key;
     923   [ #  #  #  #  :           0 :         if (!DecryptKey(master_key, crypted_secret, pubkey, key)) {
                   #  # ]
     924                 :             :             keyFail = true;
     925                 :             :             break;
     926                 :             :         }
     927                 :           0 :         keyPass = true;
     928         [ #  # ]:           0 :         if (m_decryption_thoroughly_checked)
     929                 :             :             break;
     930                 :           0 :     }
     931         [ #  # ]:           0 :     if (keyPass && keyFail) {
     932         [ #  # ]:           0 :         LogWarning("The wallet is probably corrupted: Some keys decrypt but not all.");
     933         [ #  # ]:           0 :         throw std::runtime_error("Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
     934                 :             :     }
     935         [ #  # ]:           0 :     if (keyFail || !keyPass) {
     936                 :             :         return false;
     937                 :             :     }
     938                 :           0 :     m_decryption_thoroughly_checked = true;
     939                 :           0 :     return true;
     940                 :           0 : }
     941                 :             : 
     942                 :           0 : bool DescriptorScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch)
     943                 :             : {
     944                 :           0 :     LOCK(cs_desc_man);
     945         [ #  # ]:           0 :     if (!m_map_crypted_keys.empty()) {
     946                 :             :         return false;
     947                 :             :     }
     948                 :             : 
     949         [ #  # ]:           0 :     for (const KeyMap::value_type& key_in : m_map_keys)
     950                 :             :     {
     951                 :           0 :         const CKey &key = key_in.second;
     952         [ #  # ]:           0 :         CPubKey pubkey = key.GetPubKey();
     953   [ #  #  #  #  :           0 :         CKeyingMaterial secret{UCharCast(key.begin()), UCharCast(key.end())};
                   #  # ]
     954                 :           0 :         std::vector<unsigned char> crypted_secret;
     955   [ #  #  #  #  :           0 :         if (!EncryptSecret(master_key, secret, pubkey.GetHash(), crypted_secret)) {
                   #  # ]
     956                 :           0 :             return false;
     957                 :             :         }
     958   [ #  #  #  #  :           0 :         m_map_crypted_keys[pubkey.GetID()] = make_pair(pubkey, crypted_secret);
                   #  # ]
     959   [ #  #  #  # ]:           0 :         batch->WriteCryptedDescriptorKey(GetID(), pubkey, crypted_secret);
     960                 :           0 :     }
     961                 :           0 :     m_map_keys.clear();
     962                 :           0 :     return true;
     963                 :           0 : }
     964                 :             : 
     965                 :       51020 : util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, int64_t& index)
     966                 :             : {
     967                 :       51020 :     LOCK(cs_desc_man);
     968         [ +  - ]:       51020 :     auto op_dest = GetNewDestination(type);
     969                 :       51020 :     index = m_wallet_descriptor.next_index - 1;
     970         [ +  - ]:       51020 :     return op_dest;
     971                 :       51020 : }
     972                 :             : 
     973                 :         360 : void DescriptorScriptPubKeyMan::ReturnDestination(int64_t index, bool internal, const CTxDestination& addr)
     974                 :             : {
     975                 :         360 :     LOCK(cs_desc_man);
     976                 :             :     // Only return when the index was the most recent
     977         [ +  - ]:         360 :     if (m_wallet_descriptor.next_index - 1 == index) {
     978                 :         360 :         m_wallet_descriptor.next_index--;
     979                 :             :     }
     980   [ +  -  +  -  :         360 :     WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);
             +  -  +  - ]
     981         [ +  - ]:         360 :     NotifyCanGetAddressesChanged();
     982                 :         360 : }
     983                 :             : 
     984                 :      240362 : std::map<CKeyID, CKey> DescriptorScriptPubKeyMan::GetKeys() const
     985                 :             : {
     986                 :      240362 :     AssertLockHeld(cs_desc_man);
     987   [ -  +  -  - ]:      240362 :     if (m_storage.HasEncryptionKeys() && !m_storage.IsLocked()) {
     988                 :           0 :         KeyMap keys;
     989         [ #  # ]:           0 :         for (const auto& key_pair : m_map_crypted_keys) {
     990                 :           0 :             const CPubKey& pubkey = key_pair.second.first;
     991                 :           0 :             const std::vector<unsigned char>& crypted_secret = key_pair.second.second;
     992                 :           0 :             CKey key;
     993   [ #  #  #  # ]:           0 :             m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
     994         [ #  # ]:           0 :                 return DecryptKey(encryption_key, crypted_secret, pubkey, key);
     995                 :             :             });
     996   [ #  #  #  #  :           0 :             keys[pubkey.GetID()] = key;
                   #  # ]
     997                 :           0 :         }
     998                 :             :         return keys;
     999                 :           0 :     }
    1000                 :      240362 :     return m_map_keys;
    1001                 :             : }
    1002                 :             : 
    1003                 :           0 : bool DescriptorScriptPubKeyMan::HasPrivKey(const CKeyID& keyid) const
    1004                 :             : {
    1005                 :           0 :     AssertLockHeld(cs_desc_man);
    1006   [ #  #  #  # ]:           0 :     return m_map_keys.contains(keyid) || m_map_crypted_keys.contains(keyid);
    1007                 :             : }
    1008                 :             : 
    1009                 :           0 : std::optional<CKey> DescriptorScriptPubKeyMan::GetKey(const CKeyID& keyid) const
    1010                 :             : {
    1011                 :           0 :     AssertLockHeld(cs_desc_man);
    1012   [ #  #  #  # ]:           0 :     if (m_storage.HasEncryptionKeys() && !m_storage.IsLocked()) {
    1013                 :           0 :         const auto& it = m_map_crypted_keys.find(keyid);
    1014         [ #  # ]:           0 :         if (it == m_map_crypted_keys.end()) {
    1015                 :           0 :             return std::nullopt;
    1016                 :             :         }
    1017         [ #  # ]:           0 :         const std::vector<unsigned char>& crypted_secret = it->second.second;
    1018                 :           0 :         CKey key;
    1019   [ #  #  #  #  :           0 :         if (!Assume(m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
          #  #  #  #  #  
                      # ]
    1020                 :             :             return DecryptKey(encryption_key, crypted_secret, it->second.first, key);
    1021                 :             :         }))) {
    1022                 :           0 :             return std::nullopt;
    1023                 :             :         }
    1024                 :           0 :         return key;
    1025                 :           0 :     }
    1026                 :           0 :     const auto& it = m_map_keys.find(keyid);
    1027         [ #  # ]:           0 :     if (it == m_map_keys.end()) {
    1028                 :           0 :         return std::nullopt;
    1029                 :             :     }
    1030                 :           0 :     return it->second;
    1031                 :             : }
    1032                 :             : 
    1033                 :      204771 : bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
    1034                 :             : {
    1035                 :      204771 :     WalletBatch batch(m_storage.GetDatabase());
    1036   [ +  -  +  - ]:      204771 :     if (!batch.TxnBegin()) return false;
    1037         [ +  - ]:      204771 :     bool res = TopUpWithDB(batch, size);
    1038   [ +  -  -  +  :      204771 :     if (!batch.TxnCommit()) throw std::runtime_error(strprintf("Error during descriptors keypool top up. Cannot commit changes for wallet [%s]", m_storage.LogName()));
          -  -  -  -  -  
                      - ]
    1039                 :             :     return res;
    1040                 :      204771 : }
    1041                 :             : 
    1042                 :      227296 : bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int size)
    1043                 :             : {
    1044                 :      227296 :     LOCK(cs_desc_man);
    1045         [ +  - ]:      227296 :     std::set<CScript> new_spks;
    1046                 :      227296 :     unsigned int target_size;
    1047         [ +  - ]:      227296 :     if (size > 0) {
    1048                 :             :         target_size = size;
    1049                 :             :     } else {
    1050                 :      227296 :         target_size = m_keypool_size;
    1051                 :             :     }
    1052                 :             : 
    1053                 :             :     // Calculate the new range_end
    1054         [ +  - ]:      227296 :     int32_t new_range_end = std::max(m_wallet_descriptor.next_index + (int32_t)target_size, m_wallet_descriptor.range_end);
    1055                 :             : 
    1056                 :             :     // If the descriptor is not ranged, we actually just want to fill the first cache item
    1057   [ +  -  +  + ]:      227296 :     if (!m_wallet_descriptor.descriptor->IsRange()) {
    1058                 :       11846 :         new_range_end = 1;
    1059                 :       11846 :         m_wallet_descriptor.range_end = 1;
    1060                 :       11846 :         m_wallet_descriptor.range_start = 0;
    1061                 :             :     }
    1062                 :             : 
    1063                 :      227296 :     FlatSigningProvider provider;
    1064         [ +  - ]:      454592 :     provider.keys = GetKeys();
    1065                 :             : 
    1066         [ +  - ]:      227296 :     uint256 id = GetID();
    1067         [ +  + ]:      448254 :     for (int32_t i = m_max_cached_index + 1; i < new_range_end; ++i) {
    1068                 :      220958 :         FlatSigningProvider out_keys;
    1069                 :      220958 :         std::vector<CScript> scripts_temp;
    1070                 :      220958 :         DescriptorCache temp_cache;
    1071                 :             :         // Maybe we have a cached xpub and we can expand from the cache first
    1072   [ +  -  +  + ]:      220958 :         if (!m_wallet_descriptor.descriptor->ExpandFromCache(i, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
    1073   [ +  -  -  + ]:       21750 :             if (!m_wallet_descriptor.descriptor->Expand(i, provider, scripts_temp, out_keys, &temp_cache)) return false;
    1074                 :             :         }
    1075                 :             :         // Add all of the scriptPubKeys to the scriptPubKey set
    1076         [ +  - ]:      220958 :         new_spks.insert(scripts_temp.begin(), scripts_temp.end());
    1077         [ +  + ]:      449788 :         for (const CScript& script : scripts_temp) {
    1078         [ +  - ]:      228830 :             m_map_script_pub_keys[script] = i;
    1079                 :             :         }
    1080         [ +  + ]:      546181 :         for (const auto& pk_pair : out_keys.pubkeys) {
    1081                 :      325223 :             const CPubKey& pubkey = pk_pair.second;
    1082         [ +  + ]:      325223 :             if (m_map_pubkeys.contains(pubkey)) {
    1083                 :             :                 // We don't need to give an error here.
    1084                 :             :                 // It doesn't matter which of many valid indexes the pubkey has, we just need an index where we can derive it and its private key
    1085                 :       81309 :                 continue;
    1086                 :             :             }
    1087         [ +  - ]:      243914 :             m_map_pubkeys[pubkey] = i;
    1088                 :             :         }
    1089                 :             :         // Merge and write the cache
    1090         [ +  - ]:      220958 :         DescriptorCache new_items = m_wallet_descriptor.cache.MergeAndDiff(temp_cache);
    1091   [ +  -  -  + ]:      220958 :         if (!batch.WriteDescriptorCacheItems(id, new_items)) {
    1092   [ #  #  #  # ]:           0 :             throw std::runtime_error(std::string(__func__) + ": writing cache items failed");
    1093                 :             :         }
    1094                 :      220958 :         m_max_cached_index++;
    1095                 :      220958 :     }
    1096                 :      227296 :     m_wallet_descriptor.range_end = new_range_end;
    1097   [ +  -  +  - ]:      227296 :     batch.WriteDescriptor(GetID(), m_wallet_descriptor);
    1098                 :             : 
    1099                 :             :     // By this point, the cache size should be the size of the entire range
    1100         [ -  + ]:      227296 :     assert(m_wallet_descriptor.range_end - 1 == m_max_cached_index);
    1101                 :             : 
    1102         [ +  - ]:      227296 :     m_storage.TopUpCallback(new_spks, this);
    1103         [ +  - ]:      227296 :     NotifyCanGetAddressesChanged();
    1104                 :             :     return true;
    1105         [ +  - ]:      454592 : }
    1106                 :             : 
    1107                 :        2899 : std::vector<WalletDestination> DescriptorScriptPubKeyMan::MarkUnusedAddresses(const CScript& script)
    1108                 :             : {
    1109                 :        2899 :     LOCK(cs_desc_man);
    1110                 :        2899 :     std::vector<WalletDestination> result;
    1111   [ +  -  +  - ]:        2899 :     if (IsMine(script)) {
    1112         [ +  - ]:        2899 :         int32_t index = m_map_script_pub_keys[script];
    1113         [ +  + ]:        2899 :         if (index >= m_wallet_descriptor.next_index) {
    1114         [ +  - ]:         707 :             WalletLogPrintf("%s: Detected a used keypool item at index %d, mark all keypool items up to this item as used\n", __func__, index);
    1115         [ +  - ]:         707 :             auto out_keys = std::make_unique<FlatSigningProvider>();
    1116                 :         707 :             std::vector<CScript> scripts_temp;
    1117         [ +  + ]:        1414 :             while (index >= m_wallet_descriptor.next_index) {
    1118   [ +  -  -  + ]:         707 :                 if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.next_index, m_wallet_descriptor.cache, scripts_temp, *out_keys)) {
    1119   [ #  #  #  # ]:           0 :                     throw std::runtime_error(std::string(__func__) + ": Unable to expand descriptor from cache");
    1120                 :             :                 }
    1121                 :         707 :                 CTxDestination dest;
    1122         [ +  - ]:         707 :                 ExtractDestination(scripts_temp[0], dest);
    1123                 :         707 :                 result.push_back({dest, std::nullopt});
    1124                 :         707 :                 m_wallet_descriptor.next_index++;
    1125                 :         707 :             }
    1126         [ +  - ]:        1414 :         }
    1127   [ +  -  -  + ]:        2899 :         if (!TopUp()) {
    1128         [ #  # ]:           0 :             WalletLogPrintf("%s: Topping up keypool failed (locked wallet)\n", __func__);
    1129                 :             :         }
    1130                 :             :     }
    1131                 :             : 
    1132         [ +  - ]:        2899 :     return result;
    1133   [ +  -  +  - ]:        4313 : }
    1134                 :             : 
    1135                 :           0 : void DescriptorScriptPubKeyMan::AddDescriptorKey(const CKey& key, const CPubKey &pubkey)
    1136                 :             : {
    1137                 :           0 :     LOCK(cs_desc_man);
    1138   [ #  #  #  # ]:           0 :     WalletBatch batch(m_storage.GetDatabase());
    1139   [ #  #  #  # ]:           0 :     if (!AddDescriptorKeyWithDB(batch, key, pubkey)) {
    1140   [ #  #  #  # ]:           0 :         throw std::runtime_error(std::string(__func__) + ": writing descriptor private key failed");
    1141                 :             :     }
    1142         [ #  # ]:           0 : }
    1143                 :             : 
    1144                 :       24704 : bool DescriptorScriptPubKeyMan::AddDescriptorKeyWithDB(WalletBatch& batch, const CKey& key, const CPubKey &pubkey)
    1145                 :             : {
    1146                 :       24704 :     AssertLockHeld(cs_desc_man);
    1147         [ -  + ]:       24704 :     assert(!m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
    1148                 :             : 
    1149                 :             :     // Check if provided key already exists
    1150   [ +  +  -  + ]:       49301 :     if (m_map_keys.contains(pubkey.GetID()) ||
    1151                 :       24597 :         m_map_crypted_keys.contains(pubkey.GetID())) {
    1152                 :         107 :         return true;
    1153                 :             :     }
    1154                 :             : 
    1155         [ -  + ]:       24597 :     if (m_storage.HasEncryptionKeys()) {
    1156         [ #  # ]:           0 :         if (m_storage.IsLocked()) {
    1157                 :             :             return false;
    1158                 :             :         }
    1159                 :             : 
    1160                 :           0 :         std::vector<unsigned char> crypted_secret;
    1161   [ #  #  #  #  :           0 :         CKeyingMaterial secret{UCharCast(key.begin()), UCharCast(key.end())};
                   #  # ]
    1162   [ #  #  #  #  :           0 :         if (!m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
                   #  # ]
    1163                 :           0 :                 return EncryptSecret(encryption_key, secret, pubkey.GetHash(), crypted_secret);
    1164                 :             :             })) {
    1165                 :             :             return false;
    1166                 :             :         }
    1167                 :             : 
    1168   [ #  #  #  #  :           0 :         m_map_crypted_keys[pubkey.GetID()] = make_pair(pubkey, crypted_secret);
                   #  # ]
    1169   [ #  #  #  # ]:           0 :         return batch.WriteCryptedDescriptorKey(GetID(), pubkey, crypted_secret);
    1170                 :           0 :     } else {
    1171                 :       24597 :         m_map_keys[pubkey.GetID()] = key;
    1172   [ +  -  +  - ]:       24597 :         return batch.WriteDescriptorKey(GetID(), pubkey, key.GetPrivKey());
    1173                 :             :     }
    1174                 :             : }
    1175                 :             : 
    1176                 :           0 : void DescriptorScriptPubKeyMan::SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal)
    1177                 :             : {
    1178                 :           0 :     LOCK(cs_desc_man);
    1179   [ #  #  #  # ]:           0 :     Assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
    1180         [ #  # ]:           0 :     Assert(!m_wallet_descriptor.descriptor);
    1181                 :             : 
    1182   [ #  #  #  # ]:           0 :     m_wallet_descriptor = GenerateWalletDescriptor(master_key.Neuter(), addr_type, internal);
    1183                 :             : 
    1184                 :             :     // Store the master private key, and descriptor
    1185   [ #  #  #  #  :           0 :     if (!AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) {
                   #  # ]
    1186   [ #  #  #  # ]:           0 :         throw std::runtime_error(std::string(__func__) + ": writing descriptor master private key failed");
    1187                 :             :     }
    1188   [ #  #  #  #  :           0 :     if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
                   #  # ]
    1189   [ #  #  #  # ]:           0 :         throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
    1190                 :             :     }
    1191                 :             : 
    1192                 :             :     // Set m_decryption_thoroughly_checked for encrypted wallets
    1193   [ #  #  #  # ]:           0 :     if (m_storage.HasEncryptionKeys()) {
    1194                 :           0 :         m_decryption_thoroughly_checked = true;
    1195                 :             :     }
    1196                 :             : 
    1197                 :             :     // TopUp
    1198         [ #  # ]:           0 :     TopUpWithDB(batch);
    1199                 :             : 
    1200         [ #  # ]:           0 :     m_storage.UnsetBlankWalletFlag(batch);
    1201                 :           0 : }
    1202                 :             : 
    1203                 :       10859 : bool DescriptorScriptPubKeyMan::IsHDEnabled() const
    1204                 :             : {
    1205                 :       10859 :     LOCK(cs_desc_man);
    1206   [ +  -  +  - ]:       10859 :     return m_wallet_descriptor.descriptor->IsRange();
    1207                 :       10859 : }
    1208                 :             : 
    1209                 :      202815 : bool DescriptorScriptPubKeyMan::CanGetAddresses(bool internal) const
    1210                 :             : {
    1211                 :             :     // We can only give out addresses from descriptors that are single type (not combo), ranged,
    1212                 :             :     // and either have cached keys or can generate more keys (ignoring encryption)
    1213                 :      202815 :     LOCK(cs_desc_man);
    1214   [ +  -  +  + ]:      405630 :     return m_wallet_descriptor.descriptor->IsSingleType() &&
    1215   [ +  -  +  -  :      404790 :            m_wallet_descriptor.descriptor->IsRange() &&
                   +  + ]
    1216   [ +  -  +  +  :      404893 :            (HavePrivateKeys() || m_wallet_descriptor.next_index < m_wallet_descriptor.range_end);
                   +  - ]
    1217                 :      202815 : }
    1218                 :             : 
    1219                 :      227368 : bool DescriptorScriptPubKeyMan::HavePrivateKeys() const
    1220                 :             : {
    1221                 :      227368 :     LOCK(cs_desc_man);
    1222   [ +  +  -  +  :      227368 :     return m_map_keys.size() > 0 || m_map_crypted_keys.size() > 0;
                   +  - ]
    1223                 :      227368 : }
    1224                 :             : 
    1225                 :           0 : bool DescriptorScriptPubKeyMan::HaveCryptedKeys() const
    1226                 :             : {
    1227                 :           0 :     LOCK(cs_desc_man);
    1228         [ #  # ]:           0 :     return !m_map_crypted_keys.empty();
    1229                 :           0 : }
    1230                 :             : 
    1231                 :        8037 : unsigned int DescriptorScriptPubKeyMan::GetKeyPoolSize() const
    1232                 :             : {
    1233                 :        8037 :     LOCK(cs_desc_man);
    1234         [ +  - ]:        8037 :     return m_wallet_descriptor.range_end - m_wallet_descriptor.next_index;
    1235                 :        8037 : }
    1236                 :             : 
    1237                 :       17727 : int64_t DescriptorScriptPubKeyMan::GetTimeFirstKey() const
    1238                 :             : {
    1239                 :       17727 :     LOCK(cs_desc_man);
    1240         [ +  - ]:       17727 :     return m_wallet_descriptor.creation_time;
    1241                 :       17727 : }
    1242                 :             : 
    1243                 :       49239 : std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvider(const CScript& script, bool include_private) const
    1244                 :             : {
    1245                 :       49239 :     LOCK(cs_desc_man);
    1246                 :             : 
    1247                 :             :     // Find the index of the script
    1248                 :       49239 :     auto it = m_map_script_pub_keys.find(script);
    1249         [ +  + ]:       49239 :     if (it == m_map_script_pub_keys.end()) {
    1250                 :       23884 :         return nullptr;
    1251                 :             :     }
    1252         [ +  - ]:       25355 :     int32_t index = it->second;
    1253                 :             : 
    1254         [ +  - ]:       25355 :     return GetSigningProvider(index, include_private);
    1255                 :       49239 : }
    1256                 :             : 
    1257                 :        1997 : std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvider(const CPubKey& pubkey) const
    1258                 :             : {
    1259                 :        1997 :     LOCK(cs_desc_man);
    1260                 :             : 
    1261                 :             :     // Find index of the pubkey
    1262                 :        1997 :     auto it = m_map_pubkeys.find(pubkey);
    1263         [ +  + ]:        1997 :     if (it == m_map_pubkeys.end()) {
    1264                 :        1959 :         return nullptr;
    1265                 :             :     }
    1266         [ +  - ]:          38 :     int32_t index = it->second;
    1267                 :             : 
    1268                 :             :     // Always try to get the signing provider with private keys. This function should only be called during signing anyways
    1269         [ +  - ]:          38 :     std::unique_ptr<FlatSigningProvider> out = GetSigningProvider(index, true);
    1270   [ +  -  +  -  :          38 :     if (!out->HaveKey(pubkey.GetID())) {
                   +  + ]
    1271                 :           2 :         return nullptr;
    1272                 :             :     }
    1273                 :          36 :     return out;
    1274                 :        2035 : }
    1275                 :             : 
    1276                 :       25393 : std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvider(int32_t index, bool include_private) const
    1277                 :             : {
    1278                 :       25393 :     AssertLockHeld(cs_desc_man);
    1279                 :             : 
    1280                 :       25393 :     std::unique_ptr<FlatSigningProvider> out_keys = std::make_unique<FlatSigningProvider>();
    1281                 :             : 
    1282                 :             :     // Fetch SigningProvider from cache to avoid re-deriving
    1283                 :       25393 :     auto it = m_map_signing_providers.find(index);
    1284         [ +  + ]:       25393 :     if (it != m_map_signing_providers.end()) {
    1285   [ +  -  +  - ]:       16222 :         out_keys->Merge(FlatSigningProvider{it->second});
    1286                 :             :     } else {
    1287                 :             :         // Get the scripts, keys, and key origins for this script
    1288                 :        9171 :         std::vector<CScript> scripts_temp;
    1289   [ +  -  -  + ]:        9171 :         if (!m_wallet_descriptor.descriptor->ExpandFromCache(index, m_wallet_descriptor.cache, scripts_temp, *out_keys)) return nullptr;
    1290                 :             : 
    1291                 :             :         // Cache SigningProvider so we don't need to re-derive if we need this SigningProvider again
    1292   [ +  -  +  - ]:        9171 :         m_map_signing_providers[index] = *out_keys;
    1293                 :        9171 :     }
    1294                 :             : 
    1295   [ +  -  +  +  :       25393 :     if (HavePrivateKeys() && include_private) {
                   +  + ]
    1296                 :        5029 :         FlatSigningProvider master_provider;
    1297         [ +  - ]:       10058 :         master_provider.keys = GetKeys();
    1298         [ +  - ]:        5029 :         m_wallet_descriptor.descriptor->ExpandPrivate(index, master_provider, *out_keys);
    1299                 :             : 
    1300                 :             :         // Always include musig_secnonces as this descriptor may have a participant private key
    1301                 :             :         // but not a musig() descriptor
    1302                 :        5029 :         out_keys->musig2_secnonces = &m_musig2_secnonces;
    1303                 :        5029 :     }
    1304                 :             : 
    1305                 :       25393 :     return out_keys;
    1306                 :       25393 : }
    1307                 :             : 
    1308                 :        3639 : std::unique_ptr<SigningProvider> DescriptorScriptPubKeyMan::GetSolvingProvider(const CScript& script) const
    1309                 :             : {
    1310         [ -  + ]:        3639 :     return GetSigningProvider(script, false);
    1311                 :             : }
    1312                 :             : 
    1313                 :         686 : bool DescriptorScriptPubKeyMan::CanProvide(const CScript& script, SignatureData& sigdata)
    1314                 :             : {
    1315                 :         686 :     return IsMine(script);
    1316                 :             : }
    1317                 :             : 
    1318                 :        4140 : bool DescriptorScriptPubKeyMan::SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors) const
    1319                 :             : {
    1320                 :        4140 :     std::unique_ptr<FlatSigningProvider> keys = std::make_unique<FlatSigningProvider>();
    1321         [ +  + ]:       10087 :     for (const auto& coin_pair : coins) {
    1322         [ +  - ]:        5947 :         std::unique_ptr<FlatSigningProvider> coin_keys = GetSigningProvider(coin_pair.second.out.scriptPubKey, true);
    1323         [ +  + ]:        5947 :         if (!coin_keys) {
    1324                 :        5423 :             continue;
    1325                 :             :         }
    1326         [ +  - ]:         524 :         keys->Merge(std::move(*coin_keys));
    1327                 :        5947 :     }
    1328                 :             : 
    1329   [ +  -  +  - ]:        4140 :     return ::SignTransaction(tx, keys.get(), coins, {.sighash_type = sighash}, input_errors);
    1330                 :        4140 : }
    1331                 :             : 
    1332                 :       19404 : SigningResult DescriptorScriptPubKeyMan::SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const
    1333                 :             : {
    1334   [ +  -  +  - ]:       38808 :     std::unique_ptr<FlatSigningProvider> keys = GetSigningProvider(GetScriptForDestination(pkhash), true);
    1335         [ +  + ]:       19404 :     if (!keys) {
    1336                 :             :         return SigningResult::PRIVATE_KEY_NOT_AVAILABLE;
    1337                 :             :     }
    1338                 :             : 
    1339                 :        4741 :     CKey key;
    1340   [ +  -  +  -  :        4741 :     if (!keys->GetKey(ToKeyID(pkhash), key)) {
                   +  + ]
    1341                 :             :         return SigningResult::PRIVATE_KEY_NOT_AVAILABLE;
    1342                 :             :     }
    1343                 :             : 
    1344   [ +  -  -  + ]:        4469 :     if (!MessageSign(key, message, str_sig)) {
    1345                 :           0 :         return SigningResult::SIGNING_FAILED;
    1346                 :             :     }
    1347                 :             :     return SigningResult::OK;
    1348                 :       24145 : }
    1349                 :             : 
    1350                 :        5219 : std::optional<PSBTError> DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbtx, const PrecomputedTransactionData& txdata, const common::PSBTFillOptions& options, int* n_signed) const
    1351                 :             : {
    1352         [ -  + ]:        5219 :     if (n_signed) {
    1353                 :           0 :         *n_signed = 0;
    1354                 :             :     }
    1355   [ -  +  +  + ]:        7875 :     for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
    1356                 :        2749 :         PSBTInput& input = psbtx.inputs.at(i);
    1357                 :             : 
    1358         [ +  + ]:        2749 :         if (PSBTInputSigned(input)) {
    1359                 :         381 :             continue;
    1360                 :             :         }
    1361                 :             : 
    1362                 :             :         // Get the scriptPubKey to know which SigningProvider to use
    1363                 :        2368 :         CScript script;
    1364         [ +  + ]:        2368 :         if (!input.witness_utxo.IsNull()) {
    1365                 :         845 :             script = input.witness_utxo.scriptPubKey;
    1366         [ -  + ]:        1523 :         } else if (input.non_witness_utxo) {
    1367   [ #  #  #  # ]:           0 :             if (input.prev_out >= input.non_witness_utxo->vout.size()) {
    1368                 :           0 :                 return PSBTError::MISSING_INPUTS;
    1369                 :             :             }
    1370                 :           0 :             script = input.non_witness_utxo->vout[input.prev_out].scriptPubKey;
    1371                 :             :         } else {
    1372                 :             :             // There's no UTXO so we can just skip this now
    1373                 :        1523 :             continue;
    1374                 :             :         }
    1375                 :             : 
    1376         [ +  - ]:         845 :         std::unique_ptr<FlatSigningProvider> keys = std::make_unique<FlatSigningProvider>();
    1377         [ +  - ]:         845 :         std::unique_ptr<FlatSigningProvider> script_keys = GetSigningProvider(script, /*include_private=*/options.sign);
    1378         [ -  + ]:         845 :         if (script_keys) {
    1379         [ #  # ]:           0 :             keys->Merge(std::move(*script_keys));
    1380                 :             :         } else {
    1381                 :             :             // Maybe there are pubkeys listed that we can sign for
    1382                 :         845 :             std::vector<CPubKey> pubkeys;
    1383         [ +  - ]:         845 :             pubkeys.reserve(input.hd_keypaths.size() + 2);
    1384                 :             : 
    1385                 :             :             // ECDSA Pubkeys
    1386   [ +  -  +  + ]:        1062 :             for (const auto& [pk, _] : input.hd_keypaths) {
    1387         [ +  - ]:         217 :                 pubkeys.push_back(pk);
    1388                 :             :             }
    1389                 :             : 
    1390                 :             :             // Taproot output pubkey
    1391                 :         845 :             std::vector<std::vector<unsigned char>> sols;
    1392   [ +  -  -  + ]:         845 :             if (Solver(script, sols) == TxoutType::WITNESS_V1_TAPROOT) {
    1393         [ #  # ]:           0 :                 sols[0].insert(sols[0].begin(), 0x02);
    1394         [ #  # ]:           0 :                 pubkeys.emplace_back(sols[0]);
    1395         [ #  # ]:           0 :                 sols[0][0] = 0x03;
    1396         [ #  # ]:           0 :                 pubkeys.emplace_back(sols[0]);
    1397                 :             :             }
    1398                 :             : 
    1399                 :             :             // Taproot pubkeys
    1400         [ +  + ]:        1735 :             for (const auto& pk_pair : input.m_tap_bip32_paths) {
    1401                 :         890 :                 const XOnlyPubKey& pubkey = pk_pair.first;
    1402         [ +  + ]:        2670 :                 for (unsigned char prefix : {0x02, 0x03}) {
    1403                 :        1780 :                     unsigned char b[33] = {prefix};
    1404                 :        1780 :                     std::copy(pubkey.begin(), pubkey.end(), b + 1);
    1405                 :        1780 :                     CPubKey fullpubkey;
    1406                 :        1780 :                     fullpubkey.Set(b, b + 33);
    1407         [ +  - ]:        1780 :                     pubkeys.push_back(fullpubkey);
    1408                 :             :                 }
    1409                 :             :             }
    1410                 :             : 
    1411         [ +  + ]:        2842 :             for (const auto& pubkey : pubkeys) {
    1412         [ +  - ]:        1997 :                 std::unique_ptr<FlatSigningProvider> pk_keys = GetSigningProvider(pubkey);
    1413         [ +  + ]:        1997 :                 if (pk_keys) {
    1414         [ +  - ]:          36 :                     keys->Merge(std::move(*pk_keys));
    1415                 :             :                 }
    1416                 :        1997 :             }
    1417                 :         845 :         }
    1418                 :             : 
    1419   [ +  -  +  + ]:         845 :         PSBTError res = SignPSBTInput(HidingSigningProvider(keys.get(), /*hide_secret=*/!options.sign, /*hide_origin=*/!options.bip32_derivs), psbtx, i, &txdata, options, /*out_sigdata=*/nullptr);
    1420         [ +  + ]:         845 :         if (res != PSBTError::OK && res != PSBTError::INCOMPLETE) {
    1421         [ -  + ]:          93 :             return res;
    1422                 :             :         }
    1423                 :             : 
    1424         [ +  - ]:         752 :         bool signed_one = PSBTInputSigned(input);
    1425   [ -  +  -  -  :         752 :         if (n_signed && (signed_one || !options.sign)) {
                   -  - ]
    1426                 :             :             // If sign is false, we assume that we _could_ sign if we get here. This
    1427                 :             :             // will never have false negatives; it is hard to tell under what i
    1428                 :             :             // circumstances it could have false positives.
    1429                 :           0 :             (*n_signed)++;
    1430                 :             :         }
    1431   [ +  -  +  - ]:        3306 :     }
    1432                 :             : 
    1433                 :             :     // Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
    1434   [ -  +  +  + ]:        8079 :     for (unsigned int i = 0; i < psbtx.outputs.size(); ++i) {
    1435                 :        2953 :         std::unique_ptr<SigningProvider> keys = GetSolvingProvider(psbtx.outputs.at(i).script);
    1436         [ +  - ]:        2953 :         if (!keys) {
    1437                 :        2953 :             continue;
    1438                 :             :         }
    1439         [ #  # ]:           0 :         UpdatePSBTOutput(HidingSigningProvider(keys.get(), /*hide_secret=*/true, /*hide_origin=*/!options.bip32_derivs), psbtx, i);
    1440                 :        2953 :     }
    1441                 :             : 
    1442                 :        5126 :     return {};
    1443                 :             : }
    1444                 :             : 
    1445                 :       19404 : std::unique_ptr<CKeyMetadata> DescriptorScriptPubKeyMan::GetMetadata(const CTxDestination& dest) const
    1446                 :             : {
    1447         [ +  - ]:       19404 :     std::unique_ptr<SigningProvider> provider = GetSigningProvider(GetScriptForDestination(dest));
    1448         [ +  - ]:       19404 :     if (provider) {
    1449         [ +  - ]:       19404 :         KeyOriginInfo orig;
    1450         [ +  - ]:       19404 :         CKeyID key_id = GetKeyForDestination(*provider, dest);
    1451   [ +  -  +  + ]:       19404 :         if (provider->GetKeyOrigin(key_id, orig)) {
    1452         [ +  - ]:       11371 :             LOCK(cs_desc_man);
    1453         [ +  - ]:       11371 :             std::unique_ptr<CKeyMetadata> meta = std::make_unique<CKeyMetadata>();
    1454         [ +  - ]:       11371 :             meta->key_origin = orig;
    1455         [ +  - ]:       11371 :             meta->has_key_origin = true;
    1456                 :       11371 :             meta->nCreateTime = m_wallet_descriptor.creation_time;
    1457         [ +  - ]:       11371 :             return meta;
    1458                 :       11371 :         }
    1459                 :       19404 :     }
    1460                 :        8033 :     return nullptr;
    1461                 :       19404 : }
    1462                 :             : 
    1463                 :      726465 : uint256 DescriptorScriptPubKeyMan::GetID() const
    1464                 :             : {
    1465                 :      726465 :     LOCK(cs_desc_man);
    1466         [ +  - ]:      726465 :     return m_wallet_descriptor.id;
    1467                 :      726465 : }
    1468                 :             : 
    1469                 :           0 : void DescriptorScriptPubKeyMan::Load()
    1470                 :             : {
    1471                 :           0 :     LOCK(cs_desc_man);
    1472                 :           0 :     std::set<CScript> new_spks;
    1473         [ #  # ]:           0 :     for (int32_t i = m_wallet_descriptor.range_start; i < m_wallet_descriptor.range_end; ++i) {
    1474                 :           0 :         FlatSigningProvider out_keys;
    1475                 :           0 :         std::vector<CScript> scripts_temp;
    1476   [ #  #  #  # ]:           0 :         if (!m_wallet_descriptor.descriptor->ExpandFromCache(i, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
    1477         [ #  # ]:           0 :             throw std::runtime_error("Error: Unable to expand wallet descriptor from cache");
    1478                 :             :         }
    1479                 :             :         // Add all of the scriptPubKeys to the scriptPubKey set
    1480         [ #  # ]:           0 :         new_spks.insert(scripts_temp.begin(), scripts_temp.end());
    1481         [ #  # ]:           0 :         for (const CScript& script : scripts_temp) {
    1482         [ #  # ]:           0 :             if (m_map_script_pub_keys.contains(script)) {
    1483   [ #  #  #  #  :           0 :                 throw std::runtime_error(strprintf("Error: Already loaded script at index %d as being at index %d", i, m_map_script_pub_keys[script]));
                   #  # ]
    1484                 :             :             }
    1485         [ #  # ]:           0 :             m_map_script_pub_keys[script] = i;
    1486                 :             :         }
    1487         [ #  # ]:           0 :         for (const auto& pk_pair : out_keys.pubkeys) {
    1488                 :           0 :             const CPubKey& pubkey = pk_pair.second;
    1489         [ #  # ]:           0 :             if (m_map_pubkeys.contains(pubkey)) {
    1490                 :             :                 // We don't need to give an error here.
    1491                 :             :                 // It doesn't matter which of many valid indexes the pubkey has, we just need an index where we can derive it and its private key
    1492                 :           0 :                 continue;
    1493                 :             :             }
    1494         [ #  # ]:           0 :             m_map_pubkeys[pubkey] = i;
    1495                 :             :         }
    1496                 :           0 :         m_max_cached_index++;
    1497                 :           0 :     }
    1498                 :             :     // Make sure the wallet knows about our new spks
    1499         [ #  # ]:           0 :     m_storage.TopUpCallback(new_spks, this);
    1500         [ #  # ]:           0 : }
    1501                 :             : 
    1502                 :        1793 : bool DescriptorScriptPubKeyMan::HasWalletDescriptor(const WalletDescriptor& desc) const
    1503                 :             : {
    1504                 :        1793 :     LOCK(cs_desc_man);
    1505   [ +  -  +  -  :        6857 :     return !m_wallet_descriptor.id.IsNull() && !desc.id.IsNull() && m_wallet_descriptor.id == desc.id;
             +  +  +  - ]
    1506                 :        1793 : }
    1507                 :             : 
    1508                 :       17829 : void DescriptorScriptPubKeyMan::WriteDescriptor()
    1509                 :             : {
    1510                 :       17829 :     LOCK(cs_desc_man);
    1511   [ +  -  +  - ]:       17829 :     WalletBatch batch(m_storage.GetDatabase());
    1512   [ +  -  +  -  :       17829 :     if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
                   -  + ]
    1513   [ #  #  #  # ]:           0 :         throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
    1514                 :             :     }
    1515         [ +  - ]:       35658 : }
    1516                 :             : 
    1517                 :       14731 : WalletDescriptor DescriptorScriptPubKeyMan::GetWalletDescriptor() const
    1518                 :             : {
    1519                 :       14731 :     return m_wallet_descriptor;
    1520                 :             : }
    1521                 :             : 
    1522                 :       21865 : std::unordered_set<CScript, SaltedSipHasher> DescriptorScriptPubKeyMan::GetScriptPubKeys() const
    1523                 :             : {
    1524                 :       21865 :     return GetScriptPubKeys(0);
    1525                 :             : }
    1526                 :             : 
    1527                 :       21865 : std::unordered_set<CScript, SaltedSipHasher> DescriptorScriptPubKeyMan::GetScriptPubKeys(int32_t minimum_index) const
    1528                 :             : {
    1529                 :       21865 :     LOCK(cs_desc_man);
    1530         [ +  - ]:       21865 :     std::unordered_set<CScript, SaltedSipHasher> script_pub_keys;
    1531         [ +  - ]:       21865 :     script_pub_keys.reserve(m_map_script_pub_keys.size());
    1532                 :             : 
    1533   [ +  -  +  + ]:       77121 :     for (auto const& [script_pub_key, index] : m_map_script_pub_keys) {
    1534   [ +  -  +  - ]:       55256 :         if (index >= minimum_index) script_pub_keys.insert(script_pub_key);
    1535                 :             :     }
    1536         [ +  - ]:       21865 :     return script_pub_keys;
    1537                 :       21865 : }
    1538                 :             : 
    1539                 :        8037 : int32_t DescriptorScriptPubKeyMan::GetEndRange() const
    1540                 :             : {
    1541                 :        8037 :     return m_max_cached_index + 1;
    1542                 :             : }
    1543                 :             : 
    1544                 :        8037 : bool DescriptorScriptPubKeyMan::GetDescriptorString(std::string& out, const bool priv) const
    1545                 :             : {
    1546                 :        8037 :     LOCK(cs_desc_man);
    1547                 :             : 
    1548                 :        8037 :     FlatSigningProvider provider;
    1549         [ +  - ]:       16074 :     provider.keys = GetKeys();
    1550                 :             : 
    1551         [ +  + ]:        8037 :     if (priv) {
    1552                 :             :         // For the private version, always return the master key to avoid
    1553                 :             :         // exposing child private keys. The risk implications of exposing child
    1554                 :             :         // private keys together with the parent xpub may be non-obvious for users.
    1555         [ +  - ]:        1103 :         return m_wallet_descriptor.descriptor->ToPrivateString(provider, out);
    1556                 :             :     }
    1557                 :             : 
    1558         [ +  - ]:        6934 :     return m_wallet_descriptor.descriptor->ToNormalizedString(provider, out, &m_wallet_descriptor.cache);
    1559         [ +  - ]:       16074 : }
    1560                 :             : 
    1561                 :           0 : void DescriptorScriptPubKeyMan::UpgradeDescriptorCache()
    1562                 :             : {
    1563                 :           0 :     LOCK(cs_desc_man);
    1564   [ #  #  #  #  :           0 :     if (m_storage.IsLocked() || m_storage.IsWalletFlagSet(WALLET_FLAG_LAST_HARDENED_XPUB_CACHED)) {
             #  #  #  # ]
    1565                 :           0 :         return;
    1566                 :             :     }
    1567                 :             : 
    1568                 :             :     // Skip if we have the last hardened xpub cache
    1569   [ #  #  #  # ]:           0 :     if (m_wallet_descriptor.cache.GetCachedLastHardenedExtPubKeys().size() > 0) {
    1570                 :             :         return;
    1571                 :             :     }
    1572                 :             : 
    1573                 :             :     // Expand the descriptor
    1574                 :           0 :     FlatSigningProvider provider;
    1575         [ #  # ]:           0 :     provider.keys = GetKeys();
    1576                 :           0 :     FlatSigningProvider out_keys;
    1577                 :           0 :     std::vector<CScript> scripts_temp;
    1578                 :           0 :     DescriptorCache temp_cache;
    1579   [ #  #  #  # ]:           0 :     if (!m_wallet_descriptor.descriptor->Expand(0, provider, scripts_temp, out_keys, &temp_cache)){
    1580         [ #  # ]:           0 :         throw std::runtime_error("Unable to expand descriptor");
    1581                 :             :     }
    1582                 :             : 
    1583                 :             :     // Cache the last hardened xpubs
    1584         [ #  # ]:           0 :     DescriptorCache diff = m_wallet_descriptor.cache.MergeAndDiff(temp_cache);
    1585   [ #  #  #  #  :           0 :     if (!WalletBatch(m_storage.GetDatabase()).WriteDescriptorCacheItems(GetID(), diff)) {
          #  #  #  #  #  
                      # ]
    1586   [ #  #  #  # ]:           0 :         throw std::runtime_error(std::string(__func__) + ": writing cache items failed");
    1587                 :             :     }
    1588         [ #  # ]:           0 : }
    1589                 :             : 
    1590                 :         102 : util::Result<void> DescriptorScriptPubKeyMan::UpdateWalletDescriptor(WalletDescriptor& descriptor, const FlatSigningProvider& provider)
    1591                 :             : {
    1592                 :         102 :     LOCK(cs_desc_man);
    1593         [ +  - ]:         102 :     std::string error;
    1594   [ +  -  -  + ]:         102 :     if (!CanUpdateToWalletDescriptor(descriptor, error)) {
    1595         [ #  # ]:           0 :         return util::Error{Untranslated(std::move(error))};
    1596                 :             :     }
    1597                 :             : 
    1598                 :         102 :     m_map_pubkeys.clear();
    1599                 :         102 :     m_map_script_pub_keys.clear();
    1600                 :         102 :     m_max_cached_index = -1;
    1601         [ +  - ]:         102 :     m_wallet_descriptor = descriptor;
    1602                 :             : 
    1603   [ +  -  +  - ]:         102 :     WalletBatch batch(m_storage.GetDatabase());
    1604         [ +  - ]:         102 :     UpdateWithSigningProvider(batch, provider);
    1605         [ +  - ]:         102 :     NotifyFirstKeyTimeChanged(this, m_wallet_descriptor.creation_time);
    1606                 :         102 :     return {};
    1607         [ +  - ]:         306 : }
    1608                 :             : 
    1609                 :       22525 : void DescriptorScriptPubKeyMan::UpdateWithSigningProvider(WalletBatch& batch, const FlatSigningProvider& signing_provider)
    1610                 :             : {
    1611                 :       22525 :     AssertLockHeld(cs_desc_man);
    1612                 :             :     // Add the private keys to the descriptor
    1613         [ +  + ]:       47229 :     for (const auto& entry : signing_provider.keys) {
    1614                 :       24704 :         const CKey& key = entry.second;
    1615         [ -  + ]:       24704 :         if (!AddDescriptorKeyWithDB(batch, key, key.GetPubKey())) {
    1616   [ #  #  #  # ]:           0 :             throw std::runtime_error(std::string(__func__) + ": writing descriptor private key failed");
    1617                 :             :         }
    1618                 :             :     }
    1619                 :             : 
    1620                 :             :     // Top up key pool, to generate scriptPubKeys
    1621         [ -  + ]:       22525 :     if (!TopUpWithDB(batch)) {
    1622         [ #  # ]:           0 :         throw std::runtime_error("Could not top up scriptPubKeys");
    1623                 :             :     }
    1624                 :       22525 : }
    1625                 :             : 
    1626                 :        1691 : bool DescriptorScriptPubKeyMan::CanUpdateToWalletDescriptor(const WalletDescriptor& descriptor, std::string& error)
    1627                 :             : {
    1628                 :        1691 :     LOCK(cs_desc_man);
    1629   [ +  -  +  + ]:        1691 :     if (!HasWalletDescriptor(descriptor)) {
    1630   [ +  -  +  - ]:        1691 :         error = "can only update matching descriptor";
    1631                 :             :         return false;
    1632                 :             :     }
    1633                 :             : 
    1634   [ +  -  +  + ]:         213 :     if (!descriptor.descriptor->IsRange()) {
    1635                 :             :         // Skip range check for non-range descriptors
    1636                 :             :         return true;
    1637                 :             :     }
    1638                 :             : 
    1639         [ +  - ]:           9 :     if (descriptor.range_start > m_wallet_descriptor.range_start ||
    1640         [ +  - ]:           9 :         descriptor.range_end < m_wallet_descriptor.range_end) {
    1641                 :             :         // Use inclusive range for error
    1642                 :          18 :         error = strprintf("new range must include current range = [%d,%d]",
    1643                 :           9 :                           m_wallet_descriptor.range_start,
    1644         [ +  - ]:           9 :                           m_wallet_descriptor.range_end - 1);
    1645                 :           9 :         return false;
    1646                 :             :     }
    1647                 :             : 
    1648                 :             :     return true;
    1649                 :        1691 : }
    1650                 :             : } // namespace wallet
        

Generated by: LCOV version 2.0-1