Line data Source code
1 : // Copyright (c) 2009-2020 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 <chain.h>
6 : #include <core_io.h>
7 : #include <interfaces/chain.h>
8 : #include <key_io.h>
9 : #include <merkleblock.h>
10 : #include <rpc/util.h>
11 : #include <script/descriptor.h>
12 : #include <script/script.h>
13 : #include <script/standard.h>
14 : #include <sync.h>
15 : #include <util/bip32.h>
16 : #include <util/system.h>
17 : #include <util/time.h>
18 : #include <util/translation.h>
19 : #include <wallet/rpcwallet.h>
20 : #include <wallet/wallet.h>
21 :
22 : #include <stdint.h>
23 : #include <tuple>
24 :
25 : #include <boost/algorithm/string.hpp>
26 :
27 : #include <univalue.h>
28 :
29 :
30 :
31 : using interfaces::FoundBlock;
32 :
33 84 : std::string static EncodeDumpString(const std::string &str) {
34 84 : std::stringstream ret;
35 156 : for (const unsigned char c : str) {
36 72 : if (c <= 32 || c >= 128 || c == '%') {
37 0 : ret << '%' << HexStr(Span<const unsigned char>(&c, 1));
38 0 : } else {
39 72 : ret << c;
40 : }
41 72 : }
42 84 : return ret.str();
43 84 : }
44 :
45 48 : static std::string DecodeDumpString(const std::string &str) {
46 48 : std::stringstream ret;
47 72 : for (unsigned int pos = 0; pos < str.length(); pos++) {
48 24 : unsigned char c = str[pos];
49 24 : if (c == '%' && pos+2 < str.length()) {
50 0 : c = (((str[pos+1]>>6)*9+((str[pos+1]-'0')&15)) << 4) |
51 0 : ((str[pos+2]>>6)*9+((str[pos+2]-'0')&15));
52 : pos += 2;
53 0 : }
54 24 : ret << c;
55 : }
56 48 : return ret.str();
57 48 : }
58 :
59 1250 : static bool GetWalletAddressesForKey(LegacyScriptPubKeyMan* spk_man, const CWallet* const pwallet, const CKeyID& keyid, std::string& strAddr, std::string& strLabel) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
60 : {
61 : bool fLabelFound = false;
62 1250 : CKey key;
63 1250 : spk_man->GetKey(keyid, key);
64 5000 : for (const auto& dest : GetAllDestinationsForKey(key.GetPubKey())) {
65 3750 : const auto* address_book_entry = pwallet->FindAddressBookEntry(dest);
66 3750 : if (address_book_entry) {
67 84 : if (!strAddr.empty()) {
68 6 : strAddr += ",";
69 : }
70 84 : strAddr += EncodeDestination(dest);
71 84 : strLabel = EncodeDumpString(address_book_entry->GetLabel());
72 : fLabelFound = true;
73 84 : }
74 : }
75 1250 : if (!fLabelFound) {
76 1172 : strAddr = EncodeDestination(GetDestinationForKey(key.GetPubKey(), pwallet->m_default_address_type));
77 1172 : }
78 : return fLabelFound;
79 1250 : }
80 :
81 : static const int64_t TIMESTAMP_MIN = 0;
82 :
83 341 : static void RescanWallet(CWallet& wallet, const WalletRescanReserver& reserver, int64_t time_begin = TIMESTAMP_MIN, bool update = true)
84 : {
85 341 : int64_t scanned_time = wallet.RescanFromTime(time_begin, reserver, update);
86 341 : if (wallet.IsAbortingRescan()) {
87 0 : throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");
88 341 : } else if (scanned_time > time_begin) {
89 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Rescan was unable to fully rescan the blockchain. Some transactions may be missing.");
90 : }
91 341 : }
92 :
93 2418 : RPCHelpMan importprivkey()
94 : {
95 9672 : return RPCHelpMan{"importprivkey",
96 2418 : "\nAdds a private key (as returned by dumpprivkey) to your wallet. Requires a new wallet backup.\n"
97 : "Hint: use importmulti to import more than one private key.\n"
98 : "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n"
99 : "may report that the imported key exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n"
100 : "Note: Use \"getwalletinfo\" to query the scanning progress.\n",
101 9672 : {
102 2418 : {"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key (see dumpprivkey)"},
103 2418 : {"label", RPCArg::Type::STR, /* default */ "current label if address exists, otherwise \"\"", "An optional label"},
104 2418 : {"rescan", RPCArg::Type::BOOL, /* default */ "true", "Rescan the wallet for transactions"},
105 : },
106 2418 : RPCResult{RPCResult::Type::NONE, "", ""},
107 2418 : RPCExamples{
108 2418 : "\nDump a private key\n"
109 2418 : + HelpExampleCli("dumpprivkey", "\"myaddress\"") +
110 : "\nImport the private key with rescan\n"
111 2418 : + HelpExampleCli("importprivkey", "\"mykey\"") +
112 : "\nImport using a label and without rescan\n"
113 2418 : + HelpExampleCli("importprivkey", "\"mykey\" \"testing\" false") +
114 : "\nImport using default blank label and without rescan\n"
115 2418 : + HelpExampleCli("importprivkey", "\"mykey\" \"\" false") +
116 : "\nAs a JSON-RPC call\n"
117 2418 : + HelpExampleRpc("importprivkey", "\"mykey\", \"testing\", false")
118 : },
119 2732 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
120 : {
121 314 : std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
122 314 : if (!wallet) return NullUniValue;
123 314 : CWallet* const pwallet = wallet.get();
124 :
125 314 : if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
126 2 : throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
127 : }
128 :
129 312 : EnsureLegacyScriptPubKeyMan(*wallet, true);
130 :
131 311 : WalletRescanReserver reserver(*pwallet);
132 : bool fRescan = true;
133 : {
134 311 : LOCK(pwallet->cs_wallet);
135 :
136 311 : EnsureWalletIsUnlocked(pwallet);
137 :
138 311 : std::string strSecret = request.params[0].get_str();
139 311 : std::string strLabel = "";
140 311 : if (!request.params[1].isNull())
141 282 : strLabel = request.params[1].get_str();
142 :
143 : // Whether to perform rescan after import
144 311 : if (!request.params[2].isNull())
145 19 : fRescan = request.params[2].get_bool();
146 :
147 311 : if (fRescan && pwallet->chain().havePruned()) {
148 : // Exit early and print an error.
149 : // If a block is pruned after this check, we will import the key(s),
150 : // but fail the rescan with a generic error.
151 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned");
152 : }
153 :
154 311 : if (fRescan && !reserver.reserve()) {
155 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
156 : }
157 :
158 311 : CKey key = DecodeSecret(strSecret);
159 311 : if (!key.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
160 :
161 310 : CPubKey pubkey = key.GetPubKey();
162 310 : CHECK_NONFATAL(key.VerifyPubKey(pubkey));
163 310 : CKeyID vchAddress = pubkey.GetID();
164 : {
165 310 : pwallet->MarkDirty();
166 :
167 : // We don't know which corresponding address will be used;
168 : // label all new addresses, and label existing addresses if a
169 : // label was passed.
170 1234 : for (const auto& dest : GetAllDestinationsForKey(pubkey)) {
171 924 : if (!request.params[1].isNull() || !pwallet->FindAddressBookEntry(dest)) {
172 911 : pwallet->SetAddressBook(dest, strLabel, "receive");
173 911 : }
174 : }
175 :
176 : // Use timestamp of 1 to scan the whole chain
177 310 : if (!pwallet->ImportPrivKeys({{vchAddress, key}}, 1)) {
178 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
179 : }
180 :
181 : // Add the wpkh script for this key if possible
182 310 : if (pubkey.IsCompressed()) {
183 307 : pwallet->ImportScripts({GetScriptForDestination(WitnessV0KeyHash(vchAddress))}, 0 /* timestamp */);
184 307 : }
185 : }
186 311 : }
187 310 : if (fRescan) {
188 297 : RescanWallet(*pwallet, reserver);
189 : }
190 :
191 310 : return NullUniValue;
192 315 : },
193 : };
194 0 : }
195 :
196 2104 : RPCHelpMan abortrescan()
197 : {
198 4208 : return RPCHelpMan{"abortrescan",
199 2104 : "\nStops current wallet rescan triggered by an RPC call, e.g. by an importprivkey call.\n"
200 : "Note: Use \"getwalletinfo\" to query the scanning progress.\n",
201 2104 : {},
202 2104 : RPCResult{RPCResult::Type::BOOL, "", "Whether the abort was successful"},
203 2104 : RPCExamples{
204 2104 : "\nImport a private key\n"
205 2104 : + HelpExampleCli("importprivkey", "\"mykey\"") +
206 : "\nAbort the running wallet rescan\n"
207 2104 : + HelpExampleCli("abortrescan", "") +
208 : "\nAs a JSON-RPC call\n"
209 2104 : + HelpExampleRpc("abortrescan", "")
210 : },
211 2104 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
212 : {
213 0 : std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
214 0 : if (!wallet) return NullUniValue;
215 0 : CWallet* const pwallet = wallet.get();
216 :
217 0 : if (!pwallet->IsScanning() || pwallet->IsAbortingRescan()) return false;
218 0 : pwallet->AbortRescan();
219 0 : return true;
220 0 : },
221 : };
222 0 : }
223 :
224 2193 : RPCHelpMan importaddress()
225 : {
226 10965 : return RPCHelpMan{"importaddress",
227 2193 : "\nAdds an address or script (in hex) that can be watched as if it were in your wallet but cannot be used to spend. Requires a new wallet backup.\n"
228 : "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n"
229 : "may report that the imported address exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n"
230 : "If you have the full public key, you should call importpubkey instead of this.\n"
231 : "Hint: use importmulti to import more than one address.\n"
232 : "\nNote: If you import a non-standard raw script in hex form, outputs sending to it will be treated\n"
233 : "as change, and not show up in many RPCs.\n"
234 : "Note: Use \"getwalletinfo\" to query the scanning progress.\n",
235 10965 : {
236 2193 : {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The Bitcoin address (or hex-encoded script)"},
237 2193 : {"label", RPCArg::Type::STR, /* default */ "\"\"", "An optional label"},
238 2193 : {"rescan", RPCArg::Type::BOOL, /* default */ "true", "Rescan the wallet for transactions"},
239 2193 : {"p2sh", RPCArg::Type::BOOL, /* default */ "false", "Add the P2SH version of the script as well"},
240 : },
241 2193 : RPCResult{RPCResult::Type::NONE, "", ""},
242 2193 : RPCExamples{
243 2193 : "\nImport an address with rescan\n"
244 2193 : + HelpExampleCli("importaddress", "\"myaddress\"") +
245 : "\nImport using a label without rescan\n"
246 2193 : + HelpExampleCli("importaddress", "\"myaddress\" \"testing\" false") +
247 : "\nAs a JSON-RPC call\n"
248 2193 : + HelpExampleRpc("importaddress", "\"myaddress\", \"testing\", false")
249 : },
250 2282 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
251 : {
252 89 : std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
253 89 : if (!wallet) return NullUniValue;
254 89 : CWallet* const pwallet = wallet.get();
255 :
256 89 : EnsureLegacyScriptPubKeyMan(*pwallet, true);
257 :
258 88 : std::string strLabel;
259 88 : if (!request.params[1].isNull())
260 76 : strLabel = request.params[1].get_str();
261 :
262 : // Whether to perform rescan after import
263 : bool fRescan = true;
264 88 : if (!request.params[2].isNull())
265 74 : fRescan = request.params[2].get_bool();
266 :
267 88 : if (fRescan && pwallet->chain().havePruned()) {
268 : // Exit early and print an error.
269 : // If a block is pruned after this check, we will import the key(s),
270 : // but fail the rescan with a generic error.
271 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned");
272 : }
273 :
274 88 : WalletRescanReserver reserver(*pwallet);
275 88 : if (fRescan && !reserver.reserve()) {
276 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
277 : }
278 :
279 : // Whether to import a p2sh version, too
280 : bool fP2SH = false;
281 88 : if (!request.params[3].isNull())
282 43 : fP2SH = request.params[3].get_bool();
283 :
284 : {
285 88 : LOCK(pwallet->cs_wallet);
286 :
287 88 : CTxDestination dest = DecodeDestination(request.params[0].get_str());
288 88 : if (IsValidDestination(dest)) {
289 39 : if (fP2SH) {
290 1 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot use the p2sh flag with an address - use a script instead");
291 : }
292 :
293 38 : pwallet->MarkDirty();
294 :
295 38 : pwallet->ImportScriptPubKeys(strLabel, {GetScriptForDestination(dest)}, false /* have_solving_data */, true /* apply_label */, 1 /* timestamp */);
296 87 : } else if (IsHex(request.params[0].get_str())) {
297 44 : std::vector<unsigned char> data(ParseHex(request.params[0].get_str()));
298 44 : CScript redeem_script(data.begin(), data.end());
299 :
300 44 : std::set<CScript> scripts = {redeem_script};
301 44 : pwallet->ImportScripts(scripts, 0 /* timestamp */);
302 :
303 44 : if (fP2SH) {
304 42 : scripts.insert(GetScriptForDestination(ScriptHash(redeem_script)));
305 42 : }
306 :
307 44 : pwallet->ImportScriptPubKeys(strLabel, scripts, false /* have_solving_data */, true /* apply_label */, 1 /* timestamp */);
308 44 : } else {
309 5 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
310 : }
311 88 : }
312 82 : if (fRescan)
313 : {
314 19 : RescanWallet(*pwallet, reserver);
315 : {
316 19 : LOCK(pwallet->cs_wallet);
317 19 : pwallet->ReacceptWalletTransactions();
318 19 : }
319 19 : }
320 :
321 82 : return NullUniValue;
322 95 : },
323 : };
324 0 : }
325 :
326 2107 : RPCHelpMan importprunedfunds()
327 : {
328 6321 : return RPCHelpMan{"importprunedfunds",
329 2107 : "\nImports funds without rescan. Corresponding address or script must previously be included in wallet. Aimed towards pruned wallets. The end-user is responsible to import additional transactions that subsequently spend the imported outputs or rescan after the point in the blockchain the transaction is included.\n",
330 6321 : {
331 2107 : {"rawtransaction", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "A raw transaction in hex funding an already-existing address in wallet"},
332 2107 : {"txoutproof", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex output from gettxoutproof that contains the transaction"},
333 : },
334 2107 : RPCResult{RPCResult::Type::NONE, "", ""},
335 2107 : RPCExamples{""},
336 2110 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
337 : {
338 3 : std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
339 3 : if (!wallet) return NullUniValue;
340 3 : CWallet* const pwallet = wallet.get();
341 :
342 3 : CMutableTransaction tx;
343 3 : if (!DecodeHexTx(tx, request.params[0].get_str()))
344 0 : throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
345 3 : uint256 hashTx = tx.GetHash();
346 :
347 3 : CDataStream ssMB(ParseHexV(request.params[1], "proof"), SER_NETWORK, PROTOCOL_VERSION);
348 3 : CMerkleBlock merkleBlock;
349 3 : ssMB >> merkleBlock;
350 :
351 : //Search partial merkle tree in proof for our transaction and index in valid block
352 3 : std::vector<uint256> vMatch;
353 3 : std::vector<unsigned int> vIndex;
354 3 : if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot) {
355 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Something wrong with merkleblock");
356 : }
357 :
358 3 : LOCK(pwallet->cs_wallet);
359 3 : int height;
360 3 : if (!pwallet->chain().findAncestorByHash(pwallet->GetLastBlockHash(), merkleBlock.header.GetHash(), FoundBlock().height(height))) {
361 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
362 : }
363 :
364 3 : std::vector<uint256>::const_iterator it;
365 3 : if ((it = std::find(vMatch.begin(), vMatch.end(), hashTx)) == vMatch.end()) {
366 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction given doesn't exist in proof");
367 : }
368 :
369 3 : unsigned int txnIndex = vIndex[it - vMatch.begin()];
370 :
371 3 : CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, height, merkleBlock.header.GetHash(), txnIndex);
372 :
373 3 : CTransactionRef tx_ref = MakeTransactionRef(tx);
374 3 : if (pwallet->IsMine(*tx_ref)) {
375 2 : pwallet->AddToWallet(std::move(tx_ref), confirm);
376 2 : return NullUniValue;
377 : }
378 :
379 1 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No addresses in wallet correspond to included transaction");
380 3 : },
381 : };
382 0 : }
383 :
384 2107 : RPCHelpMan removeprunedfunds()
385 : {
386 4214 : return RPCHelpMan{"removeprunedfunds",
387 2107 : "\nDeletes the specified transaction from the wallet. Meant for use with pruned wallets and as a companion to importprunedfunds. This will affect wallet balances.\n",
388 4214 : {
389 2107 : {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex-encoded id of the transaction you are deleting"},
390 : },
391 2107 : RPCResult{RPCResult::Type::NONE, "", ""},
392 2107 : RPCExamples{
393 2107 : HelpExampleCli("removeprunedfunds", "\"a8d0c0184dde994a09ec054286f1ce581bebf46446a512166eae7628734ea0a5\"") +
394 : "\nAs a JSON-RPC call\n"
395 2107 : + HelpExampleRpc("removeprunedfunds", "\"a8d0c0184dde994a09ec054286f1ce581bebf46446a512166eae7628734ea0a5\"")
396 : },
397 2110 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
398 : {
399 3 : std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
400 3 : if (!wallet) return NullUniValue;
401 3 : CWallet* const pwallet = wallet.get();
402 :
403 3 : LOCK(pwallet->cs_wallet);
404 :
405 3 : uint256 hash(ParseHashV(request.params[0], "txid"));
406 3 : std::vector<uint256> vHash;
407 3 : vHash.push_back(hash);
408 3 : std::vector<uint256> vHashOut;
409 :
410 3 : if (pwallet->ZapSelectTx(vHash, vHashOut) != DBErrors::LOAD_OK) {
411 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Could not properly delete the transaction.");
412 : }
413 :
414 3 : if(vHashOut.empty()) {
415 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Transaction does not exist in wallet.");
416 : }
417 :
418 2 : return NullUniValue;
419 3 : },
420 : };
421 0 : }
422 :
423 2139 : RPCHelpMan importpubkey()
424 : {
425 8556 : return RPCHelpMan{"importpubkey",
426 2139 : "\nAdds a public key (in hex) that can be watched as if it were in your wallet but cannot be used to spend. Requires a new wallet backup.\n"
427 : "Hint: use importmulti to import more than one public key.\n"
428 : "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n"
429 : "may report that the imported pubkey exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n"
430 : "Note: Use \"getwalletinfo\" to query the scanning progress.\n",
431 8556 : {
432 2139 : {"pubkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The hex-encoded public key"},
433 2139 : {"label", RPCArg::Type::STR, /* default */ "\"\"", "An optional label"},
434 2139 : {"rescan", RPCArg::Type::BOOL, /* default */ "true", "Rescan the wallet for transactions"},
435 : },
436 2139 : RPCResult{RPCResult::Type::NONE, "", ""},
437 2139 : RPCExamples{
438 2139 : "\nImport a public key with rescan\n"
439 2139 : + HelpExampleCli("importpubkey", "\"mypubkey\"") +
440 : "\nImport using a label without rescan\n"
441 2139 : + HelpExampleCli("importpubkey", "\"mypubkey\" \"testing\" false") +
442 : "\nAs a JSON-RPC call\n"
443 2139 : + HelpExampleRpc("importpubkey", "\"mypubkey\", \"testing\", false")
444 : },
445 2174 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
446 : {
447 35 : std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
448 35 : if (!wallet) return NullUniValue;
449 35 : CWallet* const pwallet = wallet.get();
450 :
451 35 : EnsureLegacyScriptPubKeyMan(*wallet, true);
452 :
453 34 : std::string strLabel;
454 34 : if (!request.params[1].isNull())
455 23 : strLabel = request.params[1].get_str();
456 :
457 : // Whether to perform rescan after import
458 : bool fRescan = true;
459 34 : if (!request.params[2].isNull())
460 19 : fRescan = request.params[2].get_bool();
461 :
462 34 : if (fRescan && pwallet->chain().havePruned()) {
463 : // Exit early and print an error.
464 : // If a block is pruned after this check, we will import the key(s),
465 : // but fail the rescan with a generic error.
466 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned");
467 : }
468 :
469 34 : WalletRescanReserver reserver(*pwallet);
470 34 : if (fRescan && !reserver.reserve()) {
471 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
472 : }
473 :
474 34 : if (!IsHex(request.params[0].get_str()))
475 1 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey must be a hex string");
476 33 : std::vector<unsigned char> data(ParseHex(request.params[0].get_str()));
477 33 : CPubKey pubKey(data.begin(), data.end());
478 33 : if (!pubKey.IsFullyValid())
479 1 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey is not a valid public key");
480 :
481 : {
482 32 : LOCK(pwallet->cs_wallet);
483 :
484 32 : std::set<CScript> script_pub_keys;
485 124 : for (const auto& dest : GetAllDestinationsForKey(pubKey)) {
486 92 : script_pub_keys.insert(GetScriptForDestination(dest));
487 : }
488 :
489 32 : pwallet->MarkDirty();
490 :
491 32 : pwallet->ImportScriptPubKeys(strLabel, script_pub_keys, true /* have_solving_data */, true /* apply_label */, 1 /* timestamp */);
492 :
493 32 : pwallet->ImportPubKeys({pubKey.GetID()}, {{pubKey.GetID(), pubKey}} , {} /* key_origins */, false /* add_keypool */, false /* internal */, 1 /* timestamp */);
494 32 : }
495 32 : if (fRescan)
496 : {
497 20 : RescanWallet(*pwallet, reserver);
498 : {
499 20 : LOCK(pwallet->cs_wallet);
500 20 : pwallet->ReacceptWalletTransactions();
501 20 : }
502 20 : }
503 :
504 32 : return NullUniValue;
505 35 : },
506 : };
507 0 : }
508 :
509 :
510 2110 : RPCHelpMan importwallet()
511 : {
512 4220 : return RPCHelpMan{"importwallet",
513 2110 : "\nImports keys from a wallet dump file (see dumpwallet). Requires a new wallet backup to include imported keys.\n"
514 : "Note: Use \"getwalletinfo\" to query the scanning progress.\n",
515 4220 : {
516 2110 : {"filename", RPCArg::Type::STR, RPCArg::Optional::NO, "The wallet file"},
517 : },
518 2110 : RPCResult{RPCResult::Type::NONE, "", ""},
519 2110 : RPCExamples{
520 2110 : "\nDump the wallet\n"
521 2110 : + HelpExampleCli("dumpwallet", "\"test\"") +
522 : "\nImport the wallet\n"
523 2110 : + HelpExampleCli("importwallet", "\"test\"") +
524 : "\nImport using the json rpc call\n"
525 2110 : + HelpExampleRpc("importwallet", "\"test\"")
526 : },
527 2116 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
528 : {
529 6 : std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
530 6 : if (!wallet) return NullUniValue;
531 6 : CWallet* const pwallet = wallet.get();
532 :
533 6 : EnsureLegacyScriptPubKeyMan(*wallet, true);
534 :
535 5 : if (pwallet->chain().havePruned()) {
536 : // Exit early and print an error.
537 : // If a block is pruned after this check, we will import the key(s),
538 : // but fail the rescan with a generic error.
539 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Importing wallets is disabled when blocks are pruned");
540 : }
541 :
542 5 : WalletRescanReserver reserver(*pwallet);
543 5 : if (!reserver.reserve()) {
544 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
545 : }
546 :
547 5 : int64_t nTimeBegin = 0;
548 : bool fGood = true;
549 : {
550 5 : LOCK(pwallet->cs_wallet);
551 :
552 5 : EnsureWalletIsUnlocked(pwallet);
553 :
554 5 : fsbridge::ifstream file;
555 5 : file.open(request.params[0].get_str(), std::ios::in | std::ios::ate);
556 5 : if (!file.is_open()) {
557 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
558 : }
559 5 : CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(nTimeBegin)));
560 :
561 5 : int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg());
562 5 : file.seekg(0, file.beg);
563 :
564 : // Use uiInterface.ShowProgress instead of pwallet.ShowProgress because pwallet.ShowProgress has a cancel button tied to AbortRescan which
565 : // we don't want for this progress bar showing the import progress. uiInterface.ShowProgress does not have a cancel button.
566 5 : pwallet->chain().showProgress(strprintf("%s " + _("Importing...").translated, pwallet->GetDisplayName()), 0, false); // show progress dialog in GUI
567 5 : std::vector<std::tuple<CKey, int64_t, bool, std::string>> keys;
568 5 : std::vector<std::pair<CScript, int64_t>> scripts;
569 1775 : while (file.good()) {
570 1770 : pwallet->chain().showProgress("", std::max(1, std::min(50, (int)(((double)file.tellg() / (double)nFilesize) * 100))), false);
571 1770 : std::string line;
572 1770 : std::getline(file, line);
573 1770 : if (line.empty() || line[0] == '#')
574 53 : continue;
575 :
576 1717 : std::vector<std::string> vstr;
577 1717 : boost::split(vstr, line, boost::is_any_of(" "));
578 1717 : if (vstr.size() < 2)
579 0 : continue;
580 1717 : CKey key = DecodeSecret(vstr[0]);
581 1717 : if (key.IsValid()) {
582 858 : int64_t nTime = ParseISO8601DateTime(vstr[1]);
583 858 : std::string strLabel;
584 858 : bool fLabel = true;
585 1716 : for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
586 1716 : if (vstr[nStr].front() == '#')
587 858 : break;
588 858 : if (vstr[nStr] == "change=1")
589 29 : fLabel = false;
590 858 : if (vstr[nStr] == "reserve=1")
591 777 : fLabel = false;
592 858 : if (vstr[nStr].substr(0,6) == "label=") {
593 48 : strLabel = DecodeDumpString(vstr[nStr].substr(6));
594 48 : fLabel = true;
595 48 : }
596 : }
597 858 : keys.push_back(std::make_tuple(key, nTime, fLabel, strLabel));
598 1717 : } else if(IsHex(vstr[0])) {
599 859 : std::vector<unsigned char> vData(ParseHex(vstr[0]));
600 859 : CScript script = CScript(vData.begin(), vData.end());
601 859 : int64_t birth_time = ParseISO8601DateTime(vstr[1]);
602 859 : scripts.push_back(std::pair<CScript, int64_t>(script, birth_time));
603 859 : }
604 1770 : }
605 5 : file.close();
606 : // We now know whether we are importing private keys, so we can error if private keys are disabled
607 5 : if (keys.size() > 0 && pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
608 0 : pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
609 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Importing wallets is disabled when private keys are disabled");
610 : }
611 5 : double total = (double)(keys.size() + scripts.size());
612 1727 : double progress = 0;
613 863 : for (const auto& key_tuple : keys) {
614 858 : pwallet->chain().showProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
615 858 : const CKey& key = std::get<0>(key_tuple);
616 858 : int64_t time = std::get<1>(key_tuple);
617 858 : bool has_label = std::get<2>(key_tuple);
618 858 : std::string label = std::get<3>(key_tuple);
619 :
620 858 : CPubKey pubkey = key.GetPubKey();
621 858 : CHECK_NONFATAL(key.VerifyPubKey(pubkey));
622 858 : CKeyID keyid = pubkey.GetID();
623 :
624 858 : pwallet->WalletLogPrintf("Importing %s...\n", EncodeDestination(PKHash(keyid)));
625 :
626 858 : if (!pwallet->ImportPrivKeys({{keyid, key}}, time)) {
627 0 : pwallet->WalletLogPrintf("Error importing key for %s\n", EncodeDestination(PKHash(keyid)));
628 : fGood = false;
629 0 : continue;
630 : }
631 :
632 858 : if (has_label)
633 52 : pwallet->SetAddressBook(PKHash(keyid), label, "receive");
634 :
635 858 : nTimeBegin = std::min(nTimeBegin, time);
636 858 : progress++;
637 858 : }
638 864 : for (const auto& script_pair : scripts) {
639 859 : pwallet->chain().showProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
640 859 : const CScript& script = script_pair.first;
641 859 : int64_t time = script_pair.second;
642 :
643 859 : if (!pwallet->ImportScripts({script}, time)) {
644 0 : pwallet->WalletLogPrintf("Error importing script %s\n", HexStr(script));
645 : fGood = false;
646 0 : continue;
647 : }
648 859 : if (time > 0) {
649 0 : nTimeBegin = std::min(nTimeBegin, time);
650 0 : }
651 :
652 859 : progress++;
653 859 : }
654 5 : pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
655 5 : }
656 5 : pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
657 5 : RescanWallet(*pwallet, reserver, nTimeBegin, false /* update */);
658 5 : pwallet->MarkDirty();
659 :
660 5 : if (!fGood)
661 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys/scripts to wallet");
662 :
663 5 : return NullUniValue;
664 6 : },
665 : };
666 0 : }
667 :
668 2338 : RPCHelpMan dumpprivkey()
669 : {
670 4676 : return RPCHelpMan{"dumpprivkey",
671 2338 : "\nReveals the private key corresponding to 'address'.\n"
672 : "Then the importprivkey can be used with this output\n",
673 4676 : {
674 2338 : {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address for the private key"},
675 : },
676 2338 : RPCResult{
677 2338 : RPCResult::Type::STR, "key", "The private key"
678 : },
679 2338 : RPCExamples{
680 2338 : HelpExampleCli("dumpprivkey", "\"myaddress\"")
681 2338 : + HelpExampleCli("importprivkey", "\"mykey\"")
682 2338 : + HelpExampleRpc("dumpprivkey", "\"myaddress\"")
683 : },
684 2572 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
685 : {
686 234 : std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
687 234 : if (!wallet) return NullUniValue;
688 234 : const CWallet* const pwallet = wallet.get();
689 :
690 234 : LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*wallet);
691 :
692 233 : LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
693 :
694 233 : EnsureWalletIsUnlocked(pwallet);
695 :
696 233 : std::string strAddress = request.params[0].get_str();
697 233 : CTxDestination dest = DecodeDestination(strAddress);
698 233 : if (!IsValidDestination(dest)) {
699 1 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
700 : }
701 232 : auto keyid = GetKeyForDestination(spk_man, dest);
702 232 : if (keyid.IsNull()) {
703 1 : throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
704 : }
705 231 : CKey vchSecret;
706 231 : if (!spk_man.GetKey(keyid, vchSecret)) {
707 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
708 : }
709 231 : return EncodeSecret(vchSecret);
710 234 : },
711 : };
712 0 : }
713 :
714 :
715 2112 : RPCHelpMan dumpwallet()
716 : {
717 4224 : return RPCHelpMan{"dumpwallet",
718 2112 : "\nDumps all wallet keys in a human-readable format to a server-side file. This does not allow overwriting existing files.\n"
719 : "Imported scripts are included in the dumpfile, but corresponding BIP173 addresses, etc. may not be added automatically by importwallet.\n"
720 : "Note that if your wallet contains keys which are not derived from your HD seed (e.g. imported keys), these are not covered by\n"
721 : "only backing up the seed itself, and must be backed up too (e.g. ensure you back up the whole dumpfile).\n",
722 4224 : {
723 2112 : {"filename", RPCArg::Type::STR, RPCArg::Optional::NO, "The filename with path (absolute path recommended)"},
724 : },
725 2112 : RPCResult{
726 2112 : RPCResult::Type::OBJ, "", "",
727 4224 : {
728 2112 : {RPCResult::Type::STR, "filename", "The filename with full absolute path"},
729 : }
730 : },
731 2112 : RPCExamples{
732 2112 : HelpExampleCli("dumpwallet", "\"test\"")
733 2112 : + HelpExampleRpc("dumpwallet", "\"test\"")
734 : },
735 2120 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
736 : {
737 8 : std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
738 8 : if (!pwallet) return NullUniValue;
739 :
740 8 : CWallet& wallet = *pwallet;
741 8 : LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(wallet);
742 :
743 : // Make sure the results are valid at least up to the most recent block
744 : // the user could have gotten from another RPC command prior to now
745 7 : wallet.BlockUntilSyncedToCurrentChain();
746 :
747 7 : LOCK2(wallet.cs_wallet, spk_man.cs_KeyStore);
748 :
749 7 : EnsureWalletIsUnlocked(&wallet);
750 :
751 7 : fs::path filepath = request.params[0].get_str();
752 7 : filepath = fs::absolute(filepath);
753 :
754 : /* Prevent arbitrary files from being overwritten. There have been reports
755 : * that users have overwritten wallet files this way:
756 : * https://github.com/bitcoin/bitcoin/issues/9934
757 : * It may also avoid other security issues.
758 : */
759 7 : if (fs::exists(filepath)) {
760 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, filepath.string() + " already exists. If you are sure this is what you want, move it out of the way first");
761 : }
762 :
763 6 : fsbridge::ofstream file;
764 6 : file.open(filepath);
765 6 : if (!file.is_open())
766 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
767 :
768 6 : std::map<CKeyID, int64_t> mapKeyBirth;
769 6 : const std::map<CKeyID, int64_t>& mapKeyPool = spk_man.GetAllReserveKeys();
770 6 : wallet.GetKeyBirthTimes(mapKeyBirth);
771 :
772 6 : std::set<CScriptID> scripts = spk_man.GetCScripts();
773 :
774 : // sort time/key pairs
775 6 : std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
776 1256 : for (const auto& entry : mapKeyBirth) {
777 1250 : vKeyBirth.push_back(std::make_pair(entry.second, entry.first));
778 0 : }
779 6 : mapKeyBirth.clear();
780 6 : std::sort(vKeyBirth.begin(), vKeyBirth.end());
781 :
782 : // produce output
783 6 : file << strprintf("# Wallet dump created by Bitcoin %s\n", CLIENT_BUILD);
784 6 : file << strprintf("# * Created on %s\n", FormatISO8601DateTime(GetTime()));
785 6 : file << strprintf("# * Best block at time of backup was %i (%s),\n", wallet.GetLastBlockHeight(), wallet.GetLastBlockHash().ToString());
786 6 : int64_t block_time = 0;
787 6 : CHECK_NONFATAL(wallet.chain().findBlock(wallet.GetLastBlockHash(), FoundBlock().time(block_time)));
788 6 : file << strprintf("# mined on %s\n", FormatISO8601DateTime(block_time));
789 6 : file << "\n";
790 :
791 : // add the base58check encoded extended master if the wallet uses HD
792 6 : CKeyID seed_id = spk_man.GetHDChain().seed_id;
793 6 : if (!seed_id.IsNull())
794 : {
795 5 : CKey seed;
796 5 : if (spk_man.GetKey(seed_id, seed)) {
797 5 : CExtKey masterKey;
798 5 : masterKey.SetSeed(seed.begin(), seed.size());
799 :
800 5 : file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n\n";
801 5 : }
802 5 : }
803 1256 : for (std::vector<std::pair<int64_t, CKeyID> >::const_iterator it = vKeyBirth.begin(); it != vKeyBirth.end(); it++) {
804 1250 : const CKeyID &keyid = it->second;
805 1250 : std::string strTime = FormatISO8601DateTime(it->first);
806 1250 : std::string strAddr;
807 1250 : std::string strLabel;
808 1250 : CKey key;
809 1250 : if (spk_man.GetKey(keyid, key)) {
810 1250 : file << strprintf("%s %s ", EncodeSecret(key), strTime);
811 1250 : if (GetWalletAddressesForKey(&spk_man, &wallet, keyid, strAddr, strLabel)) {
812 78 : file << strprintf("label=%s", strLabel);
813 1250 : } else if (keyid == seed_id) {
814 5 : file << "hdseed=1";
815 1167 : } else if (mapKeyPool.count(keyid)) {
816 957 : file << "reserve=1";
817 210 : } else if (spk_man.mapKeyMetadata[keyid].hdKeypath == "s") {
818 1 : file << "inactivehdseed=1";
819 : } else {
820 209 : file << "change=1";
821 : }
822 1250 : file << strprintf(" # addr=%s%s\n", strAddr, (spk_man.mapKeyMetadata[keyid].has_key_origin ? " hdkeypath="+WriteHDKeypath(spk_man.mapKeyMetadata[keyid].key_origin.path) : ""));
823 1250 : }
824 1250 : }
825 6 : file << "\n";
826 1258 : for (const CScriptID &scriptid : scripts) {
827 1252 : CScript script;
828 1252 : std::string create_time = "0";
829 1252 : std::string address = EncodeDestination(ScriptHash(scriptid));
830 : // get birth times for scripts with metadata
831 1252 : auto it = spk_man.m_script_metadata.find(scriptid);
832 1252 : if (it != spk_man.m_script_metadata.end()) {
833 0 : create_time = FormatISO8601DateTime(it->second.nCreateTime);
834 0 : }
835 1252 : if(spk_man.GetCScript(scriptid, script)) {
836 1252 : file << strprintf("%s %s script=1", HexStr(script), create_time);
837 1252 : file << strprintf(" # addr=%s\n", address);
838 1252 : }
839 1252 : }
840 6 : file << "\n";
841 6 : file << "# End of dump\n";
842 6 : file.close();
843 :
844 6 : UniValue reply(UniValue::VOBJ);
845 6 : reply.pushKV("filename", filepath.string());
846 :
847 6 : return reply;
848 8 : },
849 : };
850 0 : }
851 :
852 752 : struct ImportData
853 : {
854 : // Input data
855 : std::unique_ptr<CScript> redeemscript; //!< Provided redeemScript; will be moved to `import_scripts` if relevant.
856 : std::unique_ptr<CScript> witnessscript; //!< Provided witnessScript; will be moved to `import_scripts` if relevant.
857 :
858 : // Output data
859 : std::set<CScript> import_scripts;
860 : std::map<CKeyID, bool> used_keys; //!< Import these private keys if available (the value indicates whether if the key is required for solvability)
861 : std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> key_origins;
862 : };
863 :
864 : enum class ScriptContext
865 : {
866 : TOP, //!< Top-level scriptPubKey
867 : P2SH, //!< P2SH redeemScript
868 : WITNESS_V0, //!< P2WSH witnessScript
869 : };
870 :
871 : // Analyse the provided scriptPubKey, determining which keys and which redeem scripts from the ImportData struct are needed to spend it, and mark them as used.
872 : // Returns an error string, or the empty string for success.
873 126 : static std::string RecurseImportData(const CScript& script, ImportData& import_data, const ScriptContext script_ctx)
874 : {
875 : // Use Solver to obtain script type and parsed pubkeys or hashes:
876 126 : std::vector<std::vector<unsigned char>> solverdata;
877 126 : TxoutType script_type = Solver(script, solverdata);
878 :
879 126 : switch (script_type) {
880 : case TxoutType::PUBKEY: {
881 0 : CPubKey pubkey(solverdata[0].begin(), solverdata[0].end());
882 0 : import_data.used_keys.emplace(pubkey.GetID(), false);
883 0 : return "";
884 0 : }
885 : case TxoutType::PUBKEYHASH: {
886 37 : CKeyID id = CKeyID(uint160(solverdata[0]));
887 37 : import_data.used_keys[id] = true;
888 37 : return "";
889 37 : }
890 : case TxoutType::SCRIPTHASH: {
891 30 : if (script_ctx == ScriptContext::P2SH) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2SH inside another P2SH");
892 30 : if (script_ctx == ScriptContext::WITNESS_V0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2SH inside a P2WSH");
893 30 : CHECK_NONFATAL(script_ctx == ScriptContext::TOP);
894 30 : CScriptID id = CScriptID(uint160(solverdata[0]));
895 30 : auto subscript = std::move(import_data.redeemscript); // Remove redeemscript from import_data to check for superfluous script later.
896 30 : if (!subscript) return "missing redeemscript";
897 30 : if (CScriptID(*subscript) != id) return "redeemScript does not match the scriptPubKey";
898 30 : import_data.import_scripts.emplace(*subscript);
899 30 : return RecurseImportData(*subscript, import_data, ScriptContext::P2SH);
900 30 : }
901 : case TxoutType::MULTISIG: {
902 20 : for (size_t i = 1; i + 1< solverdata.size(); ++i) {
903 15 : CPubKey pubkey(solverdata[i].begin(), solverdata[i].end());
904 15 : import_data.used_keys.emplace(pubkey.GetID(), false);
905 15 : }
906 5 : return "";
907 : }
908 : case TxoutType::WITNESS_V0_SCRIPTHASH: {
909 2 : if (script_ctx == ScriptContext::WITNESS_V0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2WSH inside another P2WSH");
910 2 : uint256 fullid(solverdata[0]);
911 2 : CScriptID id;
912 2 : CRIPEMD160().Write(fullid.begin(), fullid.size()).Finalize(id.begin());
913 2 : auto subscript = std::move(import_data.witnessscript); // Remove redeemscript from import_data to check for superfluous script later.
914 2 : if (!subscript) return "missing witnessscript";
915 2 : if (CScriptID(*subscript) != id) return "witnessScript does not match the scriptPubKey or redeemScript";
916 2 : if (script_ctx == ScriptContext::TOP) {
917 1 : import_data.import_scripts.emplace(script); // Special rule for IsMine: native P2WSH requires the TOP script imported (see script/ismine.cpp)
918 1 : }
919 2 : import_data.import_scripts.emplace(*subscript);
920 2 : return RecurseImportData(*subscript, import_data, ScriptContext::WITNESS_V0);
921 2 : }
922 : case TxoutType::WITNESS_V0_KEYHASH: {
923 52 : if (script_ctx == ScriptContext::WITNESS_V0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2WPKH inside P2WSH");
924 52 : CKeyID id = CKeyID(uint160(solverdata[0]));
925 52 : import_data.used_keys[id] = true;
926 52 : if (script_ctx == ScriptContext::TOP) {
927 26 : import_data.import_scripts.emplace(script); // Special rule for IsMine: native P2WPKH requires the TOP script imported (see script/ismine.cpp)
928 26 : }
929 52 : return "";
930 52 : }
931 : case TxoutType::NULL_DATA:
932 0 : return "unspendable script";
933 : case TxoutType::NONSTANDARD:
934 : case TxoutType::WITNESS_UNKNOWN:
935 : default:
936 0 : return "unrecognized script";
937 : }
938 126 : }
939 :
940 143 : static UniValue ProcessImportLegacy(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<CKeyID>& ordered_pubkeys)
941 : {
942 147 : UniValue warnings(UniValue::VARR);
943 :
944 : // First ensure scriptPubKey has either a script or JSON with "address" string
945 143 : const UniValue& scriptPubKey = data["scriptPubKey"];
946 143 : bool isScript = scriptPubKey.getType() == UniValue::VSTR;
947 143 : if (!isScript && !(scriptPubKey.getType() == UniValue::VOBJ && scriptPubKey.exists("address"))) {
948 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "scriptPubKey must be string with script or JSON with address string");
949 : }
950 143 : const std::string& output = isScript ? scriptPubKey.get_str() : scriptPubKey["address"].get_str();
951 :
952 : // Optional fields.
953 143 : const std::string& strRedeemScript = data.exists("redeemscript") ? data["redeemscript"].get_str() : "";
954 143 : const std::string& witness_script_hex = data.exists("witnessscript") ? data["witnessscript"].get_str() : "";
955 143 : const UniValue& pubKeys = data.exists("pubkeys") ? data["pubkeys"].get_array() : UniValue();
956 143 : const UniValue& keys = data.exists("keys") ? data["keys"].get_array() : UniValue();
957 143 : const bool internal = data.exists("internal") ? data["internal"].get_bool() : false;
958 143 : const bool watchOnly = data.exists("watchonly") ? data["watchonly"].get_bool() : false;
959 :
960 143 : if (data.exists("range")) {
961 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for a non-descriptor import");
962 : }
963 :
964 : // Generate the script and destination for the scriptPubKey provided
965 143 : CScript script;
966 143 : if (!isScript) {
967 79 : CTxDestination dest = DecodeDestination(output);
968 79 : if (!IsValidDestination(dest)) {
969 1 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address \"" + output + "\"");
970 : }
971 78 : script = GetScriptForDestination(dest);
972 79 : } else {
973 64 : if (!IsHex(output)) {
974 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid scriptPubKey \"" + output + "\"");
975 : }
976 64 : std::vector<unsigned char> vData(ParseHex(output));
977 64 : script = CScript(vData.begin(), vData.end());
978 64 : CTxDestination dest;
979 64 : if (!ExtractDestination(script, dest) && !internal) {
980 3 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Internal must be set to true for nonstandard scriptPubKey imports.");
981 : }
982 64 : }
983 139 : script_pub_keys.emplace(script);
984 :
985 : // Parse all arguments
986 139 : if (strRedeemScript.size()) {
987 30 : if (!IsHex(strRedeemScript)) {
988 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid redeem script \"" + strRedeemScript + "\": must be hex string");
989 : }
990 30 : auto parsed_redeemscript = ParseHex(strRedeemScript);
991 30 : import_data.redeemscript = MakeUnique<CScript>(parsed_redeemscript.begin(), parsed_redeemscript.end());
992 30 : }
993 139 : if (witness_script_hex.size()) {
994 2 : if (!IsHex(witness_script_hex)) {
995 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid witness script \"" + witness_script_hex + "\": must be hex string");
996 : }
997 2 : auto parsed_witnessscript = ParseHex(witness_script_hex);
998 2 : import_data.witnessscript = MakeUnique<CScript>(parsed_witnessscript.begin(), parsed_witnessscript.end());
999 2 : }
1000 182 : for (size_t i = 0; i < pubKeys.size(); ++i) {
1001 43 : const auto& str = pubKeys[i].get_str();
1002 43 : if (!IsHex(str)) {
1003 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey \"" + str + "\" must be a hex string");
1004 : }
1005 43 : auto parsed_pubkey = ParseHex(str);
1006 43 : CPubKey pubkey(parsed_pubkey.begin(), parsed_pubkey.end());
1007 43 : if (!pubkey.IsFullyValid()) {
1008 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey \"" + str + "\" is not a valid public key");
1009 : }
1010 43 : pubkey_map.emplace(pubkey.GetID(), pubkey);
1011 43 : ordered_pubkeys.push_back(pubkey.GetID());
1012 43 : }
1013 192 : for (size_t i = 0; i < keys.size(); ++i) {
1014 53 : const auto& str = keys[i].get_str();
1015 53 : CKey key = DecodeSecret(str);
1016 53 : if (!key.IsValid()) {
1017 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
1018 : }
1019 53 : CPubKey pubkey = key.GetPubKey();
1020 53 : CKeyID id = pubkey.GetID();
1021 53 : if (pubkey_map.count(id)) {
1022 0 : pubkey_map.erase(id);
1023 : }
1024 53 : privkey_map.emplace(id, key);
1025 53 : }
1026 :
1027 :
1028 : // Verify and process input data
1029 139 : have_solving_data = import_data.redeemscript || import_data.witnessscript || pubkey_map.size() || privkey_map.size();
1030 139 : if (have_solving_data) {
1031 : // Match up data in import_data with the scriptPubKey in script.
1032 94 : auto error = RecurseImportData(script, import_data, ScriptContext::TOP);
1033 :
1034 : // Verify whether the watchonly option corresponds to the availability of private keys.
1035 192 : bool spendable = std::all_of(import_data.used_keys.begin(), import_data.used_keys.end(), [&](const std::pair<CKeyID, bool>& used_key){ return privkey_map.count(used_key.first) > 0; });
1036 94 : if (!watchOnly && !spendable) {
1037 12 : warnings.push_back("Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag.");
1038 : }
1039 94 : if (watchOnly && spendable) {
1040 1 : warnings.push_back("All private keys are provided, outputs will be considered spendable. If this is intentional, do not specify the watchonly flag.");
1041 : }
1042 :
1043 : // Check that all required keys for solvability are provided.
1044 94 : if (error.empty()) {
1045 198 : for (const auto& require_key : import_data.used_keys) {
1046 104 : if (!require_key.second) continue; // Not a required key
1047 89 : if (pubkey_map.count(require_key.first) == 0 && privkey_map.count(require_key.first) == 0) {
1048 4 : error = "some required keys are missing";
1049 : }
1050 89 : }
1051 94 : }
1052 :
1053 94 : if (!error.empty()) {
1054 4 : warnings.push_back("Importing as non-solvable: " + error + ". If this is intentional, don't provide any keys, pubkeys, witnessscript, or redeemscript.");
1055 4 : import_data = ImportData();
1056 4 : pubkey_map.clear();
1057 4 : privkey_map.clear();
1058 4 : have_solving_data = false;
1059 4 : } else {
1060 : // RecurseImportData() removes any relevant redeemscript/witnessscript from import_data, so we can use that to discover if a superfluous one was provided.
1061 90 : if (import_data.redeemscript) warnings.push_back("Ignoring redeemscript as this is not a P2SH script.");
1062 90 : if (import_data.witnessscript) warnings.push_back("Ignoring witnessscript as this is not a (P2SH-)P2WSH script.");
1063 141 : for (auto it = privkey_map.begin(); it != privkey_map.end(); ) {
1064 51 : auto oldit = it++;
1065 51 : if (import_data.used_keys.count(oldit->first) == 0) {
1066 0 : warnings.push_back("Ignoring irrelevant private key.");
1067 0 : privkey_map.erase(oldit);
1068 0 : }
1069 51 : }
1070 131 : for (auto it = pubkey_map.begin(); it != pubkey_map.end(); ) {
1071 41 : auto oldit = it++;
1072 41 : auto key_data_it = import_data.used_keys.find(oldit->first);
1073 41 : if (key_data_it == import_data.used_keys.end() || !key_data_it->second) {
1074 0 : warnings.push_back("Ignoring public key \"" + HexStr(oldit->first) + "\" as it doesn't appear inside P2PKH or P2WPKH.");
1075 0 : pubkey_map.erase(oldit);
1076 0 : }
1077 41 : }
1078 : }
1079 94 : }
1080 :
1081 : return warnings;
1082 147 : }
1083 :
1084 38 : static UniValue ProcessImportDescriptor(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<CKeyID>& ordered_pubkeys)
1085 : {
1086 45 : UniValue warnings(UniValue::VARR);
1087 :
1088 38 : const std::string& descriptor = data["desc"].get_str();
1089 38 : FlatSigningProvider keys;
1090 38 : std::string error;
1091 38 : auto parsed_desc = Parse(descriptor, keys, error, /* require_checksum = */ true);
1092 38 : if (!parsed_desc) {
1093 1 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
1094 : }
1095 :
1096 37 : have_solving_data = parsed_desc->IsSolvable();
1097 37 : const bool watch_only = data.exists("watchonly") ? data["watchonly"].get_bool() : false;
1098 :
1099 37 : int64_t range_start = 0, range_end = 0;
1100 37 : if (!parsed_desc->IsRange() && data.exists("range")) {
1101 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for an un-ranged descriptor");
1102 37 : } else if (parsed_desc->IsRange()) {
1103 12 : if (!data.exists("range")) {
1104 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor is ranged, please specify the range");
1105 : }
1106 11 : std::tie(range_start, range_end) = ParseDescriptorRange(data["range"]);
1107 6 : }
1108 :
1109 31 : const UniValue& priv_keys = data.exists("keys") ? data["keys"].get_array() : UniValue();
1110 :
1111 : // Expand all descriptors to get public keys and scripts, and private keys if available.
1112 88 : for (int i = range_start; i <= range_end; ++i) {
1113 57 : FlatSigningProvider out_keys;
1114 57 : std::vector<CScript> scripts_temp;
1115 57 : parsed_desc->Expand(i, keys, scripts_temp, out_keys);
1116 57 : std::copy(scripts_temp.begin(), scripts_temp.end(), std::inserter(script_pub_keys, script_pub_keys.end()));
1117 112 : for (const auto& key_pair : out_keys.pubkeys) {
1118 55 : ordered_pubkeys.push_back(key_pair.first);
1119 0 : }
1120 :
1121 62 : for (const auto& x : out_keys.scripts) {
1122 5 : import_data.import_scripts.emplace(x.second);
1123 0 : }
1124 :
1125 57 : parsed_desc->ExpandPrivate(i, keys, out_keys);
1126 :
1127 57 : std::copy(out_keys.pubkeys.begin(), out_keys.pubkeys.end(), std::inserter(pubkey_map, pubkey_map.end()));
1128 57 : std::copy(out_keys.keys.begin(), out_keys.keys.end(), std::inserter(privkey_map, privkey_map.end()));
1129 57 : import_data.key_origins.insert(out_keys.origins.begin(), out_keys.origins.end());
1130 57 : }
1131 :
1132 32 : for (size_t i = 0; i < priv_keys.size(); ++i) {
1133 1 : const auto& str = priv_keys[i].get_str();
1134 1 : CKey key = DecodeSecret(str);
1135 1 : if (!key.IsValid()) {
1136 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
1137 : }
1138 1 : CPubKey pubkey = key.GetPubKey();
1139 1 : CKeyID id = pubkey.GetID();
1140 :
1141 : // Check if this private key corresponds to a public key from the descriptor
1142 1 : if (!pubkey_map.count(id)) {
1143 0 : warnings.push_back("Ignoring irrelevant private key.");
1144 : } else {
1145 1 : privkey_map.emplace(id, key);
1146 : }
1147 1 : }
1148 :
1149 : // Check if all the public keys have corresponding private keys in the import for spendability.
1150 : // This does not take into account threshold multisigs which could be spendable without all keys.
1151 : // Thus, threshold multisigs without all keys will be considered not spendable here, even if they are,
1152 : // perhaps triggering a false warning message. This is consistent with the current wallet IsMine check.
1153 62 : bool spendable = std::all_of(pubkey_map.begin(), pubkey_map.end(),
1154 31 : [&](const std::pair<CKeyID, CPubKey>& used_key) {
1155 31 : return privkey_map.count(used_key.first) > 0;
1156 31 : }) && std::all_of(import_data.key_origins.begin(), import_data.key_origins.end(),
1157 10 : [&](const std::pair<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& entry) {
1158 10 : return privkey_map.count(entry.first) > 0;
1159 : });
1160 31 : if (!watch_only && !spendable) {
1161 14 : warnings.push_back("Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag.");
1162 : }
1163 31 : if (watch_only && spendable) {
1164 0 : warnings.push_back("All private keys are provided, outputs will be considered spendable. If this is intentional, do not specify the watchonly flag.");
1165 : }
1166 :
1167 : return warnings;
1168 38 : }
1169 :
1170 185 : static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
1171 : {
1172 185 : UniValue warnings(UniValue::VARR);
1173 185 : UniValue result(UniValue::VOBJ);
1174 :
1175 : try {
1176 185 : const bool internal = data.exists("internal") ? data["internal"].get_bool() : false;
1177 : // Internal addresses should not have a label
1178 185 : if (internal && data.exists("label")) {
1179 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Internal addresses should not have a label");
1180 : }
1181 184 : const std::string& label = data.exists("label") ? data["label"].get_str() : "";
1182 184 : const bool add_keypool = data.exists("keypool") ? data["keypool"].get_bool() : false;
1183 :
1184 : // Add to keypool only works with privkeys disabled
1185 184 : if (add_keypool && !pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
1186 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Keys can only be imported to the keypool when private keys are disabled");
1187 : }
1188 :
1189 183 : ImportData import_data;
1190 183 : std::map<CKeyID, CPubKey> pubkey_map;
1191 183 : std::map<CKeyID, CKey> privkey_map;
1192 183 : std::set<CScript> script_pub_keys;
1193 183 : std::vector<CKeyID> ordered_pubkeys;
1194 183 : bool have_solving_data;
1195 :
1196 183 : if (data.exists("scriptPubKey") && data.exists("desc")) {
1197 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Both a descriptor and a scriptPubKey should not be provided.");
1198 182 : } else if (data.exists("scriptPubKey")) {
1199 143 : warnings = ProcessImportLegacy(import_data, pubkey_map, privkey_map, script_pub_keys, have_solving_data, data, ordered_pubkeys);
1200 178 : } else if (data.exists("desc")) {
1201 38 : warnings = ProcessImportDescriptor(import_data, pubkey_map, privkey_map, script_pub_keys, have_solving_data, data, ordered_pubkeys);
1202 : } else {
1203 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Either a descriptor or scriptPubKey must be provided.");
1204 : }
1205 :
1206 : // If private keys are disabled, abort if private keys are being imported
1207 170 : if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !privkey_map.empty()) {
1208 2 : throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
1209 : }
1210 :
1211 : // Check whether we have any work to do
1212 362 : for (const CScript& script : script_pub_keys) {
1213 194 : if (pwallet->IsMine(script) & ISMINE_SPENDABLE) {
1214 1 : throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script (\"" + HexStr(script) + "\")");
1215 : }
1216 : }
1217 :
1218 : // All good, time to import
1219 167 : pwallet->MarkDirty();
1220 167 : if (!pwallet->ImportScripts(import_data.import_scripts, timestamp)) {
1221 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Error adding script to wallet");
1222 : }
1223 167 : if (!pwallet->ImportPrivKeys(privkey_map, timestamp)) {
1224 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
1225 : }
1226 167 : if (!pwallet->ImportPubKeys(ordered_pubkeys, pubkey_map, import_data.key_origins, add_keypool, internal, timestamp)) {
1227 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
1228 : }
1229 167 : if (!pwallet->ImportScriptPubKeys(label, script_pub_keys, have_solving_data, !internal, timestamp)) {
1230 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
1231 : }
1232 :
1233 167 : result.pushKV("success", UniValue(true));
1234 185 : } catch (const UniValue& e) {
1235 18 : result.pushKV("success", UniValue(false));
1236 18 : result.pushKV("error", e);
1237 18 : } catch (...) {
1238 0 : result.pushKV("success", UniValue(false));
1239 :
1240 0 : result.pushKV("error", JSONRPCError(RPC_MISC_ERROR, "Missing required fields"));
1241 18 : }
1242 185 : if (warnings.size()) result.pushKV("warnings", warnings);
1243 : return result;
1244 203 : }
1245 :
1246 481 : static int64_t GetImportTimestamp(const UniValue& data, int64_t now)
1247 : {
1248 483 : if (data.exists("timestamp")) {
1249 480 : const UniValue& timestamp = data["timestamp"];
1250 480 : if (timestamp.isNum()) {
1251 272 : return timestamp.get_int64();
1252 208 : } else if (timestamp.isStr() && timestamp.get_str() == "now") {
1253 207 : return now;
1254 : }
1255 1 : throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Expected number or \"now\" timestamp value for key. got type %s", uvTypeName(timestamp.type())));
1256 1 : }
1257 1 : throw JSONRPCError(RPC_TYPE_ERROR, "Missing required timestamp field for key");
1258 481 : }
1259 :
1260 2282 : RPCHelpMan importmulti()
1261 : {
1262 41076 : return RPCHelpMan{"importmulti",
1263 2282 : "\nImport addresses/scripts (with private or public keys, redeem script (P2SH)), optionally rescanning the blockchain from the earliest creation time of the imported scripts. Requires a new wallet backup.\n"
1264 : "If an address/script is imported without all of the private keys required to spend from that address, it will be watchonly. The 'watchonly' option must be set to true in this case or a warning will be returned.\n"
1265 : "Conversely, if all the private keys are provided and the address/script is spendable, the watchonly option must be set to false, or a warning will be returned.\n"
1266 : "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n"
1267 : "may report that the imported keys, addresses or scripts exist but related transactions are still missing.\n"
1268 : "Note: Use \"getwalletinfo\" to query the scanning progress.\n",
1269 6846 : {
1270 4564 : {"requests", RPCArg::Type::ARR, RPCArg::Optional::NO, "Data to be imported",
1271 4564 : {
1272 4564 : {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
1273 29666 : {
1274 2282 : {"desc", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Descriptor to import. If using descriptor, do not also provide address/scriptPubKey, scripts, or pubkeys"},
1275 4564 : {"scriptPubKey", RPCArg::Type::STR, RPCArg::Optional::NO, "Type of scriptPubKey (string for script, json for address). Should not be provided if using a descriptor",
1276 2282 : /* oneline_description */ "", {"\"<script>\" | { \"address\":\"<address>\" }", "string / json"}
1277 : },
1278 4564 : {"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, "Creation time of the key expressed in " + UNIX_EPOCH_TIME + ",\n"
1279 : " or the string \"now\" to substitute the current synced blockchain time. The timestamp of the oldest\n"
1280 : " key will determine how far back blockchain rescans need to begin for missing wallet transactions.\n"
1281 : " \"now\" can be specified to bypass scanning, for keys which are known to never have been used, and\n"
1282 : " 0 can be specified to scan the entire blockchain. Blocks up to 2 hours before the earliest key\n"
1283 : " creation time of all keys being imported by the importmulti call will be scanned.",
1284 2282 : /* oneline_description */ "", {"timestamp | \"now\"", "integer / string"}
1285 : },
1286 2282 : {"redeemscript", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Allowed only if the scriptPubKey is a P2SH or P2SH-P2WSH address/scriptPubKey"},
1287 2282 : {"witnessscript", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Allowed only if the scriptPubKey is a P2SH-P2WSH or P2WSH address/scriptPubKey"},
1288 4564 : {"pubkeys", RPCArg::Type::ARR, /* default */ "empty array", "Array of strings giving pubkeys to import. They must occur in P2PKH or P2WPKH scripts. They are not required when the private key is also provided (see the \"keys\" argument).",
1289 4564 : {
1290 2282 : {"pubKey", RPCArg::Type::STR, RPCArg::Optional::OMITTED, ""},
1291 : }
1292 : },
1293 4564 : {"keys", RPCArg::Type::ARR, /* default */ "empty array", "Array of strings giving private keys to import. The corresponding public keys must occur in the output or redeemscript.",
1294 4564 : {
1295 2282 : {"key", RPCArg::Type::STR, RPCArg::Optional::OMITTED, ""},
1296 : }
1297 : },
1298 2282 : {"range", RPCArg::Type::RANGE, RPCArg::Optional::OMITTED, "If a ranged descriptor is used, this specifies the end or the range (in the form [begin,end]) to import"},
1299 2282 : {"internal", RPCArg::Type::BOOL, /* default */ "false", "Stating whether matching outputs should be treated as not incoming payments (also known as change)"},
1300 2282 : {"watchonly", RPCArg::Type::BOOL, /* default */ "false", "Stating whether matching outputs should be considered watchonly."},
1301 2282 : {"label", RPCArg::Type::STR, /* default */ "''", "Label to assign to the address, only allowed with internal=false"},
1302 2282 : {"keypool", RPCArg::Type::BOOL, /* default */ "false", "Stating whether imported public keys should be added to the keypool for when users request new addresses. Only allowed when wallet private keys are disabled"},
1303 : },
1304 : },
1305 : },
1306 2282 : "\"requests\""},
1307 4564 : {"options", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED_NAMED_ARG, "",
1308 4564 : {
1309 2282 : {"rescan", RPCArg::Type::BOOL, /* default */ "true", "Stating if should rescan the blockchain after all imports"},
1310 : },
1311 2282 : "\"options\""},
1312 : },
1313 2282 : RPCResult{
1314 2282 : RPCResult::Type::ARR, "", "Response is an array with the same size as the input that has the execution result",
1315 4564 : {
1316 4564 : {RPCResult::Type::OBJ, "", "",
1317 9128 : {
1318 2282 : {RPCResult::Type::BOOL, "success", ""},
1319 4564 : {RPCResult::Type::ARR, "warnings", /* optional */ true, "",
1320 4564 : {
1321 2282 : {RPCResult::Type::STR, "", ""},
1322 : }},
1323 4564 : {RPCResult::Type::OBJ, "error", /* optional */ true, "",
1324 4564 : {
1325 2282 : {RPCResult::Type::ELISION, "", "JSONRPC error"},
1326 : }},
1327 : }},
1328 : }
1329 : },
1330 2282 : RPCExamples{
1331 2282 : HelpExampleCli("importmulti", "'[{ \"scriptPubKey\": { \"address\": \"<my address>\" }, \"timestamp\":1455191478 }, "
1332 2282 : "{ \"scriptPubKey\": { \"address\": \"<my 2nd address>\" }, \"label\": \"example 2\", \"timestamp\": 1455191480 }]'") +
1333 2282 : HelpExampleCli("importmulti", "'[{ \"scriptPubKey\": { \"address\": \"<my address>\" }, \"timestamp\":1455191478 }]' '{ \"rescan\": false}'")
1334 : },
1335 2460 : [&](const RPCHelpMan& self, const JSONRPCRequest& mainRequest) -> UniValue
1336 : {
1337 178 : std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(mainRequest);
1338 178 : if (!wallet) return NullUniValue;
1339 178 : CWallet* const pwallet = wallet.get();
1340 :
1341 178 : RPCTypeCheck(mainRequest.params, {UniValue::VARR, UniValue::VOBJ});
1342 :
1343 178 : EnsureLegacyScriptPubKeyMan(*wallet, true);
1344 :
1345 177 : const UniValue& requests = mainRequest.params[0];
1346 :
1347 : //Default options
1348 : bool fRescan = true;
1349 :
1350 177 : if (!mainRequest.params[1].isNull()) {
1351 108 : const UniValue& options = mainRequest.params[1];
1352 :
1353 108 : if (options.exists("rescan")) {
1354 108 : fRescan = options["rescan"].get_bool();
1355 108 : }
1356 108 : }
1357 :
1358 177 : WalletRescanReserver reserver(*pwallet);
1359 177 : if (fRescan && !reserver.reserve()) {
1360 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
1361 : }
1362 :
1363 177 : int64_t now = 0;
1364 : bool fRunScan = false;
1365 177 : int64_t nLowestTimestamp = 0;
1366 177 : UniValue response(UniValue::VARR);
1367 : {
1368 177 : LOCK(pwallet->cs_wallet);
1369 177 : EnsureWalletIsUnlocked(pwallet);
1370 :
1371 : // Verify all timestamps are present before importing any keys.
1372 177 : CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(nLowestTimestamp).mtpTime(now)));
1373 362 : for (const UniValue& data : requests.getValues()) {
1374 187 : GetImportTimestamp(data, now);
1375 : }
1376 :
1377 175 : const int64_t minimumTimestamp = 1;
1378 :
1379 360 : for (const UniValue& data : requests.getValues()) {
1380 185 : const int64_t timestamp = std::max(GetImportTimestamp(data, now), minimumTimestamp);
1381 185 : const UniValue result = ProcessImport(pwallet, data, timestamp);
1382 185 : response.push_back(result);
1383 :
1384 185 : if (!fRescan) {
1385 36 : continue;
1386 : }
1387 :
1388 : // If at least one request was successful then allow rescan.
1389 149 : if (result["success"].get_bool()) {
1390 : fRunScan = true;
1391 131 : }
1392 :
1393 : // Get the lowest timestamp.
1394 149 : if (timestamp < nLowestTimestamp) {
1395 122 : nLowestTimestamp = timestamp;
1396 122 : }
1397 185 : }
1398 177 : }
1399 175 : if (fRescan && fRunScan && requests.size()) {
1400 121 : int64_t scannedTime = pwallet->RescanFromTime(nLowestTimestamp, reserver, true /* update */);
1401 : {
1402 121 : LOCK(pwallet->cs_wallet);
1403 121 : pwallet->ReacceptWalletTransactions();
1404 121 : }
1405 :
1406 121 : if (pwallet->IsAbortingRescan()) {
1407 0 : throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");
1408 : }
1409 121 : if (scannedTime > nLowestTimestamp) {
1410 1 : std::vector<UniValue> results = response.getValues();
1411 1 : response.clear();
1412 1 : response.setArray();
1413 : size_t i = 0;
1414 3 : for (const UniValue& request : requests.getValues()) {
1415 : // If key creation date is within the successfully scanned
1416 : // range, or if the import result already has an error set, let
1417 : // the result stand unmodified. Otherwise replace the result
1418 : // with an error message.
1419 2 : if (scannedTime <= GetImportTimestamp(request, now) || results.at(i).exists("error")) {
1420 1 : response.push_back(results.at(i));
1421 : } else {
1422 1 : UniValue result = UniValue(UniValue::VOBJ);
1423 1 : result.pushKV("success", UniValue(false));
1424 1 : result.pushKV(
1425 1 : "error",
1426 2 : JSONRPCError(
1427 : RPC_MISC_ERROR,
1428 1 : strprintf("Rescan failed for key with creation timestamp %d. There was an error reading a "
1429 : "block from time %d, which is after or within %d seconds of key creation, and "
1430 : "could contain transactions pertaining to the key. As a result, transactions "
1431 : "and coins using this key may not appear in the wallet. This error could be "
1432 : "caused by pruning or data corruption (see bitcoind log for details) and could "
1433 : "be dealt with by downloading and rescanning the relevant blocks (see -reindex "
1434 : "and -rescan options).",
1435 1 : GetImportTimestamp(request, now), scannedTime - TIMESTAMP_WINDOW - 1, TIMESTAMP_WINDOW)));
1436 1 : response.push_back(std::move(result));
1437 1 : }
1438 2 : ++i;
1439 : }
1440 1 : }
1441 121 : }
1442 :
1443 175 : return response;
1444 180 : },
1445 : };
1446 0 : }
1447 :
1448 106 : static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
1449 : {
1450 106 : UniValue warnings(UniValue::VARR);
1451 106 : UniValue result(UniValue::VOBJ);
1452 :
1453 : try {
1454 106 : if (!data.exists("desc")) {
1455 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor not found.");
1456 : }
1457 :
1458 105 : const std::string& descriptor = data["desc"].get_str();
1459 105 : const bool active = data.exists("active") ? data["active"].get_bool() : false;
1460 105 : const bool internal = data.exists("internal") ? data["internal"].get_bool() : false;
1461 105 : const std::string& label = data.exists("label") ? data["label"].get_str() : "";
1462 :
1463 : // Parse descriptor string
1464 105 : FlatSigningProvider keys;
1465 105 : std::string error;
1466 105 : auto parsed_desc = Parse(descriptor, keys, error, /* require_checksum = */ true);
1467 105 : if (!parsed_desc) {
1468 1 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
1469 : }
1470 :
1471 : // Range check
1472 : int64_t range_start = 0, range_end = 1, next_index = 0;
1473 104 : if (!parsed_desc->IsRange() && data.exists("range")) {
1474 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for an un-ranged descriptor");
1475 103 : } else if (parsed_desc->IsRange()) {
1476 30 : if (data.exists("range")) {
1477 29 : auto range = ParseDescriptorRange(data["range"]);
1478 : range_start = range.first;
1479 24 : range_end = range.second + 1; // Specified range end is inclusive, but we need range end as exclusive
1480 29 : } else {
1481 1 : warnings.push_back("Range not given, using default keypool range");
1482 : range_start = 0;
1483 1 : range_end = gArgs.GetArg("-keypool", DEFAULT_KEYPOOL_SIZE);
1484 : }
1485 : next_index = range_start;
1486 :
1487 25 : if (data.exists("next_index")) {
1488 8 : next_index = data["next_index"].get_int64();
1489 : // bound checks
1490 8 : if (next_index < range_start || next_index >= range_end) {
1491 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "next_index is out of range");
1492 : }
1493 : }
1494 : }
1495 :
1496 : // Active descriptors must be ranged
1497 98 : if (active && !parsed_desc->IsRange()) {
1498 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Active descriptors must be ranged");
1499 : }
1500 :
1501 : // Ranged descriptors should not have a label
1502 97 : if (data.exists("range") && data.exists("label")) {
1503 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Ranged descriptors should not have a label");
1504 : }
1505 :
1506 : // Internal addresses should not have a label either
1507 96 : if (internal && data.exists("label")) {
1508 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Internal addresses should not have a label");
1509 : }
1510 :
1511 : // Combo descriptor check
1512 95 : if (active && !parsed_desc->IsSingleType()) {
1513 1 : throw JSONRPCError(RPC_WALLET_ERROR, "Combo descriptors cannot be set to active");
1514 : }
1515 :
1516 : // If the wallet disabled private keys, abort if private keys exist
1517 94 : if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !keys.keys.empty()) {
1518 1 : throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
1519 : }
1520 :
1521 : // Need to ExpandPrivate to check if private keys are available for all pubkeys
1522 93 : FlatSigningProvider expand_keys;
1523 93 : std::vector<CScript> scripts;
1524 93 : parsed_desc->Expand(0, keys, scripts, expand_keys);
1525 93 : parsed_desc->ExpandPrivate(0, keys, expand_keys);
1526 :
1527 : // Check if all private keys are provided
1528 235 : bool have_all_privkeys = !expand_keys.keys.empty();
1529 192 : for (const auto& entry : expand_keys.origins) {
1530 99 : const CKeyID& key_id = entry.first;
1531 99 : CKey key;
1532 99 : if (!expand_keys.GetKey(key_id, key)) {
1533 : have_all_privkeys = false;
1534 50 : break;
1535 : }
1536 99 : }
1537 :
1538 : // If private keys are enabled, check some things.
1539 93 : if (!pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
1540 48 : if (keys.keys.empty()) {
1541 1 : throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import descriptor without private keys to a wallet with private keys enabled");
1542 : }
1543 47 : if (!have_all_privkeys) {
1544 4 : warnings.push_back("Not all private keys provided. Some wallet functionality may return unexpected errors");
1545 : }
1546 : }
1547 :
1548 92 : WalletDescriptor w_desc(std::move(parsed_desc), timestamp, range_start, range_end, next_index);
1549 :
1550 : // Check if the wallet already contains the descriptor
1551 92 : auto existing_spk_manager = pwallet->GetDescriptorScriptPubKeyMan(w_desc);
1552 92 : if (existing_spk_manager) {
1553 13 : LOCK(existing_spk_manager->cs_desc_man);
1554 13 : if (range_start > existing_spk_manager->GetWalletDescriptor().range_start) {
1555 0 : throw JSONRPCError(RPC_INVALID_PARAMS, strprintf("range_start can only decrease; current range = [%d,%d]", existing_spk_manager->GetWalletDescriptor().range_start, existing_spk_manager->GetWalletDescriptor().range_end));
1556 : }
1557 13 : }
1558 :
1559 : // Add descriptor to the wallet
1560 92 : auto spk_manager = pwallet->AddWalletDescriptor(w_desc, keys, label);
1561 92 : if (spk_manager == nullptr) {
1562 0 : throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Could not add descriptor '%s'", descriptor));
1563 : }
1564 :
1565 : // Set descriptor as active if necessary
1566 92 : if (active) {
1567 19 : if (!w_desc.descriptor->GetOutputType()) {
1568 1 : warnings.push_back("Unknown output type, cannot set descriptor to active.");
1569 : } else {
1570 18 : pwallet->AddActiveScriptPubKeyMan(spk_manager->GetID(), *w_desc.descriptor->GetOutputType(), internal);
1571 : }
1572 : }
1573 :
1574 92 : result.pushKV("success", UniValue(true));
1575 106 : } catch (const UniValue& e) {
1576 14 : result.pushKV("success", UniValue(false));
1577 14 : result.pushKV("error", e);
1578 14 : } catch (...) {
1579 0 : result.pushKV("success", UniValue(false));
1580 :
1581 0 : result.pushKV("error", JSONRPCError(RPC_MISC_ERROR, "Missing required fields"));
1582 14 : }
1583 106 : if (warnings.size()) result.pushKV("warnings", warnings);
1584 : return result;
1585 120 : }
1586 :
1587 2203 : RPCHelpMan importdescriptors()
1588 : {
1589 24233 : return RPCHelpMan{"importdescriptors",
1590 2203 : "\nImport descriptors. This will trigger a rescan of the blockchain based on the earliest timestamp of all descriptors being imported. Requires a new wallet backup.\n"
1591 : "\nNote: This call can take over an hour to complete if using an early timestamp; during that time, other rpc calls\n"
1592 : "may report that the imported keys, addresses or scripts exist but related transactions are still missing.\n",
1593 4406 : {
1594 4406 : {"requests", RPCArg::Type::ARR, RPCArg::Optional::NO, "Data to be imported",
1595 4406 : {
1596 4406 : {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
1597 17624 : {
1598 2203 : {"desc", RPCArg::Type::STR, RPCArg::Optional::NO, "Descriptor to import."},
1599 2203 : {"active", RPCArg::Type::BOOL, /* default */ "false", "Set this descriptor to be the active descriptor for the corresponding output type/externality"},
1600 2203 : {"range", RPCArg::Type::RANGE, RPCArg::Optional::OMITTED, "If a ranged descriptor is used, this specifies the end or the range (in the form [begin,end]) to import"},
1601 2203 : {"next_index", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "If a ranged descriptor is set to active, this specifies the next index to generate addresses from"},
1602 4406 : {"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, "Time from which to start rescanning the blockchain for this descriptor, in " + UNIX_EPOCH_TIME + "\n"
1603 : " Use the string \"now\" to substitute the current synced blockchain time.\n"
1604 : " \"now\" can be specified to bypass scanning, for outputs which are known to never have been used, and\n"
1605 : " 0 can be specified to scan the entire blockchain. Blocks up to 2 hours before the earliest timestamp\n"
1606 : " of all descriptors being imported will be scanned.",
1607 2203 : /* oneline_description */ "", {"timestamp | \"now\"", "integer / string"}
1608 : },
1609 2203 : {"internal", RPCArg::Type::BOOL, /* default */ "false", "Whether matching outputs should be treated as not incoming payments (e.g. change)"},
1610 2203 : {"label", RPCArg::Type::STR, /* default */ "''", "Label to assign to the address, only allowed with internal=false"},
1611 : },
1612 : },
1613 : },
1614 2203 : "\"requests\""},
1615 : },
1616 2203 : RPCResult{
1617 2203 : RPCResult::Type::ARR, "", "Response is an array with the same size as the input that has the execution result",
1618 4406 : {
1619 4406 : {RPCResult::Type::OBJ, "", "",
1620 8812 : {
1621 2203 : {RPCResult::Type::BOOL, "success", ""},
1622 4406 : {RPCResult::Type::ARR, "warnings", /* optional */ true, "",
1623 4406 : {
1624 2203 : {RPCResult::Type::STR, "", ""},
1625 : }},
1626 4406 : {RPCResult::Type::OBJ, "error", /* optional */ true, "",
1627 4406 : {
1628 2203 : {RPCResult::Type::ELISION, "", "JSONRPC error"},
1629 : }},
1630 : }},
1631 : }
1632 : },
1633 2203 : RPCExamples{
1634 2203 : HelpExampleCli("importdescriptors", "'[{ \"desc\": \"<my descriptor>\", \"timestamp\":1455191478, \"internal\": true }, "
1635 2203 : "{ \"desc\": \"<my desccriptor 2>\", \"label\": \"example 2\", \"timestamp\": 1455191480 }]'") +
1636 2203 : HelpExampleCli("importdescriptors", "'[{ \"desc\": \"<my descriptor>\", \"timestamp\":1455191478, \"active\": true, \"range\": [0,100], \"label\": \"<my bech32 wallet>\" }]'")
1637 : },
1638 2302 : [&](const RPCHelpMan& self, const JSONRPCRequest& main_request) -> UniValue
1639 : {
1640 99 : std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(main_request);
1641 99 : if (!wallet) return NullUniValue;
1642 99 : CWallet* const pwallet = wallet.get();
1643 :
1644 : // Make sure wallet is a descriptor wallet
1645 99 : if (!pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
1646 0 : throw JSONRPCError(RPC_WALLET_ERROR, "importdescriptors is not available for non-descriptor wallets");
1647 : }
1648 :
1649 99 : RPCTypeCheck(main_request.params, {UniValue::VARR, UniValue::VOBJ});
1650 :
1651 99 : WalletRescanReserver reserver(*pwallet);
1652 99 : if (!reserver.reserve()) {
1653 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
1654 : }
1655 :
1656 99 : const UniValue& requests = main_request.params[0];
1657 99 : const int64_t minimum_timestamp = 1;
1658 99 : int64_t now = 0;
1659 99 : int64_t lowest_timestamp = 0;
1660 : bool rescan = false;
1661 99 : UniValue response(UniValue::VARR);
1662 : {
1663 99 : LOCK(pwallet->cs_wallet);
1664 99 : EnsureWalletIsUnlocked(pwallet);
1665 :
1666 99 : CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(lowest_timestamp).mtpTime(now)));
1667 :
1668 : // Get all timestamps and extract the lowest timestamp
1669 205 : for (const UniValue& request : requests.getValues()) {
1670 : // This throws an error if "timestamp" doesn't exist
1671 106 : const int64_t timestamp = std::max(GetImportTimestamp(request, now), minimum_timestamp);
1672 106 : const UniValue result = ProcessDescriptorImport(pwallet, request, timestamp);
1673 106 : response.push_back(result);
1674 :
1675 106 : if (lowest_timestamp > timestamp ) {
1676 61 : lowest_timestamp = timestamp;
1677 61 : }
1678 :
1679 : // If we know the chain tip, and at least one request was successful then allow rescan
1680 106 : if (!rescan && result["success"].get_bool()) {
1681 : rescan = true;
1682 85 : }
1683 106 : }
1684 99 : pwallet->ConnectScriptPubKeyManNotifiers();
1685 99 : }
1686 :
1687 : // Rescan the blockchain using the lowest timestamp
1688 99 : if (rescan) {
1689 85 : int64_t scanned_time = pwallet->RescanFromTime(lowest_timestamp, reserver, true /* update */);
1690 : {
1691 85 : LOCK(pwallet->cs_wallet);
1692 85 : pwallet->ReacceptWalletTransactions();
1693 85 : }
1694 :
1695 85 : if (pwallet->IsAbortingRescan()) {
1696 0 : throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");
1697 : }
1698 :
1699 85 : if (scanned_time > lowest_timestamp) {
1700 0 : std::vector<UniValue> results = response.getValues();
1701 0 : response.clear();
1702 0 : response.setArray();
1703 :
1704 : // Compose the response
1705 0 : for (unsigned int i = 0; i < requests.size(); ++i) {
1706 0 : const UniValue& request = requests.getValues().at(i);
1707 :
1708 : // If the descriptor timestamp is within the successfully scanned
1709 : // range, or if the import result already has an error set, let
1710 : // the result stand unmodified. Otherwise replace the result
1711 : // with an error message.
1712 0 : if (scanned_time <= GetImportTimestamp(request, now) || results.at(i).exists("error")) {
1713 0 : response.push_back(results.at(i));
1714 : } else {
1715 0 : UniValue result = UniValue(UniValue::VOBJ);
1716 0 : result.pushKV("success", UniValue(false));
1717 0 : result.pushKV(
1718 0 : "error",
1719 0 : JSONRPCError(
1720 : RPC_MISC_ERROR,
1721 0 : strprintf("Rescan failed for descriptor with timestamp %d. There was an error reading a "
1722 : "block from time %d, which is after or within %d seconds of key creation, and "
1723 : "could contain transactions pertaining to the desc. As a result, transactions "
1724 : "and coins using this desc may not appear in the wallet. This error could be "
1725 : "caused by pruning or data corruption (see bitcoind log for details) and could "
1726 : "be dealt with by downloading and rescanning the relevant blocks (see -reindex "
1727 : "and -rescan options).",
1728 0 : GetImportTimestamp(request, now), scanned_time - TIMESTAMP_WINDOW - 1, TIMESTAMP_WINDOW)));
1729 0 : response.push_back(std::move(result));
1730 0 : }
1731 : }
1732 0 : }
1733 85 : }
1734 :
1735 99 : return response;
1736 99 : },
1737 : };
1738 0 : }
|