Branch data Line data Source code
1 : : // Copyright (c) 2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #include <core_io.h>
7 : : #include <key_io.h>
8 : : #include <rpc/server.h>
9 : : #include <rpc/util.h>
10 : : #include <util/translation.h>
11 : : #include <wallet/context.h>
12 : : #include <wallet/receive.h>
13 : : #include <wallet/rpc/wallet.h>
14 : : #include <wallet/rpc/util.h>
15 : : #include <wallet/wallet.h>
16 : : #include <wallet/walletutil.h>
17 [ + - ]: 2 :
18 [ + - ]: 2 : #include <optional>
19 : :
20 : : #include <univalue.h>
21 : :
22 : :
23 : : namespace wallet {
24 : :
25 [ + - ]: 2 : static const std::map<uint64_t, std::string> WALLET_FLAG_CAVEATS{
26 [ + - ]: 2 : {WALLET_FLAG_AVOID_REUSE,
27 : 2 : "You need to rescan the blockchain in order to correctly mark used "
28 [ + - ][ + - ]: 6 : "destinations in the past. Until this is done, some destinations may "
[ + - ][ - + ]
[ # # ]
29 [ + - ][ + - ]: 2 : "be considered unused, even if the opposite is the case."},
[ + - ]
30 [ + - ][ + - ]: 2 : };
[ + - ]
31 : :
32 : : /** Checks if a CKey is in the given CWallet compressed or otherwise*/
33 : 0 : bool HaveKey(const SigningProvider& wallet, const CKey& key)
34 : : {
35 : 0 : CKey key2;
36 [ # # ][ # # ]: 0 : key2.Set(key.begin(), key.end(), !key.IsCompressed());
[ # # ][ # # ]
37 [ # # ][ # # ]: 0 : return wallet.HaveKey(key.GetPubKey().GetID()) || wallet.HaveKey(key2.GetPubKey().GetID());
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
38 : 0 : }
39 : :
40 : 0 : static RPCHelpMan getwalletinfo()
41 : : {
42 [ # # ][ # # ]: 0 : return RPCHelpMan{"getwalletinfo",
[ # # ][ # # ]
43 [ # # ]: 0 : "Returns an object containing various wallet state info.\n",
44 : 0 : {},
45 [ # # ][ # # ]: 0 : RPCResult{
46 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ, "", "",
47 [ # # ]: 0 : {
48 : 0 : {
49 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "walletname", "the wallet name"},
[ # # ]
50 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "walletversion", "the wallet version"},
[ # # ]
51 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "format", "the database format (bdb or sqlite)"},
[ # # ]
52 [ # # ][ # # ]: 0 : {RPCResult::Type::STR_AMOUNT, "balance", "DEPRECATED. Identical to getbalances().mine.trusted"},
[ # # ]
53 [ # # ][ # # ]: 0 : {RPCResult::Type::STR_AMOUNT, "unconfirmed_balance", "DEPRECATED. Identical to getbalances().mine.untrusted_pending"},
[ # # ][ # # ]
54 [ # # ][ # # ]: 0 : {RPCResult::Type::STR_AMOUNT, "immature_balance", "DEPRECATED. Identical to getbalances().mine.immature"},
[ # # ]
55 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "txcount", "the total number of transactions in the wallet"},
[ # # ]
56 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "keypoololdest", /*optional=*/true, "the " + UNIX_EPOCH_TIME + " of the oldest pre-generated key in the key pool. Legacy wallets only."},
[ # # ][ # # ]
57 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "keypoolsize", "how many new keys are pre-generated (only counts external keys)"},
[ # # ]
58 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "keypoolsize_hd_internal", /*optional=*/true, "how many new keys are pre-generated for internal use (used for change outputs, only appears if the wallet is using this feature, otherwise external keys are used)"},
[ # # ]
59 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM_TIME, "unlocked_until", /*optional=*/true, "the " + UNIX_EPOCH_TIME + " until which the wallet is unlocked for transfers, or 0 if the wallet is locked (only present for passphrase-encrypted wallets)"},
[ # # ][ # # ]
60 [ # # ][ # # ]: 0 : {RPCResult::Type::STR_AMOUNT, "paytxfee", "the transaction fee configuration, set in " + CURRENCY_UNIT + "/kvB"},
[ # # ][ # # ]
61 [ # # ][ # # ]: 0 : {RPCResult::Type::STR_HEX, "hdseedid", /*optional=*/true, "the Hash160 of the HD seed (only present when HD is enabled)"},
[ # # ]
62 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "private_keys_enabled", "false if privatekeys are disabled for this wallet (enforced watch-only wallet)"},
[ # # ]
63 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "avoid_reuse", "whether this wallet tracks clean/dirty coins in terms of reuse"},
[ # # ]
64 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ, "scanning", "current scanning details, or false if no scan is in progress",
[ # # ]
65 [ # # ]: 0 : {
66 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "duration", "elapsed seconds since scan start"},
[ # # ]
67 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "progress", "scanning progress percentage [0.0, 1.0]"},
[ # # ]
68 : : }, /*skip_type_check=*/true},
69 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "descriptors", "whether this wallet uses descriptors for scriptPubKey management"},
[ # # ]
70 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "external_signer", "whether this wallet is configured to use an external signer such as a hardware wallet"},
[ # # ]
71 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "blank", "Whether this wallet intentionally does not contain any keys, scripts, or descriptors"},
[ # # ]
72 [ # # ]: 0 : RESULT_LAST_PROCESSED_BLOCK,
73 : : }},
74 : : },
75 [ # # ]: 0 : RPCExamples{
76 [ # # ][ # # ]: 0 : HelpExampleCli("getwalletinfo", "")
[ # # ]
77 [ # # ][ # # ]: 0 : + HelpExampleRpc("getwalletinfo", "")
[ # # ][ # # ]
78 : : },
79 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
80 : : {
81 : 0 : const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
82 [ # # ][ # # ]: 0 : if (!pwallet) return UniValue::VNULL;
83 : :
84 : : // Make sure the results are valid at least up to the most recent block
85 : : // the user could have gotten from another RPC command prior to now
86 [ # # ]: 0 : pwallet->BlockUntilSyncedToCurrentChain();
87 : :
88 [ # # ][ # # ]: 0 : LOCK(pwallet->cs_wallet);
89 : :
90 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
91 : 2 :
92 [ # # ]: 0 : size_t kpExternalSize = pwallet->KeypoolCountExternalKeys();
93 [ # # ]: 0 : const auto bal = GetBalance(*pwallet);
94 [ # # ][ # # ]: 0 : obj.pushKV("walletname", pwallet->GetName());
[ # # ]
95 [ # # ][ # # ]: 0 : obj.pushKV("walletversion", pwallet->GetVersion());
[ # # ][ # # ]
96 [ # # ][ # # ]: 0 : obj.pushKV("format", pwallet->GetDatabase().Format());
[ # # ][ # # ]
[ # # ]
97 [ # # ][ # # ]: 0 : obj.pushKV("balance", ValueFromAmount(bal.m_mine_trusted));
[ # # ]
98 [ # # ][ # # ]: 0 : obj.pushKV("unconfirmed_balance", ValueFromAmount(bal.m_mine_untrusted_pending));
[ # # ]
99 [ # # ][ # # ]: 2 : obj.pushKV("immature_balance", ValueFromAmount(bal.m_mine_immature));
[ # # ]
100 [ # # ][ # # ]: 0 : obj.pushKV("txcount", (int)pwallet->mapWallet.size());
[ # # ]
101 [ # # ]: 0 : const auto kp_oldest = pwallet->GetOldestKeyPoolTime();
102 [ # # ]: 0 : if (kp_oldest.has_value()) {
103 [ # # ][ # # ]: 0 : obj.pushKV("keypoololdest", kp_oldest.value());
[ # # ][ # # ]
104 : 0 : }
105 [ # # ][ # # ]: 0 : obj.pushKV("keypoolsize", (int64_t)kpExternalSize);
[ # # ]
106 : :
107 [ # # ]: 0 : LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
108 [ # # ]: 0 : if (spk_man) {
109 [ # # ]: 0 : CKeyID seed_id = spk_man->GetHDChain().seed_id;
110 [ # # ][ # # ]: 0 : if (!seed_id.IsNull()) {
111 [ # # ][ # # ]: 0 : obj.pushKV("hdseedid", seed_id.GetHex());
[ # # ][ # # ]
112 : 0 : }
113 : 0 : }
114 : :
115 [ # # ][ # # ]: 0 : if (pwallet->CanSupportFeature(FEATURE_HD_SPLIT)) {
116 [ # # ][ # # ]: 0 : obj.pushKV("keypoolsize_hd_internal", (int64_t)(pwallet->GetKeyPoolSize() - kpExternalSize));
[ # # ][ # # ]
117 : 0 : }
118 [ # # ][ # # ]: 0 : if (pwallet->IsCrypted()) {
119 [ # # ][ # # ]: 0 : obj.pushKV("unlocked_until", pwallet->nRelockTime);
[ # # ]
120 : 0 : }
121 [ # # ][ # # ]: 0 : obj.pushKV("paytxfee", ValueFromAmount(pwallet->m_pay_tx_fee.GetFeePerK()));
[ # # ][ # # ]
122 [ # # ][ # # ]: 0 : obj.pushKV("private_keys_enabled", !pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
[ # # ][ # # ]
123 [ # # ][ # # ]: 0 : obj.pushKV("avoid_reuse", pwallet->IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE));
[ # # ][ # # ]
124 [ # # ][ # # ]: 0 : if (pwallet->IsScanning()) {
125 [ # # ]: 0 : UniValue scanning(UniValue::VOBJ);
126 [ # # ][ # # ]: 0 : scanning.pushKV("duration", Ticks<std::chrono::seconds>(pwallet->ScanningDuration()));
[ # # ][ # # ]
[ # # ]
127 [ # # ][ # # ]: 0 : scanning.pushKV("progress", pwallet->ScanningProgress());
[ # # ][ # # ]
128 [ # # ][ # # ]: 0 : obj.pushKV("scanning", scanning);
[ # # ]
129 : 0 : } else {
130 [ # # ][ # # ]: 0 : obj.pushKV("scanning", false);
[ # # ]
131 : : }
132 [ # # ][ # # ]: 0 : obj.pushKV("descriptors", pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
[ # # ][ # # ]
133 [ # # ][ # # ]: 0 : obj.pushKV("external_signer", pwallet->IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER));
[ # # ][ # # ]
134 [ # # ][ # # ]: 0 : obj.pushKV("blank", pwallet->IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET));
[ # # ][ # # ]
135 : :
136 [ # # ]: 0 : AppendLastProcessedBlock(obj, *pwallet);
137 : 0 : return obj;
138 [ # # ]: 0 : },
139 : : };
140 : 0 : }
141 : :
142 : 0 : static RPCHelpMan listwalletdir()
143 : : {
144 [ # # ][ # # ]: 0 : return RPCHelpMan{"listwalletdir",
[ # # ][ # # ]
[ # # ]
145 [ # # ]: 0 : "Returns a list of wallets in the wallet directory.\n",
146 : 0 : {},
147 [ # # ][ # # ]: 0 : RPCResult{
148 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ, "", "",
149 [ # # ]: 0 : {
150 [ # # ][ # # ]: 0 : {RPCResult::Type::ARR, "wallets", "",
[ # # ]
151 [ # # ]: 0 : {
152 [ # # ][ # # ]: 0 : {RPCResult::Type::OBJ, "", "",
[ # # ]
153 [ # # ]: 0 : {
154 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "name", "The wallet name"},
[ # # ]
155 : : }},
156 : : }},
157 : : }
158 : : },
159 [ # # ]: 0 : RPCExamples{
160 [ # # ][ # # ]: 0 : HelpExampleCli("listwalletdir", "")
[ # # ]
161 [ # # ][ # # ]: 0 : + HelpExampleRpc("listwalletdir", "")
[ # # ][ # # ]
162 : : },
163 [ + - ]: 2 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
164 [ + - ]: 2 : {
165 [ + - ][ # # ]: 2 : UniValue wallets(UniValue::VARR);
166 [ + - ][ # # ]: 2 : for (const auto& path : ListDatabases(GetWalletDir())) {
[ # # ][ # # ]
167 [ + - ][ # # ]: 2 : UniValue wallet(UniValue::VOBJ);
168 [ + - ][ # # ]: 2 : wallet.pushKV("name", path.u8string());
[ # # ][ # # ]
[ # # ]
169 [ + - ][ # # ]: 2 : wallets.push_back(wallet);
[ # # ]
170 [ + - ]: 2 : }
171 : :
172 [ # # ]: 0 : UniValue result(UniValue::VOBJ);
173 [ # # ][ # # ]: 0 : result.pushKV("wallets", wallets);
[ # # ]
174 : 0 : return result;
175 [ # # ]: 0 : },
176 : : };
177 : 0 : }
178 : :
179 : 0 : static RPCHelpMan listwallets()
180 : : {
181 [ # # ][ # # ]: 0 : return RPCHelpMan{"listwallets",
[ # # ]
182 [ # # ]: 0 : "Returns a list of currently loaded wallets.\n"
183 : : "For full information on the wallet, use \"getwalletinfo\"\n",
184 : 0 : {},
185 [ # # ][ # # ]: 0 : RPCResult{
186 [ # # ][ # # ]: 0 : RPCResult::Type::ARR, "", "",
187 [ # # ]: 0 : {
188 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "walletname", "the wallet name"},
[ # # ]
189 : : }
190 : : },
191 [ # # ]: 0 : RPCExamples{
192 [ # # ][ # # ]: 0 : HelpExampleCli("listwallets", "")
[ # # ]
193 [ # # ][ # # ]: 0 : + HelpExampleRpc("listwallets", "")
[ # # ][ # # ]
194 : : },
195 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
196 : : {
197 [ # # ]: 0 : UniValue obj(UniValue::VARR);
198 : :
199 [ # # ]: 0 : WalletContext& context = EnsureWalletContext(request.context);
200 [ # # ][ # # ]: 0 : for (const std::shared_ptr<CWallet>& wallet : GetWallets(context)) {
201 [ # # ]: 0 : LOCK(wallet->cs_wallet);
202 [ # # ][ # # ]: 0 : obj.push_back(wallet->GetName());
203 : 0 : }
204 : :
205 : 0 : return obj;
206 [ # # ]: 0 : },
207 : : };
208 : 0 : }
209 : :
210 : 0 : static RPCHelpMan loadwallet()
211 : : {
212 [ # # ][ # # ]: 0 : return RPCHelpMan{"loadwallet",
[ # # ][ # # ]
[ # # ]
213 [ # # ]: 0 : "\nLoads a wallet from a wallet file or directory."
214 : : "\nNote that all wallet command-line options used when starting bitcoind will be"
215 : : "\napplied to the new wallet.\n",
216 [ # # ]: 0 : {
217 [ # # ][ # # ]: 0 : {"filename", RPCArg::Type::STR, RPCArg::Optional::NO, "The wallet directory or .dat file."},
[ # # ]
218 [ # # ][ # # ]: 0 : {"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."},
[ # # ]
219 : : },
220 [ # # ][ # # ]: 0 : RPCResult{
221 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ, "", "",
222 [ # # ]: 0 : {
223 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "name", "The wallet name if loaded successfully."},
[ # # ]
224 [ # # ][ # # ]: 0 : {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to loading the wallet.",
[ # # ]
225 [ # # ]: 0 : {
226 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "", ""},
[ # # ]
227 : : }},
228 : : }
229 : : },
230 [ # # ]: 0 : RPCExamples{
231 [ # # ][ # # ]: 0 : HelpExampleCli("loadwallet", "\"test.dat\"")
[ # # ]
232 [ # # ][ # # ]: 0 : + HelpExampleRpc("loadwallet", "\"test.dat\"")
[ # # ][ # # ]
233 : : },
234 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
235 : : {
236 : 0 : WalletContext& context = EnsureWalletContext(request.context);
237 : 0 : const std::string name(request.params[0].get_str());
238 : :
239 : 0 : DatabaseOptions options;
240 : : DatabaseStatus status;
241 [ # # ]: 0 : ReadDatabaseArgs(*context.args, options);
242 : 0 : options.require_existing = true;
243 : 0 : bilingual_str error;
244 : 0 : std::vector<bilingual_str> warnings;
245 [ # # ][ # # ]: 0 : std::optional<bool> load_on_start = request.params[1].isNull() ? std::nullopt : std::optional<bool>(request.params[1].get_bool());
[ # # ][ # # ]
246 : :
247 : : {
248 [ # # ][ # # ]: 0 : LOCK(context.wallets_mutex);
249 [ # # ][ # # ]: 0 : if (std::any_of(context.wallets.begin(), context.wallets.end(), [&name](const auto& wallet) { return wallet->GetName() == name; })) {
250 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_WALLET_ALREADY_LOADED, "Wallet \"" + name + "\" is already loaded.");
[ # # ][ # # ]
[ # # ]
251 : : }
252 : 0 : }
253 : :
254 [ # # ]: 0 : std::shared_ptr<CWallet> const wallet = LoadWallet(context, name, load_on_start, options, status, error, warnings);
255 : :
256 [ # # ]: 0 : HandleWalletError(wallet, status, error);
257 : :
258 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
259 [ # # ][ # # ]: 0 : obj.pushKV("name", wallet->GetName());
[ # # ]
260 [ # # ]: 0 : PushWarnings(warnings, obj);
261 : :
262 : 0 : return obj;
263 [ # # ]: 0 : },
264 [ + - ]: 2 : };
265 : 0 : }
266 : :
267 : 0 : static RPCHelpMan setwalletflag()
268 : : {
269 : 0 : std::string flags;
270 [ # # ]: 0 : for (auto& it : WALLET_FLAG_MAP)
271 [ # # ]: 0 : if (it.second & MUTABLE_WALLET_FLAGS)
272 [ # # ][ # # ]: 0 : flags += (flags == "" ? "" : ", ") + it.first;
[ # # ]
273 : :
274 [ # # ][ # # ]: 0 : return RPCHelpMan{"setwalletflag",
[ # # ][ # # ]
275 [ # # ]: 0 : "\nChange the state of the given wallet flag for a wallet.\n",
276 [ # # ]: 0 : {
277 [ # # ][ # # ]: 0 : {"flag", RPCArg::Type::STR, RPCArg::Optional::NO, "The name of the flag to change. Current available flags: " + flags},
[ # # ]
278 [ # # ][ # # ]: 0 : {"value", RPCArg::Type::BOOL, RPCArg::Default{true}, "The new state."},
[ # # ][ # # ]
279 : : },
280 [ # # ][ # # ]: 0 : RPCResult{
281 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ, "", "",
282 [ # # ]: 0 : {
283 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "flag_name", "The name of the flag that was modified"},
[ # # ]
284 [ # # ][ # # ]: 0 : {RPCResult::Type::BOOL, "flag_state", "The new state of the flag"},
[ # # ]
285 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "warnings", /*optional=*/true, "Any warnings associated with the change"},
[ # # ]
286 : : }
287 : : },
288 [ # # ]: 0 : RPCExamples{
289 [ # # ][ # # ]: 0 : HelpExampleCli("setwalletflag", "avoid_reuse")
[ # # ]
290 [ # # ][ # # ]: 0 : + HelpExampleRpc("setwalletflag", "\"avoid_reuse\"")
[ # # ][ # # ]
291 : : },
292 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
293 : : {
294 : 0 : std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
295 [ # # ][ # # ]: 0 : if (!pwallet) return UniValue::VNULL;
296 : :
297 [ # # ][ # # ]: 0 : std::string flag_str = request.params[0].get_str();
[ # # ]
298 [ # # ][ # # ]: 0 : bool value = request.params[1].isNull() || request.params[1].get_bool();
[ # # ][ # # ]
299 : :
300 [ # # ][ # # ]: 0 : if (!WALLET_FLAG_MAP.count(flag_str)) {
301 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unknown wallet flag: %s", flag_str));
[ # # ]
302 : : }
303 : :
304 [ # # ]: 0 : auto flag = WALLET_FLAG_MAP.at(flag_str);
305 : :
306 [ # # ]: 0 : if (!(flag & MUTABLE_WALLET_FLAGS)) {
307 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Wallet flag is immutable: %s", flag_str));
[ # # ]
308 : : }
309 : :
310 [ # # ]: 0 : UniValue res(UniValue::VOBJ);
311 : :
312 [ # # ][ # # ]: 0 : if (pwallet->IsWalletFlagSet(flag) == value) {
313 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Wallet flag is already set to %s: %s", value ? "true" : "false", flag_str));
[ # # ]
314 : : }
315 : :
316 [ # # ][ # # ]: 0 : res.pushKV("flag_name", flag_str);
[ # # ]
317 [ # # ][ # # ]: 0 : res.pushKV("flag_state", value);
[ # # ]
318 : :
319 [ # # ]: 0 : if (value) {
320 [ # # ]: 0 : pwallet->SetWalletFlag(flag);
321 : 0 : } else {
322 [ # # ]: 0 : pwallet->UnsetWalletFlag(flag);
323 : : }
324 : :
325 [ # # ][ # # ]: 0 : if (flag && value && WALLET_FLAG_CAVEATS.count(flag)) {
[ # # ][ # # ]
326 [ # # ][ # # ]: 0 : res.pushKV("warnings", WALLET_FLAG_CAVEATS.at(flag));
[ # # ][ # # ]
327 : 0 : }
328 : :
329 : 0 : return res;
330 [ # # ]: 0 : },
331 : : };
332 : 0 : }
333 : :
334 : 0 : static RPCHelpMan createwallet()
335 : : {
336 [ # # ][ # # ]: 0 : return RPCHelpMan{
[ # # ][ # # ]
337 [ # # ]: 0 : "createwallet",
338 [ # # ]: 0 : "\nCreates and loads a new wallet.\n",
339 [ # # ]: 0 : {
340 [ # # ][ # # ]: 0 : {"wallet_name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name for the new wallet. If this is a path, the wallet will be created at the path location."},
[ # # ]
341 [ # # ][ # # ]: 0 : {"disable_private_keys", RPCArg::Type::BOOL, RPCArg::Default{false}, "Disable the possibility of private keys (only watchonlys are possible in this mode)."},
[ # # ][ # # ]
342 [ # # ][ # # ]: 0 : {"blank", RPCArg::Type::BOOL, RPCArg::Default{false}, "Create a blank wallet. A blank wallet has no keys or HD seed. One can be set using sethdseed."},
[ # # ][ # # ]
343 [ # # ][ # # ]: 0 : {"passphrase", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Encrypt the wallet with this passphrase."},
[ # # ]
344 [ # # ][ # # ]: 0 : {"avoid_reuse", RPCArg::Type::BOOL, RPCArg::Default{false}, "Keep track of coin reuse, and treat dirty and clean coins differently with privacy considerations in mind."},
[ # # ][ # # ]
345 [ # # ][ # # ]: 0 : {"descriptors", RPCArg::Type::BOOL, RPCArg::Default{true}, "Create a native descriptor wallet. The wallet will use descriptors internally to handle address creation."
[ # # ][ # # ]
346 : : " Setting to \"false\" will create a legacy wallet; This is only possible with the -deprecatedrpc=create_bdb setting because, the legacy wallet type is being deprecated and"
347 : : " support for creating and opening legacy wallets will be removed in the future."},
348 [ # # ][ # # ]: 0 : {"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."},
[ # # ]
349 [ # # ][ # # ]: 0 : {"external_signer", RPCArg::Type::BOOL, RPCArg::Default{false}, "Use an external signer such as a hardware wallet. Requires -signer to be configured. Wallet creation will fail if keys cannot be fetched. Requires disable_private_keys and descriptors set to true."},
[ # # ][ # # ]
350 : : },
351 [ # # ][ # # ]: 0 : RPCResult{
352 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ, "", "",
353 [ # # ]: 0 : {
354 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "name", "The wallet name if created successfully. If the wallet was created using a full path, the wallet_name will be the full path."},
[ # # ]
355 [ # # ][ # # ]: 0 : {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to creating and loading the wallet.",
[ # # ]
356 [ # # ]: 0 : {
357 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "", ""},
[ # # ]
358 : : }},
359 : : }
360 : : },
361 [ # # ]: 0 : RPCExamples{
362 [ # # ][ # # ]: 0 : HelpExampleCli("createwallet", "\"testwallet\"")
[ # # ]
363 [ # # ][ # # ]: 0 : + HelpExampleRpc("createwallet", "\"testwallet\"")
[ # # ][ # # ]
364 [ # # ][ # # ]: 0 : + HelpExampleCliNamed("createwallet", {{"wallet_name", "descriptors"}, {"avoid_reuse", true}, {"descriptors", true}, {"load_on_startup", true}})
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
365 [ # # ][ # # ]: 0 : + HelpExampleRpcNamed("createwallet", {{"wallet_name", "descriptors"}, {"avoid_reuse", true}, {"descriptors", true}, {"load_on_startup", true}})
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
366 : : },
367 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
368 : : {
369 : 0 : WalletContext& context = EnsureWalletContext(request.context);
370 : 0 : uint64_t flags = 0;
371 [ # # ][ # # ]: 0 : if (!request.params[1].isNull() && request.params[1].get_bool()) {
372 : 0 : flags |= WALLET_FLAG_DISABLE_PRIVATE_KEYS;
373 : 0 : }
374 : :
375 [ # # ][ # # ]: 0 : if (!request.params[2].isNull() && request.params[2].get_bool()) {
376 : 0 : flags |= WALLET_FLAG_BLANK_WALLET;
377 : 0 : }
378 : 0 : SecureString passphrase;
379 [ # # ]: 0 : passphrase.reserve(100);
380 : 0 : std::vector<bilingual_str> warnings;
381 [ # # ][ # # ]: 0 : if (!request.params[3].isNull()) {
[ # # ]
382 [ # # ][ # # ]: 0 : passphrase = std::string_view{request.params[3].get_str()};
[ # # ]
383 [ # # ]: 0 : if (passphrase.empty()) {
384 : : // Empty string means unencrypted
385 [ # # ][ # # ]: 0 : warnings.emplace_back(Untranslated("Empty string given as passphrase, wallet will not be encrypted."));
[ # # ]
386 : 0 : }
387 : 0 : }
388 : :
389 [ # # ][ # # ]: 0 : if (!request.params[4].isNull() && request.params[4].get_bool()) {
[ # # ][ # # ]
[ # # ][ # # ]
390 : 0 : flags |= WALLET_FLAG_AVOID_REUSE;
391 : 0 : }
392 [ # # ][ # # ]: 0 : if (self.Arg<bool>(5)) {
393 : : #ifndef USE_SQLITE
394 : : throw JSONRPCError(RPC_WALLET_ERROR, "Compiled without sqlite support (required for descriptor wallets)");
395 : : #endif
396 : 0 : flags |= WALLET_FLAG_DESCRIPTORS;
397 : 0 : } else {
398 [ # # ][ # # ]: 0 : if (!context.chain->rpcEnableDeprecated("create_bdb")) {
[ # # ]
399 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_WALLET_ERROR, "BDB wallet creation is deprecated and will be removed in a future release."
[ # # ]
400 : : " In this release it can be re-enabled temporarily with the -deprecatedrpc=create_bdb setting.");
401 : : }
402 : : }
403 [ # # ][ # # ]: 0 : if (!request.params[7].isNull() && request.params[7].get_bool()) {
[ # # ][ # # ]
[ # # ][ # # ]
404 : : #ifdef ENABLE_EXTERNAL_SIGNER
405 : : flags |= WALLET_FLAG_EXTERNAL_SIGNER;
406 : : #else
407 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Compiled without external signing support (required for external signing)");
[ # # ]
408 : : #endif
409 : : }
410 : :
411 : : #ifndef USE_BDB
412 [ # # ]: 0 : if (!(flags & WALLET_FLAG_DESCRIPTORS)) {
413 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Compiled without bdb support (required for legacy wallets)");
[ # # ]
414 : : }
415 : : #endif
416 : :
417 : 0 : DatabaseOptions options;
418 : : DatabaseStatus status;
419 [ # # ]: 0 : ReadDatabaseArgs(*context.args, options);
420 : 0 : options.require_create = true;
421 : 0 : options.create_flags = flags;
422 [ # # ]: 0 : options.create_passphrase = passphrase;
423 : 0 : bilingual_str error;
424 [ # # ][ # # ]: 0 : std::optional<bool> load_on_start = request.params[6].isNull() ? std::nullopt : std::optional<bool>(request.params[6].get_bool());
[ # # ][ # # ]
[ # # ]
425 [ # # ][ # # ]: 0 : const std::shared_ptr<CWallet> wallet = CreateWallet(context, request.params[0].get_str(), load_on_start, options, status, error, warnings);
[ # # ]
426 [ # # ]: 0 : if (!wallet) {
427 : 0 : RPCErrorCode code = status == DatabaseStatus::FAILED_ENCRYPT ? RPC_WALLET_ENCRYPTION_FAILED : RPC_WALLET_ERROR;
428 [ # # ]: 0 : throw JSONRPCError(code, error.original);
429 : : }
430 : :
431 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
432 [ # # ][ # # ]: 0 : obj.pushKV("name", wallet->GetName());
[ # # ][ # # ]
433 [ # # ]: 0 : PushWarnings(warnings, obj);
434 : :
435 : 0 : return obj;
436 [ # # ]: 0 : },
437 : : };
438 : 0 : }
439 : :
440 : 0 : static RPCHelpMan unloadwallet()
441 : : {
442 [ # # ][ # # ]: 0 : return RPCHelpMan{"unloadwallet",
[ # # ][ # # ]
[ # # ]
443 [ # # ]: 0 : "Unloads the wallet referenced by the request endpoint, otherwise unloads the wallet specified in the argument.\n"
444 : : "Specifying the wallet name on a wallet endpoint is invalid.",
445 [ # # ]: 0 : {
446 [ # # ][ # # ]: 0 : {"wallet_name", RPCArg::Type::STR, RPCArg::DefaultHint{"the wallet name from the RPC endpoint"}, "The name of the wallet to unload. If provided both here and in the RPC endpoint, the two must be identical."},
[ # # ][ # # ]
447 [ # # ][ # # ]: 0 : {"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."},
[ # # ]
448 : : },
449 [ # # ][ # # ]: 0 : RPCResult{RPCResult::Type::OBJ, "", "", {
[ # # ][ # # ]
[ # # ]
450 [ # # ][ # # ]: 0 : {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to unloading the wallet.",
[ # # ]
451 [ # # ]: 0 : {
452 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "", ""},
[ # # ]
453 : : }},
454 : : }},
455 [ # # ]: 0 : RPCExamples{
456 [ # # ][ # # ]: 0 : HelpExampleCli("unloadwallet", "wallet_name")
[ # # ]
457 [ # # ][ # # ]: 0 : + HelpExampleRpc("unloadwallet", "wallet_name")
[ # # ][ # # ]
458 : : },
459 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
460 : : {
461 : 0 : std::string wallet_name;
462 [ # # ][ # # ]: 0 : if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
463 [ # # ][ # # ]: 0 : if (!(request.params[0].isNull() || request.params[0].get_str() == wallet_name)) {
[ # # ][ # # ]
[ # # ]
464 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "RPC endpoint wallet and wallet_name parameter specify different wallets");
[ # # ]
465 : : }
466 : 0 : } else {
467 [ # # ][ # # ]: 0 : wallet_name = request.params[0].get_str();
[ # # ]
468 : : }
469 : :
470 [ # # ]: 0 : WalletContext& context = EnsureWalletContext(request.context);
471 [ # # ]: 0 : std::shared_ptr<CWallet> wallet = GetWallet(context, wallet_name);
472 [ # # ]: 0 : if (!wallet) {
473 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
[ # # ]
474 : : }
475 : :
476 : 0 : std::vector<bilingual_str> warnings;
477 : : {
478 [ # # ]: 0 : WalletRescanReserver reserver(*wallet);
479 [ # # ][ # # ]: 0 : if (!reserver.reserve()) {
480 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
[ # # ]
481 : : }
482 : :
483 : : // Release the "main" shared pointer and prevent further notifications.
484 : : // Note that any attempt to load the same wallet would fail until the wallet
485 : : // is destroyed (see CheckUniqueFileid).
486 [ # # ]: 0 : std::optional<bool> load_on_start{self.MaybeArg<bool>(1)};
487 [ # # ][ # # ]: 0 : if (!RemoveWallet(context, wallet, load_on_start, warnings)) {
488 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_MISC_ERROR, "Requested wallet already unloaded");
[ # # ]
489 : : }
490 : 0 : }
491 : :
492 [ # # ]: 0 : UnloadWallet(std::move(wallet));
493 : :
494 [ # # ]: 0 : UniValue result(UniValue::VOBJ);
495 [ # # ]: 0 : PushWarnings(warnings, result);
496 : :
497 : 0 : return result;
498 [ # # ]: 0 : },
499 : : };
500 : 0 : }
501 : :
502 : 0 : static RPCHelpMan sethdseed()
503 : : {
504 [ # # ][ # # ]: 0 : return RPCHelpMan{"sethdseed",
[ # # ]
505 : : "\nSet or generate a new HD wallet seed. Non-HD wallets will not be upgraded to being a HD wallet. Wallets that are already\n"
506 : : "HD will have a new HD seed set so that new keys added to the keypool will be derived from this new seed.\n"
507 [ # # ][ # # ]: 0 : "\nNote that you will need to MAKE A NEW BACKUP of your wallet after setting the HD wallet seed." + HELP_REQUIRING_PASSPHRASE +
508 : : "Note: This command is only compatible with legacy wallets.\n",
509 [ # # ]: 0 : {
510 [ # # ][ # # ]: 0 : {"newkeypool", RPCArg::Type::BOOL, RPCArg::Default{true}, "Whether to flush old unused addresses, including change addresses, from the keypool and regenerate it.\n"
[ # # ][ # # ]
511 : : "If true, the next address from getnewaddress and change address from getrawchangeaddress will be from this new seed.\n"
512 : : "If false, addresses (including change addresses if the wallet already had HD Chain Split enabled) from the existing\n"
513 : : "keypool will be used until it has been depleted."},
514 [ # # ][ # # ]: 0 : {"seed", RPCArg::Type::STR, RPCArg::DefaultHint{"random seed"}, "The WIF private key to use as the new HD seed.\n"
[ # # ][ # # ]
515 : : "The seed value can be retrieved using the dumpwallet command. It is the private key marked hdseed=1"},
516 : : },
517 [ # # ][ # # ]: 0 : RPCResult{RPCResult::Type::NONE, "", ""},
[ # # ][ # # ]
518 [ # # ]: 0 : RPCExamples{
519 [ # # ][ # # ]: 0 : HelpExampleCli("sethdseed", "")
[ # # ]
520 [ # # ][ # # ]: 0 : + HelpExampleCli("sethdseed", "false")
[ # # ][ # # ]
521 [ # # ][ # # ]: 0 : + HelpExampleCli("sethdseed", "true \"wifkey\"")
[ # # ][ # # ]
522 [ # # ][ # # ]: 0 : + HelpExampleRpc("sethdseed", "true, \"wifkey\"")
[ # # ][ # # ]
523 : : },
524 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
525 : : {
526 : 0 : std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
527 [ # # ][ # # ]: 0 : if (!pwallet) return UniValue::VNULL;
528 : :
529 [ # # ]: 0 : LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet, true);
530 : :
531 [ # # ][ # # ]: 0 : if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
532 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Cannot set a HD seed to a wallet with private keys disabled");
[ # # ]
533 : : }
534 : :
535 [ # # ][ # # ]: 0 : LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
536 : :
537 : : // Do not do anything to non-HD wallets
538 [ # # ][ # # ]: 0 : if (!pwallet->CanSupportFeature(FEATURE_HD)) {
539 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Cannot set an HD seed on a non-HD wallet. Use the upgradewallet RPC in order to upgrade a non-HD wallet to HD");
[ # # ]
540 : : }
541 : :
542 [ # # ]: 0 : EnsureWalletIsUnlocked(*pwallet);
543 : :
544 : 0 : bool flush_key_pool = true;
545 [ # # ][ # # ]: 0 : if (!request.params[0].isNull()) {
546 [ # # ][ # # ]: 0 : flush_key_pool = request.params[0].get_bool();
547 : 0 : }
548 : :
549 [ # # ]: 0 : CPubKey master_pub_key;
550 [ # # ][ # # ]: 0 : if (request.params[1].isNull()) {
551 [ # # ]: 0 : master_pub_key = spk_man.GenerateNewSeed();
552 : 0 : } else {
553 [ # # ][ # # ]: 0 : CKey key = DecodeSecret(request.params[1].get_str());
[ # # ]
554 [ # # ][ # # ]: 0 : if (!key.IsValid()) {
555 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
[ # # ]
556 : : }
557 : :
558 [ # # ][ # # ]: 0 : if (HaveKey(spk_man, key)) {
559 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Already have this key (either as an HD seed or as a loose private key)");
[ # # ]
560 : : }
561 : :
562 [ # # ]: 0 : master_pub_key = spk_man.DeriveNewSeed(key);
563 : 0 : }
564 : :
565 [ # # ]: 0 : spk_man.SetHDSeed(master_pub_key);
566 [ # # ][ # # ]: 0 : if (flush_key_pool) spk_man.NewKeyPool();
567 : :
568 [ # # ]: 0 : return UniValue::VNULL;
569 : 0 : },
570 : : };
571 : 0 : }
572 : :
573 : 0 : static RPCHelpMan upgradewallet()
574 : : {
575 [ # # ][ # # ]: 0 : return RPCHelpMan{"upgradewallet",
[ # # ][ # # ]
576 [ # # ]: 0 : "\nUpgrade the wallet. Upgrades to the latest version if no version number is specified.\n"
577 : : "New keys may be generated and a new wallet backup will need to be made.",
578 [ # # ]: 0 : {
579 [ # # ][ # # ]: 0 : {"version", RPCArg::Type::NUM, RPCArg::Default{int{FEATURE_LATEST}}, "The version number to upgrade to. Default is the latest wallet version."}
[ # # ][ # # ]
580 : : },
581 [ # # ][ # # ]: 0 : RPCResult{
582 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ, "", "",
583 [ # # ]: 0 : {
584 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "wallet_name", "Name of wallet this operation was performed on"},
[ # # ]
585 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "previous_version", "Version of wallet before this operation"},
[ # # ]
586 [ # # ][ # # ]: 0 : {RPCResult::Type::NUM, "current_version", "Version of wallet after this operation"},
[ # # ]
587 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "result", /*optional=*/true, "Description of result, if no error"},
[ # # ]
588 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "error", /*optional=*/true, "Error message (if there is one)"}
[ # # ]
589 : : },
590 : : },
591 [ # # ]: 0 : RPCExamples{
592 [ # # ][ # # ]: 0 : HelpExampleCli("upgradewallet", "169900")
[ # # ]
593 [ # # ][ # # ]: 0 : + HelpExampleRpc("upgradewallet", "169900")
[ # # ][ # # ]
594 : : },
595 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
596 : : {
597 : 0 : std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
598 [ # # ][ # # ]: 0 : if (!pwallet) return UniValue::VNULL;
599 : :
600 [ # # ]: 0 : EnsureWalletIsUnlocked(*pwallet);
601 : :
602 : 0 : int version = 0;
603 [ # # ][ # # ]: 0 : if (!request.params[0].isNull()) {
604 [ # # ][ # # ]: 0 : version = request.params[0].getInt<int>();
605 : 0 : }
606 : 0 : bilingual_str error;
607 [ # # ]: 0 : const int previous_version{pwallet->GetVersion()};
608 [ # # ]: 0 : const bool wallet_upgraded{pwallet->UpgradeWallet(version, error)};
609 [ # # ]: 0 : const int current_version{pwallet->GetVersion()};
610 : 0 : std::string result;
611 : :
612 [ # # ]: 0 : if (wallet_upgraded) {
613 [ # # ]: 0 : if (previous_version == current_version) {
614 [ # # ]: 0 : result = "Already at latest version. Wallet version unchanged.";
615 : 0 : } else {
616 [ # # ]: 0 : result = strprintf("Wallet upgraded successfully from version %i to version %i.", previous_version, current_version);
617 : : }
618 : 0 : }
619 : :
620 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
621 [ # # ][ # # ]: 0 : obj.pushKV("wallet_name", pwallet->GetName());
[ # # ]
622 [ # # ][ # # ]: 0 : obj.pushKV("previous_version", previous_version);
[ # # ]
623 [ # # ][ # # ]: 0 : obj.pushKV("current_version", current_version);
[ # # ]
624 [ # # ]: 0 : if (!result.empty()) {
625 [ # # ][ # # ]: 0 : obj.pushKV("result", result);
[ # # ]
626 : 0 : } else {
627 [ # # ][ # # ]: 0 : CHECK_NONFATAL(!error.empty());
628 [ # # ][ # # ]: 0 : obj.pushKV("error", error.original);
[ # # ]
629 : : }
630 : 0 : return obj;
631 [ # # ]: 0 : },
632 : : };
633 : 0 : }
634 : :
635 : 0 : RPCHelpMan simulaterawtransaction()
636 : : {
637 [ # # ][ # # ]: 0 : return RPCHelpMan{"simulaterawtransaction",
[ # # ][ # # ]
[ # # ][ # # ]
638 [ # # ]: 0 : "\nCalculate the balance change resulting in the signing and broadcasting of the given transaction(s).\n",
639 [ # # ]: 0 : {
640 [ # # ][ # # ]: 0 : {"rawtxs", RPCArg::Type::ARR, RPCArg::Optional::OMITTED, "An array of hex strings of raw transactions.\n",
[ # # ]
641 [ # # ]: 0 : {
642 [ # # ][ # # ]: 0 : {"rawtx", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, ""},
[ # # ]
643 : : },
644 : : },
645 [ # # ][ # # ]: 0 : {"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "",
[ # # ]
646 [ # # ]: 0 : {
647 [ # # ][ # # ]: 0 : {"include_watchonly", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Whether to include watch-only addresses (see RPC importaddress)"},
[ # # ][ # # ]
648 : : },
649 : : },
650 : : },
651 [ # # ][ # # ]: 0 : RPCResult{
652 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ, "", "",
653 [ # # ]: 0 : {
654 [ # # ][ # # ]: 0 : {RPCResult::Type::STR_AMOUNT, "balance_change", "The wallet balance change (negative means decrease)."},
[ # # ]
655 : : }
656 : : },
657 [ # # ]: 0 : RPCExamples{
658 [ # # ][ # # ]: 0 : HelpExampleCli("simulaterawtransaction", "[\"myhex\"]")
[ # # ]
659 [ # # ][ # # ]: 0 : + HelpExampleRpc("simulaterawtransaction", "[\"myhex\"]")
[ # # ][ # # ]
660 : : },
661 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
662 : : {
663 : 0 : const std::shared_ptr<const CWallet> rpc_wallet = GetWalletForJSONRPCRequest(request);
664 [ # # ][ # # ]: 0 : if (!rpc_wallet) return UniValue::VNULL;
665 : 0 : const CWallet& wallet = *rpc_wallet;
666 : :
667 [ # # ]: 0 : LOCK(wallet.cs_wallet);
668 : :
669 [ # # ]: 0 : UniValue include_watchonly(UniValue::VNULL);
670 [ # # ][ # # ]: 0 : if (request.params[1].isObject()) {
[ # # ]
671 [ # # ][ # # ]: 0 : UniValue options = request.params[1];
672 [ # # ]: 0 : RPCTypeCheckObj(options,
673 [ # # ]: 0 : {
674 [ # # ][ # # ]: 0 : {"include_watchonly", UniValueType(UniValue::VBOOL)},
675 : : },
676 : : true, true);
677 : :
678 [ # # ][ # # ]: 0 : include_watchonly = options["include_watchonly"];
[ # # ]
679 : 0 : }
680 : :
681 : 0 : isminefilter filter = ISMINE_SPENDABLE;
682 [ # # ][ # # ]: 0 : if (ParseIncludeWatchonly(include_watchonly, wallet)) {
683 : 0 : filter |= ISMINE_WATCH_ONLY;
684 : 0 : }
685 : :
686 [ # # ][ # # ]: 0 : const auto& txs = request.params[0].get_array();
687 : 0 : CAmount changes{0};
688 : 0 : std::map<COutPoint, CAmount> new_utxos; // UTXO:s that were made available in transaction array
689 : 0 : std::set<COutPoint> spent;
690 : :
691 [ # # ][ # # ]: 0 : for (size_t i = 0; i < txs.size(); ++i) {
692 [ # # ]: 0 : CMutableTransaction mtx;
693 [ # # ][ # # ]: 0 : if (!DecodeHexTx(mtx, txs[i].get_str(), /* try_no_witness */ true, /* try_witness */ true)) {
[ # # ][ # # ]
694 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Transaction hex string decoding failure.");
[ # # ]
695 : : }
696 : :
697 : : // Fetch previous transactions (inputs)
698 : 0 : std::map<COutPoint, Coin> coins;
699 [ # # ]: 0 : for (const CTxIn& txin : mtx.vin) {
700 [ # # ]: 0 : coins[txin.prevout]; // Create empty map entry keyed by prevout.
701 : : }
702 [ # # ][ # # ]: 0 : wallet.chain().findCoins(coins);
703 : :
704 : : // Fetch debit; we are *spending* these; if the transaction is signed and
705 : : // broadcast, we will lose everything in these
706 [ # # ]: 0 : for (const auto& txin : mtx.vin) {
707 : 0 : const auto& outpoint = txin.prevout;
708 [ # # ][ # # ]: 0 : if (spent.count(outpoint)) {
709 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Transaction(s) are spending the same output more than once");
[ # # ]
710 : : }
711 [ # # ][ # # ]: 0 : if (new_utxos.count(outpoint)) {
712 [ # # ]: 0 : changes -= new_utxos.at(outpoint);
713 [ # # ]: 0 : new_utxos.erase(outpoint);
714 : 0 : } else {
715 [ # # ][ # # ]: 0 : if (coins.at(outpoint).IsSpent()) {
[ # # ]
716 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "One or more transaction inputs are missing or have been spent already");
[ # # ]
717 : : }
718 [ # # ]: 0 : changes -= wallet.GetDebit(txin, filter);
719 : : }
720 [ # # ]: 0 : spent.insert(outpoint);
721 : : }
722 : :
723 : : // Iterate over outputs; we are *receiving* these, if the wallet considers
724 : : // them "mine"; if the transaction is signed and broadcast, we will receive
725 : : // everything in these
726 : : // Also populate new_utxos in case these are spent in later transactions
727 : :
728 [ # # ]: 0 : const auto& hash = mtx.GetHash();
729 [ # # ]: 0 : for (size_t i = 0; i < mtx.vout.size(); ++i) {
730 : 0 : const auto& txout = mtx.vout[i];
731 [ # # ]: 0 : bool is_mine = 0 < (wallet.IsMine(txout) & filter);
732 [ # # ][ # # ]: 0 : changes += new_utxos[COutPoint(hash, i)] = is_mine ? txout.nValue : 0;
[ # # ][ # # ]
733 : 0 : }
734 : 0 : }
735 : :
736 [ # # ]: 0 : UniValue result(UniValue::VOBJ);
737 [ # # ][ # # ]: 0 : result.pushKV("balance_change", ValueFromAmount(changes));
[ # # ]
738 : :
739 : 0 : return result;
740 [ # # ]: 0 : }
741 : : };
742 : 0 : }
743 : :
744 : 0 : static RPCHelpMan migratewallet()
745 : : {
746 [ # # ][ # # ]: 0 : return RPCHelpMan{"migratewallet",
[ # # ][ # # ]
747 [ # # ]: 0 : "EXPERIMENTAL warning: This call may not work as expected and may be changed in future releases\n"
748 : : "\nMigrate the wallet to a descriptor wallet.\n"
749 : : "A new wallet backup will need to be made.\n"
750 : : "\nThe migration process will create a backup of the wallet before migrating. This backup\n"
751 : : "file will be named <wallet name>-<timestamp>.legacy.bak and can be found in the directory\n"
752 : : "for this wallet. In the event of an incorrect migration, the backup can be restored using restorewallet."
753 : : "\nEncrypted wallets must have the passphrase provided as an argument to this call.",
754 [ # # ]: 0 : {
755 [ # # ][ # # ]: 0 : {"wallet_name", RPCArg::Type::STR, RPCArg::DefaultHint{"the wallet name from the RPC endpoint"}, "The name of the wallet to migrate. If provided both here and in the RPC endpoint, the two must be identical."},
[ # # ][ # # ]
756 [ # # ][ # # ]: 0 : {"passphrase", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "The wallet passphrase"},
[ # # ]
757 : : },
758 [ # # ][ # # ]: 0 : RPCResult{
759 [ # # ][ # # ]: 0 : RPCResult::Type::OBJ, "", "",
760 [ # # ]: 0 : {
761 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "wallet_name", "The name of the primary migrated wallet"},
[ # # ]
762 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "watchonly_name", /*optional=*/true, "The name of the migrated wallet containing the watchonly scripts"},
[ # # ]
763 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "solvables_name", /*optional=*/true, "The name of the migrated wallet containing solvable but not watched scripts"},
[ # # ]
764 [ # # ][ # # ]: 0 : {RPCResult::Type::STR, "backup_path", "The location of the backup of the original wallet"},
[ # # ]
765 : : }
766 : : },
767 [ # # ]: 0 : RPCExamples{
768 [ # # ][ # # ]: 0 : HelpExampleCli("migratewallet", "")
[ # # ]
769 [ # # ][ # # ]: 0 : + HelpExampleRpc("migratewallet", "")
[ # # ][ # # ]
770 : : },
771 : 0 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
772 : : {
773 : 0 : std::string wallet_name;
774 [ # # ][ # # ]: 0 : if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
775 [ # # ][ # # ]: 0 : if (!(request.params[0].isNull() || request.params[0].get_str() == wallet_name)) {
[ # # ][ # # ]
[ # # ]
776 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "RPC endpoint wallet and wallet_name parameter specify different wallets");
[ # # ]
777 : : }
778 : 0 : } else {
779 [ # # ][ # # ]: 0 : if (request.params[0].isNull()) {
780 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Either RPC endpoint wallet or wallet_name parameter must be provided");
[ # # ]
781 : : }
782 [ # # ][ # # ]: 0 : wallet_name = request.params[0].get_str();
[ # # ]
783 : : }
784 : :
785 : 0 : SecureString wallet_pass;
786 [ # # ]: 0 : wallet_pass.reserve(100);
787 [ # # ][ # # ]: 0 : if (!request.params[1].isNull()) {
788 [ # # ][ # # ]: 0 : wallet_pass = std::string_view{request.params[1].get_str()};
[ # # ]
789 : 0 : }
790 : :
791 [ # # ]: 0 : WalletContext& context = EnsureWalletContext(request.context);
792 [ # # ]: 0 : util::Result<MigrationResult> res = MigrateLegacyToDescriptor(wallet_name, wallet_pass, context);
793 [ # # ]: 0 : if (!res) {
794 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(res).original);
[ # # ]
795 : : }
796 : :
797 [ # # ]: 0 : UniValue r{UniValue::VOBJ};
798 [ # # ][ # # ]: 0 : r.pushKV("wallet_name", res->wallet_name);
[ # # ][ # # ]
799 [ # # ][ # # ]: 0 : if (res->watchonly_wallet) {
800 [ # # ][ # # ]: 0 : r.pushKV("watchonly_name", res->watchonly_wallet->GetName());
[ # # ][ # # ]
801 : 0 : }
802 [ # # ][ # # ]: 0 : if (res->solvables_wallet) {
803 [ # # ][ # # ]: 0 : r.pushKV("solvables_name", res->solvables_wallet->GetName());
[ # # ][ # # ]
804 : 0 : }
805 [ # # ][ # # ]: 0 : r.pushKV("backup_path", res->backup_path.u8string());
[ # # ][ # # ]
[ # # ]
806 : :
807 : 0 : return r;
808 [ # # ]: 0 : },
809 : : };
810 : 0 : }
811 : :
812 : : // addresses
813 : : RPCHelpMan getaddressinfo();
814 : : RPCHelpMan getnewaddress();
815 : : RPCHelpMan getrawchangeaddress();
816 : : RPCHelpMan setlabel();
817 : : RPCHelpMan listaddressgroupings();
818 : : RPCHelpMan addmultisigaddress();
819 : : RPCHelpMan keypoolrefill();
820 : : RPCHelpMan newkeypool();
821 : : RPCHelpMan getaddressesbylabel();
822 : : RPCHelpMan listlabels();
823 : : #ifdef ENABLE_EXTERNAL_SIGNER
824 : : RPCHelpMan walletdisplayaddress();
825 : : #endif // ENABLE_EXTERNAL_SIGNER
826 : :
827 : : // backup
828 : : RPCHelpMan dumpprivkey();
829 : : RPCHelpMan importprivkey();
830 : : RPCHelpMan importaddress();
831 : : RPCHelpMan importpubkey();
832 : : RPCHelpMan dumpwallet();
833 : : RPCHelpMan importwallet();
834 : : RPCHelpMan importprunedfunds();
835 : : RPCHelpMan removeprunedfunds();
836 : : RPCHelpMan importmulti();
837 : : RPCHelpMan importdescriptors();
838 : : RPCHelpMan listdescriptors();
839 : : RPCHelpMan backupwallet();
840 : : RPCHelpMan restorewallet();
841 : :
842 : : // coins
843 : : RPCHelpMan getreceivedbyaddress();
844 : : RPCHelpMan getreceivedbylabel();
845 : : RPCHelpMan getbalance();
846 : : RPCHelpMan getunconfirmedbalance();
847 : : RPCHelpMan lockunspent();
848 : : RPCHelpMan listlockunspent();
849 : : RPCHelpMan getbalances();
850 : : RPCHelpMan listunspent();
851 : :
852 : : // encryption
853 : : RPCHelpMan walletpassphrase();
854 : : RPCHelpMan walletpassphrasechange();
855 : : RPCHelpMan walletlock();
856 : : RPCHelpMan encryptwallet();
857 : :
858 : : // spend
859 : : RPCHelpMan sendtoaddress();
860 : : RPCHelpMan sendmany();
861 : : RPCHelpMan settxfee();
862 : : RPCHelpMan fundrawtransaction();
863 : : RPCHelpMan bumpfee();
864 : : RPCHelpMan psbtbumpfee();
865 : : RPCHelpMan send();
866 : : RPCHelpMan sendall();
867 : : RPCHelpMan walletprocesspsbt();
868 : : RPCHelpMan walletcreatefundedpsbt();
869 : : RPCHelpMan signrawtransactionwithwallet();
870 : :
871 : : // signmessage
872 : : RPCHelpMan signmessage();
873 : :
874 : : // transactions
875 : : RPCHelpMan listreceivedbyaddress();
876 : : RPCHelpMan listreceivedbylabel();
877 : : RPCHelpMan listtransactions();
878 : : RPCHelpMan listsinceblock();
879 : : RPCHelpMan gettransaction();
880 : : RPCHelpMan abandontransaction();
881 : : RPCHelpMan rescanblockchain();
882 : : RPCHelpMan abortrescan();
883 : :
884 : 0 : Span<const CRPCCommand> GetWalletRPCCommands()
885 : : {
886 [ # # ][ # # ]: 0 : static const CRPCCommand commands[]{
[ # # ]
887 [ # # ][ # # ]: 0 : {"rawtransactions", &fundrawtransaction},
888 [ # # ][ # # ]: 0 : {"wallet", &abandontransaction},
889 [ # # ][ # # ]: 0 : {"wallet", &abortrescan},
890 [ # # ][ # # ]: 0 : {"wallet", &addmultisigaddress},
891 [ # # ][ # # ]: 0 : {"wallet", &backupwallet},
892 [ # # ][ # # ]: 0 : {"wallet", &bumpfee},
893 [ # # ][ # # ]: 0 : {"wallet", &psbtbumpfee},
894 [ # # ][ # # ]: 0 : {"wallet", &createwallet},
895 [ # # ][ # # ]: 0 : {"wallet", &restorewallet},
896 [ # # ][ # # ]: 0 : {"wallet", &dumpprivkey},
897 [ # # ][ # # ]: 0 : {"wallet", &dumpwallet},
898 [ # # ][ # # ]: 0 : {"wallet", &encryptwallet},
899 [ # # ][ # # ]: 0 : {"wallet", &getaddressesbylabel},
900 [ # # ][ # # ]: 0 : {"wallet", &getaddressinfo},
901 [ # # ][ # # ]: 0 : {"wallet", &getbalance},
902 [ # # ][ # # ]: 0 : {"wallet", &getnewaddress},
903 [ # # ][ # # ]: 0 : {"wallet", &getrawchangeaddress},
904 [ # # ][ # # ]: 0 : {"wallet", &getreceivedbyaddress},
905 [ # # ][ # # ]: 0 : {"wallet", &getreceivedbylabel},
906 [ # # ][ # # ]: 0 : {"wallet", &gettransaction},
907 [ # # ][ # # ]: 0 : {"wallet", &getunconfirmedbalance},
908 [ # # ][ # # ]: 0 : {"wallet", &getbalances},
909 [ # # ][ # # ]: 0 : {"wallet", &getwalletinfo},
910 [ # # ][ # # ]: 0 : {"wallet", &importaddress},
911 [ # # ][ # # ]: 0 : {"wallet", &importdescriptors},
912 [ # # ][ # # ]: 0 : {"wallet", &importmulti},
913 [ # # ][ # # ]: 0 : {"wallet", &importprivkey},
914 [ # # ][ # # ]: 0 : {"wallet", &importprunedfunds},
915 [ # # ][ # # ]: 0 : {"wallet", &importpubkey},
916 [ # # ][ # # ]: 0 : {"wallet", &importwallet},
917 [ # # ][ # # ]: 0 : {"wallet", &keypoolrefill},
918 [ # # ][ # # ]: 0 : {"wallet", &listaddressgroupings},
919 [ # # ][ # # ]: 0 : {"wallet", &listdescriptors},
920 [ # # ][ # # ]: 0 : {"wallet", &listlabels},
921 [ # # ][ # # ]: 0 : {"wallet", &listlockunspent},
922 [ # # ][ # # ]: 0 : {"wallet", &listreceivedbyaddress},
923 [ # # ][ # # ]: 0 : {"wallet", &listreceivedbylabel},
924 [ # # ][ # # ]: 0 : {"wallet", &listsinceblock},
925 [ # # ][ # # ]: 0 : {"wallet", &listtransactions},
926 [ # # ][ # # ]: 0 : {"wallet", &listunspent},
927 [ # # ][ # # ]: 0 : {"wallet", &listwalletdir},
928 [ # # ][ # # ]: 0 : {"wallet", &listwallets},
929 [ # # ][ # # ]: 0 : {"wallet", &loadwallet},
930 [ # # ][ # # ]: 0 : {"wallet", &lockunspent},
931 [ # # ][ # # ]: 0 : {"wallet", &migratewallet},
932 [ # # ][ # # ]: 0 : {"wallet", &newkeypool},
933 [ # # ][ # # ]: 0 : {"wallet", &removeprunedfunds},
934 [ # # ][ # # ]: 0 : {"wallet", &rescanblockchain},
935 [ # # ][ # # ]: 0 : {"wallet", &send},
936 [ # # ][ # # ]: 0 : {"wallet", &sendmany},
937 [ # # ][ # # ]: 0 : {"wallet", &sendtoaddress},
938 [ # # ][ # # ]: 0 : {"wallet", &sethdseed},
939 [ # # ][ # # ]: 0 : {"wallet", &setlabel},
940 [ # # ][ # # ]: 0 : {"wallet", &settxfee},
941 [ # # ][ # # ]: 0 : {"wallet", &setwalletflag},
942 [ # # ][ # # ]: 0 : {"wallet", &signmessage},
943 [ # # ][ # # ]: 0 : {"wallet", &signrawtransactionwithwallet},
944 [ # # ][ # # ]: 0 : {"wallet", &simulaterawtransaction},
945 [ # # ][ # # ]: 0 : {"wallet", &sendall},
946 [ # # ][ # # ]: 0 : {"wallet", &unloadwallet},
947 [ # # ][ # # ]: 0 : {"wallet", &upgradewallet},
948 [ # # ][ # # ]: 0 : {"wallet", &walletcreatefundedpsbt},
949 : : #ifdef ENABLE_EXTERNAL_SIGNER
950 : : {"wallet", &walletdisplayaddress},
951 : : #endif // ENABLE_EXTERNAL_SIGNER
952 [ # # ][ # # ]: 0 : {"wallet", &walletlock},
953 [ # # ][ # # ]: 0 : {"wallet", &walletpassphrase},
954 [ # # ][ # # ]: 0 : {"wallet", &walletpassphrasechange},
955 [ # # ][ # # ]: 0 : {"wallet", &walletprocesspsbt},
956 : : };
957 : 0 : return commands;
958 : 0 : }
959 : : } // namespace wallet
|