Branch data Line data Source code
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 : 1669 : bool GetAvoidReuseFlag(const CWallet& wallet, const UniValue& param) {
22 : 1669 : bool can_avoid_reuse = wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
23 [ + + ]: 1669 : bool avoid_reuse = param.isNull() ? can_avoid_reuse : param.get_bool();
24 : :
25 [ - + ]: 1669 : if (avoid_reuse && !can_avoid_reuse) {
26 [ # # # # ]: 0 : throw JSONRPCError(RPC_WALLET_ERROR, "wallet does not have the \"avoid reuse\" feature enabled");
27 : : }
28 : :
29 : 1669 : return avoid_reuse;
30 : : }
31 : :
32 : 339 : std::string EnsureUniqueWalletName(const JSONRPCRequest& request, const std::string* wallet_name)
33 : : {
34 [ + - ]: 339 : std::string endpoint_wallet;
35 [ + - + + ]: 339 : if (GetWalletNameFromJSONRPCRequest(request, endpoint_wallet)) {
36 : : // wallet endpoint was used
37 [ + + + + ]: 193 : if (wallet_name && *wallet_name != endpoint_wallet) {
38 [ + - ]: 6 : throw JSONRPCError(RPC_INVALID_PARAMETER,
39 [ + - + - ]: 12 : "The RPC endpoint wallet and the wallet name parameter specify different wallets");
40 : : }
41 : 187 : return endpoint_wallet;
42 : : }
43 : :
44 : : // Not a wallet endpoint; parameter must be provided
45 [ + + ]: 146 : if (!wallet_name) {
46 [ + - ]: 5 : throw JSONRPCError(RPC_INVALID_PARAMETER,
47 [ + - + - ]: 10 : "Either the RPC endpoint wallet or the wallet name parameter must be provided");
48 : : }
49 : :
50 [ + - ]: 141 : return *wallet_name;
51 : 328 : }
52 : :
53 : 17866 : bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& wallet_name)
54 : : {
55 [ + + ]: 17866 : if (request.URI.starts_with(WALLET_ENDPOINT_BASE)) {
56 : : // wallet endpoint was used
57 : 12995 : wallet_name = UrlDecode(std::string_view{request.URI}.substr(WALLET_ENDPOINT_BASE.size()));
58 : 12995 : return true;
59 : : }
60 : : return false;
61 : : }
62 : :
63 : 17527 : std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
64 : : {
65 : 17527 : CHECK_NONFATAL(request.mode == JSONRPCRequest::EXECUTE);
66 : 17527 : WalletContext& context = EnsureWalletContext(request.context);
67 : :
68 [ + - ]: 17527 : std::string wallet_name;
69 [ + - + + ]: 17527 : if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
70 [ + - ]: 12802 : std::shared_ptr<CWallet> pwallet = GetWallet(context, wallet_name);
71 [ + + + - : 12813 : if (!pwallet) throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
+ - ]
72 : : return pwallet;
73 : 11 : }
74 : :
75 : 4725 : size_t count{0};
76 [ + - ]: 4725 : auto wallet = GetDefaultWallet(context, count);
77 [ + + - + ]: 4725 : if (wallet) return wallet;
78 : :
79 [ + + ]: 23 : if (count == 0) {
80 [ + - ]: 11 : throw JSONRPCError(
81 [ + - + - ]: 22 : 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)");
82 : : }
83 [ + - ]: 12 : throw JSONRPCError(RPC_WALLET_NOT_SPECIFIED,
84 [ + - + - ]: 24 : "Multiple wallets are loaded. Please select which wallet to use by requesting the RPC through the /wallet/<walletname> URI path.");
85 : 22218 : }
86 : :
87 : 3772 : void EnsureWalletIsUnlocked(const CWallet& wallet)
88 : : {
89 [ + + ]: 3772 : if (wallet.IsLocked()) {
90 [ + - + - ]: 30 : throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please enter the wallet passphrase with walletpassphrase first.");
91 : : }
92 : 3757 : }
93 : :
94 : 18667 : WalletContext& EnsureWalletContext(const std::any& context)
95 : : {
96 : 18667 : auto wallet_context = util::AnyPtr<WalletContext>(context);
97 [ - + ]: 18667 : if (!wallet_context) {
98 [ # # # # ]: 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet context not found");
99 : : }
100 : 18667 : return *wallet_context;
101 : : }
102 : :
103 : 10833 : std::string LabelFromValue(const UniValue& value)
104 : : {
105 [ + + + - ]: 10833 : static const std::string empty_string;
106 [ + + ]: 10833 : if (value.isNull()) return empty_string;
107 : :
108 : 337 : const std::string& label{value.get_str()};
109 [ + + ]: 337 : if (label == "*")
110 [ + - + - ]: 12 : throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, "Invalid label name");
111 : 331 : return label;
112 : : }
113 : :
114 : 35537 : void PushParentDescriptors(const CWallet& wallet, const CScript& script_pubkey, UniValue& entry)
115 : : {
116 : 35537 : UniValue parent_descs(UniValue::VARR);
117 [ + - + + ]: 71074 : for (const auto& desc: wallet.GetWalletDescriptors(script_pubkey)) {
118 : 35537 : std::string desc_str;
119 : 35537 : FlatSigningProvider dummy_provider;
120 [ + - + - : 35537 : if (!CHECK_NONFATAL(desc.descriptor->ToNormalizedString(dummy_provider, desc_str, &desc.cache))) continue;
- + ]
121 [ + - + - ]: 35537 : parent_descs.push_back(desc_str);
122 : 71074 : }
123 [ + - + - ]: 71074 : entry.pushKV("parent_descs", std::move(parent_descs));
124 : 35537 : }
125 : :
126 : 178 : void HandleWalletError(const std::shared_ptr<CWallet> wallet, DatabaseStatus& status, bilingual_str& error)
127 : : {
128 [ + + ]: 178 : if (!wallet) {
129 : : // Map bad format to not found, since bad format is returned when the
130 : : // wallet directory exists, but doesn't contain a data file.
131 : 27 : RPCErrorCode code = RPC_WALLET_ERROR;
132 [ + - + + : 27 : switch (status) {
+ ]
133 : 15 : case DatabaseStatus::FAILED_NOT_FOUND:
134 : 15 : case DatabaseStatus::FAILED_BAD_FORMAT:
135 : 15 : case DatabaseStatus::FAILED_LEGACY_DISABLED:
136 : 15 : code = RPC_WALLET_NOT_FOUND;
137 : 15 : break;
138 : 0 : case DatabaseStatus::FAILED_ALREADY_LOADED:
139 : 0 : code = RPC_WALLET_ALREADY_LOADED;
140 : 0 : break;
141 : 1 : case DatabaseStatus::FAILED_ALREADY_EXISTS:
142 : 1 : code = RPC_WALLET_ALREADY_EXISTS;
143 : 1 : break;
144 : 1 : case DatabaseStatus::FAILED_INVALID_BACKUP_FILE:
145 : 1 : code = RPC_INVALID_PARAMETER;
146 : 1 : break;
147 : : default: // RPC_WALLET_ERROR is returned for all other cases.
148 : : break;
149 : : }
150 [ + - ]: 27 : throw JSONRPCError(code, error.original);
151 : : }
152 : 151 : }
153 : :
154 : 1465 : void AppendLastProcessedBlock(UniValue& entry, const CWallet& wallet)
155 : : {
156 : 1465 : AssertLockHeld(wallet.cs_wallet);
157 : 1465 : UniValue lastprocessedblock{UniValue::VOBJ};
158 [ + - + - : 2930 : lastprocessedblock.pushKV("hash", wallet.GetLastBlockHash().GetHex());
+ - + - ]
159 [ + - + - : 2930 : lastprocessedblock.pushKV("height", wallet.GetLastBlockHeight());
+ - ]
160 [ + - + - ]: 2930 : entry.pushKV("lastprocessedblock", std::move(lastprocessedblock));
161 : 1465 : }
162 : :
163 : : } // namespace wallet
|