Reject duplicate wallet filenames
[bitcoinplatinum.git] / src / wallet / rpcwallet.cpp
blob14560860cb448ed7a0ba5bfbb439fd9d4e493cbe
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
6 #include "amount.h"
7 #include "base58.h"
8 #include "chain.h"
9 #include "consensus/validation.h"
10 #include "core_io.h"
11 #include "init.h"
12 #include "httpserver.h"
13 #include "validation.h"
14 #include "net.h"
15 #include "policy/feerate.h"
16 #include "policy/fees.h"
17 #include "policy/policy.h"
18 #include "policy/rbf.h"
19 #include "rpc/mining.h"
20 #include "rpc/server.h"
21 #include "script/sign.h"
22 #include "timedata.h"
23 #include "util.h"
24 #include "utilmoneystr.h"
25 #include "wallet/coincontrol.h"
26 #include "wallet/feebumper.h"
27 #include "wallet/wallet.h"
28 #include "wallet/walletdb.h"
30 #include <stdint.h>
32 #include <univalue.h>
34 static const std::string WALLET_ENDPOINT_BASE = "/wallet/";
36 CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
38 if (request.URI.substr(0, WALLET_ENDPOINT_BASE.size()) == WALLET_ENDPOINT_BASE) {
39 // wallet endpoint was used
40 std::string requestedWallet = urlDecode(request.URI.substr(WALLET_ENDPOINT_BASE.size()));
41 for (CWalletRef pwallet : ::vpwallets) {
42 if (pwallet->GetName() == requestedWallet) {
43 return pwallet;
46 throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
48 return ::vpwallets.size() == 1 || (request.fHelp && ::vpwallets.size() > 0) ? ::vpwallets[0] : nullptr;
51 std::string HelpRequiringPassphrase(CWallet * const pwallet)
53 return pwallet && pwallet->IsCrypted()
54 ? "\nRequires wallet passphrase to be set with walletpassphrase call."
55 : "";
58 bool EnsureWalletIsAvailable(CWallet * const pwallet, bool avoidException)
60 if (pwallet) return true;
61 if (avoidException) return false;
62 if (::vpwallets.empty()) {
63 // Note: It isn't currently possible to trigger this error because
64 // wallet RPC methods aren't registered unless a wallet is loaded. But
65 // this error is being kept as a precaution, because it's possible in
66 // the future that wallet RPC methods might get or remain registered
67 // when no wallets are loaded.
68 throw JSONRPCError(
69 RPC_METHOD_NOT_FOUND, "Method not found (wallet method is disabled because no wallet is loaded)");
71 throw JSONRPCError(RPC_WALLET_NOT_SPECIFIED,
72 "Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path).");
75 void EnsureWalletIsUnlocked(CWallet * const pwallet)
77 if (pwallet->IsLocked()) {
78 throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please enter the wallet passphrase with walletpassphrase first.");
82 void WalletTxToJSON(const CWalletTx& wtx, UniValue& entry)
84 int confirms = wtx.GetDepthInMainChain();
85 entry.push_back(Pair("confirmations", confirms));
86 if (wtx.IsCoinBase())
87 entry.push_back(Pair("generated", true));
88 if (confirms > 0)
90 entry.push_back(Pair("blockhash", wtx.hashBlock.GetHex()));
91 entry.push_back(Pair("blockindex", wtx.nIndex));
92 entry.push_back(Pair("blocktime", mapBlockIndex[wtx.hashBlock]->GetBlockTime()));
93 } else {
94 entry.push_back(Pair("trusted", wtx.IsTrusted()));
96 uint256 hash = wtx.GetHash();
97 entry.push_back(Pair("txid", hash.GetHex()));
98 UniValue conflicts(UniValue::VARR);
99 for (const uint256& conflict : wtx.GetConflicts())
100 conflicts.push_back(conflict.GetHex());
101 entry.push_back(Pair("walletconflicts", conflicts));
102 entry.push_back(Pair("time", wtx.GetTxTime()));
103 entry.push_back(Pair("timereceived", (int64_t)wtx.nTimeReceived));
105 // Add opt-in RBF status
106 std::string rbfStatus = "no";
107 if (confirms <= 0) {
108 LOCK(mempool.cs);
109 RBFTransactionState rbfState = IsRBFOptIn(wtx, mempool);
110 if (rbfState == RBF_TRANSACTIONSTATE_UNKNOWN)
111 rbfStatus = "unknown";
112 else if (rbfState == RBF_TRANSACTIONSTATE_REPLACEABLE_BIP125)
113 rbfStatus = "yes";
115 entry.push_back(Pair("bip125-replaceable", rbfStatus));
117 for (const std::pair<std::string, std::string>& item : wtx.mapValue)
118 entry.push_back(Pair(item.first, item.second));
121 std::string AccountFromValue(const UniValue& value)
123 std::string strAccount = value.get_str();
124 if (strAccount == "*")
125 throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME, "Invalid account name");
126 return strAccount;
129 UniValue getnewaddress(const JSONRPCRequest& request)
131 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
132 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
133 return NullUniValue;
136 if (request.fHelp || request.params.size() > 1)
137 throw std::runtime_error(
138 "getnewaddress ( \"account\" )\n"
139 "\nReturns a new Bitcoin address for receiving payments.\n"
140 "If 'account' is specified (DEPRECATED), it is added to the address book \n"
141 "so payments received with the address will be credited to 'account'.\n"
142 "\nArguments:\n"
143 "1. \"account\" (string, optional) DEPRECATED. The account name for the address to be linked to. If not provided, the default account \"\" is used. It can also be set to the empty string \"\" to represent the default account. The account does not need to exist, it will be created if there is no account by the given name.\n"
144 "\nResult:\n"
145 "\"address\" (string) The new bitcoin address\n"
146 "\nExamples:\n"
147 + HelpExampleCli("getnewaddress", "")
148 + HelpExampleRpc("getnewaddress", "")
151 LOCK2(cs_main, pwallet->cs_wallet);
153 // Parse the account first so we don't generate a key if there's an error
154 std::string strAccount;
155 if (!request.params[0].isNull())
156 strAccount = AccountFromValue(request.params[0]);
158 if (!pwallet->IsLocked()) {
159 pwallet->TopUpKeyPool();
162 // Generate a new key that is added to wallet
163 CPubKey newKey;
164 if (!pwallet->GetKeyFromPool(newKey)) {
165 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
167 CKeyID keyID = newKey.GetID();
169 pwallet->SetAddressBook(keyID, strAccount, "receive");
171 return CBitcoinAddress(keyID).ToString();
175 CBitcoinAddress GetAccountAddress(CWallet* const pwallet, std::string strAccount, bool bForceNew=false)
177 CPubKey pubKey;
178 if (!pwallet->GetAccountPubkey(pubKey, strAccount, bForceNew)) {
179 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
182 return CBitcoinAddress(pubKey.GetID());
185 UniValue getaccountaddress(const JSONRPCRequest& request)
187 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
188 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
189 return NullUniValue;
192 if (request.fHelp || request.params.size() != 1)
193 throw std::runtime_error(
194 "getaccountaddress \"account\"\n"
195 "\nDEPRECATED. Returns the current Bitcoin address for receiving payments to this account.\n"
196 "\nArguments:\n"
197 "1. \"account\" (string, required) The account name for the address. It can also be set to the empty string \"\" to represent the default account. The account does not need to exist, it will be created and a new address created if there is no account by the given name.\n"
198 "\nResult:\n"
199 "\"address\" (string) The account bitcoin address\n"
200 "\nExamples:\n"
201 + HelpExampleCli("getaccountaddress", "")
202 + HelpExampleCli("getaccountaddress", "\"\"")
203 + HelpExampleCli("getaccountaddress", "\"myaccount\"")
204 + HelpExampleRpc("getaccountaddress", "\"myaccount\"")
207 LOCK2(cs_main, pwallet->cs_wallet);
209 // Parse the account first so we don't generate a key if there's an error
210 std::string strAccount = AccountFromValue(request.params[0]);
212 UniValue ret(UniValue::VSTR);
214 ret = GetAccountAddress(pwallet, strAccount).ToString();
215 return ret;
219 UniValue getrawchangeaddress(const JSONRPCRequest& request)
221 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
222 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
223 return NullUniValue;
226 if (request.fHelp || request.params.size() > 0)
227 throw std::runtime_error(
228 "getrawchangeaddress\n"
229 "\nReturns a new Bitcoin address, for receiving change.\n"
230 "This is for use with raw transactions, NOT normal use.\n"
231 "\nResult:\n"
232 "\"address\" (string) The address\n"
233 "\nExamples:\n"
234 + HelpExampleCli("getrawchangeaddress", "")
235 + HelpExampleRpc("getrawchangeaddress", "")
238 LOCK2(cs_main, pwallet->cs_wallet);
240 if (!pwallet->IsLocked()) {
241 pwallet->TopUpKeyPool();
244 CReserveKey reservekey(pwallet);
245 CPubKey vchPubKey;
246 if (!reservekey.GetReservedKey(vchPubKey, true))
247 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
249 reservekey.KeepKey();
251 CKeyID keyID = vchPubKey.GetID();
253 return CBitcoinAddress(keyID).ToString();
257 UniValue setaccount(const JSONRPCRequest& request)
259 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
260 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
261 return NullUniValue;
264 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
265 throw std::runtime_error(
266 "setaccount \"address\" \"account\"\n"
267 "\nDEPRECATED. Sets the account associated with the given address.\n"
268 "\nArguments:\n"
269 "1. \"address\" (string, required) The bitcoin address to be associated with an account.\n"
270 "2. \"account\" (string, required) The account to assign the address to.\n"
271 "\nExamples:\n"
272 + HelpExampleCli("setaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"tabby\"")
273 + HelpExampleRpc("setaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"tabby\"")
276 LOCK2(cs_main, pwallet->cs_wallet);
278 CBitcoinAddress address(request.params[0].get_str());
279 if (!address.IsValid())
280 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
282 std::string strAccount;
283 if (request.params.size() > 1)
284 strAccount = AccountFromValue(request.params[1]);
286 // Only add the account if the address is yours.
287 if (IsMine(*pwallet, address.Get())) {
288 // Detect when changing the account of an address that is the 'unused current key' of another account:
289 if (pwallet->mapAddressBook.count(address.Get())) {
290 std::string strOldAccount = pwallet->mapAddressBook[address.Get()].name;
291 if (address == GetAccountAddress(pwallet, strOldAccount)) {
292 GetAccountAddress(pwallet, strOldAccount, true);
295 pwallet->SetAddressBook(address.Get(), strAccount, "receive");
297 else
298 throw JSONRPCError(RPC_MISC_ERROR, "setaccount can only be used with own address");
300 return NullUniValue;
304 UniValue getaccount(const JSONRPCRequest& request)
306 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
307 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
308 return NullUniValue;
311 if (request.fHelp || request.params.size() != 1)
312 throw std::runtime_error(
313 "getaccount \"address\"\n"
314 "\nDEPRECATED. Returns the account associated with the given address.\n"
315 "\nArguments:\n"
316 "1. \"address\" (string, required) The bitcoin address for account lookup.\n"
317 "\nResult:\n"
318 "\"accountname\" (string) the account address\n"
319 "\nExamples:\n"
320 + HelpExampleCli("getaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\"")
321 + HelpExampleRpc("getaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\"")
324 LOCK2(cs_main, pwallet->cs_wallet);
326 CBitcoinAddress address(request.params[0].get_str());
327 if (!address.IsValid())
328 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
330 std::string strAccount;
331 std::map<CTxDestination, CAddressBookData>::iterator mi = pwallet->mapAddressBook.find(address.Get());
332 if (mi != pwallet->mapAddressBook.end() && !(*mi).second.name.empty()) {
333 strAccount = (*mi).second.name;
335 return strAccount;
339 UniValue getaddressesbyaccount(const JSONRPCRequest& request)
341 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
342 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
343 return NullUniValue;
346 if (request.fHelp || request.params.size() != 1)
347 throw std::runtime_error(
348 "getaddressesbyaccount \"account\"\n"
349 "\nDEPRECATED. Returns the list of addresses for the given account.\n"
350 "\nArguments:\n"
351 "1. \"account\" (string, required) The account name.\n"
352 "\nResult:\n"
353 "[ (json array of string)\n"
354 " \"address\" (string) a bitcoin address associated with the given account\n"
355 " ,...\n"
356 "]\n"
357 "\nExamples:\n"
358 + HelpExampleCli("getaddressesbyaccount", "\"tabby\"")
359 + HelpExampleRpc("getaddressesbyaccount", "\"tabby\"")
362 LOCK2(cs_main, pwallet->cs_wallet);
364 std::string strAccount = AccountFromValue(request.params[0]);
366 // Find all addresses that have the given account
367 UniValue ret(UniValue::VARR);
368 for (const std::pair<CBitcoinAddress, CAddressBookData>& item : pwallet->mapAddressBook) {
369 const CBitcoinAddress& address = item.first;
370 const std::string& strName = item.second.name;
371 if (strName == strAccount)
372 ret.push_back(address.ToString());
374 return ret;
377 static void SendMoney(CWallet * const pwallet, const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, CWalletTx& wtxNew, const CCoinControl& coin_control)
379 CAmount curBalance = pwallet->GetBalance();
381 // Check amount
382 if (nValue <= 0)
383 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid amount");
385 if (nValue > curBalance)
386 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Insufficient funds");
388 if (pwallet->GetBroadcastTransactions() && !g_connman) {
389 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
392 // Parse Bitcoin address
393 CScript scriptPubKey = GetScriptForDestination(address);
395 // Create and send the transaction
396 CReserveKey reservekey(pwallet);
397 CAmount nFeeRequired;
398 std::string strError;
399 std::vector<CRecipient> vecSend;
400 int nChangePosRet = -1;
401 CRecipient recipient = {scriptPubKey, nValue, fSubtractFeeFromAmount};
402 vecSend.push_back(recipient);
403 if (!pwallet->CreateTransaction(vecSend, wtxNew, reservekey, nFeeRequired, nChangePosRet, strError, coin_control)) {
404 if (!fSubtractFeeFromAmount && nValue + nFeeRequired > curBalance)
405 strError = strprintf("Error: This transaction requires a transaction fee of at least %s", FormatMoney(nFeeRequired));
406 throw JSONRPCError(RPC_WALLET_ERROR, strError);
408 CValidationState state;
409 if (!pwallet->CommitTransaction(wtxNew, reservekey, g_connman.get(), state)) {
410 strError = strprintf("Error: The transaction was rejected! Reason given: %s", state.GetRejectReason());
411 throw JSONRPCError(RPC_WALLET_ERROR, strError);
415 UniValue sendtoaddress(const JSONRPCRequest& request)
417 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
418 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
419 return NullUniValue;
422 if (request.fHelp || request.params.size() < 2 || request.params.size() > 8)
423 throw std::runtime_error(
424 "sendtoaddress \"address\" amount ( \"comment\" \"comment_to\" subtractfeefromamount replaceable conf_target \"estimate_mode\")\n"
425 "\nSend an amount to a given address.\n"
426 + HelpRequiringPassphrase(pwallet) +
427 "\nArguments:\n"
428 "1. \"address\" (string, required) The bitcoin address to send to.\n"
429 "2. \"amount\" (numeric or string, required) The amount in " + CURRENCY_UNIT + " to send. eg 0.1\n"
430 "3. \"comment\" (string, optional) A comment used to store what the transaction is for. \n"
431 " This is not part of the transaction, just kept in your wallet.\n"
432 "4. \"comment_to\" (string, optional) A comment to store the name of the person or organization \n"
433 " to which you're sending the transaction. This is not part of the \n"
434 " transaction, just kept in your wallet.\n"
435 "5. subtractfeefromamount (boolean, optional, default=false) The fee will be deducted from the amount being sent.\n"
436 " The recipient will receive less bitcoins than you enter in the amount field.\n"
437 "6. replaceable (boolean, optional) Allow this transaction to be replaced by a transaction with higher fees via BIP 125\n"
438 "7. conf_target (numeric, optional) Confirmation target (in blocks)\n"
439 "8. \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
440 " \"UNSET\"\n"
441 " \"ECONOMICAL\"\n"
442 " \"CONSERVATIVE\"\n"
443 "\nResult:\n"
444 "\"txid\" (string) The transaction id.\n"
445 "\nExamples:\n"
446 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.1")
447 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.1 \"donation\" \"seans outpost\"")
448 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.1 \"\" \"\" true")
449 + HelpExampleRpc("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\", 0.1, \"donation\", \"seans outpost\"")
452 LOCK2(cs_main, pwallet->cs_wallet);
454 CBitcoinAddress address(request.params[0].get_str());
455 if (!address.IsValid())
456 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
458 // Amount
459 CAmount nAmount = AmountFromValue(request.params[1]);
460 if (nAmount <= 0)
461 throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
463 // Wallet comments
464 CWalletTx wtx;
465 if (request.params.size() > 2 && !request.params[2].isNull() && !request.params[2].get_str().empty())
466 wtx.mapValue["comment"] = request.params[2].get_str();
467 if (request.params.size() > 3 && !request.params[3].isNull() && !request.params[3].get_str().empty())
468 wtx.mapValue["to"] = request.params[3].get_str();
470 bool fSubtractFeeFromAmount = false;
471 if (request.params.size() > 4 && !request.params[4].isNull()) {
472 fSubtractFeeFromAmount = request.params[4].get_bool();
475 CCoinControl coin_control;
476 if (request.params.size() > 5 && !request.params[5].isNull()) {
477 coin_control.signalRbf = request.params[5].get_bool();
480 if (request.params.size() > 6 && !request.params[6].isNull()) {
481 coin_control.m_confirm_target = ParseConfirmTarget(request.params[6]);
484 if (request.params.size() > 7 && !request.params[7].isNull()) {
485 if (!FeeModeFromString(request.params[7].get_str(), coin_control.m_fee_mode)) {
486 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid estimate_mode parameter");
491 EnsureWalletIsUnlocked(pwallet);
493 SendMoney(pwallet, address.Get(), nAmount, fSubtractFeeFromAmount, wtx, coin_control);
495 return wtx.GetHash().GetHex();
498 UniValue listaddressgroupings(const JSONRPCRequest& request)
500 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
501 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
502 return NullUniValue;
505 if (request.fHelp || request.params.size() != 0)
506 throw std::runtime_error(
507 "listaddressgroupings\n"
508 "\nLists groups of addresses which have had their common ownership\n"
509 "made public by common use as inputs or as the resulting change\n"
510 "in past transactions\n"
511 "\nResult:\n"
512 "[\n"
513 " [\n"
514 " [\n"
515 " \"address\", (string) The bitcoin address\n"
516 " amount, (numeric) The amount in " + CURRENCY_UNIT + "\n"
517 " \"account\" (string, optional) DEPRECATED. The account\n"
518 " ]\n"
519 " ,...\n"
520 " ]\n"
521 " ,...\n"
522 "]\n"
523 "\nExamples:\n"
524 + HelpExampleCli("listaddressgroupings", "")
525 + HelpExampleRpc("listaddressgroupings", "")
528 LOCK2(cs_main, pwallet->cs_wallet);
530 UniValue jsonGroupings(UniValue::VARR);
531 std::map<CTxDestination, CAmount> balances = pwallet->GetAddressBalances();
532 for (std::set<CTxDestination> grouping : pwallet->GetAddressGroupings()) {
533 UniValue jsonGrouping(UniValue::VARR);
534 for (CTxDestination address : grouping)
536 UniValue addressInfo(UniValue::VARR);
537 addressInfo.push_back(CBitcoinAddress(address).ToString());
538 addressInfo.push_back(ValueFromAmount(balances[address]));
540 if (pwallet->mapAddressBook.find(CBitcoinAddress(address).Get()) != pwallet->mapAddressBook.end()) {
541 addressInfo.push_back(pwallet->mapAddressBook.find(CBitcoinAddress(address).Get())->second.name);
544 jsonGrouping.push_back(addressInfo);
546 jsonGroupings.push_back(jsonGrouping);
548 return jsonGroupings;
551 UniValue signmessage(const JSONRPCRequest& request)
553 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
554 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
555 return NullUniValue;
558 if (request.fHelp || request.params.size() != 2)
559 throw std::runtime_error(
560 "signmessage \"address\" \"message\"\n"
561 "\nSign a message with the private key of an address"
562 + HelpRequiringPassphrase(pwallet) + "\n"
563 "\nArguments:\n"
564 "1. \"address\" (string, required) The bitcoin address to use for the private key.\n"
565 "2. \"message\" (string, required) The message to create a signature of.\n"
566 "\nResult:\n"
567 "\"signature\" (string) The signature of the message encoded in base 64\n"
568 "\nExamples:\n"
569 "\nUnlock the wallet for 30 seconds\n"
570 + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
571 "\nCreate the signature\n"
572 + HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
573 "\nVerify the signature\n"
574 + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
575 "\nAs json rpc\n"
576 + HelpExampleRpc("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"my message\"")
579 LOCK2(cs_main, pwallet->cs_wallet);
581 EnsureWalletIsUnlocked(pwallet);
583 std::string strAddress = request.params[0].get_str();
584 std::string strMessage = request.params[1].get_str();
586 CBitcoinAddress addr(strAddress);
587 if (!addr.IsValid())
588 throw JSONRPCError(RPC_TYPE_ERROR, "Invalid address");
590 CKeyID keyID;
591 if (!addr.GetKeyID(keyID))
592 throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
594 CKey key;
595 if (!pwallet->GetKey(keyID, key)) {
596 throw JSONRPCError(RPC_WALLET_ERROR, "Private key not available");
599 CHashWriter ss(SER_GETHASH, 0);
600 ss << strMessageMagic;
601 ss << strMessage;
603 std::vector<unsigned char> vchSig;
604 if (!key.SignCompact(ss.GetHash(), vchSig))
605 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");
607 return EncodeBase64(&vchSig[0], vchSig.size());
610 UniValue getreceivedbyaddress(const JSONRPCRequest& request)
612 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
613 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
614 return NullUniValue;
617 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
618 throw std::runtime_error(
619 "getreceivedbyaddress \"address\" ( minconf )\n"
620 "\nReturns the total amount received by the given address in transactions with at least minconf confirmations.\n"
621 "\nArguments:\n"
622 "1. \"address\" (string, required) The bitcoin address for transactions.\n"
623 "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n"
624 "\nResult:\n"
625 "amount (numeric) The total amount in " + CURRENCY_UNIT + " received at this address.\n"
626 "\nExamples:\n"
627 "\nThe amount from transactions with at least 1 confirmation\n"
628 + HelpExampleCli("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\"") +
629 "\nThe amount including unconfirmed transactions, zero confirmations\n"
630 + HelpExampleCli("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" 0") +
631 "\nThe amount with at least 6 confirmations\n"
632 + HelpExampleCli("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" 6") +
633 "\nAs a json rpc call\n"
634 + HelpExampleRpc("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", 6")
637 LOCK2(cs_main, pwallet->cs_wallet);
639 // Bitcoin address
640 CBitcoinAddress address = CBitcoinAddress(request.params[0].get_str());
641 if (!address.IsValid())
642 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
643 CScript scriptPubKey = GetScriptForDestination(address.Get());
644 if (!IsMine(*pwallet, scriptPubKey)) {
645 return ValueFromAmount(0);
648 // Minimum confirmations
649 int nMinDepth = 1;
650 if (!request.params[1].isNull())
651 nMinDepth = request.params[1].get_int();
653 // Tally
654 CAmount nAmount = 0;
655 for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
656 const CWalletTx& wtx = pairWtx.second;
657 if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
658 continue;
660 for (const CTxOut& txout : wtx.tx->vout)
661 if (txout.scriptPubKey == scriptPubKey)
662 if (wtx.GetDepthInMainChain() >= nMinDepth)
663 nAmount += txout.nValue;
666 return ValueFromAmount(nAmount);
670 UniValue getreceivedbyaccount(const JSONRPCRequest& request)
672 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
673 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
674 return NullUniValue;
677 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
678 throw std::runtime_error(
679 "getreceivedbyaccount \"account\" ( minconf )\n"
680 "\nDEPRECATED. Returns the total amount received by addresses with <account> in transactions with at least [minconf] confirmations.\n"
681 "\nArguments:\n"
682 "1. \"account\" (string, required) The selected account, may be the default account using \"\".\n"
683 "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n"
684 "\nResult:\n"
685 "amount (numeric) The total amount in " + CURRENCY_UNIT + " received for this account.\n"
686 "\nExamples:\n"
687 "\nAmount received by the default account with at least 1 confirmation\n"
688 + HelpExampleCli("getreceivedbyaccount", "\"\"") +
689 "\nAmount received at the tabby account including unconfirmed amounts with zero confirmations\n"
690 + HelpExampleCli("getreceivedbyaccount", "\"tabby\" 0") +
691 "\nThe amount with at least 6 confirmations\n"
692 + HelpExampleCli("getreceivedbyaccount", "\"tabby\" 6") +
693 "\nAs a json rpc call\n"
694 + HelpExampleRpc("getreceivedbyaccount", "\"tabby\", 6")
697 LOCK2(cs_main, pwallet->cs_wallet);
699 // Minimum confirmations
700 int nMinDepth = 1;
701 if (!request.params[1].isNull())
702 nMinDepth = request.params[1].get_int();
704 // Get the set of pub keys assigned to account
705 std::string strAccount = AccountFromValue(request.params[0]);
706 std::set<CTxDestination> setAddress = pwallet->GetAccountAddresses(strAccount);
708 // Tally
709 CAmount nAmount = 0;
710 for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
711 const CWalletTx& wtx = pairWtx.second;
712 if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
713 continue;
715 for (const CTxOut& txout : wtx.tx->vout)
717 CTxDestination address;
718 if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*pwallet, address) && setAddress.count(address)) {
719 if (wtx.GetDepthInMainChain() >= nMinDepth)
720 nAmount += txout.nValue;
725 return ValueFromAmount(nAmount);
729 UniValue getbalance(const JSONRPCRequest& request)
731 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
732 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
733 return NullUniValue;
736 if (request.fHelp || request.params.size() > 3)
737 throw std::runtime_error(
738 "getbalance ( \"account\" minconf include_watchonly )\n"
739 "\nIf account is not specified, returns the server's total available balance.\n"
740 "If account is specified (DEPRECATED), returns the balance in the account.\n"
741 "Note that the account \"\" is not the same as leaving the parameter out.\n"
742 "The server total may be different to the balance in the default \"\" account.\n"
743 "\nArguments:\n"
744 "1. \"account\" (string, optional) DEPRECATED. The account string may be given as a\n"
745 " specific account name to find the balance associated with wallet keys in\n"
746 " a named account, or as the empty string (\"\") to find the balance\n"
747 " associated with wallet keys not in any named account, or as \"*\" to find\n"
748 " the balance associated with all wallet keys regardless of account.\n"
749 " When this option is specified, it calculates the balance in a different\n"
750 " way than when it is not specified, and which can count spends twice when\n"
751 " there are conflicting pending transactions (such as those created by\n"
752 " the bumpfee command), temporarily resulting in low or even negative\n"
753 " balances. In general, account balance calculation is not considered\n"
754 " reliable and has resulted in confusing outcomes, so it is recommended to\n"
755 " avoid passing this argument.\n"
756 "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n"
757 "3. include_watchonly (bool, optional, default=false) Also include balance in watch-only addresses (see 'importaddress')\n"
758 "\nResult:\n"
759 "amount (numeric) The total amount in " + CURRENCY_UNIT + " received for this account.\n"
760 "\nExamples:\n"
761 "\nThe total amount in the wallet with 1 or more confirmations\n"
762 + HelpExampleCli("getbalance", "") +
763 "\nThe total amount in the wallet at least 6 blocks confirmed\n"
764 + HelpExampleCli("getbalance", "\"*\" 6") +
765 "\nAs a json rpc call\n"
766 + HelpExampleRpc("getbalance", "\"*\", 6")
769 LOCK2(cs_main, pwallet->cs_wallet);
771 if (request.params.size() == 0)
772 return ValueFromAmount(pwallet->GetBalance());
774 const std::string& account_param = request.params[0].get_str();
775 const std::string* account = account_param != "*" ? &account_param : nullptr;
777 int nMinDepth = 1;
778 if (!request.params[1].isNull())
779 nMinDepth = request.params[1].get_int();
780 isminefilter filter = ISMINE_SPENDABLE;
781 if(!request.params[2].isNull())
782 if(request.params[2].get_bool())
783 filter = filter | ISMINE_WATCH_ONLY;
785 return ValueFromAmount(pwallet->GetLegacyBalance(filter, nMinDepth, account));
788 UniValue getunconfirmedbalance(const JSONRPCRequest &request)
790 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
791 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
792 return NullUniValue;
795 if (request.fHelp || request.params.size() > 0)
796 throw std::runtime_error(
797 "getunconfirmedbalance\n"
798 "Returns the server's total unconfirmed balance\n");
800 LOCK2(cs_main, pwallet->cs_wallet);
802 return ValueFromAmount(pwallet->GetUnconfirmedBalance());
806 UniValue movecmd(const JSONRPCRequest& request)
808 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
809 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
810 return NullUniValue;
813 if (request.fHelp || request.params.size() < 3 || request.params.size() > 5)
814 throw std::runtime_error(
815 "move \"fromaccount\" \"toaccount\" amount ( minconf \"comment\" )\n"
816 "\nDEPRECATED. Move a specified amount from one account in your wallet to another.\n"
817 "\nArguments:\n"
818 "1. \"fromaccount\" (string, required) The name of the account to move funds from. May be the default account using \"\".\n"
819 "2. \"toaccount\" (string, required) The name of the account to move funds to. May be the default account using \"\".\n"
820 "3. amount (numeric) Quantity of " + CURRENCY_UNIT + " to move between accounts.\n"
821 "4. (dummy) (numeric, optional) Ignored. Remains for backward compatibility.\n"
822 "5. \"comment\" (string, optional) An optional comment, stored in the wallet only.\n"
823 "\nResult:\n"
824 "true|false (boolean) true if successful.\n"
825 "\nExamples:\n"
826 "\nMove 0.01 " + CURRENCY_UNIT + " from the default account to the account named tabby\n"
827 + HelpExampleCli("move", "\"\" \"tabby\" 0.01") +
828 "\nMove 0.01 " + CURRENCY_UNIT + " timotei to akiko with a comment and funds have 6 confirmations\n"
829 + HelpExampleCli("move", "\"timotei\" \"akiko\" 0.01 6 \"happy birthday!\"") +
830 "\nAs a json rpc call\n"
831 + HelpExampleRpc("move", "\"timotei\", \"akiko\", 0.01, 6, \"happy birthday!\"")
834 LOCK2(cs_main, pwallet->cs_wallet);
836 std::string strFrom = AccountFromValue(request.params[0]);
837 std::string strTo = AccountFromValue(request.params[1]);
838 CAmount nAmount = AmountFromValue(request.params[2]);
839 if (nAmount <= 0)
840 throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
841 if (request.params.size() > 3)
842 // unused parameter, used to be nMinDepth, keep type-checking it though
843 (void)request.params[3].get_int();
844 std::string strComment;
845 if (request.params.size() > 4)
846 strComment = request.params[4].get_str();
848 if (!pwallet->AccountMove(strFrom, strTo, nAmount, strComment)) {
849 throw JSONRPCError(RPC_DATABASE_ERROR, "database error");
852 return true;
856 UniValue sendfrom(const JSONRPCRequest& request)
858 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
859 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
860 return NullUniValue;
863 if (request.fHelp || request.params.size() < 3 || request.params.size() > 6)
864 throw std::runtime_error(
865 "sendfrom \"fromaccount\" \"toaddress\" amount ( minconf \"comment\" \"comment_to\" )\n"
866 "\nDEPRECATED (use sendtoaddress). Sent an amount from an account to a bitcoin address."
867 + HelpRequiringPassphrase(pwallet) + "\n"
868 "\nArguments:\n"
869 "1. \"fromaccount\" (string, required) The name of the account to send funds from. May be the default account using \"\".\n"
870 " Specifying an account does not influence coin selection, but it does associate the newly created\n"
871 " transaction with the account, so the account's balance computation and transaction history can reflect\n"
872 " the spend.\n"
873 "2. \"toaddress\" (string, required) The bitcoin address to send funds to.\n"
874 "3. amount (numeric or string, required) The amount in " + CURRENCY_UNIT + " (transaction fee is added on top).\n"
875 "4. minconf (numeric, optional, default=1) Only use funds with at least this many confirmations.\n"
876 "5. \"comment\" (string, optional) A comment used to store what the transaction is for. \n"
877 " This is not part of the transaction, just kept in your wallet.\n"
878 "6. \"comment_to\" (string, optional) An optional comment to store the name of the person or organization \n"
879 " to which you're sending the transaction. This is not part of the transaction, \n"
880 " it is just kept in your wallet.\n"
881 "\nResult:\n"
882 "\"txid\" (string) The transaction id.\n"
883 "\nExamples:\n"
884 "\nSend 0.01 " + CURRENCY_UNIT + " from the default account to the address, must have at least 1 confirmation\n"
885 + HelpExampleCli("sendfrom", "\"\" \"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.01") +
886 "\nSend 0.01 from the tabby account to the given address, funds must have at least 6 confirmations\n"
887 + HelpExampleCli("sendfrom", "\"tabby\" \"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.01 6 \"donation\" \"seans outpost\"") +
888 "\nAs a json rpc call\n"
889 + HelpExampleRpc("sendfrom", "\"tabby\", \"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\", 0.01, 6, \"donation\", \"seans outpost\"")
892 LOCK2(cs_main, pwallet->cs_wallet);
894 std::string strAccount = AccountFromValue(request.params[0]);
895 CBitcoinAddress address(request.params[1].get_str());
896 if (!address.IsValid())
897 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
898 CAmount nAmount = AmountFromValue(request.params[2]);
899 if (nAmount <= 0)
900 throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
901 int nMinDepth = 1;
902 if (request.params.size() > 3)
903 nMinDepth = request.params[3].get_int();
905 CWalletTx wtx;
906 wtx.strFromAccount = strAccount;
907 if (request.params.size() > 4 && !request.params[4].isNull() && !request.params[4].get_str().empty())
908 wtx.mapValue["comment"] = request.params[4].get_str();
909 if (request.params.size() > 5 && !request.params[5].isNull() && !request.params[5].get_str().empty())
910 wtx.mapValue["to"] = request.params[5].get_str();
912 EnsureWalletIsUnlocked(pwallet);
914 // Check funds
915 CAmount nBalance = pwallet->GetLegacyBalance(ISMINE_SPENDABLE, nMinDepth, &strAccount);
916 if (nAmount > nBalance)
917 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
919 CCoinControl no_coin_control; // This is a deprecated API
920 SendMoney(pwallet, address.Get(), nAmount, false, wtx, no_coin_control);
922 return wtx.GetHash().GetHex();
926 UniValue sendmany(const JSONRPCRequest& request)
928 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
929 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
930 return NullUniValue;
933 if (request.fHelp || request.params.size() < 2 || request.params.size() > 8)
934 throw std::runtime_error(
935 "sendmany \"fromaccount\" {\"address\":amount,...} ( minconf \"comment\" [\"address\",...] replaceable conf_target \"estimate_mode\")\n"
936 "\nSend multiple times. Amounts are double-precision floating point numbers."
937 + HelpRequiringPassphrase(pwallet) + "\n"
938 "\nArguments:\n"
939 "1. \"fromaccount\" (string, required) DEPRECATED. The account to send the funds from. Should be \"\" for the default account\n"
940 "2. \"amounts\" (string, required) A json object with addresses and amounts\n"
941 " {\n"
942 " \"address\":amount (numeric or string) The bitcoin address is the key, the numeric amount (can be string) in " + CURRENCY_UNIT + " is the value\n"
943 " ,...\n"
944 " }\n"
945 "3. minconf (numeric, optional, default=1) Only use the balance confirmed at least this many times.\n"
946 "4. \"comment\" (string, optional) A comment\n"
947 "5. subtractfeefrom (array, optional) A json array with addresses.\n"
948 " The fee will be equally deducted from the amount of each selected address.\n"
949 " Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n"
950 " If no addresses are specified here, the sender pays the fee.\n"
951 " [\n"
952 " \"address\" (string) Subtract fee from this address\n"
953 " ,...\n"
954 " ]\n"
955 "6. replaceable (boolean, optional) Allow this transaction to be replaced by a transaction with higher fees via BIP 125\n"
956 "7. conf_target (numeric, optional) Confirmation target (in blocks)\n"
957 "8. \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
958 " \"UNSET\"\n"
959 " \"ECONOMICAL\"\n"
960 " \"CONSERVATIVE\"\n"
961 "\nResult:\n"
962 "\"txid\" (string) The transaction id for the send. Only 1 transaction is created regardless of \n"
963 " the number of addresses.\n"
964 "\nExamples:\n"
965 "\nSend two amounts to two different addresses:\n"
966 + HelpExampleCli("sendmany", "\"\" \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\"") +
967 "\nSend two amounts to two different addresses setting the confirmation and comment:\n"
968 + HelpExampleCli("sendmany", "\"\" \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\" 6 \"testing\"") +
969 "\nSend two amounts to two different addresses, subtract fee from amount:\n"
970 + HelpExampleCli("sendmany", "\"\" \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\" 1 \"\" \"[\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\",\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\"]\"") +
971 "\nAs a json rpc call\n"
972 + HelpExampleRpc("sendmany", "\"\", \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\", 6, \"testing\"")
975 LOCK2(cs_main, pwallet->cs_wallet);
977 if (pwallet->GetBroadcastTransactions() && !g_connman) {
978 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
981 std::string strAccount = AccountFromValue(request.params[0]);
982 UniValue sendTo = request.params[1].get_obj();
983 int nMinDepth = 1;
984 if (!request.params[2].isNull())
985 nMinDepth = request.params[2].get_int();
987 CWalletTx wtx;
988 wtx.strFromAccount = strAccount;
989 if (request.params.size() > 3 && !request.params[3].isNull() && !request.params[3].get_str().empty())
990 wtx.mapValue["comment"] = request.params[3].get_str();
992 UniValue subtractFeeFromAmount(UniValue::VARR);
993 if (request.params.size() > 4 && !request.params[4].isNull())
994 subtractFeeFromAmount = request.params[4].get_array();
996 CCoinControl coin_control;
997 if (request.params.size() > 5 && !request.params[5].isNull()) {
998 coin_control.signalRbf = request.params[5].get_bool();
1001 if (request.params.size() > 6 && !request.params[6].isNull()) {
1002 coin_control.m_confirm_target = ParseConfirmTarget(request.params[6]);
1005 if (request.params.size() > 7 && !request.params[7].isNull()) {
1006 if (!FeeModeFromString(request.params[7].get_str(), coin_control.m_fee_mode)) {
1007 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid estimate_mode parameter");
1011 std::set<CBitcoinAddress> setAddress;
1012 std::vector<CRecipient> vecSend;
1014 CAmount totalAmount = 0;
1015 std::vector<std::string> keys = sendTo.getKeys();
1016 for (const std::string& name_ : keys)
1018 CBitcoinAddress address(name_);
1019 if (!address.IsValid())
1020 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ")+name_);
1022 if (setAddress.count(address))
1023 throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ")+name_);
1024 setAddress.insert(address);
1026 CScript scriptPubKey = GetScriptForDestination(address.Get());
1027 CAmount nAmount = AmountFromValue(sendTo[name_]);
1028 if (nAmount <= 0)
1029 throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
1030 totalAmount += nAmount;
1032 bool fSubtractFeeFromAmount = false;
1033 for (unsigned int idx = 0; idx < subtractFeeFromAmount.size(); idx++) {
1034 const UniValue& addr = subtractFeeFromAmount[idx];
1035 if (addr.get_str() == name_)
1036 fSubtractFeeFromAmount = true;
1039 CRecipient recipient = {scriptPubKey, nAmount, fSubtractFeeFromAmount};
1040 vecSend.push_back(recipient);
1043 EnsureWalletIsUnlocked(pwallet);
1045 // Check funds
1046 CAmount nBalance = pwallet->GetLegacyBalance(ISMINE_SPENDABLE, nMinDepth, &strAccount);
1047 if (totalAmount > nBalance)
1048 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
1050 // Send
1051 CReserveKey keyChange(pwallet);
1052 CAmount nFeeRequired = 0;
1053 int nChangePosRet = -1;
1054 std::string strFailReason;
1055 bool fCreated = pwallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired, nChangePosRet, strFailReason, coin_control);
1056 if (!fCreated)
1057 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason);
1058 CValidationState state;
1059 if (!pwallet->CommitTransaction(wtx, keyChange, g_connman.get(), state)) {
1060 strFailReason = strprintf("Transaction commit failed:: %s", state.GetRejectReason());
1061 throw JSONRPCError(RPC_WALLET_ERROR, strFailReason);
1064 return wtx.GetHash().GetHex();
1067 // Defined in rpc/misc.cpp
1068 extern CScript _createmultisig_redeemScript(CWallet * const pwallet, const UniValue& params);
1070 UniValue addmultisigaddress(const JSONRPCRequest& request)
1072 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1073 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1074 return NullUniValue;
1077 if (request.fHelp || request.params.size() < 2 || request.params.size() > 3)
1079 std::string msg = "addmultisigaddress nrequired [\"key\",...] ( \"account\" )\n"
1080 "\nAdd a nrequired-to-sign multisignature address to the wallet.\n"
1081 "Each key is a Bitcoin address or hex-encoded public key.\n"
1082 "If 'account' is specified (DEPRECATED), assign address to that account.\n"
1084 "\nArguments:\n"
1085 "1. nrequired (numeric, required) The number of required signatures out of the n keys or addresses.\n"
1086 "2. \"keys\" (string, required) A json array of bitcoin addresses or hex-encoded public keys\n"
1087 " [\n"
1088 " \"address\" (string) bitcoin address or hex-encoded public key\n"
1089 " ...,\n"
1090 " ]\n"
1091 "3. \"account\" (string, optional) DEPRECATED. An account to assign the addresses to.\n"
1093 "\nResult:\n"
1094 "\"address\" (string) A bitcoin address associated with the keys.\n"
1096 "\nExamples:\n"
1097 "\nAdd a multisig address from 2 addresses\n"
1098 + HelpExampleCli("addmultisigaddress", "2 \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"") +
1099 "\nAs json rpc call\n"
1100 + HelpExampleRpc("addmultisigaddress", "2, \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"")
1102 throw std::runtime_error(msg);
1105 LOCK2(cs_main, pwallet->cs_wallet);
1107 std::string strAccount;
1108 if (request.params.size() > 2)
1109 strAccount = AccountFromValue(request.params[2]);
1111 // Construct using pay-to-script-hash:
1112 CScript inner = _createmultisig_redeemScript(pwallet, request.params);
1113 CScriptID innerID(inner);
1114 pwallet->AddCScript(inner);
1116 pwallet->SetAddressBook(innerID, strAccount, "send");
1117 return CBitcoinAddress(innerID).ToString();
1120 class Witnessifier : public boost::static_visitor<bool>
1122 public:
1123 CWallet * const pwallet;
1124 CScriptID result;
1126 Witnessifier(CWallet *_pwallet) : pwallet(_pwallet) {}
1128 bool operator()(const CNoDestination &dest) const { return false; }
1130 bool operator()(const CKeyID &keyID) {
1131 if (pwallet) {
1132 CScript basescript = GetScriptForDestination(keyID);
1133 isminetype typ;
1134 typ = IsMine(*pwallet, basescript, SIGVERSION_WITNESS_V0);
1135 if (typ != ISMINE_SPENDABLE && typ != ISMINE_WATCH_SOLVABLE)
1136 return false;
1137 CScript witscript = GetScriptForWitness(basescript);
1138 pwallet->AddCScript(witscript);
1139 result = CScriptID(witscript);
1140 return true;
1142 return false;
1145 bool operator()(const CScriptID &scriptID) {
1146 CScript subscript;
1147 if (pwallet && pwallet->GetCScript(scriptID, subscript)) {
1148 int witnessversion;
1149 std::vector<unsigned char> witprog;
1150 if (subscript.IsWitnessProgram(witnessversion, witprog)) {
1151 result = scriptID;
1152 return true;
1154 isminetype typ;
1155 typ = IsMine(*pwallet, subscript, SIGVERSION_WITNESS_V0);
1156 if (typ != ISMINE_SPENDABLE && typ != ISMINE_WATCH_SOLVABLE)
1157 return false;
1158 CScript witscript = GetScriptForWitness(subscript);
1159 pwallet->AddCScript(witscript);
1160 result = CScriptID(witscript);
1161 return true;
1163 return false;
1167 UniValue addwitnessaddress(const JSONRPCRequest& request)
1169 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1170 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1171 return NullUniValue;
1174 if (request.fHelp || request.params.size() < 1 || request.params.size() > 1)
1176 std::string msg = "addwitnessaddress \"address\"\n"
1177 "\nAdd a witness address for a script (with pubkey or redeemscript known).\n"
1178 "It returns the witness script.\n"
1180 "\nArguments:\n"
1181 "1. \"address\" (string, required) An address known to the wallet\n"
1183 "\nResult:\n"
1184 "\"witnessaddress\", (string) The value of the new address (P2SH of witness script).\n"
1185 "}\n"
1187 throw std::runtime_error(msg);
1191 LOCK(cs_main);
1192 if (!IsWitnessEnabled(chainActive.Tip(), Params().GetConsensus()) && !GetBoolArg("-walletprematurewitness", false)) {
1193 throw JSONRPCError(RPC_WALLET_ERROR, "Segregated witness not enabled on network");
1197 CBitcoinAddress address(request.params[0].get_str());
1198 if (!address.IsValid())
1199 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
1201 Witnessifier w(pwallet);
1202 CTxDestination dest = address.Get();
1203 bool ret = boost::apply_visitor(w, dest);
1204 if (!ret) {
1205 throw JSONRPCError(RPC_WALLET_ERROR, "Public key or redeemscript not known to wallet, or the key is uncompressed");
1208 pwallet->SetAddressBook(w.result, "", "receive");
1210 return CBitcoinAddress(w.result).ToString();
1213 struct tallyitem
1215 CAmount nAmount;
1216 int nConf;
1217 std::vector<uint256> txids;
1218 bool fIsWatchonly;
1219 tallyitem()
1221 nAmount = 0;
1222 nConf = std::numeric_limits<int>::max();
1223 fIsWatchonly = false;
1227 UniValue ListReceived(CWallet * const pwallet, const UniValue& params, bool fByAccounts)
1229 // Minimum confirmations
1230 int nMinDepth = 1;
1231 if (!params[0].isNull())
1232 nMinDepth = params[0].get_int();
1234 // Whether to include empty accounts
1235 bool fIncludeEmpty = false;
1236 if (!params[1].isNull())
1237 fIncludeEmpty = params[1].get_bool();
1239 isminefilter filter = ISMINE_SPENDABLE;
1240 if(!params[2].isNull())
1241 if(params[2].get_bool())
1242 filter = filter | ISMINE_WATCH_ONLY;
1244 // Tally
1245 std::map<CBitcoinAddress, tallyitem> mapTally;
1246 for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
1247 const CWalletTx& wtx = pairWtx.second;
1249 if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
1250 continue;
1252 int nDepth = wtx.GetDepthInMainChain();
1253 if (nDepth < nMinDepth)
1254 continue;
1256 for (const CTxOut& txout : wtx.tx->vout)
1258 CTxDestination address;
1259 if (!ExtractDestination(txout.scriptPubKey, address))
1260 continue;
1262 isminefilter mine = IsMine(*pwallet, address);
1263 if(!(mine & filter))
1264 continue;
1266 tallyitem& item = mapTally[address];
1267 item.nAmount += txout.nValue;
1268 item.nConf = std::min(item.nConf, nDepth);
1269 item.txids.push_back(wtx.GetHash());
1270 if (mine & ISMINE_WATCH_ONLY)
1271 item.fIsWatchonly = true;
1275 // Reply
1276 UniValue ret(UniValue::VARR);
1277 std::map<std::string, tallyitem> mapAccountTally;
1278 for (const std::pair<CBitcoinAddress, CAddressBookData>& item : pwallet->mapAddressBook) {
1279 const CBitcoinAddress& address = item.first;
1280 const std::string& strAccount = item.second.name;
1281 std::map<CBitcoinAddress, tallyitem>::iterator it = mapTally.find(address);
1282 if (it == mapTally.end() && !fIncludeEmpty)
1283 continue;
1285 CAmount nAmount = 0;
1286 int nConf = std::numeric_limits<int>::max();
1287 bool fIsWatchonly = false;
1288 if (it != mapTally.end())
1290 nAmount = (*it).second.nAmount;
1291 nConf = (*it).second.nConf;
1292 fIsWatchonly = (*it).second.fIsWatchonly;
1295 if (fByAccounts)
1297 tallyitem& _item = mapAccountTally[strAccount];
1298 _item.nAmount += nAmount;
1299 _item.nConf = std::min(_item.nConf, nConf);
1300 _item.fIsWatchonly = fIsWatchonly;
1302 else
1304 UniValue obj(UniValue::VOBJ);
1305 if(fIsWatchonly)
1306 obj.push_back(Pair("involvesWatchonly", true));
1307 obj.push_back(Pair("address", address.ToString()));
1308 obj.push_back(Pair("account", strAccount));
1309 obj.push_back(Pair("amount", ValueFromAmount(nAmount)));
1310 obj.push_back(Pair("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf)));
1311 if (!fByAccounts)
1312 obj.push_back(Pair("label", strAccount));
1313 UniValue transactions(UniValue::VARR);
1314 if (it != mapTally.end())
1316 for (const uint256& _item : (*it).second.txids)
1318 transactions.push_back(_item.GetHex());
1321 obj.push_back(Pair("txids", transactions));
1322 ret.push_back(obj);
1326 if (fByAccounts)
1328 for (std::map<std::string, tallyitem>::iterator it = mapAccountTally.begin(); it != mapAccountTally.end(); ++it)
1330 CAmount nAmount = (*it).second.nAmount;
1331 int nConf = (*it).second.nConf;
1332 UniValue obj(UniValue::VOBJ);
1333 if((*it).second.fIsWatchonly)
1334 obj.push_back(Pair("involvesWatchonly", true));
1335 obj.push_back(Pair("account", (*it).first));
1336 obj.push_back(Pair("amount", ValueFromAmount(nAmount)));
1337 obj.push_back(Pair("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf)));
1338 ret.push_back(obj);
1342 return ret;
1345 UniValue listreceivedbyaddress(const JSONRPCRequest& request)
1347 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1348 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1349 return NullUniValue;
1352 if (request.fHelp || request.params.size() > 3)
1353 throw std::runtime_error(
1354 "listreceivedbyaddress ( minconf include_empty include_watchonly)\n"
1355 "\nList balances by receiving address.\n"
1356 "\nArguments:\n"
1357 "1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included.\n"
1358 "2. include_empty (bool, optional, default=false) Whether to include addresses that haven't received any payments.\n"
1359 "3. include_watchonly (bool, optional, default=false) Whether to include watch-only addresses (see 'importaddress').\n"
1361 "\nResult:\n"
1362 "[\n"
1363 " {\n"
1364 " \"involvesWatchonly\" : true, (bool) Only returned if imported addresses were involved in transaction\n"
1365 " \"address\" : \"receivingaddress\", (string) The receiving address\n"
1366 " \"account\" : \"accountname\", (string) DEPRECATED. The account of the receiving address. The default account is \"\".\n"
1367 " \"amount\" : x.xxx, (numeric) The total amount in " + CURRENCY_UNIT + " received by the address\n"
1368 " \"confirmations\" : n, (numeric) The number of confirmations of the most recent transaction included\n"
1369 " \"label\" : \"label\", (string) A comment for the address/transaction, if any\n"
1370 " \"txids\": [\n"
1371 " n, (numeric) The ids of transactions received with the address \n"
1372 " ...\n"
1373 " ]\n"
1374 " }\n"
1375 " ,...\n"
1376 "]\n"
1378 "\nExamples:\n"
1379 + HelpExampleCli("listreceivedbyaddress", "")
1380 + HelpExampleCli("listreceivedbyaddress", "6 true")
1381 + HelpExampleRpc("listreceivedbyaddress", "6, true, true")
1384 LOCK2(cs_main, pwallet->cs_wallet);
1386 return ListReceived(pwallet, request.params, false);
1389 UniValue listreceivedbyaccount(const JSONRPCRequest& request)
1391 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1392 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1393 return NullUniValue;
1396 if (request.fHelp || request.params.size() > 3)
1397 throw std::runtime_error(
1398 "listreceivedbyaccount ( minconf include_empty include_watchonly)\n"
1399 "\nDEPRECATED. List balances by account.\n"
1400 "\nArguments:\n"
1401 "1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included.\n"
1402 "2. include_empty (bool, optional, default=false) Whether to include accounts that haven't received any payments.\n"
1403 "3. include_watchonly (bool, optional, default=false) Whether to include watch-only addresses (see 'importaddress').\n"
1405 "\nResult:\n"
1406 "[\n"
1407 " {\n"
1408 " \"involvesWatchonly\" : true, (bool) Only returned if imported addresses were involved in transaction\n"
1409 " \"account\" : \"accountname\", (string) The account name of the receiving account\n"
1410 " \"amount\" : x.xxx, (numeric) The total amount received by addresses with this account\n"
1411 " \"confirmations\" : n, (numeric) The number of confirmations of the most recent transaction included\n"
1412 " \"label\" : \"label\" (string) A comment for the address/transaction, if any\n"
1413 " }\n"
1414 " ,...\n"
1415 "]\n"
1417 "\nExamples:\n"
1418 + HelpExampleCli("listreceivedbyaccount", "")
1419 + HelpExampleCli("listreceivedbyaccount", "6 true")
1420 + HelpExampleRpc("listreceivedbyaccount", "6, true, true")
1423 LOCK2(cs_main, pwallet->cs_wallet);
1425 return ListReceived(pwallet, request.params, true);
1428 static void MaybePushAddress(UniValue & entry, const CTxDestination &dest)
1430 CBitcoinAddress addr;
1431 if (addr.Set(dest))
1432 entry.push_back(Pair("address", addr.ToString()));
1436 * List transactions based on the given criteria.
1438 * @param pwallet The wallet.
1439 * @param wtx The wallet transaction.
1440 * @param strAccount The account, if any, or "*" for all.
1441 * @param nMinDepth The minimum confirmation depth.
1442 * @param fLong Whether to include the JSON version of the transaction.
1443 * @param ret The UniValue into which the result is stored.
1444 * @param filter The "is mine" filter bool.
1446 void ListTransactions(CWallet* const pwallet, const CWalletTx& wtx, const std::string& strAccount, int nMinDepth, bool fLong, UniValue& ret, const isminefilter& filter)
1448 CAmount nFee;
1449 std::string strSentAccount;
1450 std::list<COutputEntry> listReceived;
1451 std::list<COutputEntry> listSent;
1453 wtx.GetAmounts(listReceived, listSent, nFee, strSentAccount, filter);
1455 bool fAllAccounts = (strAccount == std::string("*"));
1456 bool involvesWatchonly = wtx.IsFromMe(ISMINE_WATCH_ONLY);
1458 // Sent
1459 if ((!listSent.empty() || nFee != 0) && (fAllAccounts || strAccount == strSentAccount))
1461 for (const COutputEntry& s : listSent)
1463 UniValue entry(UniValue::VOBJ);
1464 if (involvesWatchonly || (::IsMine(*pwallet, s.destination) & ISMINE_WATCH_ONLY)) {
1465 entry.push_back(Pair("involvesWatchonly", true));
1467 entry.push_back(Pair("account", strSentAccount));
1468 MaybePushAddress(entry, s.destination);
1469 entry.push_back(Pair("category", "send"));
1470 entry.push_back(Pair("amount", ValueFromAmount(-s.amount)));
1471 if (pwallet->mapAddressBook.count(s.destination)) {
1472 entry.push_back(Pair("label", pwallet->mapAddressBook[s.destination].name));
1474 entry.push_back(Pair("vout", s.vout));
1475 entry.push_back(Pair("fee", ValueFromAmount(-nFee)));
1476 if (fLong)
1477 WalletTxToJSON(wtx, entry);
1478 entry.push_back(Pair("abandoned", wtx.isAbandoned()));
1479 ret.push_back(entry);
1483 // Received
1484 if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth)
1486 for (const COutputEntry& r : listReceived)
1488 std::string account;
1489 if (pwallet->mapAddressBook.count(r.destination)) {
1490 account = pwallet->mapAddressBook[r.destination].name;
1492 if (fAllAccounts || (account == strAccount))
1494 UniValue entry(UniValue::VOBJ);
1495 if (involvesWatchonly || (::IsMine(*pwallet, r.destination) & ISMINE_WATCH_ONLY)) {
1496 entry.push_back(Pair("involvesWatchonly", true));
1498 entry.push_back(Pair("account", account));
1499 MaybePushAddress(entry, r.destination);
1500 if (wtx.IsCoinBase())
1502 if (wtx.GetDepthInMainChain() < 1)
1503 entry.push_back(Pair("category", "orphan"));
1504 else if (wtx.GetBlocksToMaturity() > 0)
1505 entry.push_back(Pair("category", "immature"));
1506 else
1507 entry.push_back(Pair("category", "generate"));
1509 else
1511 entry.push_back(Pair("category", "receive"));
1513 entry.push_back(Pair("amount", ValueFromAmount(r.amount)));
1514 if (pwallet->mapAddressBook.count(r.destination)) {
1515 entry.push_back(Pair("label", account));
1517 entry.push_back(Pair("vout", r.vout));
1518 if (fLong)
1519 WalletTxToJSON(wtx, entry);
1520 ret.push_back(entry);
1526 void AcentryToJSON(const CAccountingEntry& acentry, const std::string& strAccount, UniValue& ret)
1528 bool fAllAccounts = (strAccount == std::string("*"));
1530 if (fAllAccounts || acentry.strAccount == strAccount)
1532 UniValue entry(UniValue::VOBJ);
1533 entry.push_back(Pair("account", acentry.strAccount));
1534 entry.push_back(Pair("category", "move"));
1535 entry.push_back(Pair("time", acentry.nTime));
1536 entry.push_back(Pair("amount", ValueFromAmount(acentry.nCreditDebit)));
1537 entry.push_back(Pair("otheraccount", acentry.strOtherAccount));
1538 entry.push_back(Pair("comment", acentry.strComment));
1539 ret.push_back(entry);
1543 UniValue listtransactions(const JSONRPCRequest& request)
1545 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1546 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1547 return NullUniValue;
1550 if (request.fHelp || request.params.size() > 4)
1551 throw std::runtime_error(
1552 "listtransactions ( \"account\" count skip include_watchonly)\n"
1553 "\nReturns up to 'count' most recent transactions skipping the first 'from' transactions for account 'account'.\n"
1554 "\nArguments:\n"
1555 "1. \"account\" (string, optional) DEPRECATED. The account name. Should be \"*\".\n"
1556 "2. count (numeric, optional, default=10) The number of transactions to return\n"
1557 "3. skip (numeric, optional, default=0) The number of transactions to skip\n"
1558 "4. include_watchonly (bool, optional, default=false) Include transactions to watch-only addresses (see 'importaddress')\n"
1559 "\nResult:\n"
1560 "[\n"
1561 " {\n"
1562 " \"account\":\"accountname\", (string) DEPRECATED. The account name associated with the transaction. \n"
1563 " It will be \"\" for the default account.\n"
1564 " \"address\":\"address\", (string) The bitcoin address of the transaction. Not present for \n"
1565 " move transactions (category = move).\n"
1566 " \"category\":\"send|receive|move\", (string) The transaction category. 'move' is a local (off blockchain)\n"
1567 " transaction between accounts, and not associated with an address,\n"
1568 " transaction id or block. 'send' and 'receive' transactions are \n"
1569 " associated with an address, transaction id and block details\n"
1570 " \"amount\": x.xxx, (numeric) The amount in " + CURRENCY_UNIT + ". This is negative for the 'send' category, and for the\n"
1571 " 'move' category for moves outbound. It is positive for the 'receive' category,\n"
1572 " and for the 'move' category for inbound funds.\n"
1573 " \"label\": \"label\", (string) A comment for the address/transaction, if any\n"
1574 " \"vout\": n, (numeric) the vout value\n"
1575 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the \n"
1576 " 'send' category of transactions.\n"
1577 " \"confirmations\": n, (numeric) The number of confirmations for the transaction. Available for 'send' and \n"
1578 " 'receive' category of transactions. Negative confirmations indicate the\n"
1579 " transaction conflicts with the block chain\n"
1580 " \"trusted\": xxx, (bool) Whether we consider the outputs of this unconfirmed transaction safe to spend.\n"
1581 " \"blockhash\": \"hashvalue\", (string) The block hash containing the transaction. Available for 'send' and 'receive'\n"
1582 " category of transactions.\n"
1583 " \"blockindex\": n, (numeric) The index of the transaction in the block that includes it. Available for 'send' and 'receive'\n"
1584 " category of transactions.\n"
1585 " \"blocktime\": xxx, (numeric) The block time in seconds since epoch (1 Jan 1970 GMT).\n"
1586 " \"txid\": \"transactionid\", (string) The transaction id. Available for 'send' and 'receive' category of transactions.\n"
1587 " \"time\": xxx, (numeric) The transaction time in seconds since epoch (midnight Jan 1 1970 GMT).\n"
1588 " \"timereceived\": xxx, (numeric) The time received in seconds since epoch (midnight Jan 1 1970 GMT). Available \n"
1589 " for 'send' and 'receive' category of transactions.\n"
1590 " \"comment\": \"...\", (string) If a comment is associated with the transaction.\n"
1591 " \"otheraccount\": \"accountname\", (string) DEPRECATED. For the 'move' category of transactions, the account the funds came \n"
1592 " from (for receiving funds, positive amounts), or went to (for sending funds,\n"
1593 " negative amounts).\n"
1594 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1595 " may be unknown for unconfirmed transactions not in the mempool\n"
1596 " \"abandoned\": xxx (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
1597 " 'send' category of transactions.\n"
1598 " }\n"
1599 "]\n"
1601 "\nExamples:\n"
1602 "\nList the most recent 10 transactions in the systems\n"
1603 + HelpExampleCli("listtransactions", "") +
1604 "\nList transactions 100 to 120\n"
1605 + HelpExampleCli("listtransactions", "\"*\" 20 100") +
1606 "\nAs a json rpc call\n"
1607 + HelpExampleRpc("listtransactions", "\"*\", 20, 100")
1610 LOCK2(cs_main, pwallet->cs_wallet);
1612 std::string strAccount = "*";
1613 if (!request.params[0].isNull())
1614 strAccount = request.params[0].get_str();
1615 int nCount = 10;
1616 if (!request.params[1].isNull())
1617 nCount = request.params[1].get_int();
1618 int nFrom = 0;
1619 if (!request.params[2].isNull())
1620 nFrom = request.params[2].get_int();
1621 isminefilter filter = ISMINE_SPENDABLE;
1622 if(!request.params[3].isNull())
1623 if(request.params[3].get_bool())
1624 filter = filter | ISMINE_WATCH_ONLY;
1626 if (nCount < 0)
1627 throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative count");
1628 if (nFrom < 0)
1629 throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative from");
1631 UniValue ret(UniValue::VARR);
1633 const CWallet::TxItems & txOrdered = pwallet->wtxOrdered;
1635 // iterate backwards until we have nCount items to return:
1636 for (CWallet::TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
1638 CWalletTx *const pwtx = (*it).second.first;
1639 if (pwtx != 0)
1640 ListTransactions(pwallet, *pwtx, strAccount, 0, true, ret, filter);
1641 CAccountingEntry *const pacentry = (*it).second.second;
1642 if (pacentry != 0)
1643 AcentryToJSON(*pacentry, strAccount, ret);
1645 if ((int)ret.size() >= (nCount+nFrom)) break;
1647 // ret is newest to oldest
1649 if (nFrom > (int)ret.size())
1650 nFrom = ret.size();
1651 if ((nFrom + nCount) > (int)ret.size())
1652 nCount = ret.size() - nFrom;
1654 std::vector<UniValue> arrTmp = ret.getValues();
1656 std::vector<UniValue>::iterator first = arrTmp.begin();
1657 std::advance(first, nFrom);
1658 std::vector<UniValue>::iterator last = arrTmp.begin();
1659 std::advance(last, nFrom+nCount);
1661 if (last != arrTmp.end()) arrTmp.erase(last, arrTmp.end());
1662 if (first != arrTmp.begin()) arrTmp.erase(arrTmp.begin(), first);
1664 std::reverse(arrTmp.begin(), arrTmp.end()); // Return oldest to newest
1666 ret.clear();
1667 ret.setArray();
1668 ret.push_backV(arrTmp);
1670 return ret;
1673 UniValue listaccounts(const JSONRPCRequest& request)
1675 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1676 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1677 return NullUniValue;
1680 if (request.fHelp || request.params.size() > 2)
1681 throw std::runtime_error(
1682 "listaccounts ( minconf include_watchonly)\n"
1683 "\nDEPRECATED. Returns Object that has account names as keys, account balances as values.\n"
1684 "\nArguments:\n"
1685 "1. minconf (numeric, optional, default=1) Only include transactions with at least this many confirmations\n"
1686 "2. include_watchonly (bool, optional, default=false) Include balances in watch-only addresses (see 'importaddress')\n"
1687 "\nResult:\n"
1688 "{ (json object where keys are account names, and values are numeric balances\n"
1689 " \"account\": x.xxx, (numeric) The property name is the account name, and the value is the total balance for the account.\n"
1690 " ...\n"
1691 "}\n"
1692 "\nExamples:\n"
1693 "\nList account balances where there at least 1 confirmation\n"
1694 + HelpExampleCli("listaccounts", "") +
1695 "\nList account balances including zero confirmation transactions\n"
1696 + HelpExampleCli("listaccounts", "0") +
1697 "\nList account balances for 6 or more confirmations\n"
1698 + HelpExampleCli("listaccounts", "6") +
1699 "\nAs json rpc call\n"
1700 + HelpExampleRpc("listaccounts", "6")
1703 LOCK2(cs_main, pwallet->cs_wallet);
1705 int nMinDepth = 1;
1706 if (request.params.size() > 0)
1707 nMinDepth = request.params[0].get_int();
1708 isminefilter includeWatchonly = ISMINE_SPENDABLE;
1709 if(request.params.size() > 1)
1710 if(request.params[1].get_bool())
1711 includeWatchonly = includeWatchonly | ISMINE_WATCH_ONLY;
1713 std::map<std::string, CAmount> mapAccountBalances;
1714 for (const std::pair<CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) {
1715 if (IsMine(*pwallet, entry.first) & includeWatchonly) { // This address belongs to me
1716 mapAccountBalances[entry.second.name] = 0;
1720 for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
1721 const CWalletTx& wtx = pairWtx.second;
1722 CAmount nFee;
1723 std::string strSentAccount;
1724 std::list<COutputEntry> listReceived;
1725 std::list<COutputEntry> listSent;
1726 int nDepth = wtx.GetDepthInMainChain();
1727 if (wtx.GetBlocksToMaturity() > 0 || nDepth < 0)
1728 continue;
1729 wtx.GetAmounts(listReceived, listSent, nFee, strSentAccount, includeWatchonly);
1730 mapAccountBalances[strSentAccount] -= nFee;
1731 for (const COutputEntry& s : listSent)
1732 mapAccountBalances[strSentAccount] -= s.amount;
1733 if (nDepth >= nMinDepth)
1735 for (const COutputEntry& r : listReceived)
1736 if (pwallet->mapAddressBook.count(r.destination)) {
1737 mapAccountBalances[pwallet->mapAddressBook[r.destination].name] += r.amount;
1739 else
1740 mapAccountBalances[""] += r.amount;
1744 const std::list<CAccountingEntry>& acentries = pwallet->laccentries;
1745 for (const CAccountingEntry& entry : acentries)
1746 mapAccountBalances[entry.strAccount] += entry.nCreditDebit;
1748 UniValue ret(UniValue::VOBJ);
1749 for (const std::pair<std::string, CAmount>& accountBalance : mapAccountBalances) {
1750 ret.push_back(Pair(accountBalance.first, ValueFromAmount(accountBalance.second)));
1752 return ret;
1755 UniValue listsinceblock(const JSONRPCRequest& request)
1757 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1758 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1759 return NullUniValue;
1762 if (request.fHelp || request.params.size() > 4)
1763 throw std::runtime_error(
1764 "listsinceblock ( \"blockhash\" target_confirmations include_watchonly include_removed )\n"
1765 "\nGet all transactions in blocks since block [blockhash], or all transactions if omitted.\n"
1766 "If \"blockhash\" is no longer a part of the main chain, transactions from the fork point onward are included.\n"
1767 "Additionally, if include_removed is set, transactions affecting the wallet which were removed are returned in the \"removed\" array.\n"
1768 "\nArguments:\n"
1769 "1. \"blockhash\" (string, optional) The block hash to list transactions since\n"
1770 "2. target_confirmations: (numeric, optional, default=1) Return the nth block hash from the main chain. e.g. 1 would mean the best block hash. Note: this is not used as a filter, but only affects [lastblock] in the return value\n"
1771 "3. include_watchonly: (bool, optional, default=false) Include transactions to watch-only addresses (see 'importaddress')\n"
1772 "4. include_removed: (bool, optional, default=true) Show transactions that were removed due to a reorg in the \"removed\" array\n"
1773 " (not guaranteed to work on pruned nodes)\n"
1774 "\nResult:\n"
1775 "{\n"
1776 " \"transactions\": [\n"
1777 " \"account\":\"accountname\", (string) DEPRECATED. The account name associated with the transaction. Will be \"\" for the default account.\n"
1778 " \"address\":\"address\", (string) The bitcoin address of the transaction. Not present for move transactions (category = move).\n"
1779 " \"category\":\"send|receive\", (string) The transaction category. 'send' has negative amounts, 'receive' has positive amounts.\n"
1780 " \"amount\": x.xxx, (numeric) The amount in " + CURRENCY_UNIT + ". This is negative for the 'send' category, and for the 'move' category for moves \n"
1781 " outbound. It is positive for the 'receive' category, and for the 'move' category for inbound funds.\n"
1782 " \"vout\" : n, (numeric) the vout value\n"
1783 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the 'send' category of transactions.\n"
1784 " \"confirmations\": n, (numeric) The number of confirmations for the transaction. Available for 'send' and 'receive' category of transactions.\n"
1785 " When it's < 0, it means the transaction conflicted that many blocks ago.\n"
1786 " \"blockhash\": \"hashvalue\", (string) The block hash containing the transaction. Available for 'send' and 'receive' category of transactions.\n"
1787 " \"blockindex\": n, (numeric) The index of the transaction in the block that includes it. Available for 'send' and 'receive' category of transactions.\n"
1788 " \"blocktime\": xxx, (numeric) The block time in seconds since epoch (1 Jan 1970 GMT).\n"
1789 " \"txid\": \"transactionid\", (string) The transaction id. Available for 'send' and 'receive' category of transactions.\n"
1790 " \"time\": xxx, (numeric) The transaction time in seconds since epoch (Jan 1 1970 GMT).\n"
1791 " \"timereceived\": xxx, (numeric) The time received in seconds since epoch (Jan 1 1970 GMT). Available for 'send' and 'receive' category of transactions.\n"
1792 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1793 " may be unknown for unconfirmed transactions not in the mempool\n"
1794 " \"abandoned\": xxx, (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the 'send' category of transactions.\n"
1795 " \"comment\": \"...\", (string) If a comment is associated with the transaction.\n"
1796 " \"label\" : \"label\" (string) A comment for the address/transaction, if any\n"
1797 " \"to\": \"...\", (string) If a comment to is associated with the transaction.\n"
1798 " ],\n"
1799 " \"removed\": [\n"
1800 " <structure is the same as \"transactions\" above, only present if include_removed=true>\n"
1801 " Note: transactions that were readded in the active chain will appear as-is in this array, and may thus have a positive confirmation count.\n"
1802 " ],\n"
1803 " \"lastblock\": \"lastblockhash\" (string) The hash of the block (target_confirmations-1) from the best block on the main chain. This is typically used to feed back into listsinceblock the next time you call it. So you would generally use a target_confirmations of say 6, so you will be continually re-notified of transactions until they've reached 6 confirmations plus any new ones\n"
1804 "}\n"
1805 "\nExamples:\n"
1806 + HelpExampleCli("listsinceblock", "")
1807 + HelpExampleCli("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\" 6")
1808 + HelpExampleRpc("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\", 6")
1811 LOCK2(cs_main, pwallet->cs_wallet);
1813 const CBlockIndex* pindex = NULL; // Block index of the specified block or the common ancestor, if the block provided was in a deactivated chain.
1814 const CBlockIndex* paltindex = NULL; // Block index of the specified block, even if it's in a deactivated chain.
1815 int target_confirms = 1;
1816 isminefilter filter = ISMINE_SPENDABLE;
1818 if (!request.params[0].isNull()) {
1819 uint256 blockId;
1821 blockId.SetHex(request.params[0].get_str());
1822 BlockMap::iterator it = mapBlockIndex.find(blockId);
1823 if (it != mapBlockIndex.end()) {
1824 paltindex = pindex = it->second;
1825 if (chainActive[pindex->nHeight] != pindex) {
1826 // the block being asked for is a part of a deactivated chain;
1827 // we don't want to depend on its perceived height in the block
1828 // chain, we want to instead use the last common ancestor
1829 pindex = chainActive.FindFork(pindex);
1834 if (!request.params[1].isNull()) {
1835 target_confirms = request.params[1].get_int();
1837 if (target_confirms < 1) {
1838 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter");
1842 if (!request.params[2].isNull() && request.params[2].get_bool()) {
1843 filter = filter | ISMINE_WATCH_ONLY;
1846 bool include_removed = (request.params[3].isNull() || request.params[3].get_bool());
1848 int depth = pindex ? (1 + chainActive.Height() - pindex->nHeight) : -1;
1850 UniValue transactions(UniValue::VARR);
1852 for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
1853 CWalletTx tx = pairWtx.second;
1855 if (depth == -1 || tx.GetDepthInMainChain() < depth) {
1856 ListTransactions(pwallet, tx, "*", 0, true, transactions, filter);
1860 // when a reorg'd block is requested, we also list any relevant transactions
1861 // in the blocks of the chain that was detached
1862 UniValue removed(UniValue::VARR);
1863 while (include_removed && paltindex && paltindex != pindex) {
1864 CBlock block;
1865 if (!ReadBlockFromDisk(block, paltindex, Params().GetConsensus())) {
1866 throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
1868 for (const CTransactionRef& tx : block.vtx) {
1869 if (pwallet->mapWallet.count(tx->GetHash()) > 0) {
1870 // We want all transactions regardless of confirmation count to appear here,
1871 // even negative confirmation ones, hence the big negative.
1872 ListTransactions(pwallet, pwallet->mapWallet[tx->GetHash()], "*", -100000000, true, removed, filter);
1875 paltindex = paltindex->pprev;
1878 CBlockIndex *pblockLast = chainActive[chainActive.Height() + 1 - target_confirms];
1879 uint256 lastblock = pblockLast ? pblockLast->GetBlockHash() : uint256();
1881 UniValue ret(UniValue::VOBJ);
1882 ret.push_back(Pair("transactions", transactions));
1883 if (include_removed) ret.push_back(Pair("removed", removed));
1884 ret.push_back(Pair("lastblock", lastblock.GetHex()));
1886 return ret;
1889 UniValue gettransaction(const JSONRPCRequest& request)
1891 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1892 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1893 return NullUniValue;
1896 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
1897 throw std::runtime_error(
1898 "gettransaction \"txid\" ( include_watchonly )\n"
1899 "\nGet detailed information about in-wallet transaction <txid>\n"
1900 "\nArguments:\n"
1901 "1. \"txid\" (string, required) The transaction id\n"
1902 "2. \"include_watchonly\" (bool, optional, default=false) Whether to include watch-only addresses in balance calculation and details[]\n"
1903 "\nResult:\n"
1904 "{\n"
1905 " \"amount\" : x.xxx, (numeric) The transaction amount in " + CURRENCY_UNIT + "\n"
1906 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the \n"
1907 " 'send' category of transactions.\n"
1908 " \"confirmations\" : n, (numeric) The number of confirmations\n"
1909 " \"blockhash\" : \"hash\", (string) The block hash\n"
1910 " \"blockindex\" : xx, (numeric) The index of the transaction in the block that includes it\n"
1911 " \"blocktime\" : ttt, (numeric) The time in seconds since epoch (1 Jan 1970 GMT)\n"
1912 " \"txid\" : \"transactionid\", (string) The transaction id.\n"
1913 " \"time\" : ttt, (numeric) The transaction time in seconds since epoch (1 Jan 1970 GMT)\n"
1914 " \"timereceived\" : ttt, (numeric) The time received in seconds since epoch (1 Jan 1970 GMT)\n"
1915 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1916 " may be unknown for unconfirmed transactions not in the mempool\n"
1917 " \"details\" : [\n"
1918 " {\n"
1919 " \"account\" : \"accountname\", (string) DEPRECATED. The account name involved in the transaction, can be \"\" for the default account.\n"
1920 " \"address\" : \"address\", (string) The bitcoin address involved in the transaction\n"
1921 " \"category\" : \"send|receive\", (string) The category, either 'send' or 'receive'\n"
1922 " \"amount\" : x.xxx, (numeric) The amount in " + CURRENCY_UNIT + "\n"
1923 " \"label\" : \"label\", (string) A comment for the address/transaction, if any\n"
1924 " \"vout\" : n, (numeric) the vout value\n"
1925 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the \n"
1926 " 'send' category of transactions.\n"
1927 " \"abandoned\": xxx (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
1928 " 'send' category of transactions.\n"
1929 " }\n"
1930 " ,...\n"
1931 " ],\n"
1932 " \"hex\" : \"data\" (string) Raw data for transaction\n"
1933 "}\n"
1935 "\nExamples:\n"
1936 + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
1937 + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\" true")
1938 + HelpExampleRpc("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
1941 LOCK2(cs_main, pwallet->cs_wallet);
1943 uint256 hash;
1944 hash.SetHex(request.params[0].get_str());
1946 isminefilter filter = ISMINE_SPENDABLE;
1947 if(!request.params[1].isNull())
1948 if(request.params[1].get_bool())
1949 filter = filter | ISMINE_WATCH_ONLY;
1951 UniValue entry(UniValue::VOBJ);
1952 if (!pwallet->mapWallet.count(hash)) {
1953 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid or non-wallet transaction id");
1955 const CWalletTx& wtx = pwallet->mapWallet[hash];
1957 CAmount nCredit = wtx.GetCredit(filter);
1958 CAmount nDebit = wtx.GetDebit(filter);
1959 CAmount nNet = nCredit - nDebit;
1960 CAmount nFee = (wtx.IsFromMe(filter) ? wtx.tx->GetValueOut() - nDebit : 0);
1962 entry.push_back(Pair("amount", ValueFromAmount(nNet - nFee)));
1963 if (wtx.IsFromMe(filter))
1964 entry.push_back(Pair("fee", ValueFromAmount(nFee)));
1966 WalletTxToJSON(wtx, entry);
1968 UniValue details(UniValue::VARR);
1969 ListTransactions(pwallet, wtx, "*", 0, false, details, filter);
1970 entry.push_back(Pair("details", details));
1972 std::string strHex = EncodeHexTx(static_cast<CTransaction>(wtx), RPCSerializationFlags());
1973 entry.push_back(Pair("hex", strHex));
1975 return entry;
1978 UniValue abandontransaction(const JSONRPCRequest& request)
1980 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1981 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1982 return NullUniValue;
1985 if (request.fHelp || request.params.size() != 1)
1986 throw std::runtime_error(
1987 "abandontransaction \"txid\"\n"
1988 "\nMark in-wallet transaction <txid> as abandoned\n"
1989 "This will mark this transaction and all its in-wallet descendants as abandoned which will allow\n"
1990 "for their inputs to be respent. It can be used to replace \"stuck\" or evicted transactions.\n"
1991 "It only works on transactions which are not included in a block and are not currently in the mempool.\n"
1992 "It has no effect on transactions which are already conflicted or abandoned.\n"
1993 "\nArguments:\n"
1994 "1. \"txid\" (string, required) The transaction id\n"
1995 "\nResult:\n"
1996 "\nExamples:\n"
1997 + HelpExampleCli("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
1998 + HelpExampleRpc("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
2001 LOCK2(cs_main, pwallet->cs_wallet);
2003 uint256 hash;
2004 hash.SetHex(request.params[0].get_str());
2006 if (!pwallet->mapWallet.count(hash)) {
2007 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid or non-wallet transaction id");
2009 if (!pwallet->AbandonTransaction(hash)) {
2010 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not eligible for abandonment");
2013 return NullUniValue;
2017 UniValue backupwallet(const JSONRPCRequest& request)
2019 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2020 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2021 return NullUniValue;
2024 if (request.fHelp || request.params.size() != 1)
2025 throw std::runtime_error(
2026 "backupwallet \"destination\"\n"
2027 "\nSafely copies current wallet file to destination, which can be a directory or a path with filename.\n"
2028 "\nArguments:\n"
2029 "1. \"destination\" (string) The destination directory or file\n"
2030 "\nExamples:\n"
2031 + HelpExampleCli("backupwallet", "\"backup.dat\"")
2032 + HelpExampleRpc("backupwallet", "\"backup.dat\"")
2035 LOCK2(cs_main, pwallet->cs_wallet);
2037 std::string strDest = request.params[0].get_str();
2038 if (!pwallet->BackupWallet(strDest)) {
2039 throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!");
2042 return NullUniValue;
2046 UniValue keypoolrefill(const JSONRPCRequest& request)
2048 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2049 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2050 return NullUniValue;
2053 if (request.fHelp || request.params.size() > 1)
2054 throw std::runtime_error(
2055 "keypoolrefill ( newsize )\n"
2056 "\nFills the keypool."
2057 + HelpRequiringPassphrase(pwallet) + "\n"
2058 "\nArguments\n"
2059 "1. newsize (numeric, optional, default=100) The new keypool size\n"
2060 "\nExamples:\n"
2061 + HelpExampleCli("keypoolrefill", "")
2062 + HelpExampleRpc("keypoolrefill", "")
2065 LOCK2(cs_main, pwallet->cs_wallet);
2067 // 0 is interpreted by TopUpKeyPool() as the default keypool size given by -keypool
2068 unsigned int kpSize = 0;
2069 if (!request.params[0].isNull()) {
2070 if (request.params[0].get_int() < 0)
2071 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected valid size.");
2072 kpSize = (unsigned int)request.params[0].get_int();
2075 EnsureWalletIsUnlocked(pwallet);
2076 pwallet->TopUpKeyPool(kpSize);
2078 if (pwallet->GetKeyPoolSize() < kpSize) {
2079 throw JSONRPCError(RPC_WALLET_ERROR, "Error refreshing keypool.");
2082 return NullUniValue;
2086 static void LockWallet(CWallet* pWallet)
2088 LOCK(pWallet->cs_wallet);
2089 pWallet->nRelockTime = 0;
2090 pWallet->Lock();
2093 UniValue walletpassphrase(const JSONRPCRequest& request)
2095 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2096 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2097 return NullUniValue;
2100 if (pwallet->IsCrypted() && (request.fHelp || request.params.size() != 2)) {
2101 throw std::runtime_error(
2102 "walletpassphrase \"passphrase\" timeout\n"
2103 "\nStores the wallet decryption key in memory for 'timeout' seconds.\n"
2104 "This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
2105 "\nArguments:\n"
2106 "1. \"passphrase\" (string, required) The wallet passphrase\n"
2107 "2. timeout (numeric, required) The time to keep the decryption key in seconds.\n"
2108 "\nNote:\n"
2109 "Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
2110 "time that overrides the old one.\n"
2111 "\nExamples:\n"
2112 "\nUnlock the wallet for 60 seconds\n"
2113 + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 60") +
2114 "\nLock the wallet again (before 60 seconds)\n"
2115 + HelpExampleCli("walletlock", "") +
2116 "\nAs json rpc call\n"
2117 + HelpExampleRpc("walletpassphrase", "\"my pass phrase\", 60")
2121 LOCK2(cs_main, pwallet->cs_wallet);
2123 if (request.fHelp)
2124 return true;
2125 if (!pwallet->IsCrypted()) {
2126 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrase was called.");
2129 // Note that the walletpassphrase is stored in request.params[0] which is not mlock()ed
2130 SecureString strWalletPass;
2131 strWalletPass.reserve(100);
2132 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
2133 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2134 strWalletPass = request.params[0].get_str().c_str();
2136 if (strWalletPass.length() > 0)
2138 if (!pwallet->Unlock(strWalletPass)) {
2139 throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
2142 else
2143 throw std::runtime_error(
2144 "walletpassphrase <passphrase> <timeout>\n"
2145 "Stores the wallet decryption key in memory for <timeout> seconds.");
2147 pwallet->TopUpKeyPool();
2149 int64_t nSleepTime = request.params[1].get_int64();
2150 pwallet->nRelockTime = GetTime() + nSleepTime;
2151 RPCRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), boost::bind(LockWallet, pwallet), nSleepTime);
2153 return NullUniValue;
2157 UniValue walletpassphrasechange(const JSONRPCRequest& request)
2159 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2160 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2161 return NullUniValue;
2164 if (pwallet->IsCrypted() && (request.fHelp || request.params.size() != 2)) {
2165 throw std::runtime_error(
2166 "walletpassphrasechange \"oldpassphrase\" \"newpassphrase\"\n"
2167 "\nChanges the wallet passphrase from 'oldpassphrase' to 'newpassphrase'.\n"
2168 "\nArguments:\n"
2169 "1. \"oldpassphrase\" (string) The current passphrase\n"
2170 "2. \"newpassphrase\" (string) The new passphrase\n"
2171 "\nExamples:\n"
2172 + HelpExampleCli("walletpassphrasechange", "\"old one\" \"new one\"")
2173 + HelpExampleRpc("walletpassphrasechange", "\"old one\", \"new one\"")
2177 LOCK2(cs_main, pwallet->cs_wallet);
2179 if (request.fHelp)
2180 return true;
2181 if (!pwallet->IsCrypted()) {
2182 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrasechange was called.");
2185 // TODO: get rid of these .c_str() calls by implementing SecureString::operator=(std::string)
2186 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2187 SecureString strOldWalletPass;
2188 strOldWalletPass.reserve(100);
2189 strOldWalletPass = request.params[0].get_str().c_str();
2191 SecureString strNewWalletPass;
2192 strNewWalletPass.reserve(100);
2193 strNewWalletPass = request.params[1].get_str().c_str();
2195 if (strOldWalletPass.length() < 1 || strNewWalletPass.length() < 1)
2196 throw std::runtime_error(
2197 "walletpassphrasechange <oldpassphrase> <newpassphrase>\n"
2198 "Changes the wallet passphrase from <oldpassphrase> to <newpassphrase>.");
2200 if (!pwallet->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass)) {
2201 throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
2204 return NullUniValue;
2208 UniValue walletlock(const JSONRPCRequest& request)
2210 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2211 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2212 return NullUniValue;
2215 if (pwallet->IsCrypted() && (request.fHelp || request.params.size() != 0)) {
2216 throw std::runtime_error(
2217 "walletlock\n"
2218 "\nRemoves the wallet encryption key from memory, locking the wallet.\n"
2219 "After calling this method, you will need to call walletpassphrase again\n"
2220 "before being able to call any methods which require the wallet to be unlocked.\n"
2221 "\nExamples:\n"
2222 "\nSet the passphrase for 2 minutes to perform a transaction\n"
2223 + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 120") +
2224 "\nPerform a send (requires passphrase set)\n"
2225 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 1.0") +
2226 "\nClear the passphrase since we are done before 2 minutes is up\n"
2227 + HelpExampleCli("walletlock", "") +
2228 "\nAs json rpc call\n"
2229 + HelpExampleRpc("walletlock", "")
2233 LOCK2(cs_main, pwallet->cs_wallet);
2235 if (request.fHelp)
2236 return true;
2237 if (!pwallet->IsCrypted()) {
2238 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletlock was called.");
2241 pwallet->Lock();
2242 pwallet->nRelockTime = 0;
2244 return NullUniValue;
2248 UniValue encryptwallet(const JSONRPCRequest& request)
2250 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2251 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2252 return NullUniValue;
2255 if (!pwallet->IsCrypted() && (request.fHelp || request.params.size() != 1)) {
2256 throw std::runtime_error(
2257 "encryptwallet \"passphrase\"\n"
2258 "\nEncrypts the wallet with 'passphrase'. This is for first time encryption.\n"
2259 "After this, any calls that interact with private keys such as sending or signing \n"
2260 "will require the passphrase to be set prior the making these calls.\n"
2261 "Use the walletpassphrase call for this, and then walletlock call.\n"
2262 "If the wallet is already encrypted, use the walletpassphrasechange call.\n"
2263 "Note that this will shutdown the server.\n"
2264 "\nArguments:\n"
2265 "1. \"passphrase\" (string) The pass phrase to encrypt the wallet with. It must be at least 1 character, but should be long.\n"
2266 "\nExamples:\n"
2267 "\nEncrypt your wallet\n"
2268 + HelpExampleCli("encryptwallet", "\"my pass phrase\"") +
2269 "\nNow set the passphrase to use the wallet, such as for signing or sending bitcoin\n"
2270 + HelpExampleCli("walletpassphrase", "\"my pass phrase\"") +
2271 "\nNow we can do something like sign\n"
2272 + HelpExampleCli("signmessage", "\"address\" \"test message\"") +
2273 "\nNow lock the wallet again by removing the passphrase\n"
2274 + HelpExampleCli("walletlock", "") +
2275 "\nAs a json rpc call\n"
2276 + HelpExampleRpc("encryptwallet", "\"my pass phrase\"")
2280 LOCK2(cs_main, pwallet->cs_wallet);
2282 if (request.fHelp)
2283 return true;
2284 if (pwallet->IsCrypted()) {
2285 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an encrypted wallet, but encryptwallet was called.");
2288 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
2289 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2290 SecureString strWalletPass;
2291 strWalletPass.reserve(100);
2292 strWalletPass = request.params[0].get_str().c_str();
2294 if (strWalletPass.length() < 1)
2295 throw std::runtime_error(
2296 "encryptwallet <passphrase>\n"
2297 "Encrypts the wallet with <passphrase>.");
2299 if (!pwallet->EncryptWallet(strWalletPass)) {
2300 throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: Failed to encrypt the wallet.");
2303 // BDB seems to have a bad habit of writing old data into
2304 // slack space in .dat files; that is bad if the old data is
2305 // unencrypted private keys. So:
2306 StartShutdown();
2307 return "wallet encrypted; Bitcoin server stopping, restart to run with encrypted wallet. The keypool has been flushed and a new HD seed was generated (if you are using HD). You need to make a new backup.";
2310 UniValue lockunspent(const JSONRPCRequest& request)
2312 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2313 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2314 return NullUniValue;
2317 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
2318 throw std::runtime_error(
2319 "lockunspent unlock ([{\"txid\":\"txid\",\"vout\":n},...])\n"
2320 "\nUpdates list of temporarily unspendable outputs.\n"
2321 "Temporarily lock (unlock=false) or unlock (unlock=true) specified transaction outputs.\n"
2322 "If no transaction outputs are specified when unlocking then all current locked transaction outputs are unlocked.\n"
2323 "A locked transaction output will not be chosen by automatic coin selection, when spending bitcoins.\n"
2324 "Locks are stored in memory only. Nodes start with zero locked outputs, and the locked output list\n"
2325 "is always cleared (by virtue of process exit) when a node stops or fails.\n"
2326 "Also see the listunspent call\n"
2327 "\nArguments:\n"
2328 "1. unlock (boolean, required) Whether to unlock (true) or lock (false) the specified transactions\n"
2329 "2. \"transactions\" (string, optional) A json array of objects. Each object the txid (string) vout (numeric)\n"
2330 " [ (json array of json objects)\n"
2331 " {\n"
2332 " \"txid\":\"id\", (string) The transaction id\n"
2333 " \"vout\": n (numeric) The output number\n"
2334 " }\n"
2335 " ,...\n"
2336 " ]\n"
2338 "\nResult:\n"
2339 "true|false (boolean) Whether the command was successful or not\n"
2341 "\nExamples:\n"
2342 "\nList the unspent transactions\n"
2343 + HelpExampleCli("listunspent", "") +
2344 "\nLock an unspent transaction\n"
2345 + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2346 "\nList the locked transactions\n"
2347 + HelpExampleCli("listlockunspent", "") +
2348 "\nUnlock the transaction again\n"
2349 + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2350 "\nAs a json rpc call\n"
2351 + HelpExampleRpc("lockunspent", "false, \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"")
2354 LOCK2(cs_main, pwallet->cs_wallet);
2356 if (request.params.size() == 1)
2357 RPCTypeCheck(request.params, {UniValue::VBOOL});
2358 else
2359 RPCTypeCheck(request.params, {UniValue::VBOOL, UniValue::VARR});
2361 bool fUnlock = request.params[0].get_bool();
2363 if (request.params.size() == 1) {
2364 if (fUnlock)
2365 pwallet->UnlockAllCoins();
2366 return true;
2369 UniValue outputs = request.params[1].get_array();
2370 for (unsigned int idx = 0; idx < outputs.size(); idx++) {
2371 const UniValue& output = outputs[idx];
2372 if (!output.isObject())
2373 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected object");
2374 const UniValue& o = output.get_obj();
2376 RPCTypeCheckObj(o,
2378 {"txid", UniValueType(UniValue::VSTR)},
2379 {"vout", UniValueType(UniValue::VNUM)},
2382 std::string txid = find_value(o, "txid").get_str();
2383 if (!IsHex(txid))
2384 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected hex txid");
2386 int nOutput = find_value(o, "vout").get_int();
2387 if (nOutput < 0)
2388 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout must be positive");
2390 COutPoint outpt(uint256S(txid), nOutput);
2392 if (fUnlock)
2393 pwallet->UnlockCoin(outpt);
2394 else
2395 pwallet->LockCoin(outpt);
2398 return true;
2401 UniValue listlockunspent(const JSONRPCRequest& request)
2403 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2404 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2405 return NullUniValue;
2408 if (request.fHelp || request.params.size() > 0)
2409 throw std::runtime_error(
2410 "listlockunspent\n"
2411 "\nReturns list of temporarily unspendable outputs.\n"
2412 "See the lockunspent call to lock and unlock transactions for spending.\n"
2413 "\nResult:\n"
2414 "[\n"
2415 " {\n"
2416 " \"txid\" : \"transactionid\", (string) The transaction id locked\n"
2417 " \"vout\" : n (numeric) The vout value\n"
2418 " }\n"
2419 " ,...\n"
2420 "]\n"
2421 "\nExamples:\n"
2422 "\nList the unspent transactions\n"
2423 + HelpExampleCli("listunspent", "") +
2424 "\nLock an unspent transaction\n"
2425 + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2426 "\nList the locked transactions\n"
2427 + HelpExampleCli("listlockunspent", "") +
2428 "\nUnlock the transaction again\n"
2429 + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2430 "\nAs a json rpc call\n"
2431 + HelpExampleRpc("listlockunspent", "")
2434 LOCK2(cs_main, pwallet->cs_wallet);
2436 std::vector<COutPoint> vOutpts;
2437 pwallet->ListLockedCoins(vOutpts);
2439 UniValue ret(UniValue::VARR);
2441 for (COutPoint &outpt : vOutpts) {
2442 UniValue o(UniValue::VOBJ);
2444 o.push_back(Pair("txid", outpt.hash.GetHex()));
2445 o.push_back(Pair("vout", (int)outpt.n));
2446 ret.push_back(o);
2449 return ret;
2452 UniValue settxfee(const JSONRPCRequest& request)
2454 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2455 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2456 return NullUniValue;
2459 if (request.fHelp || request.params.size() < 1 || request.params.size() > 1)
2460 throw std::runtime_error(
2461 "settxfee amount\n"
2462 "\nSet the transaction fee per kB. Overwrites the paytxfee parameter.\n"
2463 "\nArguments:\n"
2464 "1. amount (numeric or string, required) The transaction fee in " + CURRENCY_UNIT + "/kB\n"
2465 "\nResult\n"
2466 "true|false (boolean) Returns true if successful\n"
2467 "\nExamples:\n"
2468 + HelpExampleCli("settxfee", "0.00001")
2469 + HelpExampleRpc("settxfee", "0.00001")
2472 LOCK2(cs_main, pwallet->cs_wallet);
2474 // Amount
2475 CAmount nAmount = AmountFromValue(request.params[0]);
2477 payTxFee = CFeeRate(nAmount, 1000);
2478 return true;
2481 UniValue getwalletinfo(const JSONRPCRequest& request)
2483 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2484 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2485 return NullUniValue;
2488 if (request.fHelp || request.params.size() != 0)
2489 throw std::runtime_error(
2490 "getwalletinfo\n"
2491 "Returns an object containing various wallet state info.\n"
2492 "\nResult:\n"
2493 "{\n"
2494 " \"walletname\": xxxxx, (string) the wallet name\n"
2495 " \"walletversion\": xxxxx, (numeric) the wallet version\n"
2496 " \"balance\": xxxxxxx, (numeric) the total confirmed balance of the wallet in " + CURRENCY_UNIT + "\n"
2497 " \"unconfirmed_balance\": xxx, (numeric) the total unconfirmed balance of the wallet in " + CURRENCY_UNIT + "\n"
2498 " \"immature_balance\": xxxxxx, (numeric) the total immature balance of the wallet in " + CURRENCY_UNIT + "\n"
2499 " \"txcount\": xxxxxxx, (numeric) the total number of transactions in the wallet\n"
2500 " \"keypoololdest\": xxxxxx, (numeric) the timestamp (seconds since Unix epoch) of the oldest pre-generated key in the key pool\n"
2501 " \"keypoolsize\": xxxx, (numeric) how many new keys are pre-generated (only counts external keys)\n"
2502 " \"keypoolsize_hd_internal\": xxxx, (numeric) 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)\n"
2503 " \"unlocked_until\": ttt, (numeric) the timestamp in seconds since epoch (midnight Jan 1 1970 GMT) that the wallet is unlocked for transfers, or 0 if the wallet is locked\n"
2504 " \"paytxfee\": x.xxxx, (numeric) the transaction fee configuration, set in " + CURRENCY_UNIT + "/kB\n"
2505 " \"hdmasterkeyid\": \"<hash160>\" (string) the Hash160 of the HD master pubkey\n"
2506 "}\n"
2507 "\nExamples:\n"
2508 + HelpExampleCli("getwalletinfo", "")
2509 + HelpExampleRpc("getwalletinfo", "")
2512 LOCK2(cs_main, pwallet->cs_wallet);
2514 UniValue obj(UniValue::VOBJ);
2516 size_t kpExternalSize = pwallet->KeypoolCountExternalKeys();
2517 obj.push_back(Pair("walletname", pwallet->GetName()));
2518 obj.push_back(Pair("walletversion", pwallet->GetVersion()));
2519 obj.push_back(Pair("balance", ValueFromAmount(pwallet->GetBalance())));
2520 obj.push_back(Pair("unconfirmed_balance", ValueFromAmount(pwallet->GetUnconfirmedBalance())));
2521 obj.push_back(Pair("immature_balance", ValueFromAmount(pwallet->GetImmatureBalance())));
2522 obj.push_back(Pair("txcount", (int)pwallet->mapWallet.size()));
2523 obj.push_back(Pair("keypoololdest", pwallet->GetOldestKeyPoolTime()));
2524 obj.push_back(Pair("keypoolsize", (int64_t)kpExternalSize));
2525 CKeyID masterKeyID = pwallet->GetHDChain().masterKeyID;
2526 if (!masterKeyID.IsNull() && pwallet->CanSupportFeature(FEATURE_HD_SPLIT)) {
2527 obj.push_back(Pair("keypoolsize_hd_internal", (int64_t)(pwallet->GetKeyPoolSize() - kpExternalSize)));
2529 if (pwallet->IsCrypted()) {
2530 obj.push_back(Pair("unlocked_until", pwallet->nRelockTime));
2532 obj.push_back(Pair("paytxfee", ValueFromAmount(payTxFee.GetFeePerK())));
2533 if (!masterKeyID.IsNull())
2534 obj.push_back(Pair("hdmasterkeyid", masterKeyID.GetHex()));
2535 return obj;
2538 UniValue listwallets(const JSONRPCRequest& request)
2540 if (request.fHelp || request.params.size() != 0)
2541 throw std::runtime_error(
2542 "listwallets\n"
2543 "Returns a list of currently loaded wallets.\n"
2544 "For full information on the wallet, use \"getwalletinfo\"\n"
2545 "\nResult:\n"
2546 "[ (json array of strings)\n"
2547 " \"walletname\" (string) the wallet name\n"
2548 " ...\n"
2549 "]\n"
2550 "\nExamples:\n"
2551 + HelpExampleCli("listwallets", "")
2552 + HelpExampleRpc("listwallets", "")
2555 UniValue obj(UniValue::VARR);
2557 for (CWalletRef pwallet : vpwallets) {
2559 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2560 return NullUniValue;
2563 LOCK(pwallet->cs_wallet);
2565 obj.push_back(pwallet->GetName());
2568 return obj;
2571 UniValue resendwallettransactions(const JSONRPCRequest& request)
2573 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2574 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2575 return NullUniValue;
2578 if (request.fHelp || request.params.size() != 0)
2579 throw std::runtime_error(
2580 "resendwallettransactions\n"
2581 "Immediately re-broadcast unconfirmed wallet transactions to all peers.\n"
2582 "Intended only for testing; the wallet code periodically re-broadcasts\n"
2583 "automatically.\n"
2584 "Returns array of transaction ids that were re-broadcast.\n"
2587 if (!g_connman)
2588 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
2590 LOCK2(cs_main, pwallet->cs_wallet);
2592 std::vector<uint256> txids = pwallet->ResendWalletTransactionsBefore(GetTime(), g_connman.get());
2593 UniValue result(UniValue::VARR);
2594 for (const uint256& txid : txids)
2596 result.push_back(txid.ToString());
2598 return result;
2601 UniValue listunspent(const JSONRPCRequest& request)
2603 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2604 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2605 return NullUniValue;
2608 if (request.fHelp || request.params.size() > 5)
2609 throw std::runtime_error(
2610 "listunspent ( minconf maxconf [\"addresses\",...] [include_unsafe] [query_options])\n"
2611 "\nReturns array of unspent transaction outputs\n"
2612 "with between minconf and maxconf (inclusive) confirmations.\n"
2613 "Optionally filter to only include txouts paid to specified addresses.\n"
2614 "\nArguments:\n"
2615 "1. minconf (numeric, optional, default=1) The minimum confirmations to filter\n"
2616 "2. maxconf (numeric, optional, default=9999999) The maximum confirmations to filter\n"
2617 "3. \"addresses\" (string) A json array of bitcoin addresses to filter\n"
2618 " [\n"
2619 " \"address\" (string) bitcoin address\n"
2620 " ,...\n"
2621 " ]\n"
2622 "4. include_unsafe (bool, optional, default=true) Include outputs that are not safe to spend\n"
2623 " See description of \"safe\" attribute below.\n"
2624 "5. query_options (json, optional) JSON with query options\n"
2625 " {\n"
2626 " \"minimumAmount\" (numeric or string, default=0) Minimum value of each UTXO in " + CURRENCY_UNIT + "\n"
2627 " \"maximumAmount\" (numeric or string, default=unlimited) Maximum value of each UTXO in " + CURRENCY_UNIT + "\n"
2628 " \"maximumCount\" (numeric or string, default=unlimited) Maximum number of UTXOs\n"
2629 " \"minimumSumAmount\" (numeric or string, default=unlimited) Minimum sum value of all UTXOs in " + CURRENCY_UNIT + "\n"
2630 " }\n"
2631 "\nResult\n"
2632 "[ (array of json object)\n"
2633 " {\n"
2634 " \"txid\" : \"txid\", (string) the transaction id \n"
2635 " \"vout\" : n, (numeric) the vout value\n"
2636 " \"address\" : \"address\", (string) the bitcoin address\n"
2637 " \"account\" : \"account\", (string) DEPRECATED. The associated account, or \"\" for the default account\n"
2638 " \"scriptPubKey\" : \"key\", (string) the script key\n"
2639 " \"amount\" : x.xxx, (numeric) the transaction output amount in " + CURRENCY_UNIT + "\n"
2640 " \"confirmations\" : n, (numeric) The number of confirmations\n"
2641 " \"redeemScript\" : n (string) The redeemScript if scriptPubKey is P2SH\n"
2642 " \"spendable\" : xxx, (bool) Whether we have the private keys to spend this output\n"
2643 " \"solvable\" : xxx, (bool) Whether we know how to spend this output, ignoring the lack of keys\n"
2644 " \"safe\" : xxx (bool) Whether this output is considered safe to spend. Unconfirmed transactions\n"
2645 " from outside keys and unconfirmed replacement transactions are considered unsafe\n"
2646 " and are not eligible for spending by fundrawtransaction and sendtoaddress.\n"
2647 " }\n"
2648 " ,...\n"
2649 "]\n"
2651 "\nExamples\n"
2652 + HelpExampleCli("listunspent", "")
2653 + HelpExampleCli("listunspent", "6 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
2654 + HelpExampleRpc("listunspent", "6, 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
2655 + HelpExampleCli("listunspent", "6 9999999 '[]' true '{ \"minimumAmount\": 0.005 }'")
2656 + HelpExampleRpc("listunspent", "6, 9999999, [] , true, { \"minimumAmount\": 0.005 } ")
2659 int nMinDepth = 1;
2660 if (request.params.size() > 0 && !request.params[0].isNull()) {
2661 RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
2662 nMinDepth = request.params[0].get_int();
2665 int nMaxDepth = 9999999;
2666 if (request.params.size() > 1 && !request.params[1].isNull()) {
2667 RPCTypeCheckArgument(request.params[1], UniValue::VNUM);
2668 nMaxDepth = request.params[1].get_int();
2671 std::set<CBitcoinAddress> setAddress;
2672 if (request.params.size() > 2 && !request.params[2].isNull()) {
2673 RPCTypeCheckArgument(request.params[2], UniValue::VARR);
2674 UniValue inputs = request.params[2].get_array();
2675 for (unsigned int idx = 0; idx < inputs.size(); idx++) {
2676 const UniValue& input = inputs[idx];
2677 CBitcoinAddress address(input.get_str());
2678 if (!address.IsValid())
2679 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ")+input.get_str());
2680 if (setAddress.count(address))
2681 throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ")+input.get_str());
2682 setAddress.insert(address);
2686 bool include_unsafe = true;
2687 if (request.params.size() > 3 && !request.params[3].isNull()) {
2688 RPCTypeCheckArgument(request.params[3], UniValue::VBOOL);
2689 include_unsafe = request.params[3].get_bool();
2692 CAmount nMinimumAmount = 0;
2693 CAmount nMaximumAmount = MAX_MONEY;
2694 CAmount nMinimumSumAmount = MAX_MONEY;
2695 uint64_t nMaximumCount = 0;
2697 if (!request.params[4].isNull()) {
2698 const UniValue& options = request.params[4].get_obj();
2700 if (options.exists("minimumAmount"))
2701 nMinimumAmount = AmountFromValue(options["minimumAmount"]);
2703 if (options.exists("maximumAmount"))
2704 nMaximumAmount = AmountFromValue(options["maximumAmount"]);
2706 if (options.exists("minimumSumAmount"))
2707 nMinimumSumAmount = AmountFromValue(options["minimumSumAmount"]);
2709 if (options.exists("maximumCount"))
2710 nMaximumCount = options["maximumCount"].get_int64();
2713 UniValue results(UniValue::VARR);
2714 std::vector<COutput> vecOutputs;
2715 assert(pwallet != NULL);
2716 LOCK2(cs_main, pwallet->cs_wallet);
2718 pwallet->AvailableCoins(vecOutputs, !include_unsafe, NULL, nMinimumAmount, nMaximumAmount, nMinimumSumAmount, nMaximumCount, nMinDepth, nMaxDepth);
2719 for (const COutput& out : vecOutputs) {
2720 CTxDestination address;
2721 const CScript& scriptPubKey = out.tx->tx->vout[out.i].scriptPubKey;
2722 bool fValidAddress = ExtractDestination(scriptPubKey, address);
2724 if (setAddress.size() && (!fValidAddress || !setAddress.count(address)))
2725 continue;
2727 UniValue entry(UniValue::VOBJ);
2728 entry.push_back(Pair("txid", out.tx->GetHash().GetHex()));
2729 entry.push_back(Pair("vout", out.i));
2731 if (fValidAddress) {
2732 entry.push_back(Pair("address", CBitcoinAddress(address).ToString()));
2734 if (pwallet->mapAddressBook.count(address)) {
2735 entry.push_back(Pair("account", pwallet->mapAddressBook[address].name));
2738 if (scriptPubKey.IsPayToScriptHash()) {
2739 const CScriptID& hash = boost::get<CScriptID>(address);
2740 CScript redeemScript;
2741 if (pwallet->GetCScript(hash, redeemScript)) {
2742 entry.push_back(Pair("redeemScript", HexStr(redeemScript.begin(), redeemScript.end())));
2747 entry.push_back(Pair("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
2748 entry.push_back(Pair("amount", ValueFromAmount(out.tx->tx->vout[out.i].nValue)));
2749 entry.push_back(Pair("confirmations", out.nDepth));
2750 entry.push_back(Pair("spendable", out.fSpendable));
2751 entry.push_back(Pair("solvable", out.fSolvable));
2752 entry.push_back(Pair("safe", out.fSafe));
2753 results.push_back(entry);
2756 return results;
2759 UniValue fundrawtransaction(const JSONRPCRequest& request)
2761 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2762 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2763 return NullUniValue;
2766 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
2767 throw std::runtime_error(
2768 "fundrawtransaction \"hexstring\" ( options )\n"
2769 "\nAdd inputs to a transaction until it has enough in value to meet its out value.\n"
2770 "This will not modify existing inputs, and will add at most one change output to the outputs.\n"
2771 "No existing outputs will be modified unless \"subtractFeeFromOutputs\" is specified.\n"
2772 "Note that inputs which were signed may need to be resigned after completion since in/outputs have been added.\n"
2773 "The inputs added will not be signed, use signrawtransaction for that.\n"
2774 "Note that all existing inputs must have their previous output transaction be in the wallet.\n"
2775 "Note that all inputs selected must be of standard form and P2SH scripts must be\n"
2776 "in the wallet using importaddress or addmultisigaddress (to calculate fees).\n"
2777 "You can see whether this is the case by checking the \"solvable\" field in the listunspent output.\n"
2778 "Only pay-to-pubkey, multisig, and P2SH versions thereof are currently supported for watch-only\n"
2779 "\nArguments:\n"
2780 "1. \"hexstring\" (string, required) The hex string of the raw transaction\n"
2781 "2. options (object, optional)\n"
2782 " {\n"
2783 " \"changeAddress\" (string, optional, default pool address) The bitcoin address to receive the change\n"
2784 " \"changePosition\" (numeric, optional, default random) The index of the change output\n"
2785 " \"includeWatching\" (boolean, optional, default false) Also select inputs which are watch only\n"
2786 " \"lockUnspents\" (boolean, optional, default false) Lock selected unspent outputs\n"
2787 " \"feeRate\" (numeric, optional, default not set: makes wallet determine the fee) Set a specific feerate (" + CURRENCY_UNIT + " per KB)\n"
2788 " \"subtractFeeFromOutputs\" (array, optional) A json array of integers.\n"
2789 " The fee will be equally deducted from the amount of each specified output.\n"
2790 " The outputs are specified by their zero-based index, before any change output is added.\n"
2791 " Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n"
2792 " If no outputs are specified here, the sender pays the fee.\n"
2793 " [vout_index,...]\n"
2794 " \"replaceable\" (boolean, optional) Marks this transaction as BIP125 replaceable.\n"
2795 " Allows this transaction to be replaced by a transaction with higher fees\n"
2796 " \"conf_target\" (numeric, optional) Confirmation target (in blocks)\n"
2797 " \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
2798 " \"UNSET\"\n"
2799 " \"ECONOMICAL\"\n"
2800 " \"CONSERVATIVE\"\n"
2801 " }\n"
2802 " for backward compatibility: passing in a true instead of an object will result in {\"includeWatching\":true}\n"
2803 "\nResult:\n"
2804 "{\n"
2805 " \"hex\": \"value\", (string) The resulting raw transaction (hex-encoded string)\n"
2806 " \"fee\": n, (numeric) Fee in " + CURRENCY_UNIT + " the resulting transaction pays\n"
2807 " \"changepos\": n (numeric) The position of the added change output, or -1\n"
2808 "}\n"
2809 "\nExamples:\n"
2810 "\nCreate a transaction with no inputs\n"
2811 + HelpExampleCli("createrawtransaction", "\"[]\" \"{\\\"myaddress\\\":0.01}\"") +
2812 "\nAdd sufficient unsigned inputs to meet the output value\n"
2813 + HelpExampleCli("fundrawtransaction", "\"rawtransactionhex\"") +
2814 "\nSign the transaction\n"
2815 + HelpExampleCli("signrawtransaction", "\"fundedtransactionhex\"") +
2816 "\nSend the transaction\n"
2817 + HelpExampleCli("sendrawtransaction", "\"signedtransactionhex\"")
2820 RPCTypeCheck(request.params, {UniValue::VSTR});
2822 CCoinControl coinControl;
2823 int changePosition = -1;
2824 bool lockUnspents = false;
2825 UniValue subtractFeeFromOutputs;
2826 std::set<int> setSubtractFeeFromOutputs;
2828 if (!request.params[1].isNull()) {
2829 if (request.params[1].type() == UniValue::VBOOL) {
2830 // backward compatibility bool only fallback
2831 coinControl.fAllowWatchOnly = request.params[1].get_bool();
2833 else {
2834 RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VOBJ});
2836 UniValue options = request.params[1];
2838 RPCTypeCheckObj(options,
2840 {"changeAddress", UniValueType(UniValue::VSTR)},
2841 {"changePosition", UniValueType(UniValue::VNUM)},
2842 {"includeWatching", UniValueType(UniValue::VBOOL)},
2843 {"lockUnspents", UniValueType(UniValue::VBOOL)},
2844 {"reserveChangeKey", UniValueType(UniValue::VBOOL)}, // DEPRECATED (and ignored), should be removed in 0.16 or so.
2845 {"feeRate", UniValueType()}, // will be checked below
2846 {"subtractFeeFromOutputs", UniValueType(UniValue::VARR)},
2847 {"replaceable", UniValueType(UniValue::VBOOL)},
2848 {"conf_target", UniValueType(UniValue::VNUM)},
2849 {"estimate_mode", UniValueType(UniValue::VSTR)},
2851 true, true);
2853 if (options.exists("changeAddress")) {
2854 CBitcoinAddress address(options["changeAddress"].get_str());
2856 if (!address.IsValid())
2857 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "changeAddress must be a valid bitcoin address");
2859 coinControl.destChange = address.Get();
2862 if (options.exists("changePosition"))
2863 changePosition = options["changePosition"].get_int();
2865 if (options.exists("includeWatching"))
2866 coinControl.fAllowWatchOnly = options["includeWatching"].get_bool();
2868 if (options.exists("lockUnspents"))
2869 lockUnspents = options["lockUnspents"].get_bool();
2871 if (options.exists("feeRate"))
2873 coinControl.m_feerate = CFeeRate(AmountFromValue(options["feeRate"]));
2874 coinControl.fOverrideFeeRate = true;
2877 if (options.exists("subtractFeeFromOutputs"))
2878 subtractFeeFromOutputs = options["subtractFeeFromOutputs"].get_array();
2880 if (options.exists("replaceable")) {
2881 coinControl.signalRbf = options["replaceable"].get_bool();
2883 if (options.exists("conf_target")) {
2884 if (options.exists("feeRate")) {
2885 throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot specify both conf_target and feeRate");
2887 coinControl.m_confirm_target = ParseConfirmTarget(options["conf_target"]);
2889 if (options.exists("estimate_mode")) {
2890 if (options.exists("feeRate")) {
2891 throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot specify both estimate_mode and feeRate");
2893 if (!FeeModeFromString(options["estimate_mode"].get_str(), coinControl.m_fee_mode)) {
2894 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid estimate_mode parameter");
2900 // parse hex string from parameter
2901 CMutableTransaction tx;
2902 if (!DecodeHexTx(tx, request.params[0].get_str(), true))
2903 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
2905 if (tx.vout.size() == 0)
2906 throw JSONRPCError(RPC_INVALID_PARAMETER, "TX must have at least one output");
2908 if (changePosition != -1 && (changePosition < 0 || (unsigned int)changePosition > tx.vout.size()))
2909 throw JSONRPCError(RPC_INVALID_PARAMETER, "changePosition out of bounds");
2911 for (unsigned int idx = 0; idx < subtractFeeFromOutputs.size(); idx++) {
2912 int pos = subtractFeeFromOutputs[idx].get_int();
2913 if (setSubtractFeeFromOutputs.count(pos))
2914 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, duplicated position: %d", pos));
2915 if (pos < 0)
2916 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, negative position: %d", pos));
2917 if (pos >= int(tx.vout.size()))
2918 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, position too large: %d", pos));
2919 setSubtractFeeFromOutputs.insert(pos);
2922 CAmount nFeeOut;
2923 std::string strFailReason;
2925 if (!pwallet->FundTransaction(tx, nFeeOut, changePosition, strFailReason, lockUnspents, setSubtractFeeFromOutputs, coinControl)) {
2926 throw JSONRPCError(RPC_WALLET_ERROR, strFailReason);
2929 UniValue result(UniValue::VOBJ);
2930 result.push_back(Pair("hex", EncodeHexTx(tx)));
2931 result.push_back(Pair("changepos", changePosition));
2932 result.push_back(Pair("fee", ValueFromAmount(nFeeOut)));
2934 return result;
2937 UniValue bumpfee(const JSONRPCRequest& request)
2939 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2941 if (!EnsureWalletIsAvailable(pwallet, request.fHelp))
2942 return NullUniValue;
2944 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) {
2945 throw std::runtime_error(
2946 "bumpfee \"txid\" ( options ) \n"
2947 "\nBumps the fee of an opt-in-RBF transaction T, replacing it with a new transaction B.\n"
2948 "An opt-in RBF transaction with the given txid must be in the wallet.\n"
2949 "The command will pay the additional fee by decreasing (or perhaps removing) its change output.\n"
2950 "If the change output is not big enough to cover the increased fee, the command will currently fail\n"
2951 "instead of adding new inputs to compensate. (A future implementation could improve this.)\n"
2952 "The command will fail if the wallet or mempool contains a transaction that spends one of T's outputs.\n"
2953 "By default, the new fee will be calculated automatically using estimatefee.\n"
2954 "The user can specify a confirmation target for estimatefee.\n"
2955 "Alternatively, the user can specify totalFee, or use RPC settxfee to set a higher fee rate.\n"
2956 "At a minimum, the new fee rate must be high enough to pay an additional new relay fee (incrementalfee\n"
2957 "returned by getnetworkinfo) to enter the node's mempool.\n"
2958 "\nArguments:\n"
2959 "1. txid (string, required) The txid to be bumped\n"
2960 "2. options (object, optional)\n"
2961 " {\n"
2962 " \"confTarget\" (numeric, optional) Confirmation target (in blocks)\n"
2963 " \"totalFee\" (numeric, optional) Total fee (NOT feerate) to pay, in satoshis.\n"
2964 " In rare cases, the actual fee paid might be slightly higher than the specified\n"
2965 " totalFee if the tx change output has to be removed because it is too close to\n"
2966 " the dust threshold.\n"
2967 " \"replaceable\" (boolean, optional, default true) Whether the new transaction should still be\n"
2968 " marked bip-125 replaceable. If true, the sequence numbers in the transaction will\n"
2969 " be left unchanged from the original. If false, any input sequence numbers in the\n"
2970 " original transaction that were less than 0xfffffffe will be increased to 0xfffffffe\n"
2971 " so the new transaction will not be explicitly bip-125 replaceable (though it may\n"
2972 " still be replaceable in practice, for example if it has unconfirmed ancestors which\n"
2973 " are replaceable).\n"
2974 " \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
2975 " \"UNSET\"\n"
2976 " \"ECONOMICAL\"\n"
2977 " \"CONSERVATIVE\"\n"
2978 " }\n"
2979 "\nResult:\n"
2980 "{\n"
2981 " \"txid\": \"value\", (string) The id of the new transaction\n"
2982 " \"origfee\": n, (numeric) Fee of the replaced transaction\n"
2983 " \"fee\": n, (numeric) Fee of the new transaction\n"
2984 " \"errors\": [ str... ] (json array of strings) Errors encountered during processing (may be empty)\n"
2985 "}\n"
2986 "\nExamples:\n"
2987 "\nBump the fee, get the new transaction\'s txid\n" +
2988 HelpExampleCli("bumpfee", "<txid>"));
2991 RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VOBJ});
2992 uint256 hash;
2993 hash.SetHex(request.params[0].get_str());
2995 // optional parameters
2996 CAmount totalFee = 0;
2997 CCoinControl coin_control;
2998 coin_control.signalRbf = true;
2999 if (!request.params[1].isNull()) {
3000 UniValue options = request.params[1];
3001 RPCTypeCheckObj(options,
3003 {"confTarget", UniValueType(UniValue::VNUM)},
3004 {"totalFee", UniValueType(UniValue::VNUM)},
3005 {"replaceable", UniValueType(UniValue::VBOOL)},
3006 {"estimate_mode", UniValueType(UniValue::VSTR)},
3008 true, true);
3010 if (options.exists("confTarget") && options.exists("totalFee")) {
3011 throw JSONRPCError(RPC_INVALID_PARAMETER, "confTarget and totalFee options should not both be set. Please provide either a confirmation target for fee estimation or an explicit total fee for the transaction.");
3012 } else if (options.exists("confTarget")) { // TODO: alias this to conf_target
3013 coin_control.m_confirm_target = ParseConfirmTarget(options["confTarget"]);
3014 } else if (options.exists("totalFee")) {
3015 totalFee = options["totalFee"].get_int64();
3016 if (totalFee <= 0) {
3017 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid totalFee %s (must be greater than 0)", FormatMoney(totalFee)));
3021 if (options.exists("replaceable")) {
3022 coin_control.signalRbf = options["replaceable"].get_bool();
3024 if (options.exists("estimate_mode")) {
3025 if (!FeeModeFromString(options["estimate_mode"].get_str(), coin_control.m_fee_mode)) {
3026 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid estimate_mode parameter");
3031 LOCK2(cs_main, pwallet->cs_wallet);
3032 EnsureWalletIsUnlocked(pwallet);
3034 CFeeBumper feeBump(pwallet, hash, coin_control, totalFee);
3035 BumpFeeResult res = feeBump.getResult();
3036 if (res != BumpFeeResult::OK)
3038 switch(res) {
3039 case BumpFeeResult::INVALID_ADDRESS_OR_KEY:
3040 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, feeBump.getErrors()[0]);
3041 break;
3042 case BumpFeeResult::INVALID_REQUEST:
3043 throw JSONRPCError(RPC_INVALID_REQUEST, feeBump.getErrors()[0]);
3044 break;
3045 case BumpFeeResult::INVALID_PARAMETER:
3046 throw JSONRPCError(RPC_INVALID_PARAMETER, feeBump.getErrors()[0]);
3047 break;
3048 case BumpFeeResult::WALLET_ERROR:
3049 throw JSONRPCError(RPC_WALLET_ERROR, feeBump.getErrors()[0]);
3050 break;
3051 default:
3052 throw JSONRPCError(RPC_MISC_ERROR, feeBump.getErrors()[0]);
3053 break;
3057 // sign bumped transaction
3058 if (!feeBump.signTransaction(pwallet)) {
3059 throw JSONRPCError(RPC_WALLET_ERROR, "Can't sign transaction.");
3061 // commit the bumped transaction
3062 if(!feeBump.commit(pwallet)) {
3063 throw JSONRPCError(RPC_WALLET_ERROR, feeBump.getErrors()[0]);
3065 UniValue result(UniValue::VOBJ);
3066 result.push_back(Pair("txid", feeBump.getBumpedTxId().GetHex()));
3067 result.push_back(Pair("origfee", ValueFromAmount(feeBump.getOldFee())));
3068 result.push_back(Pair("fee", ValueFromAmount(feeBump.getNewFee())));
3069 UniValue errors(UniValue::VARR);
3070 for (const std::string& err: feeBump.getErrors())
3071 errors.push_back(err);
3072 result.push_back(Pair("errors", errors));
3074 return result;
3077 UniValue generate(const JSONRPCRequest& request)
3079 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
3081 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
3082 return NullUniValue;
3085 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) {
3086 throw std::runtime_error(
3087 "generate nblocks ( maxtries )\n"
3088 "\nMine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.\n"
3089 "\nArguments:\n"
3090 "1. nblocks (numeric, required) How many blocks are generated immediately.\n"
3091 "2. maxtries (numeric, optional) How many iterations to try (default = 1000000).\n"
3092 "\nResult:\n"
3093 "[ blockhashes ] (array) hashes of blocks generated\n"
3094 "\nExamples:\n"
3095 "\nGenerate 11 blocks\n"
3096 + HelpExampleCli("generate", "11")
3100 int num_generate = request.params[0].get_int();
3101 uint64_t max_tries = 1000000;
3102 if (request.params.size() > 1 && !request.params[1].isNull()) {
3103 max_tries = request.params[1].get_int();
3106 std::shared_ptr<CReserveScript> coinbase_script;
3107 pwallet->GetScriptForMining(coinbase_script);
3109 // If the keypool is exhausted, no script is returned at all. Catch this.
3110 if (!coinbase_script) {
3111 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
3114 //throw an error if no script was provided
3115 if (coinbase_script->reserveScript.empty()) {
3116 throw JSONRPCError(RPC_INTERNAL_ERROR, "No coinbase script available");
3119 return generateBlocks(coinbase_script, num_generate, max_tries, true);
3122 extern UniValue abortrescan(const JSONRPCRequest& request); // in rpcdump.cpp
3123 extern UniValue dumpprivkey(const JSONRPCRequest& request); // in rpcdump.cpp
3124 extern UniValue importprivkey(const JSONRPCRequest& request);
3125 extern UniValue importaddress(const JSONRPCRequest& request);
3126 extern UniValue importpubkey(const JSONRPCRequest& request);
3127 extern UniValue dumpwallet(const JSONRPCRequest& request);
3128 extern UniValue importwallet(const JSONRPCRequest& request);
3129 extern UniValue importprunedfunds(const JSONRPCRequest& request);
3130 extern UniValue removeprunedfunds(const JSONRPCRequest& request);
3131 extern UniValue importmulti(const JSONRPCRequest& request);
3133 static const CRPCCommand commands[] =
3134 { // category name actor (function) okSafeMode
3135 // --------------------- ------------------------ ----------------------- ----------
3136 { "rawtransactions", "fundrawtransaction", &fundrawtransaction, false, {"hexstring","options"} },
3137 { "hidden", "resendwallettransactions", &resendwallettransactions, true, {} },
3138 { "wallet", "abandontransaction", &abandontransaction, false, {"txid"} },
3139 { "wallet", "abortrescan", &abortrescan, false, {} },
3140 { "wallet", "addmultisigaddress", &addmultisigaddress, true, {"nrequired","keys","account"} },
3141 { "wallet", "addwitnessaddress", &addwitnessaddress, true, {"address"} },
3142 { "wallet", "backupwallet", &backupwallet, true, {"destination"} },
3143 { "wallet", "bumpfee", &bumpfee, true, {"txid", "options"} },
3144 { "wallet", "dumpprivkey", &dumpprivkey, true, {"address"} },
3145 { "wallet", "dumpwallet", &dumpwallet, true, {"filename"} },
3146 { "wallet", "encryptwallet", &encryptwallet, true, {"passphrase"} },
3147 { "wallet", "getaccountaddress", &getaccountaddress, true, {"account"} },
3148 { "wallet", "getaccount", &getaccount, true, {"address"} },
3149 { "wallet", "getaddressesbyaccount", &getaddressesbyaccount, true, {"account"} },
3150 { "wallet", "getbalance", &getbalance, false, {"account","minconf","include_watchonly"} },
3151 { "wallet", "getnewaddress", &getnewaddress, true, {"account"} },
3152 { "wallet", "getrawchangeaddress", &getrawchangeaddress, true, {} },
3153 { "wallet", "getreceivedbyaccount", &getreceivedbyaccount, false, {"account","minconf"} },
3154 { "wallet", "getreceivedbyaddress", &getreceivedbyaddress, false, {"address","minconf"} },
3155 { "wallet", "gettransaction", &gettransaction, false, {"txid","include_watchonly"} },
3156 { "wallet", "getunconfirmedbalance", &getunconfirmedbalance, false, {} },
3157 { "wallet", "getwalletinfo", &getwalletinfo, false, {} },
3158 { "wallet", "importmulti", &importmulti, true, {"requests","options"} },
3159 { "wallet", "importprivkey", &importprivkey, true, {"privkey","label","rescan"} },
3160 { "wallet", "importwallet", &importwallet, true, {"filename"} },
3161 { "wallet", "importaddress", &importaddress, true, {"address","label","rescan","p2sh"} },
3162 { "wallet", "importprunedfunds", &importprunedfunds, true, {"rawtransaction","txoutproof"} },
3163 { "wallet", "importpubkey", &importpubkey, true, {"pubkey","label","rescan"} },
3164 { "wallet", "keypoolrefill", &keypoolrefill, true, {"newsize"} },
3165 { "wallet", "listaccounts", &listaccounts, false, {"minconf","include_watchonly"} },
3166 { "wallet", "listaddressgroupings", &listaddressgroupings, false, {} },
3167 { "wallet", "listlockunspent", &listlockunspent, false, {} },
3168 { "wallet", "listreceivedbyaccount", &listreceivedbyaccount, false, {"minconf","include_empty","include_watchonly"} },
3169 { "wallet", "listreceivedbyaddress", &listreceivedbyaddress, false, {"minconf","include_empty","include_watchonly"} },
3170 { "wallet", "listsinceblock", &listsinceblock, false, {"blockhash","target_confirmations","include_watchonly","include_removed"} },
3171 { "wallet", "listtransactions", &listtransactions, false, {"account","count","skip","include_watchonly"} },
3172 { "wallet", "listunspent", &listunspent, false, {"minconf","maxconf","addresses","include_unsafe","query_options"} },
3173 { "wallet", "listwallets", &listwallets, true, {} },
3174 { "wallet", "lockunspent", &lockunspent, true, {"unlock","transactions"} },
3175 { "wallet", "move", &movecmd, false, {"fromaccount","toaccount","amount","minconf","comment"} },
3176 { "wallet", "sendfrom", &sendfrom, false, {"fromaccount","toaddress","amount","minconf","comment","comment_to"} },
3177 { "wallet", "sendmany", &sendmany, false, {"fromaccount","amounts","minconf","comment","subtractfeefrom","replaceable","conf_target","estimate_mode"} },
3178 { "wallet", "sendtoaddress", &sendtoaddress, false, {"address","amount","comment","comment_to","subtractfeefromamount","replaceable","conf_target","estimate_mode"} },
3179 { "wallet", "setaccount", &setaccount, true, {"address","account"} },
3180 { "wallet", "settxfee", &settxfee, true, {"amount"} },
3181 { "wallet", "signmessage", &signmessage, true, {"address","message"} },
3182 { "wallet", "walletlock", &walletlock, true, {} },
3183 { "wallet", "walletpassphrasechange", &walletpassphrasechange, true, {"oldpassphrase","newpassphrase"} },
3184 { "wallet", "walletpassphrase", &walletpassphrase, true, {"passphrase","timeout"} },
3185 { "wallet", "removeprunedfunds", &removeprunedfunds, true, {"txid"} },
3187 { "generating", "generate", &generate, true, {"nblocks","maxtries"} },
3190 void RegisterWalletRPCCommands(CRPCTable &t)
3192 if (GetBoolArg("-disablewallet", false))
3193 return;
3195 for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
3196 t.appendCommand(commands[vcidx].name, &commands[vcidx]);