Merge #10789: Punctuation/grammer fixes in rpcwallet.cpp
[bitcoinplatinum.git] / src / wallet / rpcwallet.cpp
blobfb4d30d493419a1c0a800e34df85c1c807a231ff
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_INVALID_PARAMETER, "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) {
61 if (!avoidException)
62 throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (disabled)");
63 else
64 return false;
66 return true;
69 void EnsureWalletIsUnlocked(CWallet * const pwallet)
71 if (pwallet->IsLocked()) {
72 throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please enter the wallet passphrase with walletpassphrase first.");
76 void WalletTxToJSON(const CWalletTx& wtx, UniValue& entry)
78 int confirms = wtx.GetDepthInMainChain();
79 entry.push_back(Pair("confirmations", confirms));
80 if (wtx.IsCoinBase())
81 entry.push_back(Pair("generated", true));
82 if (confirms > 0)
84 entry.push_back(Pair("blockhash", wtx.hashBlock.GetHex()));
85 entry.push_back(Pair("blockindex", wtx.nIndex));
86 entry.push_back(Pair("blocktime", mapBlockIndex[wtx.hashBlock]->GetBlockTime()));
87 } else {
88 entry.push_back(Pair("trusted", wtx.IsTrusted()));
90 uint256 hash = wtx.GetHash();
91 entry.push_back(Pair("txid", hash.GetHex()));
92 UniValue conflicts(UniValue::VARR);
93 for (const uint256& conflict : wtx.GetConflicts())
94 conflicts.push_back(conflict.GetHex());
95 entry.push_back(Pair("walletconflicts", conflicts));
96 entry.push_back(Pair("time", wtx.GetTxTime()));
97 entry.push_back(Pair("timereceived", (int64_t)wtx.nTimeReceived));
99 // Add opt-in RBF status
100 std::string rbfStatus = "no";
101 if (confirms <= 0) {
102 LOCK(mempool.cs);
103 RBFTransactionState rbfState = IsRBFOptIn(wtx, mempool);
104 if (rbfState == RBF_TRANSACTIONSTATE_UNKNOWN)
105 rbfStatus = "unknown";
106 else if (rbfState == RBF_TRANSACTIONSTATE_REPLACEABLE_BIP125)
107 rbfStatus = "yes";
109 entry.push_back(Pair("bip125-replaceable", rbfStatus));
111 for (const std::pair<std::string, std::string>& item : wtx.mapValue)
112 entry.push_back(Pair(item.first, item.second));
115 std::string AccountFromValue(const UniValue& value)
117 std::string strAccount = value.get_str();
118 if (strAccount == "*")
119 throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME, "Invalid account name");
120 return strAccount;
123 UniValue getnewaddress(const JSONRPCRequest& request)
125 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
126 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
127 return NullUniValue;
130 if (request.fHelp || request.params.size() > 1)
131 throw std::runtime_error(
132 "getnewaddress ( \"account\" )\n"
133 "\nReturns a new Bitcoin address for receiving payments.\n"
134 "If 'account' is specified (DEPRECATED), it is added to the address book \n"
135 "so payments received with the address will be credited to 'account'.\n"
136 "\nArguments:\n"
137 "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"
138 "\nResult:\n"
139 "\"address\" (string) The new bitcoin address\n"
140 "\nExamples:\n"
141 + HelpExampleCli("getnewaddress", "")
142 + HelpExampleRpc("getnewaddress", "")
145 LOCK2(cs_main, pwallet->cs_wallet);
147 // Parse the account first so we don't generate a key if there's an error
148 std::string strAccount;
149 if (!request.params[0].isNull())
150 strAccount = AccountFromValue(request.params[0]);
152 if (!pwallet->IsLocked()) {
153 pwallet->TopUpKeyPool();
156 // Generate a new key that is added to wallet
157 CPubKey newKey;
158 if (!pwallet->GetKeyFromPool(newKey)) {
159 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
161 CKeyID keyID = newKey.GetID();
163 pwallet->SetAddressBook(keyID, strAccount, "receive");
165 return CBitcoinAddress(keyID).ToString();
169 CBitcoinAddress GetAccountAddress(CWallet* const pwallet, std::string strAccount, bool bForceNew=false)
171 CPubKey pubKey;
172 if (!pwallet->GetAccountPubkey(pubKey, strAccount, bForceNew)) {
173 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
176 return CBitcoinAddress(pubKey.GetID());
179 UniValue getaccountaddress(const JSONRPCRequest& request)
181 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
182 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
183 return NullUniValue;
186 if (request.fHelp || request.params.size() != 1)
187 throw std::runtime_error(
188 "getaccountaddress \"account\"\n"
189 "\nDEPRECATED. Returns the current Bitcoin address for receiving payments to this account.\n"
190 "\nArguments:\n"
191 "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"
192 "\nResult:\n"
193 "\"address\" (string) The account bitcoin address\n"
194 "\nExamples:\n"
195 + HelpExampleCli("getaccountaddress", "")
196 + HelpExampleCli("getaccountaddress", "\"\"")
197 + HelpExampleCli("getaccountaddress", "\"myaccount\"")
198 + HelpExampleRpc("getaccountaddress", "\"myaccount\"")
201 LOCK2(cs_main, pwallet->cs_wallet);
203 // Parse the account first so we don't generate a key if there's an error
204 std::string strAccount = AccountFromValue(request.params[0]);
206 UniValue ret(UniValue::VSTR);
208 ret = GetAccountAddress(pwallet, strAccount).ToString();
209 return ret;
213 UniValue getrawchangeaddress(const JSONRPCRequest& request)
215 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
216 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
217 return NullUniValue;
220 if (request.fHelp || request.params.size() > 0)
221 throw std::runtime_error(
222 "getrawchangeaddress\n"
223 "\nReturns a new Bitcoin address, for receiving change.\n"
224 "This is for use with raw transactions, NOT normal use.\n"
225 "\nResult:\n"
226 "\"address\" (string) The address\n"
227 "\nExamples:\n"
228 + HelpExampleCli("getrawchangeaddress", "")
229 + HelpExampleRpc("getrawchangeaddress", "")
232 LOCK2(cs_main, pwallet->cs_wallet);
234 if (!pwallet->IsLocked()) {
235 pwallet->TopUpKeyPool();
238 CReserveKey reservekey(pwallet);
239 CPubKey vchPubKey;
240 if (!reservekey.GetReservedKey(vchPubKey, true))
241 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
243 reservekey.KeepKey();
245 CKeyID keyID = vchPubKey.GetID();
247 return CBitcoinAddress(keyID).ToString();
251 UniValue setaccount(const JSONRPCRequest& request)
253 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
254 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
255 return NullUniValue;
258 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
259 throw std::runtime_error(
260 "setaccount \"address\" \"account\"\n"
261 "\nDEPRECATED. Sets the account associated with the given address.\n"
262 "\nArguments:\n"
263 "1. \"address\" (string, required) The bitcoin address to be associated with an account.\n"
264 "2. \"account\" (string, required) The account to assign the address to.\n"
265 "\nExamples:\n"
266 + HelpExampleCli("setaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"tabby\"")
267 + HelpExampleRpc("setaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"tabby\"")
270 LOCK2(cs_main, pwallet->cs_wallet);
272 CBitcoinAddress address(request.params[0].get_str());
273 if (!address.IsValid())
274 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
276 std::string strAccount;
277 if (request.params.size() > 1)
278 strAccount = AccountFromValue(request.params[1]);
280 // Only add the account if the address is yours.
281 if (IsMine(*pwallet, address.Get())) {
282 // Detect when changing the account of an address that is the 'unused current key' of another account:
283 if (pwallet->mapAddressBook.count(address.Get())) {
284 std::string strOldAccount = pwallet->mapAddressBook[address.Get()].name;
285 if (address == GetAccountAddress(pwallet, strOldAccount)) {
286 GetAccountAddress(pwallet, strOldAccount, true);
289 pwallet->SetAddressBook(address.Get(), strAccount, "receive");
291 else
292 throw JSONRPCError(RPC_MISC_ERROR, "setaccount can only be used with own address");
294 return NullUniValue;
298 UniValue getaccount(const JSONRPCRequest& request)
300 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
301 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
302 return NullUniValue;
305 if (request.fHelp || request.params.size() != 1)
306 throw std::runtime_error(
307 "getaccount \"address\"\n"
308 "\nDEPRECATED. Returns the account associated with the given address.\n"
309 "\nArguments:\n"
310 "1. \"address\" (string, required) The bitcoin address for account lookup.\n"
311 "\nResult:\n"
312 "\"accountname\" (string) the account address\n"
313 "\nExamples:\n"
314 + HelpExampleCli("getaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\"")
315 + HelpExampleRpc("getaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\"")
318 LOCK2(cs_main, pwallet->cs_wallet);
320 CBitcoinAddress address(request.params[0].get_str());
321 if (!address.IsValid())
322 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
324 std::string strAccount;
325 std::map<CTxDestination, CAddressBookData>::iterator mi = pwallet->mapAddressBook.find(address.Get());
326 if (mi != pwallet->mapAddressBook.end() && !(*mi).second.name.empty()) {
327 strAccount = (*mi).second.name;
329 return strAccount;
333 UniValue getaddressesbyaccount(const JSONRPCRequest& request)
335 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
336 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
337 return NullUniValue;
340 if (request.fHelp || request.params.size() != 1)
341 throw std::runtime_error(
342 "getaddressesbyaccount \"account\"\n"
343 "\nDEPRECATED. Returns the list of addresses for the given account.\n"
344 "\nArguments:\n"
345 "1. \"account\" (string, required) The account name.\n"
346 "\nResult:\n"
347 "[ (json array of string)\n"
348 " \"address\" (string) a bitcoin address associated with the given account\n"
349 " ,...\n"
350 "]\n"
351 "\nExamples:\n"
352 + HelpExampleCli("getaddressesbyaccount", "\"tabby\"")
353 + HelpExampleRpc("getaddressesbyaccount", "\"tabby\"")
356 LOCK2(cs_main, pwallet->cs_wallet);
358 std::string strAccount = AccountFromValue(request.params[0]);
360 // Find all addresses that have the given account
361 UniValue ret(UniValue::VARR);
362 for (const std::pair<CBitcoinAddress, CAddressBookData>& item : pwallet->mapAddressBook) {
363 const CBitcoinAddress& address = item.first;
364 const std::string& strName = item.second.name;
365 if (strName == strAccount)
366 ret.push_back(address.ToString());
368 return ret;
371 static void SendMoney(CWallet * const pwallet, const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, CWalletTx& wtxNew, const CCoinControl& coin_control)
373 CAmount curBalance = pwallet->GetBalance();
375 // Check amount
376 if (nValue <= 0)
377 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid amount");
379 if (nValue > curBalance)
380 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Insufficient funds");
382 if (pwallet->GetBroadcastTransactions() && !g_connman) {
383 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
386 // Parse Bitcoin address
387 CScript scriptPubKey = GetScriptForDestination(address);
389 // Create and send the transaction
390 CReserveKey reservekey(pwallet);
391 CAmount nFeeRequired;
392 std::string strError;
393 std::vector<CRecipient> vecSend;
394 int nChangePosRet = -1;
395 CRecipient recipient = {scriptPubKey, nValue, fSubtractFeeFromAmount};
396 vecSend.push_back(recipient);
397 if (!pwallet->CreateTransaction(vecSend, wtxNew, reservekey, nFeeRequired, nChangePosRet, strError, coin_control)) {
398 if (!fSubtractFeeFromAmount && nValue + nFeeRequired > curBalance)
399 strError = strprintf("Error: This transaction requires a transaction fee of at least %s", FormatMoney(nFeeRequired));
400 throw JSONRPCError(RPC_WALLET_ERROR, strError);
402 CValidationState state;
403 if (!pwallet->CommitTransaction(wtxNew, reservekey, g_connman.get(), state)) {
404 strError = strprintf("Error: The transaction was rejected! Reason given: %s", state.GetRejectReason());
405 throw JSONRPCError(RPC_WALLET_ERROR, strError);
409 UniValue sendtoaddress(const JSONRPCRequest& request)
411 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
412 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
413 return NullUniValue;
416 if (request.fHelp || request.params.size() < 2 || request.params.size() > 8)
417 throw std::runtime_error(
418 "sendtoaddress \"address\" amount ( \"comment\" \"comment_to\" subtractfeefromamount replaceable conf_target \"estimate_mode\")\n"
419 "\nSend an amount to a given address.\n"
420 + HelpRequiringPassphrase(pwallet) +
421 "\nArguments:\n"
422 "1. \"address\" (string, required) The bitcoin address to send to.\n"
423 "2. \"amount\" (numeric or string, required) The amount in " + CURRENCY_UNIT + " to send. eg 0.1\n"
424 "3. \"comment\" (string, optional) A comment used to store what the transaction is for. \n"
425 " This is not part of the transaction, just kept in your wallet.\n"
426 "4. \"comment_to\" (string, optional) A comment to store the name of the person or organization \n"
427 " to which you're sending the transaction. This is not part of the \n"
428 " transaction, just kept in your wallet.\n"
429 "5. subtractfeefromamount (boolean, optional, default=false) The fee will be deducted from the amount being sent.\n"
430 " The recipient will receive less bitcoins than you enter in the amount field.\n"
431 "6. replaceable (boolean, optional) Allow this transaction to be replaced by a transaction with higher fees via BIP 125\n"
432 "7. conf_target (numeric, optional) Confirmation target (in blocks)\n"
433 "8. \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
434 " \"UNSET\"\n"
435 " \"ECONOMICAL\"\n"
436 " \"CONSERVATIVE\"\n"
437 "\nResult:\n"
438 "\"txid\" (string) The transaction id.\n"
439 "\nExamples:\n"
440 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.1")
441 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.1 \"donation\" \"seans outpost\"")
442 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.1 \"\" \"\" true")
443 + HelpExampleRpc("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\", 0.1, \"donation\", \"seans outpost\"")
446 LOCK2(cs_main, pwallet->cs_wallet);
448 CBitcoinAddress address(request.params[0].get_str());
449 if (!address.IsValid())
450 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
452 // Amount
453 CAmount nAmount = AmountFromValue(request.params[1]);
454 if (nAmount <= 0)
455 throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
457 // Wallet comments
458 CWalletTx wtx;
459 if (request.params.size() > 2 && !request.params[2].isNull() && !request.params[2].get_str().empty())
460 wtx.mapValue["comment"] = request.params[2].get_str();
461 if (request.params.size() > 3 && !request.params[3].isNull() && !request.params[3].get_str().empty())
462 wtx.mapValue["to"] = request.params[3].get_str();
464 bool fSubtractFeeFromAmount = false;
465 if (request.params.size() > 4 && !request.params[4].isNull()) {
466 fSubtractFeeFromAmount = request.params[4].get_bool();
469 CCoinControl coin_control;
470 if (request.params.size() > 5 && !request.params[5].isNull()) {
471 coin_control.signalRbf = request.params[5].get_bool();
474 if (request.params.size() > 6 && !request.params[6].isNull()) {
475 coin_control.m_confirm_target = ParseConfirmTarget(request.params[6]);
478 if (request.params.size() > 7 && !request.params[7].isNull()) {
479 if (!FeeModeFromString(request.params[7].get_str(), coin_control.m_fee_mode)) {
480 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid estimate_mode parameter");
485 EnsureWalletIsUnlocked(pwallet);
487 SendMoney(pwallet, address.Get(), nAmount, fSubtractFeeFromAmount, wtx, coin_control);
489 return wtx.GetHash().GetHex();
492 UniValue listaddressgroupings(const JSONRPCRequest& request)
494 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
495 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
496 return NullUniValue;
499 if (request.fHelp || request.params.size() != 0)
500 throw std::runtime_error(
501 "listaddressgroupings\n"
502 "\nLists groups of addresses which have had their common ownership\n"
503 "made public by common use as inputs or as the resulting change\n"
504 "in past transactions\n"
505 "\nResult:\n"
506 "[\n"
507 " [\n"
508 " [\n"
509 " \"address\", (string) The bitcoin address\n"
510 " amount, (numeric) The amount in " + CURRENCY_UNIT + "\n"
511 " \"account\" (string, optional) DEPRECATED. The account\n"
512 " ]\n"
513 " ,...\n"
514 " ]\n"
515 " ,...\n"
516 "]\n"
517 "\nExamples:\n"
518 + HelpExampleCli("listaddressgroupings", "")
519 + HelpExampleRpc("listaddressgroupings", "")
522 LOCK2(cs_main, pwallet->cs_wallet);
524 UniValue jsonGroupings(UniValue::VARR);
525 std::map<CTxDestination, CAmount> balances = pwallet->GetAddressBalances();
526 for (std::set<CTxDestination> grouping : pwallet->GetAddressGroupings()) {
527 UniValue jsonGrouping(UniValue::VARR);
528 for (CTxDestination address : grouping)
530 UniValue addressInfo(UniValue::VARR);
531 addressInfo.push_back(CBitcoinAddress(address).ToString());
532 addressInfo.push_back(ValueFromAmount(balances[address]));
534 if (pwallet->mapAddressBook.find(CBitcoinAddress(address).Get()) != pwallet->mapAddressBook.end()) {
535 addressInfo.push_back(pwallet->mapAddressBook.find(CBitcoinAddress(address).Get())->second.name);
538 jsonGrouping.push_back(addressInfo);
540 jsonGroupings.push_back(jsonGrouping);
542 return jsonGroupings;
545 UniValue signmessage(const JSONRPCRequest& request)
547 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
548 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
549 return NullUniValue;
552 if (request.fHelp || request.params.size() != 2)
553 throw std::runtime_error(
554 "signmessage \"address\" \"message\"\n"
555 "\nSign a message with the private key of an address"
556 + HelpRequiringPassphrase(pwallet) + "\n"
557 "\nArguments:\n"
558 "1. \"address\" (string, required) The bitcoin address to use for the private key.\n"
559 "2. \"message\" (string, required) The message to create a signature of.\n"
560 "\nResult:\n"
561 "\"signature\" (string) The signature of the message encoded in base 64\n"
562 "\nExamples:\n"
563 "\nUnlock the wallet for 30 seconds\n"
564 + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
565 "\nCreate the signature\n"
566 + HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
567 "\nVerify the signature\n"
568 + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
569 "\nAs json rpc\n"
570 + HelpExampleRpc("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"my message\"")
573 LOCK2(cs_main, pwallet->cs_wallet);
575 EnsureWalletIsUnlocked(pwallet);
577 std::string strAddress = request.params[0].get_str();
578 std::string strMessage = request.params[1].get_str();
580 CBitcoinAddress addr(strAddress);
581 if (!addr.IsValid())
582 throw JSONRPCError(RPC_TYPE_ERROR, "Invalid address");
584 CKeyID keyID;
585 if (!addr.GetKeyID(keyID))
586 throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
588 CKey key;
589 if (!pwallet->GetKey(keyID, key)) {
590 throw JSONRPCError(RPC_WALLET_ERROR, "Private key not available");
593 CHashWriter ss(SER_GETHASH, 0);
594 ss << strMessageMagic;
595 ss << strMessage;
597 std::vector<unsigned char> vchSig;
598 if (!key.SignCompact(ss.GetHash(), vchSig))
599 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");
601 return EncodeBase64(&vchSig[0], vchSig.size());
604 UniValue getreceivedbyaddress(const JSONRPCRequest& request)
606 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
607 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
608 return NullUniValue;
611 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
612 throw std::runtime_error(
613 "getreceivedbyaddress \"address\" ( minconf )\n"
614 "\nReturns the total amount received by the given address in transactions with at least minconf confirmations.\n"
615 "\nArguments:\n"
616 "1. \"address\" (string, required) The bitcoin address for transactions.\n"
617 "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n"
618 "\nResult:\n"
619 "amount (numeric) The total amount in " + CURRENCY_UNIT + " received at this address.\n"
620 "\nExamples:\n"
621 "\nThe amount from transactions with at least 1 confirmation\n"
622 + HelpExampleCli("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\"") +
623 "\nThe amount including unconfirmed transactions, zero confirmations\n"
624 + HelpExampleCli("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" 0") +
625 "\nThe amount with at least 6 confirmations\n"
626 + HelpExampleCli("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" 6") +
627 "\nAs a json rpc call\n"
628 + HelpExampleRpc("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", 6")
631 LOCK2(cs_main, pwallet->cs_wallet);
633 // Bitcoin address
634 CBitcoinAddress address = CBitcoinAddress(request.params[0].get_str());
635 if (!address.IsValid())
636 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
637 CScript scriptPubKey = GetScriptForDestination(address.Get());
638 if (!IsMine(*pwallet, scriptPubKey)) {
639 return ValueFromAmount(0);
642 // Minimum confirmations
643 int nMinDepth = 1;
644 if (!request.params[1].isNull())
645 nMinDepth = request.params[1].get_int();
647 // Tally
648 CAmount nAmount = 0;
649 for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
650 const CWalletTx& wtx = pairWtx.second;
651 if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
652 continue;
654 for (const CTxOut& txout : wtx.tx->vout)
655 if (txout.scriptPubKey == scriptPubKey)
656 if (wtx.GetDepthInMainChain() >= nMinDepth)
657 nAmount += txout.nValue;
660 return ValueFromAmount(nAmount);
664 UniValue getreceivedbyaccount(const JSONRPCRequest& request)
666 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
667 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
668 return NullUniValue;
671 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
672 throw std::runtime_error(
673 "getreceivedbyaccount \"account\" ( minconf )\n"
674 "\nDEPRECATED. Returns the total amount received by addresses with <account> in transactions with at least [minconf] confirmations.\n"
675 "\nArguments:\n"
676 "1. \"account\" (string, required) The selected account, may be the default account using \"\".\n"
677 "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n"
678 "\nResult:\n"
679 "amount (numeric) The total amount in " + CURRENCY_UNIT + " received for this account.\n"
680 "\nExamples:\n"
681 "\nAmount received by the default account with at least 1 confirmation\n"
682 + HelpExampleCli("getreceivedbyaccount", "\"\"") +
683 "\nAmount received at the tabby account including unconfirmed amounts with zero confirmations\n"
684 + HelpExampleCli("getreceivedbyaccount", "\"tabby\" 0") +
685 "\nThe amount with at least 6 confirmations\n"
686 + HelpExampleCli("getreceivedbyaccount", "\"tabby\" 6") +
687 "\nAs a json rpc call\n"
688 + HelpExampleRpc("getreceivedbyaccount", "\"tabby\", 6")
691 LOCK2(cs_main, pwallet->cs_wallet);
693 // Minimum confirmations
694 int nMinDepth = 1;
695 if (!request.params[1].isNull())
696 nMinDepth = request.params[1].get_int();
698 // Get the set of pub keys assigned to account
699 std::string strAccount = AccountFromValue(request.params[0]);
700 std::set<CTxDestination> setAddress = pwallet->GetAccountAddresses(strAccount);
702 // Tally
703 CAmount nAmount = 0;
704 for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
705 const CWalletTx& wtx = pairWtx.second;
706 if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
707 continue;
709 for (const CTxOut& txout : wtx.tx->vout)
711 CTxDestination address;
712 if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*pwallet, address) && setAddress.count(address)) {
713 if (wtx.GetDepthInMainChain() >= nMinDepth)
714 nAmount += txout.nValue;
719 return ValueFromAmount(nAmount);
723 UniValue getbalance(const JSONRPCRequest& request)
725 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
726 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
727 return NullUniValue;
730 if (request.fHelp || request.params.size() > 3)
731 throw std::runtime_error(
732 "getbalance ( \"account\" minconf include_watchonly )\n"
733 "\nIf account is not specified, returns the server's total available balance.\n"
734 "If account is specified (DEPRECATED), returns the balance in the account.\n"
735 "Note that the account \"\" is not the same as leaving the parameter out.\n"
736 "The server total may be different to the balance in the default \"\" account.\n"
737 "\nArguments:\n"
738 "1. \"account\" (string, optional) DEPRECATED. The account string may be given as a\n"
739 " specific account name to find the balance associated with wallet keys in\n"
740 " a named account, or as the empty string (\"\") to find the balance\n"
741 " associated with wallet keys not in any named account, or as \"*\" to find\n"
742 " the balance associated with all wallet keys regardless of account.\n"
743 " When this option is specified, it calculates the balance in a different\n"
744 " way than when it is not specified, and which can count spends twice when\n"
745 " there are conflicting pending transactions (such as those created by\n"
746 " the bumpfee command), temporarily resulting in low or even negative\n"
747 " balances. In general, account balance calculation is not considered\n"
748 " reliable and has resulted in confusing outcomes, so it is recommended to\n"
749 " avoid passing this argument.\n"
750 "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n"
751 "3. include_watchonly (bool, optional, default=false) Also include balance in watch-only addresses (see 'importaddress')\n"
752 "\nResult:\n"
753 "amount (numeric) The total amount in " + CURRENCY_UNIT + " received for this account.\n"
754 "\nExamples:\n"
755 "\nThe total amount in the wallet with 1 or more confirmations\n"
756 + HelpExampleCli("getbalance", "") +
757 "\nThe total amount in the wallet at least 6 blocks confirmed\n"
758 + HelpExampleCli("getbalance", "\"*\" 6") +
759 "\nAs a json rpc call\n"
760 + HelpExampleRpc("getbalance", "\"*\", 6")
763 LOCK2(cs_main, pwallet->cs_wallet);
765 if (request.params.size() == 0)
766 return ValueFromAmount(pwallet->GetBalance());
768 const std::string& account_param = request.params[0].get_str();
769 const std::string* account = account_param != "*" ? &account_param : nullptr;
771 int nMinDepth = 1;
772 if (!request.params[1].isNull())
773 nMinDepth = request.params[1].get_int();
774 isminefilter filter = ISMINE_SPENDABLE;
775 if(!request.params[2].isNull())
776 if(request.params[2].get_bool())
777 filter = filter | ISMINE_WATCH_ONLY;
779 return ValueFromAmount(pwallet->GetLegacyBalance(filter, nMinDepth, account));
782 UniValue getunconfirmedbalance(const JSONRPCRequest &request)
784 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
785 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
786 return NullUniValue;
789 if (request.fHelp || request.params.size() > 0)
790 throw std::runtime_error(
791 "getunconfirmedbalance\n"
792 "Returns the server's total unconfirmed balance\n");
794 LOCK2(cs_main, pwallet->cs_wallet);
796 return ValueFromAmount(pwallet->GetUnconfirmedBalance());
800 UniValue movecmd(const JSONRPCRequest& request)
802 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
803 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
804 return NullUniValue;
807 if (request.fHelp || request.params.size() < 3 || request.params.size() > 5)
808 throw std::runtime_error(
809 "move \"fromaccount\" \"toaccount\" amount ( minconf \"comment\" )\n"
810 "\nDEPRECATED. Move a specified amount from one account in your wallet to another.\n"
811 "\nArguments:\n"
812 "1. \"fromaccount\" (string, required) The name of the account to move funds from. May be the default account using \"\".\n"
813 "2. \"toaccount\" (string, required) The name of the account to move funds to. May be the default account using \"\".\n"
814 "3. amount (numeric) Quantity of " + CURRENCY_UNIT + " to move between accounts.\n"
815 "4. (dummy) (numeric, optional) Ignored. Remains for backward compatibility.\n"
816 "5. \"comment\" (string, optional) An optional comment, stored in the wallet only.\n"
817 "\nResult:\n"
818 "true|false (boolean) true if successful.\n"
819 "\nExamples:\n"
820 "\nMove 0.01 " + CURRENCY_UNIT + " from the default account to the account named tabby\n"
821 + HelpExampleCli("move", "\"\" \"tabby\" 0.01") +
822 "\nMove 0.01 " + CURRENCY_UNIT + " timotei to akiko with a comment and funds have 6 confirmations\n"
823 + HelpExampleCli("move", "\"timotei\" \"akiko\" 0.01 6 \"happy birthday!\"") +
824 "\nAs a json rpc call\n"
825 + HelpExampleRpc("move", "\"timotei\", \"akiko\", 0.01, 6, \"happy birthday!\"")
828 LOCK2(cs_main, pwallet->cs_wallet);
830 std::string strFrom = AccountFromValue(request.params[0]);
831 std::string strTo = AccountFromValue(request.params[1]);
832 CAmount nAmount = AmountFromValue(request.params[2]);
833 if (nAmount <= 0)
834 throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
835 if (request.params.size() > 3)
836 // unused parameter, used to be nMinDepth, keep type-checking it though
837 (void)request.params[3].get_int();
838 std::string strComment;
839 if (request.params.size() > 4)
840 strComment = request.params[4].get_str();
842 if (!pwallet->AccountMove(strFrom, strTo, nAmount, strComment)) {
843 throw JSONRPCError(RPC_DATABASE_ERROR, "database error");
846 return true;
850 UniValue sendfrom(const JSONRPCRequest& request)
852 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
853 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
854 return NullUniValue;
857 if (request.fHelp || request.params.size() < 3 || request.params.size() > 6)
858 throw std::runtime_error(
859 "sendfrom \"fromaccount\" \"toaddress\" amount ( minconf \"comment\" \"comment_to\" )\n"
860 "\nDEPRECATED (use sendtoaddress). Sent an amount from an account to a bitcoin address."
861 + HelpRequiringPassphrase(pwallet) + "\n"
862 "\nArguments:\n"
863 "1. \"fromaccount\" (string, required) The name of the account to send funds from. May be the default account using \"\".\n"
864 " Specifying an account does not influence coin selection, but it does associate the newly created\n"
865 " transaction with the account, so the account's balance computation and transaction history can reflect\n"
866 " the spend.\n"
867 "2. \"toaddress\" (string, required) The bitcoin address to send funds to.\n"
868 "3. amount (numeric or string, required) The amount in " + CURRENCY_UNIT + " (transaction fee is added on top).\n"
869 "4. minconf (numeric, optional, default=1) Only use funds with at least this many confirmations.\n"
870 "5. \"comment\" (string, optional) A comment used to store what the transaction is for. \n"
871 " This is not part of the transaction, just kept in your wallet.\n"
872 "6. \"comment_to\" (string, optional) An optional comment to store the name of the person or organization \n"
873 " to which you're sending the transaction. This is not part of the transaction, \n"
874 " it is just kept in your wallet.\n"
875 "\nResult:\n"
876 "\"txid\" (string) The transaction id.\n"
877 "\nExamples:\n"
878 "\nSend 0.01 " + CURRENCY_UNIT + " from the default account to the address, must have at least 1 confirmation\n"
879 + HelpExampleCli("sendfrom", "\"\" \"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.01") +
880 "\nSend 0.01 from the tabby account to the given address, funds must have at least 6 confirmations\n"
881 + HelpExampleCli("sendfrom", "\"tabby\" \"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.01 6 \"donation\" \"seans outpost\"") +
882 "\nAs a json rpc call\n"
883 + HelpExampleRpc("sendfrom", "\"tabby\", \"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\", 0.01, 6, \"donation\", \"seans outpost\"")
886 LOCK2(cs_main, pwallet->cs_wallet);
888 std::string strAccount = AccountFromValue(request.params[0]);
889 CBitcoinAddress address(request.params[1].get_str());
890 if (!address.IsValid())
891 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
892 CAmount nAmount = AmountFromValue(request.params[2]);
893 if (nAmount <= 0)
894 throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
895 int nMinDepth = 1;
896 if (request.params.size() > 3)
897 nMinDepth = request.params[3].get_int();
899 CWalletTx wtx;
900 wtx.strFromAccount = strAccount;
901 if (request.params.size() > 4 && !request.params[4].isNull() && !request.params[4].get_str().empty())
902 wtx.mapValue["comment"] = request.params[4].get_str();
903 if (request.params.size() > 5 && !request.params[5].isNull() && !request.params[5].get_str().empty())
904 wtx.mapValue["to"] = request.params[5].get_str();
906 EnsureWalletIsUnlocked(pwallet);
908 // Check funds
909 CAmount nBalance = pwallet->GetLegacyBalance(ISMINE_SPENDABLE, nMinDepth, &strAccount);
910 if (nAmount > nBalance)
911 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
913 CCoinControl no_coin_control; // This is a deprecated API
914 SendMoney(pwallet, address.Get(), nAmount, false, wtx, no_coin_control);
916 return wtx.GetHash().GetHex();
920 UniValue sendmany(const JSONRPCRequest& request)
922 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
923 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
924 return NullUniValue;
927 if (request.fHelp || request.params.size() < 2 || request.params.size() > 8)
928 throw std::runtime_error(
929 "sendmany \"fromaccount\" {\"address\":amount,...} ( minconf \"comment\" [\"address\",...] replaceable conf_target \"estimate_mode\")\n"
930 "\nSend multiple times. Amounts are double-precision floating point numbers."
931 + HelpRequiringPassphrase(pwallet) + "\n"
932 "\nArguments:\n"
933 "1. \"fromaccount\" (string, required) DEPRECATED. The account to send the funds from. Should be \"\" for the default account\n"
934 "2. \"amounts\" (string, required) A json object with addresses and amounts\n"
935 " {\n"
936 " \"address\":amount (numeric or string) The bitcoin address is the key, the numeric amount (can be string) in " + CURRENCY_UNIT + " is the value\n"
937 " ,...\n"
938 " }\n"
939 "3. minconf (numeric, optional, default=1) Only use the balance confirmed at least this many times.\n"
940 "4. \"comment\" (string, optional) A comment\n"
941 "5. subtractfeefrom (array, optional) A json array with addresses.\n"
942 " The fee will be equally deducted from the amount of each selected address.\n"
943 " Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n"
944 " If no addresses are specified here, the sender pays the fee.\n"
945 " [\n"
946 " \"address\" (string) Subtract fee from this address\n"
947 " ,...\n"
948 " ]\n"
949 "6. replaceable (boolean, optional) Allow this transaction to be replaced by a transaction with higher fees via BIP 125\n"
950 "7. conf_target (numeric, optional) Confirmation target (in blocks)\n"
951 "8. \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
952 " \"UNSET\"\n"
953 " \"ECONOMICAL\"\n"
954 " \"CONSERVATIVE\"\n"
955 "\nResult:\n"
956 "\"txid\" (string) The transaction id for the send. Only 1 transaction is created regardless of \n"
957 " the number of addresses.\n"
958 "\nExamples:\n"
959 "\nSend two amounts to two different addresses:\n"
960 + HelpExampleCli("sendmany", "\"\" \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\"") +
961 "\nSend two amounts to two different addresses setting the confirmation and comment:\n"
962 + HelpExampleCli("sendmany", "\"\" \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\" 6 \"testing\"") +
963 "\nSend two amounts to two different addresses, subtract fee from amount:\n"
964 + HelpExampleCli("sendmany", "\"\" \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\" 1 \"\" \"[\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\",\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\"]\"") +
965 "\nAs a json rpc call\n"
966 + HelpExampleRpc("sendmany", "\"\", \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\", 6, \"testing\"")
969 LOCK2(cs_main, pwallet->cs_wallet);
971 if (pwallet->GetBroadcastTransactions() && !g_connman) {
972 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
975 std::string strAccount = AccountFromValue(request.params[0]);
976 UniValue sendTo = request.params[1].get_obj();
977 int nMinDepth = 1;
978 if (!request.params[2].isNull())
979 nMinDepth = request.params[2].get_int();
981 CWalletTx wtx;
982 wtx.strFromAccount = strAccount;
983 if (request.params.size() > 3 && !request.params[3].isNull() && !request.params[3].get_str().empty())
984 wtx.mapValue["comment"] = request.params[3].get_str();
986 UniValue subtractFeeFromAmount(UniValue::VARR);
987 if (request.params.size() > 4 && !request.params[4].isNull())
988 subtractFeeFromAmount = request.params[4].get_array();
990 CCoinControl coin_control;
991 if (request.params.size() > 5 && !request.params[5].isNull()) {
992 coin_control.signalRbf = request.params[5].get_bool();
995 if (request.params.size() > 6 && !request.params[6].isNull()) {
996 coin_control.m_confirm_target = ParseConfirmTarget(request.params[6]);
999 if (request.params.size() > 7 && !request.params[7].isNull()) {
1000 if (!FeeModeFromString(request.params[7].get_str(), coin_control.m_fee_mode)) {
1001 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid estimate_mode parameter");
1005 std::set<CBitcoinAddress> setAddress;
1006 std::vector<CRecipient> vecSend;
1008 CAmount totalAmount = 0;
1009 std::vector<std::string> keys = sendTo.getKeys();
1010 for (const std::string& name_ : keys)
1012 CBitcoinAddress address(name_);
1013 if (!address.IsValid())
1014 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ")+name_);
1016 if (setAddress.count(address))
1017 throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ")+name_);
1018 setAddress.insert(address);
1020 CScript scriptPubKey = GetScriptForDestination(address.Get());
1021 CAmount nAmount = AmountFromValue(sendTo[name_]);
1022 if (nAmount <= 0)
1023 throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
1024 totalAmount += nAmount;
1026 bool fSubtractFeeFromAmount = false;
1027 for (unsigned int idx = 0; idx < subtractFeeFromAmount.size(); idx++) {
1028 const UniValue& addr = subtractFeeFromAmount[idx];
1029 if (addr.get_str() == name_)
1030 fSubtractFeeFromAmount = true;
1033 CRecipient recipient = {scriptPubKey, nAmount, fSubtractFeeFromAmount};
1034 vecSend.push_back(recipient);
1037 EnsureWalletIsUnlocked(pwallet);
1039 // Check funds
1040 CAmount nBalance = pwallet->GetLegacyBalance(ISMINE_SPENDABLE, nMinDepth, &strAccount);
1041 if (totalAmount > nBalance)
1042 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
1044 // Send
1045 CReserveKey keyChange(pwallet);
1046 CAmount nFeeRequired = 0;
1047 int nChangePosRet = -1;
1048 std::string strFailReason;
1049 bool fCreated = pwallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired, nChangePosRet, strFailReason, coin_control);
1050 if (!fCreated)
1051 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason);
1052 CValidationState state;
1053 if (!pwallet->CommitTransaction(wtx, keyChange, g_connman.get(), state)) {
1054 strFailReason = strprintf("Transaction commit failed:: %s", state.GetRejectReason());
1055 throw JSONRPCError(RPC_WALLET_ERROR, strFailReason);
1058 return wtx.GetHash().GetHex();
1061 // Defined in rpc/misc.cpp
1062 extern CScript _createmultisig_redeemScript(CWallet * const pwallet, const UniValue& params);
1064 UniValue addmultisigaddress(const JSONRPCRequest& request)
1066 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1067 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1068 return NullUniValue;
1071 if (request.fHelp || request.params.size() < 2 || request.params.size() > 3)
1073 std::string msg = "addmultisigaddress nrequired [\"key\",...] ( \"account\" )\n"
1074 "\nAdd a nrequired-to-sign multisignature address to the wallet.\n"
1075 "Each key is a Bitcoin address or hex-encoded public key.\n"
1076 "If 'account' is specified (DEPRECATED), assign address to that account.\n"
1078 "\nArguments:\n"
1079 "1. nrequired (numeric, required) The number of required signatures out of the n keys or addresses.\n"
1080 "2. \"keys\" (string, required) A json array of bitcoin addresses or hex-encoded public keys\n"
1081 " [\n"
1082 " \"address\" (string) bitcoin address or hex-encoded public key\n"
1083 " ...,\n"
1084 " ]\n"
1085 "3. \"account\" (string, optional) DEPRECATED. An account to assign the addresses to.\n"
1087 "\nResult:\n"
1088 "\"address\" (string) A bitcoin address associated with the keys.\n"
1090 "\nExamples:\n"
1091 "\nAdd a multisig address from 2 addresses\n"
1092 + HelpExampleCli("addmultisigaddress", "2 \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"") +
1093 "\nAs json rpc call\n"
1094 + HelpExampleRpc("addmultisigaddress", "2, \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"")
1096 throw std::runtime_error(msg);
1099 LOCK2(cs_main, pwallet->cs_wallet);
1101 std::string strAccount;
1102 if (request.params.size() > 2)
1103 strAccount = AccountFromValue(request.params[2]);
1105 // Construct using pay-to-script-hash:
1106 CScript inner = _createmultisig_redeemScript(pwallet, request.params);
1107 CScriptID innerID(inner);
1108 pwallet->AddCScript(inner);
1110 pwallet->SetAddressBook(innerID, strAccount, "send");
1111 return CBitcoinAddress(innerID).ToString();
1114 class Witnessifier : public boost::static_visitor<bool>
1116 public:
1117 CWallet * const pwallet;
1118 CScriptID result;
1120 Witnessifier(CWallet *_pwallet) : pwallet(_pwallet) {}
1122 bool operator()(const CNoDestination &dest) const { return false; }
1124 bool operator()(const CKeyID &keyID) {
1125 if (pwallet) {
1126 CScript basescript = GetScriptForDestination(keyID);
1127 isminetype typ;
1128 typ = IsMine(*pwallet, basescript, SIGVERSION_WITNESS_V0);
1129 if (typ != ISMINE_SPENDABLE && typ != ISMINE_WATCH_SOLVABLE)
1130 return false;
1131 CScript witscript = GetScriptForWitness(basescript);
1132 pwallet->AddCScript(witscript);
1133 result = CScriptID(witscript);
1134 return true;
1136 return false;
1139 bool operator()(const CScriptID &scriptID) {
1140 CScript subscript;
1141 if (pwallet && pwallet->GetCScript(scriptID, subscript)) {
1142 int witnessversion;
1143 std::vector<unsigned char> witprog;
1144 if (subscript.IsWitnessProgram(witnessversion, witprog)) {
1145 result = scriptID;
1146 return true;
1148 isminetype typ;
1149 typ = IsMine(*pwallet, subscript, SIGVERSION_WITNESS_V0);
1150 if (typ != ISMINE_SPENDABLE && typ != ISMINE_WATCH_SOLVABLE)
1151 return false;
1152 CScript witscript = GetScriptForWitness(subscript);
1153 pwallet->AddCScript(witscript);
1154 result = CScriptID(witscript);
1155 return true;
1157 return false;
1161 UniValue addwitnessaddress(const JSONRPCRequest& request)
1163 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1164 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1165 return NullUniValue;
1168 if (request.fHelp || request.params.size() < 1 || request.params.size() > 1)
1170 std::string msg = "addwitnessaddress \"address\"\n"
1171 "\nAdd a witness address for a script (with pubkey or redeemscript known).\n"
1172 "It returns the witness script.\n"
1174 "\nArguments:\n"
1175 "1. \"address\" (string, required) An address known to the wallet\n"
1177 "\nResult:\n"
1178 "\"witnessaddress\", (string) The value of the new address (P2SH of witness script).\n"
1179 "}\n"
1181 throw std::runtime_error(msg);
1185 LOCK(cs_main);
1186 if (!IsWitnessEnabled(chainActive.Tip(), Params().GetConsensus()) && !GetBoolArg("-walletprematurewitness", false)) {
1187 throw JSONRPCError(RPC_WALLET_ERROR, "Segregated witness not enabled on network");
1191 CBitcoinAddress address(request.params[0].get_str());
1192 if (!address.IsValid())
1193 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
1195 Witnessifier w(pwallet);
1196 CTxDestination dest = address.Get();
1197 bool ret = boost::apply_visitor(w, dest);
1198 if (!ret) {
1199 throw JSONRPCError(RPC_WALLET_ERROR, "Public key or redeemscript not known to wallet, or the key is uncompressed");
1202 pwallet->SetAddressBook(w.result, "", "receive");
1204 return CBitcoinAddress(w.result).ToString();
1207 struct tallyitem
1209 CAmount nAmount;
1210 int nConf;
1211 std::vector<uint256> txids;
1212 bool fIsWatchonly;
1213 tallyitem()
1215 nAmount = 0;
1216 nConf = std::numeric_limits<int>::max();
1217 fIsWatchonly = false;
1221 UniValue ListReceived(CWallet * const pwallet, const UniValue& params, bool fByAccounts)
1223 // Minimum confirmations
1224 int nMinDepth = 1;
1225 if (!params[0].isNull())
1226 nMinDepth = params[0].get_int();
1228 // Whether to include empty accounts
1229 bool fIncludeEmpty = false;
1230 if (!params[1].isNull())
1231 fIncludeEmpty = params[1].get_bool();
1233 isminefilter filter = ISMINE_SPENDABLE;
1234 if(!params[2].isNull())
1235 if(params[2].get_bool())
1236 filter = filter | ISMINE_WATCH_ONLY;
1238 // Tally
1239 std::map<CBitcoinAddress, tallyitem> mapTally;
1240 for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
1241 const CWalletTx& wtx = pairWtx.second;
1243 if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
1244 continue;
1246 int nDepth = wtx.GetDepthInMainChain();
1247 if (nDepth < nMinDepth)
1248 continue;
1250 for (const CTxOut& txout : wtx.tx->vout)
1252 CTxDestination address;
1253 if (!ExtractDestination(txout.scriptPubKey, address))
1254 continue;
1256 isminefilter mine = IsMine(*pwallet, address);
1257 if(!(mine & filter))
1258 continue;
1260 tallyitem& item = mapTally[address];
1261 item.nAmount += txout.nValue;
1262 item.nConf = std::min(item.nConf, nDepth);
1263 item.txids.push_back(wtx.GetHash());
1264 if (mine & ISMINE_WATCH_ONLY)
1265 item.fIsWatchonly = true;
1269 // Reply
1270 UniValue ret(UniValue::VARR);
1271 std::map<std::string, tallyitem> mapAccountTally;
1272 for (const std::pair<CBitcoinAddress, CAddressBookData>& item : pwallet->mapAddressBook) {
1273 const CBitcoinAddress& address = item.first;
1274 const std::string& strAccount = item.second.name;
1275 std::map<CBitcoinAddress, tallyitem>::iterator it = mapTally.find(address);
1276 if (it == mapTally.end() && !fIncludeEmpty)
1277 continue;
1279 CAmount nAmount = 0;
1280 int nConf = std::numeric_limits<int>::max();
1281 bool fIsWatchonly = false;
1282 if (it != mapTally.end())
1284 nAmount = (*it).second.nAmount;
1285 nConf = (*it).second.nConf;
1286 fIsWatchonly = (*it).second.fIsWatchonly;
1289 if (fByAccounts)
1291 tallyitem& _item = mapAccountTally[strAccount];
1292 _item.nAmount += nAmount;
1293 _item.nConf = std::min(_item.nConf, nConf);
1294 _item.fIsWatchonly = fIsWatchonly;
1296 else
1298 UniValue obj(UniValue::VOBJ);
1299 if(fIsWatchonly)
1300 obj.push_back(Pair("involvesWatchonly", true));
1301 obj.push_back(Pair("address", address.ToString()));
1302 obj.push_back(Pair("account", strAccount));
1303 obj.push_back(Pair("amount", ValueFromAmount(nAmount)));
1304 obj.push_back(Pair("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf)));
1305 if (!fByAccounts)
1306 obj.push_back(Pair("label", strAccount));
1307 UniValue transactions(UniValue::VARR);
1308 if (it != mapTally.end())
1310 for (const uint256& _item : (*it).second.txids)
1312 transactions.push_back(_item.GetHex());
1315 obj.push_back(Pair("txids", transactions));
1316 ret.push_back(obj);
1320 if (fByAccounts)
1322 for (std::map<std::string, tallyitem>::iterator it = mapAccountTally.begin(); it != mapAccountTally.end(); ++it)
1324 CAmount nAmount = (*it).second.nAmount;
1325 int nConf = (*it).second.nConf;
1326 UniValue obj(UniValue::VOBJ);
1327 if((*it).second.fIsWatchonly)
1328 obj.push_back(Pair("involvesWatchonly", true));
1329 obj.push_back(Pair("account", (*it).first));
1330 obj.push_back(Pair("amount", ValueFromAmount(nAmount)));
1331 obj.push_back(Pair("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf)));
1332 ret.push_back(obj);
1336 return ret;
1339 UniValue listreceivedbyaddress(const JSONRPCRequest& request)
1341 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1342 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1343 return NullUniValue;
1346 if (request.fHelp || request.params.size() > 3)
1347 throw std::runtime_error(
1348 "listreceivedbyaddress ( minconf include_empty include_watchonly)\n"
1349 "\nList balances by receiving address.\n"
1350 "\nArguments:\n"
1351 "1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included.\n"
1352 "2. include_empty (bool, optional, default=false) Whether to include addresses that haven't received any payments.\n"
1353 "3. include_watchonly (bool, optional, default=false) Whether to include watch-only addresses (see 'importaddress').\n"
1355 "\nResult:\n"
1356 "[\n"
1357 " {\n"
1358 " \"involvesWatchonly\" : true, (bool) Only returned if imported addresses were involved in transaction\n"
1359 " \"address\" : \"receivingaddress\", (string) The receiving address\n"
1360 " \"account\" : \"accountname\", (string) DEPRECATED. The account of the receiving address. The default account is \"\".\n"
1361 " \"amount\" : x.xxx, (numeric) The total amount in " + CURRENCY_UNIT + " received by the address\n"
1362 " \"confirmations\" : n, (numeric) The number of confirmations of the most recent transaction included\n"
1363 " \"label\" : \"label\", (string) A comment for the address/transaction, if any\n"
1364 " \"txids\": [\n"
1365 " n, (numeric) The ids of transactions received with the address \n"
1366 " ...\n"
1367 " ]\n"
1368 " }\n"
1369 " ,...\n"
1370 "]\n"
1372 "\nExamples:\n"
1373 + HelpExampleCli("listreceivedbyaddress", "")
1374 + HelpExampleCli("listreceivedbyaddress", "6 true")
1375 + HelpExampleRpc("listreceivedbyaddress", "6, true, true")
1378 LOCK2(cs_main, pwallet->cs_wallet);
1380 return ListReceived(pwallet, request.params, false);
1383 UniValue listreceivedbyaccount(const JSONRPCRequest& request)
1385 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1386 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1387 return NullUniValue;
1390 if (request.fHelp || request.params.size() > 3)
1391 throw std::runtime_error(
1392 "listreceivedbyaccount ( minconf include_empty include_watchonly)\n"
1393 "\nDEPRECATED. List balances by account.\n"
1394 "\nArguments:\n"
1395 "1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included.\n"
1396 "2. include_empty (bool, optional, default=false) Whether to include accounts that haven't received any payments.\n"
1397 "3. include_watchonly (bool, optional, default=false) Whether to include watch-only addresses (see 'importaddress').\n"
1399 "\nResult:\n"
1400 "[\n"
1401 " {\n"
1402 " \"involvesWatchonly\" : true, (bool) Only returned if imported addresses were involved in transaction\n"
1403 " \"account\" : \"accountname\", (string) The account name of the receiving account\n"
1404 " \"amount\" : x.xxx, (numeric) The total amount received by addresses with this account\n"
1405 " \"confirmations\" : n, (numeric) The number of confirmations of the most recent transaction included\n"
1406 " \"label\" : \"label\" (string) A comment for the address/transaction, if any\n"
1407 " }\n"
1408 " ,...\n"
1409 "]\n"
1411 "\nExamples:\n"
1412 + HelpExampleCli("listreceivedbyaccount", "")
1413 + HelpExampleCli("listreceivedbyaccount", "6 true")
1414 + HelpExampleRpc("listreceivedbyaccount", "6, true, true")
1417 LOCK2(cs_main, pwallet->cs_wallet);
1419 return ListReceived(pwallet, request.params, true);
1422 static void MaybePushAddress(UniValue & entry, const CTxDestination &dest)
1424 CBitcoinAddress addr;
1425 if (addr.Set(dest))
1426 entry.push_back(Pair("address", addr.ToString()));
1430 * List transactions based on the given criteria.
1432 * @param pwallet The wallet.
1433 * @param wtx The wallet transaction.
1434 * @param strAccount The account, if any, or "*" for all.
1435 * @param nMinDepth The minimum confirmation depth.
1436 * @param fLong Whether to include the JSON version of the transaction.
1437 * @param ret The UniValue into which the result is stored.
1438 * @param filter The "is mine" filter bool.
1440 void ListTransactions(CWallet* const pwallet, const CWalletTx& wtx, const std::string& strAccount, int nMinDepth, bool fLong, UniValue& ret, const isminefilter& filter)
1442 CAmount nFee;
1443 std::string strSentAccount;
1444 std::list<COutputEntry> listReceived;
1445 std::list<COutputEntry> listSent;
1447 wtx.GetAmounts(listReceived, listSent, nFee, strSentAccount, filter);
1449 bool fAllAccounts = (strAccount == std::string("*"));
1450 bool involvesWatchonly = wtx.IsFromMe(ISMINE_WATCH_ONLY);
1452 // Sent
1453 if ((!listSent.empty() || nFee != 0) && (fAllAccounts || strAccount == strSentAccount))
1455 for (const COutputEntry& s : listSent)
1457 UniValue entry(UniValue::VOBJ);
1458 if (involvesWatchonly || (::IsMine(*pwallet, s.destination) & ISMINE_WATCH_ONLY)) {
1459 entry.push_back(Pair("involvesWatchonly", true));
1461 entry.push_back(Pair("account", strSentAccount));
1462 MaybePushAddress(entry, s.destination);
1463 entry.push_back(Pair("category", "send"));
1464 entry.push_back(Pair("amount", ValueFromAmount(-s.amount)));
1465 if (pwallet->mapAddressBook.count(s.destination)) {
1466 entry.push_back(Pair("label", pwallet->mapAddressBook[s.destination].name));
1468 entry.push_back(Pair("vout", s.vout));
1469 entry.push_back(Pair("fee", ValueFromAmount(-nFee)));
1470 if (fLong)
1471 WalletTxToJSON(wtx, entry);
1472 entry.push_back(Pair("abandoned", wtx.isAbandoned()));
1473 ret.push_back(entry);
1477 // Received
1478 if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth)
1480 for (const COutputEntry& r : listReceived)
1482 std::string account;
1483 if (pwallet->mapAddressBook.count(r.destination)) {
1484 account = pwallet->mapAddressBook[r.destination].name;
1486 if (fAllAccounts || (account == strAccount))
1488 UniValue entry(UniValue::VOBJ);
1489 if (involvesWatchonly || (::IsMine(*pwallet, r.destination) & ISMINE_WATCH_ONLY)) {
1490 entry.push_back(Pair("involvesWatchonly", true));
1492 entry.push_back(Pair("account", account));
1493 MaybePushAddress(entry, r.destination);
1494 if (wtx.IsCoinBase())
1496 if (wtx.GetDepthInMainChain() < 1)
1497 entry.push_back(Pair("category", "orphan"));
1498 else if (wtx.GetBlocksToMaturity() > 0)
1499 entry.push_back(Pair("category", "immature"));
1500 else
1501 entry.push_back(Pair("category", "generate"));
1503 else
1505 entry.push_back(Pair("category", "receive"));
1507 entry.push_back(Pair("amount", ValueFromAmount(r.amount)));
1508 if (pwallet->mapAddressBook.count(r.destination)) {
1509 entry.push_back(Pair("label", account));
1511 entry.push_back(Pair("vout", r.vout));
1512 if (fLong)
1513 WalletTxToJSON(wtx, entry);
1514 ret.push_back(entry);
1520 void AcentryToJSON(const CAccountingEntry& acentry, const std::string& strAccount, UniValue& ret)
1522 bool fAllAccounts = (strAccount == std::string("*"));
1524 if (fAllAccounts || acentry.strAccount == strAccount)
1526 UniValue entry(UniValue::VOBJ);
1527 entry.push_back(Pair("account", acentry.strAccount));
1528 entry.push_back(Pair("category", "move"));
1529 entry.push_back(Pair("time", acentry.nTime));
1530 entry.push_back(Pair("amount", ValueFromAmount(acentry.nCreditDebit)));
1531 entry.push_back(Pair("otheraccount", acentry.strOtherAccount));
1532 entry.push_back(Pair("comment", acentry.strComment));
1533 ret.push_back(entry);
1537 UniValue listtransactions(const JSONRPCRequest& request)
1539 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1540 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1541 return NullUniValue;
1544 if (request.fHelp || request.params.size() > 4)
1545 throw std::runtime_error(
1546 "listtransactions ( \"account\" count skip include_watchonly)\n"
1547 "\nReturns up to 'count' most recent transactions skipping the first 'from' transactions for account 'account'.\n"
1548 "\nArguments:\n"
1549 "1. \"account\" (string, optional) DEPRECATED. The account name. Should be \"*\".\n"
1550 "2. count (numeric, optional, default=10) The number of transactions to return\n"
1551 "3. skip (numeric, optional, default=0) The number of transactions to skip\n"
1552 "4. include_watchonly (bool, optional, default=false) Include transactions to watch-only addresses (see 'importaddress')\n"
1553 "\nResult:\n"
1554 "[\n"
1555 " {\n"
1556 " \"account\":\"accountname\", (string) DEPRECATED. The account name associated with the transaction. \n"
1557 " It will be \"\" for the default account.\n"
1558 " \"address\":\"address\", (string) The bitcoin address of the transaction. Not present for \n"
1559 " move transactions (category = move).\n"
1560 " \"category\":\"send|receive|move\", (string) The transaction category. 'move' is a local (off blockchain)\n"
1561 " transaction between accounts, and not associated with an address,\n"
1562 " transaction id or block. 'send' and 'receive' transactions are \n"
1563 " associated with an address, transaction id and block details\n"
1564 " \"amount\": x.xxx, (numeric) The amount in " + CURRENCY_UNIT + ". This is negative for the 'send' category, and for the\n"
1565 " 'move' category for moves outbound. It is positive for the 'receive' category,\n"
1566 " and for the 'move' category for inbound funds.\n"
1567 " \"label\": \"label\", (string) A comment for the address/transaction, if any\n"
1568 " \"vout\": n, (numeric) the vout value\n"
1569 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the \n"
1570 " 'send' category of transactions.\n"
1571 " \"confirmations\": n, (numeric) The number of confirmations for the transaction. Available for 'send' and \n"
1572 " 'receive' category of transactions. Negative confirmations indicate the\n"
1573 " transaction conflicts with the block chain\n"
1574 " \"trusted\": xxx, (bool) Whether we consider the outputs of this unconfirmed transaction safe to spend.\n"
1575 " \"blockhash\": \"hashvalue\", (string) The block hash containing the transaction. Available for 'send' and 'receive'\n"
1576 " category of transactions.\n"
1577 " \"blockindex\": n, (numeric) The index of the transaction in the block that includes it. Available for 'send' and 'receive'\n"
1578 " category of transactions.\n"
1579 " \"blocktime\": xxx, (numeric) The block time in seconds since epoch (1 Jan 1970 GMT).\n"
1580 " \"txid\": \"transactionid\", (string) The transaction id. Available for 'send' and 'receive' category of transactions.\n"
1581 " \"time\": xxx, (numeric) The transaction time in seconds since epoch (midnight Jan 1 1970 GMT).\n"
1582 " \"timereceived\": xxx, (numeric) The time received in seconds since epoch (midnight Jan 1 1970 GMT). Available \n"
1583 " for 'send' and 'receive' category of transactions.\n"
1584 " \"comment\": \"...\", (string) If a comment is associated with the transaction.\n"
1585 " \"otheraccount\": \"accountname\", (string) DEPRECATED. For the 'move' category of transactions, the account the funds came \n"
1586 " from (for receiving funds, positive amounts), or went to (for sending funds,\n"
1587 " negative amounts).\n"
1588 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1589 " may be unknown for unconfirmed transactions not in the mempool\n"
1590 " \"abandoned\": xxx (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
1591 " 'send' category of transactions.\n"
1592 " }\n"
1593 "]\n"
1595 "\nExamples:\n"
1596 "\nList the most recent 10 transactions in the systems\n"
1597 + HelpExampleCli("listtransactions", "") +
1598 "\nList transactions 100 to 120\n"
1599 + HelpExampleCli("listtransactions", "\"*\" 20 100") +
1600 "\nAs a json rpc call\n"
1601 + HelpExampleRpc("listtransactions", "\"*\", 20, 100")
1604 LOCK2(cs_main, pwallet->cs_wallet);
1606 std::string strAccount = "*";
1607 if (!request.params[0].isNull())
1608 strAccount = request.params[0].get_str();
1609 int nCount = 10;
1610 if (!request.params[1].isNull())
1611 nCount = request.params[1].get_int();
1612 int nFrom = 0;
1613 if (!request.params[2].isNull())
1614 nFrom = request.params[2].get_int();
1615 isminefilter filter = ISMINE_SPENDABLE;
1616 if(!request.params[3].isNull())
1617 if(request.params[3].get_bool())
1618 filter = filter | ISMINE_WATCH_ONLY;
1620 if (nCount < 0)
1621 throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative count");
1622 if (nFrom < 0)
1623 throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative from");
1625 UniValue ret(UniValue::VARR);
1627 const CWallet::TxItems & txOrdered = pwallet->wtxOrdered;
1629 // iterate backwards until we have nCount items to return:
1630 for (CWallet::TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
1632 CWalletTx *const pwtx = (*it).second.first;
1633 if (pwtx != 0)
1634 ListTransactions(pwallet, *pwtx, strAccount, 0, true, ret, filter);
1635 CAccountingEntry *const pacentry = (*it).second.second;
1636 if (pacentry != 0)
1637 AcentryToJSON(*pacentry, strAccount, ret);
1639 if ((int)ret.size() >= (nCount+nFrom)) break;
1641 // ret is newest to oldest
1643 if (nFrom > (int)ret.size())
1644 nFrom = ret.size();
1645 if ((nFrom + nCount) > (int)ret.size())
1646 nCount = ret.size() - nFrom;
1648 std::vector<UniValue> arrTmp = ret.getValues();
1650 std::vector<UniValue>::iterator first = arrTmp.begin();
1651 std::advance(first, nFrom);
1652 std::vector<UniValue>::iterator last = arrTmp.begin();
1653 std::advance(last, nFrom+nCount);
1655 if (last != arrTmp.end()) arrTmp.erase(last, arrTmp.end());
1656 if (first != arrTmp.begin()) arrTmp.erase(arrTmp.begin(), first);
1658 std::reverse(arrTmp.begin(), arrTmp.end()); // Return oldest to newest
1660 ret.clear();
1661 ret.setArray();
1662 ret.push_backV(arrTmp);
1664 return ret;
1667 UniValue listaccounts(const JSONRPCRequest& request)
1669 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1670 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1671 return NullUniValue;
1674 if (request.fHelp || request.params.size() > 2)
1675 throw std::runtime_error(
1676 "listaccounts ( minconf include_watchonly)\n"
1677 "\nDEPRECATED. Returns Object that has account names as keys, account balances as values.\n"
1678 "\nArguments:\n"
1679 "1. minconf (numeric, optional, default=1) Only include transactions with at least this many confirmations\n"
1680 "2. include_watchonly (bool, optional, default=false) Include balances in watch-only addresses (see 'importaddress')\n"
1681 "\nResult:\n"
1682 "{ (json object where keys are account names, and values are numeric balances\n"
1683 " \"account\": x.xxx, (numeric) The property name is the account name, and the value is the total balance for the account.\n"
1684 " ...\n"
1685 "}\n"
1686 "\nExamples:\n"
1687 "\nList account balances where there at least 1 confirmation\n"
1688 + HelpExampleCli("listaccounts", "") +
1689 "\nList account balances including zero confirmation transactions\n"
1690 + HelpExampleCli("listaccounts", "0") +
1691 "\nList account balances for 6 or more confirmations\n"
1692 + HelpExampleCli("listaccounts", "6") +
1693 "\nAs json rpc call\n"
1694 + HelpExampleRpc("listaccounts", "6")
1697 LOCK2(cs_main, pwallet->cs_wallet);
1699 int nMinDepth = 1;
1700 if (request.params.size() > 0)
1701 nMinDepth = request.params[0].get_int();
1702 isminefilter includeWatchonly = ISMINE_SPENDABLE;
1703 if(request.params.size() > 1)
1704 if(request.params[1].get_bool())
1705 includeWatchonly = includeWatchonly | ISMINE_WATCH_ONLY;
1707 std::map<std::string, CAmount> mapAccountBalances;
1708 for (const std::pair<CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) {
1709 if (IsMine(*pwallet, entry.first) & includeWatchonly) { // This address belongs to me
1710 mapAccountBalances[entry.second.name] = 0;
1714 for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
1715 const CWalletTx& wtx = pairWtx.second;
1716 CAmount nFee;
1717 std::string strSentAccount;
1718 std::list<COutputEntry> listReceived;
1719 std::list<COutputEntry> listSent;
1720 int nDepth = wtx.GetDepthInMainChain();
1721 if (wtx.GetBlocksToMaturity() > 0 || nDepth < 0)
1722 continue;
1723 wtx.GetAmounts(listReceived, listSent, nFee, strSentAccount, includeWatchonly);
1724 mapAccountBalances[strSentAccount] -= nFee;
1725 for (const COutputEntry& s : listSent)
1726 mapAccountBalances[strSentAccount] -= s.amount;
1727 if (nDepth >= nMinDepth)
1729 for (const COutputEntry& r : listReceived)
1730 if (pwallet->mapAddressBook.count(r.destination)) {
1731 mapAccountBalances[pwallet->mapAddressBook[r.destination].name] += r.amount;
1733 else
1734 mapAccountBalances[""] += r.amount;
1738 const std::list<CAccountingEntry>& acentries = pwallet->laccentries;
1739 for (const CAccountingEntry& entry : acentries)
1740 mapAccountBalances[entry.strAccount] += entry.nCreditDebit;
1742 UniValue ret(UniValue::VOBJ);
1743 for (const std::pair<std::string, CAmount>& accountBalance : mapAccountBalances) {
1744 ret.push_back(Pair(accountBalance.first, ValueFromAmount(accountBalance.second)));
1746 return ret;
1749 UniValue listsinceblock(const JSONRPCRequest& request)
1751 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1752 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1753 return NullUniValue;
1756 if (request.fHelp || request.params.size() > 4)
1757 throw std::runtime_error(
1758 "listsinceblock ( \"blockhash\" target_confirmations include_watchonly include_removed )\n"
1759 "\nGet all transactions in blocks since block [blockhash], or all transactions if omitted.\n"
1760 "If \"blockhash\" is no longer a part of the main chain, transactions from the fork point onward are included.\n"
1761 "Additionally, if include_removed is set, transactions affecting the wallet which were removed are returned in the \"removed\" array.\n"
1762 "\nArguments:\n"
1763 "1. \"blockhash\" (string, optional) The block hash to list transactions since\n"
1764 "2. target_confirmations: (numeric, optional, default=1) The confirmations required, must be 1 or more\n"
1765 "3. include_watchonly: (bool, optional, default=false) Include transactions to watch-only addresses (see 'importaddress')\n"
1766 "4. include_removed: (bool, optional, default=true) Show transactions that were removed due to a reorg in the \"removed\" array\n"
1767 " (not guaranteed to work on pruned nodes)\n"
1768 "\nResult:\n"
1769 "{\n"
1770 " \"transactions\": [\n"
1771 " \"account\":\"accountname\", (string) DEPRECATED. The account name associated with the transaction. Will be \"\" for the default account.\n"
1772 " \"address\":\"address\", (string) The bitcoin address of the transaction. Not present for move transactions (category = move).\n"
1773 " \"category\":\"send|receive\", (string) The transaction category. 'send' has negative amounts, 'receive' has positive amounts.\n"
1774 " \"amount\": x.xxx, (numeric) The amount in " + CURRENCY_UNIT + ". This is negative for the 'send' category, and for the 'move' category for moves \n"
1775 " outbound. It is positive for the 'receive' category, and for the 'move' category for inbound funds.\n"
1776 " \"vout\" : n, (numeric) the vout value\n"
1777 " \"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"
1778 " \"confirmations\": n, (numeric) The number of confirmations for the transaction. Available for 'send' and 'receive' category of transactions.\n"
1779 " When it's < 0, it means the transaction conflicted that many blocks ago.\n"
1780 " \"blockhash\": \"hashvalue\", (string) The block hash containing the transaction. Available for 'send' and 'receive' category of transactions.\n"
1781 " \"blockindex\": n, (numeric) The index of the transaction in the block that includes it. Available for 'send' and 'receive' category of transactions.\n"
1782 " \"blocktime\": xxx, (numeric) The block time in seconds since epoch (1 Jan 1970 GMT).\n"
1783 " \"txid\": \"transactionid\", (string) The transaction id. Available for 'send' and 'receive' category of transactions.\n"
1784 " \"time\": xxx, (numeric) The transaction time in seconds since epoch (Jan 1 1970 GMT).\n"
1785 " \"timereceived\": xxx, (numeric) The time received in seconds since epoch (Jan 1 1970 GMT). Available for 'send' and 'receive' category of transactions.\n"
1786 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1787 " may be unknown for unconfirmed transactions not in the mempool\n"
1788 " \"abandoned\": xxx, (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the 'send' category of transactions.\n"
1789 " \"comment\": \"...\", (string) If a comment is associated with the transaction.\n"
1790 " \"label\" : \"label\" (string) A comment for the address/transaction, if any\n"
1791 " \"to\": \"...\", (string) If a comment to is associated with the transaction.\n"
1792 " ],\n"
1793 " \"removed\": [\n"
1794 " <structure is the same as \"transactions\" above, only present if include_removed=true>\n"
1795 " 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"
1796 " ],\n"
1797 " \"lastblock\": \"lastblockhash\" (string) The hash of the last block\n"
1798 "}\n"
1799 "\nExamples:\n"
1800 + HelpExampleCli("listsinceblock", "")
1801 + HelpExampleCli("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\" 6")
1802 + HelpExampleRpc("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\", 6")
1805 LOCK2(cs_main, pwallet->cs_wallet);
1807 const CBlockIndex* pindex = NULL; // Block index of the specified block or the common ancestor, if the block provided was in a deactivated chain.
1808 const CBlockIndex* paltindex = NULL; // Block index of the specified block, even if it's in a deactivated chain.
1809 int target_confirms = 1;
1810 isminefilter filter = ISMINE_SPENDABLE;
1812 if (!request.params[0].isNull()) {
1813 uint256 blockId;
1815 blockId.SetHex(request.params[0].get_str());
1816 BlockMap::iterator it = mapBlockIndex.find(blockId);
1817 if (it != mapBlockIndex.end()) {
1818 paltindex = pindex = it->second;
1819 if (chainActive[pindex->nHeight] != pindex) {
1820 // the block being asked for is a part of a deactivated chain;
1821 // we don't want to depend on its perceived height in the block
1822 // chain, we want to instead use the last common ancestor
1823 pindex = chainActive.FindFork(pindex);
1828 if (!request.params[1].isNull()) {
1829 target_confirms = request.params[1].get_int();
1831 if (target_confirms < 1) {
1832 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter");
1836 if (!request.params[2].isNull() && request.params[2].get_bool()) {
1837 filter = filter | ISMINE_WATCH_ONLY;
1840 bool include_removed = (request.params[3].isNull() || request.params[3].get_bool());
1842 int depth = pindex ? (1 + chainActive.Height() - pindex->nHeight) : -1;
1844 UniValue transactions(UniValue::VARR);
1846 for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
1847 CWalletTx tx = pairWtx.second;
1849 if (depth == -1 || tx.GetDepthInMainChain() < depth) {
1850 ListTransactions(pwallet, tx, "*", 0, true, transactions, filter);
1854 // when a reorg'd block is requested, we also list any relevant transactions
1855 // in the blocks of the chain that was detached
1856 UniValue removed(UniValue::VARR);
1857 while (include_removed && paltindex && paltindex != pindex) {
1858 CBlock block;
1859 if (!ReadBlockFromDisk(block, paltindex, Params().GetConsensus())) {
1860 throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
1862 for (const CTransactionRef& tx : block.vtx) {
1863 if (pwallet->mapWallet.count(tx->GetHash()) > 0) {
1864 // We want all transactions regardless of confirmation count to appear here,
1865 // even negative confirmation ones, hence the big negative.
1866 ListTransactions(pwallet, pwallet->mapWallet[tx->GetHash()], "*", -100000000, true, removed, filter);
1869 paltindex = paltindex->pprev;
1872 CBlockIndex *pblockLast = chainActive[chainActive.Height() + 1 - target_confirms];
1873 uint256 lastblock = pblockLast ? pblockLast->GetBlockHash() : uint256();
1875 UniValue ret(UniValue::VOBJ);
1876 ret.push_back(Pair("transactions", transactions));
1877 if (include_removed) ret.push_back(Pair("removed", removed));
1878 ret.push_back(Pair("lastblock", lastblock.GetHex()));
1880 return ret;
1883 UniValue gettransaction(const JSONRPCRequest& request)
1885 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1886 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1887 return NullUniValue;
1890 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
1891 throw std::runtime_error(
1892 "gettransaction \"txid\" ( include_watchonly )\n"
1893 "\nGet detailed information about in-wallet transaction <txid>\n"
1894 "\nArguments:\n"
1895 "1. \"txid\" (string, required) The transaction id\n"
1896 "2. \"include_watchonly\" (bool, optional, default=false) Whether to include watch-only addresses in balance calculation and details[]\n"
1897 "\nResult:\n"
1898 "{\n"
1899 " \"amount\" : x.xxx, (numeric) The transaction amount in " + CURRENCY_UNIT + "\n"
1900 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the \n"
1901 " 'send' category of transactions.\n"
1902 " \"confirmations\" : n, (numeric) The number of confirmations\n"
1903 " \"blockhash\" : \"hash\", (string) The block hash\n"
1904 " \"blockindex\" : xx, (numeric) The index of the transaction in the block that includes it\n"
1905 " \"blocktime\" : ttt, (numeric) The time in seconds since epoch (1 Jan 1970 GMT)\n"
1906 " \"txid\" : \"transactionid\", (string) The transaction id.\n"
1907 " \"time\" : ttt, (numeric) The transaction time in seconds since epoch (1 Jan 1970 GMT)\n"
1908 " \"timereceived\" : ttt, (numeric) The time received in seconds since epoch (1 Jan 1970 GMT)\n"
1909 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1910 " may be unknown for unconfirmed transactions not in the mempool\n"
1911 " \"details\" : [\n"
1912 " {\n"
1913 " \"account\" : \"accountname\", (string) DEPRECATED. The account name involved in the transaction, can be \"\" for the default account.\n"
1914 " \"address\" : \"address\", (string) The bitcoin address involved in the transaction\n"
1915 " \"category\" : \"send|receive\", (string) The category, either 'send' or 'receive'\n"
1916 " \"amount\" : x.xxx, (numeric) The amount in " + CURRENCY_UNIT + "\n"
1917 " \"label\" : \"label\", (string) A comment for the address/transaction, if any\n"
1918 " \"vout\" : n, (numeric) the vout value\n"
1919 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the \n"
1920 " 'send' category of transactions.\n"
1921 " \"abandoned\": xxx (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
1922 " 'send' category of transactions.\n"
1923 " }\n"
1924 " ,...\n"
1925 " ],\n"
1926 " \"hex\" : \"data\" (string) Raw data for transaction\n"
1927 "}\n"
1929 "\nExamples:\n"
1930 + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
1931 + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\" true")
1932 + HelpExampleRpc("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
1935 LOCK2(cs_main, pwallet->cs_wallet);
1937 uint256 hash;
1938 hash.SetHex(request.params[0].get_str());
1940 isminefilter filter = ISMINE_SPENDABLE;
1941 if(!request.params[1].isNull())
1942 if(request.params[1].get_bool())
1943 filter = filter | ISMINE_WATCH_ONLY;
1945 UniValue entry(UniValue::VOBJ);
1946 if (!pwallet->mapWallet.count(hash)) {
1947 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid or non-wallet transaction id");
1949 const CWalletTx& wtx = pwallet->mapWallet[hash];
1951 CAmount nCredit = wtx.GetCredit(filter);
1952 CAmount nDebit = wtx.GetDebit(filter);
1953 CAmount nNet = nCredit - nDebit;
1954 CAmount nFee = (wtx.IsFromMe(filter) ? wtx.tx->GetValueOut() - nDebit : 0);
1956 entry.push_back(Pair("amount", ValueFromAmount(nNet - nFee)));
1957 if (wtx.IsFromMe(filter))
1958 entry.push_back(Pair("fee", ValueFromAmount(nFee)));
1960 WalletTxToJSON(wtx, entry);
1962 UniValue details(UniValue::VARR);
1963 ListTransactions(pwallet, wtx, "*", 0, false, details, filter);
1964 entry.push_back(Pair("details", details));
1966 std::string strHex = EncodeHexTx(static_cast<CTransaction>(wtx), RPCSerializationFlags());
1967 entry.push_back(Pair("hex", strHex));
1969 return entry;
1972 UniValue abandontransaction(const JSONRPCRequest& request)
1974 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
1975 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
1976 return NullUniValue;
1979 if (request.fHelp || request.params.size() != 1)
1980 throw std::runtime_error(
1981 "abandontransaction \"txid\"\n"
1982 "\nMark in-wallet transaction <txid> as abandoned\n"
1983 "This will mark this transaction and all its in-wallet descendants as abandoned which will allow\n"
1984 "for their inputs to be respent. It can be used to replace \"stuck\" or evicted transactions.\n"
1985 "It only works on transactions which are not included in a block and are not currently in the mempool.\n"
1986 "It has no effect on transactions which are already conflicted or abandoned.\n"
1987 "\nArguments:\n"
1988 "1. \"txid\" (string, required) The transaction id\n"
1989 "\nResult:\n"
1990 "\nExamples:\n"
1991 + HelpExampleCli("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
1992 + HelpExampleRpc("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
1995 LOCK2(cs_main, pwallet->cs_wallet);
1997 uint256 hash;
1998 hash.SetHex(request.params[0].get_str());
2000 if (!pwallet->mapWallet.count(hash)) {
2001 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid or non-wallet transaction id");
2003 if (!pwallet->AbandonTransaction(hash)) {
2004 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not eligible for abandonment");
2007 return NullUniValue;
2011 UniValue backupwallet(const JSONRPCRequest& request)
2013 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2014 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2015 return NullUniValue;
2018 if (request.fHelp || request.params.size() != 1)
2019 throw std::runtime_error(
2020 "backupwallet \"destination\"\n"
2021 "\nSafely copies current wallet file to destination, which can be a directory or a path with filename.\n"
2022 "\nArguments:\n"
2023 "1. \"destination\" (string) The destination directory or file\n"
2024 "\nExamples:\n"
2025 + HelpExampleCli("backupwallet", "\"backup.dat\"")
2026 + HelpExampleRpc("backupwallet", "\"backup.dat\"")
2029 LOCK2(cs_main, pwallet->cs_wallet);
2031 std::string strDest = request.params[0].get_str();
2032 if (!pwallet->BackupWallet(strDest)) {
2033 throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!");
2036 return NullUniValue;
2040 UniValue keypoolrefill(const JSONRPCRequest& request)
2042 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2043 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2044 return NullUniValue;
2047 if (request.fHelp || request.params.size() > 1)
2048 throw std::runtime_error(
2049 "keypoolrefill ( newsize )\n"
2050 "\nFills the keypool."
2051 + HelpRequiringPassphrase(pwallet) + "\n"
2052 "\nArguments\n"
2053 "1. newsize (numeric, optional, default=100) The new keypool size\n"
2054 "\nExamples:\n"
2055 + HelpExampleCli("keypoolrefill", "")
2056 + HelpExampleRpc("keypoolrefill", "")
2059 LOCK2(cs_main, pwallet->cs_wallet);
2061 // 0 is interpreted by TopUpKeyPool() as the default keypool size given by -keypool
2062 unsigned int kpSize = 0;
2063 if (!request.params[0].isNull()) {
2064 if (request.params[0].get_int() < 0)
2065 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected valid size.");
2066 kpSize = (unsigned int)request.params[0].get_int();
2069 EnsureWalletIsUnlocked(pwallet);
2070 pwallet->TopUpKeyPool(kpSize);
2072 if (pwallet->GetKeyPoolSize() < kpSize) {
2073 throw JSONRPCError(RPC_WALLET_ERROR, "Error refreshing keypool.");
2076 return NullUniValue;
2080 static void LockWallet(CWallet* pWallet)
2082 LOCK(pWallet->cs_wallet);
2083 pWallet->nRelockTime = 0;
2084 pWallet->Lock();
2087 UniValue walletpassphrase(const JSONRPCRequest& request)
2089 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2090 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2091 return NullUniValue;
2094 if (pwallet->IsCrypted() && (request.fHelp || request.params.size() != 2)) {
2095 throw std::runtime_error(
2096 "walletpassphrase \"passphrase\" timeout\n"
2097 "\nStores the wallet decryption key in memory for 'timeout' seconds.\n"
2098 "This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
2099 "\nArguments:\n"
2100 "1. \"passphrase\" (string, required) The wallet passphrase\n"
2101 "2. timeout (numeric, required) The time to keep the decryption key in seconds.\n"
2102 "\nNote:\n"
2103 "Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
2104 "time that overrides the old one.\n"
2105 "\nExamples:\n"
2106 "\nUnlock the wallet for 60 seconds\n"
2107 + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 60") +
2108 "\nLock the wallet again (before 60 seconds)\n"
2109 + HelpExampleCli("walletlock", "") +
2110 "\nAs json rpc call\n"
2111 + HelpExampleRpc("walletpassphrase", "\"my pass phrase\", 60")
2115 LOCK2(cs_main, pwallet->cs_wallet);
2117 if (request.fHelp)
2118 return true;
2119 if (!pwallet->IsCrypted()) {
2120 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrase was called.");
2123 // Note that the walletpassphrase is stored in request.params[0] which is not mlock()ed
2124 SecureString strWalletPass;
2125 strWalletPass.reserve(100);
2126 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
2127 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2128 strWalletPass = request.params[0].get_str().c_str();
2130 if (strWalletPass.length() > 0)
2132 if (!pwallet->Unlock(strWalletPass)) {
2133 throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
2136 else
2137 throw std::runtime_error(
2138 "walletpassphrase <passphrase> <timeout>\n"
2139 "Stores the wallet decryption key in memory for <timeout> seconds.");
2141 pwallet->TopUpKeyPool();
2143 int64_t nSleepTime = request.params[1].get_int64();
2144 pwallet->nRelockTime = GetTime() + nSleepTime;
2145 RPCRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), boost::bind(LockWallet, pwallet), nSleepTime);
2147 return NullUniValue;
2151 UniValue walletpassphrasechange(const JSONRPCRequest& request)
2153 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2154 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2155 return NullUniValue;
2158 if (pwallet->IsCrypted() && (request.fHelp || request.params.size() != 2)) {
2159 throw std::runtime_error(
2160 "walletpassphrasechange \"oldpassphrase\" \"newpassphrase\"\n"
2161 "\nChanges the wallet passphrase from 'oldpassphrase' to 'newpassphrase'.\n"
2162 "\nArguments:\n"
2163 "1. \"oldpassphrase\" (string) The current passphrase\n"
2164 "2. \"newpassphrase\" (string) The new passphrase\n"
2165 "\nExamples:\n"
2166 + HelpExampleCli("walletpassphrasechange", "\"old one\" \"new one\"")
2167 + HelpExampleRpc("walletpassphrasechange", "\"old one\", \"new one\"")
2171 LOCK2(cs_main, pwallet->cs_wallet);
2173 if (request.fHelp)
2174 return true;
2175 if (!pwallet->IsCrypted()) {
2176 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrasechange was called.");
2179 // TODO: get rid of these .c_str() calls by implementing SecureString::operator=(std::string)
2180 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2181 SecureString strOldWalletPass;
2182 strOldWalletPass.reserve(100);
2183 strOldWalletPass = request.params[0].get_str().c_str();
2185 SecureString strNewWalletPass;
2186 strNewWalletPass.reserve(100);
2187 strNewWalletPass = request.params[1].get_str().c_str();
2189 if (strOldWalletPass.length() < 1 || strNewWalletPass.length() < 1)
2190 throw std::runtime_error(
2191 "walletpassphrasechange <oldpassphrase> <newpassphrase>\n"
2192 "Changes the wallet passphrase from <oldpassphrase> to <newpassphrase>.");
2194 if (!pwallet->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass)) {
2195 throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
2198 return NullUniValue;
2202 UniValue walletlock(const JSONRPCRequest& request)
2204 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2205 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2206 return NullUniValue;
2209 if (pwallet->IsCrypted() && (request.fHelp || request.params.size() != 0)) {
2210 throw std::runtime_error(
2211 "walletlock\n"
2212 "\nRemoves the wallet encryption key from memory, locking the wallet.\n"
2213 "After calling this method, you will need to call walletpassphrase again\n"
2214 "before being able to call any methods which require the wallet to be unlocked.\n"
2215 "\nExamples:\n"
2216 "\nSet the passphrase for 2 minutes to perform a transaction\n"
2217 + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 120") +
2218 "\nPerform a send (requires passphrase set)\n"
2219 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 1.0") +
2220 "\nClear the passphrase since we are done before 2 minutes is up\n"
2221 + HelpExampleCli("walletlock", "") +
2222 "\nAs json rpc call\n"
2223 + HelpExampleRpc("walletlock", "")
2227 LOCK2(cs_main, pwallet->cs_wallet);
2229 if (request.fHelp)
2230 return true;
2231 if (!pwallet->IsCrypted()) {
2232 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletlock was called.");
2235 pwallet->Lock();
2236 pwallet->nRelockTime = 0;
2238 return NullUniValue;
2242 UniValue encryptwallet(const JSONRPCRequest& request)
2244 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2245 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2246 return NullUniValue;
2249 if (!pwallet->IsCrypted() && (request.fHelp || request.params.size() != 1)) {
2250 throw std::runtime_error(
2251 "encryptwallet \"passphrase\"\n"
2252 "\nEncrypts the wallet with 'passphrase'. This is for first time encryption.\n"
2253 "After this, any calls that interact with private keys such as sending or signing \n"
2254 "will require the passphrase to be set prior the making these calls.\n"
2255 "Use the walletpassphrase call for this, and then walletlock call.\n"
2256 "If the wallet is already encrypted, use the walletpassphrasechange call.\n"
2257 "Note that this will shutdown the server.\n"
2258 "\nArguments:\n"
2259 "1. \"passphrase\" (string) The pass phrase to encrypt the wallet with. It must be at least 1 character, but should be long.\n"
2260 "\nExamples:\n"
2261 "\nEncrypt your wallet\n"
2262 + HelpExampleCli("encryptwallet", "\"my pass phrase\"") +
2263 "\nNow set the passphrase to use the wallet, such as for signing or sending bitcoin\n"
2264 + HelpExampleCli("walletpassphrase", "\"my pass phrase\"") +
2265 "\nNow we can do something like sign\n"
2266 + HelpExampleCli("signmessage", "\"address\" \"test message\"") +
2267 "\nNow lock the wallet again by removing the passphrase\n"
2268 + HelpExampleCli("walletlock", "") +
2269 "\nAs a json rpc call\n"
2270 + HelpExampleRpc("encryptwallet", "\"my pass phrase\"")
2274 LOCK2(cs_main, pwallet->cs_wallet);
2276 if (request.fHelp)
2277 return true;
2278 if (pwallet->IsCrypted()) {
2279 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an encrypted wallet, but encryptwallet was called.");
2282 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
2283 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2284 SecureString strWalletPass;
2285 strWalletPass.reserve(100);
2286 strWalletPass = request.params[0].get_str().c_str();
2288 if (strWalletPass.length() < 1)
2289 throw std::runtime_error(
2290 "encryptwallet <passphrase>\n"
2291 "Encrypts the wallet with <passphrase>.");
2293 if (!pwallet->EncryptWallet(strWalletPass)) {
2294 throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: Failed to encrypt the wallet.");
2297 // BDB seems to have a bad habit of writing old data into
2298 // slack space in .dat files; that is bad if the old data is
2299 // unencrypted private keys. So:
2300 StartShutdown();
2301 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.";
2304 UniValue lockunspent(const JSONRPCRequest& request)
2306 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2307 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2308 return NullUniValue;
2311 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
2312 throw std::runtime_error(
2313 "lockunspent unlock ([{\"txid\":\"txid\",\"vout\":n},...])\n"
2314 "\nUpdates list of temporarily unspendable outputs.\n"
2315 "Temporarily lock (unlock=false) or unlock (unlock=true) specified transaction outputs.\n"
2316 "If no transaction outputs are specified when unlocking then all current locked transaction outputs are unlocked.\n"
2317 "A locked transaction output will not be chosen by automatic coin selection, when spending bitcoins.\n"
2318 "Locks are stored in memory only. Nodes start with zero locked outputs, and the locked output list\n"
2319 "is always cleared (by virtue of process exit) when a node stops or fails.\n"
2320 "Also see the listunspent call\n"
2321 "\nArguments:\n"
2322 "1. unlock (boolean, required) Whether to unlock (true) or lock (false) the specified transactions\n"
2323 "2. \"transactions\" (string, optional) A json array of objects. Each object the txid (string) vout (numeric)\n"
2324 " [ (json array of json objects)\n"
2325 " {\n"
2326 " \"txid\":\"id\", (string) The transaction id\n"
2327 " \"vout\": n (numeric) The output number\n"
2328 " }\n"
2329 " ,...\n"
2330 " ]\n"
2332 "\nResult:\n"
2333 "true|false (boolean) Whether the command was successful or not\n"
2335 "\nExamples:\n"
2336 "\nList the unspent transactions\n"
2337 + HelpExampleCli("listunspent", "") +
2338 "\nLock an unspent transaction\n"
2339 + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2340 "\nList the locked transactions\n"
2341 + HelpExampleCli("listlockunspent", "") +
2342 "\nUnlock the transaction again\n"
2343 + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2344 "\nAs a json rpc call\n"
2345 + HelpExampleRpc("lockunspent", "false, \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"")
2348 LOCK2(cs_main, pwallet->cs_wallet);
2350 if (request.params.size() == 1)
2351 RPCTypeCheck(request.params, {UniValue::VBOOL});
2352 else
2353 RPCTypeCheck(request.params, {UniValue::VBOOL, UniValue::VARR});
2355 bool fUnlock = request.params[0].get_bool();
2357 if (request.params.size() == 1) {
2358 if (fUnlock)
2359 pwallet->UnlockAllCoins();
2360 return true;
2363 UniValue outputs = request.params[1].get_array();
2364 for (unsigned int idx = 0; idx < outputs.size(); idx++) {
2365 const UniValue& output = outputs[idx];
2366 if (!output.isObject())
2367 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected object");
2368 const UniValue& o = output.get_obj();
2370 RPCTypeCheckObj(o,
2372 {"txid", UniValueType(UniValue::VSTR)},
2373 {"vout", UniValueType(UniValue::VNUM)},
2376 std::string txid = find_value(o, "txid").get_str();
2377 if (!IsHex(txid))
2378 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected hex txid");
2380 int nOutput = find_value(o, "vout").get_int();
2381 if (nOutput < 0)
2382 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout must be positive");
2384 COutPoint outpt(uint256S(txid), nOutput);
2386 if (fUnlock)
2387 pwallet->UnlockCoin(outpt);
2388 else
2389 pwallet->LockCoin(outpt);
2392 return true;
2395 UniValue listlockunspent(const JSONRPCRequest& request)
2397 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2398 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2399 return NullUniValue;
2402 if (request.fHelp || request.params.size() > 0)
2403 throw std::runtime_error(
2404 "listlockunspent\n"
2405 "\nReturns list of temporarily unspendable outputs.\n"
2406 "See the lockunspent call to lock and unlock transactions for spending.\n"
2407 "\nResult:\n"
2408 "[\n"
2409 " {\n"
2410 " \"txid\" : \"transactionid\", (string) The transaction id locked\n"
2411 " \"vout\" : n (numeric) The vout value\n"
2412 " }\n"
2413 " ,...\n"
2414 "]\n"
2415 "\nExamples:\n"
2416 "\nList the unspent transactions\n"
2417 + HelpExampleCli("listunspent", "") +
2418 "\nLock an unspent transaction\n"
2419 + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2420 "\nList the locked transactions\n"
2421 + HelpExampleCli("listlockunspent", "") +
2422 "\nUnlock the transaction again\n"
2423 + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2424 "\nAs a json rpc call\n"
2425 + HelpExampleRpc("listlockunspent", "")
2428 LOCK2(cs_main, pwallet->cs_wallet);
2430 std::vector<COutPoint> vOutpts;
2431 pwallet->ListLockedCoins(vOutpts);
2433 UniValue ret(UniValue::VARR);
2435 for (COutPoint &outpt : vOutpts) {
2436 UniValue o(UniValue::VOBJ);
2438 o.push_back(Pair("txid", outpt.hash.GetHex()));
2439 o.push_back(Pair("vout", (int)outpt.n));
2440 ret.push_back(o);
2443 return ret;
2446 UniValue settxfee(const JSONRPCRequest& request)
2448 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2449 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2450 return NullUniValue;
2453 if (request.fHelp || request.params.size() < 1 || request.params.size() > 1)
2454 throw std::runtime_error(
2455 "settxfee amount\n"
2456 "\nSet the transaction fee per kB. Overwrites the paytxfee parameter.\n"
2457 "\nArguments:\n"
2458 "1. amount (numeric or string, required) The transaction fee in " + CURRENCY_UNIT + "/kB\n"
2459 "\nResult\n"
2460 "true|false (boolean) Returns true if successful\n"
2461 "\nExamples:\n"
2462 + HelpExampleCli("settxfee", "0.00001")
2463 + HelpExampleRpc("settxfee", "0.00001")
2466 LOCK2(cs_main, pwallet->cs_wallet);
2468 // Amount
2469 CAmount nAmount = AmountFromValue(request.params[0]);
2471 payTxFee = CFeeRate(nAmount, 1000);
2472 return true;
2475 UniValue getwalletinfo(const JSONRPCRequest& request)
2477 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2478 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2479 return NullUniValue;
2482 if (request.fHelp || request.params.size() != 0)
2483 throw std::runtime_error(
2484 "getwalletinfo\n"
2485 "Returns an object containing various wallet state info.\n"
2486 "\nResult:\n"
2487 "{\n"
2488 " \"walletname\": xxxxx, (string) the wallet name\n"
2489 " \"walletversion\": xxxxx, (numeric) the wallet version\n"
2490 " \"balance\": xxxxxxx, (numeric) the total confirmed balance of the wallet in " + CURRENCY_UNIT + "\n"
2491 " \"unconfirmed_balance\": xxx, (numeric) the total unconfirmed balance of the wallet in " + CURRENCY_UNIT + "\n"
2492 " \"immature_balance\": xxxxxx, (numeric) the total immature balance of the wallet in " + CURRENCY_UNIT + "\n"
2493 " \"txcount\": xxxxxxx, (numeric) the total number of transactions in the wallet\n"
2494 " \"keypoololdest\": xxxxxx, (numeric) the timestamp (seconds since Unix epoch) of the oldest pre-generated key in the key pool\n"
2495 " \"keypoolsize\": xxxx, (numeric) how many new keys are pre-generated (only counts external keys)\n"
2496 " \"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"
2497 " \"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"
2498 " \"paytxfee\": x.xxxx, (numeric) the transaction fee configuration, set in " + CURRENCY_UNIT + "/kB\n"
2499 " \"hdmasterkeyid\": \"<hash160>\" (string) the Hash160 of the HD master pubkey\n"
2500 "}\n"
2501 "\nExamples:\n"
2502 + HelpExampleCli("getwalletinfo", "")
2503 + HelpExampleRpc("getwalletinfo", "")
2506 LOCK2(cs_main, pwallet->cs_wallet);
2508 UniValue obj(UniValue::VOBJ);
2510 size_t kpExternalSize = pwallet->KeypoolCountExternalKeys();
2511 obj.push_back(Pair("walletname", pwallet->GetName()));
2512 obj.push_back(Pair("walletversion", pwallet->GetVersion()));
2513 obj.push_back(Pair("balance", ValueFromAmount(pwallet->GetBalance())));
2514 obj.push_back(Pair("unconfirmed_balance", ValueFromAmount(pwallet->GetUnconfirmedBalance())));
2515 obj.push_back(Pair("immature_balance", ValueFromAmount(pwallet->GetImmatureBalance())));
2516 obj.push_back(Pair("txcount", (int)pwallet->mapWallet.size()));
2517 obj.push_back(Pair("keypoololdest", pwallet->GetOldestKeyPoolTime()));
2518 obj.push_back(Pair("keypoolsize", (int64_t)kpExternalSize));
2519 CKeyID masterKeyID = pwallet->GetHDChain().masterKeyID;
2520 if (!masterKeyID.IsNull() && pwallet->CanSupportFeature(FEATURE_HD_SPLIT)) {
2521 obj.push_back(Pair("keypoolsize_hd_internal", (int64_t)(pwallet->GetKeyPoolSize() - kpExternalSize)));
2523 if (pwallet->IsCrypted()) {
2524 obj.push_back(Pair("unlocked_until", pwallet->nRelockTime));
2526 obj.push_back(Pair("paytxfee", ValueFromAmount(payTxFee.GetFeePerK())));
2527 if (!masterKeyID.IsNull())
2528 obj.push_back(Pair("hdmasterkeyid", masterKeyID.GetHex()));
2529 return obj;
2532 UniValue listwallets(const JSONRPCRequest& request)
2534 if (request.fHelp || request.params.size() != 0)
2535 throw std::runtime_error(
2536 "listwallets\n"
2537 "Returns a list of currently loaded wallets.\n"
2538 "For full information on the wallet, use \"getwalletinfo\"\n"
2539 "\nResult:\n"
2540 "[ (json array of strings)\n"
2541 " \"walletname\" (string) the wallet name\n"
2542 " ...\n"
2543 "]\n"
2544 "\nExamples:\n"
2545 + HelpExampleCli("listwallets", "")
2546 + HelpExampleRpc("listwallets", "")
2549 UniValue obj(UniValue::VARR);
2551 for (CWalletRef pwallet : vpwallets) {
2553 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2554 return NullUniValue;
2557 LOCK(pwallet->cs_wallet);
2559 obj.push_back(pwallet->GetName());
2562 return obj;
2565 UniValue resendwallettransactions(const JSONRPCRequest& request)
2567 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2568 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2569 return NullUniValue;
2572 if (request.fHelp || request.params.size() != 0)
2573 throw std::runtime_error(
2574 "resendwallettransactions\n"
2575 "Immediately re-broadcast unconfirmed wallet transactions to all peers.\n"
2576 "Intended only for testing; the wallet code periodically re-broadcasts\n"
2577 "automatically.\n"
2578 "Returns array of transaction ids that were re-broadcast.\n"
2581 if (!g_connman)
2582 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
2584 LOCK2(cs_main, pwallet->cs_wallet);
2586 std::vector<uint256> txids = pwallet->ResendWalletTransactionsBefore(GetTime(), g_connman.get());
2587 UniValue result(UniValue::VARR);
2588 for (const uint256& txid : txids)
2590 result.push_back(txid.ToString());
2592 return result;
2595 UniValue listunspent(const JSONRPCRequest& request)
2597 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2598 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2599 return NullUniValue;
2602 if (request.fHelp || request.params.size() > 5)
2603 throw std::runtime_error(
2604 "listunspent ( minconf maxconf [\"addresses\",...] [include_unsafe] [query_options])\n"
2605 "\nReturns array of unspent transaction outputs\n"
2606 "with between minconf and maxconf (inclusive) confirmations.\n"
2607 "Optionally filter to only include txouts paid to specified addresses.\n"
2608 "\nArguments:\n"
2609 "1. minconf (numeric, optional, default=1) The minimum confirmations to filter\n"
2610 "2. maxconf (numeric, optional, default=9999999) The maximum confirmations to filter\n"
2611 "3. \"addresses\" (string) A json array of bitcoin addresses to filter\n"
2612 " [\n"
2613 " \"address\" (string) bitcoin address\n"
2614 " ,...\n"
2615 " ]\n"
2616 "4. include_unsafe (bool, optional, default=true) Include outputs that are not safe to spend\n"
2617 " See description of \"safe\" attribute below.\n"
2618 "5. query_options (json, optional) JSON with query options\n"
2619 " {\n"
2620 " \"minimumAmount\" (numeric or string, default=0) Minimum value of each UTXO in " + CURRENCY_UNIT + "\n"
2621 " \"maximumAmount\" (numeric or string, default=unlimited) Maximum value of each UTXO in " + CURRENCY_UNIT + "\n"
2622 " \"maximumCount\" (numeric or string, default=unlimited) Maximum number of UTXOs\n"
2623 " \"minimumSumAmount\" (numeric or string, default=unlimited) Minimum sum value of all UTXOs in " + CURRENCY_UNIT + "\n"
2624 " }\n"
2625 "\nResult\n"
2626 "[ (array of json object)\n"
2627 " {\n"
2628 " \"txid\" : \"txid\", (string) the transaction id \n"
2629 " \"vout\" : n, (numeric) the vout value\n"
2630 " \"address\" : \"address\", (string) the bitcoin address\n"
2631 " \"account\" : \"account\", (string) DEPRECATED. The associated account, or \"\" for the default account\n"
2632 " \"scriptPubKey\" : \"key\", (string) the script key\n"
2633 " \"amount\" : x.xxx, (numeric) the transaction output amount in " + CURRENCY_UNIT + "\n"
2634 " \"confirmations\" : n, (numeric) The number of confirmations\n"
2635 " \"redeemScript\" : n (string) The redeemScript if scriptPubKey is P2SH\n"
2636 " \"spendable\" : xxx, (bool) Whether we have the private keys to spend this output\n"
2637 " \"solvable\" : xxx, (bool) Whether we know how to spend this output, ignoring the lack of keys\n"
2638 " \"safe\" : xxx (bool) Whether this output is considered safe to spend. Unconfirmed transactions\n"
2639 " from outside keys and unconfirmed replacement transactions are considered unsafe\n"
2640 " and are not eligible for spending by fundrawtransaction and sendtoaddress.\n"
2641 " }\n"
2642 " ,...\n"
2643 "]\n"
2645 "\nExamples\n"
2646 + HelpExampleCli("listunspent", "")
2647 + HelpExampleCli("listunspent", "6 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
2648 + HelpExampleRpc("listunspent", "6, 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
2649 + HelpExampleCli("listunspent", "6 9999999 '[]' true '{ \"minimumAmount\": 0.005 }'")
2650 + HelpExampleRpc("listunspent", "6, 9999999, [] , true, { \"minimumAmount\": 0.005 } ")
2653 int nMinDepth = 1;
2654 if (request.params.size() > 0 && !request.params[0].isNull()) {
2655 RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
2656 nMinDepth = request.params[0].get_int();
2659 int nMaxDepth = 9999999;
2660 if (request.params.size() > 1 && !request.params[1].isNull()) {
2661 RPCTypeCheckArgument(request.params[1], UniValue::VNUM);
2662 nMaxDepth = request.params[1].get_int();
2665 std::set<CBitcoinAddress> setAddress;
2666 if (request.params.size() > 2 && !request.params[2].isNull()) {
2667 RPCTypeCheckArgument(request.params[2], UniValue::VARR);
2668 UniValue inputs = request.params[2].get_array();
2669 for (unsigned int idx = 0; idx < inputs.size(); idx++) {
2670 const UniValue& input = inputs[idx];
2671 CBitcoinAddress address(input.get_str());
2672 if (!address.IsValid())
2673 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ")+input.get_str());
2674 if (setAddress.count(address))
2675 throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ")+input.get_str());
2676 setAddress.insert(address);
2680 bool include_unsafe = true;
2681 if (request.params.size() > 3 && !request.params[3].isNull()) {
2682 RPCTypeCheckArgument(request.params[3], UniValue::VBOOL);
2683 include_unsafe = request.params[3].get_bool();
2686 CAmount nMinimumAmount = 0;
2687 CAmount nMaximumAmount = MAX_MONEY;
2688 CAmount nMinimumSumAmount = MAX_MONEY;
2689 uint64_t nMaximumCount = 0;
2691 if (!request.params[4].isNull()) {
2692 const UniValue& options = request.params[4].get_obj();
2694 if (options.exists("minimumAmount"))
2695 nMinimumAmount = AmountFromValue(options["minimumAmount"]);
2697 if (options.exists("maximumAmount"))
2698 nMaximumAmount = AmountFromValue(options["maximumAmount"]);
2700 if (options.exists("minimumSumAmount"))
2701 nMinimumSumAmount = AmountFromValue(options["minimumSumAmount"]);
2703 if (options.exists("maximumCount"))
2704 nMaximumCount = options["maximumCount"].get_int64();
2707 UniValue results(UniValue::VARR);
2708 std::vector<COutput> vecOutputs;
2709 assert(pwallet != NULL);
2710 LOCK2(cs_main, pwallet->cs_wallet);
2712 pwallet->AvailableCoins(vecOutputs, !include_unsafe, NULL, nMinimumAmount, nMaximumAmount, nMinimumSumAmount, nMaximumCount, nMinDepth, nMaxDepth);
2713 for (const COutput& out : vecOutputs) {
2714 CTxDestination address;
2715 const CScript& scriptPubKey = out.tx->tx->vout[out.i].scriptPubKey;
2716 bool fValidAddress = ExtractDestination(scriptPubKey, address);
2718 if (setAddress.size() && (!fValidAddress || !setAddress.count(address)))
2719 continue;
2721 UniValue entry(UniValue::VOBJ);
2722 entry.push_back(Pair("txid", out.tx->GetHash().GetHex()));
2723 entry.push_back(Pair("vout", out.i));
2725 if (fValidAddress) {
2726 entry.push_back(Pair("address", CBitcoinAddress(address).ToString()));
2728 if (pwallet->mapAddressBook.count(address)) {
2729 entry.push_back(Pair("account", pwallet->mapAddressBook[address].name));
2732 if (scriptPubKey.IsPayToScriptHash()) {
2733 const CScriptID& hash = boost::get<CScriptID>(address);
2734 CScript redeemScript;
2735 if (pwallet->GetCScript(hash, redeemScript)) {
2736 entry.push_back(Pair("redeemScript", HexStr(redeemScript.begin(), redeemScript.end())));
2741 entry.push_back(Pair("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
2742 entry.push_back(Pair("amount", ValueFromAmount(out.tx->tx->vout[out.i].nValue)));
2743 entry.push_back(Pair("confirmations", out.nDepth));
2744 entry.push_back(Pair("spendable", out.fSpendable));
2745 entry.push_back(Pair("solvable", out.fSolvable));
2746 entry.push_back(Pair("safe", out.fSafe));
2747 results.push_back(entry);
2750 return results;
2753 UniValue fundrawtransaction(const JSONRPCRequest& request)
2755 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2756 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2757 return NullUniValue;
2760 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
2761 throw std::runtime_error(
2762 "fundrawtransaction \"hexstring\" ( options )\n"
2763 "\nAdd inputs to a transaction until it has enough in value to meet its out value.\n"
2764 "This will not modify existing inputs, and will add at most one change output to the outputs.\n"
2765 "No existing outputs will be modified unless \"subtractFeeFromOutputs\" is specified.\n"
2766 "Note that inputs which were signed may need to be resigned after completion since in/outputs have been added.\n"
2767 "The inputs added will not be signed, use signrawtransaction for that.\n"
2768 "Note that all existing inputs must have their previous output transaction be in the wallet.\n"
2769 "Note that all inputs selected must be of standard form and P2SH scripts must be\n"
2770 "in the wallet using importaddress or addmultisigaddress (to calculate fees).\n"
2771 "You can see whether this is the case by checking the \"solvable\" field in the listunspent output.\n"
2772 "Only pay-to-pubkey, multisig, and P2SH versions thereof are currently supported for watch-only\n"
2773 "\nArguments:\n"
2774 "1. \"hexstring\" (string, required) The hex string of the raw transaction\n"
2775 "2. options (object, optional)\n"
2776 " {\n"
2777 " \"changeAddress\" (string, optional, default pool address) The bitcoin address to receive the change\n"
2778 " \"changePosition\" (numeric, optional, default random) The index of the change output\n"
2779 " \"includeWatching\" (boolean, optional, default false) Also select inputs which are watch only\n"
2780 " \"lockUnspents\" (boolean, optional, default false) Lock selected unspent outputs\n"
2781 " \"feeRate\" (numeric, optional, default not set: makes wallet determine the fee) Set a specific feerate (" + CURRENCY_UNIT + " per KB)\n"
2782 " \"subtractFeeFromOutputs\" (array, optional) A json array of integers.\n"
2783 " The fee will be equally deducted from the amount of each specified output.\n"
2784 " The outputs are specified by their zero-based index, before any change output is added.\n"
2785 " Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n"
2786 " If no outputs are specified here, the sender pays the fee.\n"
2787 " [vout_index,...]\n"
2788 " \"replaceable\" (boolean, optional) Marks this transaction as BIP125 replaceable.\n"
2789 " Allows this transaction to be replaced by a transaction with higher fees\n"
2790 " \"conf_target\" (numeric, optional) Confirmation target (in blocks)\n"
2791 " \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
2792 " \"UNSET\"\n"
2793 " \"ECONOMICAL\"\n"
2794 " \"CONSERVATIVE\"\n"
2795 " }\n"
2796 " for backward compatibility: passing in a true instead of an object will result in {\"includeWatching\":true}\n"
2797 "\nResult:\n"
2798 "{\n"
2799 " \"hex\": \"value\", (string) The resulting raw transaction (hex-encoded string)\n"
2800 " \"fee\": n, (numeric) Fee in " + CURRENCY_UNIT + " the resulting transaction pays\n"
2801 " \"changepos\": n (numeric) The position of the added change output, or -1\n"
2802 "}\n"
2803 "\nExamples:\n"
2804 "\nCreate a transaction with no inputs\n"
2805 + HelpExampleCli("createrawtransaction", "\"[]\" \"{\\\"myaddress\\\":0.01}\"") +
2806 "\nAdd sufficient unsigned inputs to meet the output value\n"
2807 + HelpExampleCli("fundrawtransaction", "\"rawtransactionhex\"") +
2808 "\nSign the transaction\n"
2809 + HelpExampleCli("signrawtransaction", "\"fundedtransactionhex\"") +
2810 "\nSend the transaction\n"
2811 + HelpExampleCli("sendrawtransaction", "\"signedtransactionhex\"")
2814 RPCTypeCheck(request.params, {UniValue::VSTR});
2816 CCoinControl coinControl;
2817 int changePosition = -1;
2818 bool lockUnspents = false;
2819 UniValue subtractFeeFromOutputs;
2820 std::set<int> setSubtractFeeFromOutputs;
2822 if (!request.params[1].isNull()) {
2823 if (request.params[1].type() == UniValue::VBOOL) {
2824 // backward compatibility bool only fallback
2825 coinControl.fAllowWatchOnly = request.params[1].get_bool();
2827 else {
2828 RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VOBJ});
2830 UniValue options = request.params[1];
2832 RPCTypeCheckObj(options,
2834 {"changeAddress", UniValueType(UniValue::VSTR)},
2835 {"changePosition", UniValueType(UniValue::VNUM)},
2836 {"includeWatching", UniValueType(UniValue::VBOOL)},
2837 {"lockUnspents", UniValueType(UniValue::VBOOL)},
2838 {"reserveChangeKey", UniValueType(UniValue::VBOOL)}, // DEPRECATED (and ignored), should be removed in 0.16 or so.
2839 {"feeRate", UniValueType()}, // will be checked below
2840 {"subtractFeeFromOutputs", UniValueType(UniValue::VARR)},
2841 {"replaceable", UniValueType(UniValue::VBOOL)},
2842 {"conf_target", UniValueType(UniValue::VNUM)},
2843 {"estimate_mode", UniValueType(UniValue::VSTR)},
2845 true, true);
2847 if (options.exists("changeAddress")) {
2848 CBitcoinAddress address(options["changeAddress"].get_str());
2850 if (!address.IsValid())
2851 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "changeAddress must be a valid bitcoin address");
2853 coinControl.destChange = address.Get();
2856 if (options.exists("changePosition"))
2857 changePosition = options["changePosition"].get_int();
2859 if (options.exists("includeWatching"))
2860 coinControl.fAllowWatchOnly = options["includeWatching"].get_bool();
2862 if (options.exists("lockUnspents"))
2863 lockUnspents = options["lockUnspents"].get_bool();
2865 if (options.exists("feeRate"))
2867 coinControl.m_feerate = CFeeRate(AmountFromValue(options["feeRate"]));
2868 coinControl.fOverrideFeeRate = true;
2871 if (options.exists("subtractFeeFromOutputs"))
2872 subtractFeeFromOutputs = options["subtractFeeFromOutputs"].get_array();
2874 if (options.exists("replaceable")) {
2875 coinControl.signalRbf = options["replaceable"].get_bool();
2877 if (options.exists("conf_target")) {
2878 coinControl.m_confirm_target = ParseConfirmTarget(options["conf_target"]);
2880 if (options.exists("estimate_mode")) {
2881 if (!FeeModeFromString(options["estimate_mode"].get_str(), coinControl.m_fee_mode)) {
2882 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid estimate_mode parameter");
2888 // parse hex string from parameter
2889 CMutableTransaction tx;
2890 if (!DecodeHexTx(tx, request.params[0].get_str(), true))
2891 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
2893 if (tx.vout.size() == 0)
2894 throw JSONRPCError(RPC_INVALID_PARAMETER, "TX must have at least one output");
2896 if (changePosition != -1 && (changePosition < 0 || (unsigned int)changePosition > tx.vout.size()))
2897 throw JSONRPCError(RPC_INVALID_PARAMETER, "changePosition out of bounds");
2899 for (unsigned int idx = 0; idx < subtractFeeFromOutputs.size(); idx++) {
2900 int pos = subtractFeeFromOutputs[idx].get_int();
2901 if (setSubtractFeeFromOutputs.count(pos))
2902 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, duplicated position: %d", pos));
2903 if (pos < 0)
2904 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, negative position: %d", pos));
2905 if (pos >= int(tx.vout.size()))
2906 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, position too large: %d", pos));
2907 setSubtractFeeFromOutputs.insert(pos);
2910 CAmount nFeeOut;
2911 std::string strFailReason;
2913 if (!pwallet->FundTransaction(tx, nFeeOut, changePosition, strFailReason, lockUnspents, setSubtractFeeFromOutputs, coinControl)) {
2914 throw JSONRPCError(RPC_WALLET_ERROR, strFailReason);
2917 UniValue result(UniValue::VOBJ);
2918 result.push_back(Pair("hex", EncodeHexTx(tx)));
2919 result.push_back(Pair("changepos", changePosition));
2920 result.push_back(Pair("fee", ValueFromAmount(nFeeOut)));
2922 return result;
2925 UniValue bumpfee(const JSONRPCRequest& request)
2927 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2929 if (!EnsureWalletIsAvailable(pwallet, request.fHelp))
2930 return NullUniValue;
2932 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) {
2933 throw std::runtime_error(
2934 "bumpfee \"txid\" ( options ) \n"
2935 "\nBumps the fee of an opt-in-RBF transaction T, replacing it with a new transaction B.\n"
2936 "An opt-in RBF transaction with the given txid must be in the wallet.\n"
2937 "The command will pay the additional fee by decreasing (or perhaps removing) its change output.\n"
2938 "If the change output is not big enough to cover the increased fee, the command will currently fail\n"
2939 "instead of adding new inputs to compensate. (A future implementation could improve this.)\n"
2940 "The command will fail if the wallet or mempool contains a transaction that spends one of T's outputs.\n"
2941 "By default, the new fee will be calculated automatically using estimatefee.\n"
2942 "The user can specify a confirmation target for estimatefee.\n"
2943 "Alternatively, the user can specify totalFee, or use RPC settxfee to set a higher fee rate.\n"
2944 "At a minimum, the new fee rate must be high enough to pay an additional new relay fee (incrementalfee\n"
2945 "returned by getnetworkinfo) to enter the node's mempool.\n"
2946 "\nArguments:\n"
2947 "1. txid (string, required) The txid to be bumped\n"
2948 "2. options (object, optional)\n"
2949 " {\n"
2950 " \"confTarget\" (numeric, optional) Confirmation target (in blocks)\n"
2951 " \"totalFee\" (numeric, optional) Total fee (NOT feerate) to pay, in satoshis.\n"
2952 " In rare cases, the actual fee paid might be slightly higher than the specified\n"
2953 " totalFee if the tx change output has to be removed because it is too close to\n"
2954 " the dust threshold.\n"
2955 " \"replaceable\" (boolean, optional, default true) Whether the new transaction should still be\n"
2956 " marked bip-125 replaceable. If true, the sequence numbers in the transaction will\n"
2957 " be left unchanged from the original. If false, any input sequence numbers in the\n"
2958 " original transaction that were less than 0xfffffffe will be increased to 0xfffffffe\n"
2959 " so the new transaction will not be explicitly bip-125 replaceable (though it may\n"
2960 " still be replaceable in practice, for example if it has unconfirmed ancestors which\n"
2961 " are replaceable).\n"
2962 " \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
2963 " \"UNSET\"\n"
2964 " \"ECONOMICAL\"\n"
2965 " \"CONSERVATIVE\"\n"
2966 " }\n"
2967 "\nResult:\n"
2968 "{\n"
2969 " \"txid\": \"value\", (string) The id of the new transaction\n"
2970 " \"origfee\": n, (numeric) Fee of the replaced transaction\n"
2971 " \"fee\": n, (numeric) Fee of the new transaction\n"
2972 " \"errors\": [ str... ] (json array of strings) Errors encountered during processing (may be empty)\n"
2973 "}\n"
2974 "\nExamples:\n"
2975 "\nBump the fee, get the new transaction\'s txid\n" +
2976 HelpExampleCli("bumpfee", "<txid>"));
2979 RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VOBJ});
2980 uint256 hash;
2981 hash.SetHex(request.params[0].get_str());
2983 // optional parameters
2984 CAmount totalFee = 0;
2985 CCoinControl coin_control;
2986 coin_control.signalRbf = true;
2987 if (!request.params[1].isNull()) {
2988 UniValue options = request.params[1];
2989 RPCTypeCheckObj(options,
2991 {"confTarget", UniValueType(UniValue::VNUM)},
2992 {"totalFee", UniValueType(UniValue::VNUM)},
2993 {"replaceable", UniValueType(UniValue::VBOOL)},
2994 {"estimate_mode", UniValueType(UniValue::VSTR)},
2996 true, true);
2998 if (options.exists("confTarget") && options.exists("totalFee")) {
2999 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.");
3000 } else if (options.exists("confTarget")) { // TODO: alias this to conf_target
3001 coin_control.m_confirm_target = ParseConfirmTarget(options["confTarget"]);
3002 } else if (options.exists("totalFee")) {
3003 totalFee = options["totalFee"].get_int64();
3004 if (totalFee <= 0) {
3005 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid totalFee %s (must be greater than 0)", FormatMoney(totalFee)));
3009 if (options.exists("replaceable")) {
3010 coin_control.signalRbf = options["replaceable"].get_bool();
3012 if (options.exists("estimate_mode")) {
3013 if (!FeeModeFromString(options["estimate_mode"].get_str(), coin_control.m_fee_mode)) {
3014 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid estimate_mode parameter");
3019 LOCK2(cs_main, pwallet->cs_wallet);
3020 EnsureWalletIsUnlocked(pwallet);
3022 CFeeBumper feeBump(pwallet, hash, coin_control, totalFee);
3023 BumpFeeResult res = feeBump.getResult();
3024 if (res != BumpFeeResult::OK)
3026 switch(res) {
3027 case BumpFeeResult::INVALID_ADDRESS_OR_KEY:
3028 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, feeBump.getErrors()[0]);
3029 break;
3030 case BumpFeeResult::INVALID_REQUEST:
3031 throw JSONRPCError(RPC_INVALID_REQUEST, feeBump.getErrors()[0]);
3032 break;
3033 case BumpFeeResult::INVALID_PARAMETER:
3034 throw JSONRPCError(RPC_INVALID_PARAMETER, feeBump.getErrors()[0]);
3035 break;
3036 case BumpFeeResult::WALLET_ERROR:
3037 throw JSONRPCError(RPC_WALLET_ERROR, feeBump.getErrors()[0]);
3038 break;
3039 default:
3040 throw JSONRPCError(RPC_MISC_ERROR, feeBump.getErrors()[0]);
3041 break;
3045 // sign bumped transaction
3046 if (!feeBump.signTransaction(pwallet)) {
3047 throw JSONRPCError(RPC_WALLET_ERROR, "Can't sign transaction.");
3049 // commit the bumped transaction
3050 if(!feeBump.commit(pwallet)) {
3051 throw JSONRPCError(RPC_WALLET_ERROR, feeBump.getErrors()[0]);
3053 UniValue result(UniValue::VOBJ);
3054 result.push_back(Pair("txid", feeBump.getBumpedTxId().GetHex()));
3055 result.push_back(Pair("origfee", ValueFromAmount(feeBump.getOldFee())));
3056 result.push_back(Pair("fee", ValueFromAmount(feeBump.getNewFee())));
3057 UniValue errors(UniValue::VARR);
3058 for (const std::string& err: feeBump.getErrors())
3059 errors.push_back(err);
3060 result.push_back(Pair("errors", errors));
3062 return result;
3065 UniValue generate(const JSONRPCRequest& request)
3067 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
3069 if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
3070 return NullUniValue;
3073 if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) {
3074 throw std::runtime_error(
3075 "generate nblocks ( maxtries )\n"
3076 "\nMine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.\n"
3077 "\nArguments:\n"
3078 "1. nblocks (numeric, required) How many blocks are generated immediately.\n"
3079 "2. maxtries (numeric, optional) How many iterations to try (default = 1000000).\n"
3080 "\nResult:\n"
3081 "[ blockhashes ] (array) hashes of blocks generated\n"
3082 "\nExamples:\n"
3083 "\nGenerate 11 blocks\n"
3084 + HelpExampleCli("generate", "11")
3088 int num_generate = request.params[0].get_int();
3089 uint64_t max_tries = 1000000;
3090 if (request.params.size() > 1 && !request.params[1].isNull()) {
3091 max_tries = request.params[1].get_int();
3094 std::shared_ptr<CReserveScript> coinbase_script;
3095 pwallet->GetScriptForMining(coinbase_script);
3097 // If the keypool is exhausted, no script is returned at all. Catch this.
3098 if (!coinbase_script) {
3099 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
3102 //throw an error if no script was provided
3103 if (coinbase_script->reserveScript.empty()) {
3104 throw JSONRPCError(RPC_INTERNAL_ERROR, "No coinbase script available");
3107 return generateBlocks(coinbase_script, num_generate, max_tries, true);
3110 extern UniValue abortrescan(const JSONRPCRequest& request); // in rpcdump.cpp
3111 extern UniValue dumpprivkey(const JSONRPCRequest& request); // in rpcdump.cpp
3112 extern UniValue importprivkey(const JSONRPCRequest& request);
3113 extern UniValue importaddress(const JSONRPCRequest& request);
3114 extern UniValue importpubkey(const JSONRPCRequest& request);
3115 extern UniValue dumpwallet(const JSONRPCRequest& request);
3116 extern UniValue importwallet(const JSONRPCRequest& request);
3117 extern UniValue importprunedfunds(const JSONRPCRequest& request);
3118 extern UniValue removeprunedfunds(const JSONRPCRequest& request);
3119 extern UniValue importmulti(const JSONRPCRequest& request);
3121 static const CRPCCommand commands[] =
3122 { // category name actor (function) okSafeMode
3123 // --------------------- ------------------------ ----------------------- ----------
3124 { "rawtransactions", "fundrawtransaction", &fundrawtransaction, false, {"hexstring","options"} },
3125 { "hidden", "resendwallettransactions", &resendwallettransactions, true, {} },
3126 { "wallet", "abandontransaction", &abandontransaction, false, {"txid"} },
3127 { "wallet", "abortrescan", &abortrescan, false, {} },
3128 { "wallet", "addmultisigaddress", &addmultisigaddress, true, {"nrequired","keys","account"} },
3129 { "wallet", "addwitnessaddress", &addwitnessaddress, true, {"address"} },
3130 { "wallet", "backupwallet", &backupwallet, true, {"destination"} },
3131 { "wallet", "bumpfee", &bumpfee, true, {"txid", "options"} },
3132 { "wallet", "dumpprivkey", &dumpprivkey, true, {"address"} },
3133 { "wallet", "dumpwallet", &dumpwallet, true, {"filename"} },
3134 { "wallet", "encryptwallet", &encryptwallet, true, {"passphrase"} },
3135 { "wallet", "getaccountaddress", &getaccountaddress, true, {"account"} },
3136 { "wallet", "getaccount", &getaccount, true, {"address"} },
3137 { "wallet", "getaddressesbyaccount", &getaddressesbyaccount, true, {"account"} },
3138 { "wallet", "getbalance", &getbalance, false, {"account","minconf","include_watchonly"} },
3139 { "wallet", "getnewaddress", &getnewaddress, true, {"account"} },
3140 { "wallet", "getrawchangeaddress", &getrawchangeaddress, true, {} },
3141 { "wallet", "getreceivedbyaccount", &getreceivedbyaccount, false, {"account","minconf"} },
3142 { "wallet", "getreceivedbyaddress", &getreceivedbyaddress, false, {"address","minconf"} },
3143 { "wallet", "gettransaction", &gettransaction, false, {"txid","include_watchonly"} },
3144 { "wallet", "getunconfirmedbalance", &getunconfirmedbalance, false, {} },
3145 { "wallet", "getwalletinfo", &getwalletinfo, false, {} },
3146 { "wallet", "importmulti", &importmulti, true, {"requests","options"} },
3147 { "wallet", "importprivkey", &importprivkey, true, {"privkey","label","rescan"} },
3148 { "wallet", "importwallet", &importwallet, true, {"filename"} },
3149 { "wallet", "importaddress", &importaddress, true, {"address","label","rescan","p2sh"} },
3150 { "wallet", "importprunedfunds", &importprunedfunds, true, {"rawtransaction","txoutproof"} },
3151 { "wallet", "importpubkey", &importpubkey, true, {"pubkey","label","rescan"} },
3152 { "wallet", "keypoolrefill", &keypoolrefill, true, {"newsize"} },
3153 { "wallet", "listaccounts", &listaccounts, false, {"minconf","include_watchonly"} },
3154 { "wallet", "listaddressgroupings", &listaddressgroupings, false, {} },
3155 { "wallet", "listlockunspent", &listlockunspent, false, {} },
3156 { "wallet", "listreceivedbyaccount", &listreceivedbyaccount, false, {"minconf","include_empty","include_watchonly"} },
3157 { "wallet", "listreceivedbyaddress", &listreceivedbyaddress, false, {"minconf","include_empty","include_watchonly"} },
3158 { "wallet", "listsinceblock", &listsinceblock, false, {"blockhash","target_confirmations","include_watchonly","include_removed"} },
3159 { "wallet", "listtransactions", &listtransactions, false, {"account","count","skip","include_watchonly"} },
3160 { "wallet", "listunspent", &listunspent, false, {"minconf","maxconf","addresses","include_unsafe","query_options"} },
3161 { "wallet", "listwallets", &listwallets, true, {} },
3162 { "wallet", "lockunspent", &lockunspent, true, {"unlock","transactions"} },
3163 { "wallet", "move", &movecmd, false, {"fromaccount","toaccount","amount","minconf","comment"} },
3164 { "wallet", "sendfrom", &sendfrom, false, {"fromaccount","toaddress","amount","minconf","comment","comment_to"} },
3165 { "wallet", "sendmany", &sendmany, false, {"fromaccount","amounts","minconf","comment","subtractfeefrom","replaceable","conf_target","estimate_mode"} },
3166 { "wallet", "sendtoaddress", &sendtoaddress, false, {"address","amount","comment","comment_to","subtractfeefromamount","replaceable","conf_target","estimate_mode"} },
3167 { "wallet", "setaccount", &setaccount, true, {"address","account"} },
3168 { "wallet", "settxfee", &settxfee, true, {"amount"} },
3169 { "wallet", "signmessage", &signmessage, true, {"address","message"} },
3170 { "wallet", "walletlock", &walletlock, true, {} },
3171 { "wallet", "walletpassphrasechange", &walletpassphrasechange, true, {"oldpassphrase","newpassphrase"} },
3172 { "wallet", "walletpassphrase", &walletpassphrase, true, {"passphrase","timeout"} },
3173 { "wallet", "removeprunedfunds", &removeprunedfunds, true, {"txid"} },
3175 { "generating", "generate", &generate, true, {"nblocks","maxtries"} },
3178 void RegisterWalletRPCCommands(CRPCTable &t)
3180 if (GetBoolArg("-disablewallet", false))
3181 return;
3183 for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
3184 t.appendCommand(commands[vcidx].name, &commands[vcidx]);