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