Merge #10956: Fix typos
[bitcoinplatinum.git] / src / wallet / rpcwallet.cpp
blob75f836a29d5cf711d8e1950f6ddea2a0ce94dd12
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 CScript witscript = GetScriptForWitness(basescript);
1134 SignatureData sigs;
1135 // This check is to make sure that the script we created can actually be solved for and signed by us
1136 // if we were to have the private keys. This is just to make sure that the script is valid and that,
1137 // if found in a transaction, we would still accept and relay that transaction.
1138 if (!ProduceSignature(DummySignatureCreator(pwallet), witscript, sigs) ||
1139 !VerifyScript(sigs.scriptSig, witscript, &sigs.scriptWitness, MANDATORY_SCRIPT_VERIFY_FLAGS | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, DummySignatureCreator(pwallet).Checker())) {
1140 return false;
1142 pwallet->AddCScript(witscript);
1143 result = CScriptID(witscript);
1144 return true;
1146 return false;
1149 bool operator()(const CScriptID &scriptID) {
1150 CScript subscript;
1151 if (pwallet && pwallet->GetCScript(scriptID, subscript)) {
1152 int witnessversion;
1153 std::vector<unsigned char> witprog;
1154 if (subscript.IsWitnessProgram(witnessversion, witprog)) {
1155 result = scriptID;
1156 return true;
1158 CScript witscript = GetScriptForWitness(subscript);
1159 SignatureData sigs;
1160 // This check is to make sure that the script we created can actually be solved for and signed by us
1161 // if we were to have the private keys. This is just to make sure that the script is valid and that,
1162 // if found in a transaction, we would still accept and relay that transaction.
1163 if (!ProduceSignature(DummySignatureCreator(pwallet), witscript, sigs) ||
1164 !VerifyScript(sigs.scriptSig, witscript, &sigs.scriptWitness, MANDATORY_SCRIPT_VERIFY_FLAGS | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, DummySignatureCreator(pwallet).Checker())) {
1165 return false;
1167 pwallet->AddCScript(witscript);
1168 result = CScriptID(witscript);
1169 return true;
1171 return false;
1175 UniValue addwitnessaddress(const JSONRPCRequest& request)
1177 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1178 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1179 return NullUniValue;
1182 if (request.fHelp || request.params.size() < 1 || request.params.size() > 1)
1184 std::string msg = "addwitnessaddress \"address\"\n"
1185 "\nAdd a witness address for a script (with pubkey or redeemscript known).\n"
1186 "It returns the witness script.\n"
1188 "\nArguments:\n"
1189 "1. \"address\" (string, required) An address known to the wallet\n"
1191 "\nResult:\n"
1192 "\"witnessaddress\", (string) The value of the new address (P2SH of witness script).\n"
1193 "}\n"
1195 throw std::runtime_error(msg);
1199 LOCK(cs_main);
1200 if (!IsWitnessEnabled(chainActive.Tip(), Params().GetConsensus()) && !gArgs.GetBoolArg("-walletprematurewitness", false)) {
1201 throw JSONRPCError(RPC_WALLET_ERROR, "Segregated witness not enabled on network");
1205 CBitcoinAddress address(request.params[0].get_str());
1206 if (!address.IsValid())
1207 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
1209 Witnessifier w(pwallet);
1210 CTxDestination dest = address.Get();
1211 bool ret = boost::apply_visitor(w, dest);
1212 if (!ret) {
1213 throw JSONRPCError(RPC_WALLET_ERROR, "Public key or redeemscript not known to wallet, or the key is uncompressed");
1216 pwallet->SetAddressBook(w.result, "", "receive");
1218 return CBitcoinAddress(w.result).ToString();
1221 struct tallyitem
1223 CAmount nAmount;
1224 int nConf;
1225 std::vector<uint256> txids;
1226 bool fIsWatchonly;
1227 tallyitem()
1229 nAmount = 0;
1230 nConf = std::numeric_limits<int>::max();
1231 fIsWatchonly = false;
1235 UniValue ListReceived(CWallet * const pwallet, const UniValue& params, bool fByAccounts)
1237 // Minimum confirmations
1238 int nMinDepth = 1;
1239 if (!params[0].isNull())
1240 nMinDepth = params[0].get_int();
1242 // Whether to include empty accounts
1243 bool fIncludeEmpty = false;
1244 if (!params[1].isNull())
1245 fIncludeEmpty = params[1].get_bool();
1247 isminefilter filter = ISMINE_SPENDABLE;
1248 if(!params[2].isNull())
1249 if(params[2].get_bool())
1250 filter = filter | ISMINE_WATCH_ONLY;
1252 // Tally
1253 std::map<CBitcoinAddress, tallyitem> mapTally;
1254 for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
1255 const CWalletTx& wtx = pairWtx.second;
1257 if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
1258 continue;
1260 int nDepth = wtx.GetDepthInMainChain();
1261 if (nDepth < nMinDepth)
1262 continue;
1264 for (const CTxOut& txout : wtx.tx->vout)
1266 CTxDestination address;
1267 if (!ExtractDestination(txout.scriptPubKey, address))
1268 continue;
1270 isminefilter mine = IsMine(*pwallet, address);
1271 if(!(mine & filter))
1272 continue;
1274 tallyitem& item = mapTally[address];
1275 item.nAmount += txout.nValue;
1276 item.nConf = std::min(item.nConf, nDepth);
1277 item.txids.push_back(wtx.GetHash());
1278 if (mine & ISMINE_WATCH_ONLY)
1279 item.fIsWatchonly = true;
1283 // Reply
1284 UniValue ret(UniValue::VARR);
1285 std::map<std::string, tallyitem> mapAccountTally;
1286 for (const std::pair<CBitcoinAddress, CAddressBookData>& item : pwallet->mapAddressBook) {
1287 const CBitcoinAddress& address = item.first;
1288 const std::string& strAccount = item.second.name;
1289 std::map<CBitcoinAddress, tallyitem>::iterator it = mapTally.find(address);
1290 if (it == mapTally.end() && !fIncludeEmpty)
1291 continue;
1293 CAmount nAmount = 0;
1294 int nConf = std::numeric_limits<int>::max();
1295 bool fIsWatchonly = false;
1296 if (it != mapTally.end())
1298 nAmount = (*it).second.nAmount;
1299 nConf = (*it).second.nConf;
1300 fIsWatchonly = (*it).second.fIsWatchonly;
1303 if (fByAccounts)
1305 tallyitem& _item = mapAccountTally[strAccount];
1306 _item.nAmount += nAmount;
1307 _item.nConf = std::min(_item.nConf, nConf);
1308 _item.fIsWatchonly = fIsWatchonly;
1310 else
1312 UniValue obj(UniValue::VOBJ);
1313 if(fIsWatchonly)
1314 obj.push_back(Pair("involvesWatchonly", true));
1315 obj.push_back(Pair("address", address.ToString()));
1316 obj.push_back(Pair("account", strAccount));
1317 obj.push_back(Pair("amount", ValueFromAmount(nAmount)));
1318 obj.push_back(Pair("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf)));
1319 if (!fByAccounts)
1320 obj.push_back(Pair("label", strAccount));
1321 UniValue transactions(UniValue::VARR);
1322 if (it != mapTally.end())
1324 for (const uint256& _item : (*it).second.txids)
1326 transactions.push_back(_item.GetHex());
1329 obj.push_back(Pair("txids", transactions));
1330 ret.push_back(obj);
1334 if (fByAccounts)
1336 for (std::map<std::string, tallyitem>::iterator it = mapAccountTally.begin(); it != mapAccountTally.end(); ++it)
1338 CAmount nAmount = (*it).second.nAmount;
1339 int nConf = (*it).second.nConf;
1340 UniValue obj(UniValue::VOBJ);
1341 if((*it).second.fIsWatchonly)
1342 obj.push_back(Pair("involvesWatchonly", true));
1343 obj.push_back(Pair("account", (*it).first));
1344 obj.push_back(Pair("amount", ValueFromAmount(nAmount)));
1345 obj.push_back(Pair("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf)));
1346 ret.push_back(obj);
1350 return ret;
1353 UniValue listreceivedbyaddress(const JSONRPCRequest& request)
1355 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1356 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1357 return NullUniValue;
1360 if (request.fHelp || request.params.size() > 3)
1361 throw std::runtime_error(
1362 "listreceivedbyaddress ( minconf include_empty include_watchonly)\n"
1363 "\nList balances by receiving address.\n"
1364 "\nArguments:\n"
1365 "1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included.\n"
1366 "2. include_empty (bool, optional, default=false) Whether to include addresses that haven't received any payments.\n"
1367 "3. include_watchonly (bool, optional, default=false) Whether to include watch-only addresses (see 'importaddress').\n"
1369 "\nResult:\n"
1370 "[\n"
1371 " {\n"
1372 " \"involvesWatchonly\" : true, (bool) Only returned if imported addresses were involved in transaction\n"
1373 " \"address\" : \"receivingaddress\", (string) The receiving address\n"
1374 " \"account\" : \"accountname\", (string) DEPRECATED. The account of the receiving address. The default account is \"\".\n"
1375 " \"amount\" : x.xxx, (numeric) The total amount in " + CURRENCY_UNIT + " received by the address\n"
1376 " \"confirmations\" : n, (numeric) The number of confirmations of the most recent transaction included\n"
1377 " \"label\" : \"label\", (string) A comment for the address/transaction, if any\n"
1378 " \"txids\": [\n"
1379 " n, (numeric) The ids of transactions received with the address \n"
1380 " ...\n"
1381 " ]\n"
1382 " }\n"
1383 " ,...\n"
1384 "]\n"
1386 "\nExamples:\n"
1387 + HelpExampleCli("listreceivedbyaddress", "")
1388 + HelpExampleCli("listreceivedbyaddress", "6 true")
1389 + HelpExampleRpc("listreceivedbyaddress", "6, true, true")
1392 LOCK2(cs_main, pwallet->cs_wallet);
1394 return ListReceived(pwallet, request.params, false);
1397 UniValue listreceivedbyaccount(const JSONRPCRequest& request)
1399 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1400 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1401 return NullUniValue;
1404 if (request.fHelp || request.params.size() > 3)
1405 throw std::runtime_error(
1406 "listreceivedbyaccount ( minconf include_empty include_watchonly)\n"
1407 "\nDEPRECATED. List balances by account.\n"
1408 "\nArguments:\n"
1409 "1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included.\n"
1410 "2. include_empty (bool, optional, default=false) Whether to include accounts that haven't received any payments.\n"
1411 "3. include_watchonly (bool, optional, default=false) Whether to include watch-only addresses (see 'importaddress').\n"
1413 "\nResult:\n"
1414 "[\n"
1415 " {\n"
1416 " \"involvesWatchonly\" : true, (bool) Only returned if imported addresses were involved in transaction\n"
1417 " \"account\" : \"accountname\", (string) The account name of the receiving account\n"
1418 " \"amount\" : x.xxx, (numeric) The total amount received by addresses with this account\n"
1419 " \"confirmations\" : n, (numeric) The number of confirmations of the most recent transaction included\n"
1420 " \"label\" : \"label\" (string) A comment for the address/transaction, if any\n"
1421 " }\n"
1422 " ,...\n"
1423 "]\n"
1425 "\nExamples:\n"
1426 + HelpExampleCli("listreceivedbyaccount", "")
1427 + HelpExampleCli("listreceivedbyaccount", "6 true")
1428 + HelpExampleRpc("listreceivedbyaccount", "6, true, true")
1431 LOCK2(cs_main, pwallet->cs_wallet);
1433 return ListReceived(pwallet, request.params, true);
1436 static void MaybePushAddress(UniValue & entry, const CTxDestination &dest)
1438 CBitcoinAddress addr;
1439 if (addr.Set(dest))
1440 entry.push_back(Pair("address", addr.ToString()));
1444 * List transactions based on the given criteria.
1446 * @param pwallet The wallet.
1447 * @param wtx The wallet transaction.
1448 * @param strAccount The account, if any, or "*" for all.
1449 * @param nMinDepth The minimum confirmation depth.
1450 * @param fLong Whether to include the JSON version of the transaction.
1451 * @param ret The UniValue into which the result is stored.
1452 * @param filter The "is mine" filter bool.
1454 void ListTransactions(CWallet* const pwallet, const CWalletTx& wtx, const std::string& strAccount, int nMinDepth, bool fLong, UniValue& ret, const isminefilter& filter)
1456 CAmount nFee;
1457 std::string strSentAccount;
1458 std::list<COutputEntry> listReceived;
1459 std::list<COutputEntry> listSent;
1461 wtx.GetAmounts(listReceived, listSent, nFee, strSentAccount, filter);
1463 bool fAllAccounts = (strAccount == std::string("*"));
1464 bool involvesWatchonly = wtx.IsFromMe(ISMINE_WATCH_ONLY);
1466 // Sent
1467 if ((!listSent.empty() || nFee != 0) && (fAllAccounts || strAccount == strSentAccount))
1469 for (const COutputEntry& s : listSent)
1471 UniValue entry(UniValue::VOBJ);
1472 if (involvesWatchonly || (::IsMine(*pwallet, s.destination) & ISMINE_WATCH_ONLY)) {
1473 entry.push_back(Pair("involvesWatchonly", true));
1475 entry.push_back(Pair("account", strSentAccount));
1476 MaybePushAddress(entry, s.destination);
1477 entry.push_back(Pair("category", "send"));
1478 entry.push_back(Pair("amount", ValueFromAmount(-s.amount)));
1479 if (pwallet->mapAddressBook.count(s.destination)) {
1480 entry.push_back(Pair("label", pwallet->mapAddressBook[s.destination].name));
1482 entry.push_back(Pair("vout", s.vout));
1483 entry.push_back(Pair("fee", ValueFromAmount(-nFee)));
1484 if (fLong)
1485 WalletTxToJSON(wtx, entry);
1486 entry.push_back(Pair("abandoned", wtx.isAbandoned()));
1487 ret.push_back(entry);
1491 // Received
1492 if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth)
1494 for (const COutputEntry& r : listReceived)
1496 std::string account;
1497 if (pwallet->mapAddressBook.count(r.destination)) {
1498 account = pwallet->mapAddressBook[r.destination].name;
1500 if (fAllAccounts || (account == strAccount))
1502 UniValue entry(UniValue::VOBJ);
1503 if (involvesWatchonly || (::IsMine(*pwallet, r.destination) & ISMINE_WATCH_ONLY)) {
1504 entry.push_back(Pair("involvesWatchonly", true));
1506 entry.push_back(Pair("account", account));
1507 MaybePushAddress(entry, r.destination);
1508 if (wtx.IsCoinBase())
1510 if (wtx.GetDepthInMainChain() < 1)
1511 entry.push_back(Pair("category", "orphan"));
1512 else if (wtx.GetBlocksToMaturity() > 0)
1513 entry.push_back(Pair("category", "immature"));
1514 else
1515 entry.push_back(Pair("category", "generate"));
1517 else
1519 entry.push_back(Pair("category", "receive"));
1521 entry.push_back(Pair("amount", ValueFromAmount(r.amount)));
1522 if (pwallet->mapAddressBook.count(r.destination)) {
1523 entry.push_back(Pair("label", account));
1525 entry.push_back(Pair("vout", r.vout));
1526 if (fLong)
1527 WalletTxToJSON(wtx, entry);
1528 ret.push_back(entry);
1534 void AcentryToJSON(const CAccountingEntry& acentry, const std::string& strAccount, UniValue& ret)
1536 bool fAllAccounts = (strAccount == std::string("*"));
1538 if (fAllAccounts || acentry.strAccount == strAccount)
1540 UniValue entry(UniValue::VOBJ);
1541 entry.push_back(Pair("account", acentry.strAccount));
1542 entry.push_back(Pair("category", "move"));
1543 entry.push_back(Pair("time", acentry.nTime));
1544 entry.push_back(Pair("amount", ValueFromAmount(acentry.nCreditDebit)));
1545 entry.push_back(Pair("otheraccount", acentry.strOtherAccount));
1546 entry.push_back(Pair("comment", acentry.strComment));
1547 ret.push_back(entry);
1551 UniValue listtransactions(const JSONRPCRequest& request)
1553 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1554 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1555 return NullUniValue;
1558 if (request.fHelp || request.params.size() > 4)
1559 throw std::runtime_error(
1560 "listtransactions ( \"account\" count skip include_watchonly)\n"
1561 "\nReturns up to 'count' most recent transactions skipping the first 'from' transactions for account 'account'.\n"
1562 "\nArguments:\n"
1563 "1. \"account\" (string, optional) DEPRECATED. The account name. Should be \"*\".\n"
1564 "2. count (numeric, optional, default=10) The number of transactions to return\n"
1565 "3. skip (numeric, optional, default=0) The number of transactions to skip\n"
1566 "4. include_watchonly (bool, optional, default=false) Include transactions to watch-only addresses (see 'importaddress')\n"
1567 "\nResult:\n"
1568 "[\n"
1569 " {\n"
1570 " \"account\":\"accountname\", (string) DEPRECATED. The account name associated with the transaction. \n"
1571 " It will be \"\" for the default account.\n"
1572 " \"address\":\"address\", (string) The bitcoin address of the transaction. Not present for \n"
1573 " move transactions (category = move).\n"
1574 " \"category\":\"send|receive|move\", (string) The transaction category. 'move' is a local (off blockchain)\n"
1575 " transaction between accounts, and not associated with an address,\n"
1576 " transaction id or block. 'send' and 'receive' transactions are \n"
1577 " associated with an address, transaction id and block details\n"
1578 " \"amount\": x.xxx, (numeric) The amount in " + CURRENCY_UNIT + ". This is negative for the 'send' category, and for the\n"
1579 " 'move' category for moves outbound. It is positive for the 'receive' category,\n"
1580 " and for the 'move' category for inbound funds.\n"
1581 " \"label\": \"label\", (string) A comment for the address/transaction, if any\n"
1582 " \"vout\": n, (numeric) the vout value\n"
1583 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the \n"
1584 " 'send' category of transactions.\n"
1585 " \"confirmations\": n, (numeric) The number of confirmations for the transaction. Available for 'send' and \n"
1586 " 'receive' category of transactions. Negative confirmations indicate the\n"
1587 " transaction conflicts with the block chain\n"
1588 " \"trusted\": xxx, (bool) Whether we consider the outputs of this unconfirmed transaction safe to spend.\n"
1589 " \"blockhash\": \"hashvalue\", (string) The block hash containing the transaction. Available for 'send' and 'receive'\n"
1590 " category of transactions.\n"
1591 " \"blockindex\": n, (numeric) The index of the transaction in the block that includes it. Available for 'send' and 'receive'\n"
1592 " category of transactions.\n"
1593 " \"blocktime\": xxx, (numeric) The block time in seconds since epoch (1 Jan 1970 GMT).\n"
1594 " \"txid\": \"transactionid\", (string) The transaction id. Available for 'send' and 'receive' category of transactions.\n"
1595 " \"time\": xxx, (numeric) The transaction time in seconds since epoch (midnight Jan 1 1970 GMT).\n"
1596 " \"timereceived\": xxx, (numeric) The time received in seconds since epoch (midnight Jan 1 1970 GMT). Available \n"
1597 " for 'send' and 'receive' category of transactions.\n"
1598 " \"comment\": \"...\", (string) If a comment is associated with the transaction.\n"
1599 " \"otheraccount\": \"accountname\", (string) DEPRECATED. For the 'move' category of transactions, the account the funds came \n"
1600 " from (for receiving funds, positive amounts), or went to (for sending funds,\n"
1601 " negative amounts).\n"
1602 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1603 " may be unknown for unconfirmed transactions not in the mempool\n"
1604 " \"abandoned\": xxx (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
1605 " 'send' category of transactions.\n"
1606 " }\n"
1607 "]\n"
1609 "\nExamples:\n"
1610 "\nList the most recent 10 transactions in the systems\n"
1611 + HelpExampleCli("listtransactions", "") +
1612 "\nList transactions 100 to 120\n"
1613 + HelpExampleCli("listtransactions", "\"*\" 20 100") +
1614 "\nAs a json rpc call\n"
1615 + HelpExampleRpc("listtransactions", "\"*\", 20, 100")
1618 LOCK2(cs_main, pwallet->cs_wallet);
1620 std::string strAccount = "*";
1621 if (!request.params[0].isNull())
1622 strAccount = request.params[0].get_str();
1623 int nCount = 10;
1624 if (!request.params[1].isNull())
1625 nCount = request.params[1].get_int();
1626 int nFrom = 0;
1627 if (!request.params[2].isNull())
1628 nFrom = request.params[2].get_int();
1629 isminefilter filter = ISMINE_SPENDABLE;
1630 if(!request.params[3].isNull())
1631 if(request.params[3].get_bool())
1632 filter = filter | ISMINE_WATCH_ONLY;
1634 if (nCount < 0)
1635 throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative count");
1636 if (nFrom < 0)
1637 throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative from");
1639 UniValue ret(UniValue::VARR);
1641 const CWallet::TxItems & txOrdered = pwallet->wtxOrdered;
1643 // iterate backwards until we have nCount items to return:
1644 for (CWallet::TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
1646 CWalletTx *const pwtx = (*it).second.first;
1647 if (pwtx != 0)
1648 ListTransactions(pwallet, *pwtx, strAccount, 0, true, ret, filter);
1649 CAccountingEntry *const pacentry = (*it).second.second;
1650 if (pacentry != 0)
1651 AcentryToJSON(*pacentry, strAccount, ret);
1653 if ((int)ret.size() >= (nCount+nFrom)) break;
1655 // ret is newest to oldest
1657 if (nFrom > (int)ret.size())
1658 nFrom = ret.size();
1659 if ((nFrom + nCount) > (int)ret.size())
1660 nCount = ret.size() - nFrom;
1662 std::vector<UniValue> arrTmp = ret.getValues();
1664 std::vector<UniValue>::iterator first = arrTmp.begin();
1665 std::advance(first, nFrom);
1666 std::vector<UniValue>::iterator last = arrTmp.begin();
1667 std::advance(last, nFrom+nCount);
1669 if (last != arrTmp.end()) arrTmp.erase(last, arrTmp.end());
1670 if (first != arrTmp.begin()) arrTmp.erase(arrTmp.begin(), first);
1672 std::reverse(arrTmp.begin(), arrTmp.end()); // Return oldest to newest
1674 ret.clear();
1675 ret.setArray();
1676 ret.push_backV(arrTmp);
1678 return ret;
1681 UniValue listaccounts(const JSONRPCRequest& request)
1683 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1684 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1685 return NullUniValue;
1688 if (request.fHelp || request.params.size() > 2)
1689 throw std::runtime_error(
1690 "listaccounts ( minconf include_watchonly)\n"
1691 "\nDEPRECATED. Returns Object that has account names as keys, account balances as values.\n"
1692 "\nArguments:\n"
1693 "1. minconf (numeric, optional, default=1) Only include transactions with at least this many confirmations\n"
1694 "2. include_watchonly (bool, optional, default=false) Include balances in watch-only addresses (see 'importaddress')\n"
1695 "\nResult:\n"
1696 "{ (json object where keys are account names, and values are numeric balances\n"
1697 " \"account\": x.xxx, (numeric) The property name is the account name, and the value is the total balance for the account.\n"
1698 " ...\n"
1699 "}\n"
1700 "\nExamples:\n"
1701 "\nList account balances where there at least 1 confirmation\n"
1702 + HelpExampleCli("listaccounts", "") +
1703 "\nList account balances including zero confirmation transactions\n"
1704 + HelpExampleCli("listaccounts", "0") +
1705 "\nList account balances for 6 or more confirmations\n"
1706 + HelpExampleCli("listaccounts", "6") +
1707 "\nAs json rpc call\n"
1708 + HelpExampleRpc("listaccounts", "6")
1711 LOCK2(cs_main, pwallet->cs_wallet);
1713 int nMinDepth = 1;
1714 if (request.params.size() > 0)
1715 nMinDepth = request.params[0].get_int();
1716 isminefilter includeWatchonly = ISMINE_SPENDABLE;
1717 if(request.params.size() > 1)
1718 if(request.params[1].get_bool())
1719 includeWatchonly = includeWatchonly | ISMINE_WATCH_ONLY;
1721 std::map<std::string, CAmount> mapAccountBalances;
1722 for (const std::pair<CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) {
1723 if (IsMine(*pwallet, entry.first) & includeWatchonly) { // This address belongs to me
1724 mapAccountBalances[entry.second.name] = 0;
1728 for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
1729 const CWalletTx& wtx = pairWtx.second;
1730 CAmount nFee;
1731 std::string strSentAccount;
1732 std::list<COutputEntry> listReceived;
1733 std::list<COutputEntry> listSent;
1734 int nDepth = wtx.GetDepthInMainChain();
1735 if (wtx.GetBlocksToMaturity() > 0 || nDepth < 0)
1736 continue;
1737 wtx.GetAmounts(listReceived, listSent, nFee, strSentAccount, includeWatchonly);
1738 mapAccountBalances[strSentAccount] -= nFee;
1739 for (const COutputEntry& s : listSent)
1740 mapAccountBalances[strSentAccount] -= s.amount;
1741 if (nDepth >= nMinDepth)
1743 for (const COutputEntry& r : listReceived)
1744 if (pwallet->mapAddressBook.count(r.destination)) {
1745 mapAccountBalances[pwallet->mapAddressBook[r.destination].name] += r.amount;
1747 else
1748 mapAccountBalances[""] += r.amount;
1752 const std::list<CAccountingEntry>& acentries = pwallet->laccentries;
1753 for (const CAccountingEntry& entry : acentries)
1754 mapAccountBalances[entry.strAccount] += entry.nCreditDebit;
1756 UniValue ret(UniValue::VOBJ);
1757 for (const std::pair<std::string, CAmount>& accountBalance : mapAccountBalances) {
1758 ret.push_back(Pair(accountBalance.first, ValueFromAmount(accountBalance.second)));
1760 return ret;
1763 UniValue listsinceblock(const JSONRPCRequest& request)
1765 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1766 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1767 return NullUniValue;
1770 if (request.fHelp || request.params.size() > 4)
1771 throw std::runtime_error(
1772 "listsinceblock ( \"blockhash\" target_confirmations include_watchonly include_removed )\n"
1773 "\nGet all transactions in blocks since block [blockhash], or all transactions if omitted.\n"
1774 "If \"blockhash\" is no longer a part of the main chain, transactions from the fork point onward are included.\n"
1775 "Additionally, if include_removed is set, transactions affecting the wallet which were removed are returned in the \"removed\" array.\n"
1776 "\nArguments:\n"
1777 "1. \"blockhash\" (string, optional) The block hash to list transactions since\n"
1778 "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"
1779 "3. include_watchonly: (bool, optional, default=false) Include transactions to watch-only addresses (see 'importaddress')\n"
1780 "4. include_removed: (bool, optional, default=true) Show transactions that were removed due to a reorg in the \"removed\" array\n"
1781 " (not guaranteed to work on pruned nodes)\n"
1782 "\nResult:\n"
1783 "{\n"
1784 " \"transactions\": [\n"
1785 " \"account\":\"accountname\", (string) DEPRECATED. The account name associated with the transaction. Will be \"\" for the default account.\n"
1786 " \"address\":\"address\", (string) The bitcoin address of the transaction. Not present for move transactions (category = move).\n"
1787 " \"category\":\"send|receive\", (string) The transaction category. 'send' has negative amounts, 'receive' has positive amounts.\n"
1788 " \"amount\": x.xxx, (numeric) The amount in " + CURRENCY_UNIT + ". This is negative for the 'send' category, and for the 'move' category for moves \n"
1789 " outbound. It is positive for the 'receive' category, and for the 'move' category for inbound funds.\n"
1790 " \"vout\" : n, (numeric) the vout value\n"
1791 " \"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"
1792 " \"confirmations\": n, (numeric) The number of confirmations for the transaction. Available for 'send' and 'receive' category of transactions.\n"
1793 " When it's < 0, it means the transaction conflicted that many blocks ago.\n"
1794 " \"blockhash\": \"hashvalue\", (string) The block hash containing the transaction. Available for 'send' and 'receive' category of transactions.\n"
1795 " \"blockindex\": n, (numeric) The index of the transaction in the block that includes it. Available for 'send' and 'receive' category of transactions.\n"
1796 " \"blocktime\": xxx, (numeric) The block time in seconds since epoch (1 Jan 1970 GMT).\n"
1797 " \"txid\": \"transactionid\", (string) The transaction id. Available for 'send' and 'receive' category of transactions.\n"
1798 " \"time\": xxx, (numeric) The transaction time in seconds since epoch (Jan 1 1970 GMT).\n"
1799 " \"timereceived\": xxx, (numeric) The time received in seconds since epoch (Jan 1 1970 GMT). Available for 'send' and 'receive' category of transactions.\n"
1800 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1801 " may be unknown for unconfirmed transactions not in the mempool\n"
1802 " \"abandoned\": xxx, (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the 'send' category of transactions.\n"
1803 " \"comment\": \"...\", (string) If a comment is associated with the transaction.\n"
1804 " \"label\" : \"label\" (string) A comment for the address/transaction, if any\n"
1805 " \"to\": \"...\", (string) If a comment to is associated with the transaction.\n"
1806 " ],\n"
1807 " \"removed\": [\n"
1808 " <structure is the same as \"transactions\" above, only present if include_removed=true>\n"
1809 " 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"
1810 " ],\n"
1811 " \"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"
1812 "}\n"
1813 "\nExamples:\n"
1814 + HelpExampleCli("listsinceblock", "")
1815 + HelpExampleCli("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\" 6")
1816 + HelpExampleRpc("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\", 6")
1819 LOCK2(cs_main, pwallet->cs_wallet);
1821 const CBlockIndex* pindex = nullptr; // Block index of the specified block or the common ancestor, if the block provided was in a deactivated chain.
1822 const CBlockIndex* paltindex = nullptr; // Block index of the specified block, even if it's in a deactivated chain.
1823 int target_confirms = 1;
1824 isminefilter filter = ISMINE_SPENDABLE;
1826 if (!request.params[0].isNull()) {
1827 uint256 blockId;
1829 blockId.SetHex(request.params[0].get_str());
1830 BlockMap::iterator it = mapBlockIndex.find(blockId);
1831 if (it != mapBlockIndex.end()) {
1832 paltindex = pindex = it->second;
1833 if (chainActive[pindex->nHeight] != pindex) {
1834 // the block being asked for is a part of a deactivated chain;
1835 // we don't want to depend on its perceived height in the block
1836 // chain, we want to instead use the last common ancestor
1837 pindex = chainActive.FindFork(pindex);
1842 if (!request.params[1].isNull()) {
1843 target_confirms = request.params[1].get_int();
1845 if (target_confirms < 1) {
1846 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter");
1850 if (!request.params[2].isNull() && request.params[2].get_bool()) {
1851 filter = filter | ISMINE_WATCH_ONLY;
1854 bool include_removed = (request.params[3].isNull() || request.params[3].get_bool());
1856 int depth = pindex ? (1 + chainActive.Height() - pindex->nHeight) : -1;
1858 UniValue transactions(UniValue::VARR);
1860 for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
1861 CWalletTx tx = pairWtx.second;
1863 if (depth == -1 || tx.GetDepthInMainChain() < depth) {
1864 ListTransactions(pwallet, tx, "*", 0, true, transactions, filter);
1868 // when a reorg'd block is requested, we also list any relevant transactions
1869 // in the blocks of the chain that was detached
1870 UniValue removed(UniValue::VARR);
1871 while (include_removed && paltindex && paltindex != pindex) {
1872 CBlock block;
1873 if (!ReadBlockFromDisk(block, paltindex, Params().GetConsensus())) {
1874 throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
1876 for (const CTransactionRef& tx : block.vtx) {
1877 if (pwallet->mapWallet.count(tx->GetHash()) > 0) {
1878 // We want all transactions regardless of confirmation count to appear here,
1879 // even negative confirmation ones, hence the big negative.
1880 ListTransactions(pwallet, pwallet->mapWallet[tx->GetHash()], "*", -100000000, true, removed, filter);
1883 paltindex = paltindex->pprev;
1886 CBlockIndex *pblockLast = chainActive[chainActive.Height() + 1 - target_confirms];
1887 uint256 lastblock = pblockLast ? pblockLast->GetBlockHash() : uint256();
1889 UniValue ret(UniValue::VOBJ);
1890 ret.push_back(Pair("transactions", transactions));
1891 if (include_removed) ret.push_back(Pair("removed", removed));
1892 ret.push_back(Pair("lastblock", lastblock.GetHex()));
1894 return ret;
1897 UniValue gettransaction(const JSONRPCRequest& request)
1899 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1900 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1901 return NullUniValue;
1904 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
1905 throw std::runtime_error(
1906 "gettransaction \"txid\" ( include_watchonly )\n"
1907 "\nGet detailed information about in-wallet transaction <txid>\n"
1908 "\nArguments:\n"
1909 "1. \"txid\" (string, required) The transaction id\n"
1910 "2. \"include_watchonly\" (bool, optional, default=false) Whether to include watch-only addresses in balance calculation and details[]\n"
1911 "\nResult:\n"
1912 "{\n"
1913 " \"amount\" : x.xxx, (numeric) The transaction amount in " + CURRENCY_UNIT + "\n"
1914 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the \n"
1915 " 'send' category of transactions.\n"
1916 " \"confirmations\" : n, (numeric) The number of confirmations\n"
1917 " \"blockhash\" : \"hash\", (string) The block hash\n"
1918 " \"blockindex\" : xx, (numeric) The index of the transaction in the block that includes it\n"
1919 " \"blocktime\" : ttt, (numeric) The time in seconds since epoch (1 Jan 1970 GMT)\n"
1920 " \"txid\" : \"transactionid\", (string) The transaction id.\n"
1921 " \"time\" : ttt, (numeric) The transaction time in seconds since epoch (1 Jan 1970 GMT)\n"
1922 " \"timereceived\" : ttt, (numeric) The time received in seconds since epoch (1 Jan 1970 GMT)\n"
1923 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1924 " may be unknown for unconfirmed transactions not in the mempool\n"
1925 " \"details\" : [\n"
1926 " {\n"
1927 " \"account\" : \"accountname\", (string) DEPRECATED. The account name involved in the transaction, can be \"\" for the default account.\n"
1928 " \"address\" : \"address\", (string) The bitcoin address involved in the transaction\n"
1929 " \"category\" : \"send|receive\", (string) The category, either 'send' or 'receive'\n"
1930 " \"amount\" : x.xxx, (numeric) The amount in " + CURRENCY_UNIT + "\n"
1931 " \"label\" : \"label\", (string) A comment for the address/transaction, if any\n"
1932 " \"vout\" : n, (numeric) the vout value\n"
1933 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the \n"
1934 " 'send' category of transactions.\n"
1935 " \"abandoned\": xxx (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
1936 " 'send' category of transactions.\n"
1937 " }\n"
1938 " ,...\n"
1939 " ],\n"
1940 " \"hex\" : \"data\" (string) Raw data for transaction\n"
1941 "}\n"
1943 "\nExamples:\n"
1944 + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
1945 + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\" true")
1946 + HelpExampleRpc("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
1949 LOCK2(cs_main, pwallet->cs_wallet);
1951 uint256 hash;
1952 hash.SetHex(request.params[0].get_str());
1954 isminefilter filter = ISMINE_SPENDABLE;
1955 if(!request.params[1].isNull())
1956 if(request.params[1].get_bool())
1957 filter = filter | ISMINE_WATCH_ONLY;
1959 UniValue entry(UniValue::VOBJ);
1960 if (!pwallet->mapWallet.count(hash)) {
1961 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid or non-wallet transaction id");
1963 const CWalletTx& wtx = pwallet->mapWallet[hash];
1965 CAmount nCredit = wtx.GetCredit(filter);
1966 CAmount nDebit = wtx.GetDebit(filter);
1967 CAmount nNet = nCredit - nDebit;
1968 CAmount nFee = (wtx.IsFromMe(filter) ? wtx.tx->GetValueOut() - nDebit : 0);
1970 entry.push_back(Pair("amount", ValueFromAmount(nNet - nFee)));
1971 if (wtx.IsFromMe(filter))
1972 entry.push_back(Pair("fee", ValueFromAmount(nFee)));
1974 WalletTxToJSON(wtx, entry);
1976 UniValue details(UniValue::VARR);
1977 ListTransactions(pwallet, wtx, "*", 0, false, details, filter);
1978 entry.push_back(Pair("details", details));
1980 std::string strHex = EncodeHexTx(static_cast<CTransaction>(wtx), RPCSerializationFlags());
1981 entry.push_back(Pair("hex", strHex));
1983 return entry;
1986 UniValue abandontransaction(const JSONRPCRequest& request)
1988 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1989 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1990 return NullUniValue;
1993 if (request.fHelp || request.params.size() != 1)
1994 throw std::runtime_error(
1995 "abandontransaction \"txid\"\n"
1996 "\nMark in-wallet transaction <txid> as abandoned\n"
1997 "This will mark this transaction and all its in-wallet descendants as abandoned which will allow\n"
1998 "for their inputs to be respent. It can be used to replace \"stuck\" or evicted transactions.\n"
1999 "It only works on transactions which are not included in a block and are not currently in the mempool.\n"
2000 "It has no effect on transactions which are already conflicted or abandoned.\n"
2001 "\nArguments:\n"
2002 "1. \"txid\" (string, required) The transaction id\n"
2003 "\nResult:\n"
2004 "\nExamples:\n"
2005 + HelpExampleCli("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
2006 + HelpExampleRpc("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
2009 LOCK2(cs_main, pwallet->cs_wallet);
2011 uint256 hash;
2012 hash.SetHex(request.params[0].get_str());
2014 if (!pwallet->mapWallet.count(hash)) {
2015 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid or non-wallet transaction id");
2017 if (!pwallet->AbandonTransaction(hash)) {
2018 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not eligible for abandonment");
2021 return NullUniValue;
2025 UniValue backupwallet(const JSONRPCRequest& request)
2027 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2028 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2029 return NullUniValue;
2032 if (request.fHelp || request.params.size() != 1)
2033 throw std::runtime_error(
2034 "backupwallet \"destination\"\n"
2035 "\nSafely copies current wallet file to destination, which can be a directory or a path with filename.\n"
2036 "\nArguments:\n"
2037 "1. \"destination\" (string) The destination directory or file\n"
2038 "\nExamples:\n"
2039 + HelpExampleCli("backupwallet", "\"backup.dat\"")
2040 + HelpExampleRpc("backupwallet", "\"backup.dat\"")
2043 LOCK2(cs_main, pwallet->cs_wallet);
2045 std::string strDest = request.params[0].get_str();
2046 if (!pwallet->BackupWallet(strDest)) {
2047 throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!");
2050 return NullUniValue;
2054 UniValue keypoolrefill(const JSONRPCRequest& request)
2056 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2057 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2058 return NullUniValue;
2061 if (request.fHelp || request.params.size() > 1)
2062 throw std::runtime_error(
2063 "keypoolrefill ( newsize )\n"
2064 "\nFills the keypool."
2065 + HelpRequiringPassphrase(pwallet) + "\n"
2066 "\nArguments\n"
2067 "1. newsize (numeric, optional, default=100) The new keypool size\n"
2068 "\nExamples:\n"
2069 + HelpExampleCli("keypoolrefill", "")
2070 + HelpExampleRpc("keypoolrefill", "")
2073 LOCK2(cs_main, pwallet->cs_wallet);
2075 // 0 is interpreted by TopUpKeyPool() as the default keypool size given by -keypool
2076 unsigned int kpSize = 0;
2077 if (!request.params[0].isNull()) {
2078 if (request.params[0].get_int() < 0)
2079 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected valid size.");
2080 kpSize = (unsigned int)request.params[0].get_int();
2083 EnsureWalletIsUnlocked(pwallet);
2084 pwallet->TopUpKeyPool(kpSize);
2086 if (pwallet->GetKeyPoolSize() < kpSize) {
2087 throw JSONRPCError(RPC_WALLET_ERROR, "Error refreshing keypool.");
2090 return NullUniValue;
2094 static void LockWallet(CWallet* pWallet)
2096 LOCK(pWallet->cs_wallet);
2097 pWallet->nRelockTime = 0;
2098 pWallet->Lock();
2101 UniValue walletpassphrase(const JSONRPCRequest& request)
2103 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2104 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2105 return NullUniValue;
2108 if (pwallet->IsCrypted() && (request.fHelp || request.params.size() != 2)) {
2109 throw std::runtime_error(
2110 "walletpassphrase \"passphrase\" timeout\n"
2111 "\nStores the wallet decryption key in memory for 'timeout' seconds.\n"
2112 "This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
2113 "\nArguments:\n"
2114 "1. \"passphrase\" (string, required) The wallet passphrase\n"
2115 "2. timeout (numeric, required) The time to keep the decryption key in seconds.\n"
2116 "\nNote:\n"
2117 "Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
2118 "time that overrides the old one.\n"
2119 "\nExamples:\n"
2120 "\nUnlock the wallet for 60 seconds\n"
2121 + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 60") +
2122 "\nLock the wallet again (before 60 seconds)\n"
2123 + HelpExampleCli("walletlock", "") +
2124 "\nAs json rpc call\n"
2125 + HelpExampleRpc("walletpassphrase", "\"my pass phrase\", 60")
2129 LOCK2(cs_main, pwallet->cs_wallet);
2131 if (request.fHelp)
2132 return true;
2133 if (!pwallet->IsCrypted()) {
2134 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrase was called.");
2137 // Note that the walletpassphrase is stored in request.params[0] which is not mlock()ed
2138 SecureString strWalletPass;
2139 strWalletPass.reserve(100);
2140 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
2141 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2142 strWalletPass = request.params[0].get_str().c_str();
2144 if (strWalletPass.length() > 0)
2146 if (!pwallet->Unlock(strWalletPass)) {
2147 throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
2150 else
2151 throw std::runtime_error(
2152 "walletpassphrase <passphrase> <timeout>\n"
2153 "Stores the wallet decryption key in memory for <timeout> seconds.");
2155 pwallet->TopUpKeyPool();
2157 int64_t nSleepTime = request.params[1].get_int64();
2158 pwallet->nRelockTime = GetTime() + nSleepTime;
2159 RPCRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), boost::bind(LockWallet, pwallet), nSleepTime);
2161 return NullUniValue;
2165 UniValue walletpassphrasechange(const JSONRPCRequest& request)
2167 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2168 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2169 return NullUniValue;
2172 if (pwallet->IsCrypted() && (request.fHelp || request.params.size() != 2)) {
2173 throw std::runtime_error(
2174 "walletpassphrasechange \"oldpassphrase\" \"newpassphrase\"\n"
2175 "\nChanges the wallet passphrase from 'oldpassphrase' to 'newpassphrase'.\n"
2176 "\nArguments:\n"
2177 "1. \"oldpassphrase\" (string) The current passphrase\n"
2178 "2. \"newpassphrase\" (string) The new passphrase\n"
2179 "\nExamples:\n"
2180 + HelpExampleCli("walletpassphrasechange", "\"old one\" \"new one\"")
2181 + HelpExampleRpc("walletpassphrasechange", "\"old one\", \"new one\"")
2185 LOCK2(cs_main, pwallet->cs_wallet);
2187 if (request.fHelp)
2188 return true;
2189 if (!pwallet->IsCrypted()) {
2190 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrasechange was called.");
2193 // TODO: get rid of these .c_str() calls by implementing SecureString::operator=(std::string)
2194 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2195 SecureString strOldWalletPass;
2196 strOldWalletPass.reserve(100);
2197 strOldWalletPass = request.params[0].get_str().c_str();
2199 SecureString strNewWalletPass;
2200 strNewWalletPass.reserve(100);
2201 strNewWalletPass = request.params[1].get_str().c_str();
2203 if (strOldWalletPass.length() < 1 || strNewWalletPass.length() < 1)
2204 throw std::runtime_error(
2205 "walletpassphrasechange <oldpassphrase> <newpassphrase>\n"
2206 "Changes the wallet passphrase from <oldpassphrase> to <newpassphrase>.");
2208 if (!pwallet->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass)) {
2209 throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
2212 return NullUniValue;
2216 UniValue walletlock(const JSONRPCRequest& request)
2218 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2219 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2220 return NullUniValue;
2223 if (pwallet->IsCrypted() && (request.fHelp || request.params.size() != 0)) {
2224 throw std::runtime_error(
2225 "walletlock\n"
2226 "\nRemoves the wallet encryption key from memory, locking the wallet.\n"
2227 "After calling this method, you will need to call walletpassphrase again\n"
2228 "before being able to call any methods which require the wallet to be unlocked.\n"
2229 "\nExamples:\n"
2230 "\nSet the passphrase for 2 minutes to perform a transaction\n"
2231 + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 120") +
2232 "\nPerform a send (requires passphrase set)\n"
2233 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 1.0") +
2234 "\nClear the passphrase since we are done before 2 minutes is up\n"
2235 + HelpExampleCli("walletlock", "") +
2236 "\nAs json rpc call\n"
2237 + HelpExampleRpc("walletlock", "")
2241 LOCK2(cs_main, pwallet->cs_wallet);
2243 if (request.fHelp)
2244 return true;
2245 if (!pwallet->IsCrypted()) {
2246 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletlock was called.");
2249 pwallet->Lock();
2250 pwallet->nRelockTime = 0;
2252 return NullUniValue;
2256 UniValue encryptwallet(const JSONRPCRequest& request)
2258 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2259 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2260 return NullUniValue;
2263 if (!pwallet->IsCrypted() && (request.fHelp || request.params.size() != 1)) {
2264 throw std::runtime_error(
2265 "encryptwallet \"passphrase\"\n"
2266 "\nEncrypts the wallet with 'passphrase'. This is for first time encryption.\n"
2267 "After this, any calls that interact with private keys such as sending or signing \n"
2268 "will require the passphrase to be set prior the making these calls.\n"
2269 "Use the walletpassphrase call for this, and then walletlock call.\n"
2270 "If the wallet is already encrypted, use the walletpassphrasechange call.\n"
2271 "Note that this will shutdown the server.\n"
2272 "\nArguments:\n"
2273 "1. \"passphrase\" (string) The pass phrase to encrypt the wallet with. It must be at least 1 character, but should be long.\n"
2274 "\nExamples:\n"
2275 "\nEncrypt your wallet\n"
2276 + HelpExampleCli("encryptwallet", "\"my pass phrase\"") +
2277 "\nNow set the passphrase to use the wallet, such as for signing or sending bitcoin\n"
2278 + HelpExampleCli("walletpassphrase", "\"my pass phrase\"") +
2279 "\nNow we can do something like sign\n"
2280 + HelpExampleCli("signmessage", "\"address\" \"test message\"") +
2281 "\nNow lock the wallet again by removing the passphrase\n"
2282 + HelpExampleCli("walletlock", "") +
2283 "\nAs a json rpc call\n"
2284 + HelpExampleRpc("encryptwallet", "\"my pass phrase\"")
2288 LOCK2(cs_main, pwallet->cs_wallet);
2290 if (request.fHelp)
2291 return true;
2292 if (pwallet->IsCrypted()) {
2293 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an encrypted wallet, but encryptwallet was called.");
2296 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
2297 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2298 SecureString strWalletPass;
2299 strWalletPass.reserve(100);
2300 strWalletPass = request.params[0].get_str().c_str();
2302 if (strWalletPass.length() < 1)
2303 throw std::runtime_error(
2304 "encryptwallet <passphrase>\n"
2305 "Encrypts the wallet with <passphrase>.");
2307 if (!pwallet->EncryptWallet(strWalletPass)) {
2308 throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: Failed to encrypt the wallet.");
2311 // BDB seems to have a bad habit of writing old data into
2312 // slack space in .dat files; that is bad if the old data is
2313 // unencrypted private keys. So:
2314 StartShutdown();
2315 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.";
2318 UniValue lockunspent(const JSONRPCRequest& request)
2320 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2321 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2322 return NullUniValue;
2325 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
2326 throw std::runtime_error(
2327 "lockunspent unlock ([{\"txid\":\"txid\",\"vout\":n},...])\n"
2328 "\nUpdates list of temporarily unspendable outputs.\n"
2329 "Temporarily lock (unlock=false) or unlock (unlock=true) specified transaction outputs.\n"
2330 "If no transaction outputs are specified when unlocking then all current locked transaction outputs are unlocked.\n"
2331 "A locked transaction output will not be chosen by automatic coin selection, when spending bitcoins.\n"
2332 "Locks are stored in memory only. Nodes start with zero locked outputs, and the locked output list\n"
2333 "is always cleared (by virtue of process exit) when a node stops or fails.\n"
2334 "Also see the listunspent call\n"
2335 "\nArguments:\n"
2336 "1. unlock (boolean, required) Whether to unlock (true) or lock (false) the specified transactions\n"
2337 "2. \"transactions\" (string, optional) A json array of objects. Each object the txid (string) vout (numeric)\n"
2338 " [ (json array of json objects)\n"
2339 " {\n"
2340 " \"txid\":\"id\", (string) The transaction id\n"
2341 " \"vout\": n (numeric) The output number\n"
2342 " }\n"
2343 " ,...\n"
2344 " ]\n"
2346 "\nResult:\n"
2347 "true|false (boolean) Whether the command was successful or not\n"
2349 "\nExamples:\n"
2350 "\nList the unspent transactions\n"
2351 + HelpExampleCli("listunspent", "") +
2352 "\nLock an unspent transaction\n"
2353 + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2354 "\nList the locked transactions\n"
2355 + HelpExampleCli("listlockunspent", "") +
2356 "\nUnlock the transaction again\n"
2357 + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2358 "\nAs a json rpc call\n"
2359 + HelpExampleRpc("lockunspent", "false, \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"")
2362 LOCK2(cs_main, pwallet->cs_wallet);
2364 if (request.params.size() == 1)
2365 RPCTypeCheck(request.params, {UniValue::VBOOL});
2366 else
2367 RPCTypeCheck(request.params, {UniValue::VBOOL, UniValue::VARR});
2369 bool fUnlock = request.params[0].get_bool();
2371 if (request.params.size() == 1) {
2372 if (fUnlock)
2373 pwallet->UnlockAllCoins();
2374 return true;
2377 UniValue outputs = request.params[1].get_array();
2378 for (unsigned int idx = 0; idx < outputs.size(); idx++) {
2379 const UniValue& output = outputs[idx];
2380 if (!output.isObject())
2381 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected object");
2382 const UniValue& o = output.get_obj();
2384 RPCTypeCheckObj(o,
2386 {"txid", UniValueType(UniValue::VSTR)},
2387 {"vout", UniValueType(UniValue::VNUM)},
2390 std::string txid = find_value(o, "txid").get_str();
2391 if (!IsHex(txid))
2392 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected hex txid");
2394 int nOutput = find_value(o, "vout").get_int();
2395 if (nOutput < 0)
2396 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout must be positive");
2398 COutPoint outpt(uint256S(txid), nOutput);
2400 if (fUnlock)
2401 pwallet->UnlockCoin(outpt);
2402 else
2403 pwallet->LockCoin(outpt);
2406 return true;
2409 UniValue listlockunspent(const JSONRPCRequest& request)
2411 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2412 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2413 return NullUniValue;
2416 if (request.fHelp || request.params.size() > 0)
2417 throw std::runtime_error(
2418 "listlockunspent\n"
2419 "\nReturns list of temporarily unspendable outputs.\n"
2420 "See the lockunspent call to lock and unlock transactions for spending.\n"
2421 "\nResult:\n"
2422 "[\n"
2423 " {\n"
2424 " \"txid\" : \"transactionid\", (string) The transaction id locked\n"
2425 " \"vout\" : n (numeric) The vout value\n"
2426 " }\n"
2427 " ,...\n"
2428 "]\n"
2429 "\nExamples:\n"
2430 "\nList the unspent transactions\n"
2431 + HelpExampleCli("listunspent", "") +
2432 "\nLock an unspent transaction\n"
2433 + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2434 "\nList the locked transactions\n"
2435 + HelpExampleCli("listlockunspent", "") +
2436 "\nUnlock the transaction again\n"
2437 + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2438 "\nAs a json rpc call\n"
2439 + HelpExampleRpc("listlockunspent", "")
2442 LOCK2(cs_main, pwallet->cs_wallet);
2444 std::vector<COutPoint> vOutpts;
2445 pwallet->ListLockedCoins(vOutpts);
2447 UniValue ret(UniValue::VARR);
2449 for (COutPoint &outpt : vOutpts) {
2450 UniValue o(UniValue::VOBJ);
2452 o.push_back(Pair("txid", outpt.hash.GetHex()));
2453 o.push_back(Pair("vout", (int)outpt.n));
2454 ret.push_back(o);
2457 return ret;
2460 UniValue settxfee(const JSONRPCRequest& request)
2462 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2463 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2464 return NullUniValue;
2467 if (request.fHelp || request.params.size() < 1 || request.params.size() > 1)
2468 throw std::runtime_error(
2469 "settxfee amount\n"
2470 "\nSet the transaction fee per kB. Overwrites the paytxfee parameter.\n"
2471 "\nArguments:\n"
2472 "1. amount (numeric or string, required) The transaction fee in " + CURRENCY_UNIT + "/kB\n"
2473 "\nResult\n"
2474 "true|false (boolean) Returns true if successful\n"
2475 "\nExamples:\n"
2476 + HelpExampleCli("settxfee", "0.00001")
2477 + HelpExampleRpc("settxfee", "0.00001")
2480 LOCK2(cs_main, pwallet->cs_wallet);
2482 // Amount
2483 CAmount nAmount = AmountFromValue(request.params[0]);
2485 payTxFee = CFeeRate(nAmount, 1000);
2486 return true;
2489 UniValue getwalletinfo(const JSONRPCRequest& request)
2491 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2492 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2493 return NullUniValue;
2496 if (request.fHelp || request.params.size() != 0)
2497 throw std::runtime_error(
2498 "getwalletinfo\n"
2499 "Returns an object containing various wallet state info.\n"
2500 "\nResult:\n"
2501 "{\n"
2502 " \"walletname\": xxxxx, (string) the wallet name\n"
2503 " \"walletversion\": xxxxx, (numeric) the wallet version\n"
2504 " \"balance\": xxxxxxx, (numeric) the total confirmed balance of the wallet in " + CURRENCY_UNIT + "\n"
2505 " \"unconfirmed_balance\": xxx, (numeric) the total unconfirmed balance of the wallet in " + CURRENCY_UNIT + "\n"
2506 " \"immature_balance\": xxxxxx, (numeric) the total immature balance of the wallet in " + CURRENCY_UNIT + "\n"
2507 " \"txcount\": xxxxxxx, (numeric) the total number of transactions in the wallet\n"
2508 " \"keypoololdest\": xxxxxx, (numeric) the timestamp (seconds since Unix epoch) of the oldest pre-generated key in the key pool\n"
2509 " \"keypoolsize\": xxxx, (numeric) how many new keys are pre-generated (only counts external keys)\n"
2510 " \"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"
2511 " \"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"
2512 " \"paytxfee\": x.xxxx, (numeric) the transaction fee configuration, set in " + CURRENCY_UNIT + "/kB\n"
2513 " \"hdmasterkeyid\": \"<hash160>\" (string) the Hash160 of the HD master pubkey\n"
2514 "}\n"
2515 "\nExamples:\n"
2516 + HelpExampleCli("getwalletinfo", "")
2517 + HelpExampleRpc("getwalletinfo", "")
2520 LOCK2(cs_main, pwallet->cs_wallet);
2522 UniValue obj(UniValue::VOBJ);
2524 size_t kpExternalSize = pwallet->KeypoolCountExternalKeys();
2525 obj.push_back(Pair("walletname", pwallet->GetName()));
2526 obj.push_back(Pair("walletversion", pwallet->GetVersion()));
2527 obj.push_back(Pair("balance", ValueFromAmount(pwallet->GetBalance())));
2528 obj.push_back(Pair("unconfirmed_balance", ValueFromAmount(pwallet->GetUnconfirmedBalance())));
2529 obj.push_back(Pair("immature_balance", ValueFromAmount(pwallet->GetImmatureBalance())));
2530 obj.push_back(Pair("txcount", (int)pwallet->mapWallet.size()));
2531 obj.push_back(Pair("keypoololdest", pwallet->GetOldestKeyPoolTime()));
2532 obj.push_back(Pair("keypoolsize", (int64_t)kpExternalSize));
2533 CKeyID masterKeyID = pwallet->GetHDChain().masterKeyID;
2534 if (!masterKeyID.IsNull() && pwallet->CanSupportFeature(FEATURE_HD_SPLIT)) {
2535 obj.push_back(Pair("keypoolsize_hd_internal", (int64_t)(pwallet->GetKeyPoolSize() - kpExternalSize)));
2537 if (pwallet->IsCrypted()) {
2538 obj.push_back(Pair("unlocked_until", pwallet->nRelockTime));
2540 obj.push_back(Pair("paytxfee", ValueFromAmount(payTxFee.GetFeePerK())));
2541 if (!masterKeyID.IsNull())
2542 obj.push_back(Pair("hdmasterkeyid", masterKeyID.GetHex()));
2543 return obj;
2546 UniValue listwallets(const JSONRPCRequest& request)
2548 if (request.fHelp || request.params.size() != 0)
2549 throw std::runtime_error(
2550 "listwallets\n"
2551 "Returns a list of currently loaded wallets.\n"
2552 "For full information on the wallet, use \"getwalletinfo\"\n"
2553 "\nResult:\n"
2554 "[ (json array of strings)\n"
2555 " \"walletname\" (string) the wallet name\n"
2556 " ...\n"
2557 "]\n"
2558 "\nExamples:\n"
2559 + HelpExampleCli("listwallets", "")
2560 + HelpExampleRpc("listwallets", "")
2563 UniValue obj(UniValue::VARR);
2565 for (CWalletRef pwallet : vpwallets) {
2567 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2568 return NullUniValue;
2571 LOCK(pwallet->cs_wallet);
2573 obj.push_back(pwallet->GetName());
2576 return obj;
2579 UniValue resendwallettransactions(const JSONRPCRequest& request)
2581 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2582 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2583 return NullUniValue;
2586 if (request.fHelp || request.params.size() != 0)
2587 throw std::runtime_error(
2588 "resendwallettransactions\n"
2589 "Immediately re-broadcast unconfirmed wallet transactions to all peers.\n"
2590 "Intended only for testing; the wallet code periodically re-broadcasts\n"
2591 "automatically.\n"
2592 "Returns an RPC error if -walletbroadcast is set to false.\n"
2593 "Returns array of transaction ids that were re-broadcast.\n"
2596 if (!g_connman)
2597 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
2599 LOCK2(cs_main, pwallet->cs_wallet);
2601 if (!pwallet->GetBroadcastTransactions()) {
2602 throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet transaction broadcasting is disabled with -walletbroadcast");
2605 std::vector<uint256> txids = pwallet->ResendWalletTransactionsBefore(GetTime(), g_connman.get());
2606 UniValue result(UniValue::VARR);
2607 for (const uint256& txid : txids)
2609 result.push_back(txid.ToString());
2611 return result;
2614 UniValue listunspent(const JSONRPCRequest& request)
2616 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2617 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2618 return NullUniValue;
2621 if (request.fHelp || request.params.size() > 5)
2622 throw std::runtime_error(
2623 "listunspent ( minconf maxconf [\"addresses\",...] [include_unsafe] [query_options])\n"
2624 "\nReturns array of unspent transaction outputs\n"
2625 "with between minconf and maxconf (inclusive) confirmations.\n"
2626 "Optionally filter to only include txouts paid to specified addresses.\n"
2627 "\nArguments:\n"
2628 "1. minconf (numeric, optional, default=1) The minimum confirmations to filter\n"
2629 "2. maxconf (numeric, optional, default=9999999) The maximum confirmations to filter\n"
2630 "3. \"addresses\" (string) A json array of bitcoin addresses to filter\n"
2631 " [\n"
2632 " \"address\" (string) bitcoin address\n"
2633 " ,...\n"
2634 " ]\n"
2635 "4. include_unsafe (bool, optional, default=true) Include outputs that are not safe to spend\n"
2636 " See description of \"safe\" attribute below.\n"
2637 "5. query_options (json, optional) JSON with query options\n"
2638 " {\n"
2639 " \"minimumAmount\" (numeric or string, default=0) Minimum value of each UTXO in " + CURRENCY_UNIT + "\n"
2640 " \"maximumAmount\" (numeric or string, default=unlimited) Maximum value of each UTXO in " + CURRENCY_UNIT + "\n"
2641 " \"maximumCount\" (numeric or string, default=unlimited) Maximum number of UTXOs\n"
2642 " \"minimumSumAmount\" (numeric or string, default=unlimited) Minimum sum value of all UTXOs in " + CURRENCY_UNIT + "\n"
2643 " }\n"
2644 "\nResult\n"
2645 "[ (array of json object)\n"
2646 " {\n"
2647 " \"txid\" : \"txid\", (string) the transaction id \n"
2648 " \"vout\" : n, (numeric) the vout value\n"
2649 " \"address\" : \"address\", (string) the bitcoin address\n"
2650 " \"account\" : \"account\", (string) DEPRECATED. The associated account, or \"\" for the default account\n"
2651 " \"scriptPubKey\" : \"key\", (string) the script key\n"
2652 " \"amount\" : x.xxx, (numeric) the transaction output amount in " + CURRENCY_UNIT + "\n"
2653 " \"confirmations\" : n, (numeric) The number of confirmations\n"
2654 " \"redeemScript\" : n (string) The redeemScript if scriptPubKey is P2SH\n"
2655 " \"spendable\" : xxx, (bool) Whether we have the private keys to spend this output\n"
2656 " \"solvable\" : xxx, (bool) Whether we know how to spend this output, ignoring the lack of keys\n"
2657 " \"safe\" : xxx (bool) Whether this output is considered safe to spend. Unconfirmed transactions\n"
2658 " from outside keys and unconfirmed replacement transactions are considered unsafe\n"
2659 " and are not eligible for spending by fundrawtransaction and sendtoaddress.\n"
2660 " }\n"
2661 " ,...\n"
2662 "]\n"
2664 "\nExamples\n"
2665 + HelpExampleCli("listunspent", "")
2666 + HelpExampleCli("listunspent", "6 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
2667 + HelpExampleRpc("listunspent", "6, 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
2668 + HelpExampleCli("listunspent", "6 9999999 '[]' true '{ \"minimumAmount\": 0.005 }'")
2669 + HelpExampleRpc("listunspent", "6, 9999999, [] , true, { \"minimumAmount\": 0.005 } ")
2672 int nMinDepth = 1;
2673 if (request.params.size() > 0 && !request.params[0].isNull()) {
2674 RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
2675 nMinDepth = request.params[0].get_int();
2678 int nMaxDepth = 9999999;
2679 if (request.params.size() > 1 && !request.params[1].isNull()) {
2680 RPCTypeCheckArgument(request.params[1], UniValue::VNUM);
2681 nMaxDepth = request.params[1].get_int();
2684 std::set<CBitcoinAddress> setAddress;
2685 if (request.params.size() > 2 && !request.params[2].isNull()) {
2686 RPCTypeCheckArgument(request.params[2], UniValue::VARR);
2687 UniValue inputs = request.params[2].get_array();
2688 for (unsigned int idx = 0; idx < inputs.size(); idx++) {
2689 const UniValue& input = inputs[idx];
2690 CBitcoinAddress address(input.get_str());
2691 if (!address.IsValid())
2692 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ")+input.get_str());
2693 if (setAddress.count(address))
2694 throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ")+input.get_str());
2695 setAddress.insert(address);
2699 bool include_unsafe = true;
2700 if (request.params.size() > 3 && !request.params[3].isNull()) {
2701 RPCTypeCheckArgument(request.params[3], UniValue::VBOOL);
2702 include_unsafe = request.params[3].get_bool();
2705 CAmount nMinimumAmount = 0;
2706 CAmount nMaximumAmount = MAX_MONEY;
2707 CAmount nMinimumSumAmount = MAX_MONEY;
2708 uint64_t nMaximumCount = 0;
2710 if (!request.params[4].isNull()) {
2711 const UniValue& options = request.params[4].get_obj();
2713 if (options.exists("minimumAmount"))
2714 nMinimumAmount = AmountFromValue(options["minimumAmount"]);
2716 if (options.exists("maximumAmount"))
2717 nMaximumAmount = AmountFromValue(options["maximumAmount"]);
2719 if (options.exists("minimumSumAmount"))
2720 nMinimumSumAmount = AmountFromValue(options["minimumSumAmount"]);
2722 if (options.exists("maximumCount"))
2723 nMaximumCount = options["maximumCount"].get_int64();
2726 UniValue results(UniValue::VARR);
2727 std::vector<COutput> vecOutputs;
2728 assert(pwallet != nullptr);
2729 LOCK2(cs_main, pwallet->cs_wallet);
2731 pwallet->AvailableCoins(vecOutputs, !include_unsafe, nullptr, nMinimumAmount, nMaximumAmount, nMinimumSumAmount, nMaximumCount, nMinDepth, nMaxDepth);
2732 for (const COutput& out : vecOutputs) {
2733 CTxDestination address;
2734 const CScript& scriptPubKey = out.tx->tx->vout[out.i].scriptPubKey;
2735 bool fValidAddress = ExtractDestination(scriptPubKey, address);
2737 if (setAddress.size() && (!fValidAddress || !setAddress.count(address)))
2738 continue;
2740 UniValue entry(UniValue::VOBJ);
2741 entry.push_back(Pair("txid", out.tx->GetHash().GetHex()));
2742 entry.push_back(Pair("vout", out.i));
2744 if (fValidAddress) {
2745 entry.push_back(Pair("address", CBitcoinAddress(address).ToString()));
2747 if (pwallet->mapAddressBook.count(address)) {
2748 entry.push_back(Pair("account", pwallet->mapAddressBook[address].name));
2751 if (scriptPubKey.IsPayToScriptHash()) {
2752 const CScriptID& hash = boost::get<CScriptID>(address);
2753 CScript redeemScript;
2754 if (pwallet->GetCScript(hash, redeemScript)) {
2755 entry.push_back(Pair("redeemScript", HexStr(redeemScript.begin(), redeemScript.end())));
2760 entry.push_back(Pair("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
2761 entry.push_back(Pair("amount", ValueFromAmount(out.tx->tx->vout[out.i].nValue)));
2762 entry.push_back(Pair("confirmations", out.nDepth));
2763 entry.push_back(Pair("spendable", out.fSpendable));
2764 entry.push_back(Pair("solvable", out.fSolvable));
2765 entry.push_back(Pair("safe", out.fSafe));
2766 results.push_back(entry);
2769 return results;
2772 UniValue fundrawtransaction(const JSONRPCRequest& request)
2774 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2775 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2776 return NullUniValue;
2779 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
2780 throw std::runtime_error(
2781 "fundrawtransaction \"hexstring\" ( options )\n"
2782 "\nAdd inputs to a transaction until it has enough in value to meet its out value.\n"
2783 "This will not modify existing inputs, and will add at most one change output to the outputs.\n"
2784 "No existing outputs will be modified unless \"subtractFeeFromOutputs\" is specified.\n"
2785 "Note that inputs which were signed may need to be resigned after completion since in/outputs have been added.\n"
2786 "The inputs added will not be signed, use signrawtransaction for that.\n"
2787 "Note that all existing inputs must have their previous output transaction be in the wallet.\n"
2788 "Note that all inputs selected must be of standard form and P2SH scripts must be\n"
2789 "in the wallet using importaddress or addmultisigaddress (to calculate fees).\n"
2790 "You can see whether this is the case by checking the \"solvable\" field in the listunspent output.\n"
2791 "Only pay-to-pubkey, multisig, and P2SH versions thereof are currently supported for watch-only\n"
2792 "\nArguments:\n"
2793 "1. \"hexstring\" (string, required) The hex string of the raw transaction\n"
2794 "2. options (object, optional)\n"
2795 " {\n"
2796 " \"changeAddress\" (string, optional, default pool address) The bitcoin address to receive the change\n"
2797 " \"changePosition\" (numeric, optional, default random) The index of the change output\n"
2798 " \"includeWatching\" (boolean, optional, default false) Also select inputs which are watch only\n"
2799 " \"lockUnspents\" (boolean, optional, default false) Lock selected unspent outputs\n"
2800 " \"feeRate\" (numeric, optional, default not set: makes wallet determine the fee) Set a specific feerate (" + CURRENCY_UNIT + " per KB)\n"
2801 " \"subtractFeeFromOutputs\" (array, optional) A json array of integers.\n"
2802 " The fee will be equally deducted from the amount of each specified output.\n"
2803 " The outputs are specified by their zero-based index, before any change output is added.\n"
2804 " Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n"
2805 " If no outputs are specified here, the sender pays the fee.\n"
2806 " [vout_index,...]\n"
2807 " \"replaceable\" (boolean, optional) Marks this transaction as BIP125 replaceable.\n"
2808 " Allows this transaction to be replaced by a transaction with higher fees\n"
2809 " \"conf_target\" (numeric, optional) Confirmation target (in blocks)\n"
2810 " \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
2811 " \"UNSET\"\n"
2812 " \"ECONOMICAL\"\n"
2813 " \"CONSERVATIVE\"\n"
2814 " }\n"
2815 " for backward compatibility: passing in a true instead of an object will result in {\"includeWatching\":true}\n"
2816 "\nResult:\n"
2817 "{\n"
2818 " \"hex\": \"value\", (string) The resulting raw transaction (hex-encoded string)\n"
2819 " \"fee\": n, (numeric) Fee in " + CURRENCY_UNIT + " the resulting transaction pays\n"
2820 " \"changepos\": n (numeric) The position of the added change output, or -1\n"
2821 "}\n"
2822 "\nExamples:\n"
2823 "\nCreate a transaction with no inputs\n"
2824 + HelpExampleCli("createrawtransaction", "\"[]\" \"{\\\"myaddress\\\":0.01}\"") +
2825 "\nAdd sufficient unsigned inputs to meet the output value\n"
2826 + HelpExampleCli("fundrawtransaction", "\"rawtransactionhex\"") +
2827 "\nSign the transaction\n"
2828 + HelpExampleCli("signrawtransaction", "\"fundedtransactionhex\"") +
2829 "\nSend the transaction\n"
2830 + HelpExampleCli("sendrawtransaction", "\"signedtransactionhex\"")
2833 RPCTypeCheck(request.params, {UniValue::VSTR});
2835 CCoinControl coinControl;
2836 int changePosition = -1;
2837 bool lockUnspents = false;
2838 UniValue subtractFeeFromOutputs;
2839 std::set<int> setSubtractFeeFromOutputs;
2841 if (!request.params[1].isNull()) {
2842 if (request.params[1].type() == UniValue::VBOOL) {
2843 // backward compatibility bool only fallback
2844 coinControl.fAllowWatchOnly = request.params[1].get_bool();
2846 else {
2847 RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VOBJ});
2849 UniValue options = request.params[1];
2851 RPCTypeCheckObj(options,
2853 {"changeAddress", UniValueType(UniValue::VSTR)},
2854 {"changePosition", UniValueType(UniValue::VNUM)},
2855 {"includeWatching", UniValueType(UniValue::VBOOL)},
2856 {"lockUnspents", UniValueType(UniValue::VBOOL)},
2857 {"reserveChangeKey", UniValueType(UniValue::VBOOL)}, // DEPRECATED (and ignored), should be removed in 0.16 or so.
2858 {"feeRate", UniValueType()}, // will be checked below
2859 {"subtractFeeFromOutputs", UniValueType(UniValue::VARR)},
2860 {"replaceable", UniValueType(UniValue::VBOOL)},
2861 {"conf_target", UniValueType(UniValue::VNUM)},
2862 {"estimate_mode", UniValueType(UniValue::VSTR)},
2864 true, true);
2866 if (options.exists("changeAddress")) {
2867 CBitcoinAddress address(options["changeAddress"].get_str());
2869 if (!address.IsValid())
2870 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "changeAddress must be a valid bitcoin address");
2872 coinControl.destChange = address.Get();
2875 if (options.exists("changePosition"))
2876 changePosition = options["changePosition"].get_int();
2878 if (options.exists("includeWatching"))
2879 coinControl.fAllowWatchOnly = options["includeWatching"].get_bool();
2881 if (options.exists("lockUnspents"))
2882 lockUnspents = options["lockUnspents"].get_bool();
2884 if (options.exists("feeRate"))
2886 coinControl.m_feerate = CFeeRate(AmountFromValue(options["feeRate"]));
2887 coinControl.fOverrideFeeRate = true;
2890 if (options.exists("subtractFeeFromOutputs"))
2891 subtractFeeFromOutputs = options["subtractFeeFromOutputs"].get_array();
2893 if (options.exists("replaceable")) {
2894 coinControl.signalRbf = options["replaceable"].get_bool();
2896 if (options.exists("conf_target")) {
2897 if (options.exists("feeRate")) {
2898 throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot specify both conf_target and feeRate");
2900 coinControl.m_confirm_target = ParseConfirmTarget(options["conf_target"]);
2902 if (options.exists("estimate_mode")) {
2903 if (options.exists("feeRate")) {
2904 throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot specify both estimate_mode and feeRate");
2906 if (!FeeModeFromString(options["estimate_mode"].get_str(), coinControl.m_fee_mode)) {
2907 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid estimate_mode parameter");
2913 // parse hex string from parameter
2914 CMutableTransaction tx;
2915 if (!DecodeHexTx(tx, request.params[0].get_str(), true))
2916 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
2918 if (tx.vout.size() == 0)
2919 throw JSONRPCError(RPC_INVALID_PARAMETER, "TX must have at least one output");
2921 if (changePosition != -1 && (changePosition < 0 || (unsigned int)changePosition > tx.vout.size()))
2922 throw JSONRPCError(RPC_INVALID_PARAMETER, "changePosition out of bounds");
2924 for (unsigned int idx = 0; idx < subtractFeeFromOutputs.size(); idx++) {
2925 int pos = subtractFeeFromOutputs[idx].get_int();
2926 if (setSubtractFeeFromOutputs.count(pos))
2927 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, duplicated position: %d", pos));
2928 if (pos < 0)
2929 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, negative position: %d", pos));
2930 if (pos >= int(tx.vout.size()))
2931 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, position too large: %d", pos));
2932 setSubtractFeeFromOutputs.insert(pos);
2935 CAmount nFeeOut;
2936 std::string strFailReason;
2938 if (!pwallet->FundTransaction(tx, nFeeOut, changePosition, strFailReason, lockUnspents, setSubtractFeeFromOutputs, coinControl)) {
2939 throw JSONRPCError(RPC_WALLET_ERROR, strFailReason);
2942 UniValue result(UniValue::VOBJ);
2943 result.push_back(Pair("hex", EncodeHexTx(tx)));
2944 result.push_back(Pair("changepos", changePosition));
2945 result.push_back(Pair("fee", ValueFromAmount(nFeeOut)));
2947 return result;
2950 UniValue bumpfee(const JSONRPCRequest& request)
2952 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2954 if (!EnsureWalletIsAvailable(pwallet, request.fHelp))
2955 return NullUniValue;
2957 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) {
2958 throw std::runtime_error(
2959 "bumpfee \"txid\" ( options ) \n"
2960 "\nBumps the fee of an opt-in-RBF transaction T, replacing it with a new transaction B.\n"
2961 "An opt-in RBF transaction with the given txid must be in the wallet.\n"
2962 "The command will pay the additional fee by decreasing (or perhaps removing) its change output.\n"
2963 "If the change output is not big enough to cover the increased fee, the command will currently fail\n"
2964 "instead of adding new inputs to compensate. (A future implementation could improve this.)\n"
2965 "The command will fail if the wallet or mempool contains a transaction that spends one of T's outputs.\n"
2966 "By default, the new fee will be calculated automatically using estimatefee.\n"
2967 "The user can specify a confirmation target for estimatefee.\n"
2968 "Alternatively, the user can specify totalFee, or use RPC settxfee to set a higher fee rate.\n"
2969 "At a minimum, the new fee rate must be high enough to pay an additional new relay fee (incrementalfee\n"
2970 "returned by getnetworkinfo) to enter the node's mempool.\n"
2971 "\nArguments:\n"
2972 "1. txid (string, required) The txid to be bumped\n"
2973 "2. options (object, optional)\n"
2974 " {\n"
2975 " \"confTarget\" (numeric, optional) Confirmation target (in blocks)\n"
2976 " \"totalFee\" (numeric, optional) Total fee (NOT feerate) to pay, in satoshis.\n"
2977 " In rare cases, the actual fee paid might be slightly higher than the specified\n"
2978 " totalFee if the tx change output has to be removed because it is too close to\n"
2979 " the dust threshold.\n"
2980 " \"replaceable\" (boolean, optional, default true) Whether the new transaction should still be\n"
2981 " marked bip-125 replaceable. If true, the sequence numbers in the transaction will\n"
2982 " be left unchanged from the original. If false, any input sequence numbers in the\n"
2983 " original transaction that were less than 0xfffffffe will be increased to 0xfffffffe\n"
2984 " so the new transaction will not be explicitly bip-125 replaceable (though it may\n"
2985 " still be replaceable in practice, for example if it has unconfirmed ancestors which\n"
2986 " are replaceable).\n"
2987 " \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
2988 " \"UNSET\"\n"
2989 " \"ECONOMICAL\"\n"
2990 " \"CONSERVATIVE\"\n"
2991 " }\n"
2992 "\nResult:\n"
2993 "{\n"
2994 " \"txid\": \"value\", (string) The id of the new transaction\n"
2995 " \"origfee\": n, (numeric) Fee of the replaced transaction\n"
2996 " \"fee\": n, (numeric) Fee of the new transaction\n"
2997 " \"errors\": [ str... ] (json array of strings) Errors encountered during processing (may be empty)\n"
2998 "}\n"
2999 "\nExamples:\n"
3000 "\nBump the fee, get the new transaction\'s txid\n" +
3001 HelpExampleCli("bumpfee", "<txid>"));
3004 RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VOBJ});
3005 uint256 hash;
3006 hash.SetHex(request.params[0].get_str());
3008 // optional parameters
3009 CAmount totalFee = 0;
3010 CCoinControl coin_control;
3011 coin_control.signalRbf = true;
3012 if (!request.params[1].isNull()) {
3013 UniValue options = request.params[1];
3014 RPCTypeCheckObj(options,
3016 {"confTarget", UniValueType(UniValue::VNUM)},
3017 {"totalFee", UniValueType(UniValue::VNUM)},
3018 {"replaceable", UniValueType(UniValue::VBOOL)},
3019 {"estimate_mode", UniValueType(UniValue::VSTR)},
3021 true, true);
3023 if (options.exists("confTarget") && options.exists("totalFee")) {
3024 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.");
3025 } else if (options.exists("confTarget")) { // TODO: alias this to conf_target
3026 coin_control.m_confirm_target = ParseConfirmTarget(options["confTarget"]);
3027 } else if (options.exists("totalFee")) {
3028 totalFee = options["totalFee"].get_int64();
3029 if (totalFee <= 0) {
3030 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid totalFee %s (must be greater than 0)", FormatMoney(totalFee)));
3034 if (options.exists("replaceable")) {
3035 coin_control.signalRbf = options["replaceable"].get_bool();
3037 if (options.exists("estimate_mode")) {
3038 if (!FeeModeFromString(options["estimate_mode"].get_str(), coin_control.m_fee_mode)) {
3039 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid estimate_mode parameter");
3044 LOCK2(cs_main, pwallet->cs_wallet);
3045 EnsureWalletIsUnlocked(pwallet);
3047 CFeeBumper feeBump(pwallet, hash, coin_control, totalFee);
3048 BumpFeeResult res = feeBump.getResult();
3049 if (res != BumpFeeResult::OK)
3051 switch(res) {
3052 case BumpFeeResult::INVALID_ADDRESS_OR_KEY:
3053 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, feeBump.getErrors()[0]);
3054 break;
3055 case BumpFeeResult::INVALID_REQUEST:
3056 throw JSONRPCError(RPC_INVALID_REQUEST, feeBump.getErrors()[0]);
3057 break;
3058 case BumpFeeResult::INVALID_PARAMETER:
3059 throw JSONRPCError(RPC_INVALID_PARAMETER, feeBump.getErrors()[0]);
3060 break;
3061 case BumpFeeResult::WALLET_ERROR:
3062 throw JSONRPCError(RPC_WALLET_ERROR, feeBump.getErrors()[0]);
3063 break;
3064 default:
3065 throw JSONRPCError(RPC_MISC_ERROR, feeBump.getErrors()[0]);
3066 break;
3070 // sign bumped transaction
3071 if (!feeBump.signTransaction(pwallet)) {
3072 throw JSONRPCError(RPC_WALLET_ERROR, "Can't sign transaction.");
3074 // commit the bumped transaction
3075 if(!feeBump.commit(pwallet)) {
3076 throw JSONRPCError(RPC_WALLET_ERROR, feeBump.getErrors()[0]);
3078 UniValue result(UniValue::VOBJ);
3079 result.push_back(Pair("txid", feeBump.getBumpedTxId().GetHex()));
3080 result.push_back(Pair("origfee", ValueFromAmount(feeBump.getOldFee())));
3081 result.push_back(Pair("fee", ValueFromAmount(feeBump.getNewFee())));
3082 UniValue errors(UniValue::VARR);
3083 for (const std::string& err: feeBump.getErrors())
3084 errors.push_back(err);
3085 result.push_back(Pair("errors", errors));
3087 return result;
3090 UniValue generate(const JSONRPCRequest& request)
3092 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
3094 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
3095 return NullUniValue;
3098 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) {
3099 throw std::runtime_error(
3100 "generate nblocks ( maxtries )\n"
3101 "\nMine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.\n"
3102 "\nArguments:\n"
3103 "1. nblocks (numeric, required) How many blocks are generated immediately.\n"
3104 "2. maxtries (numeric, optional) How many iterations to try (default = 1000000).\n"
3105 "\nResult:\n"
3106 "[ blockhashes ] (array) hashes of blocks generated\n"
3107 "\nExamples:\n"
3108 "\nGenerate 11 blocks\n"
3109 + HelpExampleCli("generate", "11")
3113 int num_generate = request.params[0].get_int();
3114 uint64_t max_tries = 1000000;
3115 if (request.params.size() > 1 && !request.params[1].isNull()) {
3116 max_tries = request.params[1].get_int();
3119 std::shared_ptr<CReserveScript> coinbase_script;
3120 pwallet->GetScriptForMining(coinbase_script);
3122 // If the keypool is exhausted, no script is returned at all. Catch this.
3123 if (!coinbase_script) {
3124 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
3127 //throw an error if no script was provided
3128 if (coinbase_script->reserveScript.empty()) {
3129 throw JSONRPCError(RPC_INTERNAL_ERROR, "No coinbase script available");
3132 return generateBlocks(coinbase_script, num_generate, max_tries, true);
3135 extern UniValue abortrescan(const JSONRPCRequest& request); // in rpcdump.cpp
3136 extern UniValue dumpprivkey(const JSONRPCRequest& request); // in rpcdump.cpp
3137 extern UniValue importprivkey(const JSONRPCRequest& request);
3138 extern UniValue importaddress(const JSONRPCRequest& request);
3139 extern UniValue importpubkey(const JSONRPCRequest& request);
3140 extern UniValue dumpwallet(const JSONRPCRequest& request);
3141 extern UniValue importwallet(const JSONRPCRequest& request);
3142 extern UniValue importprunedfunds(const JSONRPCRequest& request);
3143 extern UniValue removeprunedfunds(const JSONRPCRequest& request);
3144 extern UniValue importmulti(const JSONRPCRequest& request);
3146 static const CRPCCommand commands[] =
3147 { // category name actor (function) okSafeMode
3148 // --------------------- ------------------------ ----------------------- ----------
3149 { "rawtransactions", "fundrawtransaction", &fundrawtransaction, false, {"hexstring","options"} },
3150 { "hidden", "resendwallettransactions", &resendwallettransactions, true, {} },
3151 { "wallet", "abandontransaction", &abandontransaction, false, {"txid"} },
3152 { "wallet", "abortrescan", &abortrescan, false, {} },
3153 { "wallet", "addmultisigaddress", &addmultisigaddress, true, {"nrequired","keys","account"} },
3154 { "wallet", "addwitnessaddress", &addwitnessaddress, true, {"address"} },
3155 { "wallet", "backupwallet", &backupwallet, true, {"destination"} },
3156 { "wallet", "bumpfee", &bumpfee, true, {"txid", "options"} },
3157 { "wallet", "dumpprivkey", &dumpprivkey, true, {"address"} },
3158 { "wallet", "dumpwallet", &dumpwallet, true, {"filename"} },
3159 { "wallet", "encryptwallet", &encryptwallet, true, {"passphrase"} },
3160 { "wallet", "getaccountaddress", &getaccountaddress, true, {"account"} },
3161 { "wallet", "getaccount", &getaccount, true, {"address"} },
3162 { "wallet", "getaddressesbyaccount", &getaddressesbyaccount, true, {"account"} },
3163 { "wallet", "getbalance", &getbalance, false, {"account","minconf","include_watchonly"} },
3164 { "wallet", "getnewaddress", &getnewaddress, true, {"account"} },
3165 { "wallet", "getrawchangeaddress", &getrawchangeaddress, true, {} },
3166 { "wallet", "getreceivedbyaccount", &getreceivedbyaccount, false, {"account","minconf"} },
3167 { "wallet", "getreceivedbyaddress", &getreceivedbyaddress, false, {"address","minconf"} },
3168 { "wallet", "gettransaction", &gettransaction, false, {"txid","include_watchonly"} },
3169 { "wallet", "getunconfirmedbalance", &getunconfirmedbalance, false, {} },
3170 { "wallet", "getwalletinfo", &getwalletinfo, false, {} },
3171 { "wallet", "importmulti", &importmulti, true, {"requests","options"} },
3172 { "wallet", "importprivkey", &importprivkey, true, {"privkey","label","rescan"} },
3173 { "wallet", "importwallet", &importwallet, true, {"filename"} },
3174 { "wallet", "importaddress", &importaddress, true, {"address","label","rescan","p2sh"} },
3175 { "wallet", "importprunedfunds", &importprunedfunds, true, {"rawtransaction","txoutproof"} },
3176 { "wallet", "importpubkey", &importpubkey, true, {"pubkey","label","rescan"} },
3177 { "wallet", "keypoolrefill", &keypoolrefill, true, {"newsize"} },
3178 { "wallet", "listaccounts", &listaccounts, false, {"minconf","include_watchonly"} },
3179 { "wallet", "listaddressgroupings", &listaddressgroupings, false, {} },
3180 { "wallet", "listlockunspent", &listlockunspent, false, {} },
3181 { "wallet", "listreceivedbyaccount", &listreceivedbyaccount, false, {"minconf","include_empty","include_watchonly"} },
3182 { "wallet", "listreceivedbyaddress", &listreceivedbyaddress, false, {"minconf","include_empty","include_watchonly"} },
3183 { "wallet", "listsinceblock", &listsinceblock, false, {"blockhash","target_confirmations","include_watchonly","include_removed"} },
3184 { "wallet", "listtransactions", &listtransactions, false, {"account","count","skip","include_watchonly"} },
3185 { "wallet", "listunspent", &listunspent, false, {"minconf","maxconf","addresses","include_unsafe","query_options"} },
3186 { "wallet", "listwallets", &listwallets, true, {} },
3187 { "wallet", "lockunspent", &lockunspent, true, {"unlock","transactions"} },
3188 { "wallet", "move", &movecmd, false, {"fromaccount","toaccount","amount","minconf","comment"} },
3189 { "wallet", "sendfrom", &sendfrom, false, {"fromaccount","toaddress","amount","minconf","comment","comment_to"} },
3190 { "wallet", "sendmany", &sendmany, false, {"fromaccount","amounts","minconf","comment","subtractfeefrom","replaceable","conf_target","estimate_mode"} },
3191 { "wallet", "sendtoaddress", &sendtoaddress, false, {"address","amount","comment","comment_to","subtractfeefromamount","replaceable","conf_target","estimate_mode"} },
3192 { "wallet", "setaccount", &setaccount, true, {"address","account"} },
3193 { "wallet", "settxfee", &settxfee, true, {"amount"} },
3194 { "wallet", "signmessage", &signmessage, true, {"address","message"} },
3195 { "wallet", "walletlock", &walletlock, true, {} },
3196 { "wallet", "walletpassphrasechange", &walletpassphrasechange, true, {"oldpassphrase","newpassphrase"} },
3197 { "wallet", "walletpassphrase", &walletpassphrase, true, {"passphrase","timeout"} },
3198 { "wallet", "removeprunedfunds", &removeprunedfunds, true, {"txid"} },
3200 { "generating", "generate", &generate, true, {"nblocks","maxtries"} },
3203 void RegisterWalletRPCCommands(CRPCTable &t)
3205 if (gArgs.GetBoolArg("-disablewallet", false))
3206 return;
3208 for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
3209 t.appendCommand(commands[vcidx].name, &commands[vcidx]);