LCOV - code coverage report
Current view: top level - src/wallet - scriptpubkeyman.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 93.5 % 923 863
Test Date: 2025-05-10 04:59:59 Functions: 97.6 % 85 83
Branches: 57.3 % 1448 829

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

Generated by: LCOV version 2.0-1