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 : 1664 : bool GetAvoidReuseFlag(const CWallet& wallet, const UniValue& param) {
22 : 1664 : bool can_avoid_reuse = wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
23 [ + + ]: 1664 : bool avoid_reuse = param.isNull() ? can_avoid_reuse : param.get_bool();
24 : :
25 [ - + ]: 1664 : 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 : 1664 : return avoid_reuse;
30 : : }
31 : :
32 : : /** Used by RPC commands that have an include_watchonly parameter.
33 : : * We default to true for watchonly wallets if include_watchonly isn't
34 : : * explicitly set.
35 : : */
36 : 1754 : bool ParseIncludeWatchonly(const UniValue& include_watchonly, const CWallet& wallet)
37 : : {
38 [ + + ]: 1754 : if (include_watchonly.isNull()) {
39 : : // if include_watchonly isn't explicitly set, then check if we have a watchonly wallet
40 : 1729 : return wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
41 : : }
42 : :
43 : : // otherwise return whatever include_watchonly was set to
44 : 25 : return include_watchonly.get_bool();
45 : : }
46 : :
47 : 17692 : bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& wallet_name)
48 : : {
49 [ + + ]: 17692 : if (request.URI.starts_with(WALLET_ENDPOINT_BASE)) {
50 : : // wallet endpoint was used
51 : 12845 : wallet_name = UrlDecode(std::string_view{request.URI}.substr(WALLET_ENDPOINT_BASE.size()));
52 : 12845 : return true;
53 : : }
54 : : return false;
55 : : }
56 : :
57 : 17367 : std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
58 : : {
59 : 17367 : CHECK_NONFATAL(request.mode == JSONRPCRequest::EXECUTE);
60 : 17367 : WalletContext& context = EnsureWalletContext(request.context);
61 : :
62 [ + - ]: 17367 : std::string wallet_name;
63 [ + - + + ]: 17367 : if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
64 [ + - ]: 12657 : std::shared_ptr<CWallet> pwallet = GetWallet(context, wallet_name);
65 [ + + + - : 12668 : if (!pwallet) throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
+ - ]
66 : : return pwallet;
67 : 11 : }
68 : :
69 : 4710 : size_t count{0};
70 [ + - ]: 4710 : auto wallet = GetDefaultWallet(context, count);
71 [ + + - + ]: 4710 : if (wallet) return wallet;
72 : :
73 [ + + ]: 23 : if (count == 0) {
74 [ + - ]: 11 : throw JSONRPCError(
75 [ + - + - ]: 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)");
76 : : }
77 [ + - ]: 12 : throw JSONRPCError(RPC_WALLET_NOT_SPECIFIED,
78 [ + - + - ]: 24 : "Multiple wallets are loaded. Please select which wallet to use by requesting the RPC through the /wallet/<walletname> URI path.");
79 : 22043 : }
80 : :
81 : 3758 : void EnsureWalletIsUnlocked(const CWallet& wallet)
82 : : {
83 [ + + ]: 3758 : if (wallet.IsLocked()) {
84 [ + - + - ]: 30 : throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please enter the wallet passphrase with walletpassphrase first.");
85 : : }
86 : 3743 : }
87 : :
88 : 18449 : WalletContext& EnsureWalletContext(const std::any& context)
89 : : {
90 : 18449 : auto wallet_context = util::AnyPtr<WalletContext>(context);
91 [ - + ]: 18449 : if (!wallet_context) {
92 [ # # # # ]: 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet context not found");
93 : : }
94 : 18449 : return *wallet_context;
95 : : }
96 : :
97 : 10815 : std::string LabelFromValue(const UniValue& value)
98 : : {
99 [ + + + - ]: 10815 : static const std::string empty_string;
100 [ + + ]: 10815 : if (value.isNull()) return empty_string;
101 : :
102 : 337 : const std::string& label{value.get_str()};
103 [ + + ]: 337 : if (label == "*")
104 [ + - + - ]: 12 : throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, "Invalid label name");
105 : 331 : return label;
106 : : }
107 : :
108 : 34785 : void PushParentDescriptors(const CWallet& wallet, const CScript& script_pubkey, UniValue& entry)
109 : : {
110 : 34785 : UniValue parent_descs(UniValue::VARR);
111 [ + - + + ]: 69570 : for (const auto& desc: wallet.GetWalletDescriptors(script_pubkey)) {
112 : 34785 : std::string desc_str;
113 : 34785 : FlatSigningProvider dummy_provider;
114 [ + - + - : 34785 : if (!CHECK_NONFATAL(desc.descriptor->ToNormalizedString(dummy_provider, desc_str, &desc.cache))) continue;
- + ]
115 [ + - + - ]: 34785 : parent_descs.push_back(desc_str);
116 : 69570 : }
117 [ + - + - ]: 69570 : entry.pushKV("parent_descs", std::move(parent_descs));
118 : 34785 : }
119 : :
120 : 178 : void HandleWalletError(const std::shared_ptr<CWallet> wallet, DatabaseStatus& status, bilingual_str& error)
121 : : {
122 [ + + ]: 178 : if (!wallet) {
123 : : // Map bad format to not found, since bad format is returned when the
124 : : // wallet directory exists, but doesn't contain a data file.
125 : 27 : RPCErrorCode code = RPC_WALLET_ERROR;
126 [ + - + + : 27 : switch (status) {
+ ]
127 : 15 : case DatabaseStatus::FAILED_NOT_FOUND:
128 : 15 : case DatabaseStatus::FAILED_BAD_FORMAT:
129 : 15 : case DatabaseStatus::FAILED_LEGACY_DISABLED:
130 : 15 : code = RPC_WALLET_NOT_FOUND;
131 : 15 : break;
132 : 0 : case DatabaseStatus::FAILED_ALREADY_LOADED:
133 : 0 : code = RPC_WALLET_ALREADY_LOADED;
134 : 0 : break;
135 : 1 : case DatabaseStatus::FAILED_ALREADY_EXISTS:
136 : 1 : code = RPC_WALLET_ALREADY_EXISTS;
137 : 1 : break;
138 : 1 : case DatabaseStatus::FAILED_INVALID_BACKUP_FILE:
139 : 1 : code = RPC_INVALID_PARAMETER;
140 : 1 : break;
141 : : default: // RPC_WALLET_ERROR is returned for all other cases.
142 : : break;
143 : : }
144 [ + - ]: 27 : throw JSONRPCError(code, error.original);
145 : : }
146 : 151 : }
147 : :
148 : 1332 : void AppendLastProcessedBlock(UniValue& entry, const CWallet& wallet)
149 : : {
150 : 1332 : AssertLockHeld(wallet.cs_wallet);
151 : 1332 : UniValue lastprocessedblock{UniValue::VOBJ};
152 [ + - + - : 2664 : lastprocessedblock.pushKV("hash", wallet.GetLastBlockHash().GetHex());
+ - + - ]
153 [ + - + - : 2664 : lastprocessedblock.pushKV("height", wallet.GetLastBlockHeight());
+ - ]
154 [ + - + - ]: 2664 : entry.pushKV("lastprocessedblock", std::move(lastprocessedblock));
155 : 1332 : }
156 : :
157 : : } // namespace wallet
|