Bitcoin Core  29.1.0
P2P Digital Currency
util.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-present The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <wallet/rpc/util.h>
6 
7 #include <common/url.h>
8 #include <rpc/util.h>
9 #include <util/any.h>
10 #include <util/translation.h>
11 #include <wallet/context.h>
12 #include <wallet/wallet.h>
13 
14 #include <string_view>
15 #include <univalue.h>
16 
17 namespace wallet {
18 static const std::string WALLET_ENDPOINT_BASE = "/wallet/";
19 const std::string HELP_REQUIRING_PASSPHRASE{"\nRequires wallet passphrase to be set with walletpassphrase call if wallet is encrypted.\n"};
20 
21 bool GetAvoidReuseFlag(const CWallet& wallet, const UniValue& param) {
22  bool can_avoid_reuse = wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
23  bool avoid_reuse = param.isNull() ? can_avoid_reuse : param.get_bool();
24 
25  if (avoid_reuse && !can_avoid_reuse) {
26  throw JSONRPCError(RPC_WALLET_ERROR, "wallet does not have the \"avoid reuse\" feature enabled");
27  }
28 
29  return avoid_reuse;
30 }
31 
36 bool ParseIncludeWatchonly(const UniValue& include_watchonly, const CWallet& wallet)
37 {
38  if (include_watchonly.isNull()) {
39  // if include_watchonly isn't explicitly set, then check if we have a watchonly wallet
40  return wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
41  }
42 
43  // otherwise return whatever include_watchonly was set to
44  return include_watchonly.get_bool();
45 }
46 
47 bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& wallet_name)
48 {
49  if (request.URI.starts_with(WALLET_ENDPOINT_BASE)) {
50  // wallet endpoint was used
51  wallet_name = UrlDecode(std::string_view{request.URI}.substr(WALLET_ENDPOINT_BASE.size()));
52  return true;
53  }
54  return false;
55 }
56 
57 std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
58 {
60  WalletContext& context = EnsureWalletContext(request.context);
61 
62  std::string wallet_name;
63  if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
64  std::shared_ptr<CWallet> pwallet = GetWallet(context, wallet_name);
65  if (!pwallet) throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
66  return pwallet;
67  }
68 
69  size_t count{0};
70  auto wallet = GetDefaultWallet(context, count);
71  if (wallet) return wallet;
72 
73  if (count == 0) {
74  throw JSONRPCError(
75  RPC_WALLET_NOT_FOUND, "No wallet is loaded. Load a wallet using loadwallet or create a new one with createwallet. (Note: A default wallet is no longer automatically created)");
76  }
78  "Multiple wallets are loaded. Please select which wallet to use by requesting the RPC through the /wallet/<walletname> URI path.");
79 }
80 
82 {
83  if (wallet.IsLocked()) {
84  throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please enter the wallet passphrase with walletpassphrase first.");
85  }
86 }
87 
88 WalletContext& EnsureWalletContext(const std::any& context)
89 {
90  auto wallet_context = util::AnyPtr<WalletContext>(context);
91  if (!wallet_context) {
92  throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet context not found");
93  }
94  return *wallet_context;
95 }
96 
97 // also_create should only be set to true only when the RPC is expected to add things to a blank wallet and make it no longer blank
99 {
100  LegacyScriptPubKeyMan* spk_man = wallet.GetLegacyScriptPubKeyMan();
101  if (!spk_man && also_create) {
102  spk_man = wallet.GetOrCreateLegacyScriptPubKeyMan();
103  }
104  if (!spk_man) {
105  throw JSONRPCError(RPC_WALLET_ERROR, "Only legacy wallets are supported by this command");
106  }
107  return *spk_man;
108 }
109 
111 {
112  const LegacyScriptPubKeyMan* spk_man = wallet.GetLegacyScriptPubKeyMan();
113  if (!spk_man) {
114  throw JSONRPCError(RPC_WALLET_ERROR, "Only legacy wallets are supported by this command");
115  }
116  return *spk_man;
117 }
118 
119 std::string LabelFromValue(const UniValue& value)
120 {
121  static const std::string empty_string;
122  if (value.isNull()) return empty_string;
123 
124  const std::string& label{value.get_str()};
125  if (label == "*")
126  throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, "Invalid label name");
127  return label;
128 }
129 
130 void PushParentDescriptors(const CWallet& wallet, const CScript& script_pubkey, UniValue& entry)
131 {
132  UniValue parent_descs(UniValue::VARR);
133  for (const auto& desc: wallet.GetWalletDescriptors(script_pubkey)) {
134  parent_descs.push_back(desc.descriptor->ToString());
135  }
136  entry.pushKV("parent_descs", std::move(parent_descs));
137 }
138 
139 void HandleWalletError(const std::shared_ptr<CWallet> wallet, DatabaseStatus& status, bilingual_str& error)
140 {
141  if (!wallet) {
142  // Map bad format to not found, since bad format is returned when the
143  // wallet directory exists, but doesn't contain a data file.
145  switch (status) {
148  code = RPC_WALLET_NOT_FOUND;
149  break;
152  break;
155  break;
157  code = RPC_INVALID_PARAMETER;
158  break;
159  default: // RPC_WALLET_ERROR is returned for all other cases.
160  break;
161  }
162  throw JSONRPCError(code, error.original);
163  }
164 }
165 
167 {
168  AssertLockHeld(wallet.cs_wallet);
169  UniValue lastprocessedblock{UniValue::VOBJ};
170  lastprocessedblock.pushKV("hash", wallet.GetLastBlockHash().GetHex());
171  lastprocessedblock.pushKV("height", wallet.GetLastBlockHeight());
172  entry.pushKV("lastprocessedblock", std::move(lastprocessedblock));
173 }
174 
175 } // namespace wallet
No wallet specified (error when there are multiple wallets loaded)
Definition: protocol.h:81
void push_back(UniValue val)
Definition: univalue.cpp:104
std::any context
Definition: request.h:45
AssertLockHeld(pool.cs)
Enter the wallet passphrase with walletpassphrase first.
Definition: protocol.h:75
bool get_bool() const
Bilingual messages:
Definition: translation.h:24
#define CHECK_NONFATAL(condition)
Identity function.
Definition: check.h:81
bool ParseIncludeWatchonly(const UniValue &include_watchonly, const CWallet &wallet)
Used by RPC commands that have an include_watchonly parameter.
Definition: util.cpp:36
const std::string & get_str() const
std::string UrlDecode(std::string_view url_encoded)
Definition: url.cpp:12
This same wallet is already loaded.
Definition: protocol.h:82
Invalid, missing or duplicate parameter.
Definition: protocol.h:44
void HandleWalletError(const std::shared_ptr< CWallet > wallet, DatabaseStatus &status, bilingual_str &error)
Definition: util.cpp:139
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:70
std::string LabelFromValue(const UniValue &value)
Definition: util.cpp:119
enum JSONRPCRequest::Mode mode
WalletContext & EnsureWalletContext(const std::any &context)
Definition: util.cpp:88
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:299
Invalid wallet specified.
Definition: protocol.h:80
const std::string HELP_REQUIRING_PASSPHRASE
Definition: util.cpp:19
bool isNull() const
Definition: univalue.h:79
LegacyScriptPubKeyMan & EnsureLegacyScriptPubKeyMan(CWallet &wallet, bool also_create)
Definition: util.cpp:98
std::shared_ptr< CWallet > GetWallet(WalletContext &context, const std::string &name)
Definition: wallet.cpp:204
DatabaseStatus
Definition: db.h:205
std::string original
Definition: translation.h:25
void pushKV(std::string key, UniValue val)
Definition: univalue.cpp:126
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
std::string URI
Definition: request.h:42
WalletContext struct containing references to state shared between CWallet instances, like the reference to the chain interface, and the list of opened wallets.
Definition: context.h:36
bool GetAvoidReuseFlag(const CWallet &wallet, const UniValue &param)
Definition: util.cpp:21
void EnsureWalletIsUnlocked(const CWallet &wallet)
Definition: util.cpp:81
static int count
Wallet errors.
Definition: protocol.h:71
There is already a wallet with the same name.
Definition: protocol.h:83
RPCErrorCode
Bitcoin RPC error codes.
Definition: protocol.h:24
void PushParentDescriptors(const CWallet &wallet, const CScript &script_pubkey, UniValue &entry)
Fetch parent descriptors of this scriptPubKey.
Definition: util.cpp:130
const LegacyScriptPubKeyMan & EnsureConstLegacyScriptPubKeyMan(const CWallet &wallet)
Definition: util.cpp:110
std::shared_ptr< CWallet > GetDefaultWallet(WalletContext &context, size_t &count)
Definition: wallet.cpp:197
Invalid label name.
Definition: protocol.h:73
static const std::string WALLET_ENDPOINT_BASE
Definition: util.cpp:18
void AppendLastProcessedBlock(UniValue &entry, const CWallet &wallet)
Definition: util.cpp:166
std::shared_ptr< CWallet > GetWalletForJSONRPCRequest(const JSONRPCRequest &request)
Figures out what wallet, if any, to use for a JSONRPCRequest.
Definition: util.cpp:57
bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest &request, std::string &wallet_name)
Definition: util.cpp:47