Bitcoin ABC 0.32.4
P2P Digital Currency
util.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2021 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 <rpc/util.h>
8#include <util/any.h>
9#include <util/translation.h>
10#include <util/url.h>
11#include <wallet/context.h>
12#include <wallet/wallet.h>
13
14#include <univalue.h>
15
16static const std::string WALLET_ENDPOINT_BASE = "/wallet/";
17const std::string HELP_REQUIRING_PASSPHRASE{
18 "\nRequires wallet passphrase to be set with walletpassphrase call if "
19 "wallet is encrypted.\n"};
20
21bool GetAvoidReuseFlag(const CWallet *const pwallet, const UniValue &param) {
22 bool can_avoid_reuse = pwallet->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(
28 "wallet does not have the \"avoid reuse\" feature enabled");
29 }
30
31 return avoid_reuse;
32}
33
38bool ParseIncludeWatchonly(const UniValue &include_watchonly,
39 const CWallet &pwallet) {
40 if (include_watchonly.isNull()) {
41 // if include_watchonly isn't explicitly set, then check if we have a
42 // watchonly wallet
44 }
45
46 // otherwise return whatever include_watchonly was set to
47 return include_watchonly.get_bool();
48}
49
51 std::string &wallet_name) {
52 if (request.URI.substr(0, WALLET_ENDPOINT_BASE.size()) ==
54 // wallet endpoint was used
55 wallet_name =
56 urlDecode(request.URI.substr(WALLET_ENDPOINT_BASE.size()));
57 return true;
58 }
59 return false;
60}
61
62std::shared_ptr<CWallet>
65 WalletContext &context = EnsureWalletContext(request.context);
66
67 std::string wallet_name;
68 if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
69 std::shared_ptr<CWallet> pwallet = GetWallet(context, wallet_name);
70 if (!pwallet) {
71 throw JSONRPCError(
73 "Requested wallet does not exist or is not loaded");
74 }
75 return pwallet;
76 }
77
78 std::vector<std::shared_ptr<CWallet>> wallets = GetWallets(context);
79 if (wallets.size() == 1) {
80 return wallets[0];
81 }
82
83 if (wallets.empty()) {
84 throw JSONRPCError(
86 "No wallet is loaded. Load a wallet using loadwallet or create a "
87 "new one with createwallet. (Note: A default wallet is no longer "
88 "automatically created)");
89 }
90
92 "Wallet file not specified (must request wallet RPC "
93 "through /wallet/<filename> uri-path).");
94}
95
96void EnsureWalletIsUnlocked(const CWallet *pwallet) {
97 if (pwallet->IsLocked()) {
99 "Error: Please enter the wallet passphrase with "
100 "walletpassphrase first.");
101 }
102}
103
104WalletContext &EnsureWalletContext(const std::any &context) {
105 auto wallet_context = util::AnyPtr<WalletContext>(context);
106 if (!wallet_context) {
107 throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet context not found");
108 }
109 return *wallet_context;
110}
111
112// also_create should only be set to true only when the RPC is expected to add
113// things to a blank wallet and make it no longer blank
115 bool also_create) {
116 LegacyScriptPubKeyMan *spk_man = wallet.GetLegacyScriptPubKeyMan();
117 if (!spk_man && also_create) {
118 spk_man = wallet.GetOrCreateLegacyScriptPubKeyMan();
119 }
120 if (!spk_man) {
122 "This type of wallet does not support this command");
123 }
124 return *spk_man;
125}
126
127std::string LabelFromValue(const UniValue &value) {
128 std::string label = value.get_str();
129 if (label == "*") {
130 throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, "Invalid label name");
131 }
132 return label;
133}
134
135void HandleWalletError(const std::shared_ptr<CWallet> wallet,
136 DatabaseStatus &status, bilingual_str &error) {
137 if (!wallet) {
138 // Map bad format to not found, since bad format is returned
139 // when the wallet directory exists, but doesn't contain a data
140 // file.
142 switch (status) {
146 break;
149 break;
152 break;
155 break;
156 default:
157 // RPC_WALLET_ERROR is returned for all other cases.
158 break;
159 }
160 throw JSONRPCError(code, error.original);
161 }
162}
#define CHECK_NONFATAL(condition)
Identity function.
Definition: check.h:53
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:269
bool IsLocked() const override
Definition: wallet.cpp:3262
enum JSONRPCRequest::Mode mode
std::string URI
Definition: request.h:36
std::any context
Definition: request.h:39
const std::string & get_str() const
bool isNull() const
Definition: univalue.h:104
bool get_bool() const
bool IsWalletFlagSet(uint64_t flag) const override
Check if a certain wallet flag is set.
Definition: wallet.cpp:1589
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:58
RPCErrorCode
Bitcoin RPC error codes.
Definition: protocol.h:22
@ RPC_WALLET_NOT_SPECIFIED
No wallet specified (error when there are multiple wallets loaded)
Definition: protocol.h:111
@ RPC_WALLET_INVALID_LABEL_NAME
Invalid label name.
Definition: protocol.h:94
@ RPC_WALLET_UNLOCK_NEEDED
Enter the wallet passphrase with walletpassphrase first.
Definition: protocol.h:98
@ RPC_WALLET_ALREADY_EXISTS
There is already a wallet with the same name.
Definition: protocol.h:115
@ RPC_INVALID_PARAMETER
Invalid, missing or duplicate parameter.
Definition: protocol.h:46
@ RPC_WALLET_ERROR
Wallet errors Unspecified problem with wallet (key not found etc.)
Definition: protocol.h:90
@ RPC_WALLET_ALREADY_LOADED
This same wallet is already loaded.
Definition: protocol.h:113
@ RPC_WALLET_NOT_FOUND
Invalid wallet specified.
Definition: protocol.h:109
@ RPC_INTERNAL_ERROR
Definition: protocol.h:33
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:35
Bilingual messages:
Definition: translation.h:17
std::string original
Definition: translation.h:18
std::string urlDecode(const std::string &urlEncoded)
Definition: url.cpp:10
DatabaseStatus
Definition: db.h:227
@ FAILED_INVALID_BACKUP_FILE
void EnsureWalletIsUnlocked(const CWallet *pwallet)
Definition: util.cpp:96
static const std::string WALLET_ENDPOINT_BASE
Definition: util.cpp:16
std::shared_ptr< CWallet > GetWalletForJSONRPCRequest(const JSONRPCRequest &request)
Figures out what wallet, if any, to use for a JSONRPCRequest.
Definition: util.cpp:63
LegacyScriptPubKeyMan & EnsureLegacyScriptPubKeyMan(CWallet &wallet, bool also_create)
Definition: util.cpp:114
bool GetAvoidReuseFlag(const CWallet *const pwallet, const UniValue &param)
Definition: util.cpp:21
void HandleWalletError(const std::shared_ptr< CWallet > wallet, DatabaseStatus &status, bilingual_str &error)
Definition: util.cpp:135
bool ParseIncludeWatchonly(const UniValue &include_watchonly, const CWallet &pwallet)
Used by RPC commands that have an include_watchonly parameter.
Definition: util.cpp:38
WalletContext & EnsureWalletContext(const std::any &context)
Definition: util.cpp:104
const std::string HELP_REQUIRING_PASSPHRASE
Definition: util.cpp:17
std::string LabelFromValue(const UniValue &value)
Definition: util.cpp:127
bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest &request, std::string &wallet_name)
Definition: util.cpp:50
std::vector< std::shared_ptr< CWallet > > GetWallets(WalletContext &context)
Definition: wallet.cpp:151
std::shared_ptr< CWallet > GetWallet(WalletContext &context, const std::string &name)
Definition: wallet.cpp:156
@ WALLET_FLAG_DISABLE_PRIVATE_KEYS
Definition: walletutil.h:55
@ WALLET_FLAG_AVOID_REUSE
Definition: walletutil.h:47