scriptpubkeyman.cpp raw

   1  // Copyright (c) 2019-2022 The Limenka 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  //! Value for the first BIP 32 hardened derivation. Can be used as a bit mask and as a value. See BIP 32 for more details.
  29  const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;
  30  
  31  util::Result<CTxDestination> LegacyScriptPubKeyMan::GetNewDestination(const OutputType type)
  32  {
  33      if (LEGACY_OUTPUT_TYPES.count(type) == 0) {
  34          return util::Error{_("Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types")};
  35      }
  36      assert(type != OutputType::BECH32M);
  37  
  38      // Fill-up keypool if needed
  39      TopUp();
  40  
  41      LOCK(cs_KeyStore);
  42  
  43      // Generate a new key that is added to wallet
  44      CPubKey new_key;
  45      if (!GetKeyFromPool(new_key, type)) {
  46          return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
  47      }
  48      LearnRelatedScripts(new_key, type);
  49      return GetDestinationForKey(new_key, type);
  50  }
  51  
  52  typedef std::vector<unsigned char> valtype;
  53  
  54  namespace {
  55  
  56  /**
  57   * This is an enum that tracks the execution context of a script, similar to
  58   * SigVersion in script/interpreter. It is separate however because we want to
  59   * distinguish between top-level scriptPubKey execution and P2SH redeemScript
  60   * execution (a distinction that has no impact on consensus rules).
  61   */
  62  enum class IsMineSigVersion
  63  {
  64      TOP = 0,        //!< scriptPubKey execution
  65      P2SH = 1,       //!< P2SH redeemScript
  66      WITNESS_V0 = 2, //!< P2WSH witness script execution
  67  };
  68  
  69  /**
  70   * This is an internal representation of isminetype + invalidity.
  71   * Its order is significant, as we return the max of all explored
  72   * possibilities.
  73   */
  74  enum class IsMineResult
  75  {
  76      NO = 0,         //!< Not ours
  77      WATCH_ONLY = 1, //!< Included in watch-only balance
  78      SPENDABLE = 2,  //!< Included in all balances
  79      INVALID = 3,    //!< Not spendable by anyone (uncompressed pubkey in segwit, P2SH inside P2SH or witness, witness inside witness)
  80  };
  81  
  82  bool PermitsUncompressed(IsMineSigVersion sigversion)
  83  {
  84      return sigversion == IsMineSigVersion::TOP || sigversion == IsMineSigVersion::P2SH;
  85  }
  86  
  87  bool HaveKeys(const std::vector<valtype>& pubkeys, const LegacyDataSPKM& keystore)
  88  {
  89      for (const valtype& pubkey : pubkeys) {
  90          CKeyID keyID = CPubKey(pubkey).GetID();
  91          if (!keystore.HaveKey(keyID)) return false;
  92      }
  93      return true;
  94  }
  95  
  96  //! Recursively solve script and return spendable/watchonly/invalid status.
  97  //!
  98  //! @param keystore            legacy key and script store
  99  //! @param scriptPubKey        script to solve
 100  //! @param sigversion          script type (top-level / redeemscript / witnessscript)
 101  //! @param recurse_scripthash  whether to recurse into nested p2sh and p2wsh
 102  //!                            scripts or simply treat any script that has been
 103  //!                            stored in the keystore as spendable
 104  // NOLINTNEXTLINE(misc-no-recursion)
 105  IsMineResult IsMineInner(const LegacyDataSPKM& keystore, const CScript& scriptPubKey, IsMineSigVersion sigversion, bool recurse_scripthash=true)
 106  {
 107      IsMineResult ret = IsMineResult::NO;
 108  
 109      std::vector<valtype> vSolutions;
 110      TxoutType whichType = Solver(scriptPubKey, vSolutions);
 111  
 112      CKeyID keyID;
 113      switch (whichType) {
 114      case TxoutType::NONSTANDARD:
 115      case TxoutType::NULL_DATA:
 116      case TxoutType::WITNESS_UNKNOWN:
 117      case TxoutType::WITNESS_V1_TAPROOT:
 118      case TxoutType::WITNESS_V3_SPKHASH:
 119      case TxoutType::ANCHOR:
 120          break;
 121      case TxoutType::PUBKEY:
 122          keyID = CPubKey(vSolutions[0]).GetID();
 123          if (!PermitsUncompressed(sigversion) && vSolutions[0].size() != 33) {
 124              return IsMineResult::INVALID;
 125          }
 126          if (keystore.HaveKey(keyID)) {
 127              ret = std::max(ret, IsMineResult::SPENDABLE);
 128          }
 129          break;
 130      case TxoutType::WITNESS_V0_KEYHASH:
 131      {
 132          if (sigversion == IsMineSigVersion::WITNESS_V0) {
 133              // P2WPKH inside P2WSH is invalid.
 134              return IsMineResult::INVALID;
 135          }
 136          if (sigversion == IsMineSigVersion::TOP && !keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
 137              // We do not support bare witness outputs unless the P2SH version of it would be
 138              // acceptable as well. This protects against matching before segwit activates.
 139              // This also applies to the P2WSH case.
 140              break;
 141          }
 142          ret = std::max(ret, IsMineInner(keystore, GetScriptForDestination(PKHash(uint160(vSolutions[0]))), IsMineSigVersion::WITNESS_V0));
 143          break;
 144      }
 145      case TxoutType::PUBKEYHASH:
 146          keyID = CKeyID(uint160(vSolutions[0]));
 147          if (!PermitsUncompressed(sigversion)) {
 148              CPubKey pubkey;
 149              if (keystore.GetPubKey(keyID, pubkey) && !pubkey.IsCompressed()) {
 150                  return IsMineResult::INVALID;
 151              }
 152          }
 153          if (keystore.HaveKey(keyID)) {
 154              ret = std::max(ret, IsMineResult::SPENDABLE);
 155          }
 156          break;
 157      case TxoutType::SCRIPTHASH:
 158      {
 159          if (sigversion != IsMineSigVersion::TOP) {
 160              // P2SH inside P2WSH or P2SH is invalid.
 161              return IsMineResult::INVALID;
 162          }
 163          CScriptID scriptID = CScriptID(uint160(vSolutions[0]));
 164          CScript subscript;
 165          if (keystore.GetCScript(scriptID, subscript)) {
 166              ret = std::max(ret, recurse_scripthash ? IsMineInner(keystore, subscript, IsMineSigVersion::P2SH) : IsMineResult::SPENDABLE);
 167          }
 168          break;
 169      }
 170      case TxoutType::WITNESS_V0_SCRIPTHASH:
 171      {
 172          if (sigversion == IsMineSigVersion::WITNESS_V0) {
 173              // P2WSH inside P2WSH is invalid.
 174              return IsMineResult::INVALID;
 175          }
 176          if (sigversion == IsMineSigVersion::TOP && !keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
 177              break;
 178          }
 179          CScriptID scriptID{RIPEMD160(vSolutions[0])};
 180          CScript subscript;
 181          if (keystore.GetCScript(scriptID, subscript)) {
 182              ret = std::max(ret, recurse_scripthash ? IsMineInner(keystore, subscript, IsMineSigVersion::WITNESS_V0) : IsMineResult::SPENDABLE);
 183          }
 184          break;
 185      }
 186  
 187      case TxoutType::MULTISIG:
 188      {
 189          // Never treat bare multisig outputs as ours (they can still be made watchonly-though)
 190          if (sigversion == IsMineSigVersion::TOP) {
 191              break;
 192          }
 193  
 194          // Only consider transactions "mine" if we own ALL the
 195          // keys involved. Multi-signature transactions that are
 196          // partially owned (somebody else has a key that can spend
 197          // them) enable spend-out-from-under-you attacks, especially
 198          // in shared-wallet situations.
 199          std::vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
 200          if (!PermitsUncompressed(sigversion)) {
 201              for (size_t i = 0; i < keys.size(); i++) {
 202                  if (keys[i].size() != 33) {
 203                      return IsMineResult::INVALID;
 204                  }
 205              }
 206          }
 207          if (HaveKeys(keys, keystore)) {
 208              ret = std::max(ret, IsMineResult::SPENDABLE);
 209          }
 210          break;
 211      }
 212      } // no default case, so the compiler can warn about missing cases
 213  
 214      if (ret == IsMineResult::NO && keystore.HaveWatchOnly(scriptPubKey)) {
 215          ret = std::max(ret, IsMineResult::WATCH_ONLY);
 216      }
 217      return ret;
 218  }
 219  
 220  } // namespace
 221  
 222  SigningResult ScriptPubKeyMan::SignMessageBIP322(MessageSignatureFormat format, const SigningProvider* keystore, const std::string& message, const CTxDestination& address, std::string& str_sig) const
 223  {
 224      assert(format != MessageSignatureFormat::LEGACY);
 225  
 226      MessageVerificationResult result; // unused
 227      auto txs = BIP322Txs::Create(address, message, result);
 228      assert(txs);
 229  
 230      const CTransaction& to_spend = txs->m_to_spend;
 231      CMutableTransaction to_sign(txs->m_to_sign);
 232  
 233      // Create the "unspent output" map, consisting of the to_spend output
 234      std::map<COutPoint, Coin> coins;
 235      coins[to_sign.vin[0].prevout] = Coin(to_spend.vout[0], 1, false);
 236  
 237      // Sign the transaction
 238      std::map<int, bilingual_str> errors;
 239      if (!::SignTransaction(to_sign, keystore, coins, SIGHASH_ALL, errors)) {
 240          // TODO: this may be a multisig which successfully signed but needed additional signatures
 241          return SigningResult::SIGNING_FAILED;
 242      }
 243  
 244      // We force the format to FULL, if this turned out to be a legacy format (p2pkh) signature
 245      if (to_sign.vin[0].scriptSig.size() > 0 || to_sign.vin[0].scriptWitness.IsNull()) {
 246          format = MessageSignatureFormat::FULL;
 247      }
 248  
 249      DataStream ds;
 250      if (format == MessageSignatureFormat::SIMPLE) {
 251          // Simple format output
 252          ds << to_sign.vin[0].scriptWitness.stack;
 253      } else {
 254          // Full format output
 255          ds << TX_WITH_WITNESS(to_sign);
 256      }
 257  
 258      str_sig = EncodeBase64(ds);
 259  
 260      return SigningResult::OK;
 261  }
 262  
 263  isminetype LegacyDataSPKM::IsMine(const CScript& script) const
 264  {
 265      switch (IsMineInner(*this, script, IsMineSigVersion::TOP)) {
 266      case IsMineResult::INVALID:
 267      case IsMineResult::NO:
 268          return ISMINE_NO;
 269      case IsMineResult::WATCH_ONLY:
 270          return ISMINE_WATCH_ONLY;
 271      case IsMineResult::SPENDABLE:
 272          return ISMINE_SPENDABLE;
 273      }
 274      assert(false);
 275  }
 276  
 277  bool LegacyDataSPKM::CheckDecryptionKey(const CKeyingMaterial& master_key)
 278  {
 279      {
 280          LOCK(cs_KeyStore);
 281          assert(mapKeys.empty());
 282  
 283          bool keyPass = mapCryptedKeys.empty(); // Always pass when there are no encrypted keys
 284          bool keyFail = false;
 285          CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
 286          WalletBatch batch(m_storage.GetDatabase());
 287          for (; mi != mapCryptedKeys.end(); ++mi)
 288          {
 289              const CPubKey &vchPubKey = (*mi).second.first;
 290              const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
 291              CKey key;
 292              if (!DecryptKey(master_key, vchCryptedSecret, vchPubKey, key))
 293              {
 294                  keyFail = true;
 295                  break;
 296              }
 297              keyPass = true;
 298              if (fDecryptionThoroughlyChecked)
 299                  break;
 300              else {
 301                  // Rewrite these encrypted keys with checksums
 302                  batch.WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]);
 303              }
 304          }
 305          if (keyPass && keyFail)
 306          {
 307              LogWarning("The wallet is probably corrupted: Some keys decrypt but not all.");
 308              throw std::runtime_error("Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
 309          }
 310          if (keyFail || !keyPass)
 311              return false;
 312          fDecryptionThoroughlyChecked = true;
 313      }
 314      return true;
 315  }
 316  
 317  bool LegacyScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch)
 318  {
 319      LOCK(cs_KeyStore);
 320      encrypted_batch = batch;
 321      if (!mapCryptedKeys.empty()) {
 322          encrypted_batch = nullptr;
 323          return false;
 324      }
 325  
 326      KeyMap keys_to_encrypt;
 327      keys_to_encrypt.swap(mapKeys); // Clear mapKeys so AddCryptedKeyInner will succeed.
 328      for (const KeyMap::value_type& mKey : keys_to_encrypt)
 329      {
 330          const CKey &key = mKey.second;
 331          CPubKey vchPubKey = key.GetPubKey();
 332          CKeyingMaterial vchSecret{UCharCast(key.begin()), UCharCast(key.end())};
 333          std::vector<unsigned char> vchCryptedSecret;
 334          if (!EncryptSecret(master_key, vchSecret, vchPubKey.GetHash(), vchCryptedSecret)) {
 335              encrypted_batch = nullptr;
 336              return false;
 337          }
 338          if (!AddCryptedKey(vchPubKey, vchCryptedSecret)) {
 339              encrypted_batch = nullptr;
 340              return false;
 341          }
 342      }
 343      encrypted_batch = nullptr;
 344      return true;
 345  }
 346  
 347  util::Result<CTxDestination> LegacyScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, int64_t& index, CKeyPool& keypool)
 348  {
 349      if (LEGACY_OUTPUT_TYPES.count(type) == 0) {
 350          return util::Error{_("Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types")};
 351      }
 352      assert(type != OutputType::BECH32M);
 353  
 354      LOCK(cs_KeyStore);
 355      if (!CanGetAddresses(internal)) {
 356          return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
 357      }
 358  
 359      // Fill-up keypool if needed
 360      TopUp();
 361  
 362      if (!ReserveKeyFromKeyPool(index, keypool, internal)) {
 363          return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
 364      }
 365      return GetDestinationForKey(keypool.vchPubKey, type);
 366  }
 367  
 368  bool LegacyScriptPubKeyMan::TopUpInactiveHDChain(const CKeyID seed_id, int64_t index, bool internal)
 369  {
 370      LOCK(cs_KeyStore);
 371  
 372      auto it = m_inactive_hd_chains.find(seed_id);
 373      if (it == m_inactive_hd_chains.end()) {
 374          return false;
 375      }
 376  
 377      CHDChain& chain = it->second;
 378  
 379      if (internal) {
 380          chain.m_next_internal_index = std::max(chain.m_next_internal_index, index + 1);
 381      } else {
 382          chain.m_next_external_index = std::max(chain.m_next_external_index, index + 1);
 383      }
 384  
 385      WalletBatch batch(m_storage.GetDatabase());
 386      TopUpChain(batch, chain, 0);
 387  
 388      return true;
 389  }
 390  
 391  std::vector<WalletDestination> LegacyScriptPubKeyMan::MarkUnusedAddresses(const CScript& script)
 392  {
 393      LOCK(cs_KeyStore);
 394      std::vector<WalletDestination> result;
 395      // extract addresses and check if they match with an unused keypool key
 396      for (const auto& keyid : GetAffectedKeys(script, *this)) {
 397          std::map<CKeyID, int64_t>::const_iterator mi = m_pool_key_to_index.find(keyid);
 398          if (mi != m_pool_key_to_index.end()) {
 399              WalletLogPrintf("%s: Detected a used keypool key, mark all keypool keys up to this key as used\n", __func__);
 400              for (const auto& keypool : MarkReserveKeysAsUsed(mi->second)) {
 401                  // derive all possible destinations as any of them could have been used
 402                  for (const auto& type : LEGACY_OUTPUT_TYPES) {
 403                      const auto& dest = GetDestinationForKey(keypool.vchPubKey, type);
 404                      result.push_back({dest, keypool.fInternal});
 405                  }
 406              }
 407  
 408              if (!TopUp()) {
 409                  WalletLogPrintf("%s: Topping up keypool failed (locked wallet)\n", __func__);
 410              }
 411          }
 412  
 413          // Find the key's metadata and check if it's seed id (if it has one) is inactive, i.e. it is not the current m_hd_chain seed id.
 414          // If so, TopUp the inactive hd chain
 415          auto it = mapKeyMetadata.find(keyid);
 416          if (it != mapKeyMetadata.end()){
 417              CKeyMetadata meta = it->second;
 418              if (!meta.hd_seed_id.IsNull() && meta.hd_seed_id != m_hd_chain.seed_id) {
 419                  std::vector<uint32_t> path;
 420                  if (meta.has_key_origin) {
 421                      path = meta.key_origin.path;
 422                  } else if (!ParseHDKeypath(meta.hdKeypath, path)) {
 423                      WalletLogPrintf("%s: Adding inactive seed keys failed, invalid hdKeypath: %s\n",
 424                                      __func__,
 425                                      meta.hdKeypath);
 426                  }
 427                  if (path.size() != 3) {
 428                      WalletLogPrintf("%s: Adding inactive seed keys failed, invalid path size: %d, has_key_origin: %s\n",
 429                                      __func__,
 430                                      path.size(),
 431                                      meta.has_key_origin);
 432                  } else {
 433                      bool internal = (path[1] & ~BIP32_HARDENED_KEY_LIMIT) != 0;
 434                      int64_t index = path[2] & ~BIP32_HARDENED_KEY_LIMIT;
 435  
 436                      if (!TopUpInactiveHDChain(meta.hd_seed_id, index, internal)) {
 437                          WalletLogPrintf("%s: Adding inactive seed keys failed\n", __func__);
 438                      }
 439                  }
 440              }
 441          }
 442      }
 443  
 444      return result;
 445  }
 446  
 447  bool LegacyDataSPKM::IsKeyActive(const CScript& script) const
 448  {
 449      LOCK(cs_KeyStore);
 450  
 451      if (!IsMine(script)) return false; // Not in the keystore at all
 452  
 453      for (const auto& key_id : GetAffectedKeys(script, *this)) {
 454          const auto it = mapKeyMetadata.find(key_id);
 455          if (it == mapKeyMetadata.end()) return false; // This key must be really old
 456  
 457          if (!it->second.hd_seed_id.IsNull() && it->second.hd_seed_id == m_hd_chain.seed_id) return true;
 458      }
 459  
 460      // Imported or dumped for a new keypool
 461      return false;
 462  }
 463  
 464  void LegacyScriptPubKeyMan::UpgradeKeyMetadata()
 465  {
 466      LOCK(cs_KeyStore);
 467      if (m_storage.IsLocked() || m_storage.IsWalletFlagSet(WALLET_FLAG_KEY_ORIGIN_METADATA)) {
 468          return;
 469      }
 470  
 471      std::unique_ptr<WalletBatch> batch = std::make_unique<WalletBatch>(m_storage.GetDatabase());
 472      for (auto& meta_pair : mapKeyMetadata) {
 473          CKeyMetadata& meta = meta_pair.second;
 474          if (!meta.hd_seed_id.IsNull() && !meta.has_key_origin && meta.hdKeypath != "s") { // If the hdKeypath is "s", that's the seed and it doesn't have a key origin
 475              CKey key;
 476              GetKey(meta.hd_seed_id, key);
 477              CExtKey masterKey;
 478              masterKey.SetSeed(key);
 479              // Add to map
 480              CKeyID master_id = masterKey.key.GetPubKey().GetID();
 481              std::copy(master_id.begin(), master_id.begin() + 4, meta.key_origin.fingerprint);
 482              if (!ParseHDKeypath(meta.hdKeypath, meta.key_origin.path)) {
 483                  throw std::runtime_error("Invalid stored hdKeypath");
 484              }
 485              meta.has_key_origin = true;
 486              if (meta.nVersion < CKeyMetadata::VERSION_WITH_KEY_ORIGIN) {
 487                  meta.nVersion = CKeyMetadata::VERSION_WITH_KEY_ORIGIN;
 488              }
 489  
 490              // Write meta to wallet
 491              CPubKey pubkey;
 492              if (GetPubKey(meta_pair.first, pubkey)) {
 493                  batch->WriteKeyMetadata(meta, pubkey, true);
 494              }
 495          }
 496      }
 497  }
 498  
 499  bool LegacyScriptPubKeyMan::SetupGeneration(bool force)
 500  {
 501      if ((CanGenerateKeys() && !force) || m_storage.IsLocked()) {
 502          return false;
 503      }
 504  
 505      SetHDSeed(GenerateNewSeed());
 506      if (!NewKeyPool()) {
 507          return false;
 508      }
 509      return true;
 510  }
 511  
 512  bool LegacyScriptPubKeyMan::IsHDEnabled() const
 513  {
 514      return !m_hd_chain.seed_id.IsNull();
 515  }
 516  
 517  bool LegacyScriptPubKeyMan::CanGetAddresses(bool internal) const
 518  {
 519      LOCK(cs_KeyStore);
 520      // Check if the keypool has keys
 521      bool keypool_has_keys;
 522      if (internal && m_storage.CanSupportFeature(FEATURE_HD_SPLIT)) {
 523          keypool_has_keys = setInternalKeyPool.size() > 0;
 524      } else {
 525          keypool_has_keys = KeypoolCountExternalKeys() > 0;
 526      }
 527      // If the keypool doesn't have keys, check if we can generate them
 528      if (!keypool_has_keys) {
 529          return CanGenerateKeys();
 530      }
 531      return keypool_has_keys;
 532  }
 533  
 534  bool LegacyScriptPubKeyMan::Upgrade(int prev_version, int new_version, bilingual_str& error)
 535  {
 536      LOCK(cs_KeyStore);
 537  
 538      if (m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
 539          // Nothing to do here if private keys are not enabled
 540          return true;
 541      }
 542  
 543      bool hd_upgrade = false;
 544      bool split_upgrade = false;
 545      if (IsFeatureSupported(new_version, FEATURE_HD) && !IsHDEnabled()) {
 546          WalletLogPrintf("Upgrading wallet to HD\n");
 547          m_storage.SetMinVersion(FEATURE_HD);
 548  
 549          // generate a new master key
 550          CPubKey masterPubKey = GenerateNewSeed();
 551          SetHDSeed(masterPubKey);
 552          hd_upgrade = true;
 553      }
 554      // Upgrade to HD chain split if necessary
 555      if (!IsFeatureSupported(prev_version, FEATURE_HD_SPLIT) && IsFeatureSupported(new_version, FEATURE_HD_SPLIT)) {
 556          WalletLogPrintf("Upgrading wallet to use HD chain split\n");
 557          m_storage.SetMinVersion(FEATURE_PRE_SPLIT_KEYPOOL);
 558          split_upgrade = FEATURE_HD_SPLIT > prev_version;
 559          // Upgrade the HDChain
 560          if (m_hd_chain.nVersion < CHDChain::VERSION_HD_CHAIN_SPLIT) {
 561              m_hd_chain.nVersion = CHDChain::VERSION_HD_CHAIN_SPLIT;
 562              if (!WalletBatch(m_storage.GetDatabase()).WriteHDChain(m_hd_chain)) {
 563                  throw std::runtime_error(std::string(__func__) + ": writing chain failed");
 564              }
 565          }
 566      }
 567      // Mark all keys currently in the keypool as pre-split
 568      if (split_upgrade) {
 569          MarkPreSplitKeys();
 570      }
 571      // Regenerate the keypool if upgraded to HD
 572      if (hd_upgrade) {
 573          if (!NewKeyPool()) {
 574              error = _("Unable to generate keys");
 575              return false;
 576          }
 577      }
 578      return true;
 579  }
 580  
 581  bool LegacyScriptPubKeyMan::HavePrivateKeys() const
 582  {
 583      LOCK(cs_KeyStore);
 584      return !mapKeys.empty() || !mapCryptedKeys.empty();
 585  }
 586  
 587  bool LegacyScriptPubKeyMan::HaveCryptedKeys() const
 588  {
 589      LOCK(cs_KeyStore);
 590      return !mapCryptedKeys.empty();
 591  }
 592  
 593  void LegacyScriptPubKeyMan::RewriteDB()
 594  {
 595      LOCK(cs_KeyStore);
 596      setInternalKeyPool.clear();
 597      setExternalKeyPool.clear();
 598      m_pool_key_to_index.clear();
 599      // Note: can't top-up keypool here, because wallet is locked.
 600      // User will be prompted to unlock wallet the next operation
 601      // that requires a new key.
 602  }
 603  
 604  static int64_t GetOldestKeyTimeInPool(const std::set<int64_t>& setKeyPool, WalletBatch& batch) {
 605      if (setKeyPool.empty()) {
 606          return GetTime();
 607      }
 608  
 609      CKeyPool keypool;
 610      int64_t nIndex = *(setKeyPool.begin());
 611      if (!batch.ReadPool(nIndex, keypool)) {
 612          throw std::runtime_error(std::string(__func__) + ": read oldest key in keypool failed");
 613      }
 614      assert(keypool.vchPubKey.IsValid());
 615      return keypool.nTime;
 616  }
 617  
 618  std::optional<int64_t> LegacyScriptPubKeyMan::GetOldestKeyPoolTime() const
 619  {
 620      LOCK(cs_KeyStore);
 621  
 622      WalletBatch batch(m_storage.GetDatabase());
 623  
 624      // load oldest key from keypool, get time and return
 625      int64_t oldestKey = GetOldestKeyTimeInPool(setExternalKeyPool, batch);
 626      if (IsHDEnabled() && m_storage.CanSupportFeature(FEATURE_HD_SPLIT)) {
 627          oldestKey = std::max(GetOldestKeyTimeInPool(setInternalKeyPool, batch), oldestKey);
 628          if (!set_pre_split_keypool.empty()) {
 629              oldestKey = std::max(GetOldestKeyTimeInPool(set_pre_split_keypool, batch), oldestKey);
 630          }
 631      }
 632  
 633      return oldestKey;
 634  }
 635  
 636  size_t LegacyScriptPubKeyMan::KeypoolCountExternalKeys() const
 637  {
 638      LOCK(cs_KeyStore);
 639      return setExternalKeyPool.size() + set_pre_split_keypool.size();
 640  }
 641  
 642  unsigned int LegacyScriptPubKeyMan::GetKeyPoolSize() const
 643  {
 644      LOCK(cs_KeyStore);
 645      return setInternalKeyPool.size() + setExternalKeyPool.size() + set_pre_split_keypool.size();
 646  }
 647  
 648  int64_t LegacyScriptPubKeyMan::GetTimeFirstKey() const
 649  {
 650      LOCK(cs_KeyStore);
 651      return nTimeFirstKey;
 652  }
 653  
 654  std::unique_ptr<SigningProvider> LegacyDataSPKM::GetSolvingProvider(const CScript& script) const
 655  {
 656      return std::make_unique<LegacySigningProvider>(*this);
 657  }
 658  
 659  bool LegacyDataSPKM::CanProvide(const CScript& script, SignatureData& sigdata)
 660  {
 661      IsMineResult ismine = IsMineInner(*this, script, IsMineSigVersion::TOP, /* recurse_scripthash= */ false);
 662      if (ismine == IsMineResult::SPENDABLE || ismine == IsMineResult::WATCH_ONLY) {
 663          // If ismine, it means we recognize keys or script ids in the script, or
 664          // are watching the script itself, and we can at least provide metadata
 665          // or solving information, even if not able to sign fully.
 666          return true;
 667      } else {
 668          // If, given the stuff in sigdata, we could make a valid signature, then we can provide for this script
 669          ProduceSignature(*this, DUMMY_SIGNATURE_CREATOR, script, sigdata);
 670          if (!sigdata.signatures.empty()) {
 671              // If we could make signatures, make sure we have a private key to actually make a signature
 672              bool has_privkeys = false;
 673              for (const auto& key_sig_pair : sigdata.signatures) {
 674                  has_privkeys |= HaveKey(key_sig_pair.first);
 675              }
 676              return has_privkeys;
 677          }
 678          return false;
 679      }
 680  }
 681  
 682  bool LegacyScriptPubKeyMan::SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors, std::optional<CAmount>* inputs_amount_sum) const
 683  {
 684      return ::SignTransaction(tx, this, coins, sighash, input_errors, inputs_amount_sum);
 685  }
 686  
 687  SigningResult LegacyScriptPubKeyMan::SignMessage(const MessageSignatureFormat format, const std::string& message, const CTxDestination& address, std::string& str_sig) const
 688  {
 689      if (format != MessageSignatureFormat::LEGACY) {
 690          return SignMessageBIP322(format, this, message, address, str_sig);
 691      }
 692  
 693      const PKHash* pkhash = std::get_if<PKHash>(&address);
 694      if (!pkhash) {
 695          return SigningResult::PRIVATE_KEY_NOT_AVAILABLE;
 696      }
 697  
 698      CKey key;
 699      if (!GetKey(ToKeyID(*pkhash), key)) {
 700          return SigningResult::PRIVATE_KEY_NOT_AVAILABLE;
 701      }
 702  
 703      if (MessageSign(key, message, str_sig)) {
 704          return SigningResult::OK;
 705      }
 706  
 707      return SigningResult::SIGNING_FAILED;
 708  }
 709  
 710  std::optional<PSBTError> LegacyScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbtx, const PrecomputedTransactionData& txdata, int sighash_type, bool sign, bool bip32derivs, int* n_signed, bool finalize) const
 711  {
 712      if (n_signed) {
 713          *n_signed = 0;
 714      }
 715      for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
 716          const CTxIn& txin = psbtx.tx->vin[i];
 717          PSBTInput& input = psbtx.inputs.at(i);
 718  
 719          if (PSBTInputSigned(input)) {
 720              continue;
 721          }
 722  
 723          // Get the Sighash type
 724          if (sign && input.sighash_type != std::nullopt && *input.sighash_type != sighash_type) {
 725              return PSBTError::SIGHASH_MISMATCH;
 726          }
 727  
 728          // Check non_witness_utxo has specified prevout
 729          if (input.non_witness_utxo) {
 730              if (txin.prevout.n >= input.non_witness_utxo->vout.size()) {
 731                  return PSBTError::MISSING_INPUTS;
 732              }
 733          } else if (input.witness_utxo.IsNull()) {
 734              // There's no UTXO so we can just skip this now
 735              continue;
 736          }
 737          SignPSBTInput(HidingSigningProvider(this, !sign, !bip32derivs), psbtx, i, &txdata, sighash_type, nullptr, finalize);
 738  
 739          bool signed_one = PSBTInputSigned(input);
 740          if (n_signed && (signed_one || !sign)) {
 741              // If sign is false, we assume that we _could_ sign if we get here. This
 742              // will never have false negatives; it is hard to tell under what i
 743              // circumstances it could have false positives.
 744              (*n_signed)++;
 745          }
 746      }
 747  
 748      // Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
 749      for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) {
 750          UpdatePSBTOutput(HidingSigningProvider(this, true, !bip32derivs), psbtx, i);
 751      }
 752  
 753      return {};
 754  }
 755  
 756  std::unique_ptr<CKeyMetadata> LegacyScriptPubKeyMan::GetMetadata(const CTxDestination& dest) const
 757  {
 758      LOCK(cs_KeyStore);
 759  
 760      CKeyID key_id = GetKeyForDestination(*this, dest);
 761      if (!key_id.IsNull()) {
 762          auto it = mapKeyMetadata.find(key_id);
 763          if (it != mapKeyMetadata.end()) {
 764              return std::make_unique<CKeyMetadata>(it->second);
 765          }
 766      }
 767  
 768      CScript scriptPubKey = GetScriptForDestination(dest);
 769      auto it = m_script_metadata.find(CScriptID(scriptPubKey));
 770      if (it != m_script_metadata.end()) {
 771          return std::make_unique<CKeyMetadata>(it->second);
 772      }
 773  
 774      return nullptr;
 775  }
 776  
 777  uint256 LegacyScriptPubKeyMan::GetID() const
 778  {
 779      return uint256::ONE;
 780  }
 781  
 782  /**
 783   * Update wallet first key creation time. This should be called whenever keys
 784   * are added to the wallet, with the oldest key creation time.
 785   */
 786  void LegacyScriptPubKeyMan::UpdateTimeFirstKey(int64_t nCreateTime)
 787  {
 788      AssertLockHeld(cs_KeyStore);
 789      if (nCreateTime <= 1) {
 790          // Cannot determine birthday information, so set the wallet birthday to
 791          // the beginning of time.
 792          nTimeFirstKey = 1;
 793      } else if (nTimeFirstKey == UNKNOWN_TIME || nCreateTime < nTimeFirstKey) {
 794          nTimeFirstKey = nCreateTime;
 795      }
 796  
 797      NotifyFirstKeyTimeChanged(this, nTimeFirstKey);
 798  }
 799  
 800  bool LegacyDataSPKM::LoadKey(const CKey& key, const CPubKey &pubkey)
 801  {
 802      return AddKeyPubKeyInner(key, pubkey);
 803  }
 804  
 805  bool LegacyScriptPubKeyMan::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey)
 806  {
 807      LOCK(cs_KeyStore);
 808      WalletBatch batch(m_storage.GetDatabase());
 809      return LegacyScriptPubKeyMan::AddKeyPubKeyWithDB(batch, secret, pubkey);
 810  }
 811  
 812  bool LegacyScriptPubKeyMan::AddKeyPubKeyWithDB(WalletBatch& batch, const CKey& secret, const CPubKey& pubkey)
 813  {
 814      AssertLockHeld(cs_KeyStore);
 815  
 816      // Make sure we aren't adding private keys to private key disabled wallets
 817      assert(!m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
 818  
 819      // FillableSigningProvider has no concept of wallet databases, but calls AddCryptedKey
 820      // which is overridden below.  To avoid flushes, the database handle is
 821      // tunneled through to it.
 822      bool needsDB = !encrypted_batch;
 823      if (needsDB) {
 824          encrypted_batch = &batch;
 825      }
 826      if (!AddKeyPubKeyInner(secret, pubkey)) {
 827          if (needsDB) encrypted_batch = nullptr;
 828          return false;
 829      }
 830      if (needsDB) encrypted_batch = nullptr;
 831  
 832      // check if we need to remove from watch-only
 833      CScript script;
 834      script = GetScriptForDestination(PKHash(pubkey));
 835      if (HaveWatchOnly(script)) {
 836          RemoveWatchOnly(script);
 837      }
 838      script = GetScriptForRawPubKey(pubkey);
 839      if (HaveWatchOnly(script)) {
 840          RemoveWatchOnly(script);
 841      }
 842  
 843      m_storage.UnsetBlankWalletFlag(batch);
 844      if (!m_storage.HasEncryptionKeys()) {
 845          return batch.WriteKey(pubkey,
 846                                                   secret.GetPrivKey(),
 847                                                   mapKeyMetadata[pubkey.GetID()]);
 848      }
 849      return true;
 850  }
 851  
 852  bool LegacyDataSPKM::LoadCScript(const CScript& redeemScript)
 853  {
 854      /* A sanity check was added in pull #3843 to avoid adding redeemScripts
 855       * that never can be redeemed. However, old wallets may still contain
 856       * these. Do not add them to the wallet and warn. */
 857      if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
 858      {
 859          std::string strAddr = EncodeDestination(ScriptHash(redeemScript));
 860          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);
 861          return true;
 862      }
 863  
 864      return FillableSigningProvider::AddCScript(redeemScript);
 865  }
 866  
 867  void LegacyDataSPKM::LoadKeyMetadata(const CKeyID& keyID, const CKeyMetadata& meta)
 868  {
 869      LOCK(cs_KeyStore);
 870      mapKeyMetadata[keyID] = meta;
 871  }
 872  
 873  void LegacyScriptPubKeyMan::LoadKeyMetadata(const CKeyID& keyID, const CKeyMetadata& meta)
 874  {
 875      LOCK(cs_KeyStore);
 876      LegacyDataSPKM::LoadKeyMetadata(keyID, meta);
 877      UpdateTimeFirstKey(meta.nCreateTime);
 878  }
 879  
 880  void LegacyDataSPKM::LoadScriptMetadata(const CScriptID& script_id, const CKeyMetadata& meta)
 881  {
 882      LOCK(cs_KeyStore);
 883      m_script_metadata[script_id] = meta;
 884  }
 885  
 886  void LegacyScriptPubKeyMan::LoadScriptMetadata(const CScriptID& script_id, const CKeyMetadata& meta)
 887  {
 888      LOCK(cs_KeyStore);
 889      LegacyDataSPKM::LoadScriptMetadata(script_id, meta);
 890      UpdateTimeFirstKey(meta.nCreateTime);
 891  }
 892  
 893  bool LegacyDataSPKM::AddKeyPubKeyInner(const CKey& key, const CPubKey& pubkey)
 894  {
 895      LOCK(cs_KeyStore);
 896      return FillableSigningProvider::AddKeyPubKey(key, pubkey);
 897  }
 898  
 899  bool LegacyScriptPubKeyMan::AddKeyPubKeyInner(const CKey& key, const CPubKey &pubkey)
 900  {
 901      LOCK(cs_KeyStore);
 902      if (!m_storage.HasEncryptionKeys()) {
 903          return FillableSigningProvider::AddKeyPubKey(key, pubkey);
 904      }
 905  
 906      if (m_storage.IsLocked()) {
 907          return false;
 908      }
 909  
 910      std::vector<unsigned char> vchCryptedSecret;
 911      CKeyingMaterial vchSecret{UCharCast(key.begin()), UCharCast(key.end())};
 912      if (!m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
 913              return EncryptSecret(encryption_key, vchSecret, pubkey.GetHash(), vchCryptedSecret);
 914          })) {
 915          return false;
 916      }
 917  
 918      if (!AddCryptedKey(pubkey, vchCryptedSecret)) {
 919          return false;
 920      }
 921      return true;
 922  }
 923  
 924  bool LegacyDataSPKM::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret, bool checksum_valid)
 925  {
 926      // Set fDecryptionThoroughlyChecked to false when the checksum is invalid
 927      if (!checksum_valid) {
 928          fDecryptionThoroughlyChecked = false;
 929      }
 930  
 931      return AddCryptedKeyInner(vchPubKey, vchCryptedSecret);
 932  }
 933  
 934  bool LegacyDataSPKM::AddCryptedKeyInner(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
 935  {
 936      LOCK(cs_KeyStore);
 937      assert(mapKeys.empty());
 938  
 939      mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
 940      ImplicitlyLearnRelatedKeyScripts(vchPubKey);
 941      return true;
 942  }
 943  
 944  bool LegacyScriptPubKeyMan::AddCryptedKey(const CPubKey &vchPubKey,
 945                              const std::vector<unsigned char> &vchCryptedSecret)
 946  {
 947      if (!AddCryptedKeyInner(vchPubKey, vchCryptedSecret))
 948          return false;
 949      {
 950          LOCK(cs_KeyStore);
 951          if (encrypted_batch)
 952              return encrypted_batch->WriteCryptedKey(vchPubKey,
 953                                                          vchCryptedSecret,
 954                                                          mapKeyMetadata[vchPubKey.GetID()]);
 955          else
 956              return WalletBatch(m_storage.GetDatabase()).WriteCryptedKey(vchPubKey,
 957                                                              vchCryptedSecret,
 958                                                              mapKeyMetadata[vchPubKey.GetID()]);
 959      }
 960  }
 961  
 962  bool LegacyDataSPKM::HaveWatchOnly(const CScript &dest) const
 963  {
 964      LOCK(cs_KeyStore);
 965      return setWatchOnly.count(dest) > 0;
 966  }
 967  
 968  bool LegacyDataSPKM::HaveWatchOnly() const
 969  {
 970      LOCK(cs_KeyStore);
 971      return (!setWatchOnly.empty());
 972  }
 973  
 974  static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
 975  {
 976      std::vector<std::vector<unsigned char>> solutions;
 977      return Solver(dest, solutions) == TxoutType::PUBKEY &&
 978          (pubKeyOut = CPubKey(solutions[0])).IsFullyValid();
 979  }
 980  
 981  bool LegacyScriptPubKeyMan::RemoveWatchOnly(const CScript &dest)
 982  {
 983      {
 984          LOCK(cs_KeyStore);
 985          setWatchOnly.erase(dest);
 986          CPubKey pubKey;
 987          if (ExtractPubKey(dest, pubKey)) {
 988              mapWatchKeys.erase(pubKey.GetID());
 989          }
 990          // Related CScripts are not removed; having superfluous scripts around is
 991          // harmless (see comment in ImplicitlyLearnRelatedKeyScripts).
 992      }
 993  
 994      if (!HaveWatchOnly())
 995          NotifyWatchonlyChanged(false);
 996      if (!WalletBatch(m_storage.GetDatabase()).EraseWatchOnly(dest))
 997          return false;
 998  
 999      return true;
1000  }
1001  
1002  bool LegacyDataSPKM::LoadWatchOnly(const CScript &dest)
1003  {
1004      return AddWatchOnlyInMem(dest);
1005  }
1006  
1007  bool LegacyDataSPKM::AddWatchOnlyInMem(const CScript &dest)
1008  {
1009      LOCK(cs_KeyStore);
1010      setWatchOnly.insert(dest);
1011      CPubKey pubKey;
1012      if (ExtractPubKey(dest, pubKey)) {
1013          mapWatchKeys[pubKey.GetID()] = pubKey;
1014          ImplicitlyLearnRelatedKeyScripts(pubKey);
1015      }
1016      return true;
1017  }
1018  
1019  bool LegacyScriptPubKeyMan::AddWatchOnlyWithDB(WalletBatch &batch, const CScript& dest)
1020  {
1021      if (!AddWatchOnlyInMem(dest))
1022          return false;
1023      const CKeyMetadata& meta = m_script_metadata[CScriptID(dest)];
1024      UpdateTimeFirstKey(meta.nCreateTime);
1025      NotifyWatchonlyChanged(true);
1026      if (batch.WriteWatchOnly(dest, meta)) {
1027          m_storage.UnsetBlankWalletFlag(batch);
1028          return true;
1029      }
1030      return false;
1031  }
1032  
1033  bool LegacyScriptPubKeyMan::AddWatchOnlyWithDB(WalletBatch &batch, const CScript& dest, int64_t create_time)
1034  {
1035      m_script_metadata[CScriptID(dest)].nCreateTime = create_time;
1036      return AddWatchOnlyWithDB(batch, dest);
1037  }
1038  
1039  bool LegacyScriptPubKeyMan::AddWatchOnly(const CScript& dest)
1040  {
1041      WalletBatch batch(m_storage.GetDatabase());
1042      return AddWatchOnlyWithDB(batch, dest);
1043  }
1044  
1045  bool LegacyScriptPubKeyMan::AddWatchOnly(const CScript& dest, int64_t nCreateTime)
1046  {
1047      m_script_metadata[CScriptID(dest)].nCreateTime = nCreateTime;
1048      return AddWatchOnly(dest);
1049  }
1050  
1051  void LegacyDataSPKM::LoadHDChain(const CHDChain& chain)
1052  {
1053      LOCK(cs_KeyStore);
1054      m_hd_chain = chain;
1055  }
1056  
1057  void LegacyScriptPubKeyMan::AddHDChain(const CHDChain& chain)
1058  {
1059      LOCK(cs_KeyStore);
1060      // Store the new chain
1061      if (!WalletBatch(m_storage.GetDatabase()).WriteHDChain(chain)) {
1062          throw std::runtime_error(std::string(__func__) + ": writing chain failed");
1063      }
1064      // When there's an old chain, add it as an inactive chain as we are now rotating hd chains
1065      if (!m_hd_chain.seed_id.IsNull()) {
1066          AddInactiveHDChain(m_hd_chain);
1067      }
1068  
1069      m_hd_chain = chain;
1070  }
1071  
1072  void LegacyDataSPKM::AddInactiveHDChain(const CHDChain& chain)
1073  {
1074      LOCK(cs_KeyStore);
1075      assert(!chain.seed_id.IsNull());
1076      m_inactive_hd_chains[chain.seed_id] = chain;
1077  }
1078  
1079  bool LegacyDataSPKM::HaveKey(const CKeyID &address) const
1080  {
1081      LOCK(cs_KeyStore);
1082      if (!m_storage.HasEncryptionKeys()) {
1083          return FillableSigningProvider::HaveKey(address);
1084      }
1085      return mapCryptedKeys.count(address) > 0;
1086  }
1087  
1088  bool LegacyDataSPKM::GetKey(const CKeyID &address, CKey& keyOut) const
1089  {
1090      LOCK(cs_KeyStore);
1091      if (!m_storage.HasEncryptionKeys()) {
1092          return FillableSigningProvider::GetKey(address, keyOut);
1093      }
1094  
1095      CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
1096      if (mi != mapCryptedKeys.end())
1097      {
1098          const CPubKey &vchPubKey = (*mi).second.first;
1099          const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
1100          return m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
1101              return DecryptKey(encryption_key, vchCryptedSecret, vchPubKey, keyOut);
1102          });
1103      }
1104      return false;
1105  }
1106  
1107  bool LegacyDataSPKM::GetKeyOrigin(const CKeyID& keyID, KeyOriginInfo& info) const
1108  {
1109      CKeyMetadata meta;
1110      {
1111          LOCK(cs_KeyStore);
1112          auto it = mapKeyMetadata.find(keyID);
1113          if (it == mapKeyMetadata.end()) {
1114              return false;
1115          }
1116          meta = it->second;
1117      }
1118      if (meta.has_key_origin) {
1119          std::copy(meta.key_origin.fingerprint, meta.key_origin.fingerprint + 4, info.fingerprint);
1120          info.path = meta.key_origin.path;
1121      } else { // Single pubkeys get the master fingerprint of themselves
1122          std::copy(keyID.begin(), keyID.begin() + 4, info.fingerprint);
1123      }
1124      return true;
1125  }
1126  
1127  bool LegacyDataSPKM::GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const
1128  {
1129      LOCK(cs_KeyStore);
1130      WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
1131      if (it != mapWatchKeys.end()) {
1132          pubkey_out = it->second;
1133          return true;
1134      }
1135      return false;
1136  }
1137  
1138  bool LegacyDataSPKM::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
1139  {
1140      LOCK(cs_KeyStore);
1141      if (!m_storage.HasEncryptionKeys()) {
1142          if (!FillableSigningProvider::GetPubKey(address, vchPubKeyOut)) {
1143              return GetWatchPubKey(address, vchPubKeyOut);
1144          }
1145          return true;
1146      }
1147  
1148      CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
1149      if (mi != mapCryptedKeys.end())
1150      {
1151          vchPubKeyOut = (*mi).second.first;
1152          return true;
1153      }
1154      // Check for watch-only pubkeys
1155      return GetWatchPubKey(address, vchPubKeyOut);
1156  }
1157  
1158  CPubKey LegacyScriptPubKeyMan::GenerateNewKey(WalletBatch &batch, CHDChain& hd_chain, bool internal)
1159  {
1160      assert(!m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
1161      assert(!m_storage.IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET));
1162      AssertLockHeld(cs_KeyStore);
1163      bool fCompressed = m_storage.CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
1164  
1165      CKey secret;
1166  
1167      // Create new metadata
1168      int64_t nCreationTime = GetTime();
1169      CKeyMetadata metadata(nCreationTime);
1170  
1171      // use HD key derivation if HD was enabled during wallet creation and a seed is present
1172      if (IsHDEnabled()) {
1173          DeriveNewChildKey(batch, metadata, secret, hd_chain, (m_storage.CanSupportFeature(FEATURE_HD_SPLIT) ? internal : false));
1174      } else {
1175          secret.MakeNewKey(fCompressed);
1176      }
1177  
1178      // Compressed public keys were introduced in version 0.6.0
1179      if (fCompressed) {
1180          m_storage.SetMinVersion(FEATURE_COMPRPUBKEY);
1181      }
1182  
1183      CPubKey pubkey = secret.GetPubKey();
1184      assert(secret.VerifyPubKey(pubkey));
1185  
1186      mapKeyMetadata[pubkey.GetID()] = metadata;
1187      UpdateTimeFirstKey(nCreationTime);
1188  
1189      if (!AddKeyPubKeyWithDB(batch, secret, pubkey)) {
1190          throw std::runtime_error(std::string(__func__) + ": AddKey failed");
1191      }
1192      return pubkey;
1193  }
1194  
1195  //! Try to derive an extended key, throw if it fails.
1196  static void DeriveExtKey(CExtKey& key_in, unsigned int index, CExtKey& key_out) {
1197      if (!key_in.Derive(key_out, index)) {
1198          throw std::runtime_error("Could not derive extended key");
1199      }
1200  }
1201  
1202  void LegacyScriptPubKeyMan::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata& metadata, CKey& secret, CHDChain& hd_chain, bool internal)
1203  {
1204      // for now we use a fixed keypath scheme of m/0'/0'/k
1205      CKey seed;                     //seed (256bit)
1206      CExtKey masterKey;             //hd master key
1207      CExtKey accountKey;            //key at m/0'
1208      CExtKey chainChildKey;         //key at m/0'/0' (external) or m/0'/1' (internal)
1209      CExtKey childKey;              //key at m/0'/0'/<n>'
1210  
1211      // try to get the seed
1212      if (!GetKey(hd_chain.seed_id, seed))
1213          throw std::runtime_error(std::string(__func__) + ": seed not found");
1214  
1215      masterKey.SetSeed(seed);
1216  
1217      // derive m/0'
1218      // use hardened derivation (child keys >= 0x80000000 are hardened after bip32)
1219      DeriveExtKey(masterKey, BIP32_HARDENED_KEY_LIMIT, accountKey);
1220  
1221      // derive m/0'/0' (external chain) OR m/0'/1' (internal chain)
1222      assert(internal ? m_storage.CanSupportFeature(FEATURE_HD_SPLIT) : true);
1223      DeriveExtKey(accountKey, BIP32_HARDENED_KEY_LIMIT+(internal ? 1 : 0), chainChildKey);
1224  
1225      // derive child key at next index, skip keys already known to the wallet
1226      do {
1227          // always derive hardened keys
1228          // childIndex | BIP32_HARDENED_KEY_LIMIT = derive childIndex in hardened child-index-range
1229          // example: 1 | BIP32_HARDENED_KEY_LIMIT == 0x80000001 == 2147483649
1230          if (internal) {
1231              DeriveExtKey(chainChildKey, hd_chain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT, childKey);
1232              metadata.hdKeypath = "m/0'/1'/" + ToString(hd_chain.nInternalChainCounter) + "'";
1233              metadata.key_origin.path.push_back(0 | BIP32_HARDENED_KEY_LIMIT);
1234              metadata.key_origin.path.push_back(1 | BIP32_HARDENED_KEY_LIMIT);
1235              metadata.key_origin.path.push_back(hd_chain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
1236              hd_chain.nInternalChainCounter++;
1237          }
1238          else {
1239              DeriveExtKey(chainChildKey, hd_chain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT, childKey);
1240              metadata.hdKeypath = "m/0'/0'/" + ToString(hd_chain.nExternalChainCounter) + "'";
1241              metadata.key_origin.path.push_back(0 | BIP32_HARDENED_KEY_LIMIT);
1242              metadata.key_origin.path.push_back(0 | BIP32_HARDENED_KEY_LIMIT);
1243              metadata.key_origin.path.push_back(hd_chain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
1244              hd_chain.nExternalChainCounter++;
1245          }
1246      } while (HaveKey(childKey.key.GetPubKey().GetID()));
1247      secret = childKey.key;
1248      metadata.hd_seed_id = hd_chain.seed_id;
1249      CKeyID master_id = masterKey.key.GetPubKey().GetID();
1250      std::copy(master_id.begin(), master_id.begin() + 4, metadata.key_origin.fingerprint);
1251      metadata.has_key_origin = true;
1252      // update the chain model in the database
1253      if (hd_chain.seed_id == m_hd_chain.seed_id && !batch.WriteHDChain(hd_chain))
1254          throw std::runtime_error(std::string(__func__) + ": writing HD chain model failed");
1255  }
1256  
1257  void LegacyDataSPKM::LoadKeyPool(int64_t nIndex, const CKeyPool &keypool)
1258  {
1259      LOCK(cs_KeyStore);
1260      if (keypool.m_pre_split) {
1261          set_pre_split_keypool.insert(nIndex);
1262      } else if (keypool.fInternal) {
1263          setInternalKeyPool.insert(nIndex);
1264      } else {
1265          setExternalKeyPool.insert(nIndex);
1266      }
1267      m_max_keypool_index = std::max(m_max_keypool_index, nIndex);
1268      m_pool_key_to_index[keypool.vchPubKey.GetID()] = nIndex;
1269  
1270      // If no metadata exists yet, create a default with the pool key's
1271      // creation time. Note that this may be overwritten by actually
1272      // stored metadata for that key later, which is fine.
1273      CKeyID keyid = keypool.vchPubKey.GetID();
1274      if (mapKeyMetadata.count(keyid) == 0)
1275          mapKeyMetadata[keyid] = CKeyMetadata(keypool.nTime);
1276  }
1277  
1278  bool LegacyScriptPubKeyMan::CanGenerateKeys() const
1279  {
1280      // A wallet can generate keys if it has an HD seed (IsHDEnabled) or it is a non-HD wallet (pre FEATURE_HD)
1281      LOCK(cs_KeyStore);
1282      return IsHDEnabled() || !m_storage.CanSupportFeature(FEATURE_HD);
1283  }
1284  
1285  CPubKey LegacyScriptPubKeyMan::GenerateNewSeed()
1286  {
1287      assert(!m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
1288      CKey key = GenerateRandomKey();
1289      return DeriveNewSeed(key);
1290  }
1291  
1292  CPubKey LegacyScriptPubKeyMan::DeriveNewSeed(const CKey& key)
1293  {
1294      int64_t nCreationTime = GetTime();
1295      CKeyMetadata metadata(nCreationTime);
1296  
1297      // calculate the seed
1298      CPubKey seed = key.GetPubKey();
1299      assert(key.VerifyPubKey(seed));
1300  
1301      // set the hd keypath to "s" -> Seed, refers the seed to itself
1302      metadata.hdKeypath     = "s";
1303      metadata.has_key_origin = false;
1304      metadata.hd_seed_id = seed.GetID();
1305  
1306      {
1307          LOCK(cs_KeyStore);
1308  
1309          // mem store the metadata
1310          mapKeyMetadata[seed.GetID()] = metadata;
1311  
1312          // write the key&metadata to the database
1313          if (!AddKeyPubKey(key, seed))
1314              throw std::runtime_error(std::string(__func__) + ": AddKeyPubKey failed");
1315      }
1316  
1317      return seed;
1318  }
1319  
1320  void LegacyScriptPubKeyMan::SetHDSeed(const CPubKey& seed)
1321  {
1322      LOCK(cs_KeyStore);
1323      // store the keyid (hash160) together with
1324      // the child index counter in the database
1325      // as a hdchain object
1326      CHDChain newHdChain;
1327      newHdChain.nVersion = m_storage.CanSupportFeature(FEATURE_HD_SPLIT) ? CHDChain::VERSION_HD_CHAIN_SPLIT : CHDChain::VERSION_HD_BASE;
1328      newHdChain.seed_id = seed.GetID();
1329      AddHDChain(newHdChain);
1330      NotifyCanGetAddressesChanged();
1331      WalletBatch batch(m_storage.GetDatabase());
1332      m_storage.UnsetBlankWalletFlag(batch);
1333  }
1334  
1335  /**
1336   * Mark old keypool keys as used,
1337   * and generate all new keys
1338   */
1339  bool LegacyScriptPubKeyMan::NewKeyPool()
1340  {
1341      if (m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
1342          return false;
1343      }
1344      {
1345          LOCK(cs_KeyStore);
1346          WalletBatch batch(m_storage.GetDatabase());
1347  
1348          for (const int64_t nIndex : setInternalKeyPool) {
1349              batch.ErasePool(nIndex);
1350          }
1351          setInternalKeyPool.clear();
1352  
1353          for (const int64_t nIndex : setExternalKeyPool) {
1354              batch.ErasePool(nIndex);
1355          }
1356          setExternalKeyPool.clear();
1357  
1358          for (const int64_t nIndex : set_pre_split_keypool) {
1359              batch.ErasePool(nIndex);
1360          }
1361          set_pre_split_keypool.clear();
1362  
1363          m_pool_key_to_index.clear();
1364  
1365          if (!TopUp()) {
1366              return false;
1367          }
1368          WalletLogPrintf("LegacyScriptPubKeyMan::NewKeyPool rewrote keypool\n");
1369      }
1370      return true;
1371  }
1372  
1373  bool LegacyScriptPubKeyMan::TopUp(unsigned int kpSize)
1374  {
1375      if (!CanGenerateKeys()) {
1376          return false;
1377      }
1378  
1379      WalletBatch batch(m_storage.GetDatabase());
1380      if (!batch.TxnBegin()) return false;
1381      if (!TopUpChain(batch, m_hd_chain, kpSize)) {
1382          return false;
1383      }
1384      for (auto& [chain_id, chain] : m_inactive_hd_chains) {
1385          if (!TopUpChain(batch, chain, kpSize)) {
1386              return false;
1387          }
1388      }
1389      if (!batch.TxnCommit()) throw std::runtime_error(strprintf("Error during keypool top up. Cannot commit changes for wallet %s", m_storage.GetDisplayName()));
1390      NotifyCanGetAddressesChanged();
1391      // Note: Unlike with DescriptorSPKM, LegacySPKM does not need to call
1392      // m_storage.TopUpCallback() as we do not know what new scripts the LegacySPKM is
1393      // watching for. CWallet's scriptPubKey cache is not used for LegacySPKMs.
1394      return true;
1395  }
1396  
1397  bool LegacyScriptPubKeyMan::TopUpChain(WalletBatch& batch, CHDChain& chain, unsigned int kpSize)
1398  {
1399      LOCK(cs_KeyStore);
1400  
1401      if (m_storage.IsLocked()) return false;
1402  
1403      // Top up key pool
1404      unsigned int nTargetSize;
1405      if (kpSize > 0) {
1406          nTargetSize = kpSize;
1407      } else {
1408          nTargetSize = m_keypool_size;
1409      }
1410      int64_t target = std::max((int64_t) nTargetSize, int64_t{1});
1411  
1412      // count amount of available keys (internal, external)
1413      // make sure the keypool of external and internal keys fits the user selected target (-keypool)
1414      int64_t missingExternal;
1415      int64_t missingInternal;
1416      if (chain == m_hd_chain) {
1417          missingExternal = std::max(target - (int64_t)setExternalKeyPool.size(), int64_t{0});
1418          missingInternal = std::max(target - (int64_t)setInternalKeyPool.size(), int64_t{0});
1419      } else {
1420          missingExternal = std::max(target - (chain.nExternalChainCounter - chain.m_next_external_index), int64_t{0});
1421          missingInternal = std::max(target - (chain.nInternalChainCounter - chain.m_next_internal_index), int64_t{0});
1422      }
1423  
1424      if (!IsHDEnabled() || !m_storage.CanSupportFeature(FEATURE_HD_SPLIT)) {
1425          // don't create extra internal keys
1426          missingInternal = 0;
1427      }
1428      bool internal = false;
1429      for (int64_t i = missingInternal + missingExternal; i--;) {
1430          if (i < missingInternal) {
1431              internal = true;
1432          }
1433  
1434          CPubKey pubkey(GenerateNewKey(batch, chain, internal));
1435          if (chain == m_hd_chain) {
1436              AddKeypoolPubkeyWithDB(pubkey, internal, batch);
1437          }
1438      }
1439      if (missingInternal + missingExternal > 0) {
1440          if (chain == m_hd_chain) {
1441              WalletLogPrintf("keypool added %d keys (%d internal), size=%u (%u internal)\n", missingInternal + missingExternal, missingInternal, setInternalKeyPool.size() + setExternalKeyPool.size() + set_pre_split_keypool.size(), setInternalKeyPool.size());
1442          } else {
1443              WalletLogPrintf("inactive seed with id %s added %d external keys, %d internal keys\n", HexStr(chain.seed_id), missingExternal, missingInternal);
1444          }
1445      }
1446      return true;
1447  }
1448  
1449  void LegacyScriptPubKeyMan::AddKeypoolPubkeyWithDB(const CPubKey& pubkey, const bool internal, WalletBatch& batch)
1450  {
1451      LOCK(cs_KeyStore);
1452      assert(m_max_keypool_index < std::numeric_limits<int64_t>::max()); // How in the hell did you use so many keys?
1453      int64_t index = ++m_max_keypool_index;
1454      if (!batch.WritePool(index, CKeyPool(pubkey, internal))) {
1455          throw std::runtime_error(std::string(__func__) + ": writing imported pubkey failed");
1456      }
1457      if (internal) {
1458          setInternalKeyPool.insert(index);
1459      } else {
1460          setExternalKeyPool.insert(index);
1461      }
1462      m_pool_key_to_index[pubkey.GetID()] = index;
1463  }
1464  
1465  void LegacyScriptPubKeyMan::KeepDestination(int64_t nIndex, const OutputType& type)
1466  {
1467      assert(type != OutputType::BECH32M);
1468      // Remove from key pool
1469      WalletBatch batch(m_storage.GetDatabase());
1470      batch.ErasePool(nIndex);
1471      CPubKey pubkey;
1472      bool have_pk = GetPubKey(m_index_to_reserved_key.at(nIndex), pubkey);
1473      assert(have_pk);
1474      LearnRelatedScripts(pubkey, type);
1475      m_index_to_reserved_key.erase(nIndex);
1476      WalletLogPrintf("keypool keep %d\n", nIndex);
1477  }
1478  
1479  void LegacyScriptPubKeyMan::ReturnDestination(int64_t nIndex, bool fInternal, const CTxDestination&)
1480  {
1481      // Return to key pool
1482      {
1483          LOCK(cs_KeyStore);
1484          if (fInternal) {
1485              setInternalKeyPool.insert(nIndex);
1486          } else if (!set_pre_split_keypool.empty()) {
1487              set_pre_split_keypool.insert(nIndex);
1488          } else {
1489              setExternalKeyPool.insert(nIndex);
1490          }
1491          CKeyID& pubkey_id = m_index_to_reserved_key.at(nIndex);
1492          m_pool_key_to_index[pubkey_id] = nIndex;
1493          m_index_to_reserved_key.erase(nIndex);
1494          NotifyCanGetAddressesChanged();
1495      }
1496      WalletLogPrintf("keypool return %d\n", nIndex);
1497  }
1498  
1499  bool LegacyScriptPubKeyMan::GetKeyFromPool(CPubKey& result, const OutputType type)
1500  {
1501      assert(type != OutputType::BECH32M);
1502      if (!CanGetAddresses(/*internal=*/ false)) {
1503          return false;
1504      }
1505  
1506      CKeyPool keypool;
1507      {
1508          LOCK(cs_KeyStore);
1509          int64_t nIndex;
1510          if (!ReserveKeyFromKeyPool(nIndex, keypool, /*fRequestedInternal=*/ false) && !m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
1511              if (m_storage.IsLocked()) return false;
1512              WalletBatch batch(m_storage.GetDatabase());
1513              result = GenerateNewKey(batch, m_hd_chain, /*internal=*/ false);
1514              return true;
1515          }
1516          KeepDestination(nIndex, type);
1517          result = keypool.vchPubKey;
1518      }
1519      return true;
1520  }
1521  
1522  bool LegacyScriptPubKeyMan::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fRequestedInternal)
1523  {
1524      nIndex = -1;
1525      keypool.vchPubKey = CPubKey();
1526      {
1527          LOCK(cs_KeyStore);
1528  
1529          bool fReturningInternal = fRequestedInternal;
1530          fReturningInternal &= (IsHDEnabled() && m_storage.CanSupportFeature(FEATURE_HD_SPLIT)) || m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
1531          bool use_split_keypool = set_pre_split_keypool.empty();
1532          std::set<int64_t>& setKeyPool = use_split_keypool ? (fReturningInternal ? setInternalKeyPool : setExternalKeyPool) : set_pre_split_keypool;
1533  
1534          // Get the oldest key
1535          if (setKeyPool.empty()) {
1536              return false;
1537          }
1538  
1539          WalletBatch batch(m_storage.GetDatabase());
1540  
1541          auto it = setKeyPool.begin();
1542          nIndex = *it;
1543          setKeyPool.erase(it);
1544          if (!batch.ReadPool(nIndex, keypool)) {
1545              throw std::runtime_error(std::string(__func__) + ": read failed");
1546          }
1547          CPubKey pk;
1548          if (!GetPubKey(keypool.vchPubKey.GetID(), pk)) {
1549              throw std::runtime_error(std::string(__func__) + ": unknown key in key pool");
1550          }
1551          // If the key was pre-split keypool, we don't care about what type it is
1552          if (use_split_keypool && keypool.fInternal != fReturningInternal) {
1553              throw std::runtime_error(std::string(__func__) + ": keypool entry misclassified");
1554          }
1555          if (!keypool.vchPubKey.IsValid()) {
1556              throw std::runtime_error(std::string(__func__) + ": keypool entry invalid");
1557          }
1558  
1559          assert(m_index_to_reserved_key.count(nIndex) == 0);
1560          m_index_to_reserved_key[nIndex] = keypool.vchPubKey.GetID();
1561          m_pool_key_to_index.erase(keypool.vchPubKey.GetID());
1562          WalletLogPrintf("keypool reserve %d\n", nIndex);
1563      }
1564      NotifyCanGetAddressesChanged();
1565      return true;
1566  }
1567  
1568  void LegacyScriptPubKeyMan::LearnRelatedScripts(const CPubKey& key, OutputType type)
1569  {
1570      assert(type != OutputType::BECH32M);
1571      if (key.IsCompressed() && (type == OutputType::P2SH_SEGWIT || type == OutputType::BECH32)) {
1572          CTxDestination witdest = WitnessV0KeyHash(key.GetID());
1573          CScript witprog = GetScriptForDestination(witdest);
1574          // Make sure the resulting program is solvable.
1575          const auto desc = InferDescriptor(witprog, *this);
1576          assert(desc && desc->IsSolvable());
1577          AddCScript(witprog);
1578      }
1579  }
1580  
1581  void LegacyScriptPubKeyMan::LearnAllRelatedScripts(const CPubKey& key)
1582  {
1583      if (!g_implicit_segwit) return;
1584      // OutputType::P2SH_SEGWIT always adds all necessary scripts for all types.
1585      LearnRelatedScripts(key, OutputType::P2SH_SEGWIT);
1586  }
1587  
1588  std::vector<CKeyPool> LegacyScriptPubKeyMan::MarkReserveKeysAsUsed(int64_t keypool_id)
1589  {
1590      AssertLockHeld(cs_KeyStore);
1591      bool internal = setInternalKeyPool.count(keypool_id);
1592      if (!internal) assert(setExternalKeyPool.count(keypool_id) || set_pre_split_keypool.count(keypool_id));
1593      std::set<int64_t> *setKeyPool = internal ? &setInternalKeyPool : (set_pre_split_keypool.empty() ? &setExternalKeyPool : &set_pre_split_keypool);
1594      auto it = setKeyPool->begin();
1595  
1596      std::vector<CKeyPool> result;
1597      WalletBatch batch(m_storage.GetDatabase());
1598      while (it != std::end(*setKeyPool)) {
1599          const int64_t& index = *(it);
1600          if (index > keypool_id) break; // set*KeyPool is ordered
1601  
1602          CKeyPool keypool;
1603          if (batch.ReadPool(index, keypool)) { //TODO: This should be unnecessary
1604              m_pool_key_to_index.erase(keypool.vchPubKey.GetID());
1605          }
1606          LearnAllRelatedScripts(keypool.vchPubKey);
1607          batch.ErasePool(index);
1608          WalletLogPrintf("keypool index %d removed\n", index);
1609          it = setKeyPool->erase(it);
1610          result.push_back(std::move(keypool));
1611      }
1612  
1613      return result;
1614  }
1615  
1616  std::vector<CKeyID> GetAffectedKeys(const CScript& spk, const SigningProvider& provider)
1617  {
1618      std::vector<CScript> dummy;
1619      FlatSigningProvider out;
1620      InferDescriptor(spk, provider)->Expand(0, DUMMY_SIGNING_PROVIDER, dummy, out);
1621      std::vector<CKeyID> ret;
1622      ret.reserve(out.pubkeys.size());
1623      for (const auto& entry : out.pubkeys) {
1624          ret.push_back(entry.first);
1625      }
1626      return ret;
1627  }
1628  
1629  void LegacyScriptPubKeyMan::MarkPreSplitKeys()
1630  {
1631      WalletBatch batch(m_storage.GetDatabase());
1632      for (auto it = setExternalKeyPool.begin(); it != setExternalKeyPool.end();) {
1633          int64_t index = *it;
1634          CKeyPool keypool;
1635          if (!batch.ReadPool(index, keypool)) {
1636              throw std::runtime_error(std::string(__func__) + ": read keypool entry failed");
1637          }
1638          keypool.m_pre_split = true;
1639          if (!batch.WritePool(index, keypool)) {
1640              throw std::runtime_error(std::string(__func__) + ": writing modified keypool entry failed");
1641          }
1642          set_pre_split_keypool.insert(index);
1643          it = setExternalKeyPool.erase(it);
1644      }
1645  }
1646  
1647  bool LegacyScriptPubKeyMan::AddCScript(const CScript& redeemScript)
1648  {
1649      WalletBatch batch(m_storage.GetDatabase());
1650      return AddCScriptWithDB(batch, redeemScript);
1651  }
1652  
1653  bool LegacyScriptPubKeyMan::AddCScriptWithDB(WalletBatch& batch, const CScript& redeemScript)
1654  {
1655      if (!FillableSigningProvider::AddCScript(redeemScript))
1656          return false;
1657      if (batch.WriteCScript(Hash160(redeemScript), redeemScript)) {
1658          m_storage.UnsetBlankWalletFlag(batch);
1659          return true;
1660      }
1661      return false;
1662  }
1663  
1664  bool LegacyScriptPubKeyMan::AddKeyOriginWithDB(WalletBatch& batch, const CPubKey& pubkey, const KeyOriginInfo& info)
1665  {
1666      LOCK(cs_KeyStore);
1667      std::copy(info.fingerprint, info.fingerprint + 4, mapKeyMetadata[pubkey.GetID()].key_origin.fingerprint);
1668      mapKeyMetadata[pubkey.GetID()].key_origin.path = info.path;
1669      mapKeyMetadata[pubkey.GetID()].has_key_origin = true;
1670      mapKeyMetadata[pubkey.GetID()].hdKeypath = WriteHDKeypath(info.path, /*apostrophe=*/true);
1671      return batch.WriteKeyMetadata(mapKeyMetadata[pubkey.GetID()], pubkey, true);
1672  }
1673  
1674  bool LegacyScriptPubKeyMan::ImportScripts(const std::set<CScript> scripts, int64_t timestamp)
1675  {
1676      WalletBatch batch(m_storage.GetDatabase());
1677      for (const auto& entry : scripts) {
1678          CScriptID id(entry);
1679          if (HaveCScript(id)) {
1680              WalletLogPrintf("Already have script %s, skipping\n", HexStr(entry));
1681              continue;
1682          }
1683          if (!AddCScriptWithDB(batch, entry)) {
1684              return false;
1685          }
1686  
1687          if (timestamp > 0) {
1688              m_script_metadata[CScriptID(entry)].nCreateTime = timestamp;
1689          }
1690      }
1691      if (timestamp > 0) {
1692          UpdateTimeFirstKey(timestamp);
1693      }
1694  
1695      return true;
1696  }
1697  
1698  bool LegacyScriptPubKeyMan::ImportPrivKeys(const std::map<CKeyID, CKey>& privkey_map, const int64_t timestamp)
1699  {
1700      WalletBatch batch(m_storage.GetDatabase());
1701      for (const auto& entry : privkey_map) {
1702          const CKey& key = entry.second;
1703          CPubKey pubkey = key.GetPubKey();
1704          const CKeyID& id = entry.first;
1705          assert(key.VerifyPubKey(pubkey));
1706          // Skip if we already have the key
1707          if (HaveKey(id)) {
1708              WalletLogPrintf("Already have key with pubkey %s, skipping\n", HexStr(pubkey));
1709              continue;
1710          }
1711          mapKeyMetadata[id].nCreateTime = timestamp;
1712          // If the private key is not present in the wallet, insert it.
1713          if (!AddKeyPubKeyWithDB(batch, key, pubkey)) {
1714              return false;
1715          }
1716          UpdateTimeFirstKey(timestamp);
1717      }
1718      return true;
1719  }
1720  
1721  bool LegacyScriptPubKeyMan::ImportPubKeys(const std::vector<std::pair<CKeyID, bool>>& ordered_pubkeys, const std::map<CKeyID, CPubKey>& pubkey_map, const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& key_origins, const bool add_keypool, const int64_t timestamp)
1722  {
1723      WalletBatch batch(m_storage.GetDatabase());
1724      for (const auto& entry : key_origins) {
1725          AddKeyOriginWithDB(batch, entry.second.first, entry.second.second);
1726      }
1727      for (const auto& [id, internal] : ordered_pubkeys) {
1728          auto entry = pubkey_map.find(id);
1729          if (entry == pubkey_map.end()) {
1730              continue;
1731          }
1732          const CPubKey& pubkey = entry->second;
1733          CPubKey temp;
1734          if (GetPubKey(id, temp)) {
1735              // Already have pubkey, skipping
1736              WalletLogPrintf("Already have pubkey %s, skipping\n", HexStr(temp));
1737              continue;
1738          }
1739          if (!AddWatchOnlyWithDB(batch, GetScriptForRawPubKey(pubkey), timestamp)) {
1740              return false;
1741          }
1742          mapKeyMetadata[id].nCreateTime = timestamp;
1743  
1744          // Add to keypool only works with pubkeys
1745          if (add_keypool) {
1746              AddKeypoolPubkeyWithDB(pubkey, internal, batch);
1747              NotifyCanGetAddressesChanged();
1748          }
1749      }
1750      return true;
1751  }
1752  
1753  bool LegacyScriptPubKeyMan::ImportScriptPubKeys(const std::set<CScript>& script_pub_keys, const bool have_solving_data, const int64_t timestamp)
1754  {
1755      WalletBatch batch(m_storage.GetDatabase());
1756      for (const CScript& script : script_pub_keys) {
1757          if (!have_solving_data || !IsMine(script)) { // Always call AddWatchOnly for non-solvable watch-only, so that watch timestamp gets updated
1758              if (!AddWatchOnlyWithDB(batch, script, timestamp)) {
1759                  return false;
1760              }
1761          }
1762      }
1763      return true;
1764  }
1765  
1766  std::set<CKeyID> LegacyScriptPubKeyMan::GetKeys() const
1767  {
1768      LOCK(cs_KeyStore);
1769      if (!m_storage.HasEncryptionKeys()) {
1770          return FillableSigningProvider::GetKeys();
1771      }
1772      std::set<CKeyID> set_address;
1773      for (const auto& mi : mapCryptedKeys) {
1774          set_address.insert(mi.first);
1775      }
1776      return set_address;
1777  }
1778  
1779  std::unordered_set<CScript, SaltedSipHasher> LegacyDataSPKM::GetCandidateScriptPubKeys() const
1780  {
1781      LOCK(cs_KeyStore);
1782      std::unordered_set<CScript, SaltedSipHasher> candidate_spks;
1783  
1784      // For every private key in the wallet, there should be a P2PK, P2PKH, P2WPKH, and P2SH-P2WPKH
1785      const auto& add_pubkey = [&candidate_spks](const CPubKey& pub) -> void {
1786          candidate_spks.insert(GetScriptForRawPubKey(pub));
1787          candidate_spks.insert(GetScriptForDestination(PKHash(pub)));
1788  
1789          CScript wpkh = GetScriptForDestination(WitnessV0KeyHash(pub));
1790          candidate_spks.insert(wpkh);
1791          candidate_spks.insert(GetScriptForDestination(ScriptHash(wpkh)));
1792      };
1793      for (const auto& [_, key] : mapKeys) {
1794          add_pubkey(key.GetPubKey());
1795      }
1796      for (const auto& [_, ckeypair] : mapCryptedKeys) {
1797          add_pubkey(ckeypair.first);
1798      }
1799  
1800      // mapScripts contains all redeemScripts and witnessScripts. Therefore each script in it has
1801      // itself, P2SH, P2WSH, and P2SH-P2WSH as a candidate.
1802      // Invalid scripts such as P2SH-P2SH and P2WSH-P2SH, among others, will be added as candidates.
1803      // Callers of this function will need to remove such scripts.
1804      const auto& add_script = [&candidate_spks](const CScript& script) -> void {
1805          candidate_spks.insert(script);
1806          candidate_spks.insert(GetScriptForDestination(ScriptHash(script)));
1807  
1808          CScript wsh = GetScriptForDestination(WitnessV0ScriptHash(script));
1809          candidate_spks.insert(wsh);
1810          candidate_spks.insert(GetScriptForDestination(ScriptHash(wsh)));
1811      };
1812      for (const auto& [_, script] : mapScripts) {
1813          add_script(script);
1814      }
1815  
1816      // Although setWatchOnly should only contain output scripts, we will also include each script's
1817      // P2SH, P2WSH, and P2SH-P2WSH as a precaution.
1818      for (const auto& script : setWatchOnly) {
1819          add_script(script);
1820      }
1821  
1822      return candidate_spks;
1823  }
1824  
1825  std::unordered_set<CScript, SaltedSipHasher> LegacyDataSPKM::GetScriptPubKeys() const
1826  {
1827      // Run IsMine() on each candidate output script. Any script that is not ISMINE_NO is an output
1828      // script to return.
1829      // This both filters out things that are not watched by the wallet, and things that are invalid.
1830      std::unordered_set<CScript, SaltedSipHasher> spks;
1831      for (const CScript& script : GetCandidateScriptPubKeys()) {
1832          if (IsMine(script) != ISMINE_NO) {
1833              spks.insert(script);
1834          }
1835      }
1836  
1837      return spks;
1838  }
1839  
1840  std::unordered_set<CScript, SaltedSipHasher> LegacyDataSPKM::GetNotMineScriptPubKeys() const
1841  {
1842      LOCK(cs_KeyStore);
1843      std::unordered_set<CScript, SaltedSipHasher> spks;
1844      for (const CScript& script : setWatchOnly) {
1845          if (IsMine(script) == ISMINE_NO) spks.insert(script);
1846      }
1847      return spks;
1848  }
1849  
1850  std::optional<MigrationData> LegacyDataSPKM::MigrateToDescriptor()
1851  {
1852      LOCK(cs_KeyStore);
1853      if (m_storage.IsLocked()) {
1854          return std::nullopt;
1855      }
1856  
1857      MigrationData out;
1858  
1859      std::unordered_set<CScript, SaltedSipHasher> spks{GetScriptPubKeys()};
1860  
1861      // Get all key ids
1862      std::set<CKeyID> keyids;
1863      for (const auto& key_pair : mapKeys) {
1864          keyids.insert(key_pair.first);
1865      }
1866      for (const auto& key_pair : mapCryptedKeys) {
1867          keyids.insert(key_pair.first);
1868      }
1869  
1870      // Get key metadata and figure out which keys don't have a seed
1871      // Note that we do not ignore the seeds themselves because they are considered IsMine!
1872      for (auto keyid_it = keyids.begin(); keyid_it != keyids.end();) {
1873          const CKeyID& keyid = *keyid_it;
1874          const auto& it = mapKeyMetadata.find(keyid);
1875          if (it != mapKeyMetadata.end()) {
1876              const CKeyMetadata& meta = it->second;
1877              if (meta.hdKeypath == "s" || meta.hdKeypath == "m") {
1878                  keyid_it++;
1879                  continue;
1880              }
1881              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)) {
1882                  keyid_it = keyids.erase(keyid_it);
1883                  continue;
1884              }
1885          }
1886          keyid_it++;
1887      }
1888  
1889      WalletBatch batch(m_storage.GetDatabase());
1890      if (!batch.TxnBegin()) {
1891          LogWarning("Error generating descriptors for migration, cannot initialize db transaction");
1892          return std::nullopt;
1893      }
1894  
1895      constexpr auto sanitycheck = [](const bool erased, const bool maybe_compressed_key, const CScript &spk, const LegacyDataSPKM& self, const DescriptorScriptPubKeyMan& desc_spk_man) {
1896          assert(desc_spk_man.IsMine(spk) == ISMINE_SPENDABLE);
1897          if (erased) {
1898              assert(self.IsMine(spk) == ISMINE_SPENDABLE);
1899              return;
1900          }
1901          if (maybe_compressed_key && !g_implicit_segwit) {
1902              // combo() includes segwit
1903              if (spk.IsPayToScriptHash()) return;
1904              int witness_version;
1905              std::vector<unsigned char> witness_program;
1906              if (spk.IsWitnessProgram(witness_version, witness_program)) {
1907                  if (witness_version == 0 && witness_program.size() == 20) {
1908                      return;
1909                  }
1910              }
1911          }
1912          assert(erased);
1913      };
1914  
1915      // keyids is now all non-HD keys. Each key will have its own combo descriptor
1916      for (const CKeyID& keyid : keyids) {
1917          CKey key;
1918          if (!GetKey(keyid, key)) {
1919              assert(false);
1920          }
1921  
1922          // Get birthdate from key meta
1923          uint64_t creation_time = 0;
1924          const auto& it = mapKeyMetadata.find(keyid);
1925          if (it != mapKeyMetadata.end()) {
1926              creation_time = it->second.nCreateTime;
1927          }
1928  
1929          // Get the key origin
1930          // Maybe this doesn't matter because floating keys here shouldn't have origins
1931          KeyOriginInfo info;
1932          bool has_info = GetKeyOrigin(keyid, info);
1933          std::string origin_str = has_info ? "[" + HexStr(info.fingerprint) + FormatHDKeypath(info.path) + "]" : "";
1934  
1935          // Construct the combo descriptor
1936          std::string desc_str = "combo(" + origin_str + HexStr(key.GetPubKey()) + ")";
1937          FlatSigningProvider keys;
1938          std::string error;
1939          std::vector<std::unique_ptr<Descriptor>> descs = Parse(desc_str, keys, error, false);
1940          CHECK_NONFATAL(descs.size() == 1); // It shouldn't be possible to have an invalid or multipath descriptor
1941          WalletDescriptor w_desc(std::move(descs.at(0)), creation_time, 0, 0, 0);
1942  
1943          // Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
1944          auto desc_spk_man = std::make_unique<DescriptorScriptPubKeyMan>(m_storage, w_desc, /*keypool_size=*/0);
1945          WITH_LOCK(desc_spk_man->cs_desc_man, desc_spk_man->AddDescriptorKeyWithDB(batch, key, key.GetPubKey()));
1946          desc_spk_man->TopUpWithDB(batch);
1947          auto desc_spks = desc_spk_man->GetScriptPubKeys();
1948  
1949          // Remove the scriptPubKeys from our current set
1950          for (const CScript& spk : desc_spks) {
1951              size_t erased = spks.erase(spk);
1952              sanitycheck(erased, key.IsCompressed(), spk, *this, *desc_spk_man);
1953          }
1954  
1955          out.desc_spkms.push_back(std::move(desc_spk_man));
1956      }
1957  
1958      // Handle HD keys by using the CHDChains
1959      std::vector<CHDChain> chains;
1960      chains.push_back(m_hd_chain);
1961      for (const auto& chain_pair : m_inactive_hd_chains) {
1962          chains.push_back(chain_pair.second);
1963      }
1964      for (const CHDChain& chain : chains) {
1965          for (int i = 0; i < 2; ++i) {
1966              // Skip if doing internal chain and split chain is not supported
1967              if (chain.seed_id.IsNull() || (i == 1 && !m_storage.CanSupportFeature(FEATURE_HD_SPLIT))) {
1968                  continue;
1969              }
1970              // Get the master xprv
1971              CKey seed_key;
1972              if (!GetKey(chain.seed_id, seed_key)) {
1973                  assert(false);
1974              }
1975              CExtKey master_key;
1976              master_key.SetSeed(seed_key);
1977  
1978              // Make the combo descriptor
1979              std::string xpub = EncodeExtPubKey(master_key.Neuter());
1980              std::string desc_str = "combo(" + xpub + "/0h/" + ToString(i) + "h/*h)";
1981              FlatSigningProvider keys;
1982              std::string error;
1983              std::vector<std::unique_ptr<Descriptor>> descs = Parse(desc_str, keys, error, false);
1984              CHECK_NONFATAL(descs.size() == 1); // It shouldn't be possible to have an invalid or multipath descriptor
1985              uint32_t chain_counter = std::max((i == 1 ? chain.nInternalChainCounter : chain.nExternalChainCounter), (uint32_t)0);
1986              WalletDescriptor w_desc(std::move(descs.at(0)), 0, 0, chain_counter, 0);
1987  
1988              // Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
1989              auto desc_spk_man = std::make_unique<DescriptorScriptPubKeyMan>(m_storage, w_desc, /*keypool_size=*/0);
1990              WITH_LOCK(desc_spk_man->cs_desc_man, desc_spk_man->AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey()));
1991              desc_spk_man->TopUpWithDB(batch);
1992              auto desc_spks = desc_spk_man->GetScriptPubKeys();
1993  
1994              // Remove the scriptPubKeys from our current set
1995              for (const CScript& spk : desc_spks) {
1996                  size_t erased = spks.erase(spk);
1997                  sanitycheck(erased, /*maybe_compressed_key=*/true, spk, *this, *desc_spk_man);
1998              }
1999  
2000              out.desc_spkms.push_back(std::move(desc_spk_man));
2001          }
2002      }
2003      // Add the current master seed to the migration data
2004      if (!m_hd_chain.seed_id.IsNull()) {
2005          CKey seed_key;
2006          if (!GetKey(m_hd_chain.seed_id, seed_key)) {
2007              assert(false);
2008          }
2009          out.master_key.SetSeed(seed_key);
2010      }
2011  
2012      // Handle the rest of the scriptPubKeys which must be imports and may not have all info
2013      for (auto it = spks.begin(); it != spks.end();) {
2014          const CScript& spk = *it;
2015  
2016          // Get birthdate from script meta
2017          uint64_t creation_time = 0;
2018          const auto& mit = m_script_metadata.find(CScriptID(spk));
2019          if (mit != m_script_metadata.end()) {
2020              creation_time = mit->second.nCreateTime;
2021          }
2022  
2023          // InferDescriptor as that will get us all the solving info if it is there
2024          std::unique_ptr<Descriptor> desc = InferDescriptor(spk, *GetSolvingProvider(spk));
2025  
2026          // Past bugs in InferDescriptor have caused it to create descriptors which cannot be re-parsed.
2027          // Re-parse the descriptors to detect that, and skip any that do not parse.
2028          {
2029              std::string desc_str = desc->ToString();
2030              FlatSigningProvider parsed_keys;
2031              std::string parse_error;
2032              std::vector<std::unique_ptr<Descriptor>> parsed_descs = Parse(desc_str, parsed_keys, parse_error);
2033              if (parsed_descs.empty()) {
2034                  // Remove this scriptPubKey from the set
2035                  it = spks.erase(it);
2036                  continue;
2037              }
2038          }
2039  
2040          // Get the private keys for this descriptor
2041          std::vector<CScript> scripts;
2042          FlatSigningProvider keys;
2043          if (!desc->Expand(0, DUMMY_SIGNING_PROVIDER, scripts, keys)) {
2044              assert(false);
2045          }
2046          std::set<CKeyID> privkeyids;
2047          for (const auto& key_orig_pair : keys.origins) {
2048              privkeyids.insert(key_orig_pair.first);
2049          }
2050  
2051          std::vector<CScript> desc_spks;
2052  
2053          // Make the descriptor string with private keys
2054          std::string desc_str;
2055          bool watchonly = !desc->ToPrivateString(*this, desc_str);
2056          if (watchonly && !m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
2057              out.watch_descs.emplace_back(desc->ToString(), creation_time);
2058  
2059              // Get the scriptPubKeys without writing this to the wallet
2060              FlatSigningProvider provider;
2061              desc->Expand(0, provider, desc_spks, provider);
2062          } else {
2063              // Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
2064              WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
2065              auto desc_spk_man = std::make_unique<DescriptorScriptPubKeyMan>(m_storage, w_desc, /*keypool_size=*/0);
2066              for (const auto& keyid : privkeyids) {
2067                  CKey key;
2068                  if (!GetKey(keyid, key)) {
2069                      continue;
2070                  }
2071                  WITH_LOCK(desc_spk_man->cs_desc_man, desc_spk_man->AddDescriptorKeyWithDB(batch, key, key.GetPubKey()));
2072              }
2073              desc_spk_man->TopUpWithDB(batch);
2074              auto desc_spks_set = desc_spk_man->GetScriptPubKeys();
2075              desc_spks.insert(desc_spks.end(), desc_spks_set.begin(), desc_spks_set.end());
2076  
2077              out.desc_spkms.push_back(std::move(desc_spk_man));
2078          }
2079  
2080          // Remove the scriptPubKeys from our current set
2081          for (const CScript& desc_spk : desc_spks) {
2082              auto del_it = spks.find(desc_spk);
2083              assert(del_it != spks.end());
2084              assert(IsMine(desc_spk) != ISMINE_NO);
2085              it = spks.erase(del_it);
2086          }
2087      }
2088  
2089      // Make sure that we have accounted for all scriptPubKeys
2090      if (!Assume(spks.empty())) {
2091          LogError("%s", STR_INTERNAL_BUG("Error: Some output scripts were not migrated."));
2092          return std::nullopt;
2093      }
2094  
2095      // Legacy wallets can also contain scripts whose P2SH, P2WSH, or P2SH-P2WSH it is not watching for
2096      // but can provide script data to a PSBT spending them. These "solvable" output scripts will need to
2097      // be put into the separate "solvables" wallet.
2098      // These can be detected by going through the entire candidate output scripts, finding the ISMINE_NO scripts,
2099      // and checking CanProvide() which will dummy sign.
2100      for (const CScript& script : GetCandidateScriptPubKeys()) {
2101          // Since we only care about P2SH, P2WSH, and P2SH-P2WSH, filter out any scripts that are not those
2102          if (!script.IsPayToScriptHash() && !script.IsPayToWitnessScriptHash()) {
2103              continue;
2104          }
2105          if (IsMine(script) != ISMINE_NO) {
2106              continue;
2107          }
2108          SignatureData dummy_sigdata;
2109          if (!CanProvide(script, dummy_sigdata)) {
2110              continue;
2111          }
2112  
2113          // Get birthdate from script meta
2114          uint64_t creation_time = 0;
2115          const auto& it = m_script_metadata.find(CScriptID(script));
2116          if (it != m_script_metadata.end()) {
2117              creation_time = it->second.nCreateTime;
2118          }
2119  
2120          // InferDescriptor as that will get us all the solving info if it is there
2121          std::unique_ptr<Descriptor> desc = InferDescriptor(script, *GetSolvingProvider(script));
2122          if (!desc->IsSolvable()) {
2123              // The wallet was able to provide some information, but not enough to make a descriptor that actually
2124              // contains anything useful. This is probably because the script itself is actually unsignable (e.g. P2WSH-P2WSH).
2125              continue;
2126          }
2127  
2128          // Past bugs in InferDescriptor have caused it to create descriptors which cannot be re-parsed
2129          // Re-parse the descriptors to detect that, and skip any that do not parse.
2130          {
2131              std::string desc_str = desc->ToString();
2132              FlatSigningProvider parsed_keys;
2133              std::string parse_error;
2134              std::vector<std::unique_ptr<Descriptor>> parsed_descs = Parse(desc_str, parsed_keys, parse_error, false);
2135              if (parsed_descs.empty()) {
2136                  continue;
2137              }
2138          }
2139  
2140          out.solvable_descs.emplace_back(desc->ToString(), creation_time);
2141      }
2142  
2143      // Finalize transaction
2144      if (!batch.TxnCommit()) {
2145          LogWarning("Error generating descriptors for migration, cannot commit db transaction");
2146          return std::nullopt;
2147      }
2148  
2149      return out;
2150  }
2151  
2152  bool LegacyDataSPKM::DeleteRecords()
2153  {
2154      return RunWithinTxn(m_storage.GetDatabase(), /*process_desc=*/"delete legacy records", [&](WalletBatch& batch){
2155          return DeleteRecordsWithDB(batch);
2156      });
2157  }
2158  
2159  bool LegacyDataSPKM::DeleteRecordsWithDB(WalletBatch& batch)
2160  {
2161      LOCK(cs_KeyStore);
2162      return batch.EraseRecords(DBKeys::LEGACY_TYPES);
2163  }
2164  
2165  util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type)
2166  {
2167      // Returns true if this descriptor supports getting new addresses. Conditions where we may be unable to fetch them (e.g. locked) are caught later
2168      if (!CanGetAddresses()) {
2169          return util::Error{_("No addresses available")};
2170      }
2171      {
2172          LOCK(cs_desc_man);
2173          assert(m_wallet_descriptor.descriptor->IsSingleType()); // This is a combo descriptor which should not be an active descriptor
2174          std::optional<OutputType> desc_addr_type = m_wallet_descriptor.descriptor->GetOutputType();
2175          assert(desc_addr_type);
2176          if (type != *desc_addr_type) {
2177              throw std::runtime_error(std::string(__func__) + ": Types are inconsistent. Stored type does not match type of newly generated address");
2178          }
2179  
2180          TopUp();
2181  
2182          // Get the scriptPubKey from the descriptor
2183          FlatSigningProvider out_keys;
2184          std::vector<CScript> scripts_temp;
2185          if (m_wallet_descriptor.range_end <= m_max_cached_index && !TopUp(1)) {
2186              // We can't generate anymore keys
2187              return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
2188          }
2189          if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.next_index, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
2190              // We can't generate anymore keys
2191              return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
2192          }
2193  
2194          CTxDestination dest;
2195          if (!ExtractDestination(scripts_temp[0], dest)) {
2196              return util::Error{_("Error: Cannot extract destination from the generated scriptpubkey")}; // shouldn't happen
2197          }
2198          m_wallet_descriptor.next_index++;
2199          WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);
2200          return dest;
2201      }
2202  }
2203  
2204  isminetype DescriptorScriptPubKeyMan::IsMine(const CScript& script) const
2205  {
2206      LOCK(cs_desc_man);
2207      if (m_map_script_pub_keys.count(script) > 0) {
2208          return ISMINE_SPENDABLE;
2209      }
2210      return ISMINE_NO;
2211  }
2212  
2213  bool DescriptorScriptPubKeyMan::CheckDecryptionKey(const CKeyingMaterial& master_key)
2214  {
2215      LOCK(cs_desc_man);
2216      if (!m_map_keys.empty()) {
2217          return false;
2218      }
2219  
2220      bool keyPass = m_map_crypted_keys.empty(); // Always pass when there are no encrypted keys
2221      bool keyFail = false;
2222      for (const auto& mi : m_map_crypted_keys) {
2223          const CPubKey &pubkey = mi.second.first;
2224          const std::vector<unsigned char> &crypted_secret = mi.second.second;
2225          CKey key;
2226          if (!DecryptKey(master_key, crypted_secret, pubkey, key)) {
2227              keyFail = true;
2228              break;
2229          }
2230          keyPass = true;
2231          if (m_decryption_thoroughly_checked)
2232              break;
2233      }
2234      if (keyPass && keyFail) {
2235          LogWarning("The wallet is probably corrupted: Some keys decrypt but not all.");
2236          throw std::runtime_error("Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
2237      }
2238      if (keyFail || !keyPass) {
2239          return false;
2240      }
2241      m_decryption_thoroughly_checked = true;
2242      return true;
2243  }
2244  
2245  bool DescriptorScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch)
2246  {
2247      LOCK(cs_desc_man);
2248      if (!m_map_crypted_keys.empty()) {
2249          return false;
2250      }
2251  
2252      for (const KeyMap::value_type& key_in : m_map_keys)
2253      {
2254          const CKey &key = key_in.second;
2255          CPubKey pubkey = key.GetPubKey();
2256          CKeyingMaterial secret{UCharCast(key.begin()), UCharCast(key.end())};
2257          std::vector<unsigned char> crypted_secret;
2258          if (!EncryptSecret(master_key, secret, pubkey.GetHash(), crypted_secret)) {
2259              return false;
2260          }
2261          m_map_crypted_keys[pubkey.GetID()] = make_pair(pubkey, crypted_secret);
2262          batch->WriteCryptedDescriptorKey(GetID(), pubkey, crypted_secret);
2263      }
2264      m_map_keys.clear();
2265      return true;
2266  }
2267  
2268  util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, int64_t& index, CKeyPool& keypool)
2269  {
2270      LOCK(cs_desc_man);
2271      auto op_dest = GetNewDestination(type);
2272      index = m_wallet_descriptor.next_index - 1;
2273      return op_dest;
2274  }
2275  
2276  void DescriptorScriptPubKeyMan::ReturnDestination(int64_t index, bool internal, const CTxDestination& addr)
2277  {
2278      LOCK(cs_desc_man);
2279      // Only return when the index was the most recent
2280      if (m_wallet_descriptor.next_index - 1 == index) {
2281          m_wallet_descriptor.next_index--;
2282      }
2283      WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);
2284      NotifyCanGetAddressesChanged();
2285  }
2286  
2287  std::map<CKeyID, CKey> DescriptorScriptPubKeyMan::GetKeys() const
2288  {
2289      AssertLockHeld(cs_desc_man);
2290      if (m_storage.HasEncryptionKeys() && !m_storage.IsLocked()) {
2291          KeyMap keys;
2292          for (const auto& key_pair : m_map_crypted_keys) {
2293              const CPubKey& pubkey = key_pair.second.first;
2294              const std::vector<unsigned char>& crypted_secret = key_pair.second.second;
2295              CKey key;
2296              m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
2297                  return DecryptKey(encryption_key, crypted_secret, pubkey, key);
2298              });
2299              keys[pubkey.GetID()] = key;
2300          }
2301          return keys;
2302      }
2303      return m_map_keys;
2304  }
2305  
2306  bool DescriptorScriptPubKeyMan::HasPrivKey(const CKeyID& keyid) const
2307  {
2308      AssertLockHeld(cs_desc_man);
2309      return m_map_keys.contains(keyid) || m_map_crypted_keys.contains(keyid);
2310  }
2311  
2312  std::optional<CKey> DescriptorScriptPubKeyMan::GetKey(const CKeyID& keyid) const
2313  {
2314      AssertLockHeld(cs_desc_man);
2315      if (m_storage.HasEncryptionKeys() && !m_storage.IsLocked()) {
2316          const auto& it = m_map_crypted_keys.find(keyid);
2317          if (it == m_map_crypted_keys.end()) {
2318              return std::nullopt;
2319          }
2320          const std::vector<unsigned char>& crypted_secret = it->second.second;
2321          CKey key;
2322          if (!Assume(m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
2323              return DecryptKey(encryption_key, crypted_secret, it->second.first, key);
2324          }))) {
2325              return std::nullopt;
2326          }
2327          return key;
2328      }
2329      const auto& it = m_map_keys.find(keyid);
2330      if (it == m_map_keys.end()) {
2331          return std::nullopt;
2332      }
2333      return it->second;
2334  }
2335  
2336  bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
2337  {
2338      WalletBatch batch(m_storage.GetDatabase());
2339      if (!batch.TxnBegin()) return false;
2340      bool res = TopUpWithDB(batch, size);
2341      if (!batch.TxnCommit()) throw std::runtime_error(strprintf("Error during descriptors keypool top up. Cannot commit changes for wallet %s", m_storage.GetDisplayName()));
2342      return res;
2343  }
2344  
2345  bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int size)
2346  {
2347      LOCK(cs_desc_man);
2348      std::set<CScript> new_spks;
2349      unsigned int target_size;
2350      if (size > 0) {
2351          target_size = size;
2352      } else {
2353          target_size = m_keypool_size;
2354      }
2355  
2356      // Calculate the new range_end
2357      int32_t new_range_end = std::max(m_wallet_descriptor.next_index + (int32_t)target_size, m_wallet_descriptor.range_end);
2358  
2359      // If the descriptor is not ranged, we actually just want to fill the first cache item
2360      if (!m_wallet_descriptor.descriptor->IsRange()) {
2361          new_range_end = 1;
2362          m_wallet_descriptor.range_end = 1;
2363          m_wallet_descriptor.range_start = 0;
2364      }
2365  
2366      FlatSigningProvider provider;
2367      provider.keys = GetKeys();
2368  
2369      uint256 id = GetID();
2370      for (int32_t i = m_max_cached_index + 1; i < new_range_end; ++i) {
2371          FlatSigningProvider out_keys;
2372          std::vector<CScript> scripts_temp;
2373          DescriptorCache temp_cache;
2374          // Maybe we have a cached xpub and we can expand from the cache first
2375          if (!m_wallet_descriptor.descriptor->ExpandFromCache(i, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
2376              if (!m_wallet_descriptor.descriptor->Expand(i, provider, scripts_temp, out_keys, &temp_cache)) return false;
2377          }
2378          // Add all of the scriptPubKeys to the scriptPubKey set
2379          new_spks.insert(scripts_temp.begin(), scripts_temp.end());
2380          for (const CScript& script : scripts_temp) {
2381              m_map_script_pub_keys[script] = i;
2382          }
2383          for (const auto& pk_pair : out_keys.pubkeys) {
2384              const CPubKey& pubkey = pk_pair.second;
2385              if (m_map_pubkeys.count(pubkey) != 0) {
2386                  // We don't need to give an error here.
2387                  // 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
2388                  continue;
2389              }
2390              m_map_pubkeys[pubkey] = i;
2391          }
2392          // Merge and write the cache
2393          DescriptorCache new_items = m_wallet_descriptor.cache.MergeAndDiff(temp_cache);
2394          if (!batch.WriteDescriptorCacheItems(id, new_items)) {
2395              throw std::runtime_error(std::string(__func__) + ": writing cache items failed");
2396          }
2397          m_max_cached_index++;
2398      }
2399      m_wallet_descriptor.range_end = new_range_end;
2400      batch.WriteDescriptor(GetID(), m_wallet_descriptor);
2401  
2402      // By this point, the cache size should be the size of the entire range
2403      assert(m_wallet_descriptor.range_end - 1 == m_max_cached_index);
2404  
2405      m_storage.TopUpCallback(new_spks, this);
2406      NotifyCanGetAddressesChanged();
2407      return true;
2408  }
2409  
2410  std::vector<WalletDestination> DescriptorScriptPubKeyMan::MarkUnusedAddresses(const CScript& script)
2411  {
2412      LOCK(cs_desc_man);
2413      std::vector<WalletDestination> result;
2414      if (IsMine(script)) {
2415          int32_t index = m_map_script_pub_keys[script];
2416          if (index >= m_wallet_descriptor.next_index) {
2417              WalletLogPrintf("%s: Detected a used keypool item at index %d, mark all keypool items up to this item as used\n", __func__, index);
2418              auto out_keys = std::make_unique<FlatSigningProvider>();
2419              std::vector<CScript> scripts_temp;
2420              while (index >= m_wallet_descriptor.next_index) {
2421                  if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.next_index, m_wallet_descriptor.cache, scripts_temp, *out_keys)) {
2422                      throw std::runtime_error(std::string(__func__) + ": Unable to expand descriptor from cache");
2423                  }
2424                  CTxDestination dest;
2425                  ExtractDestination(scripts_temp[0], dest);
2426                  result.push_back({dest, std::nullopt});
2427                  m_wallet_descriptor.next_index++;
2428              }
2429          }
2430          if (!TopUp()) {
2431              WalletLogPrintf("%s: Topping up keypool failed (locked wallet)\n", __func__);
2432          }
2433      }
2434  
2435      return result;
2436  }
2437  
2438  void DescriptorScriptPubKeyMan::AddDescriptorKey(const CKey& key, const CPubKey &pubkey)
2439  {
2440      LOCK(cs_desc_man);
2441      WalletBatch batch(m_storage.GetDatabase());
2442      if (!AddDescriptorKeyWithDB(batch, key, pubkey)) {
2443          throw std::runtime_error(std::string(__func__) + ": writing descriptor private key failed");
2444      }
2445  }
2446  
2447  bool DescriptorScriptPubKeyMan::AddDescriptorKeyWithDB(WalletBatch& batch, const CKey& key, const CPubKey &pubkey)
2448  {
2449      AssertLockHeld(cs_desc_man);
2450      assert(!m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
2451  
2452      // Check if provided key already exists
2453      if (m_map_keys.find(pubkey.GetID()) != m_map_keys.end() ||
2454          m_map_crypted_keys.find(pubkey.GetID()) != m_map_crypted_keys.end()) {
2455          return true;
2456      }
2457  
2458      if (m_storage.HasEncryptionKeys()) {
2459          if (m_storage.IsLocked()) {
2460              return false;
2461          }
2462  
2463          std::vector<unsigned char> crypted_secret;
2464          CKeyingMaterial secret{UCharCast(key.begin()), UCharCast(key.end())};
2465          if (!m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
2466                  return EncryptSecret(encryption_key, secret, pubkey.GetHash(), crypted_secret);
2467              })) {
2468              return false;
2469          }
2470  
2471          m_map_crypted_keys[pubkey.GetID()] = make_pair(pubkey, crypted_secret);
2472          return batch.WriteCryptedDescriptorKey(GetID(), pubkey, crypted_secret);
2473      } else {
2474          m_map_keys[pubkey.GetID()] = key;
2475          return batch.WriteDescriptorKey(GetID(), pubkey, key.GetPrivKey());
2476      }
2477  }
2478  
2479  bool DescriptorScriptPubKeyMan::SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal)
2480  {
2481      LOCK(cs_desc_man);
2482      assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
2483  
2484      // Ignore when there is already a descriptor
2485      if (m_wallet_descriptor.descriptor) {
2486          return false;
2487      }
2488  
2489      m_wallet_descriptor = GenerateWalletDescriptor(master_key.Neuter(), addr_type, internal);
2490  
2491      // Store the master private key, and descriptor
2492      if (!AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) {
2493          throw std::runtime_error(std::string(__func__) + ": writing descriptor master private key failed");
2494      }
2495      if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
2496          throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
2497      }
2498  
2499      // TopUp
2500      TopUpWithDB(batch);
2501  
2502      m_storage.UnsetBlankWalletFlag(batch);
2503      return true;
2504  }
2505  
2506  bool DescriptorScriptPubKeyMan::IsHDEnabled() const
2507  {
2508      LOCK(cs_desc_man);
2509      return m_wallet_descriptor.descriptor->IsRange();
2510  }
2511  
2512  bool DescriptorScriptPubKeyMan::CanGetAddresses(bool internal) const
2513  {
2514      // We can only give out addresses from descriptors that are single type (not combo), ranged,
2515      // and either have cached keys or can generate more keys (ignoring encryption)
2516      LOCK(cs_desc_man);
2517      return m_wallet_descriptor.descriptor->IsSingleType() &&
2518             m_wallet_descriptor.descriptor->IsRange() &&
2519             (HavePrivateKeys() || m_wallet_descriptor.next_index < m_wallet_descriptor.range_end);
2520  }
2521  
2522  bool DescriptorScriptPubKeyMan::HavePrivateKeys() const
2523  {
2524      LOCK(cs_desc_man);
2525      return m_map_keys.size() > 0 || m_map_crypted_keys.size() > 0;
2526  }
2527  
2528  bool DescriptorScriptPubKeyMan::HaveCryptedKeys() const
2529  {
2530      LOCK(cs_desc_man);
2531      return !m_map_crypted_keys.empty();
2532  }
2533  
2534  std::optional<int64_t> DescriptorScriptPubKeyMan::GetOldestKeyPoolTime() const
2535  {
2536      // This is only used for getwalletinfo output and isn't relevant to descriptor wallets.
2537      return std::nullopt;
2538  }
2539  
2540  
2541  unsigned int DescriptorScriptPubKeyMan::GetKeyPoolSize() const
2542  {
2543      LOCK(cs_desc_man);
2544      return m_wallet_descriptor.range_end - m_wallet_descriptor.next_index;
2545  }
2546  
2547  int64_t DescriptorScriptPubKeyMan::GetTimeFirstKey() const
2548  {
2549      LOCK(cs_desc_man);
2550      return m_wallet_descriptor.creation_time;
2551  }
2552  
2553  std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvider(const CScript& script, bool include_private) const
2554  {
2555      LOCK(cs_desc_man);
2556  
2557      // Find the index of the script
2558      auto it = m_map_script_pub_keys.find(script);
2559      if (it == m_map_script_pub_keys.end()) {
2560          return nullptr;
2561      }
2562      int32_t index = it->second;
2563  
2564      return GetSigningProvider(index, include_private);
2565  }
2566  
2567  std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvider(const CPubKey& pubkey) const
2568  {
2569      LOCK(cs_desc_man);
2570  
2571      // Find index of the pubkey
2572      auto it = m_map_pubkeys.find(pubkey);
2573      if (it == m_map_pubkeys.end()) {
2574          return nullptr;
2575      }
2576      int32_t index = it->second;
2577  
2578      // Always try to get the signing provider with private keys. This function should only be called during signing anyways
2579      std::unique_ptr<FlatSigningProvider> out = GetSigningProvider(index, true);
2580      if (!out->HaveKey(pubkey.GetID())) {
2581          return nullptr;
2582      }
2583      return out;
2584  }
2585  
2586  std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvider(int32_t index, bool include_private) const
2587  {
2588      AssertLockHeld(cs_desc_man);
2589  
2590      std::unique_ptr<FlatSigningProvider> out_keys = std::make_unique<FlatSigningProvider>();
2591  
2592      // Fetch SigningProvider from cache to avoid re-deriving
2593      auto it = m_map_signing_providers.find(index);
2594      if (it != m_map_signing_providers.end()) {
2595          out_keys->Merge(FlatSigningProvider{it->second});
2596      } else {
2597          // Get the scripts, keys, and key origins for this script
2598          std::vector<CScript> scripts_temp;
2599          if (!m_wallet_descriptor.descriptor->ExpandFromCache(index, m_wallet_descriptor.cache, scripts_temp, *out_keys)) return nullptr;
2600  
2601          // Cache SigningProvider so we don't need to re-derive if we need this SigningProvider again
2602          m_map_signing_providers[index] = *out_keys;
2603      }
2604  
2605      if (HavePrivateKeys() && include_private) {
2606          FlatSigningProvider master_provider;
2607          master_provider.keys = GetKeys();
2608          m_wallet_descriptor.descriptor->ExpandPrivate(index, master_provider, *out_keys);
2609      }
2610  
2611      return out_keys;
2612  }
2613  
2614  std::unique_ptr<SigningProvider> DescriptorScriptPubKeyMan::GetSolvingProvider(const CScript& script) const
2615  {
2616      return GetSigningProvider(script, false);
2617  }
2618  
2619  bool DescriptorScriptPubKeyMan::CanProvide(const CScript& script, SignatureData& sigdata)
2620  {
2621      return IsMine(script);
2622  }
2623  
2624  bool DescriptorScriptPubKeyMan::SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors, std::optional<CAmount>* inputs_amount_sum) const
2625  {
2626      std::unique_ptr<FlatSigningProvider> keys = std::make_unique<FlatSigningProvider>();
2627      for (const auto& coin_pair : coins) {
2628          std::unique_ptr<FlatSigningProvider> coin_keys = GetSigningProvider(coin_pair.second.out.scriptPubKey, true);
2629          if (!coin_keys) {
2630              continue;
2631          }
2632          keys->Merge(std::move(*coin_keys));
2633      }
2634  
2635      return ::SignTransaction(tx, keys.get(), coins, sighash, input_errors, inputs_amount_sum);
2636  }
2637  
2638  SigningResult DescriptorScriptPubKeyMan::SignMessage(const MessageSignatureFormat format, const std::string& message, const CTxDestination& address, std::string& str_sig) const
2639  {
2640      std::unique_ptr<FlatSigningProvider> keys = GetSigningProvider(GetScriptForDestination(address), true);
2641      if (!keys) {
2642          return SigningResult::PRIVATE_KEY_NOT_AVAILABLE;
2643      }
2644  
2645      if (format != MessageSignatureFormat::LEGACY) {
2646          return SignMessageBIP322(format, keys.get(), message, address, str_sig);
2647      }
2648  
2649      const PKHash* pkhash = std::get_if<PKHash>(&address);
2650      if (!pkhash) {
2651          return SigningResult::PRIVATE_KEY_NOT_AVAILABLE;
2652      }
2653  
2654      CKey key;
2655      if (!keys->GetKey(ToKeyID(*pkhash), key)) {
2656          return SigningResult::PRIVATE_KEY_NOT_AVAILABLE;
2657      }
2658  
2659      if (!MessageSign(key, message, str_sig)) {
2660          return SigningResult::SIGNING_FAILED;
2661      }
2662  
2663      return SigningResult::OK;
2664  }
2665  
2666  std::optional<PSBTError> DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbtx, const PrecomputedTransactionData& txdata, int sighash_type, bool sign, bool bip32derivs, int* n_signed, bool finalize) const
2667  {
2668      if (n_signed) {
2669          *n_signed = 0;
2670      }
2671      for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
2672          const CTxIn& txin = psbtx.tx->vin[i];
2673          PSBTInput& input = psbtx.inputs.at(i);
2674  
2675          if (PSBTInputSigned(input)) {
2676              continue;
2677          }
2678  
2679          // Get the Sighash type
2680          if (sign && input.sighash_type != std::nullopt && *input.sighash_type != sighash_type) {
2681              return PSBTError::SIGHASH_MISMATCH;
2682          }
2683  
2684          // Get the scriptPubKey to know which SigningProvider to use
2685          CScript script;
2686          if (!input.witness_utxo.IsNull()) {
2687              script = input.witness_utxo.scriptPubKey;
2688          } else if (input.non_witness_utxo) {
2689              if (txin.prevout.n >= input.non_witness_utxo->vout.size()) {
2690                  return PSBTError::MISSING_INPUTS;
2691              }
2692              script = input.non_witness_utxo->vout[txin.prevout.n].scriptPubKey;
2693          } else {
2694              // There's no UTXO so we can just skip this now
2695              continue;
2696          }
2697  
2698          std::unique_ptr<FlatSigningProvider> keys = std::make_unique<FlatSigningProvider>();
2699          std::unique_ptr<FlatSigningProvider> script_keys = GetSigningProvider(script, /*include_private=*/sign);
2700          if (script_keys) {
2701              keys->Merge(std::move(*script_keys));
2702          } else {
2703              // Maybe there are pubkeys listed that we can sign for
2704              std::vector<CPubKey> pubkeys;
2705              pubkeys.reserve(input.hd_keypaths.size() + 2);
2706  
2707              // ECDSA Pubkeys
2708              for (const auto& [pk, _] : input.hd_keypaths) {
2709                  pubkeys.push_back(pk);
2710              }
2711  
2712              // Taproot output pubkey
2713              std::vector<std::vector<unsigned char>> sols;
2714              if (Solver(script, sols) == TxoutType::WITNESS_V1_TAPROOT) {
2715                  sols[0].insert(sols[0].begin(), 0x02);
2716                  pubkeys.emplace_back(sols[0]);
2717                  sols[0][0] = 0x03;
2718                  pubkeys.emplace_back(sols[0]);
2719              }
2720  
2721              // Taproot pubkeys
2722              for (const auto& pk_pair : input.m_tap_bip32_paths) {
2723                  const XOnlyPubKey& pubkey = pk_pair.first;
2724                  for (unsigned char prefix : {0x02, 0x03}) {
2725                      unsigned char b[33] = {prefix};
2726                      std::copy(pubkey.begin(), pubkey.end(), b + 1);
2727                      CPubKey fullpubkey;
2728                      fullpubkey.Set(b, b + 33);
2729                      pubkeys.push_back(fullpubkey);
2730                  }
2731              }
2732  
2733              for (const auto& pubkey : pubkeys) {
2734                  std::unique_ptr<FlatSigningProvider> pk_keys = GetSigningProvider(pubkey);
2735                  if (pk_keys) {
2736                      keys->Merge(std::move(*pk_keys));
2737                  }
2738              }
2739          }
2740  
2741          SignPSBTInput(HidingSigningProvider(keys.get(), /*hide_secret=*/!sign, /*hide_origin=*/!bip32derivs), psbtx, i, &txdata, sighash_type, nullptr, finalize);
2742  
2743          bool signed_one = PSBTInputSigned(input);
2744          if (n_signed && (signed_one || !sign)) {
2745              // If sign is false, we assume that we _could_ sign if we get here. This
2746              // will never have false negatives; it is hard to tell under what i
2747              // circumstances it could have false positives.
2748              (*n_signed)++;
2749          }
2750      }
2751  
2752      // Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
2753      for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) {
2754          std::unique_ptr<SigningProvider> keys = GetSolvingProvider(psbtx.tx->vout.at(i).scriptPubKey);
2755          if (!keys) {
2756              continue;
2757          }
2758          UpdatePSBTOutput(HidingSigningProvider(keys.get(), /*hide_secret=*/true, /*hide_origin=*/!bip32derivs), psbtx, i);
2759      }
2760  
2761      return {};
2762  }
2763  
2764  std::unique_ptr<CKeyMetadata> DescriptorScriptPubKeyMan::GetMetadata(const CTxDestination& dest) const
2765  {
2766      std::unique_ptr<SigningProvider> provider = GetSigningProvider(GetScriptForDestination(dest));
2767      if (provider) {
2768          KeyOriginInfo orig;
2769          CKeyID key_id = GetKeyForDestination(*provider, dest);
2770          if (provider->GetKeyOrigin(key_id, orig)) {
2771              LOCK(cs_desc_man);
2772              std::unique_ptr<CKeyMetadata> meta = std::make_unique<CKeyMetadata>();
2773              meta->key_origin = orig;
2774              meta->has_key_origin = true;
2775              meta->nCreateTime = m_wallet_descriptor.creation_time;
2776              return meta;
2777          }
2778      }
2779      return nullptr;
2780  }
2781  
2782  uint256 DescriptorScriptPubKeyMan::GetID() const
2783  {
2784      LOCK(cs_desc_man);
2785      return m_wallet_descriptor.id;
2786  }
2787  
2788  void DescriptorScriptPubKeyMan::SetCache(const DescriptorCache& cache)
2789  {
2790      LOCK(cs_desc_man);
2791      std::set<CScript> new_spks;
2792      m_wallet_descriptor.cache = cache;
2793      for (int32_t i = m_wallet_descriptor.range_start; i < m_wallet_descriptor.range_end; ++i) {
2794          FlatSigningProvider out_keys;
2795          std::vector<CScript> scripts_temp;
2796          if (!m_wallet_descriptor.descriptor->ExpandFromCache(i, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
2797              throw std::runtime_error("Error: Unable to expand wallet descriptor from cache");
2798          }
2799          // Add all of the scriptPubKeys to the scriptPubKey set
2800          new_spks.insert(scripts_temp.begin(), scripts_temp.end());
2801          for (const CScript& script : scripts_temp) {
2802              if (m_map_script_pub_keys.count(script) != 0) {
2803                  throw std::runtime_error(strprintf("Error: Already loaded script at index %d as being at index %d", i, m_map_script_pub_keys[script]));
2804              }
2805              m_map_script_pub_keys[script] = i;
2806          }
2807          for (const auto& pk_pair : out_keys.pubkeys) {
2808              const CPubKey& pubkey = pk_pair.second;
2809              if (m_map_pubkeys.count(pubkey) != 0) {
2810                  // We don't need to give an error here.
2811                  // 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
2812                  continue;
2813              }
2814              m_map_pubkeys[pubkey] = i;
2815          }
2816          m_max_cached_index++;
2817      }
2818      // Make sure the wallet knows about our new spks
2819      m_storage.TopUpCallback(new_spks, this);
2820  }
2821  
2822  bool DescriptorScriptPubKeyMan::AddKey(const CKeyID& key_id, const CKey& key)
2823  {
2824      LOCK(cs_desc_man);
2825      m_map_keys[key_id] = key;
2826      return true;
2827  }
2828  
2829  bool DescriptorScriptPubKeyMan::AddCryptedKey(const CKeyID& key_id, const CPubKey& pubkey, const std::vector<unsigned char>& crypted_key)
2830  {
2831      LOCK(cs_desc_man);
2832      if (!m_map_keys.empty()) {
2833          return false;
2834      }
2835  
2836      m_map_crypted_keys[key_id] = make_pair(pubkey, crypted_key);
2837      return true;
2838  }
2839  
2840  bool DescriptorScriptPubKeyMan::HasWalletDescriptor(const WalletDescriptor& desc) const
2841  {
2842      LOCK(cs_desc_man);
2843      return !m_wallet_descriptor.id.IsNull() && !desc.id.IsNull() && m_wallet_descriptor.id == desc.id;
2844  }
2845  
2846  void DescriptorScriptPubKeyMan::WriteDescriptor()
2847  {
2848      LOCK(cs_desc_man);
2849      WalletBatch batch(m_storage.GetDatabase());
2850      if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
2851          throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
2852      }
2853  }
2854  
2855  WalletDescriptor DescriptorScriptPubKeyMan::GetWalletDescriptor() const
2856  {
2857      return m_wallet_descriptor;
2858  }
2859  
2860  std::unordered_set<CScript, SaltedSipHasher> DescriptorScriptPubKeyMan::GetScriptPubKeys() const
2861  {
2862      return GetScriptPubKeys(0);
2863  }
2864  
2865  std::unordered_set<CScript, SaltedSipHasher> DescriptorScriptPubKeyMan::GetScriptPubKeys(int32_t minimum_index) const
2866  {
2867      LOCK(cs_desc_man);
2868      std::unordered_set<CScript, SaltedSipHasher> script_pub_keys;
2869      script_pub_keys.reserve(m_map_script_pub_keys.size());
2870  
2871      for (auto const& [script_pub_key, index] : m_map_script_pub_keys) {
2872          if (index >= minimum_index) script_pub_keys.insert(script_pub_key);
2873      }
2874      return script_pub_keys;
2875  }
2876  
2877  int32_t DescriptorScriptPubKeyMan::GetEndRange() const
2878  {
2879      return m_max_cached_index + 1;
2880  }
2881  
2882  bool DescriptorScriptPubKeyMan::GetDescriptorString(std::string& out, const bool priv) const
2883  {
2884      LOCK(cs_desc_man);
2885  
2886      FlatSigningProvider provider;
2887      provider.keys = GetKeys();
2888  
2889      if (priv) {
2890          // For the private version, always return the master key to avoid
2891          // exposing child private keys. The risk implications of exposing child
2892          // private keys together with the parent xpub may be non-obvious for users.
2893          return m_wallet_descriptor.descriptor->ToPrivateString(provider, out);
2894      }
2895  
2896      return m_wallet_descriptor.descriptor->ToNormalizedString(provider, out, &m_wallet_descriptor.cache);
2897  }
2898  
2899  void DescriptorScriptPubKeyMan::UpgradeDescriptorCache()
2900  {
2901      LOCK(cs_desc_man);
2902      if (m_storage.IsLocked() || m_storage.IsWalletFlagSet(WALLET_FLAG_LAST_HARDENED_XPUB_CACHED)) {
2903          return;
2904      }
2905  
2906      // Skip if we have the last hardened xpub cache
2907      if (m_wallet_descriptor.cache.GetCachedLastHardenedExtPubKeys().size() > 0) {
2908          return;
2909      }
2910  
2911      // Expand the descriptor
2912      FlatSigningProvider provider;
2913      provider.keys = GetKeys();
2914      FlatSigningProvider out_keys;
2915      std::vector<CScript> scripts_temp;
2916      DescriptorCache temp_cache;
2917      if (!m_wallet_descriptor.descriptor->Expand(0, provider, scripts_temp, out_keys, &temp_cache)){
2918          throw std::runtime_error("Unable to expand descriptor");
2919      }
2920  
2921      // Cache the last hardened xpubs
2922      DescriptorCache diff = m_wallet_descriptor.cache.MergeAndDiff(temp_cache);
2923      if (!WalletBatch(m_storage.GetDatabase()).WriteDescriptorCacheItems(GetID(), diff)) {
2924          throw std::runtime_error(std::string(__func__) + ": writing cache items failed");
2925      }
2926  }
2927  
2928  void DescriptorScriptPubKeyMan::UpdateWalletDescriptor(WalletDescriptor& descriptor)
2929  {
2930      LOCK(cs_desc_man);
2931      std::string error;
2932      if (!CanUpdateToWalletDescriptor(descriptor, error)) {
2933          throw std::runtime_error(std::string(__func__) + ": " + error);
2934      }
2935  
2936      m_map_pubkeys.clear();
2937      m_map_script_pub_keys.clear();
2938      m_max_cached_index = -1;
2939      m_wallet_descriptor = descriptor;
2940  
2941      NotifyFirstKeyTimeChanged(this, m_wallet_descriptor.creation_time);
2942  }
2943  
2944  bool DescriptorScriptPubKeyMan::CanUpdateToWalletDescriptor(const WalletDescriptor& descriptor, std::string& error)
2945  {
2946      LOCK(cs_desc_man);
2947      if (!HasWalletDescriptor(descriptor)) {
2948          error = "can only update matching descriptor";
2949          return false;
2950      }
2951  
2952      if (!descriptor.descriptor->IsRange()) {
2953          // Skip range check for non-range descriptors
2954          return true;
2955      }
2956  
2957      if (descriptor.range_start > m_wallet_descriptor.range_start ||
2958          descriptor.range_end < m_wallet_descriptor.range_end) {
2959          // Use inclusive range for error
2960          error = strprintf("new range must include current range = [%d,%d]",
2961                            m_wallet_descriptor.range_start,
2962                            m_wallet_descriptor.range_end - 1);
2963          return false;
2964      }
2965  
2966      return true;
2967  }
2968  } // namespace wallet
2969