1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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.
9 #include <consensus/validation.h>
11 #include <httpserver.h>
12 #include <validation.h>
14 #include <policy/feerate.h>
15 #include <policy/fees.h>
16 #include <policy/policy.h>
17 #include <policy/rbf.h>
18 #include <rpc/mining.h>
19 #include <rpc/safemode.h>
20 #include <rpc/server.h>
21 #include <script/sign.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>
29 #include <wallet/walletutil.h>
31 #include <init.h> // For StartShutdown
37 static const std::string WALLET_ENDPOINT_BASE
= "/wallet/";
39 CWallet
*GetWalletForJSONRPCRequest(const JSONRPCRequest
& request
)
41 if (request
.URI
.substr(0, WALLET_ENDPOINT_BASE
.size()) == WALLET_ENDPOINT_BASE
) {
42 // wallet endpoint was used
43 std::string requestedWallet
= urlDecode(request
.URI
.substr(WALLET_ENDPOINT_BASE
.size()));
44 for (CWalletRef pwallet
: ::vpwallets
) {
45 if (pwallet
->GetName() == requestedWallet
) {
49 throw JSONRPCError(RPC_WALLET_NOT_FOUND
, "Requested wallet does not exist or is not loaded");
51 return ::vpwallets
.size() == 1 || (request
.fHelp
&& ::vpwallets
.size() > 0) ? ::vpwallets
[0] : nullptr;
54 std::string
HelpRequiringPassphrase(CWallet
* const pwallet
)
56 return pwallet
&& pwallet
->IsCrypted()
57 ? "\nRequires wallet passphrase to be set with walletpassphrase call."
61 bool EnsureWalletIsAvailable(CWallet
* const pwallet
, bool avoidException
)
63 if (pwallet
) return true;
64 if (avoidException
) return false;
65 if (::vpwallets
.empty()) {
66 // Note: It isn't currently possible to trigger this error because
67 // wallet RPC methods aren't registered unless a wallet is loaded. But
68 // this error is being kept as a precaution, because it's possible in
69 // the future that wallet RPC methods might get or remain registered
70 // when no wallets are loaded.
72 RPC_METHOD_NOT_FOUND
, "Method not found (wallet method is disabled because no wallet is loaded)");
74 throw JSONRPCError(RPC_WALLET_NOT_SPECIFIED
,
75 "Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path).");
78 void EnsureWalletIsUnlocked(CWallet
* const pwallet
)
80 if (pwallet
->IsLocked()) {
81 throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED
, "Error: Please enter the wallet passphrase with walletpassphrase first.");
85 void WalletTxToJSON(const CWalletTx
& wtx
, UniValue
& entry
)
87 int confirms
= wtx
.GetDepthInMainChain();
88 entry
.push_back(Pair("confirmations", confirms
));
90 entry
.push_back(Pair("generated", true));
93 entry
.push_back(Pair("blockhash", wtx
.hashBlock
.GetHex()));
94 entry
.push_back(Pair("blockindex", wtx
.nIndex
));
95 entry
.push_back(Pair("blocktime", mapBlockIndex
[wtx
.hashBlock
]->GetBlockTime()));
97 entry
.push_back(Pair("trusted", wtx
.IsTrusted()));
99 uint256 hash
= wtx
.GetHash();
100 entry
.push_back(Pair("txid", hash
.GetHex()));
101 UniValue
conflicts(UniValue::VARR
);
102 for (const uint256
& conflict
: wtx
.GetConflicts())
103 conflicts
.push_back(conflict
.GetHex());
104 entry
.push_back(Pair("walletconflicts", conflicts
));
105 entry
.push_back(Pair("time", wtx
.GetTxTime()));
106 entry
.push_back(Pair("timereceived", (int64_t)wtx
.nTimeReceived
));
108 // Add opt-in RBF status
109 std::string rbfStatus
= "no";
112 RBFTransactionState rbfState
= IsRBFOptIn(*wtx
.tx
, mempool
);
113 if (rbfState
== RBF_TRANSACTIONSTATE_UNKNOWN
)
114 rbfStatus
= "unknown";
115 else if (rbfState
== RBF_TRANSACTIONSTATE_REPLACEABLE_BIP125
)
118 entry
.push_back(Pair("bip125-replaceable", rbfStatus
));
120 for (const std::pair
<std::string
, std::string
>& item
: wtx
.mapValue
)
121 entry
.push_back(Pair(item
.first
, item
.second
));
124 std::string
AccountFromValue(const UniValue
& value
)
126 std::string strAccount
= value
.get_str();
127 if (strAccount
== "*")
128 throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME
, "Invalid account name");
132 UniValue
getnewaddress(const JSONRPCRequest
& request
)
134 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
135 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
139 if (request
.fHelp
|| request
.params
.size() > 1)
140 throw std::runtime_error(
141 "getnewaddress ( \"account\" )\n"
142 "\nReturns a new Bitcoin address for receiving payments.\n"
143 "If 'account' is specified (DEPRECATED), it is added to the address book \n"
144 "so payments received with the address will be credited to 'account'.\n"
146 "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"
148 "\"address\" (string) The new bitcoin address\n"
150 + HelpExampleCli("getnewaddress", "")
151 + HelpExampleRpc("getnewaddress", "")
154 LOCK2(cs_main
, pwallet
->cs_wallet
);
156 // Parse the account first so we don't generate a key if there's an error
157 std::string strAccount
;
158 if (!request
.params
[0].isNull())
159 strAccount
= AccountFromValue(request
.params
[0]);
161 if (!pwallet
->IsLocked()) {
162 pwallet
->TopUpKeyPool();
165 // Generate a new key that is added to wallet
167 if (!pwallet
->GetKeyFromPool(newKey
)) {
168 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT
, "Error: Keypool ran out, please call keypoolrefill first");
170 CKeyID keyID
= newKey
.GetID();
172 pwallet
->SetAddressBook(keyID
, strAccount
, "receive");
174 return EncodeDestination(keyID
);
178 CTxDestination
GetAccountAddress(CWallet
* const pwallet
, std::string strAccount
, bool bForceNew
=false)
181 if (!pwallet
->GetAccountPubkey(pubKey
, strAccount
, bForceNew
)) {
182 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT
, "Error: Keypool ran out, please call keypoolrefill first");
185 return pubKey
.GetID();
188 UniValue
getaccountaddress(const JSONRPCRequest
& request
)
190 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
191 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
195 if (request
.fHelp
|| request
.params
.size() != 1)
196 throw std::runtime_error(
197 "getaccountaddress \"account\"\n"
198 "\nDEPRECATED. Returns the current Bitcoin address for receiving payments to this account.\n"
200 "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"
202 "\"address\" (string) The account bitcoin address\n"
204 + HelpExampleCli("getaccountaddress", "")
205 + HelpExampleCli("getaccountaddress", "\"\"")
206 + HelpExampleCli("getaccountaddress", "\"myaccount\"")
207 + HelpExampleRpc("getaccountaddress", "\"myaccount\"")
210 LOCK2(cs_main
, pwallet
->cs_wallet
);
212 // Parse the account first so we don't generate a key if there's an error
213 std::string strAccount
= AccountFromValue(request
.params
[0]);
215 UniValue
ret(UniValue::VSTR
);
217 ret
= EncodeDestination(GetAccountAddress(pwallet
, strAccount
));
222 UniValue
getrawchangeaddress(const JSONRPCRequest
& request
)
224 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
225 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
229 if (request
.fHelp
|| request
.params
.size() > 0)
230 throw std::runtime_error(
231 "getrawchangeaddress\n"
232 "\nReturns a new Bitcoin address, for receiving change.\n"
233 "This is for use with raw transactions, NOT normal use.\n"
235 "\"address\" (string) The address\n"
237 + HelpExampleCli("getrawchangeaddress", "")
238 + HelpExampleRpc("getrawchangeaddress", "")
241 LOCK2(cs_main
, pwallet
->cs_wallet
);
243 if (!pwallet
->IsLocked()) {
244 pwallet
->TopUpKeyPool();
247 CReserveKey
reservekey(pwallet
);
249 if (!reservekey
.GetReservedKey(vchPubKey
, true))
250 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT
, "Error: Keypool ran out, please call keypoolrefill first");
252 reservekey
.KeepKey();
254 CKeyID keyID
= vchPubKey
.GetID();
256 return EncodeDestination(keyID
);
260 UniValue
setaccount(const JSONRPCRequest
& request
)
262 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
263 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
267 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2)
268 throw std::runtime_error(
269 "setaccount \"address\" \"account\"\n"
270 "\nDEPRECATED. Sets the account associated with the given address.\n"
272 "1. \"address\" (string, required) The bitcoin address to be associated with an account.\n"
273 "2. \"account\" (string, required) The account to assign the address to.\n"
275 + HelpExampleCli("setaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"tabby\"")
276 + HelpExampleRpc("setaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"tabby\"")
279 LOCK2(cs_main
, pwallet
->cs_wallet
);
281 CTxDestination dest
= DecodeDestination(request
.params
[0].get_str());
282 if (!IsValidDestination(dest
)) {
283 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid Bitcoin address");
286 std::string strAccount
;
287 if (!request
.params
[1].isNull())
288 strAccount
= AccountFromValue(request
.params
[1]);
290 // Only add the account if the address is yours.
291 if (IsMine(*pwallet
, dest
)) {
292 // Detect when changing the account of an address that is the 'unused current key' of another account:
293 if (pwallet
->mapAddressBook
.count(dest
)) {
294 std::string strOldAccount
= pwallet
->mapAddressBook
[dest
].name
;
295 if (dest
== GetAccountAddress(pwallet
, strOldAccount
)) {
296 GetAccountAddress(pwallet
, strOldAccount
, true);
299 pwallet
->SetAddressBook(dest
, strAccount
, "receive");
302 throw JSONRPCError(RPC_MISC_ERROR
, "setaccount can only be used with own address");
308 UniValue
getaccount(const JSONRPCRequest
& request
)
310 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
311 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
315 if (request
.fHelp
|| request
.params
.size() != 1)
316 throw std::runtime_error(
317 "getaccount \"address\"\n"
318 "\nDEPRECATED. Returns the account associated with the given address.\n"
320 "1. \"address\" (string, required) The bitcoin address for account lookup.\n"
322 "\"accountname\" (string) the account address\n"
324 + HelpExampleCli("getaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\"")
325 + HelpExampleRpc("getaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\"")
328 LOCK2(cs_main
, pwallet
->cs_wallet
);
330 CTxDestination dest
= DecodeDestination(request
.params
[0].get_str());
331 if (!IsValidDestination(dest
)) {
332 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid Bitcoin address");
335 std::string strAccount
;
336 std::map
<CTxDestination
, CAddressBookData
>::iterator mi
= pwallet
->mapAddressBook
.find(dest
);
337 if (mi
!= pwallet
->mapAddressBook
.end() && !(*mi
).second
.name
.empty()) {
338 strAccount
= (*mi
).second
.name
;
344 UniValue
getaddressesbyaccount(const JSONRPCRequest
& request
)
346 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
347 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
351 if (request
.fHelp
|| request
.params
.size() != 1)
352 throw std::runtime_error(
353 "getaddressesbyaccount \"account\"\n"
354 "\nDEPRECATED. Returns the list of addresses for the given account.\n"
356 "1. \"account\" (string, required) The account name.\n"
358 "[ (json array of string)\n"
359 " \"address\" (string) a bitcoin address associated with the given account\n"
363 + HelpExampleCli("getaddressesbyaccount", "\"tabby\"")
364 + HelpExampleRpc("getaddressesbyaccount", "\"tabby\"")
367 LOCK2(cs_main
, pwallet
->cs_wallet
);
369 std::string strAccount
= AccountFromValue(request
.params
[0]);
371 // Find all addresses that have the given account
372 UniValue
ret(UniValue::VARR
);
373 for (const std::pair
<CTxDestination
, CAddressBookData
>& item
: pwallet
->mapAddressBook
) {
374 const CTxDestination
& dest
= item
.first
;
375 const std::string
& strName
= item
.second
.name
;
376 if (strName
== strAccount
) {
377 ret
.push_back(EncodeDestination(dest
));
383 static void SendMoney(CWallet
* const pwallet
, const CTxDestination
&address
, CAmount nValue
, bool fSubtractFeeFromAmount
, CWalletTx
& wtxNew
, const CCoinControl
& coin_control
)
385 CAmount curBalance
= pwallet
->GetBalance();
389 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid amount");
391 if (nValue
> curBalance
)
392 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS
, "Insufficient funds");
394 if (pwallet
->GetBroadcastTransactions() && !g_connman
) {
395 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED
, "Error: Peer-to-peer functionality missing or disabled");
398 // Parse Bitcoin address
399 CScript scriptPubKey
= GetScriptForDestination(address
);
401 // Create and send the transaction
402 CReserveKey
reservekey(pwallet
);
403 CAmount nFeeRequired
;
404 std::string strError
;
405 std::vector
<CRecipient
> vecSend
;
406 int nChangePosRet
= -1;
407 CRecipient recipient
= {scriptPubKey
, nValue
, fSubtractFeeFromAmount
};
408 vecSend
.push_back(recipient
);
409 if (!pwallet
->CreateTransaction(vecSend
, wtxNew
, reservekey
, nFeeRequired
, nChangePosRet
, strError
, coin_control
)) {
410 if (!fSubtractFeeFromAmount
&& nValue
+ nFeeRequired
> curBalance
)
411 strError
= strprintf("Error: This transaction requires a transaction fee of at least %s", FormatMoney(nFeeRequired
));
412 throw JSONRPCError(RPC_WALLET_ERROR
, strError
);
414 CValidationState state
;
415 if (!pwallet
->CommitTransaction(wtxNew
, reservekey
, g_connman
.get(), state
)) {
416 strError
= strprintf("Error: The transaction was rejected! Reason given: %s", state
.GetRejectReason());
417 throw JSONRPCError(RPC_WALLET_ERROR
, strError
);
421 UniValue
sendtoaddress(const JSONRPCRequest
& request
)
423 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
424 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
428 if (request
.fHelp
|| request
.params
.size() < 2 || request
.params
.size() > 8)
429 throw std::runtime_error(
430 "sendtoaddress \"address\" amount ( \"comment\" \"comment_to\" subtractfeefromamount replaceable conf_target \"estimate_mode\")\n"
431 "\nSend an amount to a given address.\n"
432 + HelpRequiringPassphrase(pwallet
) +
434 "1. \"address\" (string, required) The bitcoin address to send to.\n"
435 "2. \"amount\" (numeric or string, required) The amount in " + CURRENCY_UNIT
+ " to send. eg 0.1\n"
436 "3. \"comment\" (string, optional) A comment used to store what the transaction is for. \n"
437 " This is not part of the transaction, just kept in your wallet.\n"
438 "4. \"comment_to\" (string, optional) A comment to store the name of the person or organization \n"
439 " to which you're sending the transaction. This is not part of the \n"
440 " transaction, just kept in your wallet.\n"
441 "5. subtractfeefromamount (boolean, optional, default=false) The fee will be deducted from the amount being sent.\n"
442 " The recipient will receive less bitcoins than you enter in the amount field.\n"
443 "6. replaceable (boolean, optional) Allow this transaction to be replaced by a transaction with higher fees via BIP 125\n"
444 "7. conf_target (numeric, optional) Confirmation target (in blocks)\n"
445 "8. \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
448 " \"CONSERVATIVE\"\n"
450 "\"txid\" (string) The transaction id.\n"
452 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.1")
453 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.1 \"donation\" \"seans outpost\"")
454 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.1 \"\" \"\" true")
455 + HelpExampleRpc("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\", 0.1, \"donation\", \"seans outpost\"")
460 // Make sure the results are valid at least up to the most recent block
461 // the user could have gotten from another RPC command prior to now
462 pwallet
->BlockUntilSyncedToCurrentChain();
464 LOCK2(cs_main
, pwallet
->cs_wallet
);
466 CTxDestination dest
= DecodeDestination(request
.params
[0].get_str());
467 if (!IsValidDestination(dest
)) {
468 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid address");
472 CAmount nAmount
= AmountFromValue(request
.params
[1]);
474 throw JSONRPCError(RPC_TYPE_ERROR
, "Invalid amount for send");
478 if (!request
.params
[2].isNull() && !request
.params
[2].get_str().empty())
479 wtx
.mapValue
["comment"] = request
.params
[2].get_str();
480 if (!request
.params
[3].isNull() && !request
.params
[3].get_str().empty())
481 wtx
.mapValue
["to"] = request
.params
[3].get_str();
483 bool fSubtractFeeFromAmount
= false;
484 if (!request
.params
[4].isNull()) {
485 fSubtractFeeFromAmount
= request
.params
[4].get_bool();
488 CCoinControl coin_control
;
489 if (!request
.params
[5].isNull()) {
490 coin_control
.signalRbf
= request
.params
[5].get_bool();
493 if (!request
.params
[6].isNull()) {
494 coin_control
.m_confirm_target
= ParseConfirmTarget(request
.params
[6]);
497 if (!request
.params
[7].isNull()) {
498 if (!FeeModeFromString(request
.params
[7].get_str(), coin_control
.m_fee_mode
)) {
499 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid estimate_mode parameter");
504 EnsureWalletIsUnlocked(pwallet
);
506 SendMoney(pwallet
, dest
, nAmount
, fSubtractFeeFromAmount
, wtx
, coin_control
);
508 return wtx
.GetHash().GetHex();
511 UniValue
listaddressgroupings(const JSONRPCRequest
& request
)
513 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
514 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
518 if (request
.fHelp
|| request
.params
.size() != 0)
519 throw std::runtime_error(
520 "listaddressgroupings\n"
521 "\nLists groups of addresses which have had their common ownership\n"
522 "made public by common use as inputs or as the resulting change\n"
523 "in past transactions\n"
528 " \"address\", (string) The bitcoin address\n"
529 " amount, (numeric) The amount in " + CURRENCY_UNIT
+ "\n"
530 " \"account\" (string, optional) DEPRECATED. The account\n"
537 + HelpExampleCli("listaddressgroupings", "")
538 + HelpExampleRpc("listaddressgroupings", "")
543 // Make sure the results are valid at least up to the most recent block
544 // the user could have gotten from another RPC command prior to now
545 pwallet
->BlockUntilSyncedToCurrentChain();
547 LOCK2(cs_main
, pwallet
->cs_wallet
);
549 UniValue
jsonGroupings(UniValue::VARR
);
550 std::map
<CTxDestination
, CAmount
> balances
= pwallet
->GetAddressBalances();
551 for (const std::set
<CTxDestination
>& grouping
: pwallet
->GetAddressGroupings()) {
552 UniValue
jsonGrouping(UniValue::VARR
);
553 for (const CTxDestination
& address
: grouping
)
555 UniValue
addressInfo(UniValue::VARR
);
556 addressInfo
.push_back(EncodeDestination(address
));
557 addressInfo
.push_back(ValueFromAmount(balances
[address
]));
559 if (pwallet
->mapAddressBook
.find(address
) != pwallet
->mapAddressBook
.end()) {
560 addressInfo
.push_back(pwallet
->mapAddressBook
.find(address
)->second
.name
);
563 jsonGrouping
.push_back(addressInfo
);
565 jsonGroupings
.push_back(jsonGrouping
);
567 return jsonGroupings
;
570 UniValue
signmessage(const JSONRPCRequest
& request
)
572 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
573 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
577 if (request
.fHelp
|| request
.params
.size() != 2)
578 throw std::runtime_error(
579 "signmessage \"address\" \"message\"\n"
580 "\nSign a message with the private key of an address"
581 + HelpRequiringPassphrase(pwallet
) + "\n"
583 "1. \"address\" (string, required) The bitcoin address to use for the private key.\n"
584 "2. \"message\" (string, required) The message to create a signature of.\n"
586 "\"signature\" (string) The signature of the message encoded in base 64\n"
588 "\nUnlock the wallet for 30 seconds\n"
589 + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
590 "\nCreate the signature\n"
591 + HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
592 "\nVerify the signature\n"
593 + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
595 + HelpExampleRpc("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"my message\"")
598 LOCK2(cs_main
, pwallet
->cs_wallet
);
600 EnsureWalletIsUnlocked(pwallet
);
602 std::string strAddress
= request
.params
[0].get_str();
603 std::string strMessage
= request
.params
[1].get_str();
605 CTxDestination dest
= DecodeDestination(strAddress
);
606 if (!IsValidDestination(dest
)) {
607 throw JSONRPCError(RPC_TYPE_ERROR
, "Invalid address");
610 const CKeyID
*keyID
= boost::get
<CKeyID
>(&dest
);
612 throw JSONRPCError(RPC_TYPE_ERROR
, "Address does not refer to key");
616 if (!pwallet
->GetKey(*keyID
, key
)) {
617 throw JSONRPCError(RPC_WALLET_ERROR
, "Private key not available");
620 CHashWriter
ss(SER_GETHASH
, 0);
621 ss
<< strMessageMagic
;
624 std::vector
<unsigned char> vchSig
;
625 if (!key
.SignCompact(ss
.GetHash(), vchSig
))
626 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Sign failed");
628 return EncodeBase64(vchSig
.data(), vchSig
.size());
631 UniValue
getreceivedbyaddress(const JSONRPCRequest
& request
)
633 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
634 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
638 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2)
639 throw std::runtime_error(
640 "getreceivedbyaddress \"address\" ( minconf )\n"
641 "\nReturns the total amount received by the given address in transactions with at least minconf confirmations.\n"
643 "1. \"address\" (string, required) The bitcoin address for transactions.\n"
644 "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n"
646 "amount (numeric) The total amount in " + CURRENCY_UNIT
+ " received at this address.\n"
648 "\nThe amount from transactions with at least 1 confirmation\n"
649 + HelpExampleCli("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\"") +
650 "\nThe amount including unconfirmed transactions, zero confirmations\n"
651 + HelpExampleCli("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" 0") +
652 "\nThe amount with at least 6 confirmations\n"
653 + HelpExampleCli("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" 6") +
654 "\nAs a json rpc call\n"
655 + HelpExampleRpc("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", 6")
660 // Make sure the results are valid at least up to the most recent block
661 // the user could have gotten from another RPC command prior to now
662 pwallet
->BlockUntilSyncedToCurrentChain();
664 LOCK2(cs_main
, pwallet
->cs_wallet
);
667 CTxDestination dest
= DecodeDestination(request
.params
[0].get_str());
668 if (!IsValidDestination(dest
)) {
669 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid Bitcoin address");
671 CScript scriptPubKey
= GetScriptForDestination(dest
);
672 if (!IsMine(*pwallet
, scriptPubKey
)) {
673 throw JSONRPCError(RPC_WALLET_ERROR
, "Address not found in wallet");
676 // Minimum confirmations
678 if (!request
.params
[1].isNull())
679 nMinDepth
= request
.params
[1].get_int();
683 for (const std::pair
<uint256
, CWalletTx
>& pairWtx
: pwallet
->mapWallet
) {
684 const CWalletTx
& wtx
= pairWtx
.second
;
685 if (wtx
.IsCoinBase() || !CheckFinalTx(*wtx
.tx
))
688 for (const CTxOut
& txout
: wtx
.tx
->vout
)
689 if (txout
.scriptPubKey
== scriptPubKey
)
690 if (wtx
.GetDepthInMainChain() >= nMinDepth
)
691 nAmount
+= txout
.nValue
;
694 return ValueFromAmount(nAmount
);
698 UniValue
getreceivedbyaccount(const JSONRPCRequest
& request
)
700 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
701 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
705 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2)
706 throw std::runtime_error(
707 "getreceivedbyaccount \"account\" ( minconf )\n"
708 "\nDEPRECATED. Returns the total amount received by addresses with <account> in transactions with at least [minconf] confirmations.\n"
710 "1. \"account\" (string, required) The selected account, may be the default account using \"\".\n"
711 "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n"
713 "amount (numeric) The total amount in " + CURRENCY_UNIT
+ " received for this account.\n"
715 "\nAmount received by the default account with at least 1 confirmation\n"
716 + HelpExampleCli("getreceivedbyaccount", "\"\"") +
717 "\nAmount received at the tabby account including unconfirmed amounts with zero confirmations\n"
718 + HelpExampleCli("getreceivedbyaccount", "\"tabby\" 0") +
719 "\nThe amount with at least 6 confirmations\n"
720 + HelpExampleCli("getreceivedbyaccount", "\"tabby\" 6") +
721 "\nAs a json rpc call\n"
722 + HelpExampleRpc("getreceivedbyaccount", "\"tabby\", 6")
727 // Make sure the results are valid at least up to the most recent block
728 // the user could have gotten from another RPC command prior to now
729 pwallet
->BlockUntilSyncedToCurrentChain();
731 LOCK2(cs_main
, pwallet
->cs_wallet
);
733 // Minimum confirmations
735 if (!request
.params
[1].isNull())
736 nMinDepth
= request
.params
[1].get_int();
738 // Get the set of pub keys assigned to account
739 std::string strAccount
= AccountFromValue(request
.params
[0]);
740 std::set
<CTxDestination
> setAddress
= pwallet
->GetAccountAddresses(strAccount
);
744 for (const std::pair
<uint256
, CWalletTx
>& pairWtx
: pwallet
->mapWallet
) {
745 const CWalletTx
& wtx
= pairWtx
.second
;
746 if (wtx
.IsCoinBase() || !CheckFinalTx(*wtx
.tx
))
749 for (const CTxOut
& txout
: wtx
.tx
->vout
)
751 CTxDestination address
;
752 if (ExtractDestination(txout
.scriptPubKey
, address
) && IsMine(*pwallet
, address
) && setAddress
.count(address
)) {
753 if (wtx
.GetDepthInMainChain() >= nMinDepth
)
754 nAmount
+= txout
.nValue
;
759 return ValueFromAmount(nAmount
);
763 UniValue
getbalance(const JSONRPCRequest
& request
)
765 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
766 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
770 if (request
.fHelp
|| request
.params
.size() > 3)
771 throw std::runtime_error(
772 "getbalance ( \"account\" minconf include_watchonly )\n"
773 "\nIf account is not specified, returns the server's total available balance.\n"
774 "The available balance is what the wallet considers currently spendable, and is\n"
775 "thus affected by options which limit spendability such as -spendzeroconfchange.\n"
776 "If account is specified (DEPRECATED), returns the balance in the account.\n"
777 "Note that the account \"\" is not the same as leaving the parameter out.\n"
778 "The server total may be different to the balance in the default \"\" account.\n"
780 "1. \"account\" (string, optional) DEPRECATED. The account string may be given as a\n"
781 " specific account name to find the balance associated with wallet keys in\n"
782 " a named account, or as the empty string (\"\") to find the balance\n"
783 " associated with wallet keys not in any named account, or as \"*\" to find\n"
784 " the balance associated with all wallet keys regardless of account.\n"
785 " When this option is specified, it calculates the balance in a different\n"
786 " way than when it is not specified, and which can count spends twice when\n"
787 " there are conflicting pending transactions (such as those created by\n"
788 " the bumpfee command), temporarily resulting in low or even negative\n"
789 " balances. In general, account balance calculation is not considered\n"
790 " reliable and has resulted in confusing outcomes, so it is recommended to\n"
791 " avoid passing this argument.\n"
792 "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n"
793 "3. include_watchonly (bool, optional, default=false) Also include balance in watch-only addresses (see 'importaddress')\n"
795 "amount (numeric) The total amount in " + CURRENCY_UNIT
+ " received for this account.\n"
797 "\nThe total amount in the wallet with 1 or more confirmations\n"
798 + HelpExampleCli("getbalance", "") +
799 "\nThe total amount in the wallet at least 6 blocks confirmed\n"
800 + HelpExampleCli("getbalance", "\"*\" 6") +
801 "\nAs a json rpc call\n"
802 + HelpExampleRpc("getbalance", "\"*\", 6")
807 // Make sure the results are valid at least up to the most recent block
808 // the user could have gotten from another RPC command prior to now
809 pwallet
->BlockUntilSyncedToCurrentChain();
811 LOCK2(cs_main
, pwallet
->cs_wallet
);
813 const UniValue
& account_value
= request
.params
[0];
814 const UniValue
& minconf
= request
.params
[1];
815 const UniValue
& include_watchonly
= request
.params
[2];
817 if (account_value
.isNull()) {
818 if (!minconf
.isNull()) {
819 throw JSONRPCError(RPC_INVALID_PARAMETER
,
820 "getbalance minconf option is only currently supported if an account is specified");
822 if (!include_watchonly
.isNull()) {
823 throw JSONRPCError(RPC_INVALID_PARAMETER
,
824 "getbalance include_watchonly option is only currently supported if an account is specified");
826 return ValueFromAmount(pwallet
->GetBalance());
829 const std::string
& account_param
= account_value
.get_str();
830 const std::string
* account
= account_param
!= "*" ? &account_param
: nullptr;
833 if (!minconf
.isNull())
834 nMinDepth
= minconf
.get_int();
835 isminefilter filter
= ISMINE_SPENDABLE
;
836 if(!include_watchonly
.isNull())
837 if(include_watchonly
.get_bool())
838 filter
= filter
| ISMINE_WATCH_ONLY
;
840 return ValueFromAmount(pwallet
->GetLegacyBalance(filter
, nMinDepth
, account
));
843 UniValue
getunconfirmedbalance(const JSONRPCRequest
&request
)
845 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
846 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
850 if (request
.fHelp
|| request
.params
.size() > 0)
851 throw std::runtime_error(
852 "getunconfirmedbalance\n"
853 "Returns the server's total unconfirmed balance\n");
857 // Make sure the results are valid at least up to the most recent block
858 // the user could have gotten from another RPC command prior to now
859 pwallet
->BlockUntilSyncedToCurrentChain();
861 LOCK2(cs_main
, pwallet
->cs_wallet
);
863 return ValueFromAmount(pwallet
->GetUnconfirmedBalance());
867 UniValue
movecmd(const JSONRPCRequest
& request
)
869 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
870 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
874 if (request
.fHelp
|| request
.params
.size() < 3 || request
.params
.size() > 5)
875 throw std::runtime_error(
876 "move \"fromaccount\" \"toaccount\" amount ( minconf \"comment\" )\n"
877 "\nDEPRECATED. Move a specified amount from one account in your wallet to another.\n"
879 "1. \"fromaccount\" (string, required) The name of the account to move funds from. May be the default account using \"\".\n"
880 "2. \"toaccount\" (string, required) The name of the account to move funds to. May be the default account using \"\".\n"
881 "3. amount (numeric) Quantity of " + CURRENCY_UNIT
+ " to move between accounts.\n"
882 "4. (dummy) (numeric, optional) Ignored. Remains for backward compatibility.\n"
883 "5. \"comment\" (string, optional) An optional comment, stored in the wallet only.\n"
885 "true|false (boolean) true if successful.\n"
887 "\nMove 0.01 " + CURRENCY_UNIT
+ " from the default account to the account named tabby\n"
888 + HelpExampleCli("move", "\"\" \"tabby\" 0.01") +
889 "\nMove 0.01 " + CURRENCY_UNIT
+ " timotei to akiko with a comment and funds have 6 confirmations\n"
890 + HelpExampleCli("move", "\"timotei\" \"akiko\" 0.01 6 \"happy birthday!\"") +
891 "\nAs a json rpc call\n"
892 + HelpExampleRpc("move", "\"timotei\", \"akiko\", 0.01, 6, \"happy birthday!\"")
896 LOCK2(cs_main
, pwallet
->cs_wallet
);
898 std::string strFrom
= AccountFromValue(request
.params
[0]);
899 std::string strTo
= AccountFromValue(request
.params
[1]);
900 CAmount nAmount
= AmountFromValue(request
.params
[2]);
902 throw JSONRPCError(RPC_TYPE_ERROR
, "Invalid amount for send");
903 if (!request
.params
[3].isNull())
904 // unused parameter, used to be nMinDepth, keep type-checking it though
905 (void)request
.params
[3].get_int();
906 std::string strComment
;
907 if (!request
.params
[4].isNull())
908 strComment
= request
.params
[4].get_str();
910 if (!pwallet
->AccountMove(strFrom
, strTo
, nAmount
, strComment
)) {
911 throw JSONRPCError(RPC_DATABASE_ERROR
, "database error");
918 UniValue
sendfrom(const JSONRPCRequest
& request
)
920 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
921 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
925 if (request
.fHelp
|| request
.params
.size() < 3 || request
.params
.size() > 6)
926 throw std::runtime_error(
927 "sendfrom \"fromaccount\" \"toaddress\" amount ( minconf \"comment\" \"comment_to\" )\n"
928 "\nDEPRECATED (use sendtoaddress). Sent an amount from an account to a bitcoin address."
929 + HelpRequiringPassphrase(pwallet
) + "\n"
931 "1. \"fromaccount\" (string, required) The name of the account to send funds from. May be the default account using \"\".\n"
932 " Specifying an account does not influence coin selection, but it does associate the newly created\n"
933 " transaction with the account, so the account's balance computation and transaction history can reflect\n"
935 "2. \"toaddress\" (string, required) The bitcoin address to send funds to.\n"
936 "3. amount (numeric or string, required) The amount in " + CURRENCY_UNIT
+ " (transaction fee is added on top).\n"
937 "4. minconf (numeric, optional, default=1) Only use funds with at least this many confirmations.\n"
938 "5. \"comment\" (string, optional) A comment used to store what the transaction is for. \n"
939 " This is not part of the transaction, just kept in your wallet.\n"
940 "6. \"comment_to\" (string, optional) An optional comment to store the name of the person or organization \n"
941 " to which you're sending the transaction. This is not part of the transaction, \n"
942 " it is just kept in your wallet.\n"
944 "\"txid\" (string) The transaction id.\n"
946 "\nSend 0.01 " + CURRENCY_UNIT
+ " from the default account to the address, must have at least 1 confirmation\n"
947 + HelpExampleCli("sendfrom", "\"\" \"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.01") +
948 "\nSend 0.01 from the tabby account to the given address, funds must have at least 6 confirmations\n"
949 + HelpExampleCli("sendfrom", "\"tabby\" \"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.01 6 \"donation\" \"seans outpost\"") +
950 "\nAs a json rpc call\n"
951 + HelpExampleRpc("sendfrom", "\"tabby\", \"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\", 0.01, 6, \"donation\", \"seans outpost\"")
956 // Make sure the results are valid at least up to the most recent block
957 // the user could have gotten from another RPC command prior to now
958 pwallet
->BlockUntilSyncedToCurrentChain();
960 LOCK2(cs_main
, pwallet
->cs_wallet
);
962 std::string strAccount
= AccountFromValue(request
.params
[0]);
963 CTxDestination dest
= DecodeDestination(request
.params
[1].get_str());
964 if (!IsValidDestination(dest
)) {
965 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid Bitcoin address");
967 CAmount nAmount
= AmountFromValue(request
.params
[2]);
969 throw JSONRPCError(RPC_TYPE_ERROR
, "Invalid amount for send");
971 if (!request
.params
[3].isNull())
972 nMinDepth
= request
.params
[3].get_int();
975 wtx
.strFromAccount
= strAccount
;
976 if (!request
.params
[4].isNull() && !request
.params
[4].get_str().empty())
977 wtx
.mapValue
["comment"] = request
.params
[4].get_str();
978 if (!request
.params
[5].isNull() && !request
.params
[5].get_str().empty())
979 wtx
.mapValue
["to"] = request
.params
[5].get_str();
981 EnsureWalletIsUnlocked(pwallet
);
984 CAmount nBalance
= pwallet
->GetLegacyBalance(ISMINE_SPENDABLE
, nMinDepth
, &strAccount
);
985 if (nAmount
> nBalance
)
986 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS
, "Account has insufficient funds");
988 CCoinControl no_coin_control
; // This is a deprecated API
989 SendMoney(pwallet
, dest
, nAmount
, false, wtx
, no_coin_control
);
991 return wtx
.GetHash().GetHex();
995 UniValue
sendmany(const JSONRPCRequest
& request
)
997 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
998 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1002 if (request
.fHelp
|| request
.params
.size() < 2 || request
.params
.size() > 8)
1003 throw std::runtime_error(
1004 "sendmany \"fromaccount\" {\"address\":amount,...} ( minconf \"comment\" [\"address\",...] replaceable conf_target \"estimate_mode\")\n"
1005 "\nSend multiple times. Amounts are double-precision floating point numbers."
1006 + HelpRequiringPassphrase(pwallet
) + "\n"
1008 "1. \"fromaccount\" (string, required) DEPRECATED. The account to send the funds from. Should be \"\" for the default account\n"
1009 "2. \"amounts\" (string, required) A json object with addresses and amounts\n"
1011 " \"address\":amount (numeric or string) The bitcoin address is the key, the numeric amount (can be string) in " + CURRENCY_UNIT
+ " is the value\n"
1014 "3. minconf (numeric, optional, default=1) Only use the balance confirmed at least this many times.\n"
1015 "4. \"comment\" (string, optional) A comment\n"
1016 "5. subtractfeefrom (array, optional) A json array with addresses.\n"
1017 " The fee will be equally deducted from the amount of each selected address.\n"
1018 " Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n"
1019 " If no addresses are specified here, the sender pays the fee.\n"
1021 " \"address\" (string) Subtract fee from this address\n"
1024 "6. replaceable (boolean, optional) Allow this transaction to be replaced by a transaction with higher fees via BIP 125\n"
1025 "7. conf_target (numeric, optional) Confirmation target (in blocks)\n"
1026 "8. \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
1029 " \"CONSERVATIVE\"\n"
1031 "\"txid\" (string) The transaction id for the send. Only 1 transaction is created regardless of \n"
1032 " the number of addresses.\n"
1034 "\nSend two amounts to two different addresses:\n"
1035 + HelpExampleCli("sendmany", "\"\" \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\"") +
1036 "\nSend two amounts to two different addresses setting the confirmation and comment:\n"
1037 + HelpExampleCli("sendmany", "\"\" \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\" 6 \"testing\"") +
1038 "\nSend two amounts to two different addresses, subtract fee from amount:\n"
1039 + HelpExampleCli("sendmany", "\"\" \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\" 1 \"\" \"[\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\",\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\"]\"") +
1040 "\nAs a json rpc call\n"
1041 + HelpExampleRpc("sendmany", "\"\", {\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\":0.01,\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\":0.02}, 6, \"testing\"")
1046 // Make sure the results are valid at least up to the most recent block
1047 // the user could have gotten from another RPC command prior to now
1048 pwallet
->BlockUntilSyncedToCurrentChain();
1050 LOCK2(cs_main
, pwallet
->cs_wallet
);
1052 if (pwallet
->GetBroadcastTransactions() && !g_connman
) {
1053 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED
, "Error: Peer-to-peer functionality missing or disabled");
1056 std::string strAccount
= AccountFromValue(request
.params
[0]);
1057 UniValue sendTo
= request
.params
[1].get_obj();
1059 if (!request
.params
[2].isNull())
1060 nMinDepth
= request
.params
[2].get_int();
1063 wtx
.strFromAccount
= strAccount
;
1064 if (!request
.params
[3].isNull() && !request
.params
[3].get_str().empty())
1065 wtx
.mapValue
["comment"] = request
.params
[3].get_str();
1067 UniValue
subtractFeeFromAmount(UniValue::VARR
);
1068 if (!request
.params
[4].isNull())
1069 subtractFeeFromAmount
= request
.params
[4].get_array();
1071 CCoinControl coin_control
;
1072 if (!request
.params
[5].isNull()) {
1073 coin_control
.signalRbf
= request
.params
[5].get_bool();
1076 if (!request
.params
[6].isNull()) {
1077 coin_control
.m_confirm_target
= ParseConfirmTarget(request
.params
[6]);
1080 if (!request
.params
[7].isNull()) {
1081 if (!FeeModeFromString(request
.params
[7].get_str(), coin_control
.m_fee_mode
)) {
1082 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid estimate_mode parameter");
1086 std::set
<CTxDestination
> destinations
;
1087 std::vector
<CRecipient
> vecSend
;
1089 CAmount totalAmount
= 0;
1090 std::vector
<std::string
> keys
= sendTo
.getKeys();
1091 for (const std::string
& name_
: keys
) {
1092 CTxDestination dest
= DecodeDestination(name_
);
1093 if (!IsValidDestination(dest
)) {
1094 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, std::string("Invalid Bitcoin address: ") + name_
);
1097 if (destinations
.count(dest
)) {
1098 throw JSONRPCError(RPC_INVALID_PARAMETER
, std::string("Invalid parameter, duplicated address: ") + name_
);
1100 destinations
.insert(dest
);
1102 CScript scriptPubKey
= GetScriptForDestination(dest
);
1103 CAmount nAmount
= AmountFromValue(sendTo
[name_
]);
1105 throw JSONRPCError(RPC_TYPE_ERROR
, "Invalid amount for send");
1106 totalAmount
+= nAmount
;
1108 bool fSubtractFeeFromAmount
= false;
1109 for (unsigned int idx
= 0; idx
< subtractFeeFromAmount
.size(); idx
++) {
1110 const UniValue
& addr
= subtractFeeFromAmount
[idx
];
1111 if (addr
.get_str() == name_
)
1112 fSubtractFeeFromAmount
= true;
1115 CRecipient recipient
= {scriptPubKey
, nAmount
, fSubtractFeeFromAmount
};
1116 vecSend
.push_back(recipient
);
1119 EnsureWalletIsUnlocked(pwallet
);
1122 CAmount nBalance
= pwallet
->GetLegacyBalance(ISMINE_SPENDABLE
, nMinDepth
, &strAccount
);
1123 if (totalAmount
> nBalance
)
1124 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS
, "Account has insufficient funds");
1127 CReserveKey
keyChange(pwallet
);
1128 CAmount nFeeRequired
= 0;
1129 int nChangePosRet
= -1;
1130 std::string strFailReason
;
1131 bool fCreated
= pwallet
->CreateTransaction(vecSend
, wtx
, keyChange
, nFeeRequired
, nChangePosRet
, strFailReason
, coin_control
);
1133 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS
, strFailReason
);
1134 CValidationState state
;
1135 if (!pwallet
->CommitTransaction(wtx
, keyChange
, g_connman
.get(), state
)) {
1136 strFailReason
= strprintf("Transaction commit failed:: %s", state
.GetRejectReason());
1137 throw JSONRPCError(RPC_WALLET_ERROR
, strFailReason
);
1140 return wtx
.GetHash().GetHex();
1143 // Defined in rpc/misc.cpp
1144 extern CScript
_createmultisig_redeemScript(CWallet
* const pwallet
, const UniValue
& params
);
1146 UniValue
addmultisigaddress(const JSONRPCRequest
& request
)
1148 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1149 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1150 return NullUniValue
;
1153 if (request
.fHelp
|| request
.params
.size() < 2 || request
.params
.size() > 3)
1155 std::string msg
= "addmultisigaddress nrequired [\"key\",...] ( \"account\" )\n"
1156 "\nAdd a nrequired-to-sign multisignature address to the wallet. Requires a new wallet backup.\n"
1157 "Each key is a Bitcoin address or hex-encoded public key.\n"
1158 "This functionality is only intended for use with non-watchonly addresses.\n"
1159 "See `importaddress` for watchonly p2sh address support.\n"
1160 "If 'account' is specified (DEPRECATED), assign address to that account.\n"
1163 "1. nrequired (numeric, required) The number of required signatures out of the n keys or addresses.\n"
1164 "2. \"keys\" (string, required) A json array of bitcoin addresses or hex-encoded public keys\n"
1166 " \"address\" (string) bitcoin address or hex-encoded public key\n"
1169 "3. \"account\" (string, optional) DEPRECATED. An account to assign the addresses to.\n"
1172 "\"address\" (string) A bitcoin address associated with the keys.\n"
1175 "\nAdd a multisig address from 2 addresses\n"
1176 + HelpExampleCli("addmultisigaddress", "2 \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"") +
1177 "\nAs json rpc call\n"
1178 + HelpExampleRpc("addmultisigaddress", "2, \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"")
1180 throw std::runtime_error(msg
);
1183 LOCK2(cs_main
, pwallet
->cs_wallet
);
1185 std::string strAccount
;
1186 if (!request
.params
[2].isNull())
1187 strAccount
= AccountFromValue(request
.params
[2]);
1189 // Construct using pay-to-script-hash:
1190 CScript inner
= _createmultisig_redeemScript(pwallet
, request
.params
);
1191 CScriptID
innerID(inner
);
1192 pwallet
->AddCScript(inner
);
1194 pwallet
->SetAddressBook(innerID
, strAccount
, "send");
1195 return EncodeDestination(innerID
);
1198 class Witnessifier
: public boost::static_visitor
<bool>
1201 CWallet
* const pwallet
;
1202 CTxDestination result
;
1203 bool already_witness
;
1205 explicit Witnessifier(CWallet
*_pwallet
) : pwallet(_pwallet
), already_witness(false) {}
1207 bool operator()(const CKeyID
&keyID
) {
1209 CScript basescript
= GetScriptForDestination(keyID
);
1210 CScript witscript
= GetScriptForWitness(basescript
);
1212 // This check is to make sure that the script we created can actually be solved for and signed by us
1213 // if we were to have the private keys. This is just to make sure that the script is valid and that,
1214 // if found in a transaction, we would still accept and relay that transaction.
1215 if (!ProduceSignature(DummySignatureCreator(pwallet
), witscript
, sigs
) ||
1216 !VerifyScript(sigs
.scriptSig
, witscript
, &sigs
.scriptWitness
, MANDATORY_SCRIPT_VERIFY_FLAGS
| SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
, DummySignatureCreator(pwallet
).Checker())) {
1219 return ExtractDestination(witscript
, result
);
1224 bool operator()(const CScriptID
&scriptID
) {
1226 if (pwallet
&& pwallet
->GetCScript(scriptID
, subscript
)) {
1228 std::vector
<unsigned char> witprog
;
1229 if (subscript
.IsWitnessProgram(witnessversion
, witprog
)) {
1230 ExtractDestination(subscript
, result
);
1231 already_witness
= true;
1234 CScript witscript
= GetScriptForWitness(subscript
);
1236 // This check is to make sure that the script we created can actually be solved for and signed by us
1237 // if we were to have the private keys. This is just to make sure that the script is valid and that,
1238 // if found in a transaction, we would still accept and relay that transaction.
1239 if (!ProduceSignature(DummySignatureCreator(pwallet
), witscript
, sigs
) ||
1240 !VerifyScript(sigs
.scriptSig
, witscript
, &sigs
.scriptWitness
, MANDATORY_SCRIPT_VERIFY_FLAGS
| SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
, DummySignatureCreator(pwallet
).Checker())) {
1243 return ExtractDestination(witscript
, result
);
1248 bool operator()(const WitnessV0KeyHash
& id
)
1250 already_witness
= true;
1255 bool operator()(const WitnessV0ScriptHash
& id
)
1257 already_witness
= true;
1262 template<typename T
>
1263 bool operator()(const T
& dest
) { return false; }
1266 UniValue
addwitnessaddress(const JSONRPCRequest
& request
)
1268 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1269 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1270 return NullUniValue
;
1273 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2)
1275 std::string msg
= "addwitnessaddress \"address\" ( p2sh )\n"
1276 "\nAdd a witness address for a script (with pubkey or redeemscript known). Requires a new wallet backup.\n"
1277 "It returns the witness script.\n"
1280 "1. \"address\" (string, required) An address known to the wallet\n"
1281 "2. p2sh (bool, optional, default=true) Embed inside P2SH\n"
1284 "\"witnessaddress\", (string) The value of the new address (P2SH or BIP173).\n"
1287 throw std::runtime_error(msg
);
1292 if (!IsWitnessEnabled(chainActive
.Tip(), Params().GetConsensus()) && !gArgs
.GetBoolArg("-walletprematurewitness", false)) {
1293 throw JSONRPCError(RPC_WALLET_ERROR
, "Segregated witness not enabled on network");
1297 CTxDestination dest
= DecodeDestination(request
.params
[0].get_str());
1298 if (!IsValidDestination(dest
)) {
1299 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid Bitcoin address");
1303 if (!request
.params
[1].isNull()) {
1304 p2sh
= request
.params
[1].get_bool();
1307 Witnessifier
w(pwallet
);
1308 bool ret
= boost::apply_visitor(w
, dest
);
1310 throw JSONRPCError(RPC_WALLET_ERROR
, "Public key or redeemscript not known to wallet, or the key is uncompressed");
1313 CScript witprogram
= GetScriptForDestination(w
.result
);
1316 w
.result
= CScriptID(witprogram
);
1319 if (w
.already_witness
) {
1320 if (!(dest
== w
.result
)) {
1321 throw JSONRPCError(RPC_WALLET_ERROR
, "Cannot convert between witness address types");
1324 pwallet
->AddCScript(witprogram
);
1325 pwallet
->SetAddressBook(w
.result
, "", "receive");
1328 return EncodeDestination(w
.result
);
1335 std::vector
<uint256
> txids
;
1340 nConf
= std::numeric_limits
<int>::max();
1341 fIsWatchonly
= false;
1345 UniValue
ListReceived(CWallet
* const pwallet
, const UniValue
& params
, bool fByAccounts
)
1347 // Minimum confirmations
1349 if (!params
[0].isNull())
1350 nMinDepth
= params
[0].get_int();
1352 // Whether to include empty accounts
1353 bool fIncludeEmpty
= false;
1354 if (!params
[1].isNull())
1355 fIncludeEmpty
= params
[1].get_bool();
1357 isminefilter filter
= ISMINE_SPENDABLE
;
1358 if(!params
[2].isNull())
1359 if(params
[2].get_bool())
1360 filter
= filter
| ISMINE_WATCH_ONLY
;
1363 std::map
<CTxDestination
, tallyitem
> mapTally
;
1364 for (const std::pair
<uint256
, CWalletTx
>& pairWtx
: pwallet
->mapWallet
) {
1365 const CWalletTx
& wtx
= pairWtx
.second
;
1367 if (wtx
.IsCoinBase() || !CheckFinalTx(*wtx
.tx
))
1370 int nDepth
= wtx
.GetDepthInMainChain();
1371 if (nDepth
< nMinDepth
)
1374 for (const CTxOut
& txout
: wtx
.tx
->vout
)
1376 CTxDestination address
;
1377 if (!ExtractDestination(txout
.scriptPubKey
, address
))
1380 isminefilter mine
= IsMine(*pwallet
, address
);
1381 if(!(mine
& filter
))
1384 tallyitem
& item
= mapTally
[address
];
1385 item
.nAmount
+= txout
.nValue
;
1386 item
.nConf
= std::min(item
.nConf
, nDepth
);
1387 item
.txids
.push_back(wtx
.GetHash());
1388 if (mine
& ISMINE_WATCH_ONLY
)
1389 item
.fIsWatchonly
= true;
1394 UniValue
ret(UniValue::VARR
);
1395 std::map
<std::string
, tallyitem
> mapAccountTally
;
1396 for (const std::pair
<CTxDestination
, CAddressBookData
>& item
: pwallet
->mapAddressBook
) {
1397 const CTxDestination
& dest
= item
.first
;
1398 const std::string
& strAccount
= item
.second
.name
;
1399 std::map
<CTxDestination
, tallyitem
>::iterator it
= mapTally
.find(dest
);
1400 if (it
== mapTally
.end() && !fIncludeEmpty
)
1403 CAmount nAmount
= 0;
1404 int nConf
= std::numeric_limits
<int>::max();
1405 bool fIsWatchonly
= false;
1406 if (it
!= mapTally
.end())
1408 nAmount
= (*it
).second
.nAmount
;
1409 nConf
= (*it
).second
.nConf
;
1410 fIsWatchonly
= (*it
).second
.fIsWatchonly
;
1415 tallyitem
& _item
= mapAccountTally
[strAccount
];
1416 _item
.nAmount
+= nAmount
;
1417 _item
.nConf
= std::min(_item
.nConf
, nConf
);
1418 _item
.fIsWatchonly
= fIsWatchonly
;
1422 UniValue
obj(UniValue::VOBJ
);
1424 obj
.push_back(Pair("involvesWatchonly", true));
1425 obj
.push_back(Pair("address", EncodeDestination(dest
)));
1426 obj
.push_back(Pair("account", strAccount
));
1427 obj
.push_back(Pair("amount", ValueFromAmount(nAmount
)));
1428 obj
.push_back(Pair("confirmations", (nConf
== std::numeric_limits
<int>::max() ? 0 : nConf
)));
1430 obj
.push_back(Pair("label", strAccount
));
1431 UniValue
transactions(UniValue::VARR
);
1432 if (it
!= mapTally
.end())
1434 for (const uint256
& _item
: (*it
).second
.txids
)
1436 transactions
.push_back(_item
.GetHex());
1439 obj
.push_back(Pair("txids", transactions
));
1446 for (const auto& entry
: mapAccountTally
)
1448 CAmount nAmount
= entry
.second
.nAmount
;
1449 int nConf
= entry
.second
.nConf
;
1450 UniValue
obj(UniValue::VOBJ
);
1451 if (entry
.second
.fIsWatchonly
)
1452 obj
.push_back(Pair("involvesWatchonly", true));
1453 obj
.push_back(Pair("account", entry
.first
));
1454 obj
.push_back(Pair("amount", ValueFromAmount(nAmount
)));
1455 obj
.push_back(Pair("confirmations", (nConf
== std::numeric_limits
<int>::max() ? 0 : nConf
)));
1463 UniValue
listreceivedbyaddress(const JSONRPCRequest
& request
)
1465 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1466 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1467 return NullUniValue
;
1470 if (request
.fHelp
|| request
.params
.size() > 3)
1471 throw std::runtime_error(
1472 "listreceivedbyaddress ( minconf include_empty include_watchonly)\n"
1473 "\nList balances by receiving address.\n"
1475 "1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included.\n"
1476 "2. include_empty (bool, optional, default=false) Whether to include addresses that haven't received any payments.\n"
1477 "3. include_watchonly (bool, optional, default=false) Whether to include watch-only addresses (see 'importaddress').\n"
1482 " \"involvesWatchonly\" : true, (bool) Only returned if imported addresses were involved in transaction\n"
1483 " \"address\" : \"receivingaddress\", (string) The receiving address\n"
1484 " \"account\" : \"accountname\", (string) DEPRECATED. The account of the receiving address. The default account is \"\".\n"
1485 " \"amount\" : x.xxx, (numeric) The total amount in " + CURRENCY_UNIT
+ " received by the address\n"
1486 " \"confirmations\" : n, (numeric) The number of confirmations of the most recent transaction included\n"
1487 " \"label\" : \"label\", (string) A comment for the address/transaction, if any\n"
1489 " n, (numeric) The ids of transactions received with the address \n"
1497 + HelpExampleCli("listreceivedbyaddress", "")
1498 + HelpExampleCli("listreceivedbyaddress", "6 true")
1499 + HelpExampleRpc("listreceivedbyaddress", "6, true, true")
1504 // Make sure the results are valid at least up to the most recent block
1505 // the user could have gotten from another RPC command prior to now
1506 pwallet
->BlockUntilSyncedToCurrentChain();
1508 LOCK2(cs_main
, pwallet
->cs_wallet
);
1510 return ListReceived(pwallet
, request
.params
, false);
1513 UniValue
listreceivedbyaccount(const JSONRPCRequest
& request
)
1515 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1516 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1517 return NullUniValue
;
1520 if (request
.fHelp
|| request
.params
.size() > 3)
1521 throw std::runtime_error(
1522 "listreceivedbyaccount ( minconf include_empty include_watchonly)\n"
1523 "\nDEPRECATED. List balances by account.\n"
1525 "1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included.\n"
1526 "2. include_empty (bool, optional, default=false) Whether to include accounts that haven't received any payments.\n"
1527 "3. include_watchonly (bool, optional, default=false) Whether to include watch-only addresses (see 'importaddress').\n"
1532 " \"involvesWatchonly\" : true, (bool) Only returned if imported addresses were involved in transaction\n"
1533 " \"account\" : \"accountname\", (string) The account name of the receiving account\n"
1534 " \"amount\" : x.xxx, (numeric) The total amount received by addresses with this account\n"
1535 " \"confirmations\" : n, (numeric) The number of confirmations of the most recent transaction included\n"
1536 " \"label\" : \"label\" (string) A comment for the address/transaction, if any\n"
1542 + HelpExampleCli("listreceivedbyaccount", "")
1543 + HelpExampleCli("listreceivedbyaccount", "6 true")
1544 + HelpExampleRpc("listreceivedbyaccount", "6, true, true")
1549 // Make sure the results are valid at least up to the most recent block
1550 // the user could have gotten from another RPC command prior to now
1551 pwallet
->BlockUntilSyncedToCurrentChain();
1553 LOCK2(cs_main
, pwallet
->cs_wallet
);
1555 return ListReceived(pwallet
, request
.params
, true);
1558 static void MaybePushAddress(UniValue
& entry
, const CTxDestination
&dest
)
1560 if (IsValidDestination(dest
)) {
1561 entry
.push_back(Pair("address", EncodeDestination(dest
)));
1566 * List transactions based on the given criteria.
1568 * @param pwallet The wallet.
1569 * @param wtx The wallet transaction.
1570 * @param strAccount The account, if any, or "*" for all.
1571 * @param nMinDepth The minimum confirmation depth.
1572 * @param fLong Whether to include the JSON version of the transaction.
1573 * @param ret The UniValue into which the result is stored.
1574 * @param filter The "is mine" filter bool.
1576 void ListTransactions(CWallet
* const pwallet
, const CWalletTx
& wtx
, const std::string
& strAccount
, int nMinDepth
, bool fLong
, UniValue
& ret
, const isminefilter
& filter
)
1579 std::string strSentAccount
;
1580 std::list
<COutputEntry
> listReceived
;
1581 std::list
<COutputEntry
> listSent
;
1583 wtx
.GetAmounts(listReceived
, listSent
, nFee
, strSentAccount
, filter
);
1585 bool fAllAccounts
= (strAccount
== std::string("*"));
1586 bool involvesWatchonly
= wtx
.IsFromMe(ISMINE_WATCH_ONLY
);
1589 if ((!listSent
.empty() || nFee
!= 0) && (fAllAccounts
|| strAccount
== strSentAccount
))
1591 for (const COutputEntry
& s
: listSent
)
1593 UniValue
entry(UniValue::VOBJ
);
1594 if (involvesWatchonly
|| (::IsMine(*pwallet
, s
.destination
) & ISMINE_WATCH_ONLY
)) {
1595 entry
.push_back(Pair("involvesWatchonly", true));
1597 entry
.push_back(Pair("account", strSentAccount
));
1598 MaybePushAddress(entry
, s
.destination
);
1599 entry
.push_back(Pair("category", "send"));
1600 entry
.push_back(Pair("amount", ValueFromAmount(-s
.amount
)));
1601 if (pwallet
->mapAddressBook
.count(s
.destination
)) {
1602 entry
.push_back(Pair("label", pwallet
->mapAddressBook
[s
.destination
].name
));
1604 entry
.push_back(Pair("vout", s
.vout
));
1605 entry
.push_back(Pair("fee", ValueFromAmount(-nFee
)));
1607 WalletTxToJSON(wtx
, entry
);
1608 entry
.push_back(Pair("abandoned", wtx
.isAbandoned()));
1609 ret
.push_back(entry
);
1614 if (listReceived
.size() > 0 && wtx
.GetDepthInMainChain() >= nMinDepth
)
1616 for (const COutputEntry
& r
: listReceived
)
1618 std::string account
;
1619 if (pwallet
->mapAddressBook
.count(r
.destination
)) {
1620 account
= pwallet
->mapAddressBook
[r
.destination
].name
;
1622 if (fAllAccounts
|| (account
== strAccount
))
1624 UniValue
entry(UniValue::VOBJ
);
1625 if (involvesWatchonly
|| (::IsMine(*pwallet
, r
.destination
) & ISMINE_WATCH_ONLY
)) {
1626 entry
.push_back(Pair("involvesWatchonly", true));
1628 entry
.push_back(Pair("account", account
));
1629 MaybePushAddress(entry
, r
.destination
);
1630 if (wtx
.IsCoinBase())
1632 if (wtx
.GetDepthInMainChain() < 1)
1633 entry
.push_back(Pair("category", "orphan"));
1634 else if (wtx
.GetBlocksToMaturity() > 0)
1635 entry
.push_back(Pair("category", "immature"));
1637 entry
.push_back(Pair("category", "generate"));
1641 entry
.push_back(Pair("category", "receive"));
1643 entry
.push_back(Pair("amount", ValueFromAmount(r
.amount
)));
1644 if (pwallet
->mapAddressBook
.count(r
.destination
)) {
1645 entry
.push_back(Pair("label", account
));
1647 entry
.push_back(Pair("vout", r
.vout
));
1649 WalletTxToJSON(wtx
, entry
);
1650 ret
.push_back(entry
);
1656 void AcentryToJSON(const CAccountingEntry
& acentry
, const std::string
& strAccount
, UniValue
& ret
)
1658 bool fAllAccounts
= (strAccount
== std::string("*"));
1660 if (fAllAccounts
|| acentry
.strAccount
== strAccount
)
1662 UniValue
entry(UniValue::VOBJ
);
1663 entry
.push_back(Pair("account", acentry
.strAccount
));
1664 entry
.push_back(Pair("category", "move"));
1665 entry
.push_back(Pair("time", acentry
.nTime
));
1666 entry
.push_back(Pair("amount", ValueFromAmount(acentry
.nCreditDebit
)));
1667 entry
.push_back(Pair("otheraccount", acentry
.strOtherAccount
));
1668 entry
.push_back(Pair("comment", acentry
.strComment
));
1669 ret
.push_back(entry
);
1673 UniValue
listtransactions(const JSONRPCRequest
& request
)
1675 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1676 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1677 return NullUniValue
;
1680 if (request
.fHelp
|| request
.params
.size() > 4)
1681 throw std::runtime_error(
1682 "listtransactions ( \"account\" count skip include_watchonly)\n"
1683 "\nReturns up to 'count' most recent transactions skipping the first 'from' transactions for account 'account'.\n"
1685 "1. \"account\" (string, optional) DEPRECATED. The account name. Should be \"*\".\n"
1686 "2. count (numeric, optional, default=10) The number of transactions to return\n"
1687 "3. skip (numeric, optional, default=0) The number of transactions to skip\n"
1688 "4. include_watchonly (bool, optional, default=false) Include transactions to watch-only addresses (see 'importaddress')\n"
1692 " \"account\":\"accountname\", (string) DEPRECATED. The account name associated with the transaction. \n"
1693 " It will be \"\" for the default account.\n"
1694 " \"address\":\"address\", (string) The bitcoin address of the transaction. Not present for \n"
1695 " move transactions (category = move).\n"
1696 " \"category\":\"send|receive|move\", (string) The transaction category. 'move' is a local (off blockchain)\n"
1697 " transaction between accounts, and not associated with an address,\n"
1698 " transaction id or block. 'send' and 'receive' transactions are \n"
1699 " associated with an address, transaction id and block details\n"
1700 " \"amount\": x.xxx, (numeric) The amount in " + CURRENCY_UNIT
+ ". This is negative for the 'send' category, and for the\n"
1701 " 'move' category for moves outbound. It is positive for the 'receive' category,\n"
1702 " and for the 'move' category for inbound funds.\n"
1703 " \"label\": \"label\", (string) A comment for the address/transaction, if any\n"
1704 " \"vout\": n, (numeric) the vout value\n"
1705 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT
+ ". This is negative and only available for the \n"
1706 " 'send' category of transactions.\n"
1707 " \"confirmations\": n, (numeric) The number of confirmations for the transaction. Available for 'send' and \n"
1708 " 'receive' category of transactions. Negative confirmations indicate the\n"
1709 " transaction conflicts with the block chain\n"
1710 " \"trusted\": xxx, (bool) Whether we consider the outputs of this unconfirmed transaction safe to spend.\n"
1711 " \"blockhash\": \"hashvalue\", (string) The block hash containing the transaction. Available for 'send' and 'receive'\n"
1712 " category of transactions.\n"
1713 " \"blockindex\": n, (numeric) The index of the transaction in the block that includes it. Available for 'send' and 'receive'\n"
1714 " category of transactions.\n"
1715 " \"blocktime\": xxx, (numeric) The block time in seconds since epoch (1 Jan 1970 GMT).\n"
1716 " \"txid\": \"transactionid\", (string) The transaction id. Available for 'send' and 'receive' category of transactions.\n"
1717 " \"time\": xxx, (numeric) The transaction time in seconds since epoch (midnight Jan 1 1970 GMT).\n"
1718 " \"timereceived\": xxx, (numeric) The time received in seconds since epoch (midnight Jan 1 1970 GMT). Available \n"
1719 " for 'send' and 'receive' category of transactions.\n"
1720 " \"comment\": \"...\", (string) If a comment is associated with the transaction.\n"
1721 " \"otheraccount\": \"accountname\", (string) DEPRECATED. For the 'move' category of transactions, the account the funds came \n"
1722 " from (for receiving funds, positive amounts), or went to (for sending funds,\n"
1723 " negative amounts).\n"
1724 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1725 " may be unknown for unconfirmed transactions not in the mempool\n"
1726 " \"abandoned\": xxx (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
1727 " 'send' category of transactions.\n"
1732 "\nList the most recent 10 transactions in the systems\n"
1733 + HelpExampleCli("listtransactions", "") +
1734 "\nList transactions 100 to 120\n"
1735 + HelpExampleCli("listtransactions", "\"*\" 20 100") +
1736 "\nAs a json rpc call\n"
1737 + HelpExampleRpc("listtransactions", "\"*\", 20, 100")
1742 // Make sure the results are valid at least up to the most recent block
1743 // the user could have gotten from another RPC command prior to now
1744 pwallet
->BlockUntilSyncedToCurrentChain();
1746 LOCK2(cs_main
, pwallet
->cs_wallet
);
1748 std::string strAccount
= "*";
1749 if (!request
.params
[0].isNull())
1750 strAccount
= request
.params
[0].get_str();
1752 if (!request
.params
[1].isNull())
1753 nCount
= request
.params
[1].get_int();
1755 if (!request
.params
[2].isNull())
1756 nFrom
= request
.params
[2].get_int();
1757 isminefilter filter
= ISMINE_SPENDABLE
;
1758 if(!request
.params
[3].isNull())
1759 if(request
.params
[3].get_bool())
1760 filter
= filter
| ISMINE_WATCH_ONLY
;
1763 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Negative count");
1765 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Negative from");
1767 UniValue
ret(UniValue::VARR
);
1769 const CWallet::TxItems
& txOrdered
= pwallet
->wtxOrdered
;
1771 // iterate backwards until we have nCount items to return:
1772 for (CWallet::TxItems::const_reverse_iterator it
= txOrdered
.rbegin(); it
!= txOrdered
.rend(); ++it
)
1774 CWalletTx
*const pwtx
= (*it
).second
.first
;
1775 if (pwtx
!= nullptr)
1776 ListTransactions(pwallet
, *pwtx
, strAccount
, 0, true, ret
, filter
);
1777 CAccountingEntry
*const pacentry
= (*it
).second
.second
;
1778 if (pacentry
!= nullptr)
1779 AcentryToJSON(*pacentry
, strAccount
, ret
);
1781 if ((int)ret
.size() >= (nCount
+nFrom
)) break;
1783 // ret is newest to oldest
1785 if (nFrom
> (int)ret
.size())
1787 if ((nFrom
+ nCount
) > (int)ret
.size())
1788 nCount
= ret
.size() - nFrom
;
1790 std::vector
<UniValue
> arrTmp
= ret
.getValues();
1792 std::vector
<UniValue
>::iterator first
= arrTmp
.begin();
1793 std::advance(first
, nFrom
);
1794 std::vector
<UniValue
>::iterator last
= arrTmp
.begin();
1795 std::advance(last
, nFrom
+nCount
);
1797 if (last
!= arrTmp
.end()) arrTmp
.erase(last
, arrTmp
.end());
1798 if (first
!= arrTmp
.begin()) arrTmp
.erase(arrTmp
.begin(), first
);
1800 std::reverse(arrTmp
.begin(), arrTmp
.end()); // Return oldest to newest
1804 ret
.push_backV(arrTmp
);
1809 UniValue
listaccounts(const JSONRPCRequest
& request
)
1811 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1812 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1813 return NullUniValue
;
1816 if (request
.fHelp
|| request
.params
.size() > 2)
1817 throw std::runtime_error(
1818 "listaccounts ( minconf include_watchonly)\n"
1819 "\nDEPRECATED. Returns Object that has account names as keys, account balances as values.\n"
1821 "1. minconf (numeric, optional, default=1) Only include transactions with at least this many confirmations\n"
1822 "2. include_watchonly (bool, optional, default=false) Include balances in watch-only addresses (see 'importaddress')\n"
1824 "{ (json object where keys are account names, and values are numeric balances\n"
1825 " \"account\": x.xxx, (numeric) The property name is the account name, and the value is the total balance for the account.\n"
1829 "\nList account balances where there at least 1 confirmation\n"
1830 + HelpExampleCli("listaccounts", "") +
1831 "\nList account balances including zero confirmation transactions\n"
1832 + HelpExampleCli("listaccounts", "0") +
1833 "\nList account balances for 6 or more confirmations\n"
1834 + HelpExampleCli("listaccounts", "6") +
1835 "\nAs json rpc call\n"
1836 + HelpExampleRpc("listaccounts", "6")
1841 // Make sure the results are valid at least up to the most recent block
1842 // the user could have gotten from another RPC command prior to now
1843 pwallet
->BlockUntilSyncedToCurrentChain();
1845 LOCK2(cs_main
, pwallet
->cs_wallet
);
1848 if (!request
.params
[0].isNull())
1849 nMinDepth
= request
.params
[0].get_int();
1850 isminefilter includeWatchonly
= ISMINE_SPENDABLE
;
1851 if(!request
.params
[1].isNull())
1852 if(request
.params
[1].get_bool())
1853 includeWatchonly
= includeWatchonly
| ISMINE_WATCH_ONLY
;
1855 std::map
<std::string
, CAmount
> mapAccountBalances
;
1856 for (const std::pair
<CTxDestination
, CAddressBookData
>& entry
: pwallet
->mapAddressBook
) {
1857 if (IsMine(*pwallet
, entry
.first
) & includeWatchonly
) { // This address belongs to me
1858 mapAccountBalances
[entry
.second
.name
] = 0;
1862 for (const std::pair
<uint256
, CWalletTx
>& pairWtx
: pwallet
->mapWallet
) {
1863 const CWalletTx
& wtx
= pairWtx
.second
;
1865 std::string strSentAccount
;
1866 std::list
<COutputEntry
> listReceived
;
1867 std::list
<COutputEntry
> listSent
;
1868 int nDepth
= wtx
.GetDepthInMainChain();
1869 if (wtx
.GetBlocksToMaturity() > 0 || nDepth
< 0)
1871 wtx
.GetAmounts(listReceived
, listSent
, nFee
, strSentAccount
, includeWatchonly
);
1872 mapAccountBalances
[strSentAccount
] -= nFee
;
1873 for (const COutputEntry
& s
: listSent
)
1874 mapAccountBalances
[strSentAccount
] -= s
.amount
;
1875 if (nDepth
>= nMinDepth
)
1877 for (const COutputEntry
& r
: listReceived
)
1878 if (pwallet
->mapAddressBook
.count(r
.destination
)) {
1879 mapAccountBalances
[pwallet
->mapAddressBook
[r
.destination
].name
] += r
.amount
;
1882 mapAccountBalances
[""] += r
.amount
;
1886 const std::list
<CAccountingEntry
>& acentries
= pwallet
->laccentries
;
1887 for (const CAccountingEntry
& entry
: acentries
)
1888 mapAccountBalances
[entry
.strAccount
] += entry
.nCreditDebit
;
1890 UniValue
ret(UniValue::VOBJ
);
1891 for (const std::pair
<std::string
, CAmount
>& accountBalance
: mapAccountBalances
) {
1892 ret
.push_back(Pair(accountBalance
.first
, ValueFromAmount(accountBalance
.second
)));
1897 UniValue
listsinceblock(const JSONRPCRequest
& request
)
1899 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1900 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1901 return NullUniValue
;
1904 if (request
.fHelp
|| request
.params
.size() > 4)
1905 throw std::runtime_error(
1906 "listsinceblock ( \"blockhash\" target_confirmations include_watchonly include_removed )\n"
1907 "\nGet all transactions in blocks since block [blockhash], or all transactions if omitted.\n"
1908 "If \"blockhash\" is no longer a part of the main chain, transactions from the fork point onward are included.\n"
1909 "Additionally, if include_removed is set, transactions affecting the wallet which were removed are returned in the \"removed\" array.\n"
1911 "1. \"blockhash\" (string, optional) The block hash to list transactions since\n"
1912 "2. target_confirmations: (numeric, optional, default=1) Return the nth block hash from the main chain. e.g. 1 would mean the best block hash. Note: this is not used as a filter, but only affects [lastblock] in the return value\n"
1913 "3. include_watchonly: (bool, optional, default=false) Include transactions to watch-only addresses (see 'importaddress')\n"
1914 "4. include_removed: (bool, optional, default=true) Show transactions that were removed due to a reorg in the \"removed\" array\n"
1915 " (not guaranteed to work on pruned nodes)\n"
1918 " \"transactions\": [\n"
1919 " \"account\":\"accountname\", (string) DEPRECATED. The account name associated with the transaction. Will be \"\" for the default account.\n"
1920 " \"address\":\"address\", (string) The bitcoin address of the transaction. Not present for move transactions (category = move).\n"
1921 " \"category\":\"send|receive\", (string) The transaction category. 'send' has negative amounts, 'receive' has positive amounts.\n"
1922 " \"amount\": x.xxx, (numeric) The amount in " + CURRENCY_UNIT
+ ". This is negative for the 'send' category, and for the 'move' category for moves \n"
1923 " outbound. It is positive for the 'receive' category, and for the 'move' category for inbound funds.\n"
1924 " \"vout\" : n, (numeric) the vout value\n"
1925 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT
+ ". This is negative and only available for the 'send' category of transactions.\n"
1926 " \"confirmations\": n, (numeric) The number of confirmations for the transaction. Available for 'send' and 'receive' category of transactions.\n"
1927 " When it's < 0, it means the transaction conflicted that many blocks ago.\n"
1928 " \"blockhash\": \"hashvalue\", (string) The block hash containing the transaction. Available for 'send' and 'receive' category of transactions.\n"
1929 " \"blockindex\": n, (numeric) The index of the transaction in the block that includes it. Available for 'send' and 'receive' category of transactions.\n"
1930 " \"blocktime\": xxx, (numeric) The block time in seconds since epoch (1 Jan 1970 GMT).\n"
1931 " \"txid\": \"transactionid\", (string) The transaction id. Available for 'send' and 'receive' category of transactions.\n"
1932 " \"time\": xxx, (numeric) The transaction time in seconds since epoch (Jan 1 1970 GMT).\n"
1933 " \"timereceived\": xxx, (numeric) The time received in seconds since epoch (Jan 1 1970 GMT). Available for 'send' and 'receive' category of transactions.\n"
1934 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1935 " may be unknown for unconfirmed transactions not in the mempool\n"
1936 " \"abandoned\": xxx, (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the 'send' category of transactions.\n"
1937 " \"comment\": \"...\", (string) If a comment is associated with the transaction.\n"
1938 " \"label\" : \"label\" (string) A comment for the address/transaction, if any\n"
1939 " \"to\": \"...\", (string) If a comment to is associated with the transaction.\n"
1942 " <structure is the same as \"transactions\" above, only present if include_removed=true>\n"
1943 " 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"
1945 " \"lastblock\": \"lastblockhash\" (string) The hash of the block (target_confirmations-1) from the best block on the main chain. This is typically used to feed back into listsinceblock the next time you call it. So you would generally use a target_confirmations of say 6, so you will be continually re-notified of transactions until they've reached 6 confirmations plus any new ones\n"
1948 + HelpExampleCli("listsinceblock", "")
1949 + HelpExampleCli("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\" 6")
1950 + HelpExampleRpc("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\", 6")
1955 // Make sure the results are valid at least up to the most recent block
1956 // the user could have gotten from another RPC command prior to now
1957 pwallet
->BlockUntilSyncedToCurrentChain();
1959 LOCK2(cs_main
, pwallet
->cs_wallet
);
1961 const CBlockIndex
* pindex
= nullptr; // Block index of the specified block or the common ancestor, if the block provided was in a deactivated chain.
1962 const CBlockIndex
* paltindex
= nullptr; // Block index of the specified block, even if it's in a deactivated chain.
1963 int target_confirms
= 1;
1964 isminefilter filter
= ISMINE_SPENDABLE
;
1966 if (!request
.params
[0].isNull() && !request
.params
[0].get_str().empty()) {
1969 blockId
.SetHex(request
.params
[0].get_str());
1970 BlockMap::iterator it
= mapBlockIndex
.find(blockId
);
1971 if (it
== mapBlockIndex
.end()) {
1972 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Block not found");
1974 paltindex
= pindex
= it
->second
;
1975 if (chainActive
[pindex
->nHeight
] != pindex
) {
1976 // the block being asked for is a part of a deactivated chain;
1977 // we don't want to depend on its perceived height in the block
1978 // chain, we want to instead use the last common ancestor
1979 pindex
= chainActive
.FindFork(pindex
);
1983 if (!request
.params
[1].isNull()) {
1984 target_confirms
= request
.params
[1].get_int();
1986 if (target_confirms
< 1) {
1987 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter");
1991 if (!request
.params
[2].isNull() && request
.params
[2].get_bool()) {
1992 filter
= filter
| ISMINE_WATCH_ONLY
;
1995 bool include_removed
= (request
.params
[3].isNull() || request
.params
[3].get_bool());
1997 int depth
= pindex
? (1 + chainActive
.Height() - pindex
->nHeight
) : -1;
1999 UniValue
transactions(UniValue::VARR
);
2001 for (const std::pair
<uint256
, CWalletTx
>& pairWtx
: pwallet
->mapWallet
) {
2002 CWalletTx tx
= pairWtx
.second
;
2004 if (depth
== -1 || tx
.GetDepthInMainChain() < depth
) {
2005 ListTransactions(pwallet
, tx
, "*", 0, true, transactions
, filter
);
2009 // when a reorg'd block is requested, we also list any relevant transactions
2010 // in the blocks of the chain that was detached
2011 UniValue
removed(UniValue::VARR
);
2012 while (include_removed
&& paltindex
&& paltindex
!= pindex
) {
2014 if (!ReadBlockFromDisk(block
, paltindex
, Params().GetConsensus())) {
2015 throw JSONRPCError(RPC_INTERNAL_ERROR
, "Can't read block from disk");
2017 for (const CTransactionRef
& tx
: block
.vtx
) {
2018 auto it
= pwallet
->mapWallet
.find(tx
->GetHash());
2019 if (it
!= pwallet
->mapWallet
.end()) {
2020 // We want all transactions regardless of confirmation count to appear here,
2021 // even negative confirmation ones, hence the big negative.
2022 ListTransactions(pwallet
, it
->second
, "*", -100000000, true, removed
, filter
);
2025 paltindex
= paltindex
->pprev
;
2028 CBlockIndex
*pblockLast
= chainActive
[chainActive
.Height() + 1 - target_confirms
];
2029 uint256 lastblock
= pblockLast
? pblockLast
->GetBlockHash() : uint256();
2031 UniValue
ret(UniValue::VOBJ
);
2032 ret
.push_back(Pair("transactions", transactions
));
2033 if (include_removed
) ret
.push_back(Pair("removed", removed
));
2034 ret
.push_back(Pair("lastblock", lastblock
.GetHex()));
2039 UniValue
gettransaction(const JSONRPCRequest
& request
)
2041 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2042 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2043 return NullUniValue
;
2046 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2)
2047 throw std::runtime_error(
2048 "gettransaction \"txid\" ( include_watchonly )\n"
2049 "\nGet detailed information about in-wallet transaction <txid>\n"
2051 "1. \"txid\" (string, required) The transaction id\n"
2052 "2. \"include_watchonly\" (bool, optional, default=false) Whether to include watch-only addresses in balance calculation and details[]\n"
2055 " \"amount\" : x.xxx, (numeric) The transaction amount in " + CURRENCY_UNIT
+ "\n"
2056 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT
+ ". This is negative and only available for the \n"
2057 " 'send' category of transactions.\n"
2058 " \"confirmations\" : n, (numeric) The number of confirmations\n"
2059 " \"blockhash\" : \"hash\", (string) The block hash\n"
2060 " \"blockindex\" : xx, (numeric) The index of the transaction in the block that includes it\n"
2061 " \"blocktime\" : ttt, (numeric) The time in seconds since epoch (1 Jan 1970 GMT)\n"
2062 " \"txid\" : \"transactionid\", (string) The transaction id.\n"
2063 " \"time\" : ttt, (numeric) The transaction time in seconds since epoch (1 Jan 1970 GMT)\n"
2064 " \"timereceived\" : ttt, (numeric) The time received in seconds since epoch (1 Jan 1970 GMT)\n"
2065 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
2066 " may be unknown for unconfirmed transactions not in the mempool\n"
2067 " \"details\" : [\n"
2069 " \"account\" : \"accountname\", (string) DEPRECATED. The account name involved in the transaction, can be \"\" for the default account.\n"
2070 " \"address\" : \"address\", (string) The bitcoin address involved in the transaction\n"
2071 " \"category\" : \"send|receive\", (string) The category, either 'send' or 'receive'\n"
2072 " \"amount\" : x.xxx, (numeric) The amount in " + CURRENCY_UNIT
+ "\n"
2073 " \"label\" : \"label\", (string) A comment for the address/transaction, if any\n"
2074 " \"vout\" : n, (numeric) the vout value\n"
2075 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT
+ ". This is negative and only available for the \n"
2076 " 'send' category of transactions.\n"
2077 " \"abandoned\": xxx (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
2078 " 'send' category of transactions.\n"
2082 " \"hex\" : \"data\" (string) Raw data for transaction\n"
2086 + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
2087 + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\" true")
2088 + HelpExampleRpc("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
2093 // Make sure the results are valid at least up to the most recent block
2094 // the user could have gotten from another RPC command prior to now
2095 pwallet
->BlockUntilSyncedToCurrentChain();
2097 LOCK2(cs_main
, pwallet
->cs_wallet
);
2100 hash
.SetHex(request
.params
[0].get_str());
2102 isminefilter filter
= ISMINE_SPENDABLE
;
2103 if(!request
.params
[1].isNull())
2104 if(request
.params
[1].get_bool())
2105 filter
= filter
| ISMINE_WATCH_ONLY
;
2107 UniValue
entry(UniValue::VOBJ
);
2108 auto it
= pwallet
->mapWallet
.find(hash
);
2109 if (it
== pwallet
->mapWallet
.end()) {
2110 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid or non-wallet transaction id");
2112 const CWalletTx
& wtx
= it
->second
;
2114 CAmount nCredit
= wtx
.GetCredit(filter
);
2115 CAmount nDebit
= wtx
.GetDebit(filter
);
2116 CAmount nNet
= nCredit
- nDebit
;
2117 CAmount nFee
= (wtx
.IsFromMe(filter
) ? wtx
.tx
->GetValueOut() - nDebit
: 0);
2119 entry
.push_back(Pair("amount", ValueFromAmount(nNet
- nFee
)));
2120 if (wtx
.IsFromMe(filter
))
2121 entry
.push_back(Pair("fee", ValueFromAmount(nFee
)));
2123 WalletTxToJSON(wtx
, entry
);
2125 UniValue
details(UniValue::VARR
);
2126 ListTransactions(pwallet
, wtx
, "*", 0, false, details
, filter
);
2127 entry
.push_back(Pair("details", details
));
2129 std::string strHex
= EncodeHexTx(*wtx
.tx
, RPCSerializationFlags());
2130 entry
.push_back(Pair("hex", strHex
));
2135 UniValue
abandontransaction(const JSONRPCRequest
& request
)
2137 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2138 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2139 return NullUniValue
;
2142 if (request
.fHelp
|| request
.params
.size() != 1)
2143 throw std::runtime_error(
2144 "abandontransaction \"txid\"\n"
2145 "\nMark in-wallet transaction <txid> as abandoned\n"
2146 "This will mark this transaction and all its in-wallet descendants as abandoned which will allow\n"
2147 "for their inputs to be respent. It can be used to replace \"stuck\" or evicted transactions.\n"
2148 "It only works on transactions which are not included in a block and are not currently in the mempool.\n"
2149 "It has no effect on transactions which are already conflicted or abandoned.\n"
2151 "1. \"txid\" (string, required) The transaction id\n"
2154 + HelpExampleCli("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
2155 + HelpExampleRpc("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
2160 // Make sure the results are valid at least up to the most recent block
2161 // the user could have gotten from another RPC command prior to now
2162 pwallet
->BlockUntilSyncedToCurrentChain();
2164 LOCK2(cs_main
, pwallet
->cs_wallet
);
2167 hash
.SetHex(request
.params
[0].get_str());
2169 if (!pwallet
->mapWallet
.count(hash
)) {
2170 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid or non-wallet transaction id");
2172 if (!pwallet
->AbandonTransaction(hash
)) {
2173 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Transaction not eligible for abandonment");
2176 return NullUniValue
;
2180 UniValue
backupwallet(const JSONRPCRequest
& request
)
2182 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2183 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2184 return NullUniValue
;
2187 if (request
.fHelp
|| request
.params
.size() != 1)
2188 throw std::runtime_error(
2189 "backupwallet \"destination\"\n"
2190 "\nSafely copies current wallet file to destination, which can be a directory or a path with filename.\n"
2192 "1. \"destination\" (string) The destination directory or file\n"
2194 + HelpExampleCli("backupwallet", "\"backup.dat\"")
2195 + HelpExampleRpc("backupwallet", "\"backup.dat\"")
2198 // Make sure the results are valid at least up to the most recent block
2199 // the user could have gotten from another RPC command prior to now
2200 pwallet
->BlockUntilSyncedToCurrentChain();
2202 LOCK2(cs_main
, pwallet
->cs_wallet
);
2204 std::string strDest
= request
.params
[0].get_str();
2205 if (!pwallet
->BackupWallet(strDest
)) {
2206 throw JSONRPCError(RPC_WALLET_ERROR
, "Error: Wallet backup failed!");
2209 return NullUniValue
;
2213 UniValue
keypoolrefill(const JSONRPCRequest
& request
)
2215 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2216 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2217 return NullUniValue
;
2220 if (request
.fHelp
|| request
.params
.size() > 1)
2221 throw std::runtime_error(
2222 "keypoolrefill ( newsize )\n"
2223 "\nFills the keypool."
2224 + HelpRequiringPassphrase(pwallet
) + "\n"
2226 "1. newsize (numeric, optional, default=100) The new keypool size\n"
2228 + HelpExampleCli("keypoolrefill", "")
2229 + HelpExampleRpc("keypoolrefill", "")
2232 LOCK2(cs_main
, pwallet
->cs_wallet
);
2234 // 0 is interpreted by TopUpKeyPool() as the default keypool size given by -keypool
2235 unsigned int kpSize
= 0;
2236 if (!request
.params
[0].isNull()) {
2237 if (request
.params
[0].get_int() < 0)
2238 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter, expected valid size.");
2239 kpSize
= (unsigned int)request
.params
[0].get_int();
2242 EnsureWalletIsUnlocked(pwallet
);
2243 pwallet
->TopUpKeyPool(kpSize
);
2245 if (pwallet
->GetKeyPoolSize() < kpSize
) {
2246 throw JSONRPCError(RPC_WALLET_ERROR
, "Error refreshing keypool.");
2249 return NullUniValue
;
2253 static void LockWallet(CWallet
* pWallet
)
2255 LOCK(pWallet
->cs_wallet
);
2256 pWallet
->nRelockTime
= 0;
2260 UniValue
walletpassphrase(const JSONRPCRequest
& request
)
2262 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2263 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2264 return NullUniValue
;
2267 if (request
.fHelp
|| request
.params
.size() != 2) {
2268 throw std::runtime_error(
2269 "walletpassphrase \"passphrase\" timeout\n"
2270 "\nStores the wallet decryption key in memory for 'timeout' seconds.\n"
2271 "This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
2273 "1. \"passphrase\" (string, required) The wallet passphrase\n"
2274 "2. timeout (numeric, required) The time to keep the decryption key in seconds.\n"
2276 "Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
2277 "time that overrides the old one.\n"
2279 "\nUnlock the wallet for 60 seconds\n"
2280 + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 60") +
2281 "\nLock the wallet again (before 60 seconds)\n"
2282 + HelpExampleCli("walletlock", "") +
2283 "\nAs json rpc call\n"
2284 + HelpExampleRpc("walletpassphrase", "\"my pass phrase\", 60")
2288 LOCK2(cs_main
, pwallet
->cs_wallet
);
2292 if (!pwallet
->IsCrypted()) {
2293 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE
, "Error: running with an unencrypted wallet, but walletpassphrase was called.");
2296 // Note that the walletpassphrase is stored in request.params[0] which is not mlock()ed
2297 SecureString strWalletPass
;
2298 strWalletPass
.reserve(100);
2299 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
2300 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2301 strWalletPass
= request
.params
[0].get_str().c_str();
2303 if (strWalletPass
.length() > 0)
2305 if (!pwallet
->Unlock(strWalletPass
)) {
2306 throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT
, "Error: The wallet passphrase entered was incorrect.");
2310 throw std::runtime_error(
2311 "walletpassphrase <passphrase> <timeout>\n"
2312 "Stores the wallet decryption key in memory for <timeout> seconds.");
2314 pwallet
->TopUpKeyPool();
2316 int64_t nSleepTime
= request
.params
[1].get_int64();
2317 pwallet
->nRelockTime
= GetTime() + nSleepTime
;
2318 RPCRunLater(strprintf("lockwallet(%s)", pwallet
->GetName()), boost::bind(LockWallet
, pwallet
), nSleepTime
);
2320 return NullUniValue
;
2324 UniValue
walletpassphrasechange(const JSONRPCRequest
& request
)
2326 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2327 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2328 return NullUniValue
;
2331 if (request
.fHelp
|| request
.params
.size() != 2) {
2332 throw std::runtime_error(
2333 "walletpassphrasechange \"oldpassphrase\" \"newpassphrase\"\n"
2334 "\nChanges the wallet passphrase from 'oldpassphrase' to 'newpassphrase'.\n"
2336 "1. \"oldpassphrase\" (string) The current passphrase\n"
2337 "2. \"newpassphrase\" (string) The new passphrase\n"
2339 + HelpExampleCli("walletpassphrasechange", "\"old one\" \"new one\"")
2340 + HelpExampleRpc("walletpassphrasechange", "\"old one\", \"new one\"")
2344 LOCK2(cs_main
, pwallet
->cs_wallet
);
2348 if (!pwallet
->IsCrypted()) {
2349 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE
, "Error: running with an unencrypted wallet, but walletpassphrasechange was called.");
2352 // TODO: get rid of these .c_str() calls by implementing SecureString::operator=(std::string)
2353 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2354 SecureString strOldWalletPass
;
2355 strOldWalletPass
.reserve(100);
2356 strOldWalletPass
= request
.params
[0].get_str().c_str();
2358 SecureString strNewWalletPass
;
2359 strNewWalletPass
.reserve(100);
2360 strNewWalletPass
= request
.params
[1].get_str().c_str();
2362 if (strOldWalletPass
.length() < 1 || strNewWalletPass
.length() < 1)
2363 throw std::runtime_error(
2364 "walletpassphrasechange <oldpassphrase> <newpassphrase>\n"
2365 "Changes the wallet passphrase from <oldpassphrase> to <newpassphrase>.");
2367 if (!pwallet
->ChangeWalletPassphrase(strOldWalletPass
, strNewWalletPass
)) {
2368 throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT
, "Error: The wallet passphrase entered was incorrect.");
2371 return NullUniValue
;
2375 UniValue
walletlock(const JSONRPCRequest
& request
)
2377 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2378 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2379 return NullUniValue
;
2382 if (request
.fHelp
|| request
.params
.size() != 0) {
2383 throw std::runtime_error(
2385 "\nRemoves the wallet encryption key from memory, locking the wallet.\n"
2386 "After calling this method, you will need to call walletpassphrase again\n"
2387 "before being able to call any methods which require the wallet to be unlocked.\n"
2389 "\nSet the passphrase for 2 minutes to perform a transaction\n"
2390 + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 120") +
2391 "\nPerform a send (requires passphrase set)\n"
2392 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 1.0") +
2393 "\nClear the passphrase since we are done before 2 minutes is up\n"
2394 + HelpExampleCli("walletlock", "") +
2395 "\nAs json rpc call\n"
2396 + HelpExampleRpc("walletlock", "")
2400 LOCK2(cs_main
, pwallet
->cs_wallet
);
2404 if (!pwallet
->IsCrypted()) {
2405 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE
, "Error: running with an unencrypted wallet, but walletlock was called.");
2409 pwallet
->nRelockTime
= 0;
2411 return NullUniValue
;
2415 UniValue
encryptwallet(const JSONRPCRequest
& request
)
2417 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2418 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2419 return NullUniValue
;
2422 if (request
.fHelp
|| request
.params
.size() != 1) {
2423 throw std::runtime_error(
2424 "encryptwallet \"passphrase\"\n"
2425 "\nEncrypts the wallet with 'passphrase'. This is for first time encryption.\n"
2426 "After this, any calls that interact with private keys such as sending or signing \n"
2427 "will require the passphrase to be set prior the making these calls.\n"
2428 "Use the walletpassphrase call for this, and then walletlock call.\n"
2429 "If the wallet is already encrypted, use the walletpassphrasechange call.\n"
2430 "Note that this will shutdown the server.\n"
2432 "1. \"passphrase\" (string) The pass phrase to encrypt the wallet with. It must be at least 1 character, but should be long.\n"
2434 "\nEncrypt your wallet\n"
2435 + HelpExampleCli("encryptwallet", "\"my pass phrase\"") +
2436 "\nNow set the passphrase to use the wallet, such as for signing or sending bitcoin\n"
2437 + HelpExampleCli("walletpassphrase", "\"my pass phrase\"") +
2438 "\nNow we can do something like sign\n"
2439 + HelpExampleCli("signmessage", "\"address\" \"test message\"") +
2440 "\nNow lock the wallet again by removing the passphrase\n"
2441 + HelpExampleCli("walletlock", "") +
2442 "\nAs a json rpc call\n"
2443 + HelpExampleRpc("encryptwallet", "\"my pass phrase\"")
2447 LOCK2(cs_main
, pwallet
->cs_wallet
);
2451 if (pwallet
->IsCrypted()) {
2452 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE
, "Error: running with an encrypted wallet, but encryptwallet was called.");
2455 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
2456 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2457 SecureString strWalletPass
;
2458 strWalletPass
.reserve(100);
2459 strWalletPass
= request
.params
[0].get_str().c_str();
2461 if (strWalletPass
.length() < 1)
2462 throw std::runtime_error(
2463 "encryptwallet <passphrase>\n"
2464 "Encrypts the wallet with <passphrase>.");
2466 if (!pwallet
->EncryptWallet(strWalletPass
)) {
2467 throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED
, "Error: Failed to encrypt the wallet.");
2470 // BDB seems to have a bad habit of writing old data into
2471 // slack space in .dat files; that is bad if the old data is
2472 // unencrypted private keys. So:
2474 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.";
2477 UniValue
lockunspent(const JSONRPCRequest
& request
)
2479 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2480 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2481 return NullUniValue
;
2484 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2)
2485 throw std::runtime_error(
2486 "lockunspent unlock ([{\"txid\":\"txid\",\"vout\":n},...])\n"
2487 "\nUpdates list of temporarily unspendable outputs.\n"
2488 "Temporarily lock (unlock=false) or unlock (unlock=true) specified transaction outputs.\n"
2489 "If no transaction outputs are specified when unlocking then all current locked transaction outputs are unlocked.\n"
2490 "A locked transaction output will not be chosen by automatic coin selection, when spending bitcoins.\n"
2491 "Locks are stored in memory only. Nodes start with zero locked outputs, and the locked output list\n"
2492 "is always cleared (by virtue of process exit) when a node stops or fails.\n"
2493 "Also see the listunspent call\n"
2495 "1. unlock (boolean, required) Whether to unlock (true) or lock (false) the specified transactions\n"
2496 "2. \"transactions\" (string, optional) A json array of objects. Each object the txid (string) vout (numeric)\n"
2497 " [ (json array of json objects)\n"
2499 " \"txid\":\"id\", (string) The transaction id\n"
2500 " \"vout\": n (numeric) The output number\n"
2506 "true|false (boolean) Whether the command was successful or not\n"
2509 "\nList the unspent transactions\n"
2510 + HelpExampleCli("listunspent", "") +
2511 "\nLock an unspent transaction\n"
2512 + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2513 "\nList the locked transactions\n"
2514 + HelpExampleCli("listlockunspent", "") +
2515 "\nUnlock the transaction again\n"
2516 + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2517 "\nAs a json rpc call\n"
2518 + HelpExampleRpc("lockunspent", "false, \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"")
2521 // Make sure the results are valid at least up to the most recent block
2522 // the user could have gotten from another RPC command prior to now
2523 pwallet
->BlockUntilSyncedToCurrentChain();
2525 LOCK2(cs_main
, pwallet
->cs_wallet
);
2527 RPCTypeCheckArgument(request
.params
[0], UniValue::VBOOL
);
2529 bool fUnlock
= request
.params
[0].get_bool();
2531 if (request
.params
[1].isNull()) {
2533 pwallet
->UnlockAllCoins();
2537 RPCTypeCheckArgument(request
.params
[1], UniValue::VARR
);
2539 const UniValue
& output_params
= request
.params
[1];
2541 // Create and validate the COutPoints first.
2543 std::vector
<COutPoint
> outputs
;
2544 outputs
.reserve(output_params
.size());
2546 for (unsigned int idx
= 0; idx
< output_params
.size(); idx
++) {
2547 const UniValue
& o
= output_params
[idx
].get_obj();
2551 {"txid", UniValueType(UniValue::VSTR
)},
2552 {"vout", UniValueType(UniValue::VNUM
)},
2555 const std::string
& txid
= find_value(o
, "txid").get_str();
2557 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter, expected hex txid");
2560 const int nOutput
= find_value(o
, "vout").get_int();
2562 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter, vout must be positive");
2565 const COutPoint
outpt(uint256S(txid
), nOutput
);
2567 const auto it
= pwallet
->mapWallet
.find(outpt
.hash
);
2568 if (it
== pwallet
->mapWallet
.end()) {
2569 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter, unknown transaction");
2572 const CWalletTx
& trans
= it
->second
;
2574 if (outpt
.n
>= trans
.tx
->vout
.size()) {
2575 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter, vout index out of bounds");
2578 if (pwallet
->IsSpent(outpt
.hash
, outpt
.n
)) {
2579 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter, expected unspent output");
2582 const bool is_locked
= pwallet
->IsLockedCoin(outpt
.hash
, outpt
.n
);
2584 if (fUnlock
&& !is_locked
) {
2585 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter, expected locked output");
2588 if (!fUnlock
&& is_locked
) {
2589 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter, output already locked");
2592 outputs
.push_back(outpt
);
2595 // Atomically set (un)locked status for the outputs.
2596 for (const COutPoint
& outpt
: outputs
) {
2597 if (fUnlock
) pwallet
->UnlockCoin(outpt
);
2598 else pwallet
->LockCoin(outpt
);
2604 UniValue
listlockunspent(const JSONRPCRequest
& request
)
2606 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2607 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2608 return NullUniValue
;
2611 if (request
.fHelp
|| request
.params
.size() > 0)
2612 throw std::runtime_error(
2614 "\nReturns list of temporarily unspendable outputs.\n"
2615 "See the lockunspent call to lock and unlock transactions for spending.\n"
2619 " \"txid\" : \"transactionid\", (string) The transaction id locked\n"
2620 " \"vout\" : n (numeric) The vout value\n"
2625 "\nList the unspent transactions\n"
2626 + HelpExampleCli("listunspent", "") +
2627 "\nLock an unspent transaction\n"
2628 + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2629 "\nList the locked transactions\n"
2630 + HelpExampleCli("listlockunspent", "") +
2631 "\nUnlock the transaction again\n"
2632 + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2633 "\nAs a json rpc call\n"
2634 + HelpExampleRpc("listlockunspent", "")
2638 LOCK2(cs_main
, pwallet
->cs_wallet
);
2640 std::vector
<COutPoint
> vOutpts
;
2641 pwallet
->ListLockedCoins(vOutpts
);
2643 UniValue
ret(UniValue::VARR
);
2645 for (COutPoint
&outpt
: vOutpts
) {
2646 UniValue
o(UniValue::VOBJ
);
2648 o
.push_back(Pair("txid", outpt
.hash
.GetHex()));
2649 o
.push_back(Pair("vout", (int)outpt
.n
));
2656 UniValue
settxfee(const JSONRPCRequest
& request
)
2658 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2659 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2660 return NullUniValue
;
2663 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 1)
2664 throw std::runtime_error(
2666 "\nSet the transaction fee per kB. Overwrites the paytxfee parameter.\n"
2668 "1. amount (numeric or string, required) The transaction fee in " + CURRENCY_UNIT
+ "/kB\n"
2670 "true|false (boolean) Returns true if successful\n"
2672 + HelpExampleCli("settxfee", "0.00001")
2673 + HelpExampleRpc("settxfee", "0.00001")
2676 LOCK2(cs_main
, pwallet
->cs_wallet
);
2679 CAmount nAmount
= AmountFromValue(request
.params
[0]);
2681 payTxFee
= CFeeRate(nAmount
, 1000);
2685 UniValue
getwalletinfo(const JSONRPCRequest
& request
)
2687 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2688 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2689 return NullUniValue
;
2692 if (request
.fHelp
|| request
.params
.size() != 0)
2693 throw std::runtime_error(
2695 "Returns an object containing various wallet state info.\n"
2698 " \"walletname\": xxxxx, (string) the wallet name\n"
2699 " \"walletversion\": xxxxx, (numeric) the wallet version\n"
2700 " \"balance\": xxxxxxx, (numeric) the total confirmed balance of the wallet in " + CURRENCY_UNIT
+ "\n"
2701 " \"unconfirmed_balance\": xxx, (numeric) the total unconfirmed balance of the wallet in " + CURRENCY_UNIT
+ "\n"
2702 " \"immature_balance\": xxxxxx, (numeric) the total immature balance of the wallet in " + CURRENCY_UNIT
+ "\n"
2703 " \"txcount\": xxxxxxx, (numeric) the total number of transactions in the wallet\n"
2704 " \"keypoololdest\": xxxxxx, (numeric) the timestamp (seconds since Unix epoch) of the oldest pre-generated key in the key pool\n"
2705 " \"keypoolsize\": xxxx, (numeric) how many new keys are pre-generated (only counts external keys)\n"
2706 " \"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"
2707 " \"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"
2708 " \"paytxfee\": x.xxxx, (numeric) the transaction fee configuration, set in " + CURRENCY_UNIT
+ "/kB\n"
2709 " \"hdmasterkeyid\": \"<hash160>\" (string) the Hash160 of the HD master pubkey\n"
2712 + HelpExampleCli("getwalletinfo", "")
2713 + HelpExampleRpc("getwalletinfo", "")
2718 // Make sure the results are valid at least up to the most recent block
2719 // the user could have gotten from another RPC command prior to now
2720 pwallet
->BlockUntilSyncedToCurrentChain();
2722 LOCK2(cs_main
, pwallet
->cs_wallet
);
2724 UniValue
obj(UniValue::VOBJ
);
2726 size_t kpExternalSize
= pwallet
->KeypoolCountExternalKeys();
2727 obj
.push_back(Pair("walletname", pwallet
->GetName()));
2728 obj
.push_back(Pair("walletversion", pwallet
->GetVersion()));
2729 obj
.push_back(Pair("balance", ValueFromAmount(pwallet
->GetBalance())));
2730 obj
.push_back(Pair("unconfirmed_balance", ValueFromAmount(pwallet
->GetUnconfirmedBalance())));
2731 obj
.push_back(Pair("immature_balance", ValueFromAmount(pwallet
->GetImmatureBalance())));
2732 obj
.push_back(Pair("txcount", (int)pwallet
->mapWallet
.size()));
2733 obj
.push_back(Pair("keypoololdest", pwallet
->GetOldestKeyPoolTime()));
2734 obj
.push_back(Pair("keypoolsize", (int64_t)kpExternalSize
));
2735 CKeyID masterKeyID
= pwallet
->GetHDChain().masterKeyID
;
2736 if (!masterKeyID
.IsNull() && pwallet
->CanSupportFeature(FEATURE_HD_SPLIT
)) {
2737 obj
.push_back(Pair("keypoolsize_hd_internal", (int64_t)(pwallet
->GetKeyPoolSize() - kpExternalSize
)));
2739 if (pwallet
->IsCrypted()) {
2740 obj
.push_back(Pair("unlocked_until", pwallet
->nRelockTime
));
2742 obj
.push_back(Pair("paytxfee", ValueFromAmount(payTxFee
.GetFeePerK())));
2743 if (!masterKeyID
.IsNull())
2744 obj
.push_back(Pair("hdmasterkeyid", masterKeyID
.GetHex()));
2748 UniValue
listwallets(const JSONRPCRequest
& request
)
2750 if (request
.fHelp
|| request
.params
.size() != 0)
2751 throw std::runtime_error(
2753 "Returns a list of currently loaded wallets.\n"
2754 "For full information on the wallet, use \"getwalletinfo\"\n"
2756 "[ (json array of strings)\n"
2757 " \"walletname\" (string) the wallet name\n"
2761 + HelpExampleCli("listwallets", "")
2762 + HelpExampleRpc("listwallets", "")
2765 UniValue
obj(UniValue::VARR
);
2767 for (CWalletRef pwallet
: vpwallets
) {
2769 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2770 return NullUniValue
;
2773 LOCK(pwallet
->cs_wallet
);
2775 obj
.push_back(pwallet
->GetName());
2781 UniValue
resendwallettransactions(const JSONRPCRequest
& request
)
2783 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2784 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2785 return NullUniValue
;
2788 if (request
.fHelp
|| request
.params
.size() != 0)
2789 throw std::runtime_error(
2790 "resendwallettransactions\n"
2791 "Immediately re-broadcast unconfirmed wallet transactions to all peers.\n"
2792 "Intended only for testing; the wallet code periodically re-broadcasts\n"
2794 "Returns an RPC error if -walletbroadcast is set to false.\n"
2795 "Returns array of transaction ids that were re-broadcast.\n"
2799 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED
, "Error: Peer-to-peer functionality missing or disabled");
2801 LOCK2(cs_main
, pwallet
->cs_wallet
);
2803 if (!pwallet
->GetBroadcastTransactions()) {
2804 throw JSONRPCError(RPC_WALLET_ERROR
, "Error: Wallet transaction broadcasting is disabled with -walletbroadcast");
2807 std::vector
<uint256
> txids
= pwallet
->ResendWalletTransactionsBefore(GetTime(), g_connman
.get());
2808 UniValue
result(UniValue::VARR
);
2809 for (const uint256
& txid
: txids
)
2811 result
.push_back(txid
.ToString());
2816 UniValue
listunspent(const JSONRPCRequest
& request
)
2818 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2819 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2820 return NullUniValue
;
2823 if (request
.fHelp
|| request
.params
.size() > 5)
2824 throw std::runtime_error(
2825 "listunspent ( minconf maxconf [\"addresses\",...] [include_unsafe] [query_options])\n"
2826 "\nReturns array of unspent transaction outputs\n"
2827 "with between minconf and maxconf (inclusive) confirmations.\n"
2828 "Optionally filter to only include txouts paid to specified addresses.\n"
2830 "1. minconf (numeric, optional, default=1) The minimum confirmations to filter\n"
2831 "2. maxconf (numeric, optional, default=9999999) The maximum confirmations to filter\n"
2832 "3. \"addresses\" (string) A json array of bitcoin addresses to filter\n"
2834 " \"address\" (string) bitcoin address\n"
2837 "4. include_unsafe (bool, optional, default=true) Include outputs that are not safe to spend\n"
2838 " See description of \"safe\" attribute below.\n"
2839 "5. query_options (json, optional) JSON with query options\n"
2841 " \"minimumAmount\" (numeric or string, default=0) Minimum value of each UTXO in " + CURRENCY_UNIT
+ "\n"
2842 " \"maximumAmount\" (numeric or string, default=unlimited) Maximum value of each UTXO in " + CURRENCY_UNIT
+ "\n"
2843 " \"maximumCount\" (numeric or string, default=unlimited) Maximum number of UTXOs\n"
2844 " \"minimumSumAmount\" (numeric or string, default=unlimited) Minimum sum value of all UTXOs in " + CURRENCY_UNIT
+ "\n"
2847 "[ (array of json object)\n"
2849 " \"txid\" : \"txid\", (string) the transaction id \n"
2850 " \"vout\" : n, (numeric) the vout value\n"
2851 " \"address\" : \"address\", (string) the bitcoin address\n"
2852 " \"account\" : \"account\", (string) DEPRECATED. The associated account, or \"\" for the default account\n"
2853 " \"scriptPubKey\" : \"key\", (string) the script key\n"
2854 " \"amount\" : x.xxx, (numeric) the transaction output amount in " + CURRENCY_UNIT
+ "\n"
2855 " \"confirmations\" : n, (numeric) The number of confirmations\n"
2856 " \"redeemScript\" : n (string) The redeemScript if scriptPubKey is P2SH\n"
2857 " \"spendable\" : xxx, (bool) Whether we have the private keys to spend this output\n"
2858 " \"solvable\" : xxx, (bool) Whether we know how to spend this output, ignoring the lack of keys\n"
2859 " \"safe\" : xxx (bool) Whether this output is considered safe to spend. Unconfirmed transactions\n"
2860 " from outside keys and unconfirmed replacement transactions are considered unsafe\n"
2861 " and are not eligible for spending by fundrawtransaction and sendtoaddress.\n"
2867 + HelpExampleCli("listunspent", "")
2868 + HelpExampleCli("listunspent", "6 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
2869 + HelpExampleRpc("listunspent", "6, 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
2870 + HelpExampleCli("listunspent", "6 9999999 '[]' true '{ \"minimumAmount\": 0.005 }'")
2871 + HelpExampleRpc("listunspent", "6, 9999999, [] , true, { \"minimumAmount\": 0.005 } ")
2877 if (!request
.params
[0].isNull()) {
2878 RPCTypeCheckArgument(request
.params
[0], UniValue::VNUM
);
2879 nMinDepth
= request
.params
[0].get_int();
2882 int nMaxDepth
= 9999999;
2883 if (!request
.params
[1].isNull()) {
2884 RPCTypeCheckArgument(request
.params
[1], UniValue::VNUM
);
2885 nMaxDepth
= request
.params
[1].get_int();
2888 std::set
<CTxDestination
> destinations
;
2889 if (!request
.params
[2].isNull()) {
2890 RPCTypeCheckArgument(request
.params
[2], UniValue::VARR
);
2891 UniValue inputs
= request
.params
[2].get_array();
2892 for (unsigned int idx
= 0; idx
< inputs
.size(); idx
++) {
2893 const UniValue
& input
= inputs
[idx
];
2894 CTxDestination dest
= DecodeDestination(input
.get_str());
2895 if (!IsValidDestination(dest
)) {
2896 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, std::string("Invalid Bitcoin address: ") + input
.get_str());
2898 if (!destinations
.insert(dest
).second
) {
2899 throw JSONRPCError(RPC_INVALID_PARAMETER
, std::string("Invalid parameter, duplicated address: ") + input
.get_str());
2904 bool include_unsafe
= true;
2905 if (!request
.params
[3].isNull()) {
2906 RPCTypeCheckArgument(request
.params
[3], UniValue::VBOOL
);
2907 include_unsafe
= request
.params
[3].get_bool();
2910 CAmount nMinimumAmount
= 0;
2911 CAmount nMaximumAmount
= MAX_MONEY
;
2912 CAmount nMinimumSumAmount
= MAX_MONEY
;
2913 uint64_t nMaximumCount
= 0;
2915 if (!request
.params
[4].isNull()) {
2916 const UniValue
& options
= request
.params
[4].get_obj();
2918 if (options
.exists("minimumAmount"))
2919 nMinimumAmount
= AmountFromValue(options
["minimumAmount"]);
2921 if (options
.exists("maximumAmount"))
2922 nMaximumAmount
= AmountFromValue(options
["maximumAmount"]);
2924 if (options
.exists("minimumSumAmount"))
2925 nMinimumSumAmount
= AmountFromValue(options
["minimumSumAmount"]);
2927 if (options
.exists("maximumCount"))
2928 nMaximumCount
= options
["maximumCount"].get_int64();
2931 // Make sure the results are valid at least up to the most recent block
2932 // the user could have gotten from another RPC command prior to now
2933 pwallet
->BlockUntilSyncedToCurrentChain();
2935 UniValue
results(UniValue::VARR
);
2936 std::vector
<COutput
> vecOutputs
;
2937 LOCK2(cs_main
, pwallet
->cs_wallet
);
2939 pwallet
->AvailableCoins(vecOutputs
, !include_unsafe
, nullptr, nMinimumAmount
, nMaximumAmount
, nMinimumSumAmount
, nMaximumCount
, nMinDepth
, nMaxDepth
);
2940 for (const COutput
& out
: vecOutputs
) {
2941 CTxDestination address
;
2942 const CScript
& scriptPubKey
= out
.tx
->tx
->vout
[out
.i
].scriptPubKey
;
2943 bool fValidAddress
= ExtractDestination(scriptPubKey
, address
);
2945 if (destinations
.size() && (!fValidAddress
|| !destinations
.count(address
)))
2948 UniValue
entry(UniValue::VOBJ
);
2949 entry
.push_back(Pair("txid", out
.tx
->GetHash().GetHex()));
2950 entry
.push_back(Pair("vout", out
.i
));
2952 if (fValidAddress
) {
2953 entry
.push_back(Pair("address", EncodeDestination(address
)));
2955 if (pwallet
->mapAddressBook
.count(address
)) {
2956 entry
.push_back(Pair("account", pwallet
->mapAddressBook
[address
].name
));
2959 if (scriptPubKey
.IsPayToScriptHash()) {
2960 const CScriptID
& hash
= boost::get
<CScriptID
>(address
);
2961 CScript redeemScript
;
2962 if (pwallet
->GetCScript(hash
, redeemScript
)) {
2963 entry
.push_back(Pair("redeemScript", HexStr(redeemScript
.begin(), redeemScript
.end())));
2968 entry
.push_back(Pair("scriptPubKey", HexStr(scriptPubKey
.begin(), scriptPubKey
.end())));
2969 entry
.push_back(Pair("amount", ValueFromAmount(out
.tx
->tx
->vout
[out
.i
].nValue
)));
2970 entry
.push_back(Pair("confirmations", out
.nDepth
));
2971 entry
.push_back(Pair("spendable", out
.fSpendable
));
2972 entry
.push_back(Pair("solvable", out
.fSolvable
));
2973 entry
.push_back(Pair("safe", out
.fSafe
));
2974 results
.push_back(entry
);
2980 UniValue
fundrawtransaction(const JSONRPCRequest
& request
)
2982 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2983 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2984 return NullUniValue
;
2987 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 3)
2988 throw std::runtime_error(
2989 "fundrawtransaction \"hexstring\" ( options iswitness )\n"
2990 "\nAdd inputs to a transaction until it has enough in value to meet its out value.\n"
2991 "This will not modify existing inputs, and will add at most one change output to the outputs.\n"
2992 "No existing outputs will be modified unless \"subtractFeeFromOutputs\" is specified.\n"
2993 "Note that inputs which were signed may need to be resigned after completion since in/outputs have been added.\n"
2994 "The inputs added will not be signed, use signrawtransaction for that.\n"
2995 "Note that all existing inputs must have their previous output transaction be in the wallet.\n"
2996 "Note that all inputs selected must be of standard form and P2SH scripts must be\n"
2997 "in the wallet using importaddress or addmultisigaddress (to calculate fees).\n"
2998 "You can see whether this is the case by checking the \"solvable\" field in the listunspent output.\n"
2999 "Only pay-to-pubkey, multisig, and P2SH versions thereof are currently supported for watch-only\n"
3001 "1. \"hexstring\" (string, required) The hex string of the raw transaction\n"
3002 "2. options (object, optional)\n"
3004 " \"changeAddress\" (string, optional, default pool address) The bitcoin address to receive the change\n"
3005 " \"changePosition\" (numeric, optional, default random) The index of the change output\n"
3006 " \"includeWatching\" (boolean, optional, default false) Also select inputs which are watch only\n"
3007 " \"lockUnspents\" (boolean, optional, default false) Lock selected unspent outputs\n"
3008 " \"feeRate\" (numeric, optional, default not set: makes wallet determine the fee) Set a specific fee rate in " + CURRENCY_UNIT
+ "/kB\n"
3009 " \"subtractFeeFromOutputs\" (array, optional) A json array of integers.\n"
3010 " The fee will be equally deducted from the amount of each specified output.\n"
3011 " The outputs are specified by their zero-based index, before any change output is added.\n"
3012 " Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n"
3013 " If no outputs are specified here, the sender pays the fee.\n"
3014 " [vout_index,...]\n"
3015 " \"replaceable\" (boolean, optional) Marks this transaction as BIP125 replaceable.\n"
3016 " Allows this transaction to be replaced by a transaction with higher fees\n"
3017 " \"conf_target\" (numeric, optional) Confirmation target (in blocks)\n"
3018 " \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
3021 " \"CONSERVATIVE\"\n"
3023 " for backward compatibility: passing in a true instead of an object will result in {\"includeWatching\":true}\n"
3024 "3. iswitness (boolean, optional) Whether the transaction hex is a serialized witness transaction \n"
3025 " If iswitness is not present, heuristic tests will be used in decoding\n"
3029 " \"hex\": \"value\", (string) The resulting raw transaction (hex-encoded string)\n"
3030 " \"fee\": n, (numeric) Fee in " + CURRENCY_UNIT
+ " the resulting transaction pays\n"
3031 " \"changepos\": n (numeric) The position of the added change output, or -1\n"
3034 "\nCreate a transaction with no inputs\n"
3035 + HelpExampleCli("createrawtransaction", "\"[]\" \"{\\\"myaddress\\\":0.01}\"") +
3036 "\nAdd sufficient unsigned inputs to meet the output value\n"
3037 + HelpExampleCli("fundrawtransaction", "\"rawtransactionhex\"") +
3038 "\nSign the transaction\n"
3039 + HelpExampleCli("signrawtransaction", "\"fundedtransactionhex\"") +
3040 "\nSend the transaction\n"
3041 + HelpExampleCli("sendrawtransaction", "\"signedtransactionhex\"")
3045 RPCTypeCheck(request
.params
, {UniValue::VSTR
});
3047 // Make sure the results are valid at least up to the most recent block
3048 // the user could have gotten from another RPC command prior to now
3049 pwallet
->BlockUntilSyncedToCurrentChain();
3051 CCoinControl coinControl
;
3052 int changePosition
= -1;
3053 bool lockUnspents
= false;
3054 UniValue subtractFeeFromOutputs
;
3055 std::set
<int> setSubtractFeeFromOutputs
;
3057 if (!request
.params
[1].isNull()) {
3058 if (request
.params
[1].type() == UniValue::VBOOL
) {
3059 // backward compatibility bool only fallback
3060 coinControl
.fAllowWatchOnly
= request
.params
[1].get_bool();
3063 RPCTypeCheck(request
.params
, {UniValue::VSTR
, UniValue::VOBJ
, UniValue::VBOOL
});
3065 UniValue options
= request
.params
[1];
3067 RPCTypeCheckObj(options
,
3069 {"changeAddress", UniValueType(UniValue::VSTR
)},
3070 {"changePosition", UniValueType(UniValue::VNUM
)},
3071 {"includeWatching", UniValueType(UniValue::VBOOL
)},
3072 {"lockUnspents", UniValueType(UniValue::VBOOL
)},
3073 {"reserveChangeKey", UniValueType(UniValue::VBOOL
)}, // DEPRECATED (and ignored), should be removed in 0.16 or so.
3074 {"feeRate", UniValueType()}, // will be checked below
3075 {"subtractFeeFromOutputs", UniValueType(UniValue::VARR
)},
3076 {"replaceable", UniValueType(UniValue::VBOOL
)},
3077 {"conf_target", UniValueType(UniValue::VNUM
)},
3078 {"estimate_mode", UniValueType(UniValue::VSTR
)},
3082 if (options
.exists("changeAddress")) {
3083 CTxDestination dest
= DecodeDestination(options
["changeAddress"].get_str());
3085 if (!IsValidDestination(dest
)) {
3086 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "changeAddress must be a valid bitcoin address");
3089 coinControl
.destChange
= dest
;
3092 if (options
.exists("changePosition"))
3093 changePosition
= options
["changePosition"].get_int();
3095 if (options
.exists("includeWatching"))
3096 coinControl
.fAllowWatchOnly
= options
["includeWatching"].get_bool();
3098 if (options
.exists("lockUnspents"))
3099 lockUnspents
= options
["lockUnspents"].get_bool();
3101 if (options
.exists("feeRate"))
3103 coinControl
.m_feerate
= CFeeRate(AmountFromValue(options
["feeRate"]));
3104 coinControl
.fOverrideFeeRate
= true;
3107 if (options
.exists("subtractFeeFromOutputs"))
3108 subtractFeeFromOutputs
= options
["subtractFeeFromOutputs"].get_array();
3110 if (options
.exists("replaceable")) {
3111 coinControl
.signalRbf
= options
["replaceable"].get_bool();
3113 if (options
.exists("conf_target")) {
3114 if (options
.exists("feeRate")) {
3115 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Cannot specify both conf_target and feeRate");
3117 coinControl
.m_confirm_target
= ParseConfirmTarget(options
["conf_target"]);
3119 if (options
.exists("estimate_mode")) {
3120 if (options
.exists("feeRate")) {
3121 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Cannot specify both estimate_mode and feeRate");
3123 if (!FeeModeFromString(options
["estimate_mode"].get_str(), coinControl
.m_fee_mode
)) {
3124 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid estimate_mode parameter");
3130 // parse hex string from parameter
3131 CMutableTransaction tx
;
3132 bool try_witness
= request
.params
[2].isNull() ? true : request
.params
[2].get_bool();
3133 bool try_no_witness
= request
.params
[2].isNull() ? true : !request
.params
[2].get_bool();
3134 if (!DecodeHexTx(tx
, request
.params
[0].get_str(), try_no_witness
, try_witness
)) {
3135 throw JSONRPCError(RPC_DESERIALIZATION_ERROR
, "TX decode failed");
3138 if (tx
.vout
.size() == 0)
3139 throw JSONRPCError(RPC_INVALID_PARAMETER
, "TX must have at least one output");
3141 if (changePosition
!= -1 && (changePosition
< 0 || (unsigned int)changePosition
> tx
.vout
.size()))
3142 throw JSONRPCError(RPC_INVALID_PARAMETER
, "changePosition out of bounds");
3144 for (unsigned int idx
= 0; idx
< subtractFeeFromOutputs
.size(); idx
++) {
3145 int pos
= subtractFeeFromOutputs
[idx
].get_int();
3146 if (setSubtractFeeFromOutputs
.count(pos
))
3147 throw JSONRPCError(RPC_INVALID_PARAMETER
, strprintf("Invalid parameter, duplicated position: %d", pos
));
3149 throw JSONRPCError(RPC_INVALID_PARAMETER
, strprintf("Invalid parameter, negative position: %d", pos
));
3150 if (pos
>= int(tx
.vout
.size()))
3151 throw JSONRPCError(RPC_INVALID_PARAMETER
, strprintf("Invalid parameter, position too large: %d", pos
));
3152 setSubtractFeeFromOutputs
.insert(pos
);
3156 std::string strFailReason
;
3158 if (!pwallet
->FundTransaction(tx
, nFeeOut
, changePosition
, strFailReason
, lockUnspents
, setSubtractFeeFromOutputs
, coinControl
)) {
3159 throw JSONRPCError(RPC_WALLET_ERROR
, strFailReason
);
3162 UniValue
result(UniValue::VOBJ
);
3163 result
.push_back(Pair("hex", EncodeHexTx(tx
)));
3164 result
.push_back(Pair("changepos", changePosition
));
3165 result
.push_back(Pair("fee", ValueFromAmount(nFeeOut
)));
3170 UniValue
bumpfee(const JSONRPCRequest
& request
)
3172 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
3174 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
))
3175 return NullUniValue
;
3177 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2) {
3178 throw std::runtime_error(
3179 "bumpfee \"txid\" ( options ) \n"
3180 "\nBumps the fee of an opt-in-RBF transaction T, replacing it with a new transaction B.\n"
3181 "An opt-in RBF transaction with the given txid must be in the wallet.\n"
3182 "The command will pay the additional fee by decreasing (or perhaps removing) its change output.\n"
3183 "If the change output is not big enough to cover the increased fee, the command will currently fail\n"
3184 "instead of adding new inputs to compensate. (A future implementation could improve this.)\n"
3185 "The command will fail if the wallet or mempool contains a transaction that spends one of T's outputs.\n"
3186 "By default, the new fee will be calculated automatically using estimatefee.\n"
3187 "The user can specify a confirmation target for estimatefee.\n"
3188 "Alternatively, the user can specify totalFee, or use RPC settxfee to set a higher fee rate.\n"
3189 "At a minimum, the new fee rate must be high enough to pay an additional new relay fee (incrementalfee\n"
3190 "returned by getnetworkinfo) to enter the node's mempool.\n"
3192 "1. txid (string, required) The txid to be bumped\n"
3193 "2. options (object, optional)\n"
3195 " \"confTarget\" (numeric, optional) Confirmation target (in blocks)\n"
3196 " \"totalFee\" (numeric, optional) Total fee (NOT feerate) to pay, in satoshis.\n"
3197 " In rare cases, the actual fee paid might be slightly higher than the specified\n"
3198 " totalFee if the tx change output has to be removed because it is too close to\n"
3199 " the dust threshold.\n"
3200 " \"replaceable\" (boolean, optional, default true) Whether the new transaction should still be\n"
3201 " marked bip-125 replaceable. If true, the sequence numbers in the transaction will\n"
3202 " be left unchanged from the original. If false, any input sequence numbers in the\n"
3203 " original transaction that were less than 0xfffffffe will be increased to 0xfffffffe\n"
3204 " so the new transaction will not be explicitly bip-125 replaceable (though it may\n"
3205 " still be replaceable in practice, for example if it has unconfirmed ancestors which\n"
3206 " are replaceable).\n"
3207 " \"estimate_mode\" (string, optional, default=UNSET) The fee estimate mode, must be one of:\n"
3210 " \"CONSERVATIVE\"\n"
3214 " \"txid\": \"value\", (string) The id of the new transaction\n"
3215 " \"origfee\": n, (numeric) Fee of the replaced transaction\n"
3216 " \"fee\": n, (numeric) Fee of the new transaction\n"
3217 " \"errors\": [ str... ] (json array of strings) Errors encountered during processing (may be empty)\n"
3220 "\nBump the fee, get the new transaction\'s txid\n" +
3221 HelpExampleCli("bumpfee", "<txid>"));
3224 RPCTypeCheck(request
.params
, {UniValue::VSTR
, UniValue::VOBJ
});
3226 hash
.SetHex(request
.params
[0].get_str());
3228 // optional parameters
3229 CAmount totalFee
= 0;
3230 CCoinControl coin_control
;
3231 coin_control
.signalRbf
= true;
3232 if (!request
.params
[1].isNull()) {
3233 UniValue options
= request
.params
[1];
3234 RPCTypeCheckObj(options
,
3236 {"confTarget", UniValueType(UniValue::VNUM
)},
3237 {"totalFee", UniValueType(UniValue::VNUM
)},
3238 {"replaceable", UniValueType(UniValue::VBOOL
)},
3239 {"estimate_mode", UniValueType(UniValue::VSTR
)},
3243 if (options
.exists("confTarget") && options
.exists("totalFee")) {
3244 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.");
3245 } else if (options
.exists("confTarget")) { // TODO: alias this to conf_target
3246 coin_control
.m_confirm_target
= ParseConfirmTarget(options
["confTarget"]);
3247 } else if (options
.exists("totalFee")) {
3248 totalFee
= options
["totalFee"].get_int64();
3249 if (totalFee
<= 0) {
3250 throw JSONRPCError(RPC_INVALID_PARAMETER
, strprintf("Invalid totalFee %s (must be greater than 0)", FormatMoney(totalFee
)));
3254 if (options
.exists("replaceable")) {
3255 coin_control
.signalRbf
= options
["replaceable"].get_bool();
3257 if (options
.exists("estimate_mode")) {
3258 if (!FeeModeFromString(options
["estimate_mode"].get_str(), coin_control
.m_fee_mode
)) {
3259 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid estimate_mode parameter");
3264 // Make sure the results are valid at least up to the most recent block
3265 // the user could have gotten from another RPC command prior to now
3266 pwallet
->BlockUntilSyncedToCurrentChain();
3268 LOCK2(cs_main
, pwallet
->cs_wallet
);
3269 EnsureWalletIsUnlocked(pwallet
);
3272 std::vector
<std::string
> errors
;
3275 CMutableTransaction mtx
;
3276 feebumper::Result res
= feebumper::CreateTransaction(pwallet
, hash
, coin_control
, totalFee
, errors
, old_fee
, new_fee
, mtx
);
3277 if (res
!= feebumper::Result::OK
) {
3279 case feebumper::Result::INVALID_ADDRESS_OR_KEY
:
3280 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, errors
[0]);
3282 case feebumper::Result::INVALID_REQUEST
:
3283 throw JSONRPCError(RPC_INVALID_REQUEST
, errors
[0]);
3285 case feebumper::Result::INVALID_PARAMETER
:
3286 throw JSONRPCError(RPC_INVALID_PARAMETER
, errors
[0]);
3288 case feebumper::Result::WALLET_ERROR
:
3289 throw JSONRPCError(RPC_WALLET_ERROR
, errors
[0]);
3292 throw JSONRPCError(RPC_MISC_ERROR
, errors
[0]);
3297 // sign bumped transaction
3298 if (!feebumper::SignTransaction(pwallet
, mtx
)) {
3299 throw JSONRPCError(RPC_WALLET_ERROR
, "Can't sign transaction.");
3301 // commit the bumped transaction
3303 if (feebumper::CommitTransaction(pwallet
, hash
, std::move(mtx
), errors
, txid
) != feebumper::Result::OK
) {
3304 throw JSONRPCError(RPC_WALLET_ERROR
, errors
[0]);
3306 UniValue
result(UniValue::VOBJ
);
3307 result
.push_back(Pair("txid", txid
.GetHex()));
3308 result
.push_back(Pair("origfee", ValueFromAmount(old_fee
)));
3309 result
.push_back(Pair("fee", ValueFromAmount(new_fee
)));
3310 UniValue
result_errors(UniValue::VARR
);
3311 for (const std::string
& error
: errors
) {
3312 result_errors
.push_back(error
);
3314 result
.push_back(Pair("errors", result_errors
));
3319 UniValue
generate(const JSONRPCRequest
& request
)
3321 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
3323 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
3324 return NullUniValue
;
3327 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2) {
3328 throw std::runtime_error(
3329 "generate nblocks ( maxtries )\n"
3330 "\nMine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.\n"
3332 "1. nblocks (numeric, required) How many blocks are generated immediately.\n"
3333 "2. maxtries (numeric, optional) How many iterations to try (default = 1000000).\n"
3335 "[ blockhashes ] (array) hashes of blocks generated\n"
3337 "\nGenerate 11 blocks\n"
3338 + HelpExampleCli("generate", "11")
3342 int num_generate
= request
.params
[0].get_int();
3343 uint64_t max_tries
= 1000000;
3344 if (!request
.params
[1].isNull()) {
3345 max_tries
= request
.params
[1].get_int();
3348 std::shared_ptr
<CReserveScript
> coinbase_script
;
3349 pwallet
->GetScriptForMining(coinbase_script
);
3351 // If the keypool is exhausted, no script is returned at all. Catch this.
3352 if (!coinbase_script
) {
3353 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT
, "Error: Keypool ran out, please call keypoolrefill first");
3356 //throw an error if no script was provided
3357 if (coinbase_script
->reserveScript
.empty()) {
3358 throw JSONRPCError(RPC_INTERNAL_ERROR
, "No coinbase script available");
3361 return generateBlocks(coinbase_script
, num_generate
, max_tries
, true);
3364 UniValue
rescanblockchain(const JSONRPCRequest
& request
)
3366 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
3367 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
3368 return NullUniValue
;
3371 if (request
.fHelp
|| request
.params
.size() > 2) {
3372 throw std::runtime_error(
3373 "rescanblockchain (\"start_height\") (\"stop_height\")\n"
3374 "\nRescan the local blockchain for wallet related transactions.\n"
3376 "1. \"start_height\" (numeric, optional) block height where the rescan should start\n"
3377 "2. \"stop_height\" (numeric, optional) the last block height that should be scanned\n"
3380 " \"start_height\" (numeric) The block height where the rescan has started. If omitted, rescan started from the genesis block.\n"
3381 " \"stop_height\" (numeric) The height of the last rescanned block. If omitted, rescan stopped at the chain tip.\n"
3384 + HelpExampleCli("rescanblockchain", "100000 120000")
3385 + HelpExampleRpc("rescanblockchain", "100000, 120000")
3389 LOCK2(cs_main
, pwallet
->cs_wallet
);
3391 CBlockIndex
*pindexStart
= chainActive
.Genesis();
3392 CBlockIndex
*pindexStop
= nullptr;
3393 if (!request
.params
[0].isNull()) {
3394 pindexStart
= chainActive
[request
.params
[0].get_int()];
3396 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid start_height");
3400 if (!request
.params
[1].isNull()) {
3401 pindexStop
= chainActive
[request
.params
[1].get_int()];
3403 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid stop_height");
3405 else if (pindexStop
->nHeight
< pindexStart
->nHeight
) {
3406 throw JSONRPCError(RPC_INVALID_PARAMETER
, "stop_height must be greater then start_height");
3410 // We can't rescan beyond non-pruned blocks, stop and throw an error
3412 CBlockIndex
*block
= pindexStop
? pindexStop
: chainActive
.Tip();
3413 while (block
&& block
->nHeight
>= pindexStart
->nHeight
) {
3414 if (!(block
->nStatus
& BLOCK_HAVE_DATA
)) {
3415 throw JSONRPCError(RPC_MISC_ERROR
, "Can't rescan beyond pruned data. Use RPC call getblockchaininfo to determine your pruned height.");
3417 block
= block
->pprev
;
3421 CBlockIndex
*stopBlock
= pwallet
->ScanForWalletTransactions(pindexStart
, pindexStop
, true);
3423 if (pwallet
->IsAbortingRescan()) {
3424 throw JSONRPCError(RPC_MISC_ERROR
, "Rescan aborted.");
3426 // if we got a nullptr returned, ScanForWalletTransactions did rescan up to the requested stopindex
3427 stopBlock
= pindexStop
? pindexStop
: chainActive
.Tip();
3430 throw JSONRPCError(RPC_MISC_ERROR
, "Rescan failed. Potentially corrupted data files.");
3433 UniValue
response(UniValue::VOBJ
);
3434 response
.pushKV("start_height", pindexStart
->nHeight
);
3435 response
.pushKV("stop_height", stopBlock
->nHeight
);
3439 extern UniValue
abortrescan(const JSONRPCRequest
& request
); // in rpcdump.cpp
3440 extern UniValue
dumpprivkey(const JSONRPCRequest
& request
); // in rpcdump.cpp
3441 extern UniValue
importprivkey(const JSONRPCRequest
& request
);
3442 extern UniValue
importaddress(const JSONRPCRequest
& request
);
3443 extern UniValue
importpubkey(const JSONRPCRequest
& request
);
3444 extern UniValue
dumpwallet(const JSONRPCRequest
& request
);
3445 extern UniValue
importwallet(const JSONRPCRequest
& request
);
3446 extern UniValue
importprunedfunds(const JSONRPCRequest
& request
);
3447 extern UniValue
removeprunedfunds(const JSONRPCRequest
& request
);
3448 extern UniValue
importmulti(const JSONRPCRequest
& request
);
3449 extern UniValue
rescanblockchain(const JSONRPCRequest
& request
);
3451 static const CRPCCommand commands
[] =
3452 { // category name actor (function) argNames
3453 // --------------------- ------------------------ ----------------------- ----------
3454 { "rawtransactions", "fundrawtransaction", &fundrawtransaction
, {"hexstring","options","iswitness"} },
3455 { "hidden", "resendwallettransactions", &resendwallettransactions
, {} },
3456 { "wallet", "abandontransaction", &abandontransaction
, {"txid"} },
3457 { "wallet", "abortrescan", &abortrescan
, {} },
3458 { "wallet", "addmultisigaddress", &addmultisigaddress
, {"nrequired","keys","account"} },
3459 { "wallet", "addwitnessaddress", &addwitnessaddress
, {"address","p2sh"} },
3460 { "wallet", "backupwallet", &backupwallet
, {"destination"} },
3461 { "wallet", "bumpfee", &bumpfee
, {"txid", "options"} },
3462 { "wallet", "dumpprivkey", &dumpprivkey
, {"address"} },
3463 { "wallet", "dumpwallet", &dumpwallet
, {"filename"} },
3464 { "wallet", "encryptwallet", &encryptwallet
, {"passphrase"} },
3465 { "wallet", "getaccountaddress", &getaccountaddress
, {"account"} },
3466 { "wallet", "getaccount", &getaccount
, {"address"} },
3467 { "wallet", "getaddressesbyaccount", &getaddressesbyaccount
, {"account"} },
3468 { "wallet", "getbalance", &getbalance
, {"account","minconf","include_watchonly"} },
3469 { "wallet", "getnewaddress", &getnewaddress
, {"account"} },
3470 { "wallet", "getrawchangeaddress", &getrawchangeaddress
, {} },
3471 { "wallet", "getreceivedbyaccount", &getreceivedbyaccount
, {"account","minconf"} },
3472 { "wallet", "getreceivedbyaddress", &getreceivedbyaddress
, {"address","minconf"} },
3473 { "wallet", "gettransaction", &gettransaction
, {"txid","include_watchonly"} },
3474 { "wallet", "getunconfirmedbalance", &getunconfirmedbalance
, {} },
3475 { "wallet", "getwalletinfo", &getwalletinfo
, {} },
3476 { "wallet", "importmulti", &importmulti
, {"requests","options"} },
3477 { "wallet", "importprivkey", &importprivkey
, {"privkey","label","rescan"} },
3478 { "wallet", "importwallet", &importwallet
, {"filename"} },
3479 { "wallet", "importaddress", &importaddress
, {"address","label","rescan","p2sh"} },
3480 { "wallet", "importprunedfunds", &importprunedfunds
, {"rawtransaction","txoutproof"} },
3481 { "wallet", "importpubkey", &importpubkey
, {"pubkey","label","rescan"} },
3482 { "wallet", "keypoolrefill", &keypoolrefill
, {"newsize"} },
3483 { "wallet", "listaccounts", &listaccounts
, {"minconf","include_watchonly"} },
3484 { "wallet", "listaddressgroupings", &listaddressgroupings
, {} },
3485 { "wallet", "listlockunspent", &listlockunspent
, {} },
3486 { "wallet", "listreceivedbyaccount", &listreceivedbyaccount
, {"minconf","include_empty","include_watchonly"} },
3487 { "wallet", "listreceivedbyaddress", &listreceivedbyaddress
, {"minconf","include_empty","include_watchonly"} },
3488 { "wallet", "listsinceblock", &listsinceblock
, {"blockhash","target_confirmations","include_watchonly","include_removed"} },
3489 { "wallet", "listtransactions", &listtransactions
, {"account","count","skip","include_watchonly"} },
3490 { "wallet", "listunspent", &listunspent
, {"minconf","maxconf","addresses","include_unsafe","query_options"} },
3491 { "wallet", "listwallets", &listwallets
, {} },
3492 { "wallet", "lockunspent", &lockunspent
, {"unlock","transactions"} },
3493 { "wallet", "move", &movecmd
, {"fromaccount","toaccount","amount","minconf","comment"} },
3494 { "wallet", "sendfrom", &sendfrom
, {"fromaccount","toaddress","amount","minconf","comment","comment_to"} },
3495 { "wallet", "sendmany", &sendmany
, {"fromaccount","amounts","minconf","comment","subtractfeefrom","replaceable","conf_target","estimate_mode"} },
3496 { "wallet", "sendtoaddress", &sendtoaddress
, {"address","amount","comment","comment_to","subtractfeefromamount","replaceable","conf_target","estimate_mode"} },
3497 { "wallet", "setaccount", &setaccount
, {"address","account"} },
3498 { "wallet", "settxfee", &settxfee
, {"amount"} },
3499 { "wallet", "signmessage", &signmessage
, {"address","message"} },
3500 { "wallet", "walletlock", &walletlock
, {} },
3501 { "wallet", "walletpassphrasechange", &walletpassphrasechange
, {"oldpassphrase","newpassphrase"} },
3502 { "wallet", "walletpassphrase", &walletpassphrase
, {"passphrase","timeout"} },
3503 { "wallet", "removeprunedfunds", &removeprunedfunds
, {"txid"} },
3504 { "wallet", "rescanblockchain", &rescanblockchain
, {"start_height", "stop_height"} },
3506 { "generating", "generate", &generate
, {"nblocks","maxtries"} },
3509 void RegisterWalletRPCCommands(CRPCTable
&t
)
3511 for (unsigned int vcidx
= 0; vcidx
< ARRAYLEN(commands
); vcidx
++)
3512 t
.appendCommand(commands
[vcidx
].name
, &commands
[vcidx
]);