LCOV - code coverage report
Current view: top level - src/wallet - scriptpubkeyman.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 34.1 % 918 313
Test Date: 2025-06-01 05:27:21 Functions: 41.0 % 83 34
Branches: 17.1 % 1456 249

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

Generated by: LCOV version 2.0-1