Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
501{RPCResult::Type::NUM, "next_index", /*optional=*/true, "The next index to generate addresses from; defined only for ranged descriptors"},502}},503}}504}},505RPCExamples{506HelpExampleCli("listdescriptors", "") + HelpExampleRpc("listdescriptors", "")507+ HelpExampleCli("listdescriptors", "true") + HelpExampleRpc("listdescriptors", "true")508},509[](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue510{511const std::shared_ptr<const CWallet> wallet = GetWalletForJSONRPCRequest(request);512if (!wallet) return UniValue::VNULL;514const bool priv = !request.params[0].isNull() && request.params[0].get_bool();515if (wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && priv) {516throw JSONRPCError(RPC_WALLET_ERROR, "Can't get private descriptor string for watch-only wallets");517}518if (priv) {519EnsureWalletIsUnlocked(*wallet);520}522LOCK(wallet->cs_wallet);523util::Expected<std::vector<WalletDescInfo>, std::string> exported = ExportDescriptors(*wallet, priv);524if (!exported) {525throw JSONRPCError(RPC_WALLET_ERROR, exported.error());526}527std::vector<WalletDescInfo> wallet_descriptors = *exported;529std::sort(wallet_descriptors.begin(), wallet_descriptors.end(), [](const auto& a, const auto& b) {530return a.descriptor < b.descriptor;531});533UniValue descriptors(UniValue::VARR);534for (const WalletDescInfo& info : wallet_descriptors) {535UniValue spk(UniValue::VOBJ);536spk.pushKV("desc", info.descriptor);537spk.pushKV("timestamp", info.creation_time);538spk.pushKV("active", info.active);539if (info.internal.has_value()) {540spk.pushKV("internal", info.internal.value());541}542if (info.range.has_value()) {543UniValue range(UniValue::VARR);544range.push_back(info.range->first);545range.push_back(info.range->second - 1);546spk.pushKV("range", std::move(range));547spk.pushKV("next", info.next_index);548spk.pushKV("next_index", info.next_index);549}550descriptors.push_back(std::move(spk));551}553UniValue response(UniValue::VOBJ);554response.pushKV("wallet_name", wallet->GetName());555response.pushKV("descriptors", std::move(descriptors));557return response;558},559};560}562RPCMethod backupwallet()563{564return RPCMethod{565"backupwallet",566"Safely copies the current wallet file to the specified destination, which can either be a directory or a path with a filename.\n",567{568{"destination", RPCArg::Type::STR, RPCArg::Optional::NO, "The destination directory or file"},569},570RPCResult{RPCResult::Type::NONE, "", ""},571RPCExamples{572HelpExampleCli("backupwallet", "\"backup.dat\"")573+ HelpExampleRpc("backupwallet", "\"backup.dat\"")574},575[](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue576{577const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);578if (!pwallet) return UniValue::VNULL;580// Make sure the results are valid at least up to the most recent block581// the user could have gotten from another RPC command prior to now582pwallet->BlockUntilSyncedToCurrentChain();584LOCK(pwallet->cs_wallet);586std::string strDest = request.params[0].get_str();587if (!pwallet->BackupWallet(strDest)) {588throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!");589}591return UniValue::VNULL;592},593};594}597RPCMethod restorewallet()598{599return RPCMethod{600"restorewallet",601"Restores and loads a wallet from backup.\n"602"\nThe rescan is significantly faster if block filters are available"603"\n(using startup option \"-blockfilterindex=1\").\n",604{605{"wallet_name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name that will be applied to the restored wallet"},606{"backup_file", RPCArg::Type::STR, RPCArg::Optional::NO, "The backup file that will be used to restore the wallet."},607{"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."},608},609RPCResult{610RPCResult::Type::OBJ, "", "",611{612{RPCResult::Type::STR, "name", "The wallet name if restored successfully."},613{RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to restoring and loading the wallet.",614{615{RPCResult::Type::STR, "", ""},616}},617}618},619RPCExamples{620HelpExampleCli("restorewallet", "\"testwallet\" \"home\\backups\\backup-file.bak\"")621+ HelpExampleRpc("restorewallet", R"("testwallet", "home\\backups\\backup-file.bak")")622+ HelpExampleCliNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak"}, {"load_on_startup", true}})623+ HelpExampleRpcNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak"}, {"load_on_startup", true}})624},625[](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue626{628WalletContext& context = EnsureWalletContext(request.context);630auto backup_file = fs::u8path(request.params[1].get_str());632std::string wallet_name = request.params[0].get_str();634std::optional<bool> load_on_start = request.params[2].isNull() ? std::nullopt : std::optional<bool>(request.params[2].get_bool());636DatabaseStatus status;637bilingual_str error;638std::vector<bilingual_str> warnings;640const std::shared_ptr<CWallet> wallet = RestoreWallet(context, backup_file, wallet_name, load_on_start, status, error, warnings);642HandleWalletError(wallet, status, error);644UniValue obj(UniValue::VOBJ);645obj.pushKV("name", wallet->GetName());646PushWarnings(warnings, obj);648return obj;650},651};652}653} // namespace wallet