LCOV - code coverage report
Current view: top level - src/wallet/rpc - backup.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 18 1249 1.4 %
Date: 2023-10-05 12:38:51 Functions: 9 58 15.5 %
Branches: 21 4483 0.5 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2009-2022 The Bitcoin Core developers
       2                 :            : // Distributed under the MIT software license, see the accompanying
       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :            : 
       5                 :            : #include <chain.h>
       6                 :            : #include <clientversion.h>
       7                 :            : #include <core_io.h>
       8                 :            : #include <hash.h>
       9                 :            : #include <interfaces/chain.h>
      10                 :            : #include <key_io.h>
      11                 :            : #include <merkleblock.h>
      12                 :            : #include <rpc/util.h>
      13                 :            : #include <script/descriptor.h>
      14                 :            : #include <script/script.h>
      15                 :            : #include <script/solver.h>
      16                 :            : #include <sync.h>
      17         [ +  - ]:          2 : #include <uint256.h>
      18         [ +  - ]:          2 : #include <util/bip32.h>
      19                 :            : #include <util/fs.h>
      20                 :            : #include <util/time.h>
      21                 :            : #include <util/translation.h>
      22                 :            : #include <wallet/rpc/util.h>
      23                 :            : #include <wallet/wallet.h>
      24                 :            : 
      25                 :            : #include <cstdint>
      26                 :            : #include <fstream>
      27                 :          2 : #include <tuple>
      28   [ +  -  +  -  :          6 : #include <string>
          +  -  -  +  #  
                      # ]
      29   [ +  -  +  -  :          2 : 
                   +  - ]
      30   [ +  -  +  -  :          2 : #include <univalue.h>
                   +  - ]
      31                 :            : 
      32                 :            : 
      33                 :            : 
      34                 :            : using interfaces::FoundBlock;
      35                 :            : 
      36                 :            : namespace wallet {
      37                 :          0 : std::string static EncodeDumpString(const std::string &str) {
      38                 :          0 :     std::stringstream ret;
      39         [ #  # ]:          0 :     for (const unsigned char c : str) {
      40   [ #  #  #  #  :          0 :         if (c <= 32 || c >= 128 || c == '%') {
                   #  # ]
      41   [ #  #  #  #  :          0 :             ret << '%' << HexStr({&c, 1});
                   #  # ]
      42                 :          0 :         } else {
      43         [ #  # ]:          0 :             ret << c;
      44                 :            :         }
      45                 :            :     }
      46         [ #  # ]:          0 :     return ret.str();
      47                 :          0 : }
      48                 :            : 
      49                 :          0 : static std::string DecodeDumpString(const std::string &str) {
      50                 :          0 :     std::stringstream ret;
      51         [ #  # ]:          0 :     for (unsigned int pos = 0; pos < str.length(); pos++) {
      52                 :          0 :         unsigned char c = str[pos];
      53   [ #  #  #  #  :          0 :         if (c == '%' && pos+2 < str.length()) {
                   #  # ]
      54                 :          0 :             c = (((str[pos+1]>>6)*9+((str[pos+1]-'0')&15)) << 4) |
      55                 :          0 :                 ((str[pos+2]>>6)*9+((str[pos+2]-'0')&15));
      56                 :          0 :             pos += 2;
      57                 :          0 :         }
      58         [ #  # ]:          0 :         ret << c;
      59                 :          0 :     }
      60         [ #  # ]:          0 :     return ret.str();
      61                 :          0 : }
      62                 :            : 
      63                 :          0 : static bool GetWalletAddressesForKey(const LegacyScriptPubKeyMan* spk_man, const CWallet& wallet, const CKeyID& keyid, std::string& strAddr, std::string& strLabel) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
      64                 :            : {
      65                 :          0 :     bool fLabelFound = false;
      66                 :          0 :     CKey key;
      67         [ #  # ]:          0 :     spk_man->GetKey(keyid, key);
      68   [ #  #  #  #  :          0 :     for (const auto& dest : GetAllDestinationsForKey(key.GetPubKey())) {
                   #  # ]
      69         [ #  # ]:          0 :         const auto* address_book_entry = wallet.FindAddressBookEntry(dest);
      70         [ #  # ]:          0 :         if (address_book_entry) {
      71         [ #  # ]:          0 :             if (!strAddr.empty()) {
      72         [ #  # ]:          0 :                 strAddr += ",";
      73                 :          0 :             }
      74   [ #  #  #  # ]:          2 :             strAddr += EncodeDestination(dest);
      75   [ #  #  #  # ]:          0 :             strLabel = EncodeDumpString(address_book_entry->GetLabel());
      76                 :          0 :             fLabelFound = true;
      77                 :          0 :         }
      78                 :            :     }
      79         [ #  # ]:          0 :     if (!fLabelFound) {
      80   [ #  #  #  #  :          0 :         strAddr = EncodeDestination(GetDestinationForKey(key.GetPubKey(), wallet.m_default_address_type));
                   #  # ]
      81                 :          0 :     }
      82                 :          0 :     return fLabelFound;
      83                 :          0 : }
      84                 :            : 
      85                 :            : static const int64_t TIMESTAMP_MIN = 0;
      86                 :            : 
      87                 :          0 : static void RescanWallet(CWallet& wallet, const WalletRescanReserver& reserver, int64_t time_begin = TIMESTAMP_MIN, bool update = true)
      88                 :            : {
      89                 :          0 :     int64_t scanned_time = wallet.RescanFromTime(time_begin, reserver, update);
      90         [ #  # ]:          0 :     if (wallet.IsAbortingRescan()) {
      91   [ #  #  #  #  :          2 :         throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");
                   #  # ]
      92         [ #  # ]:          0 :     } else if (scanned_time > time_begin) {
      93   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Rescan was unable to fully rescan the blockchain. Some transactions may be missing.");
                   #  # ]
      94                 :            :     }
      95                 :          0 : }
      96                 :            : 
      97                 :          0 : static void EnsureBlockDataFromTime(const CWallet& wallet, int64_t timestamp)
      98                 :            : {
      99                 :          2 :     auto& chain{wallet.chain()};
     100         [ #  # ]:          0 :     if (!chain.havePruned()) {
     101                 :          0 :         return;
     102                 :            :     }
     103                 :            : 
     104                 :          0 :     int height{0};
     105                 :          0 :     const bool found{chain.findFirstBlockWithTimeAndHeight(timestamp - TIMESTAMP_WINDOW, 0, FoundBlock().height(height))};
     106                 :            : 
     107         [ #  # ]:          0 :     uint256 tip_hash{WITH_LOCK(wallet.cs_wallet, return wallet.GetLastBlockHash())};
     108   [ #  #  #  # ]:          0 :     if (found && !chain.hasBlocks(tip_hash, height)) {
     109   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Pruned blocks from height %d required to import keys. Use RPC call getblockchaininfo to determine your pruned height.", height));
             #  #  #  # ]
     110                 :            :     }
     111                 :          0 : }
     112                 :            : 
     113                 :          0 : RPCHelpMan importprivkey()
     114                 :            : {
     115   [ #  #  #  #  :          0 :     return RPCHelpMan{"importprivkey",
                   #  # ]
     116         [ #  # ]:          0 :                 "\nAdds a private key (as returned by dumpprivkey) to your wallet. Requires a new wallet backup.\n"
     117                 :            :                 "Hint: use importmulti to import more than one private key.\n"
     118                 :            :             "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n"
     119                 :            :             "may report that the imported key exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n"
     120                 :            :             "The rescan parameter can be set to false if the key was never used to create transactions. If it is set to false,\n"
     121                 :            :             "but the key was used to create transactions, rescanblockchain needs to be called with the appropriate block range.\n"
     122                 :            :             "Note: Use \"getwalletinfo\" to query the scanning progress.\n"
     123                 :            :             "Note: This command is only compatible with legacy wallets. Use \"importdescriptors\" with \"combo(X)\" for descriptor wallets.\n",
     124         [ #  # ]:          0 :                 {
     125   [ #  #  #  #  :          0 :                     {"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key (see dumpprivkey)"},
                   #  # ]
     126   [ #  #  #  #  :          0 :                     {"label", RPCArg::Type::STR, RPCArg::DefaultHint{"current label if address exists, otherwise \"\""}, "An optional label"},
             #  #  #  # ]
     127   [ #  #  #  #  :          0 :                     {"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Scan the chain and mempool for wallet transactions."},
             #  #  #  # ]
     128                 :            :                 },
     129   [ #  #  #  #  :          0 :                 RPCResult{RPCResult::Type::NONE, "", ""},
             #  #  #  # ]
     130         [ #  # ]:          0 :                 RPCExamples{
     131                 :            :             "\nDump a private key\n"
     132   [ #  #  #  #  :          0 :             + HelpExampleCli("dumpprivkey", "\"myaddress\"") +
          #  #  #  #  #  
                      # ]
     133                 :            :             "\nImport the private key with rescan\n"
     134   [ #  #  #  #  :          0 :             + HelpExampleCli("importprivkey", "\"mykey\"") +
          #  #  #  #  #  
                      # ]
     135                 :            :             "\nImport using a label and without rescan\n"
     136   [ #  #  #  #  :          0 :             + HelpExampleCli("importprivkey", "\"mykey\" \"testing\" false") +
          #  #  #  #  #  
                      # ]
     137                 :            :             "\nImport using default blank label and without rescan\n"
     138   [ #  #  #  #  :          0 :             + HelpExampleCli("importprivkey", "\"mykey\" \"\" false") +
          #  #  #  #  #  
                      # ]
     139                 :            :             "\nAs a JSON-RPC call\n"
     140   [ #  #  #  #  :          0 :             + HelpExampleRpc("importprivkey", "\"mykey\", \"testing\", false")
             #  #  #  # ]
     141                 :            :                 },
     142         [ #  # ]:          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     143                 :            : {
     144                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
     145   [ #  #  #  #  :          0 :     if (!pwallet) return UniValue::VNULL;
                   #  # ]
     146                 :            : 
     147   [ #  #  #  # ]:          0 :     if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
     148   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
                   #  # ]
     149                 :            :     }
     150                 :          0 : 
     151         [ #  # ]:          0 :     EnsureLegacyScriptPubKeyMan(*pwallet, true);
     152                 :            : 
     153         [ #  # ]:          0 :     WalletRescanReserver reserver(*pwallet);
     154                 :          0 :     bool fRescan = true;
     155                 :            :     {
     156   [ #  #  #  # ]:          0 :         LOCK(pwallet->cs_wallet);
     157                 :            : 
     158         [ #  # ]:          0 :         EnsureWalletIsUnlocked(*pwallet);
     159                 :            : 
     160   [ #  #  #  #  :          0 :         std::string strSecret = request.params[0].get_str();
                   #  # ]
     161   [ #  #  #  # ]:          0 :         const std::string strLabel{LabelFromValue(request.params[1])};
     162                 :            : 
     163         [ +  - ]:          2 :         // Whether to perform rescan after import
     164   [ +  -  #  #  :          2 :         if (!request.params[2].isNull())
             #  #  #  # ]
     165   [ +  -  #  #  :          2 :             fRescan = request.params[2].get_bool();
                   #  # ]
     166         [ +  - ]:          2 : 
     167   [ +  -  #  #  :          2 :         if (fRescan && pwallet->chain().havePruned()) {
          #  #  #  #  #  
                      # ]
     168         [ +  - ]:          2 :             // Exit early and print an error.
     169         [ +  - ]:          2 :             // If a block is pruned after this check, we will import the key(s),
     170         [ +  - ]:          2 :             // but fail the rescan with a generic error.
     171   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned");
                   #  # ]
     172                 :            :         }
     173                 :            : 
     174   [ #  #  #  #  :          0 :         if (fRescan && !reserver.reserve()) {
                   #  # ]
     175   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
                   #  # ]
     176                 :            :         }
     177                 :            : 
     178         [ #  # ]:          0 :         CKey key = DecodeSecret(strSecret);
     179   [ #  #  #  #  :          0 :         if (!key.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
          #  #  #  #  #  
                      # ]
     180                 :            : 
     181         [ #  # ]:          0 :         CPubKey pubkey = key.GetPubKey();
     182   [ #  #  #  # ]:          0 :         CHECK_NONFATAL(key.VerifyPubKey(pubkey));
     183         [ #  # ]:          0 :         CKeyID vchAddress = pubkey.GetID();
     184                 :            :         {
     185         [ #  # ]:          0 :             pwallet->MarkDirty();
     186                 :            : 
     187                 :            :             // We don't know which corresponding address will be used;
     188                 :            :             // label all new addresses, and label existing addresses if a
     189                 :            :             // label was passed.
     190   [ #  #  #  # ]:          0 :             for (const auto& dest : GetAllDestinationsForKey(pubkey)) {
     191   [ #  #  #  #  :          0 :                 if (!request.params[1].isNull() || !pwallet->FindAddressBookEntry(dest)) {
          #  #  #  #  #  
                      # ]
     192   [ #  #  #  # ]:          0 :                     pwallet->SetAddressBook(dest, strLabel, AddressPurpose::RECEIVE);
     193                 :          0 :                 }
     194                 :            :             }
     195                 :            : 
     196                 :            :             // Use timestamp of 1 to scan the whole chain
     197   [ #  #  #  #  :          0 :             if (!pwallet->ImportPrivKeys({{vchAddress, key}}, 1)) {
             #  #  #  # ]
     198   [ #  #  #  #  :          0 :                 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
                   #  # ]
     199                 :            :             }
     200                 :            : 
     201                 :            :             // Add the wpkh script for this key if possible
     202   [ #  #  #  # ]:          0 :             if (pubkey.IsCompressed()) {
     203   [ #  #  #  #  :          0 :                 pwallet->ImportScripts({GetScriptForDestination(WitnessV0KeyHash(vchAddress))}, /*timestamp=*/0);
          #  #  #  #  #  
                      # ]
     204                 :          0 :             }
     205                 :            :         }
     206                 :          0 :     }
     207         [ #  # ]:          0 :     if (fRescan) {
     208         [ #  # ]:          0 :         RescanWallet(*pwallet, reserver);
     209                 :          0 :     }
     210                 :            : 
     211         [ #  # ]:          0 :     return UniValue::VNULL;
     212                 :          0 : },
     213                 :            :     };
     214                 :          0 : }
     215                 :            : 
     216                 :          0 : RPCHelpMan importaddress()
     217                 :            : {
     218   [ #  #  #  #  :          0 :     return RPCHelpMan{"importaddress",
                   #  # ]
     219         [ #  # ]:          0 :             "\nAdds an address or script (in hex) that can be watched as if it were in your wallet but cannot be used to spend. Requires a new wallet backup.\n"
     220                 :            :             "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n"
     221                 :            :             "may report that the imported address exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n"
     222                 :            :             "The rescan parameter can be set to false if the key was never used to create transactions. If it is set to false,\n"
     223                 :            :             "but the key was used to create transactions, rescanblockchain needs to be called with the appropriate block range.\n"
     224                 :            :             "If you have the full public key, you should call importpubkey instead of this.\n"
     225                 :            :             "Hint: use importmulti to import more than one address.\n"
     226                 :            :             "\nNote: If you import a non-standard raw script in hex form, outputs sending to it will be treated\n"
     227                 :            :             "as change, and not show up in many RPCs.\n"
     228                 :            :             "Note: Use \"getwalletinfo\" to query the scanning progress.\n"
     229                 :            :             "Note: This command is only compatible with legacy wallets. Use \"importdescriptors\" for descriptor wallets.\n",
     230         [ #  # ]:          0 :                 {
     231   [ #  #  #  #  :          0 :                     {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The Bitcoin address (or hex-encoded script)"},
                   #  # ]
     232   [ #  #  #  #  :          0 :                     {"label", RPCArg::Type::STR, RPCArg::Default{""}, "An optional label"},
             #  #  #  # ]
     233   [ #  #  #  #  :          0 :                     {"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Scan the chain and mempool for wallet transactions."},
             #  #  #  # ]
     234   [ #  #  #  #  :          0 :                     {"p2sh", RPCArg::Type::BOOL, RPCArg::Default{false}, "Add the P2SH version of the script as well"},
             #  #  #  # ]
     235                 :            :                 },
     236   [ #  #  #  #  :          0 :                 RPCResult{RPCResult::Type::NONE, "", ""},
             #  #  #  # ]
     237         [ #  # ]:          0 :                 RPCExamples{
     238                 :            :             "\nImport an address with rescan\n"
     239   [ #  #  #  #  :          0 :             + HelpExampleCli("importaddress", "\"myaddress\"") +
          #  #  #  #  #  
                      # ]
     240                 :            :             "\nImport using a label without rescan\n"
     241   [ #  #  #  #  :          0 :             + HelpExampleCli("importaddress", "\"myaddress\" \"testing\" false") +
          #  #  #  #  #  
                      # ]
     242                 :            :             "\nAs a JSON-RPC call\n"
     243   [ #  #  #  #  :          0 :             + HelpExampleRpc("importaddress", "\"myaddress\", \"testing\", false")
             #  #  #  # ]
     244                 :            :                 },
     245         [ #  # ]:          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     246                 :            : {
     247                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
     248   [ #  #  #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     249                 :            : 
     250         [ #  # ]:          0 :     EnsureLegacyScriptPubKeyMan(*pwallet, true);
     251                 :            : 
     252   [ #  #  #  # ]:          0 :     const std::string strLabel{LabelFromValue(request.params[1])};
     253                 :            : 
     254                 :            :     // Whether to perform rescan after import
     255                 :          0 :     bool fRescan = true;
     256   [ #  #  #  # ]:          0 :     if (!request.params[2].isNull())
     257   [ #  #  #  # ]:          0 :         fRescan = request.params[2].get_bool();
     258                 :            : 
     259   [ #  #  #  #  :          0 :     if (fRescan && pwallet->chain().havePruned()) {
                   #  # ]
     260                 :            :         // Exit early and print an error.
     261                 :            :         // If a block is pruned after this check, we will import the key(s),
     262                 :            :         // but fail the rescan with a generic error.
     263   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned");
                   #  # ]
     264         [ +  - ]:          2 :     }
     265                 :            : 
     266         [ #  # ]:          0 :     WalletRescanReserver reserver(*pwallet);
     267   [ #  #  #  # ]:          0 :     if (fRescan && !reserver.reserve()) {
     268   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
                   #  # ]
     269                 :            :     }
     270                 :            : 
     271                 :            :     // Whether to import a p2sh version, too
     272                 :          0 :     bool fP2SH = false;
     273   [ #  #  #  # ]:          0 :     if (!request.params[3].isNull())
     274   [ #  #  #  # ]:          0 :         fP2SH = request.params[3].get_bool();
     275                 :            : 
     276                 :            :     {
     277         [ #  # ]:          0 :         LOCK(pwallet->cs_wallet);
     278                 :            : 
     279   [ #  #  #  #  :          0 :         CTxDestination dest = DecodeDestination(request.params[0].get_str());
                   #  # ]
     280   [ #  #  #  # ]:          0 :         if (IsValidDestination(dest)) {
     281         [ #  # ]:          0 :             if (fP2SH) {
     282   [ #  #  #  #  :          0 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot use the p2sh flag with an address - use a script instead");
                   #  # ]
     283                 :            :             }
     284   [ #  #  #  #  :          0 :             if (OutputTypeFromDestination(dest) == OutputType::BECH32M) {
                   #  # ]
     285   [ #  #  #  #  :          0 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Bech32m addresses cannot be imported into legacy wallets");
                   #  # ]
     286                 :            :             }
     287                 :            : 
     288         [ #  # ]:          0 :             pwallet->MarkDirty();
     289                 :            : 
     290   [ #  #  #  #  :          0 :             pwallet->ImportScriptPubKeys(strLabel, {GetScriptForDestination(dest)}, /*have_solving_data=*/false, /*apply_label=*/true, /*timestamp=*/1);
                   #  # ]
     291   [ #  #  #  #  :          0 :         } else if (IsHex(request.params[0].get_str())) {
             #  #  #  # ]
     292   [ #  #  #  #  :          0 :             std::vector<unsigned char> data(ParseHex(request.params[0].get_str()));
                   #  # ]
     293         [ #  # ]:          0 :             CScript redeem_script(data.begin(), data.end());
     294                 :            : 
     295   [ #  #  #  # ]:          0 :             std::set<CScript> scripts = {redeem_script};
     296   [ #  #  #  # ]:          0 :             pwallet->ImportScripts(scripts, /*timestamp=*/0);
     297                 :            : 
     298         [ #  # ]:          0 :             if (fP2SH) {
     299   [ #  #  #  #  :          0 :                 scripts.insert(GetScriptForDestination(ScriptHash(redeem_script)));
                   #  # ]
     300                 :          0 :             }
     301                 :            : 
     302         [ #  # ]:          0 :             pwallet->ImportScriptPubKeys(strLabel, scripts, /*have_solving_data=*/false, /*apply_label=*/true, /*timestamp=*/1);
     303                 :          0 :         } else {
     304   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
                   #  # ]
     305                 :            :         }
     306                 :          0 :     }
     307         [ #  # ]:          0 :     if (fRescan)
     308                 :            :     {
     309         [ #  # ]:          0 :         RescanWallet(*pwallet, reserver);
     310         [ #  # ]:          0 :         pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
     311                 :          0 :     }
     312                 :            : 
     313         [ #  # ]:          0 :     return UniValue::VNULL;
     314                 :          0 : },
     315                 :            :     };
     316                 :          0 : }
     317                 :            : 
     318                 :          0 : RPCHelpMan importprunedfunds()
     319                 :            : {
     320   [ #  #  #  #  :          0 :     return RPCHelpMan{"importprunedfunds",
                   #  # ]
     321         [ #  # ]:          0 :                 "\nImports funds without rescan. Corresponding address or script must previously be included in wallet. Aimed towards pruned wallets. The end-user is responsible to import additional transactions that subsequently spend the imported outputs or rescan after the point in the blockchain the transaction is included.\n",
     322         [ #  # ]:          0 :                 {
     323   [ #  #  #  #  :          0 :                     {"rawtransaction", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "A raw transaction in hex funding an already-existing address in wallet"},
                   #  # ]
     324   [ #  #  #  #  :          0 :                     {"txoutproof", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex output from gettxoutproof that contains the transaction"},
                   #  # ]
     325                 :            :                 },
     326   [ #  #  #  #  :          0 :                 RPCResult{RPCResult::Type::NONE, "", ""},
             #  #  #  # ]
     327   [ #  #  #  # ]:          0 :                 RPCExamples{""},
     328         [ #  # ]:          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     329                 :            : {
     330                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
     331   [ #  #  #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     332                 :            : 
     333         [ #  # ]:          0 :     CMutableTransaction tx;
     334   [ #  #  #  #  :          0 :     if (!DecodeHexTx(tx, request.params[0].get_str())) {
             #  #  #  # ]
     335   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed. Make sure the tx has at least one input.");
                   #  # ]
     336                 :            :     }
     337         [ #  # ]:          0 :     uint256 hashTx = tx.GetHash();
     338                 :            : 
     339   [ #  #  #  #  :          0 :     DataStream ssMB{ParseHexV(request.params[1], "proof")};
          #  #  #  #  #  
                      # ]
     340         [ #  # ]:          0 :     CMerkleBlock merkleBlock;
     341         [ #  # ]:          0 :     ssMB >> merkleBlock;
     342                 :            : 
     343                 :            :     //Search partial merkle tree in proof for our transaction and index in valid block
     344                 :          0 :     std::vector<uint256> vMatch;
     345                 :          0 :     std::vector<unsigned int> vIndex;
     346   [ #  #  #  #  :          0 :     if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot) {
                   #  # ]
     347   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Something wrong with merkleblock");
                   #  # ]
     348                 :            :     }
     349                 :            : 
     350         [ #  # ]:          0 :     LOCK(pwallet->cs_wallet);
     351                 :            :     int height;
     352   [ #  #  #  #  :          0 :     if (!pwallet->chain().findAncestorByHash(pwallet->GetLastBlockHash(), merkleBlock.header.GetHash(), FoundBlock().height(height))) {
          #  #  #  #  #  
                      # ]
     353   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
                   #  # ]
     354                 :            :     }
     355                 :            : 
     356                 :          0 :     std::vector<uint256>::const_iterator it;
     357   [ #  #  #  # ]:          0 :     if ((it = std::find(vMatch.begin(), vMatch.end(), hashTx)) == vMatch.end()) {
     358   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction given doesn't exist in proof");
                   #  # ]
     359                 :            :     }
     360                 :            : 
     361                 :          0 :     unsigned int txnIndex = vIndex[it - vMatch.begin()];
     362                 :            : 
     363         [ #  # ]:          0 :     CTransactionRef tx_ref = MakeTransactionRef(tx);
     364   [ #  #  #  # ]:          0 :     if (pwallet->IsMine(*tx_ref)) {
     365   [ #  #  #  #  :          0 :         pwallet->AddToWallet(std::move(tx_ref), TxStateConfirmed{merkleBlock.header.GetHash(), height, static_cast<int>(txnIndex)});
                   #  # ]
     366         [ #  # ]:          0 :         return UniValue::VNULL;
     367                 :            :     }
     368                 :            : 
     369   [ #  #  #  #  :          0 :     throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No addresses in wallet correspond to included transaction");
                   #  # ]
     370                 :          0 : },
     371                 :            :     };
     372                 :          0 : }
     373                 :            : 
     374                 :          0 : RPCHelpMan removeprunedfunds()
     375                 :            : {
     376   [ #  #  #  #  :          0 :     return RPCHelpMan{"removeprunedfunds",
                   #  # ]
     377         [ #  # ]:          0 :                 "\nDeletes the specified transaction from the wallet. Meant for use with pruned wallets and as a companion to importprunedfunds. This will affect wallet balances.\n",
     378         [ #  # ]:          0 :                 {
     379   [ #  #  #  #  :          0 :                     {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex-encoded id of the transaction you are deleting"},
                   #  # ]
     380                 :            :                 },
     381   [ #  #  #  #  :          0 :                 RPCResult{RPCResult::Type::NONE, "", ""},
             #  #  #  # ]
     382         [ #  # ]:          0 :                 RPCExamples{
     383   [ #  #  #  #  :          0 :                     HelpExampleCli("removeprunedfunds", "\"a8d0c0184dde994a09ec054286f1ce581bebf46446a512166eae7628734ea0a5\"") +
             #  #  #  # ]
     384                 :            :             "\nAs a JSON-RPC call\n"
     385   [ #  #  #  #  :          0 :             + HelpExampleRpc("removeprunedfunds", "\"a8d0c0184dde994a09ec054286f1ce581bebf46446a512166eae7628734ea0a5\"")
             #  #  #  # ]
     386                 :            :                 },
     387         [ #  # ]:          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     388                 :            : {
     389                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
     390   [ #  #  #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     391                 :            : 
     392         [ #  # ]:          0 :     LOCK(pwallet->cs_wallet);
     393                 :            : 
     394   [ #  #  #  #  :          0 :     uint256 hash(ParseHashV(request.params[0], "txid"));
                   #  # ]
     395                 :          0 :     std::vector<uint256> vHash;
     396         [ #  # ]:          0 :     vHash.push_back(hash);
     397                 :          0 :     std::vector<uint256> vHashOut;
     398                 :            : 
     399   [ #  #  #  # ]:          0 :     if (pwallet->ZapSelectTx(vHash, vHashOut) != DBErrors::LOAD_OK) {
     400   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Could not properly delete the transaction.");
                   #  # ]
     401                 :            :     }
     402                 :            : 
     403         [ #  # ]:          0 :     if(vHashOut.empty()) {
     404   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Transaction does not exist in wallet.");
                   #  # ]
     405                 :            :     }
     406                 :            : 
     407         [ #  # ]:          0 :     return UniValue::VNULL;
     408                 :          0 : },
     409                 :            :     };
     410                 :          0 : }
     411                 :            : 
     412                 :          0 : RPCHelpMan importpubkey()
     413                 :            : {
     414   [ #  #  #  #  :          0 :     return RPCHelpMan{"importpubkey",
                   #  # ]
     415         [ #  # ]:          0 :                 "\nAdds a public key (in hex) that can be watched as if it were in your wallet but cannot be used to spend. Requires a new wallet backup.\n"
     416                 :            :                 "Hint: use importmulti to import more than one public key.\n"
     417                 :            :             "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n"
     418                 :            :             "may report that the imported pubkey exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n"
     419                 :            :             "The rescan parameter can be set to false if the key was never used to create transactions. If it is set to false,\n"
     420                 :            :             "but the key was used to create transactions, rescanblockchain needs to be called with the appropriate block range.\n"
     421                 :            :             "Note: Use \"getwalletinfo\" to query the scanning progress.\n"
     422                 :            :             "Note: This command is only compatible with legacy wallets. Use \"importdescriptors\" with \"combo(X)\" for descriptor wallets.\n",
     423         [ #  # ]:          0 :                 {
     424   [ #  #  #  #  :          0 :                     {"pubkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The hex-encoded public key"},
                   #  # ]
     425   [ #  #  #  #  :          0 :                     {"label", RPCArg::Type::STR, RPCArg::Default{""}, "An optional label"},
             #  #  #  # ]
     426   [ #  #  #  #  :          0 :                     {"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Scan the chain and mempool for wallet transactions."},
             #  #  #  # ]
     427                 :            :                 },
     428   [ #  #  #  #  :          0 :                 RPCResult{RPCResult::Type::NONE, "", ""},
             #  #  #  # ]
     429         [ #  # ]:          0 :                 RPCExamples{
     430                 :            :             "\nImport a public key with rescan\n"
     431   [ #  #  #  #  :          0 :             + HelpExampleCli("importpubkey", "\"mypubkey\"") +
          #  #  #  #  #  
                      # ]
     432                 :            :             "\nImport using a label without rescan\n"
     433   [ #  #  #  #  :          0 :             + HelpExampleCli("importpubkey", "\"mypubkey\" \"testing\" false") +
          #  #  #  #  #  
                      # ]
     434                 :            :             "\nAs a JSON-RPC call\n"
     435   [ #  #  #  #  :          0 :             + HelpExampleRpc("importpubkey", "\"mypubkey\", \"testing\", false")
             #  #  #  # ]
     436                 :            :                 },
     437         [ #  # ]:          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     438                 :            : {
     439                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
     440   [ #  #  #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     441                 :            : 
     442         [ #  # ]:          0 :     EnsureLegacyScriptPubKeyMan(*pwallet, true);
     443                 :            : 
     444   [ #  #  #  # ]:          0 :     const std::string strLabel{LabelFromValue(request.params[1])};
     445                 :            : 
     446                 :            :     // Whether to perform rescan after import
     447                 :          0 :     bool fRescan = true;
     448   [ #  #  #  # ]:          0 :     if (!request.params[2].isNull())
     449   [ #  #  #  # ]:          0 :         fRescan = request.params[2].get_bool();
     450                 :            : 
     451   [ #  #  #  #  :          0 :     if (fRescan && pwallet->chain().havePruned()) {
                   #  # ]
     452                 :            :         // Exit early and print an error.
     453                 :            :         // If a block is pruned after this check, we will import the key(s),
     454                 :            :         // but fail the rescan with a generic error.
     455   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned");
                   #  # ]
     456                 :            :     }
     457                 :            : 
     458         [ #  # ]:          0 :     WalletRescanReserver reserver(*pwallet);
     459   [ #  #  #  # ]:          0 :     if (fRescan && !reserver.reserve()) {
     460   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
                   #  # ]
     461                 :            :     }
     462                 :            : 
     463   [ #  #  #  #  :          0 :     if (!IsHex(request.params[0].get_str()))
             #  #  #  # ]
     464   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey must be a hex string");
                   #  # ]
     465   [ #  #  #  #  :          0 :     std::vector<unsigned char> data(ParseHex(request.params[0].get_str()));
                   #  # ]
     466   [ #  #  #  # ]:          0 :     CPubKey pubKey(data);
     467   [ #  #  #  # ]:          0 :     if (!pubKey.IsFullyValid())
     468   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey is not a valid public key");
                   #  # ]
     469                 :            : 
     470                 :            :     {
     471         [ #  # ]:          0 :         LOCK(pwallet->cs_wallet);
     472                 :            : 
     473                 :          0 :         std::set<CScript> script_pub_keys;
     474   [ #  #  #  # ]:          0 :         for (const auto& dest : GetAllDestinationsForKey(pubKey)) {
     475   [ #  #  #  # ]:          0 :             script_pub_keys.insert(GetScriptForDestination(dest));
     476                 :            :         }
     477                 :            : 
     478         [ #  # ]:          0 :         pwallet->MarkDirty();
     479                 :            : 
     480         [ #  # ]:          0 :         pwallet->ImportScriptPubKeys(strLabel, script_pub_keys, /*have_solving_data=*/true, /*apply_label=*/true, /*timestamp=*/1);
     481                 :            : 
     482   [ #  #  #  #  :          0 :         pwallet->ImportPubKeys({pubKey.GetID()}, {{pubKey.GetID(), pubKey}} , /*key_origins=*/{}, /*add_keypool=*/false, /*internal=*/false, /*timestamp=*/1);
          #  #  #  #  #  
                #  #  # ]
     483                 :          0 :     }
     484         [ #  # ]:          0 :     if (fRescan)
     485                 :            :     {
     486         [ #  # ]:          0 :         RescanWallet(*pwallet, reserver);
     487         [ #  # ]:          0 :         pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
     488                 :          0 :     }
     489                 :            : 
     490         [ #  # ]:          0 :     return UniValue::VNULL;
     491                 :          0 : },
     492                 :            :     };
     493                 :          0 : }
     494                 :            : 
     495                 :            : 
     496                 :          0 : RPCHelpMan importwallet()
     497                 :            : {
     498   [ #  #  #  #  :          0 :     return RPCHelpMan{"importwallet",
                   #  # ]
     499         [ #  # ]:          0 :                 "\nImports keys from a wallet dump file (see dumpwallet). Requires a new wallet backup to include imported keys.\n"
     500                 :            :                 "Note: Blockchain and Mempool will be rescanned after a successful import. Use \"getwalletinfo\" to query the scanning progress.\n"
     501                 :            :                 "Note: This command is only compatible with legacy wallets.\n",
     502         [ #  # ]:          0 :                 {
     503   [ #  #  #  #  :          0 :                     {"filename", RPCArg::Type::STR, RPCArg::Optional::NO, "The wallet file"},
                   #  # ]
     504                 :            :                 },
     505   [ #  #  #  #  :          0 :                 RPCResult{RPCResult::Type::NONE, "", ""},
             #  #  #  # ]
     506         [ #  # ]:          0 :                 RPCExamples{
     507                 :            :             "\nDump the wallet\n"
     508   [ #  #  #  #  :          0 :             + HelpExampleCli("dumpwallet", "\"test\"") +
          #  #  #  #  #  
                      # ]
     509                 :            :             "\nImport the wallet\n"
     510   [ #  #  #  #  :          0 :             + HelpExampleCli("importwallet", "\"test\"") +
          #  #  #  #  #  
                      # ]
     511                 :            :             "\nImport using the json rpc call\n"
     512   [ #  #  #  #  :          0 :             + HelpExampleRpc("importwallet", "\"test\"")
             #  #  #  # ]
     513                 :            :                 },
     514         [ #  # ]:          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     515                 :            : {
     516                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
     517   [ #  #  #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     518                 :            : 
     519         [ #  # ]:          0 :     EnsureLegacyScriptPubKeyMan(*pwallet, true);
     520                 :            : 
     521         [ #  # ]:          0 :     WalletRescanReserver reserver(*pwallet);
     522         [ #  # ]:          0 :     if (!reserver.reserve()) {
     523   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
                   #  # ]
     524                 :            :     }
     525                 :            : 
     526                 :          0 :     int64_t nTimeBegin = 0;
     527                 :          0 :     bool fGood = true;
     528                 :            :     {
     529         [ #  # ]:          0 :         LOCK(pwallet->cs_wallet);
     530                 :            : 
     531         [ #  # ]:          0 :         EnsureWalletIsUnlocked(*pwallet);
     532                 :            : 
     533         [ #  # ]:          0 :         std::ifstream file;
     534   [ #  #  #  #  :          0 :         file.open(fs::u8path(request.params[0].get_str()), std::ios::in | std::ios::ate);
          #  #  #  #  #  
                      # ]
     535   [ #  #  #  # ]:          0 :         if (!file.is_open()) {
     536   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
                   #  # ]
     537                 :            :         }
     538   [ #  #  #  #  :          0 :         CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(nTimeBegin)));
             #  #  #  # ]
     539                 :            : 
     540   [ #  #  #  #  :          0 :         int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg());
                   #  # ]
     541         [ #  # ]:          0 :         file.seekg(0, file.beg);
     542                 :            : 
     543                 :            :         // Use uiInterface.ShowProgress instead of pwallet.ShowProgress because pwallet.ShowProgress has a cancel button tied to AbortRescan which
     544                 :            :         // we don't want for this progress bar showing the import progress. uiInterface.ShowProgress does not have a cancel button.
     545   [ #  #  #  #  :          0 :         pwallet->chain().showProgress(strprintf("%s " + _("Importing…").translated, pwallet->GetDisplayName()), 0, false); // show progress dialog in GUI
          #  #  #  #  #  
                      # ]
     546                 :          0 :         std::vector<std::tuple<CKey, int64_t, bool, std::string>> keys;
     547                 :          0 :         std::vector<std::pair<CScript, int64_t>> scripts;
     548   [ #  #  #  # ]:          0 :         while (file.good()) {
     549   [ #  #  #  #  :          0 :             pwallet->chain().showProgress("", std::max(1, std::min(50, (int)(((double)file.tellg() / (double)nFilesize) * 100))), false);
          #  #  #  #  #  
                #  #  # ]
     550                 :          0 :             std::string line;
     551         [ #  # ]:          0 :             std::getline(file, line);
     552   [ #  #  #  #  :          0 :             if (line.empty() || line[0] == '#')
                   #  # ]
     553                 :          0 :                 continue;
     554                 :            : 
     555         [ #  # ]:          0 :             std::vector<std::string> vstr = SplitString(line, ' ');
     556         [ #  # ]:          0 :             if (vstr.size() < 2)
     557                 :          0 :                 continue;
     558         [ #  # ]:          0 :             CKey key = DecodeSecret(vstr[0]);
     559         [ #  # ]:          0 :             if (key.IsValid()) {
     560         [ #  # ]:          0 :                 int64_t nTime = ParseISO8601DateTime(vstr[1]);
     561                 :          0 :                 std::string strLabel;
     562                 :          0 :                 bool fLabel = true;
     563         [ #  # ]:          0 :                 for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
     564         [ #  # ]:          0 :                     if (vstr[nStr].front() == '#')
     565                 :          0 :                         break;
     566   [ #  #  #  # ]:          0 :                     if (vstr[nStr] == "change=1")
     567                 :          0 :                         fLabel = false;
     568   [ #  #  #  # ]:          0 :                     if (vstr[nStr] == "reserve=1")
     569                 :          0 :                         fLabel = false;
     570   [ #  #  #  #  :          0 :                     if (vstr[nStr].substr(0,6) == "label=") {
                   #  # ]
     571   [ #  #  #  # ]:          0 :                         strLabel = DecodeDumpString(vstr[nStr].substr(6));
     572                 :          0 :                         fLabel = true;
     573                 :          0 :                     }
     574                 :          0 :                 }
     575         [ #  # ]:          0 :                 nTimeBegin = std::min(nTimeBegin, nTime);
     576   [ #  #  #  # ]:          0 :                 keys.push_back(std::make_tuple(key, nTime, fLabel, strLabel));
     577   [ #  #  #  # ]:          0 :             } else if(IsHex(vstr[0])) {
     578         [ #  # ]:          0 :                 std::vector<unsigned char> vData(ParseHex(vstr[0]));
     579         [ #  # ]:          0 :                 CScript script = CScript(vData.begin(), vData.end());
     580         [ #  # ]:          0 :                 int64_t birth_time = ParseISO8601DateTime(vstr[1]);
     581   [ #  #  #  # ]:          0 :                 if (birth_time > 0) nTimeBegin = std::min(nTimeBegin, birth_time);
     582   [ #  #  #  # ]:          0 :                 scripts.push_back(std::pair<CScript, int64_t>(script, birth_time));
     583                 :          0 :             }
     584         [ #  # ]:          0 :         }
     585         [ #  # ]:          0 :         file.close();
     586         [ #  # ]:          0 :         EnsureBlockDataFromTime(*pwallet, nTimeBegin);
     587                 :            :         // We now know whether we are importing private keys, so we can error if private keys are disabled
     588   [ #  #  #  #  :          0 :         if (keys.size() > 0 && pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
                   #  # ]
     589   [ #  #  #  # ]:          0 :             pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
     590   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Importing wallets is disabled when private keys are disabled");
                   #  # ]
     591                 :            :         }
     592                 :          0 :         double total = (double)(keys.size() + scripts.size());
     593                 :          0 :         double progress = 0;
     594         [ #  # ]:          0 :         for (const auto& key_tuple : keys) {
     595   [ #  #  #  #  :          0 :             pwallet->chain().showProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
             #  #  #  # ]
     596                 :          0 :             const CKey& key = std::get<0>(key_tuple);
     597                 :          0 :             int64_t time = std::get<1>(key_tuple);
     598                 :          0 :             bool has_label = std::get<2>(key_tuple);
     599         [ #  # ]:          0 :             std::string label = std::get<3>(key_tuple);
     600                 :            : 
     601         [ #  # ]:          0 :             CPubKey pubkey = key.GetPubKey();
     602   [ #  #  #  # ]:          0 :             CHECK_NONFATAL(key.VerifyPubKey(pubkey));
     603         [ #  # ]:          0 :             CKeyID keyid = pubkey.GetID();
     604                 :            : 
     605   [ #  #  #  #  :          0 :             pwallet->WalletLogPrintf("Importing %s...\n", EncodeDestination(PKHash(keyid)));
                   #  # ]
     606                 :            : 
     607   [ #  #  #  #  :          0 :             if (!pwallet->ImportPrivKeys({{keyid, key}}, time)) {
             #  #  #  # ]
     608   [ #  #  #  #  :          0 :                 pwallet->WalletLogPrintf("Error importing key for %s\n", EncodeDestination(PKHash(keyid)));
                   #  # ]
     609                 :          0 :                 fGood = false;
     610                 :          0 :                 continue;
     611                 :            :             }
     612                 :            : 
     613         [ #  # ]:          0 :             if (has_label)
     614   [ #  #  #  #  :          0 :                 pwallet->SetAddressBook(PKHash(keyid), label, AddressPurpose::RECEIVE);
                   #  # ]
     615                 :          0 :             progress++;
     616         [ #  # ]:          0 :         }
     617         [ #  # ]:          0 :         for (const auto& script_pair : scripts) {
     618   [ #  #  #  #  :          0 :             pwallet->chain().showProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
             #  #  #  # ]
     619                 :          0 :             const CScript& script = script_pair.first;
     620                 :          0 :             int64_t time = script_pair.second;
     621                 :            : 
     622   [ #  #  #  #  :          0 :             if (!pwallet->ImportScripts({script}, time)) {
             #  #  #  # ]
     623   [ #  #  #  #  :          0 :                 pwallet->WalletLogPrintf("Error importing script %s\n", HexStr(script));
                   #  # ]
     624                 :          0 :                 fGood = false;
     625                 :          0 :                 continue;
     626                 :            :             }
     627                 :            : 
     628                 :          0 :             progress++;
     629                 :            :         }
     630   [ #  #  #  # ]:          0 :         pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
     631                 :          0 :     }
     632   [ #  #  #  # ]:          0 :     pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
     633         [ #  # ]:          0 :     RescanWallet(*pwallet, reserver, nTimeBegin, /*update=*/false);
     634         [ #  # ]:          0 :     pwallet->MarkDirty();
     635                 :            : 
     636         [ #  # ]:          0 :     if (!fGood)
     637   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys/scripts to wallet");
                   #  # ]
     638                 :            : 
     639         [ #  # ]:          0 :     return UniValue::VNULL;
     640                 :          0 : },
     641                 :            :     };
     642                 :          0 : }
     643                 :            : 
     644                 :          0 : RPCHelpMan dumpprivkey()
     645                 :            : {
     646   [ #  #  #  #  :          0 :     return RPCHelpMan{"dumpprivkey",
                   #  # ]
     647         [ #  # ]:          0 :                 "\nReveals the private key corresponding to 'address'.\n"
     648                 :            :                 "Then the importprivkey can be used with this output\n"
     649                 :            :                 "Note: This command is only compatible with legacy wallets.\n",
     650         [ #  # ]:          0 :                 {
     651   [ #  #  #  #  :          0 :                     {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address for the private key"},
                   #  # ]
     652                 :            :                 },
     653   [ #  #  #  # ]:          0 :                 RPCResult{
     654   [ #  #  #  # ]:          0 :                     RPCResult::Type::STR, "key", "The private key"
     655                 :            :                 },
     656         [ #  # ]:          0 :                 RPCExamples{
     657   [ #  #  #  #  :          0 :                     HelpExampleCli("dumpprivkey", "\"myaddress\"")
                   #  # ]
     658   [ #  #  #  #  :          0 :             + HelpExampleCli("importprivkey", "\"mykey\"")
             #  #  #  # ]
     659   [ #  #  #  #  :          0 :             + HelpExampleRpc("dumpprivkey", "\"myaddress\"")
             #  #  #  # ]
     660                 :            :                 },
     661         [ #  # ]:          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     662                 :            : {
     663                 :          0 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
     664   [ #  #  #  #  :          0 :     if (!pwallet) return UniValue::VNULL;
                   #  # ]
     665                 :            : 
     666         [ #  # ]:          0 :     const LegacyScriptPubKeyMan& spk_man = EnsureConstLegacyScriptPubKeyMan(*pwallet);
     667                 :            : 
     668   [ #  #  #  # ]:          0 :     LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
     669                 :            : 
     670         [ #  # ]:          0 :     EnsureWalletIsUnlocked(*pwallet);
     671                 :            : 
     672   [ #  #  #  #  :          0 :     std::string strAddress = request.params[0].get_str();
                   #  # ]
     673         [ #  # ]:          0 :     CTxDestination dest = DecodeDestination(strAddress);
     674   [ #  #  #  # ]:          0 :     if (!IsValidDestination(dest)) {
     675   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
                   #  # ]
     676                 :            :     }
     677         [ #  # ]:          0 :     auto keyid = GetKeyForDestination(spk_man, dest);
     678   [ #  #  #  # ]:          0 :     if (keyid.IsNull()) {
     679   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
                   #  # ]
     680                 :            :     }
     681                 :          0 :     CKey vchSecret;
     682   [ #  #  #  # ]:          0 :     if (!spk_man.GetKey(keyid, vchSecret)) {
     683   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
             #  #  #  # ]
     684                 :            :     }
     685   [ #  #  #  # ]:          0 :     return EncodeSecret(vchSecret);
     686                 :          0 : },
     687                 :            :     };
     688                 :          0 : }
     689                 :            : 
     690                 :            : 
     691                 :          0 : RPCHelpMan dumpwallet()
     692                 :            : {
     693   [ #  #  #  #  :          0 :     return RPCHelpMan{"dumpwallet",
             #  #  #  # ]
     694         [ #  # ]:          0 :                 "\nDumps all wallet keys in a human-readable format to a server-side file. This does not allow overwriting existing files.\n"
     695                 :            :                 "Imported scripts are included in the dumpfile, but corresponding BIP173 addresses, etc. may not be added automatically by importwallet.\n"
     696                 :            :                 "Note that if your wallet contains keys which are not derived from your HD seed (e.g. imported keys), these are not covered by\n"
     697                 :            :                 "only backing up the seed itself, and must be backed up too (e.g. ensure you back up the whole dumpfile).\n"
     698                 :            :                 "Note: This command is only compatible with legacy wallets.\n",
     699         [ #  # ]:          0 :                 {
     700   [ #  #  #  #  :          0 :                     {"filename", RPCArg::Type::STR, RPCArg::Optional::NO, "The filename with path (absolute path recommended)"},
                   #  # ]
     701                 :            :                 },
     702   [ #  #  #  # ]:          0 :                 RPCResult{
     703   [ #  #  #  # ]:          0 :                     RPCResult::Type::OBJ, "", "",
     704         [ #  # ]:          0 :                     {
     705   [ #  #  #  #  :          0 :                         {RPCResult::Type::STR, "filename", "The filename with full absolute path"},
                   #  # ]
     706                 :            :                     }
     707                 :            :                 },
     708         [ #  # ]:          0 :                 RPCExamples{
     709   [ #  #  #  #  :          0 :                     HelpExampleCli("dumpwallet", "\"test\"")
                   #  # ]
     710   [ #  #  #  #  :          0 :             + HelpExampleRpc("dumpwallet", "\"test\"")
             #  #  #  # ]
     711                 :            :                 },
     712         [ #  # ]:          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     713                 :            : {
     714                 :          0 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
     715   [ #  #  #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
     716                 :            : 
     717                 :          0 :     const CWallet& wallet = *pwallet;
     718         [ #  # ]:          0 :     const LegacyScriptPubKeyMan& spk_man = EnsureConstLegacyScriptPubKeyMan(wallet);
     719                 :            : 
     720                 :            :     // Make sure the results are valid at least up to the most recent block
     721                 :            :     // the user could have gotten from another RPC command prior to now
     722         [ #  # ]:          0 :     wallet.BlockUntilSyncedToCurrentChain();
     723                 :            : 
     724         [ #  # ]:          0 :     LOCK(wallet.cs_wallet);
     725                 :            : 
     726         [ #  # ]:          0 :     EnsureWalletIsUnlocked(wallet);
     727                 :            : 
     728   [ #  #  #  #  :          0 :     fs::path filepath = fs::u8path(request.params[0].get_str());
                   #  # ]
     729         [ #  # ]:          0 :     filepath = fs::absolute(filepath);
     730                 :            : 
     731                 :            :     /* Prevent arbitrary files from being overwritten. There have been reports
     732                 :            :      * that users have overwritten wallet files this way:
     733                 :            :      * https://github.com/bitcoin/bitcoin/issues/9934
     734                 :            :      * It may also avoid other security issues.
     735                 :            :      */
     736   [ #  #  #  # ]:          0 :     if (fs::exists(filepath)) {
     737   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, filepath.u8string() + " already exists. If you are sure this is what you want, move it out of the way first");
             #  #  #  # ]
     738                 :            :     }
     739                 :            : 
     740         [ #  # ]:          0 :     std::ofstream file;
     741         [ #  # ]:          0 :     file.open(filepath);
     742   [ #  #  #  # ]:          0 :     if (!file.is_open())
     743   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
                   #  # ]
     744                 :            : 
     745                 :          0 :     std::map<CKeyID, int64_t> mapKeyBirth;
     746         [ #  # ]:          0 :     wallet.GetKeyBirthTimes(mapKeyBirth);
     747                 :            : 
     748                 :          0 :     int64_t block_time = 0;
     749   [ #  #  #  #  :          0 :     CHECK_NONFATAL(wallet.chain().findBlock(wallet.GetLastBlockHash(), FoundBlock().time(block_time)));
                   #  # ]
     750                 :            : 
     751                 :            :     // Note: To avoid a lock order issue, access to cs_main must be locked before cs_KeyStore.
     752                 :            :     // So we do the two things in this function that lock cs_main first: GetKeyBirthTimes, and findBlock.
     753         [ #  # ]:          0 :     LOCK(spk_man.cs_KeyStore);
     754                 :            : 
     755         [ #  # ]:          0 :     const std::map<CKeyID, int64_t>& mapKeyPool = spk_man.GetAllReserveKeys();
     756         [ #  # ]:          0 :     std::set<CScriptID> scripts = spk_man.GetCScripts();
     757                 :            : 
     758                 :            :     // sort time/key pairs
     759                 :          0 :     std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
     760         [ #  # ]:          0 :     vKeyBirth.reserve(mapKeyBirth.size());
     761         [ #  # ]:          0 :     for (const auto& entry : mapKeyBirth) {
     762   [ #  #  #  # ]:          0 :         vKeyBirth.push_back(std::make_pair(entry.second, entry.first));
     763                 :            :     }
     764                 :          0 :     mapKeyBirth.clear();
     765         [ #  # ]:          0 :     std::sort(vKeyBirth.begin(), vKeyBirth.end());
     766                 :            : 
     767                 :            :     // produce output
     768   [ #  #  #  #  :          0 :     file << strprintf("# Wallet dump created by %s %s\n", PACKAGE_NAME, FormatFullVersion());
                   #  # ]
     769   [ #  #  #  #  :          0 :     file << strprintf("# * Created on %s\n", FormatISO8601DateTime(GetTime()));
             #  #  #  # ]
     770   [ #  #  #  #  :          0 :     file << strprintf("# * Best block at time of backup was %i (%s),\n", wallet.GetLastBlockHeight(), wallet.GetLastBlockHash().ToString());
          #  #  #  #  #  
                      # ]
     771   [ #  #  #  #  :          0 :     file << strprintf("#   mined on %s\n", FormatISO8601DateTime(block_time));
                   #  # ]
     772         [ #  # ]:          0 :     file << "\n";
     773                 :            : 
     774                 :            :     // add the base58check encoded extended master if the wallet uses HD
     775         [ #  # ]:          0 :     CKeyID seed_id = spk_man.GetHDChain().seed_id;
     776   [ #  #  #  # ]:          0 :     if (!seed_id.IsNull())
     777                 :            :     {
     778                 :          0 :         CKey seed;
     779   [ #  #  #  # ]:          0 :         if (spk_man.GetKey(seed_id, seed)) {
     780         [ #  # ]:          0 :             CExtKey masterKey;
     781   [ #  #  #  # ]:          0 :             masterKey.SetSeed(seed);
     782                 :            : 
     783   [ #  #  #  #  :          0 :             file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n\n";
             #  #  #  # ]
     784                 :          0 :         }
     785                 :          0 :     }
     786         [ #  # ]:          0 :     for (std::vector<std::pair<int64_t, CKeyID> >::const_iterator it = vKeyBirth.begin(); it != vKeyBirth.end(); it++) {
     787                 :          0 :         const CKeyID &keyid = it->second;
     788         [ #  # ]:          0 :         std::string strTime = FormatISO8601DateTime(it->first);
     789                 :          0 :         std::string strAddr;
     790                 :          0 :         std::string strLabel;
     791                 :          0 :         CKey key;
     792   [ #  #  #  # ]:          0 :         if (spk_man.GetKey(keyid, key)) {
     793         [ #  # ]:          0 :             CKeyMetadata metadata;
     794         [ #  # ]:          0 :             const auto it{spk_man.mapKeyMetadata.find(keyid)};
     795   [ #  #  #  # ]:          0 :             if (it != spk_man.mapKeyMetadata.end()) metadata = it->second;
     796   [ #  #  #  #  :          0 :             file << strprintf("%s %s ", EncodeSecret(key), strTime);
                   #  # ]
     797   [ #  #  #  # ]:          0 :             if (GetWalletAddressesForKey(&spk_man, wallet, keyid, strAddr, strLabel)) {
     798   [ #  #  #  # ]:          0 :                 file << strprintf("label=%s", strLabel);
     799   [ #  #  #  # ]:          0 :             } else if (keyid == seed_id) {
     800         [ #  # ]:          0 :                 file << "hdseed=1";
     801   [ #  #  #  # ]:          0 :             } else if (mapKeyPool.count(keyid)) {
     802         [ #  # ]:          0 :                 file << "reserve=1";
     803         [ #  # ]:          0 :             } else if (metadata.hdKeypath == "s") {
     804         [ #  # ]:          0 :                 file << "inactivehdseed=1";
     805                 :          0 :             } else {
     806         [ #  # ]:          0 :                 file << "change=1";
     807                 :            :             }
     808   [ #  #  #  #  :          0 :             file << strprintf(" # addr=%s%s\n", strAddr, (metadata.has_key_origin ? " hdkeypath="+WriteHDKeypath(metadata.key_origin.path, /*apostrophe=*/true) : ""));
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     809                 :          0 :         }
     810                 :          0 :     }
     811         [ #  # ]:          0 :     file << "\n";
     812         [ #  # ]:          0 :     for (const CScriptID &scriptid : scripts) {
     813         [ #  # ]:          0 :         CScript script;
     814         [ #  # ]:          0 :         std::string create_time = "0";
     815   [ #  #  #  # ]:          0 :         std::string address = EncodeDestination(ScriptHash(scriptid));
     816                 :            :         // get birth times for scripts with metadata
     817         [ #  # ]:          0 :         auto it = spk_man.m_script_metadata.find(scriptid);
     818         [ #  # ]:          0 :         if (it != spk_man.m_script_metadata.end()) {
     819         [ #  # ]:          0 :             create_time = FormatISO8601DateTime(it->second.nCreateTime);
     820                 :          0 :         }
     821   [ #  #  #  # ]:          0 :         if(spk_man.GetCScript(scriptid, script)) {
     822   [ #  #  #  #  :          0 :             file << strprintf("%s %s script=1", HexStr(script), create_time);
             #  #  #  # ]
     823   [ #  #  #  # ]:          0 :             file << strprintf(" # addr=%s\n", address);
     824                 :          0 :         }
     825                 :          0 :     }
     826         [ #  # ]:          0 :     file << "\n";
     827         [ #  # ]:          0 :     file << "# End of dump\n";
     828         [ #  # ]:          0 :     file.close();
     829                 :            : 
     830         [ #  # ]:          0 :     UniValue reply(UniValue::VOBJ);
     831   [ #  #  #  #  :          0 :     reply.pushKV("filename", filepath.u8string());
             #  #  #  # ]
     832                 :            : 
     833                 :          0 :     return reply;
     834         [ #  # ]:          0 : },
     835                 :            :     };
     836                 :          0 : }
     837                 :            : 
     838                 :            : struct ImportData
     839                 :            : {
     840                 :            :     // Input data
     841                 :            :     std::unique_ptr<CScript> redeemscript; //!< Provided redeemScript; will be moved to `import_scripts` if relevant.
     842                 :            :     std::unique_ptr<CScript> witnessscript; //!< Provided witnessScript; will be moved to `import_scripts` if relevant.
     843                 :            : 
     844                 :            :     // Output data
     845                 :            :     std::set<CScript> import_scripts;
     846                 :            :     std::map<CKeyID, bool> used_keys; //!< Import these private keys if available (the value indicates whether if the key is required for solvability)
     847                 :            :     std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> key_origins;
     848                 :            : };
     849                 :            : 
     850                 :            : enum class ScriptContext
     851                 :            : {
     852                 :            :     TOP, //!< Top-level scriptPubKey
     853                 :            :     P2SH, //!< P2SH redeemScript
     854                 :            :     WITNESS_V0, //!< P2WSH witnessScript
     855                 :            : };
     856                 :            : 
     857                 :            : // Analyse the provided scriptPubKey, determining which keys and which redeem scripts from the ImportData struct are needed to spend it, and mark them as used.
     858                 :            : // Returns an error string, or the empty string for success.
     859                 :          0 : static std::string RecurseImportData(const CScript& script, ImportData& import_data, const ScriptContext script_ctx)
     860                 :            : {
     861                 :            :     // Use Solver to obtain script type and parsed pubkeys or hashes:
     862                 :          0 :     std::vector<std::vector<unsigned char>> solverdata;
     863         [ #  # ]:          0 :     TxoutType script_type = Solver(script, solverdata);
     864                 :            : 
     865   [ #  #  #  #  :          0 :     switch (script_type) {
             #  #  #  #  
                      # ]
     866                 :            :     case TxoutType::PUBKEY: {
     867   [ #  #  #  # ]:          0 :         CPubKey pubkey(solverdata[0]);
     868   [ #  #  #  # ]:          0 :         import_data.used_keys.emplace(pubkey.GetID(), false);
     869         [ #  # ]:          0 :         return "";
     870                 :            :     }
     871                 :            :     case TxoutType::PUBKEYHASH: {
     872   [ #  #  #  #  :          0 :         CKeyID id = CKeyID(uint160(solverdata[0]));
                   #  # ]
     873         [ #  # ]:          0 :         import_data.used_keys[id] = true;
     874         [ #  # ]:          0 :         return "";
     875                 :            :     }
     876                 :            :     case TxoutType::SCRIPTHASH: {
     877   [ #  #  #  #  :          0 :         if (script_ctx == ScriptContext::P2SH) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2SH inside another P2SH");
             #  #  #  # ]
     878   [ #  #  #  #  :          0 :         if (script_ctx == ScriptContext::WITNESS_V0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2SH inside a P2WSH");
             #  #  #  # ]
     879         [ #  # ]:          0 :         CHECK_NONFATAL(script_ctx == ScriptContext::TOP);
     880   [ #  #  #  #  :          0 :         CScriptID id = CScriptID(uint160(solverdata[0]));
                   #  # ]
     881                 :          0 :         auto subscript = std::move(import_data.redeemscript); // Remove redeemscript from import_data to check for superfluous script later.
     882   [ #  #  #  # ]:          0 :         if (!subscript) return "missing redeemscript";
     883   [ #  #  #  #  :          0 :         if (CScriptID(*subscript) != id) return "redeemScript does not match the scriptPubKey";
             #  #  #  # ]
     884   [ #  #  #  # ]:          0 :         import_data.import_scripts.emplace(*subscript);
     885   [ #  #  #  # ]:          0 :         return RecurseImportData(*subscript, import_data, ScriptContext::P2SH);
     886                 :          0 :     }
     887                 :            :     case TxoutType::MULTISIG: {
     888         [ #  # ]:          0 :         for (size_t i = 1; i + 1< solverdata.size(); ++i) {
     889   [ #  #  #  # ]:          0 :             CPubKey pubkey(solverdata[i]);
     890   [ #  #  #  # ]:          0 :             import_data.used_keys.emplace(pubkey.GetID(), false);
     891                 :          0 :         }
     892         [ #  # ]:          0 :         return "";
     893                 :            :     }
     894                 :            :     case TxoutType::WITNESS_V0_SCRIPTHASH: {
     895   [ #  #  #  #  :          0 :         if (script_ctx == ScriptContext::WITNESS_V0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2WSH inside another P2WSH");
             #  #  #  # ]
     896   [ #  #  #  #  :          0 :         CScriptID id{RIPEMD160(solverdata[0])};
                   #  # ]
     897                 :          0 :         auto subscript = std::move(import_data.witnessscript); // Remove redeemscript from import_data to check for superfluous script later.
     898   [ #  #  #  # ]:          0 :         if (!subscript) return "missing witnessscript";
     899   [ #  #  #  #  :          0 :         if (CScriptID(*subscript) != id) return "witnessScript does not match the scriptPubKey or redeemScript";
             #  #  #  # ]
     900         [ #  # ]:          0 :         if (script_ctx == ScriptContext::TOP) {
     901         [ #  # ]:          0 :             import_data.import_scripts.emplace(script); // Special rule for IsMine: native P2WSH requires the TOP script imported (see script/ismine.cpp)
     902                 :          0 :         }
     903   [ #  #  #  # ]:          0 :         import_data.import_scripts.emplace(*subscript);
     904   [ #  #  #  # ]:          0 :         return RecurseImportData(*subscript, import_data, ScriptContext::WITNESS_V0);
     905                 :          0 :     }
     906                 :            :     case TxoutType::WITNESS_V0_KEYHASH: {
     907   [ #  #  #  #  :          0 :         if (script_ctx == ScriptContext::WITNESS_V0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2WPKH inside P2WSH");
             #  #  #  # ]
     908   [ #  #  #  #  :          0 :         CKeyID id = CKeyID(uint160(solverdata[0]));
                   #  # ]
     909         [ #  # ]:          0 :         import_data.used_keys[id] = true;
     910         [ #  # ]:          0 :         if (script_ctx == ScriptContext::TOP) {
     911         [ #  # ]:          0 :             import_data.import_scripts.emplace(script); // Special rule for IsMine: native P2WPKH requires the TOP script imported (see script/ismine.cpp)
     912                 :          0 :         }
     913         [ #  # ]:          0 :         return "";
     914                 :            :     }
     915                 :            :     case TxoutType::NULL_DATA:
     916         [ #  # ]:          0 :         return "unspendable script";
     917                 :            :     case TxoutType::NONSTANDARD:
     918                 :            :     case TxoutType::WITNESS_UNKNOWN:
     919                 :            :     case TxoutType::WITNESS_V1_TAPROOT:
     920         [ #  # ]:          0 :         return "unrecognized script";
     921                 :            :     } // no default case, so the compiler can warn about missing cases
     922         [ #  # ]:          0 :     NONFATAL_UNREACHABLE();
     923                 :          0 : }
     924                 :            : 
     925                 :          0 : static UniValue ProcessImportLegacy(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<CKeyID>& ordered_pubkeys)
     926                 :            : {
     927         [ #  # ]:          0 :     UniValue warnings(UniValue::VARR);
     928                 :            : 
     929                 :            :     // First ensure scriptPubKey has either a script or JSON with "address" string
     930   [ #  #  #  # ]:          0 :     const UniValue& scriptPubKey = data["scriptPubKey"];
     931                 :          0 :     bool isScript = scriptPubKey.getType() == UniValue::VSTR;
     932   [ #  #  #  #  :          0 :     if (!isScript && !(scriptPubKey.getType() == UniValue::VOBJ && scriptPubKey.exists("address"))) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     933   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "scriptPubKey must be string with script or JSON with address string");
                   #  # ]
     934                 :            :     }
     935   [ #  #  #  #  :          0 :     const std::string& output = isScript ? scriptPubKey.get_str() : scriptPubKey["address"].get_str();
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     936                 :            : 
     937                 :            :     // Optional fields.
     938   [ #  #  #  #  :          0 :     const std::string& strRedeemScript = data.exists("redeemscript") ? data["redeemscript"].get_str() : "";
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     939   [ #  #  #  #  :          0 :     const std::string& witness_script_hex = data.exists("witnessscript") ? data["witnessscript"].get_str() : "";
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     940   [ #  #  #  #  :          0 :     const UniValue& pubKeys = data.exists("pubkeys") ? data["pubkeys"].get_array() : UniValue();
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     941   [ #  #  #  #  :          0 :     const UniValue& keys = data.exists("keys") ? data["keys"].get_array() : UniValue();
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     942   [ #  #  #  #  :          0 :     const bool internal = data.exists("internal") ? data["internal"].get_bool() : false;
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     943   [ #  #  #  #  :          0 :     const bool watchOnly = data.exists("watchonly") ? data["watchonly"].get_bool() : false;
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     944                 :            : 
     945   [ #  #  #  #  :          0 :     if (data.exists("range")) {
                   #  # ]
     946   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for a non-descriptor import");
                   #  # ]
     947                 :            :     }
     948                 :            : 
     949                 :            :     // Generate the script and destination for the scriptPubKey provided
     950         [ #  # ]:          0 :     CScript script;
     951         [ #  # ]:          0 :     if (!isScript) {
     952         [ #  # ]:          0 :         CTxDestination dest = DecodeDestination(output);
     953   [ #  #  #  # ]:          0 :         if (!IsValidDestination(dest)) {
     954   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address \"" + output + "\"");
             #  #  #  # ]
     955                 :            :         }
     956   [ #  #  #  #  :          0 :         if (OutputTypeFromDestination(dest) == OutputType::BECH32M) {
                   #  # ]
     957   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Bech32m addresses cannot be imported into legacy wallets");
                   #  # ]
     958                 :            :         }
     959         [ #  # ]:          0 :         script = GetScriptForDestination(dest);
     960                 :          0 :     } else {
     961   [ #  #  #  # ]:          0 :         if (!IsHex(output)) {
     962   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid scriptPubKey \"" + output + "\"");
             #  #  #  # ]
     963                 :            :         }
     964         [ #  # ]:          0 :         std::vector<unsigned char> vData(ParseHex(output));
     965         [ #  # ]:          0 :         script = CScript(vData.begin(), vData.end());
     966         [ #  # ]:          0 :         CTxDestination dest;
     967   [ #  #  #  #  :          0 :         if (!ExtractDestination(script, dest) && !internal) {
                   #  # ]
     968   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Internal must be set to true for nonstandard scriptPubKey imports.");
                   #  # ]
     969                 :            :         }
     970                 :          0 :     }
     971         [ #  # ]:          0 :     script_pub_keys.emplace(script);
     972                 :            : 
     973                 :            :     // Parse all arguments
     974         [ #  # ]:          0 :     if (strRedeemScript.size()) {
     975   [ #  #  #  # ]:          0 :         if (!IsHex(strRedeemScript)) {
     976   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid redeem script \"" + strRedeemScript + "\": must be hex string");
             #  #  #  # ]
     977                 :            :         }
     978         [ #  # ]:          0 :         auto parsed_redeemscript = ParseHex(strRedeemScript);
     979         [ #  # ]:          0 :         import_data.redeemscript = std::make_unique<CScript>(parsed_redeemscript.begin(), parsed_redeemscript.end());
     980                 :          0 :     }
     981         [ #  # ]:          0 :     if (witness_script_hex.size()) {
     982   [ #  #  #  # ]:          0 :         if (!IsHex(witness_script_hex)) {
     983   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid witness script \"" + witness_script_hex + "\": must be hex string");
             #  #  #  # ]
     984                 :            :         }
     985         [ #  # ]:          0 :         auto parsed_witnessscript = ParseHex(witness_script_hex);
     986         [ #  # ]:          0 :         import_data.witnessscript = std::make_unique<CScript>(parsed_witnessscript.begin(), parsed_witnessscript.end());
     987                 :          0 :     }
     988         [ #  # ]:          0 :     for (size_t i = 0; i < pubKeys.size(); ++i) {
     989   [ #  #  #  # ]:          0 :         const auto& str = pubKeys[i].get_str();
     990   [ #  #  #  # ]:          0 :         if (!IsHex(str)) {
     991   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey \"" + str + "\" must be a hex string");
             #  #  #  # ]
     992                 :            :         }
     993         [ #  # ]:          0 :         auto parsed_pubkey = ParseHex(str);
     994   [ #  #  #  # ]:          0 :         CPubKey pubkey(parsed_pubkey);
     995   [ #  #  #  # ]:          0 :         if (!pubkey.IsFullyValid()) {
     996   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey \"" + str + "\" is not a valid public key");
             #  #  #  # ]
     997                 :            :         }
     998   [ #  #  #  # ]:          0 :         pubkey_map.emplace(pubkey.GetID(), pubkey);
     999   [ #  #  #  # ]:          0 :         ordered_pubkeys.push_back(pubkey.GetID());
    1000                 :          0 :     }
    1001         [ #  # ]:          0 :     for (size_t i = 0; i < keys.size(); ++i) {
    1002   [ #  #  #  # ]:          0 :         const auto& str = keys[i].get_str();
    1003         [ #  # ]:          0 :         CKey key = DecodeSecret(str);
    1004         [ #  # ]:          0 :         if (!key.IsValid()) {
    1005   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
                   #  # ]
    1006                 :            :         }
    1007         [ #  # ]:          0 :         CPubKey pubkey = key.GetPubKey();
    1008         [ #  # ]:          0 :         CKeyID id = pubkey.GetID();
    1009   [ #  #  #  # ]:          0 :         if (pubkey_map.count(id)) {
    1010         [ #  # ]:          0 :             pubkey_map.erase(id);
    1011                 :          0 :         }
    1012         [ #  # ]:          0 :         privkey_map.emplace(id, key);
    1013                 :          0 :     }
    1014                 :            : 
    1015                 :            : 
    1016                 :            :     // Verify and process input data
    1017   [ #  #  #  #  :          0 :     have_solving_data = import_data.redeemscript || import_data.witnessscript || pubkey_map.size() || privkey_map.size();
                   #  # ]
    1018         [ #  # ]:          0 :     if (have_solving_data) {
    1019                 :            :         // Match up data in import_data with the scriptPubKey in script.
    1020         [ #  # ]:          0 :         auto error = RecurseImportData(script, import_data, ScriptContext::TOP);
    1021                 :            : 
    1022                 :            :         // Verify whether the watchonly option corresponds to the availability of private keys.
    1023         [ #  # ]:          0 :         bool spendable = std::all_of(import_data.used_keys.begin(), import_data.used_keys.end(), [&](const std::pair<CKeyID, bool>& used_key){ return privkey_map.count(used_key.first) > 0; });
    1024   [ #  #  #  # ]:          0 :         if (!watchOnly && !spendable) {
    1025   [ #  #  #  # ]:          0 :             warnings.push_back("Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag.");
    1026                 :          0 :         }
    1027   [ #  #  #  # ]:          0 :         if (watchOnly && spendable) {
    1028   [ #  #  #  # ]:          0 :             warnings.push_back("All private keys are provided, outputs will be considered spendable. If this is intentional, do not specify the watchonly flag.");
    1029                 :          0 :         }
    1030                 :            : 
    1031                 :            :         // Check that all required keys for solvability are provided.
    1032         [ #  # ]:          0 :         if (error.empty()) {
    1033         [ #  # ]:          0 :             for (const auto& require_key : import_data.used_keys) {
    1034         [ #  # ]:          0 :                 if (!require_key.second) continue; // Not a required key
    1035   [ #  #  #  #  :          0 :                 if (pubkey_map.count(require_key.first) == 0 && privkey_map.count(require_key.first) == 0) {
             #  #  #  # ]
    1036         [ #  # ]:          0 :                     error = "some required keys are missing";
    1037                 :          0 :                 }
    1038                 :            :             }
    1039                 :          0 :         }
    1040                 :            : 
    1041         [ #  # ]:          0 :         if (!error.empty()) {
    1042   [ #  #  #  #  :          0 :             warnings.push_back("Importing as non-solvable: " + error + ". If this is intentional, don't provide any keys, pubkeys, witnessscript, or redeemscript.");
             #  #  #  # ]
    1043                 :          0 :             import_data = ImportData();
    1044                 :          0 :             pubkey_map.clear();
    1045                 :          0 :             privkey_map.clear();
    1046                 :          0 :             have_solving_data = false;
    1047                 :          0 :         } else {
    1048                 :            :             // RecurseImportData() removes any relevant redeemscript/witnessscript from import_data, so we can use that to discover if a superfluous one was provided.
    1049   [ #  #  #  #  :          0 :             if (import_data.redeemscript) warnings.push_back("Ignoring redeemscript as this is not a P2SH script.");
                   #  # ]
    1050   [ #  #  #  #  :          0 :             if (import_data.witnessscript) warnings.push_back("Ignoring witnessscript as this is not a (P2SH-)P2WSH script.");
                   #  # ]
    1051         [ #  # ]:          0 :             for (auto it = privkey_map.begin(); it != privkey_map.end(); ) {
    1052                 :          0 :                 auto oldit = it++;
    1053   [ #  #  #  # ]:          0 :                 if (import_data.used_keys.count(oldit->first) == 0) {
    1054   [ #  #  #  # ]:          0 :                     warnings.push_back("Ignoring irrelevant private key.");
    1055         [ #  # ]:          0 :                     privkey_map.erase(oldit);
    1056                 :          0 :                 }
    1057                 :            :             }
    1058         [ #  # ]:          0 :             for (auto it = pubkey_map.begin(); it != pubkey_map.end(); ) {
    1059                 :          0 :                 auto oldit = it++;
    1060         [ #  # ]:          0 :                 auto key_data_it = import_data.used_keys.find(oldit->first);
    1061   [ #  #  #  # ]:          0 :                 if (key_data_it == import_data.used_keys.end() || !key_data_it->second) {
    1062   [ #  #  #  #  :          0 :                     warnings.push_back("Ignoring public key \"" + HexStr(oldit->first) + "\" as it doesn't appear inside P2PKH or P2WPKH.");
          #  #  #  #  #  
                #  #  # ]
    1063         [ #  # ]:          0 :                     pubkey_map.erase(oldit);
    1064                 :          0 :                 }
    1065                 :            :             }
    1066                 :            :         }
    1067                 :          0 :     }
    1068                 :            : 
    1069                 :          0 :     return warnings;
    1070         [ #  # ]:          0 : }
    1071                 :            : 
    1072                 :          0 : static UniValue ProcessImportDescriptor(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<CKeyID>& ordered_pubkeys)
    1073                 :            : {
    1074         [ #  # ]:          0 :     UniValue warnings(UniValue::VARR);
    1075                 :            : 
    1076   [ #  #  #  #  :          0 :     const std::string& descriptor = data["desc"].get_str();
                   #  # ]
    1077                 :          0 :     FlatSigningProvider keys;
    1078                 :          0 :     std::string error;
    1079         [ #  # ]:          0 :     auto parsed_desc = Parse(descriptor, keys, error, /* require_checksum = */ true);
    1080         [ #  # ]:          0 :     if (!parsed_desc) {
    1081         [ #  # ]:          0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
    1082                 :            :     }
    1083   [ #  #  #  #  :          0 :     if (parsed_desc->GetOutputType() == OutputType::BECH32M) {
                   #  # ]
    1084   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Bech32m descriptors cannot be imported into legacy wallets");
                   #  # ]
    1085                 :            :     }
    1086                 :            : 
    1087         [ #  # ]:          0 :     have_solving_data = parsed_desc->IsSolvable();
    1088   [ #  #  #  #  :          0 :     const bool watch_only = data.exists("watchonly") ? data["watchonly"].get_bool() : false;
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    1089                 :            : 
    1090                 :          0 :     int64_t range_start = 0, range_end = 0;
    1091   [ #  #  #  #  :          0 :     if (!parsed_desc->IsRange() && data.exists("range")) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1092   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for an un-ranged descriptor");
                   #  # ]
    1093   [ #  #  #  # ]:          0 :     } else if (parsed_desc->IsRange()) {
    1094   [ #  #  #  #  :          0 :         if (!data.exists("range")) {
                   #  # ]
    1095   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor is ranged, please specify the range");
                   #  # ]
    1096                 :            :         }
    1097   [ #  #  #  #  :          0 :         std::tie(range_start, range_end) = ParseDescriptorRange(data["range"]);
                   #  # ]
    1098                 :          0 :     }
    1099                 :            : 
    1100   [ #  #  #  #  :          0 :     const UniValue& priv_keys = data.exists("keys") ? data["keys"].get_array() : UniValue();
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1101                 :            : 
    1102                 :            :     // Expand all descriptors to get public keys and scripts, and private keys if available.
    1103         [ #  # ]:          0 :     for (int i = range_start; i <= range_end; ++i) {
    1104                 :          0 :         FlatSigningProvider out_keys;
    1105                 :          0 :         std::vector<CScript> scripts_temp;
    1106         [ #  # ]:          0 :         parsed_desc->Expand(i, keys, scripts_temp, out_keys);
    1107   [ #  #  #  # ]:          0 :         std::copy(scripts_temp.begin(), scripts_temp.end(), std::inserter(script_pub_keys, script_pub_keys.end()));
    1108         [ #  # ]:          0 :         for (const auto& key_pair : out_keys.pubkeys) {
    1109         [ #  # ]:          0 :             ordered_pubkeys.push_back(key_pair.first);
    1110                 :            :         }
    1111                 :            : 
    1112         [ #  # ]:          0 :         for (const auto& x : out_keys.scripts) {
    1113         [ #  # ]:          0 :             import_data.import_scripts.emplace(x.second);
    1114                 :            :         }
    1115                 :            : 
    1116         [ #  # ]:          0 :         parsed_desc->ExpandPrivate(i, keys, out_keys);
    1117                 :            : 
    1118   [ #  #  #  # ]:          0 :         std::copy(out_keys.pubkeys.begin(), out_keys.pubkeys.end(), std::inserter(pubkey_map, pubkey_map.end()));
    1119   [ #  #  #  # ]:          0 :         std::copy(out_keys.keys.begin(), out_keys.keys.end(), std::inserter(privkey_map, privkey_map.end()));
    1120         [ #  # ]:          0 :         import_data.key_origins.insert(out_keys.origins.begin(), out_keys.origins.end());
    1121                 :          0 :     }
    1122                 :            : 
    1123         [ #  # ]:          0 :     for (size_t i = 0; i < priv_keys.size(); ++i) {
    1124   [ #  #  #  # ]:          0 :         const auto& str = priv_keys[i].get_str();
    1125         [ #  # ]:          0 :         CKey key = DecodeSecret(str);
    1126         [ #  # ]:          0 :         if (!key.IsValid()) {
    1127   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
                   #  # ]
    1128                 :            :         }
    1129         [ #  # ]:          0 :         CPubKey pubkey = key.GetPubKey();
    1130         [ #  # ]:          0 :         CKeyID id = pubkey.GetID();
    1131                 :            : 
    1132                 :            :         // Check if this private key corresponds to a public key from the descriptor
    1133   [ #  #  #  # ]:          0 :         if (!pubkey_map.count(id)) {
    1134   [ #  #  #  # ]:          0 :             warnings.push_back("Ignoring irrelevant private key.");
    1135                 :          0 :         } else {
    1136         [ #  # ]:          0 :             privkey_map.emplace(id, key);
    1137                 :            :         }
    1138                 :          0 :     }
    1139                 :            : 
    1140                 :            :     // Check if all the public keys have corresponding private keys in the import for spendability.
    1141                 :            :     // This does not take into account threshold multisigs which could be spendable without all keys.
    1142                 :            :     // Thus, threshold multisigs without all keys will be considered not spendable here, even if they are,
    1143                 :            :     // perhaps triggering a false warning message. This is consistent with the current wallet IsMine check.
    1144   [ #  #  #  # ]:          0 :     bool spendable = std::all_of(pubkey_map.begin(), pubkey_map.end(),
    1145                 :          0 :         [&](const std::pair<CKeyID, CPubKey>& used_key) {
    1146                 :          0 :             return privkey_map.count(used_key.first) > 0;
    1147   [ #  #  #  #  :          0 :         }) && std::all_of(import_data.key_origins.begin(), import_data.key_origins.end(),
                   #  # ]
    1148                 :          0 :         [&](const std::pair<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& entry) {
    1149                 :          0 :             return privkey_map.count(entry.first) > 0;
    1150                 :            :         });
    1151   [ #  #  #  # ]:          0 :     if (!watch_only && !spendable) {
    1152   [ #  #  #  # ]:          0 :         warnings.push_back("Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag.");
    1153                 :          0 :     }
    1154   [ #  #  #  # ]:          0 :     if (watch_only && spendable) {
    1155   [ #  #  #  # ]:          0 :         warnings.push_back("All private keys are provided, outputs will be considered spendable. If this is intentional, do not specify the watchonly flag.");
    1156                 :          0 :     }
    1157                 :            : 
    1158                 :          0 :     return warnings;
    1159         [ #  # ]:          0 : }
    1160                 :            : 
    1161                 :          0 : static UniValue ProcessImport(CWallet& wallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
    1162                 :            : {
    1163         [ #  # ]:          0 :     UniValue warnings(UniValue::VARR);
    1164         [ #  # ]:          0 :     UniValue result(UniValue::VOBJ);
    1165                 :            : 
    1166                 :            :     try {
    1167   [ #  #  #  #  :          0 :         const bool internal = data.exists("internal") ? data["internal"].get_bool() : false;
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    1168                 :            :         // Internal addresses should not have a label
    1169   [ #  #  #  #  :          0 :         if (internal && data.exists("label")) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1170   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Internal addresses should not have a label");
                   #  # ]
    1171                 :            :         }
    1172   [ #  #  #  #  :          0 :         const std::string label{LabelFromValue(data["label"])};
                   #  # ]
    1173   [ #  #  #  #  :          0 :         const bool add_keypool = data.exists("keypool") ? data["keypool"].get_bool() : false;
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    1174                 :            : 
    1175                 :            :         // Add to keypool only works with privkeys disabled
    1176   [ #  #  #  #  :          0 :         if (add_keypool && !wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
                   #  # ]
    1177   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Keys can only be imported to the keypool when private keys are disabled");
                   #  # ]
    1178                 :            :         }
    1179                 :            : 
    1180                 :          0 :         ImportData import_data;
    1181                 :          0 :         std::map<CKeyID, CPubKey> pubkey_map;
    1182                 :          0 :         std::map<CKeyID, CKey> privkey_map;
    1183                 :          0 :         std::set<CScript> script_pub_keys;
    1184                 :          0 :         std::vector<CKeyID> ordered_pubkeys;
    1185                 :            :         bool have_solving_data;
    1186                 :            : 
    1187   [ #  #  #  #  :          0 :         if (data.exists("scriptPubKey") && data.exists("desc")) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    1188   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Both a descriptor and a scriptPubKey should not be provided.");
                   #  # ]
    1189   [ #  #  #  #  :          0 :         } else if (data.exists("scriptPubKey")) {
                   #  # ]
    1190         [ #  # ]:          0 :             warnings = ProcessImportLegacy(import_data, pubkey_map, privkey_map, script_pub_keys, have_solving_data, data, ordered_pubkeys);
    1191   [ #  #  #  #  :          0 :         } else if (data.exists("desc")) {
                   #  # ]
    1192         [ #  # ]:          0 :             warnings = ProcessImportDescriptor(import_data, pubkey_map, privkey_map, script_pub_keys, have_solving_data, data, ordered_pubkeys);
    1193                 :          0 :         } else {
    1194   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Either a descriptor or scriptPubKey must be provided.");
                   #  # ]
    1195                 :            :         }
    1196                 :            : 
    1197                 :            :         // If private keys are disabled, abort if private keys are being imported
    1198   [ #  #  #  #  :          0 :         if (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !privkey_map.empty()) {
                   #  # ]
    1199   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
                   #  # ]
    1200                 :            :         }
    1201                 :            : 
    1202                 :            :         // Check whether we have any work to do
    1203         [ #  # ]:          0 :         for (const CScript& script : script_pub_keys) {
    1204   [ #  #  #  # ]:          0 :             if (wallet.IsMine(script) & ISMINE_SPENDABLE) {
    1205   [ #  #  #  #  :          0 :                 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script (\"" + HexStr(script) + "\")");
          #  #  #  #  #  
                #  #  # ]
    1206                 :            :             }
    1207                 :            :         }
    1208                 :            : 
    1209                 :            :         // All good, time to import
    1210         [ #  # ]:          0 :         wallet.MarkDirty();
    1211   [ #  #  #  #  :          0 :         if (!wallet.ImportScripts(import_data.import_scripts, timestamp)) {
                   #  # ]
    1212   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Error adding script to wallet");
                   #  # ]
    1213                 :            :         }
    1214   [ #  #  #  # ]:          0 :         if (!wallet.ImportPrivKeys(privkey_map, timestamp)) {
    1215   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
                   #  # ]
    1216                 :            :         }
    1217   [ #  #  #  # ]:          0 :         if (!wallet.ImportPubKeys(ordered_pubkeys, pubkey_map, import_data.key_origins, add_keypool, internal, timestamp)) {
    1218   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
                   #  # ]
    1219                 :            :         }
    1220   [ #  #  #  # ]:          0 :         if (!wallet.ImportScriptPubKeys(label, script_pub_keys, have_solving_data, !internal, timestamp)) {
    1221   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
                   #  # ]
    1222                 :            :         }
    1223                 :            : 
    1224   [ #  #  #  #  :          0 :         result.pushKV("success", UniValue(true));
                   #  # ]
    1225         [ #  # ]:          0 :     } catch (const UniValue& e) {
    1226   [ #  #  #  #  :          0 :         result.pushKV("success", UniValue(false));
                   #  # ]
    1227   [ #  #  #  #  :          0 :         result.pushKV("error", e);
                   #  # ]
    1228   [ #  #  #  # ]:          0 :     } catch (...) {
    1229   [ #  #  #  #  :          0 :         result.pushKV("success", UniValue(false));
                   #  # ]
    1230                 :            : 
    1231   [ #  #  #  #  :          0 :         result.pushKV("error", JSONRPCError(RPC_MISC_ERROR, "Missing required fields"));
             #  #  #  # ]
    1232   [ #  #  #  # ]:          0 :     }
    1233         [ #  # ]:          0 :     PushWarnings(warnings, result);
    1234                 :          0 :     return result;
    1235         [ #  # ]:          0 : }
    1236                 :            : 
    1237                 :          0 : static int64_t GetImportTimestamp(const UniValue& data, int64_t now)
    1238                 :            : {
    1239   [ #  #  #  #  :          0 :     if (data.exists("timestamp")) {
                   #  # ]
    1240   [ #  #  #  # ]:          0 :         const UniValue& timestamp = data["timestamp"];
    1241         [ #  # ]:          0 :         if (timestamp.isNum()) {
    1242                 :          0 :             return timestamp.getInt<int64_t>();
    1243         [ #  # ]:          0 :         } else if (timestamp.isStr() && timestamp.get_str() == "now") {
    1244                 :          0 :             return now;
    1245                 :            :         }
    1246   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Expected number or \"now\" timestamp value for key. got type %s", uvTypeName(timestamp.type())));
          #  #  #  #  #  
                      # ]
    1247                 :            :     }
    1248   [ #  #  #  #  :          0 :     throw JSONRPCError(RPC_TYPE_ERROR, "Missing required timestamp field for key");
                   #  # ]
    1249                 :          0 : }
    1250                 :            : 
    1251                 :          0 : RPCHelpMan importmulti()
    1252                 :            : {
    1253   [ #  #  #  #  :          0 :     return RPCHelpMan{"importmulti",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1254         [ #  # ]:          0 :                 "\nImport addresses/scripts (with private or public keys, redeem script (P2SH)), optionally rescanning the blockchain from the earliest creation time of the imported scripts. Requires a new wallet backup.\n"
    1255                 :            :                 "If an address/script is imported without all of the private keys required to spend from that address, it will be watchonly. The 'watchonly' option must be set to true in this case or a warning will be returned.\n"
    1256                 :            :                 "Conversely, if all the private keys are provided and the address/script is spendable, the watchonly option must be set to false, or a warning will be returned.\n"
    1257                 :            :             "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n"
    1258                 :            :             "may report that the imported keys, addresses or scripts exist but related transactions are still missing.\n"
    1259                 :            :             "The rescan parameter can be set to false if the key was never used to create transactions. If it is set to false,\n"
    1260                 :            :             "but the key was used to create transactions, rescanblockchain needs to be called with the appropriate block range.\n"
    1261                 :            :             "Note: Use \"getwalletinfo\" to query the scanning progress.\n"
    1262                 :            :             "Note: This command is only compatible with legacy wallets. Use \"importdescriptors\" for descriptor wallets.\n",
    1263         [ #  # ]:          0 :                 {
    1264   [ #  #  #  #  :          0 :                     {"requests", RPCArg::Type::ARR, RPCArg::Optional::NO, "Data to be imported",
                   #  # ]
    1265         [ #  # ]:          0 :                         {
    1266   [ #  #  #  #  :          0 :                             {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
                   #  # ]
    1267         [ #  # ]:          0 :                                 {
    1268   [ #  #  #  #  :          0 :                                     {"desc", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Descriptor to import. If using descriptor, do not also provide address/scriptPubKey, scripts, or pubkeys"},
                   #  # ]
    1269   [ #  #  #  #  :          0 :                                     {"scriptPubKey", RPCArg::Type::STR, RPCArg::Optional::NO, "Type of scriptPubKey (string for script, json for address). Should not be provided if using a descriptor",
                   #  # ]
    1270   [ #  #  #  #  :          0 :                                         RPCArgOptions{.type_str={"\"<script>\" | { \"address\":\"<address>\" }", "string / json"}}
             #  #  #  # ]
    1271                 :            :                                     },
    1272   [ #  #  #  #  :          0 :                                     {"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, "Creation time of the key expressed in " + UNIX_EPOCH_TIME + ",\n"
             #  #  #  # ]
    1273                 :            :                                         "or the string \"now\" to substitute the current synced blockchain time. The timestamp of the oldest\n"
    1274                 :            :                                         "key will determine how far back blockchain rescans need to begin for missing wallet transactions.\n"
    1275                 :            :                                         "\"now\" can be specified to bypass scanning, for keys which are known to never have been used, and\n"
    1276                 :            :                                         "0 can be specified to scan the entire blockchain. Blocks up to 2 hours before the earliest key\n"
    1277                 :            :                                         "creation time of all keys being imported by the importmulti call will be scanned.",
    1278   [ #  #  #  #  :          0 :                                         RPCArgOptions{.type_str={"timestamp | \"now\"", "integer / string"}}
             #  #  #  # ]
    1279                 :            :                                     },
    1280   [ #  #  #  #  :          0 :                                     {"redeemscript", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Allowed only if the scriptPubKey is a P2SH or P2SH-P2WSH address/scriptPubKey"},
                   #  # ]
    1281   [ #  #  #  #  :          0 :                                     {"witnessscript", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Allowed only if the scriptPubKey is a P2SH-P2WSH or P2WSH address/scriptPubKey"},
                   #  # ]
    1282   [ #  #  #  #  :          0 :                                     {"pubkeys", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "Array of strings giving pubkeys to import. They must occur in P2PKH or P2WPKH scripts. They are not required when the private key is also provided (see the \"keys\" argument).",
             #  #  #  # ]
    1283         [ #  # ]:          0 :                                         {
    1284   [ #  #  #  #  :          0 :                                             {"pubKey", RPCArg::Type::STR, RPCArg::Optional::OMITTED, ""},
                   #  # ]
    1285                 :            :                                         }
    1286                 :            :                                     },
    1287   [ #  #  #  #  :          0 :                                     {"keys", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "Array of strings giving private keys to import. The corresponding public keys must occur in the output or redeemscript.",
             #  #  #  # ]
    1288         [ #  # ]:          0 :                                         {
    1289   [ #  #  #  #  :          0 :                                             {"key", RPCArg::Type::STR, RPCArg::Optional::OMITTED, ""},
                   #  # ]
    1290                 :            :                                         }
    1291                 :            :                                     },
    1292   [ #  #  #  #  :          0 :                                     {"range", RPCArg::Type::RANGE, RPCArg::Optional::OMITTED, "If a ranged descriptor is used, this specifies the end or the range (in the form [begin,end]) to import"},
                   #  # ]
    1293   [ #  #  #  #  :          0 :                                     {"internal", RPCArg::Type::BOOL, RPCArg::Default{false}, "Stating whether matching outputs should be treated as not incoming payments (also known as change)"},
             #  #  #  # ]
    1294   [ #  #  #  #  :          0 :                                     {"watchonly", RPCArg::Type::BOOL, RPCArg::Default{false}, "Stating whether matching outputs should be considered watchonly."},
             #  #  #  # ]
    1295   [ #  #  #  #  :          0 :                                     {"label", RPCArg::Type::STR, RPCArg::Default{""}, "Label to assign to the address, only allowed with internal=false"},
             #  #  #  # ]
    1296   [ #  #  #  #  :          0 :                                     {"keypool", RPCArg::Type::BOOL, RPCArg::Default{false}, "Stating whether imported public keys should be added to the keypool for when users request new addresses. Only allowed when wallet private keys are disabled"},
             #  #  #  # ]
    1297                 :            :                                 },
    1298                 :            :                             },
    1299                 :            :                         },
    1300         [ #  # ]:          0 :                         RPCArgOptions{.oneline_description="requests"}},
    1301   [ #  #  #  #  :          0 :                     {"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "",
                   #  # ]
    1302         [ #  # ]:          0 :                         {
    1303   [ #  #  #  #  :          0 :                             {"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Scan the chain and mempool for wallet transactions after all imports."},
             #  #  #  # ]
    1304                 :            :                         },
    1305         [ #  # ]:          0 :                         RPCArgOptions{.oneline_description="options"}},
    1306                 :            :                 },
    1307   [ #  #  #  # ]:          0 :                 RPCResult{
    1308   [ #  #  #  # ]:          0 :                     RPCResult::Type::ARR, "", "Response is an array with the same size as the input that has the execution result",
    1309         [ #  # ]:          0 :                     {
    1310   [ #  #  #  #  :          0 :                         {RPCResult::Type::OBJ, "", "",
                   #  # ]
    1311         [ #  # ]:          0 :                         {
    1312   [ #  #  #  #  :          0 :                             {RPCResult::Type::BOOL, "success", ""},
                   #  # ]
    1313   [ #  #  #  #  :          0 :                             {RPCResult::Type::ARR, "warnings", /*optional=*/true, "",
                   #  # ]
    1314         [ #  # ]:          0 :                             {
    1315   [ #  #  #  #  :          0 :                                 {RPCResult::Type::STR, "", ""},
                   #  # ]
    1316                 :            :                             }},
    1317   [ #  #  #  #  :          0 :                             {RPCResult::Type::OBJ, "error", /*optional=*/true, "",
                   #  # ]
    1318         [ #  # ]:          0 :                             {
    1319   [ #  #  #  #  :          0 :                                 {RPCResult::Type::ELISION, "", "JSONRPC error"},
                   #  # ]
    1320                 :            :                             }},
    1321                 :            :                         }},
    1322                 :            :                     }
    1323                 :            :                 },
    1324         [ #  # ]:          0 :                 RPCExamples{
    1325   [ #  #  #  #  :          0 :                     HelpExampleCli("importmulti", "'[{ \"scriptPubKey\": { \"address\": \"<my address>\" }, \"timestamp\":1455191478 }, "
                   #  # ]
    1326         [ #  # ]:          0 :                                           "{ \"scriptPubKey\": { \"address\": \"<my 2nd address>\" }, \"label\": \"example 2\", \"timestamp\": 1455191480 }]'") +
    1327   [ #  #  #  #  :          0 :                     HelpExampleCli("importmulti", "'[{ \"scriptPubKey\": { \"address\": \"<my address>\" }, \"timestamp\":1455191478 }]' '{ \"rescan\": false}'")
                   #  # ]
    1328                 :            :                 },
    1329         [ #  # ]:          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& mainRequest) -> UniValue
    1330                 :            : {
    1331                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(mainRequest);
    1332   [ #  #  #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
    1333                 :          0 :     CWallet& wallet{*pwallet};
    1334                 :            : 
    1335                 :            :     // Make sure the results are valid at least up to the most recent block
    1336                 :            :     // the user could have gotten from another RPC command prior to now
    1337         [ #  # ]:          0 :     wallet.BlockUntilSyncedToCurrentChain();
    1338                 :            : 
    1339         [ #  # ]:          0 :     EnsureLegacyScriptPubKeyMan(*pwallet, true);
    1340                 :            : 
    1341         [ #  # ]:          0 :     const UniValue& requests = mainRequest.params[0];
    1342                 :            : 
    1343                 :            :     //Default options
    1344                 :          0 :     bool fRescan = true;
    1345                 :            : 
    1346   [ #  #  #  # ]:          0 :     if (!mainRequest.params[1].isNull()) {
    1347         [ #  # ]:          0 :         const UniValue& options = mainRequest.params[1];
    1348                 :            : 
    1349   [ #  #  #  #  :          0 :         if (options.exists("rescan")) {
                   #  # ]
    1350   [ #  #  #  #  :          0 :             fRescan = options["rescan"].get_bool();
                   #  # ]
    1351                 :          0 :         }
    1352                 :          0 :     }
    1353                 :            : 
    1354         [ #  # ]:          0 :     WalletRescanReserver reserver(*pwallet);
    1355   [ #  #  #  # ]:          0 :     if (fRescan && !reserver.reserve()) {
    1356   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
                   #  # ]
    1357                 :            :     }
    1358                 :            : 
    1359                 :          0 :     int64_t now = 0;
    1360                 :          0 :     bool fRunScan = false;
    1361                 :          0 :     int64_t nLowestTimestamp = 0;
    1362         [ #  # ]:          0 :     UniValue response(UniValue::VARR);
    1363                 :            :     {
    1364         [ #  # ]:          0 :         LOCK(pwallet->cs_wallet);
    1365                 :            : 
    1366                 :            :         // Check all requests are watchonly
    1367                 :          0 :         bool is_watchonly{true};
    1368   [ #  #  #  # ]:          0 :         for (size_t i = 0; i < requests.size(); ++i) {
    1369         [ #  # ]:          0 :             const UniValue& request = requests[i];
    1370   [ #  #  #  #  :          0 :             if (!request.exists("watchonly") || !request["watchonly"].get_bool()) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1371                 :          0 :                 is_watchonly = false;
    1372                 :          0 :                 break;
    1373                 :            :             }
    1374                 :          0 :         }
    1375                 :            :         // Wallet does not need to be unlocked if all requests are watchonly
    1376   [ #  #  #  # ]:          0 :         if (!is_watchonly) EnsureWalletIsUnlocked(wallet);
    1377                 :            : 
    1378                 :            :         // Verify all timestamps are present before importing any keys.
    1379   [ #  #  #  #  :          0 :         CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(nLowestTimestamp).mtpTime(now)));
             #  #  #  # ]
    1380   [ #  #  #  # ]:          0 :         for (const UniValue& data : requests.getValues()) {
    1381         [ #  # ]:          0 :             GetImportTimestamp(data, now);
    1382                 :            :         }
    1383                 :            : 
    1384                 :          0 :         const int64_t minimumTimestamp = 1;
    1385                 :            : 
    1386   [ #  #  #  # ]:          0 :         for (const UniValue& data : requests.getValues()) {
    1387         [ #  # ]:          0 :             const int64_t timestamp = std::max(GetImportTimestamp(data, now), minimumTimestamp);
    1388         [ #  # ]:          0 :             const UniValue result = ProcessImport(*pwallet, data, timestamp);
    1389   [ #  #  #  # ]:          0 :             response.push_back(result);
    1390                 :            : 
    1391         [ #  # ]:          0 :             if (!fRescan) {
    1392                 :          0 :                 continue;
    1393                 :            :             }
    1394                 :            : 
    1395                 :            :             // If at least one request was successful then allow rescan.
    1396   [ #  #  #  #  :          0 :             if (result["success"].get_bool()) {
             #  #  #  # ]
    1397                 :          0 :                 fRunScan = true;
    1398                 :          0 :             }
    1399                 :            : 
    1400                 :            :             // Get the lowest timestamp.
    1401         [ #  # ]:          0 :             if (timestamp < nLowestTimestamp) {
    1402                 :          0 :                 nLowestTimestamp = timestamp;
    1403                 :          0 :             }
    1404         [ #  # ]:          0 :         }
    1405                 :          0 :     }
    1406   [ #  #  #  #  :          0 :     if (fRescan && fRunScan && requests.size()) {
             #  #  #  # ]
    1407         [ #  # ]:          0 :         int64_t scannedTime = pwallet->RescanFromTime(nLowestTimestamp, reserver, /*update=*/true);
    1408         [ #  # ]:          0 :         pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
    1409                 :            : 
    1410         [ #  # ]:          0 :         if (pwallet->IsAbortingRescan()) {
    1411   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");
                   #  # ]
    1412                 :            :         }
    1413         [ #  # ]:          0 :         if (scannedTime > nLowestTimestamp) {
    1414   [ #  #  #  # ]:          0 :             std::vector<UniValue> results = response.getValues();
    1415         [ #  # ]:          0 :             response.clear();
    1416         [ #  # ]:          0 :             response.setArray();
    1417                 :          0 :             size_t i = 0;
    1418   [ #  #  #  # ]:          0 :             for (const UniValue& request : requests.getValues()) {
    1419                 :            :                 // If key creation date is within the successfully scanned
    1420                 :            :                 // range, or if the import result already has an error set, let
    1421                 :            :                 // the result stand unmodified. Otherwise replace the result
    1422                 :            :                 // with an error message.
    1423   [ #  #  #  #  :          0 :                 if (scannedTime <= GetImportTimestamp(request, now) || results.at(i).exists("error")) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    1424   [ #  #  #  #  :          0 :                     response.push_back(results.at(i));
                   #  # ]
    1425                 :          0 :                 } else {
    1426         [ #  # ]:          0 :                     UniValue result = UniValue(UniValue::VOBJ);
    1427   [ #  #  #  #  :          0 :                     result.pushKV("success", UniValue(false));
                   #  # ]
    1428         [ #  # ]:          0 :                     result.pushKV(
    1429         [ #  # ]:          0 :                         "error",
    1430         [ #  # ]:          0 :                         JSONRPCError(
    1431                 :            :                             RPC_MISC_ERROR,
    1432         [ #  # ]:          0 :                             strprintf("Rescan failed for key with creation timestamp %d. There was an error reading a "
    1433                 :            :                                       "block from time %d, which is after or within %d seconds of key creation, and "
    1434                 :            :                                       "could contain transactions pertaining to the key. As a result, transactions "
    1435                 :            :                                       "and coins using this key may not appear in the wallet. This error could be "
    1436                 :            :                                       "caused by pruning or data corruption (see bitcoind log for details) and could "
    1437                 :            :                                       "be dealt with by downloading and rescanning the relevant blocks (see -reindex "
    1438                 :            :                                       "option and rescanblockchain RPC).",
    1439         [ #  # ]:          0 :                                 GetImportTimestamp(request, now), scannedTime - TIMESTAMP_WINDOW - 1, TIMESTAMP_WINDOW)));
    1440         [ #  # ]:          0 :                     response.push_back(std::move(result));
    1441                 :          0 :                 }
    1442                 :          0 :                 ++i;
    1443                 :            :             }
    1444                 :          0 :         }
    1445                 :          0 :     }
    1446                 :            : 
    1447                 :          0 :     return response;
    1448         [ #  # ]:          0 : },
    1449                 :            :     };
    1450                 :          0 : }
    1451                 :            : 
    1452                 :          0 : static UniValue ProcessDescriptorImport(CWallet& wallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
    1453                 :            : {
    1454         [ #  # ]:          0 :     UniValue warnings(UniValue::VARR);
    1455         [ #  # ]:          0 :     UniValue result(UniValue::VOBJ);
    1456                 :            : 
    1457                 :            :     try {
    1458   [ #  #  #  #  :          0 :         if (!data.exists("desc")) {
                   #  # ]
    1459   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor not found.");
                   #  # ]
    1460                 :            :         }
    1461                 :            : 
    1462   [ #  #  #  #  :          0 :         const std::string& descriptor = data["desc"].get_str();
                   #  # ]
    1463   [ #  #  #  #  :          0 :         const bool active = data.exists("active") ? data["active"].get_bool() : false;
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    1464   [ #  #  #  #  :          0 :         const bool internal = data.exists("internal") ? data["internal"].get_bool() : false;
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    1465   [ #  #  #  #  :          0 :         const std::string label{LabelFromValue(data["label"])};
                   #  # ]
    1466                 :            : 
    1467                 :            :         // Parse descriptor string
    1468                 :          0 :         FlatSigningProvider keys;
    1469                 :          0 :         std::string error;
    1470         [ #  # ]:          0 :         auto parsed_desc = Parse(descriptor, keys, error, /* require_checksum = */ true);
    1471         [ #  # ]:          0 :         if (!parsed_desc) {
    1472         [ #  # ]:          0 :             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
    1473                 :            :         }
    1474                 :            : 
    1475                 :            :         // Range check
    1476                 :          0 :         int64_t range_start = 0, range_end = 1, next_index = 0;
    1477   [ #  #  #  #  :          0 :         if (!parsed_desc->IsRange() && data.exists("range")) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1478   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for an un-ranged descriptor");
                   #  # ]
    1479   [ #  #  #  # ]:          0 :         } else if (parsed_desc->IsRange()) {
    1480   [ #  #  #  #  :          0 :             if (data.exists("range")) {
                   #  # ]
    1481   [ #  #  #  #  :          0 :                 auto range = ParseDescriptorRange(data["range"]);
                   #  # ]
    1482                 :          0 :                 range_start = range.first;
    1483                 :          0 :                 range_end = range.second + 1; // Specified range end is inclusive, but we need range end as exclusive
    1484                 :          0 :             } else {
    1485   [ #  #  #  # ]:          0 :                 warnings.push_back("Range not given, using default keypool range");
    1486                 :          0 :                 range_start = 0;
    1487                 :          0 :                 range_end = wallet.m_keypool_size;
    1488                 :            :             }
    1489                 :          0 :             next_index = range_start;
    1490                 :            : 
    1491   [ #  #  #  #  :          0 :             if (data.exists("next_index")) {
                   #  # ]
    1492   [ #  #  #  #  :          0 :                 next_index = data["next_index"].getInt<int64_t>();
                   #  # ]
    1493                 :            :                 // bound checks
    1494         [ #  # ]:          0 :                 if (next_index < range_start || next_index >= range_end) {
    1495   [ #  #  #  #  :          0 :                     throw JSONRPCError(RPC_INVALID_PARAMETER, "next_index is out of range");
                   #  # ]
    1496                 :            :                 }
    1497                 :          0 :             }
    1498                 :          0 :         }
    1499                 :            : 
    1500                 :            :         // Active descriptors must be ranged
    1501   [ #  #  #  #  :          0 :         if (active && !parsed_desc->IsRange()) {
                   #  # ]
    1502   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Active descriptors must be ranged");
                   #  # ]
    1503                 :            :         }
    1504                 :            : 
    1505                 :            :         // Ranged descriptors should not have a label
    1506   [ #  #  #  #  :          0 :         if (data.exists("range") && data.exists("label")) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    1507   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Ranged descriptors should not have a label");
                   #  # ]
    1508                 :            :         }
    1509                 :            : 
    1510                 :            :         // Internal addresses should not have a label either
    1511   [ #  #  #  #  :          0 :         if (internal && data.exists("label")) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1512   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_INVALID_PARAMETER, "Internal addresses should not have a label");
                   #  # ]
    1513                 :            :         }
    1514                 :            : 
    1515                 :            :         // Combo descriptor check
    1516   [ #  #  #  #  :          0 :         if (active && !parsed_desc->IsSingleType()) {
                   #  # ]
    1517   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Combo descriptors cannot be set to active");
                   #  # ]
    1518                 :            :         }
    1519                 :            : 
    1520                 :            :         // If the wallet disabled private keys, abort if private keys exist
    1521   [ #  #  #  #  :          0 :         if (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !keys.keys.empty()) {
                   #  # ]
    1522   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
                   #  # ]
    1523                 :            :         }
    1524                 :            : 
    1525                 :            :         // Need to ExpandPrivate to check if private keys are available for all pubkeys
    1526                 :          0 :         FlatSigningProvider expand_keys;
    1527                 :          0 :         std::vector<CScript> scripts;
    1528   [ #  #  #  # ]:          0 :         if (!parsed_desc->Expand(0, keys, scripts, expand_keys)) {
    1529   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Cannot expand descriptor. Probably because of hardened derivations without private keys provided");
                   #  # ]
    1530                 :            :         }
    1531         [ #  # ]:          0 :         parsed_desc->ExpandPrivate(0, keys, expand_keys);
    1532                 :            : 
    1533                 :            :         // Check if all private keys are provided
    1534                 :          0 :         bool have_all_privkeys = !expand_keys.keys.empty();
    1535         [ #  # ]:          0 :         for (const auto& entry : expand_keys.origins) {
    1536                 :          0 :             const CKeyID& key_id = entry.first;
    1537                 :          0 :             CKey key;
    1538   [ #  #  #  # ]:          0 :             if (!expand_keys.GetKey(key_id, key)) {
    1539                 :          0 :                 have_all_privkeys = false;
    1540                 :          0 :                 break;
    1541                 :            :             }
    1542                 :          0 :         }
    1543                 :            : 
    1544                 :            :         // If private keys are enabled, check some things.
    1545   [ #  #  #  # ]:          0 :         if (!wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
    1546         [ #  # ]:          0 :            if (keys.keys.empty()) {
    1547   [ #  #  #  #  :          0 :                 throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import descriptor without private keys to a wallet with private keys enabled");
                   #  # ]
    1548                 :            :            }
    1549         [ #  # ]:          0 :            if (!have_all_privkeys) {
    1550   [ #  #  #  # ]:          0 :                warnings.push_back("Not all private keys provided. Some wallet functionality may return unexpected errors");
    1551                 :          0 :            }
    1552                 :          0 :         }
    1553                 :            : 
    1554   [ #  #  #  # ]:          0 :         WalletDescriptor w_desc(std::move(parsed_desc), timestamp, range_start, range_end, next_index);
    1555                 :            : 
    1556                 :            :         // Check if the wallet already contains the descriptor
    1557         [ #  # ]:          0 :         auto existing_spk_manager = wallet.GetDescriptorScriptPubKeyMan(w_desc);
    1558         [ #  # ]:          0 :         if (existing_spk_manager) {
    1559   [ #  #  #  # ]:          0 :             if (!existing_spk_manager->CanUpdateToWalletDescriptor(w_desc, error)) {
    1560         [ #  # ]:          0 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, error);
    1561                 :            :             }
    1562                 :          0 :         }
    1563                 :            : 
    1564                 :            :         // Add descriptor to the wallet
    1565         [ #  # ]:          0 :         auto spk_manager = wallet.AddWalletDescriptor(w_desc, keys, label, internal);
    1566         [ #  # ]:          0 :         if (spk_manager == nullptr) {
    1567   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Could not add descriptor '%s'", descriptor));
                   #  # ]
    1568                 :            :         }
    1569                 :            : 
    1570                 :            :         // Set descriptor as active if necessary
    1571         [ #  # ]:          0 :         if (active) {
    1572   [ #  #  #  # ]:          0 :             if (!w_desc.descriptor->GetOutputType()) {
    1573   [ #  #  #  # ]:          0 :                 warnings.push_back("Unknown output type, cannot set descriptor to active.");
    1574                 :          0 :             } else {
    1575   [ #  #  #  #  :          0 :                 wallet.AddActiveScriptPubKeyMan(spk_manager->GetID(), *w_desc.descriptor->GetOutputType(), internal);
             #  #  #  # ]
    1576                 :            :             }
    1577                 :          0 :         } else {
    1578   [ #  #  #  # ]:          0 :             if (w_desc.descriptor->GetOutputType()) {
    1579   [ #  #  #  #  :          0 :                 wallet.DeactivateScriptPubKeyMan(spk_manager->GetID(), *w_desc.descriptor->GetOutputType(), internal);
             #  #  #  # ]
    1580                 :          0 :             }
    1581                 :            :         }
    1582                 :            : 
    1583   [ #  #  #  #  :          0 :         result.pushKV("success", UniValue(true));
                   #  # ]
    1584         [ #  # ]:          0 :     } catch (const UniValue& e) {
    1585   [ #  #  #  #  :          0 :         result.pushKV("success", UniValue(false));
                   #  # ]
    1586   [ #  #  #  #  :          0 :         result.pushKV("error", e);
                   #  # ]
    1587   [ #  #  #  # ]:          0 :     }
    1588         [ #  # ]:          0 :     PushWarnings(warnings, result);
    1589                 :          0 :     return result;
    1590         [ #  # ]:          0 : }
    1591                 :            : 
    1592                 :          0 : RPCHelpMan importdescriptors()
    1593                 :            : {
    1594   [ #  #  #  #  :          0 :     return RPCHelpMan{"importdescriptors",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1595         [ #  # ]:          0 :                 "\nImport descriptors. This will trigger a rescan of the blockchain based on the earliest timestamp of all descriptors being imported. Requires a new wallet backup.\n"
    1596                 :            :             "\nNote: This call can take over an hour to complete if using an early timestamp; during that time, other rpc calls\n"
    1597                 :            :             "may report that the imported keys, addresses or scripts exist but related transactions are still missing.\n"
    1598                 :            :             "The rescan is significantly faster if block filters are available (using startup option \"-blockfilterindex=1\").\n",
    1599         [ #  # ]:          0 :                 {
    1600   [ #  #  #  #  :          0 :                     {"requests", RPCArg::Type::ARR, RPCArg::Optional::NO, "Data to be imported",
                   #  # ]
    1601         [ #  # ]:          0 :                         {
    1602   [ #  #  #  #  :          0 :                             {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
                   #  # ]
    1603         [ #  # ]:          0 :                                 {
    1604   [ #  #  #  #  :          0 :                                     {"desc", RPCArg::Type::STR, RPCArg::Optional::NO, "Descriptor to import."},
                   #  # ]
    1605   [ #  #  #  #  :          0 :                                     {"active", RPCArg::Type::BOOL, RPCArg::Default{false}, "Set this descriptor to be the active descriptor for the corresponding output type/externality"},
             #  #  #  # ]
    1606   [ #  #  #  #  :          0 :                                     {"range", RPCArg::Type::RANGE, RPCArg::Optional::OMITTED, "If a ranged descriptor is used, this specifies the end or the range (in the form [begin,end]) to import"},
                   #  # ]
    1607   [ #  #  #  #  :          0 :                                     {"next_index", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "If a ranged descriptor is set to active, this specifies the next index to generate addresses from"},
                   #  # ]
    1608   [ #  #  #  #  :          0 :                                     {"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, "Time from which to start rescanning the blockchain for this descriptor, in " + UNIX_EPOCH_TIME + "\n"
             #  #  #  # ]
    1609                 :            :                                         "Use the string \"now\" to substitute the current synced blockchain time.\n"
    1610                 :            :                                         "\"now\" can be specified to bypass scanning, for outputs which are known to never have been used, and\n"
    1611                 :            :                                         "0 can be specified to scan the entire blockchain. Blocks up to 2 hours before the earliest timestamp\n"
    1612                 :            :                                         "of all descriptors being imported will be scanned as well as the mempool.",
    1613   [ #  #  #  #  :          0 :                                         RPCArgOptions{.type_str={"timestamp | \"now\"", "integer / string"}}
             #  #  #  # ]
    1614                 :            :                                     },
    1615   [ #  #  #  #  :          0 :                                     {"internal", RPCArg::Type::BOOL, RPCArg::Default{false}, "Whether matching outputs should be treated as not incoming payments (e.g. change)"},
             #  #  #  # ]
    1616   [ #  #  #  #  :          0 :                                     {"label", RPCArg::Type::STR, RPCArg::Default{""}, "Label to assign to the address, only allowed with internal=false. Disabled for ranged descriptors"},
             #  #  #  # ]
    1617                 :            :                                 },
    1618                 :            :                             },
    1619                 :            :                         },
    1620         [ #  # ]:          0 :                         RPCArgOptions{.oneline_description="requests"}},
    1621                 :            :                 },
    1622   [ #  #  #  # ]:          0 :                 RPCResult{
    1623   [ #  #  #  # ]:          0 :                     RPCResult::Type::ARR, "", "Response is an array with the same size as the input that has the execution result",
    1624         [ #  # ]:          0 :                     {
    1625   [ #  #  #  #  :          0 :                         {RPCResult::Type::OBJ, "", "",
                   #  # ]
    1626         [ #  # ]:          0 :                         {
    1627   [ #  #  #  #  :          0 :                             {RPCResult::Type::BOOL, "success", ""},
                   #  # ]
    1628   [ #  #  #  #  :          0 :                             {RPCResult::Type::ARR, "warnings", /*optional=*/true, "",
                   #  # ]
    1629         [ #  # ]:          0 :                             {
    1630   [ #  #  #  #  :          0 :                                 {RPCResult::Type::STR, "", ""},
                   #  # ]
    1631                 :            :                             }},
    1632   [ #  #  #  #  :          0 :                             {RPCResult::Type::OBJ, "error", /*optional=*/true, "",
                   #  # ]
    1633         [ #  # ]:          0 :                             {
    1634   [ #  #  #  #  :          0 :                                 {RPCResult::Type::ELISION, "", "JSONRPC error"},
                   #  # ]
    1635                 :            :                             }},
    1636                 :            :                         }},
    1637                 :            :                     }
    1638                 :            :                 },
    1639         [ #  # ]:          0 :                 RPCExamples{
    1640   [ #  #  #  #  :          0 :                     HelpExampleCli("importdescriptors", "'[{ \"desc\": \"<my descriptor>\", \"timestamp\":1455191478, \"internal\": true }, "
                   #  # ]
    1641         [ #  # ]:          0 :                                           "{ \"desc\": \"<my descriptor 2>\", \"label\": \"example 2\", \"timestamp\": 1455191480 }]'") +
    1642   [ #  #  #  #  :          0 :                     HelpExampleCli("importdescriptors", "'[{ \"desc\": \"<my descriptor>\", \"timestamp\":1455191478, \"active\": true, \"range\": [0,100], \"label\": \"<my bech32 wallet>\" }]'")
                   #  # ]
    1643                 :            :                 },
    1644         [ #  # ]:          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& main_request) -> UniValue
    1645                 :            : {
    1646                 :          0 :     std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(main_request);
    1647   [ #  #  #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
    1648                 :          0 :     CWallet& wallet{*pwallet};
    1649                 :            : 
    1650                 :            :     // Make sure the results are valid at least up to the most recent block
    1651                 :            :     // the user could have gotten from another RPC command prior to now
    1652         [ #  # ]:          0 :     wallet.BlockUntilSyncedToCurrentChain();
    1653                 :            : 
    1654                 :            :     //  Make sure wallet is a descriptor wallet
    1655   [ #  #  #  # ]:          0 :     if (!pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
    1656   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "importdescriptors is not available for non-descriptor wallets");
                   #  # ]
    1657                 :            :     }
    1658                 :            : 
    1659         [ #  # ]:          0 :     WalletRescanReserver reserver(*pwallet);
    1660         [ #  # ]:          0 :     if (!reserver.reserve(/*with_passphrase=*/true)) {
    1661   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
                   #  # ]
    1662                 :            :     }
    1663                 :            : 
    1664                 :            :     // Ensure that the wallet is not locked for the remainder of this RPC, as
    1665                 :            :     // the passphrase is used to top up the keypool.
    1666         [ #  # ]:          0 :     LOCK(pwallet->m_relock_mutex);
    1667                 :            : 
    1668         [ #  # ]:          0 :     const UniValue& requests = main_request.params[0];
    1669                 :          0 :     const int64_t minimum_timestamp = 1;
    1670                 :          0 :     int64_t now = 0;
    1671                 :          0 :     int64_t lowest_timestamp = 0;
    1672                 :          0 :     bool rescan = false;
    1673         [ #  # ]:          0 :     UniValue response(UniValue::VARR);
    1674                 :            :     {
    1675         [ #  # ]:          0 :         LOCK(pwallet->cs_wallet);
    1676         [ #  # ]:          0 :         EnsureWalletIsUnlocked(*pwallet);
    1677                 :            : 
    1678   [ #  #  #  #  :          0 :         CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(lowest_timestamp).mtpTime(now)));
                   #  # ]
    1679                 :            : 
    1680                 :            :         // Get all timestamps and extract the lowest timestamp
    1681   [ #  #  #  # ]:          0 :         for (const UniValue& request : requests.getValues()) {
    1682                 :            :             // This throws an error if "timestamp" doesn't exist
    1683         [ #  # ]:          0 :             const int64_t timestamp = std::max(GetImportTimestamp(request, now), minimum_timestamp);
    1684         [ #  # ]:          0 :             const UniValue result = ProcessDescriptorImport(*pwallet, request, timestamp);
    1685   [ #  #  #  # ]:          0 :             response.push_back(result);
    1686                 :            : 
    1687         [ #  # ]:          0 :             if (lowest_timestamp > timestamp ) {
    1688                 :          0 :                 lowest_timestamp = timestamp;
    1689                 :          0 :             }
    1690                 :            : 
    1691                 :            :             // If we know the chain tip, and at least one request was successful then allow rescan
    1692   [ #  #  #  #  :          0 :             if (!rescan && result["success"].get_bool()) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1693                 :          0 :                 rescan = true;
    1694                 :          0 :             }
    1695                 :          0 :         }
    1696         [ #  # ]:          0 :         pwallet->ConnectScriptPubKeyManNotifiers();
    1697                 :          0 :     }
    1698                 :            : 
    1699                 :            :     // Rescan the blockchain using the lowest timestamp
    1700         [ #  # ]:          0 :     if (rescan) {
    1701         [ #  # ]:          0 :         int64_t scanned_time = pwallet->RescanFromTime(lowest_timestamp, reserver, /*update=*/true);
    1702         [ #  # ]:          0 :         pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
    1703                 :            : 
    1704         [ #  # ]:          0 :         if (pwallet->IsAbortingRescan()) {
    1705   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");
                   #  # ]
    1706                 :            :         }
    1707                 :            : 
    1708         [ #  # ]:          0 :         if (scanned_time > lowest_timestamp) {
    1709   [ #  #  #  # ]:          0 :             std::vector<UniValue> results = response.getValues();
    1710         [ #  # ]:          0 :             response.clear();
    1711         [ #  # ]:          0 :             response.setArray();
    1712                 :            : 
    1713                 :            :             // Compose the response
    1714         [ #  # ]:          0 :             for (unsigned int i = 0; i < requests.size(); ++i) {
    1715   [ #  #  #  # ]:          0 :                 const UniValue& request = requests.getValues().at(i);
    1716                 :            : 
    1717                 :            :                 // If the descriptor timestamp is within the successfully scanned
    1718                 :            :                 // range, or if the import result already has an error set, let
    1719                 :            :                 // the result stand unmodified. Otherwise replace the result
    1720                 :            :                 // with an error message.
    1721   [ #  #  #  #  :          0 :                 if (scanned_time <= GetImportTimestamp(request, now) || results.at(i).exists("error")) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    1722   [ #  #  #  #  :          0 :                     response.push_back(results.at(i));
                   #  # ]
    1723                 :          0 :                 } else {
    1724         [ #  # ]:          0 :                     UniValue result = UniValue(UniValue::VOBJ);
    1725   [ #  #  #  #  :          0 :                     result.pushKV("success", UniValue(false));
                   #  # ]
    1726         [ #  # ]:          0 :                     result.pushKV(
    1727         [ #  # ]:          0 :                         "error",
    1728         [ #  # ]:          0 :                         JSONRPCError(
    1729                 :            :                             RPC_MISC_ERROR,
    1730         [ #  # ]:          0 :                             strprintf("Rescan failed for descriptor with timestamp %d. There was an error reading a "
    1731                 :            :                                       "block from time %d, which is after or within %d seconds of key creation, and "
    1732                 :            :                                       "could contain transactions pertaining to the desc. As a result, transactions "
    1733                 :            :                                       "and coins using this desc may not appear in the wallet. This error could be "
    1734                 :            :                                       "caused by pruning or data corruption (see bitcoind log for details) and could "
    1735                 :            :                                       "be dealt with by downloading and rescanning the relevant blocks (see -reindex "
    1736                 :            :                                       "option and rescanblockchain RPC).",
    1737         [ #  # ]:          0 :                                 GetImportTimestamp(request, now), scanned_time - TIMESTAMP_WINDOW - 1, TIMESTAMP_WINDOW)));
    1738         [ #  # ]:          0 :                     response.push_back(std::move(result));
    1739                 :          0 :                 }
    1740                 :          0 :             }
    1741                 :          0 :         }
    1742                 :          0 :     }
    1743                 :            : 
    1744                 :          0 :     return response;
    1745         [ #  # ]:          0 : },
    1746                 :            :     };
    1747                 :          0 : }
    1748                 :            : 
    1749                 :          0 : RPCHelpMan listdescriptors()
    1750                 :            : {
    1751   [ #  #  #  #  :          0 :     return RPCHelpMan{
          #  #  #  #  #  
                #  #  # ]
    1752         [ #  # ]:          0 :         "listdescriptors",
    1753         [ #  # ]:          0 :         "\nList descriptors imported into a descriptor-enabled wallet.\n",
    1754         [ #  # ]:          0 :         {
    1755   [ #  #  #  #  :          0 :             {"private", RPCArg::Type::BOOL, RPCArg::Default{false}, "Show private descriptors."}
             #  #  #  # ]
    1756                 :            :         },
    1757   [ #  #  #  #  :          0 :         RPCResult{RPCResult::Type::OBJ, "", "", {
          #  #  #  #  #  
                      # ]
    1758   [ #  #  #  #  :          0 :             {RPCResult::Type::STR, "wallet_name", "Name of wallet this operation was performed on"},
                   #  # ]
    1759   [ #  #  #  #  :          0 :             {RPCResult::Type::ARR, "descriptors", "Array of descriptor objects (sorted by descriptor string representation)",
                   #  # ]
    1760         [ #  # ]:          0 :             {
    1761   [ #  #  #  #  :          0 :                 {RPCResult::Type::OBJ, "", "", {
             #  #  #  # ]
    1762   [ #  #  #  #  :          0 :                     {RPCResult::Type::STR, "desc", "Descriptor string representation"},
                   #  # ]
    1763   [ #  #  #  #  :          0 :                     {RPCResult::Type::NUM, "timestamp", "The creation time of the descriptor"},
                   #  # ]
    1764   [ #  #  #  #  :          0 :                     {RPCResult::Type::BOOL, "active", "Whether this descriptor is currently used to generate new addresses"},
                   #  # ]
    1765   [ #  #  #  #  :          0 :                     {RPCResult::Type::BOOL, "internal", /*optional=*/true, "True if this descriptor is used to generate change addresses. False if this descriptor is used to generate receiving addresses; defined only for active descriptors"},
                   #  # ]
    1766   [ #  #  #  #  :          0 :                     {RPCResult::Type::ARR_FIXED, "range", /*optional=*/true, "Defined only for ranged descriptors", {
             #  #  #  # ]
    1767   [ #  #  #  #  :          0 :                         {RPCResult::Type::NUM, "", "Range start inclusive"},
                   #  # ]
    1768   [ #  #  #  #  :          0 :                         {RPCResult::Type::NUM, "", "Range end inclusive"},
                   #  # ]
    1769                 :            :                     }},
    1770   [ #  #  #  #  :          0 :                     {RPCResult::Type::NUM, "next", /*optional=*/true, "Same as next_index field. Kept for compatibility reason."},
                   #  # ]
    1771   [ #  #  #  #  :          0 :                     {RPCResult::Type::NUM, "next_index", /*optional=*/true, "The next index to generate addresses from; defined only for ranged descriptors"},
                   #  # ]
    1772                 :            :                 }},
    1773                 :            :             }}
    1774                 :            :         }},
    1775         [ #  # ]:          0 :         RPCExamples{
    1776   [ #  #  #  #  :          0 :             HelpExampleCli("listdescriptors", "") + HelpExampleRpc("listdescriptors", "")
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1777   [ #  #  #  #  :          0 :             + HelpExampleCli("listdescriptors", "true") + HelpExampleRpc("listdescriptors", "true")
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1778                 :            :         },
    1779         [ #  # ]:          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
    1780                 :            : {
    1781                 :          0 :     const std::shared_ptr<const CWallet> wallet = GetWalletForJSONRPCRequest(request);
    1782   [ #  #  #  # ]:          0 :     if (!wallet) return UniValue::VNULL;
    1783                 :            : 
    1784   [ #  #  #  # ]:          0 :     if (!wallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
    1785   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "listdescriptors is not available for non-descriptor wallets");
                   #  # ]
    1786                 :            :     }
    1787                 :            : 
    1788   [ #  #  #  #  :          0 :     const bool priv = !request.params[0].isNull() && request.params[0].get_bool();
             #  #  #  # ]
    1789         [ #  # ]:          0 :     if (priv) {
    1790         [ #  # ]:          0 :         EnsureWalletIsUnlocked(*wallet);
    1791                 :          0 :     }
    1792                 :            : 
    1793         [ #  # ]:          0 :     LOCK(wallet->cs_wallet);
    1794                 :            : 
    1795         [ #  # ]:          0 :     const auto active_spk_mans = wallet->GetActiveScriptPubKeyMans();
    1796                 :            : 
    1797                 :            :     struct WalletDescInfo {
    1798                 :            :         std::string descriptor;
    1799                 :            :         uint64_t creation_time;
    1800                 :            :         bool active;
    1801                 :            :         std::optional<bool> internal;
    1802                 :            :         std::optional<std::pair<int64_t,int64_t>> range;
    1803                 :            :         int64_t next_index;
    1804                 :            :     };
    1805                 :            : 
    1806                 :          0 :     std::vector<WalletDescInfo> wallet_descriptors;
    1807   [ #  #  #  # ]:          0 :     for (const auto& spk_man : wallet->GetAllScriptPubKeyMans()) {
    1808         [ #  # ]:          0 :         const auto desc_spk_man = dynamic_cast<DescriptorScriptPubKeyMan*>(spk_man);
    1809         [ #  # ]:          0 :         if (!desc_spk_man) {
    1810   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Unexpected ScriptPubKey manager type.");
                   #  # ]
    1811                 :            :         }
    1812         [ #  # ]:          0 :         LOCK(desc_spk_man->cs_desc_man);
    1813         [ #  # ]:          0 :         const auto& wallet_descriptor = desc_spk_man->GetWalletDescriptor();
    1814                 :          0 :         std::string descriptor;
    1815   [ #  #  #  # ]:          0 :         if (!desc_spk_man->GetDescriptorString(descriptor, priv)) {
    1816   [ #  #  #  #  :          0 :             throw JSONRPCError(RPC_WALLET_ERROR, "Can't get descriptor string.");
                   #  # ]
    1817                 :            :         }
    1818         [ #  # ]:          0 :         const bool is_range = wallet_descriptor.descriptor->IsRange();
    1819   [ #  #  #  # ]:          0 :         wallet_descriptors.push_back({
    1820         [ #  # ]:          0 :             descriptor,
    1821                 :          0 :             wallet_descriptor.creation_time,
    1822         [ #  # ]:          0 :             active_spk_mans.count(desc_spk_man) != 0,
    1823         [ #  # ]:          0 :             wallet->IsInternalScriptPubKeyMan(desc_spk_man),
    1824   [ #  #  #  #  :          0 :             is_range ? std::optional(std::make_pair(wallet_descriptor.range_start, wallet_descriptor.range_end)) : std::nullopt,
             #  #  #  # ]
    1825                 :          0 :             wallet_descriptor.next_index
    1826                 :            :         });
    1827                 :          0 :     }
    1828                 :            : 
    1829         [ #  # ]:          0 :     std::sort(wallet_descriptors.begin(), wallet_descriptors.end(), [](const auto& a, const auto& b) {
    1830                 :          0 :         return a.descriptor < b.descriptor;
    1831                 :            :     });
    1832                 :            : 
    1833         [ #  # ]:          0 :     UniValue descriptors(UniValue::VARR);
    1834         [ #  # ]:          0 :     for (const WalletDescInfo& info : wallet_descriptors) {
    1835         [ #  # ]:          0 :         UniValue spk(UniValue::VOBJ);
    1836   [ #  #  #  #  :          0 :         spk.pushKV("desc", info.descriptor);
                   #  # ]
    1837   [ #  #  #  #  :          0 :         spk.pushKV("timestamp", info.creation_time);
                   #  # ]
    1838   [ #  #  #  #  :          0 :         spk.pushKV("active", info.active);
                   #  # ]
    1839         [ #  # ]:          0 :         if (info.internal.has_value()) {
    1840   [ #  #  #  #  :          0 :             spk.pushKV("internal", info.internal.value());
             #  #  #  # ]
    1841                 :          0 :         }
    1842         [ #  # ]:          0 :         if (info.range.has_value()) {
    1843         [ #  # ]:          0 :             UniValue range(UniValue::VARR);
    1844   [ #  #  #  #  :          0 :             range.push_back(info.range->first);
                   #  # ]
    1845   [ #  #  #  #  :          0 :             range.push_back(info.range->second - 1);
                   #  # ]
    1846   [ #  #  #  #  :          0 :             spk.pushKV("range", range);
                   #  # ]
    1847   [ #  #  #  #  :          0 :             spk.pushKV("next", info.next_index);
                   #  # ]
    1848   [ #  #  #  #  :          0 :             spk.pushKV("next_index", info.next_index);
                   #  # ]
    1849                 :          0 :         }
    1850   [ #  #  #  # ]:          0 :         descriptors.push_back(spk);
    1851                 :          0 :     }
    1852                 :            : 
    1853         [ #  # ]:          0 :     UniValue response(UniValue::VOBJ);
    1854   [ #  #  #  #  :          0 :     response.pushKV("wallet_name", wallet->GetName());
                   #  # ]
    1855   [ #  #  #  #  :          0 :     response.pushKV("descriptors", descriptors);
                   #  # ]
    1856                 :            : 
    1857                 :          0 :     return response;
    1858         [ #  # ]:          0 : },
    1859                 :            :     };
    1860                 :          0 : }
    1861                 :            : 
    1862                 :          0 : RPCHelpMan backupwallet()
    1863                 :            : {
    1864   [ #  #  #  #  :          0 :     return RPCHelpMan{"backupwallet",
                   #  # ]
    1865         [ #  # ]:          0 :                 "\nSafely copies the current wallet file to the specified destination, which can either be a directory or a path with a filename.\n",
    1866         [ #  # ]:          0 :                 {
    1867   [ #  #  #  #  :          0 :                     {"destination", RPCArg::Type::STR, RPCArg::Optional::NO, "The destination directory or file"},
                   #  # ]
    1868                 :            :                 },
    1869   [ #  #  #  #  :          0 :                 RPCResult{RPCResult::Type::NONE, "", ""},
             #  #  #  # ]
    1870         [ #  # ]:          0 :                 RPCExamples{
    1871   [ #  #  #  #  :          0 :                     HelpExampleCli("backupwallet", "\"backup.dat\"")
                   #  # ]
    1872   [ #  #  #  #  :          0 :             + HelpExampleRpc("backupwallet", "\"backup.dat\"")
             #  #  #  # ]
    1873                 :            :                 },
    1874         [ #  # ]:          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
    1875                 :            : {
    1876                 :          0 :     const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
    1877   [ #  #  #  # ]:          0 :     if (!pwallet) return UniValue::VNULL;
    1878                 :            : 
    1879                 :            :     // Make sure the results are valid at least up to the most recent block
    1880                 :            :     // the user could have gotten from another RPC command prior to now
    1881         [ #  # ]:          0 :     pwallet->BlockUntilSyncedToCurrentChain();
    1882                 :            : 
    1883         [ #  # ]:          0 :     LOCK(pwallet->cs_wallet);
    1884                 :            : 
    1885   [ #  #  #  #  :          0 :     std::string strDest = request.params[0].get_str();
                   #  # ]
    1886   [ #  #  #  # ]:          0 :     if (!pwallet->BackupWallet(strDest)) {
    1887   [ #  #  #  #  :          0 :         throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!");
             #  #  #  # ]
    1888                 :            :     }
    1889                 :            : 
    1890         [ #  # ]:          0 :     return UniValue::VNULL;
    1891                 :          0 : },
    1892                 :            :     };
    1893                 :          0 : }
    1894                 :            : 
    1895                 :            : 
    1896                 :          0 : RPCHelpMan restorewallet()
    1897                 :            : {
    1898   [ #  #  #  #  :          0 :     return RPCHelpMan{
             #  #  #  # ]
    1899         [ #  # ]:          0 :         "restorewallet",
    1900         [ #  # ]:          0 :         "\nRestores and loads a wallet from backup.\n"
    1901                 :            :         "\nThe rescan is significantly faster if a descriptor wallet is restored"
    1902                 :            :         "\nand block filters are available (using startup option \"-blockfilterindex=1\").\n",
    1903         [ #  # ]:          0 :         {
    1904   [ #  #  #  #  :          0 :             {"wallet_name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name that will be applied to the restored wallet"},
                   #  # ]
    1905   [ #  #  #  #  :          0 :             {"backup_file", RPCArg::Type::STR, RPCArg::Optional::NO, "The backup file that will be used to restore the wallet."},
                   #  # ]
    1906   [ #  #  #  #  :          0 :             {"load_on_startup", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED, "Save wallet name to persistent settings and load on startup. True to add wallet to startup list, false to remove, null to leave unchanged."},
                   #  # ]
    1907                 :            :         },
    1908   [ #  #  #  # ]:          0 :         RPCResult{
    1909   [ #  #  #  # ]:          0 :             RPCResult::Type::OBJ, "", "",
    1910         [ #  # ]:          0 :             {
    1911   [ #  #  #  #  :          0 :                 {RPCResult::Type::STR, "name", "The wallet name if restored successfully."},
                   #  # ]
    1912   [ #  #  #  #  :          0 :                 {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to restoring and loading the wallet.",
                   #  # ]
    1913         [ #  # ]:          0 :                 {
    1914   [ #  #  #  #  :          0 :                     {RPCResult::Type::STR, "", ""},
                   #  # ]
    1915                 :            :                 }},
    1916                 :            :             }
    1917                 :            :         },
    1918         [ #  # ]:          0 :         RPCExamples{
    1919   [ #  #  #  #  :          0 :             HelpExampleCli("restorewallet", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
                   #  # ]
    1920   [ #  #  #  #  :          0 :             + HelpExampleRpc("restorewallet", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
             #  #  #  # ]
    1921   [ #  #  #  #  :          0 :             + HelpExampleCliNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak\""}, {"load_on_startup", true}})
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1922   [ #  #  #  #  :          0 :             + HelpExampleRpcNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak\""}, {"load_on_startup", true}})
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1923                 :            :         },
    1924         [ #  # ]:          0 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
    1925                 :            : {
    1926                 :            : 
    1927                 :          0 :     WalletContext& context = EnsureWalletContext(request.context);
    1928                 :            : 
    1929                 :          0 :     auto backup_file = fs::u8path(request.params[1].get_str());
    1930                 :            : 
    1931   [ #  #  #  #  :          0 :     std::string wallet_name = request.params[0].get_str();
                   #  # ]
    1932                 :            : 
    1933   [ #  #  #  #  :          0 :     std::optional<bool> load_on_start = request.params[2].isNull() ? std::nullopt : std::optional<bool>(request.params[2].get_bool());
          #  #  #  #  #  
                      # ]
    1934                 :            : 
    1935                 :            :     DatabaseStatus status;
    1936                 :          0 :     bilingual_str error;
    1937                 :          0 :     std::vector<bilingual_str> warnings;
    1938                 :            : 
    1939         [ #  # ]:          0 :     const std::shared_ptr<CWallet> wallet = RestoreWallet(context, backup_file, wallet_name, load_on_start, status, error, warnings);
    1940                 :            : 
    1941         [ #  # ]:          0 :     HandleWalletError(wallet, status, error);
    1942                 :            : 
    1943         [ #  # ]:          0 :     UniValue obj(UniValue::VOBJ);
    1944   [ #  #  #  #  :          0 :     obj.pushKV("name", wallet->GetName());
                   #  # ]
    1945         [ #  # ]:          0 :     PushWarnings(warnings, obj);
    1946                 :            : 
    1947                 :          0 :     return obj;
    1948                 :            : 
    1949         [ #  # ]:          0 : },
    1950                 :            :     };
    1951                 :          0 : }
    1952                 :            : } // namespace wallet

Generated by: LCOV version 1.14