1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
9 #include "consensus/validation.h"
12 #include "validation.h"
14 #include "policy/policy.h"
15 #include "policy/rbf.h"
16 #include "rpc/server.h"
17 #include "script/sign.h"
20 #include "utilmoneystr.h"
21 #include "wallet/feebumper.h"
22 #include "wallet/wallet.h"
23 #include "wallet/walletdb.h"
27 #include <boost/assign/list_of.hpp>
31 CWallet
*GetWalletForJSONRPCRequest(const JSONRPCRequest
& request
)
36 std::string
HelpRequiringPassphrase(CWallet
* const pwallet
)
38 return pwallet
&& pwallet
->IsCrypted()
39 ? "\nRequires wallet passphrase to be set with walletpassphrase call."
43 bool EnsureWalletIsAvailable(CWallet
* const pwallet
, bool avoidException
)
47 throw JSONRPCError(RPC_METHOD_NOT_FOUND
, "Method not found (disabled)");
54 void EnsureWalletIsUnlocked(CWallet
* const pwallet
)
56 if (pwallet
->IsLocked()) {
57 throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED
, "Error: Please enter the wallet passphrase with walletpassphrase first.");
61 void WalletTxToJSON(const CWalletTx
& wtx
, UniValue
& entry
)
63 int confirms
= wtx
.GetDepthInMainChain();
64 entry
.push_back(Pair("confirmations", confirms
));
66 entry
.push_back(Pair("generated", true));
69 entry
.push_back(Pair("blockhash", wtx
.hashBlock
.GetHex()));
70 entry
.push_back(Pair("blockindex", wtx
.nIndex
));
71 entry
.push_back(Pair("blocktime", mapBlockIndex
[wtx
.hashBlock
]->GetBlockTime()));
73 entry
.push_back(Pair("trusted", wtx
.IsTrusted()));
75 uint256 hash
= wtx
.GetHash();
76 entry
.push_back(Pair("txid", hash
.GetHex()));
77 UniValue
conflicts(UniValue::VARR
);
78 BOOST_FOREACH(const uint256
& conflict
, wtx
.GetConflicts())
79 conflicts
.push_back(conflict
.GetHex());
80 entry
.push_back(Pair("walletconflicts", conflicts
));
81 entry
.push_back(Pair("time", wtx
.GetTxTime()));
82 entry
.push_back(Pair("timereceived", (int64_t)wtx
.nTimeReceived
));
84 // Add opt-in RBF status
85 std::string rbfStatus
= "no";
88 RBFTransactionState rbfState
= IsRBFOptIn(wtx
, mempool
);
89 if (rbfState
== RBF_TRANSACTIONSTATE_UNKNOWN
)
90 rbfStatus
= "unknown";
91 else if (rbfState
== RBF_TRANSACTIONSTATE_REPLACEABLE_BIP125
)
94 entry
.push_back(Pair("bip125-replaceable", rbfStatus
));
96 BOOST_FOREACH(const PAIRTYPE(std::string
, std::string
)& item
, wtx
.mapValue
)
97 entry
.push_back(Pair(item
.first
, item
.second
));
100 std::string
AccountFromValue(const UniValue
& value
)
102 std::string strAccount
= value
.get_str();
103 if (strAccount
== "*")
104 throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME
, "Invalid account name");
108 UniValue
getnewaddress(const JSONRPCRequest
& request
)
110 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
111 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
115 if (request
.fHelp
|| request
.params
.size() > 1)
116 throw std::runtime_error(
117 "getnewaddress ( \"account\" )\n"
118 "\nReturns a new Bitcoin address for receiving payments.\n"
119 "If 'account' is specified (DEPRECATED), it is added to the address book \n"
120 "so payments received with the address will be credited to 'account'.\n"
122 "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"
124 "\"address\" (string) The new bitcoin address\n"
126 + HelpExampleCli("getnewaddress", "")
127 + HelpExampleRpc("getnewaddress", "")
130 LOCK2(cs_main
, pwallet
->cs_wallet
);
132 // Parse the account first so we don't generate a key if there's an error
133 std::string strAccount
;
134 if (request
.params
.size() > 0)
135 strAccount
= AccountFromValue(request
.params
[0]);
137 if (!pwallet
->IsLocked()) {
138 pwallet
->TopUpKeyPool();
141 // Generate a new key that is added to wallet
143 if (!pwallet
->GetKeyFromPool(newKey
)) {
144 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT
, "Error: Keypool ran out, please call keypoolrefill first");
146 CKeyID keyID
= newKey
.GetID();
148 pwallet
->SetAddressBook(keyID
, strAccount
, "receive");
150 return CBitcoinAddress(keyID
).ToString();
154 CBitcoinAddress
GetAccountAddress(CWallet
* const pwallet
, std::string strAccount
, bool bForceNew
=false)
157 if (!pwallet
->GetAccountPubkey(pubKey
, strAccount
, bForceNew
)) {
158 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT
, "Error: Keypool ran out, please call keypoolrefill first");
161 return CBitcoinAddress(pubKey
.GetID());
164 UniValue
getaccountaddress(const JSONRPCRequest
& request
)
166 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
167 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
171 if (request
.fHelp
|| request
.params
.size() != 1)
172 throw std::runtime_error(
173 "getaccountaddress \"account\"\n"
174 "\nDEPRECATED. Returns the current Bitcoin address for receiving payments to this account.\n"
176 "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"
178 "\"address\" (string) The account bitcoin address\n"
180 + HelpExampleCli("getaccountaddress", "")
181 + HelpExampleCli("getaccountaddress", "\"\"")
182 + HelpExampleCli("getaccountaddress", "\"myaccount\"")
183 + HelpExampleRpc("getaccountaddress", "\"myaccount\"")
186 LOCK2(cs_main
, pwallet
->cs_wallet
);
188 // Parse the account first so we don't generate a key if there's an error
189 std::string strAccount
= AccountFromValue(request
.params
[0]);
191 UniValue
ret(UniValue::VSTR
);
193 ret
= GetAccountAddress(pwallet
, strAccount
).ToString();
198 UniValue
getrawchangeaddress(const JSONRPCRequest
& request
)
200 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
201 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
205 if (request
.fHelp
|| request
.params
.size() > 1)
206 throw std::runtime_error(
207 "getrawchangeaddress\n"
208 "\nReturns a new Bitcoin address, for receiving change.\n"
209 "This is for use with raw transactions, NOT normal use.\n"
211 "\"address\" (string) The address\n"
213 + HelpExampleCli("getrawchangeaddress", "")
214 + HelpExampleRpc("getrawchangeaddress", "")
217 LOCK2(cs_main
, pwallet
->cs_wallet
);
219 if (!pwallet
->IsLocked()) {
220 pwallet
->TopUpKeyPool();
223 CReserveKey
reservekey(pwallet
);
225 if (!reservekey
.GetReservedKey(vchPubKey
, true))
226 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT
, "Error: Keypool ran out, please call keypoolrefill first");
228 reservekey
.KeepKey();
230 CKeyID keyID
= vchPubKey
.GetID();
232 return CBitcoinAddress(keyID
).ToString();
236 UniValue
setaccount(const JSONRPCRequest
& request
)
238 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
239 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
243 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2)
244 throw std::runtime_error(
245 "setaccount \"address\" \"account\"\n"
246 "\nDEPRECATED. Sets the account associated with the given address.\n"
248 "1. \"address\" (string, required) The bitcoin address to be associated with an account.\n"
249 "2. \"account\" (string, required) The account to assign the address to.\n"
251 + HelpExampleCli("setaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"tabby\"")
252 + HelpExampleRpc("setaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"tabby\"")
255 LOCK2(cs_main
, pwallet
->cs_wallet
);
257 CBitcoinAddress
address(request
.params
[0].get_str());
258 if (!address
.IsValid())
259 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid Bitcoin address");
261 std::string strAccount
;
262 if (request
.params
.size() > 1)
263 strAccount
= AccountFromValue(request
.params
[1]);
265 // Only add the account if the address is yours.
266 if (IsMine(*pwallet
, address
.Get())) {
267 // Detect when changing the account of an address that is the 'unused current key' of another account:
268 if (pwallet
->mapAddressBook
.count(address
.Get())) {
269 std::string strOldAccount
= pwallet
->mapAddressBook
[address
.Get()].name
;
270 if (address
== GetAccountAddress(pwallet
, strOldAccount
)) {
271 GetAccountAddress(pwallet
, strOldAccount
, true);
274 pwallet
->SetAddressBook(address
.Get(), strAccount
, "receive");
277 throw JSONRPCError(RPC_MISC_ERROR
, "setaccount can only be used with own address");
283 UniValue
getaccount(const JSONRPCRequest
& request
)
285 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
286 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
290 if (request
.fHelp
|| request
.params
.size() != 1)
291 throw std::runtime_error(
292 "getaccount \"address\"\n"
293 "\nDEPRECATED. Returns the account associated with the given address.\n"
295 "1. \"address\" (string, required) The bitcoin address for account lookup.\n"
297 "\"accountname\" (string) the account address\n"
299 + HelpExampleCli("getaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\"")
300 + HelpExampleRpc("getaccount", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\"")
303 LOCK2(cs_main
, pwallet
->cs_wallet
);
305 CBitcoinAddress
address(request
.params
[0].get_str());
306 if (!address
.IsValid())
307 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid Bitcoin address");
309 std::string strAccount
;
310 std::map
<CTxDestination
, CAddressBookData
>::iterator mi
= pwallet
->mapAddressBook
.find(address
.Get());
311 if (mi
!= pwallet
->mapAddressBook
.end() && !(*mi
).second
.name
.empty()) {
312 strAccount
= (*mi
).second
.name
;
318 UniValue
getaddressesbyaccount(const JSONRPCRequest
& request
)
320 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
321 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
325 if (request
.fHelp
|| request
.params
.size() != 1)
326 throw std::runtime_error(
327 "getaddressesbyaccount \"account\"\n"
328 "\nDEPRECATED. Returns the list of addresses for the given account.\n"
330 "1. \"account\" (string, required) The account name.\n"
332 "[ (json array of string)\n"
333 " \"address\" (string) a bitcoin address associated with the given account\n"
337 + HelpExampleCli("getaddressesbyaccount", "\"tabby\"")
338 + HelpExampleRpc("getaddressesbyaccount", "\"tabby\"")
341 LOCK2(cs_main
, pwallet
->cs_wallet
);
343 std::string strAccount
= AccountFromValue(request
.params
[0]);
345 // Find all addresses that have the given account
346 UniValue
ret(UniValue::VARR
);
347 for (const std::pair
<CBitcoinAddress
, CAddressBookData
>& item
: pwallet
->mapAddressBook
) {
348 const CBitcoinAddress
& address
= item
.first
;
349 const std::string
& strName
= item
.second
.name
;
350 if (strName
== strAccount
)
351 ret
.push_back(address
.ToString());
356 static void SendMoney(CWallet
* const pwallet
, const CTxDestination
&address
, CAmount nValue
, bool fSubtractFeeFromAmount
, CWalletTx
& wtxNew
)
358 CAmount curBalance
= pwallet
->GetBalance();
362 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid amount");
364 if (nValue
> curBalance
)
365 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS
, "Insufficient funds");
367 if (pwallet
->GetBroadcastTransactions() && !g_connman
) {
368 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED
, "Error: Peer-to-peer functionality missing or disabled");
371 // Parse Bitcoin address
372 CScript scriptPubKey
= GetScriptForDestination(address
);
374 // Create and send the transaction
375 CReserveKey
reservekey(pwallet
);
376 CAmount nFeeRequired
;
377 std::string strError
;
378 std::vector
<CRecipient
> vecSend
;
379 int nChangePosRet
= -1;
380 CRecipient recipient
= {scriptPubKey
, nValue
, fSubtractFeeFromAmount
};
381 vecSend
.push_back(recipient
);
382 if (!pwallet
->CreateTransaction(vecSend
, wtxNew
, reservekey
, nFeeRequired
, nChangePosRet
, strError
)) {
383 if (!fSubtractFeeFromAmount
&& nValue
+ nFeeRequired
> curBalance
)
384 strError
= strprintf("Error: This transaction requires a transaction fee of at least %s", FormatMoney(nFeeRequired
));
385 throw JSONRPCError(RPC_WALLET_ERROR
, strError
);
387 CValidationState state
;
388 if (!pwallet
->CommitTransaction(wtxNew
, reservekey
, g_connman
.get(), state
)) {
389 strError
= strprintf("Error: The transaction was rejected! Reason given: %s", state
.GetRejectReason());
390 throw JSONRPCError(RPC_WALLET_ERROR
, strError
);
394 UniValue
sendtoaddress(const JSONRPCRequest
& request
)
396 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
397 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
401 if (request
.fHelp
|| request
.params
.size() < 2 || request
.params
.size() > 5)
402 throw std::runtime_error(
403 "sendtoaddress \"address\" amount ( \"comment\" \"comment_to\" subtractfeefromamount )\n"
404 "\nSend an amount to a given address.\n"
405 + HelpRequiringPassphrase(pwallet
) +
407 "1. \"address\" (string, required) The bitcoin address to send to.\n"
408 "2. \"amount\" (numeric or string, required) The amount in " + CURRENCY_UNIT
+ " to send. eg 0.1\n"
409 "3. \"comment\" (string, optional) A comment used to store what the transaction is for. \n"
410 " This is not part of the transaction, just kept in your wallet.\n"
411 "4. \"comment_to\" (string, optional) A comment to store the name of the person or organization \n"
412 " to which you're sending the transaction. This is not part of the \n"
413 " transaction, just kept in your wallet.\n"
414 "5. subtractfeefromamount (boolean, optional, default=false) The fee will be deducted from the amount being sent.\n"
415 " The recipient will receive less bitcoins than you enter in the amount field.\n"
417 "\"txid\" (string) The transaction id.\n"
419 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.1")
420 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.1 \"donation\" \"seans outpost\"")
421 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.1 \"\" \"\" true")
422 + HelpExampleRpc("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\", 0.1, \"donation\", \"seans outpost\"")
425 LOCK2(cs_main
, pwallet
->cs_wallet
);
427 CBitcoinAddress
address(request
.params
[0].get_str());
428 if (!address
.IsValid())
429 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid Bitcoin address");
432 CAmount nAmount
= AmountFromValue(request
.params
[1]);
434 throw JSONRPCError(RPC_TYPE_ERROR
, "Invalid amount for send");
438 if (request
.params
.size() > 2 && !request
.params
[2].isNull() && !request
.params
[2].get_str().empty())
439 wtx
.mapValue
["comment"] = request
.params
[2].get_str();
440 if (request
.params
.size() > 3 && !request
.params
[3].isNull() && !request
.params
[3].get_str().empty())
441 wtx
.mapValue
["to"] = request
.params
[3].get_str();
443 bool fSubtractFeeFromAmount
= false;
444 if (request
.params
.size() > 4)
445 fSubtractFeeFromAmount
= request
.params
[4].get_bool();
447 EnsureWalletIsUnlocked(pwallet
);
449 SendMoney(pwallet
, address
.Get(), nAmount
, fSubtractFeeFromAmount
, wtx
);
451 return wtx
.GetHash().GetHex();
454 UniValue
listaddressgroupings(const JSONRPCRequest
& request
)
456 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
457 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
462 throw std::runtime_error(
463 "listaddressgroupings\n"
464 "\nLists groups of addresses which have had their common ownership\n"
465 "made public by common use as inputs or as the resulting change\n"
466 "in past transactions\n"
471 " \"address\", (string) The bitcoin address\n"
472 " amount, (numeric) The amount in " + CURRENCY_UNIT
+ "\n"
473 " \"account\" (string, optional) DEPRECATED. The account\n"
480 + HelpExampleCli("listaddressgroupings", "")
481 + HelpExampleRpc("listaddressgroupings", "")
484 LOCK2(cs_main
, pwallet
->cs_wallet
);
486 UniValue
jsonGroupings(UniValue::VARR
);
487 std::map
<CTxDestination
, CAmount
> balances
= pwallet
->GetAddressBalances();
488 for (std::set
<CTxDestination
> grouping
: pwallet
->GetAddressGroupings()) {
489 UniValue
jsonGrouping(UniValue::VARR
);
490 BOOST_FOREACH(CTxDestination address
, grouping
)
492 UniValue
addressInfo(UniValue::VARR
);
493 addressInfo
.push_back(CBitcoinAddress(address
).ToString());
494 addressInfo
.push_back(ValueFromAmount(balances
[address
]));
496 if (pwallet
->mapAddressBook
.find(CBitcoinAddress(address
).Get()) != pwallet
->mapAddressBook
.end()) {
497 addressInfo
.push_back(pwallet
->mapAddressBook
.find(CBitcoinAddress(address
).Get())->second
.name
);
500 jsonGrouping
.push_back(addressInfo
);
502 jsonGroupings
.push_back(jsonGrouping
);
504 return jsonGroupings
;
507 UniValue
signmessage(const JSONRPCRequest
& request
)
509 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
510 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
514 if (request
.fHelp
|| request
.params
.size() != 2)
515 throw std::runtime_error(
516 "signmessage \"address\" \"message\"\n"
517 "\nSign a message with the private key of an address"
518 + HelpRequiringPassphrase(pwallet
) + "\n"
520 "1. \"address\" (string, required) The bitcoin address to use for the private key.\n"
521 "2. \"message\" (string, required) The message to create a signature of.\n"
523 "\"signature\" (string) The signature of the message encoded in base 64\n"
525 "\nUnlock the wallet for 30 seconds\n"
526 + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
527 "\nCreate the signature\n"
528 + HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
529 "\nVerify the signature\n"
530 + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
532 + HelpExampleRpc("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"my message\"")
535 LOCK2(cs_main
, pwallet
->cs_wallet
);
537 EnsureWalletIsUnlocked(pwallet
);
539 std::string strAddress
= request
.params
[0].get_str();
540 std::string strMessage
= request
.params
[1].get_str();
542 CBitcoinAddress
addr(strAddress
);
544 throw JSONRPCError(RPC_TYPE_ERROR
, "Invalid address");
547 if (!addr
.GetKeyID(keyID
))
548 throw JSONRPCError(RPC_TYPE_ERROR
, "Address does not refer to key");
551 if (!pwallet
->GetKey(keyID
, key
)) {
552 throw JSONRPCError(RPC_WALLET_ERROR
, "Private key not available");
555 CHashWriter
ss(SER_GETHASH
, 0);
556 ss
<< strMessageMagic
;
559 std::vector
<unsigned char> vchSig
;
560 if (!key
.SignCompact(ss
.GetHash(), vchSig
))
561 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Sign failed");
563 return EncodeBase64(&vchSig
[0], vchSig
.size());
566 UniValue
getreceivedbyaddress(const JSONRPCRequest
& request
)
568 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
569 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
573 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2)
574 throw std::runtime_error(
575 "getreceivedbyaddress \"address\" ( minconf )\n"
576 "\nReturns the total amount received by the given address in transactions with at least minconf confirmations.\n"
578 "1. \"address\" (string, required) The bitcoin address for transactions.\n"
579 "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n"
581 "amount (numeric) The total amount in " + CURRENCY_UNIT
+ " received at this address.\n"
583 "\nThe amount from transactions with at least 1 confirmation\n"
584 + HelpExampleCli("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\"") +
585 "\nThe amount including unconfirmed transactions, zero confirmations\n"
586 + HelpExampleCli("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" 0") +
587 "\nThe amount with at least 6 confirmation, very safe\n"
588 + HelpExampleCli("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" 6") +
589 "\nAs a json rpc call\n"
590 + HelpExampleRpc("getreceivedbyaddress", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", 6")
593 LOCK2(cs_main
, pwallet
->cs_wallet
);
596 CBitcoinAddress address
= CBitcoinAddress(request
.params
[0].get_str());
597 if (!address
.IsValid())
598 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid Bitcoin address");
599 CScript scriptPubKey
= GetScriptForDestination(address
.Get());
600 if (!IsMine(*pwallet
, scriptPubKey
)) {
601 return ValueFromAmount(0);
604 // Minimum confirmations
606 if (request
.params
.size() > 1)
607 nMinDepth
= request
.params
[1].get_int();
611 for (const std::pair
<uint256
, CWalletTx
>& pairWtx
: pwallet
->mapWallet
) {
612 const CWalletTx
& wtx
= pairWtx
.second
;
613 if (wtx
.IsCoinBase() || !CheckFinalTx(*wtx
.tx
))
616 BOOST_FOREACH(const CTxOut
& txout
, wtx
.tx
->vout
)
617 if (txout
.scriptPubKey
== scriptPubKey
)
618 if (wtx
.GetDepthInMainChain() >= nMinDepth
)
619 nAmount
+= txout
.nValue
;
622 return ValueFromAmount(nAmount
);
626 UniValue
getreceivedbyaccount(const JSONRPCRequest
& request
)
628 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
629 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
633 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2)
634 throw std::runtime_error(
635 "getreceivedbyaccount \"account\" ( minconf )\n"
636 "\nDEPRECATED. Returns the total amount received by addresses with <account> in transactions with at least [minconf] confirmations.\n"
638 "1. \"account\" (string, required) The selected account, may be the default account using \"\".\n"
639 "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n"
641 "amount (numeric) The total amount in " + CURRENCY_UNIT
+ " received for this account.\n"
643 "\nAmount received by the default account with at least 1 confirmation\n"
644 + HelpExampleCli("getreceivedbyaccount", "\"\"") +
645 "\nAmount received at the tabby account including unconfirmed amounts with zero confirmations\n"
646 + HelpExampleCli("getreceivedbyaccount", "\"tabby\" 0") +
647 "\nThe amount with at least 6 confirmation, very safe\n"
648 + HelpExampleCli("getreceivedbyaccount", "\"tabby\" 6") +
649 "\nAs a json rpc call\n"
650 + HelpExampleRpc("getreceivedbyaccount", "\"tabby\", 6")
653 LOCK2(cs_main
, pwallet
->cs_wallet
);
655 // Minimum confirmations
657 if (request
.params
.size() > 1)
658 nMinDepth
= request
.params
[1].get_int();
660 // Get the set of pub keys assigned to account
661 std::string strAccount
= AccountFromValue(request
.params
[0]);
662 std::set
<CTxDestination
> setAddress
= pwallet
->GetAccountAddresses(strAccount
);
666 for (const std::pair
<uint256
, CWalletTx
>& pairWtx
: pwallet
->mapWallet
) {
667 const CWalletTx
& wtx
= pairWtx
.second
;
668 if (wtx
.IsCoinBase() || !CheckFinalTx(*wtx
.tx
))
671 BOOST_FOREACH(const CTxOut
& txout
, wtx
.tx
->vout
)
673 CTxDestination address
;
674 if (ExtractDestination(txout
.scriptPubKey
, address
) && IsMine(*pwallet
, address
) && setAddress
.count(address
)) {
675 if (wtx
.GetDepthInMainChain() >= nMinDepth
)
676 nAmount
+= txout
.nValue
;
681 return ValueFromAmount(nAmount
);
685 UniValue
getbalance(const JSONRPCRequest
& request
)
687 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
688 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
692 if (request
.fHelp
|| request
.params
.size() > 3)
693 throw std::runtime_error(
694 "getbalance ( \"account\" minconf include_watchonly )\n"
695 "\nIf account is not specified, returns the server's total available balance.\n"
696 "If account is specified (DEPRECATED), returns the balance in the account.\n"
697 "Note that the account \"\" is not the same as leaving the parameter out.\n"
698 "The server total may be different to the balance in the default \"\" account.\n"
700 "1. \"account\" (string, optional) DEPRECATED. The account string may be given as a\n"
701 " specific account name to find the balance associated with wallet keys in\n"
702 " a named account, or as the empty string (\"\") to find the balance\n"
703 " associated with wallet keys not in any named account, or as \"*\" to find\n"
704 " the balance associated with all wallet keys regardless of account.\n"
705 " When this option is specified, it calculates the balance in a different\n"
706 " way than when it is not specified, and which can count spends twice when\n"
707 " there are conflicting pending transactions (such as those created by\n"
708 " the bumpfee command), temporarily resulting in low or even negative\n"
709 " balances. In general, account balance calculation is not considered\n"
710 " reliable and has resulted in confusing outcomes, so it is recommended to\n"
711 " avoid passing this argument.\n"
712 "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n"
713 "3. include_watchonly (bool, optional, default=false) Also include balance in watch-only addresses (see 'importaddress')\n"
715 "amount (numeric) The total amount in " + CURRENCY_UNIT
+ " received for this account.\n"
717 "\nThe total amount in the wallet\n"
718 + HelpExampleCli("getbalance", "") +
719 "\nThe total amount in the wallet at least 5 blocks confirmed\n"
720 + HelpExampleCli("getbalance", "\"*\" 6") +
721 "\nAs a json rpc call\n"
722 + HelpExampleRpc("getbalance", "\"*\", 6")
725 LOCK2(cs_main
, pwallet
->cs_wallet
);
727 if (request
.params
.size() == 0)
728 return ValueFromAmount(pwallet
->GetBalance());
731 if (request
.params
.size() > 1)
732 nMinDepth
= request
.params
[1].get_int();
733 isminefilter filter
= ISMINE_SPENDABLE
;
734 if(request
.params
.size() > 2)
735 if(request
.params
[2].get_bool())
736 filter
= filter
| ISMINE_WATCH_ONLY
;
738 if (request
.params
[0].get_str() == "*") {
739 // Calculate total balance in a very different way from GetBalance().
740 // The biggest difference is that GetBalance() sums up all unspent
741 // TxOuts paying to the wallet, while this sums up both spent and
742 // unspent TxOuts paying to the wallet, and then subtracts the values of
743 // TxIns spending from the wallet. This also has fewer restrictions on
744 // which unconfirmed transactions are considered trusted.
745 CAmount nBalance
= 0;
746 for (const std::pair
<uint256
, CWalletTx
>& pairWtx
: pwallet
->mapWallet
) {
747 const CWalletTx
& wtx
= pairWtx
.second
;
748 if (!CheckFinalTx(wtx
) || wtx
.GetBlocksToMaturity() > 0 || wtx
.GetDepthInMainChain() < 0)
752 std::string strSentAccount
;
753 std::list
<COutputEntry
> listReceived
;
754 std::list
<COutputEntry
> listSent
;
755 wtx
.GetAmounts(listReceived
, listSent
, allFee
, strSentAccount
, filter
);
756 if (wtx
.GetDepthInMainChain() >= nMinDepth
)
758 BOOST_FOREACH(const COutputEntry
& r
, listReceived
)
759 nBalance
+= r
.amount
;
761 BOOST_FOREACH(const COutputEntry
& s
, listSent
)
762 nBalance
-= s
.amount
;
765 return ValueFromAmount(nBalance
);
768 std::string strAccount
= AccountFromValue(request
.params
[0]);
770 CAmount nBalance
= pwallet
->GetAccountBalance(strAccount
, nMinDepth
, filter
);
772 return ValueFromAmount(nBalance
);
775 UniValue
getunconfirmedbalance(const JSONRPCRequest
&request
)
777 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
778 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
782 if (request
.fHelp
|| request
.params
.size() > 0)
783 throw std::runtime_error(
784 "getunconfirmedbalance\n"
785 "Returns the server's total unconfirmed balance\n");
787 LOCK2(cs_main
, pwallet
->cs_wallet
);
789 return ValueFromAmount(pwallet
->GetUnconfirmedBalance());
793 UniValue
movecmd(const JSONRPCRequest
& request
)
795 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
796 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
800 if (request
.fHelp
|| request
.params
.size() < 3 || request
.params
.size() > 5)
801 throw std::runtime_error(
802 "move \"fromaccount\" \"toaccount\" amount ( minconf \"comment\" )\n"
803 "\nDEPRECATED. Move a specified amount from one account in your wallet to another.\n"
805 "1. \"fromaccount\" (string, required) The name of the account to move funds from. May be the default account using \"\".\n"
806 "2. \"toaccount\" (string, required) The name of the account to move funds to. May be the default account using \"\".\n"
807 "3. amount (numeric) Quantity of " + CURRENCY_UNIT
+ " to move between accounts.\n"
808 "4. (dummy) (numeric, optional) Ignored. Remains for backward compatibility.\n"
809 "5. \"comment\" (string, optional) An optional comment, stored in the wallet only.\n"
811 "true|false (boolean) true if successful.\n"
813 "\nMove 0.01 " + CURRENCY_UNIT
+ " from the default account to the account named tabby\n"
814 + HelpExampleCli("move", "\"\" \"tabby\" 0.01") +
815 "\nMove 0.01 " + CURRENCY_UNIT
+ " timotei to akiko with a comment and funds have 6 confirmations\n"
816 + HelpExampleCli("move", "\"timotei\" \"akiko\" 0.01 6 \"happy birthday!\"") +
817 "\nAs a json rpc call\n"
818 + HelpExampleRpc("move", "\"timotei\", \"akiko\", 0.01, 6, \"happy birthday!\"")
821 LOCK2(cs_main
, pwallet
->cs_wallet
);
823 std::string strFrom
= AccountFromValue(request
.params
[0]);
824 std::string strTo
= AccountFromValue(request
.params
[1]);
825 CAmount nAmount
= AmountFromValue(request
.params
[2]);
827 throw JSONRPCError(RPC_TYPE_ERROR
, "Invalid amount for send");
828 if (request
.params
.size() > 3)
829 // unused parameter, used to be nMinDepth, keep type-checking it though
830 (void)request
.params
[3].get_int();
831 std::string strComment
;
832 if (request
.params
.size() > 4)
833 strComment
= request
.params
[4].get_str();
835 if (!pwallet
->AccountMove(strFrom
, strTo
, nAmount
, strComment
)) {
836 throw JSONRPCError(RPC_DATABASE_ERROR
, "database error");
843 UniValue
sendfrom(const JSONRPCRequest
& request
)
845 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
846 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
850 if (request
.fHelp
|| request
.params
.size() < 3 || request
.params
.size() > 6)
851 throw std::runtime_error(
852 "sendfrom \"fromaccount\" \"toaddress\" amount ( minconf \"comment\" \"comment_to\" )\n"
853 "\nDEPRECATED (use sendtoaddress). Sent an amount from an account to a bitcoin address."
854 + HelpRequiringPassphrase(pwallet
) + "\n"
856 "1. \"fromaccount\" (string, required) The name of the account to send funds from. May be the default account using \"\".\n"
857 " Specifying an account does not influence coin selection, but it does associate the newly created\n"
858 " transaction with the account, so the account's balance computation and transaction history can reflect\n"
860 "2. \"toaddress\" (string, required) The bitcoin address to send funds to.\n"
861 "3. amount (numeric or string, required) The amount in " + CURRENCY_UNIT
+ " (transaction fee is added on top).\n"
862 "4. minconf (numeric, optional, default=1) Only use funds with at least this many confirmations.\n"
863 "5. \"comment\" (string, optional) A comment used to store what the transaction is for. \n"
864 " This is not part of the transaction, just kept in your wallet.\n"
865 "6. \"comment_to\" (string, optional) An optional comment to store the name of the person or organization \n"
866 " to which you're sending the transaction. This is not part of the transaction, \n"
867 " it is just kept in your wallet.\n"
869 "\"txid\" (string) The transaction id.\n"
871 "\nSend 0.01 " + CURRENCY_UNIT
+ " from the default account to the address, must have at least 1 confirmation\n"
872 + HelpExampleCli("sendfrom", "\"\" \"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.01") +
873 "\nSend 0.01 from the tabby account to the given address, funds must have at least 6 confirmations\n"
874 + HelpExampleCli("sendfrom", "\"tabby\" \"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 0.01 6 \"donation\" \"seans outpost\"") +
875 "\nAs a json rpc call\n"
876 + HelpExampleRpc("sendfrom", "\"tabby\", \"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\", 0.01, 6, \"donation\", \"seans outpost\"")
879 LOCK2(cs_main
, pwallet
->cs_wallet
);
881 std::string strAccount
= AccountFromValue(request
.params
[0]);
882 CBitcoinAddress
address(request
.params
[1].get_str());
883 if (!address
.IsValid())
884 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid Bitcoin address");
885 CAmount nAmount
= AmountFromValue(request
.params
[2]);
887 throw JSONRPCError(RPC_TYPE_ERROR
, "Invalid amount for send");
889 if (request
.params
.size() > 3)
890 nMinDepth
= request
.params
[3].get_int();
893 wtx
.strFromAccount
= strAccount
;
894 if (request
.params
.size() > 4 && !request
.params
[4].isNull() && !request
.params
[4].get_str().empty())
895 wtx
.mapValue
["comment"] = request
.params
[4].get_str();
896 if (request
.params
.size() > 5 && !request
.params
[5].isNull() && !request
.params
[5].get_str().empty())
897 wtx
.mapValue
["to"] = request
.params
[5].get_str();
899 EnsureWalletIsUnlocked(pwallet
);
902 CAmount nBalance
= pwallet
->GetAccountBalance(strAccount
, nMinDepth
, ISMINE_SPENDABLE
);
903 if (nAmount
> nBalance
)
904 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS
, "Account has insufficient funds");
906 SendMoney(pwallet
, address
.Get(), nAmount
, false, wtx
);
908 return wtx
.GetHash().GetHex();
912 UniValue
sendmany(const JSONRPCRequest
& request
)
914 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
915 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
919 if (request
.fHelp
|| request
.params
.size() < 2 || request
.params
.size() > 5)
920 throw std::runtime_error(
921 "sendmany \"fromaccount\" {\"address\":amount,...} ( minconf \"comment\" [\"address\",...] )\n"
922 "\nSend multiple times. Amounts are double-precision floating point numbers."
923 + HelpRequiringPassphrase(pwallet
) + "\n"
925 "1. \"fromaccount\" (string, required) DEPRECATED. The account to send the funds from. Should be \"\" for the default account\n"
926 "2. \"amounts\" (string, required) A json object with addresses and amounts\n"
928 " \"address\":amount (numeric or string) The bitcoin address is the key, the numeric amount (can be string) in " + CURRENCY_UNIT
+ " is the value\n"
931 "3. minconf (numeric, optional, default=1) Only use the balance confirmed at least this many times.\n"
932 "4. \"comment\" (string, optional) A comment\n"
933 "5. subtractfeefrom (array, optional) A json array with addresses.\n"
934 " The fee will be equally deducted from the amount of each selected address.\n"
935 " Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n"
936 " If no addresses are specified here, the sender pays the fee.\n"
938 " \"address\" (string) Subtract fee from this address\n"
942 "\"txid\" (string) The transaction id for the send. Only 1 transaction is created regardless of \n"
943 " the number of addresses.\n"
945 "\nSend two amounts to two different addresses:\n"
946 + HelpExampleCli("sendmany", "\"\" \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\"") +
947 "\nSend two amounts to two different addresses setting the confirmation and comment:\n"
948 + HelpExampleCli("sendmany", "\"\" \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\" 6 \"testing\"") +
949 "\nSend two amounts to two different addresses, subtract fee from amount:\n"
950 + HelpExampleCli("sendmany", "\"\" \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\" 1 \"\" \"[\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\",\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\"]\"") +
951 "\nAs a json rpc call\n"
952 + HelpExampleRpc("sendmany", "\"\", \"{\\\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\\\":0.01,\\\"1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz\\\":0.02}\", 6, \"testing\"")
955 LOCK2(cs_main
, pwallet
->cs_wallet
);
957 if (pwallet
->GetBroadcastTransactions() && !g_connman
) {
958 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED
, "Error: Peer-to-peer functionality missing or disabled");
961 std::string strAccount
= AccountFromValue(request
.params
[0]);
962 UniValue sendTo
= request
.params
[1].get_obj();
964 if (request
.params
.size() > 2)
965 nMinDepth
= request
.params
[2].get_int();
968 wtx
.strFromAccount
= strAccount
;
969 if (request
.params
.size() > 3 && !request
.params
[3].isNull() && !request
.params
[3].get_str().empty())
970 wtx
.mapValue
["comment"] = request
.params
[3].get_str();
972 UniValue
subtractFeeFromAmount(UniValue::VARR
);
973 if (request
.params
.size() > 4)
974 subtractFeeFromAmount
= request
.params
[4].get_array();
976 std::set
<CBitcoinAddress
> setAddress
;
977 std::vector
<CRecipient
> vecSend
;
979 CAmount totalAmount
= 0;
980 std::vector
<std::string
> keys
= sendTo
.getKeys();
981 BOOST_FOREACH(const std::string
& name_
, keys
)
983 CBitcoinAddress
address(name_
);
984 if (!address
.IsValid())
985 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, std::string("Invalid Bitcoin address: ")+name_
);
987 if (setAddress
.count(address
))
988 throw JSONRPCError(RPC_INVALID_PARAMETER
, std::string("Invalid parameter, duplicated address: ")+name_
);
989 setAddress
.insert(address
);
991 CScript scriptPubKey
= GetScriptForDestination(address
.Get());
992 CAmount nAmount
= AmountFromValue(sendTo
[name_
]);
994 throw JSONRPCError(RPC_TYPE_ERROR
, "Invalid amount for send");
995 totalAmount
+= nAmount
;
997 bool fSubtractFeeFromAmount
= false;
998 for (unsigned int idx
= 0; idx
< subtractFeeFromAmount
.size(); idx
++) {
999 const UniValue
& addr
= subtractFeeFromAmount
[idx
];
1000 if (addr
.get_str() == name_
)
1001 fSubtractFeeFromAmount
= true;
1004 CRecipient recipient
= {scriptPubKey
, nAmount
, fSubtractFeeFromAmount
};
1005 vecSend
.push_back(recipient
);
1008 EnsureWalletIsUnlocked(pwallet
);
1011 CAmount nBalance
= pwallet
->GetAccountBalance(strAccount
, nMinDepth
, ISMINE_SPENDABLE
);
1012 if (totalAmount
> nBalance
)
1013 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS
, "Account has insufficient funds");
1016 CReserveKey
keyChange(pwallet
);
1017 CAmount nFeeRequired
= 0;
1018 int nChangePosRet
= -1;
1019 std::string strFailReason
;
1020 bool fCreated
= pwallet
->CreateTransaction(vecSend
, wtx
, keyChange
, nFeeRequired
, nChangePosRet
, strFailReason
);
1022 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS
, strFailReason
);
1023 CValidationState state
;
1024 if (!pwallet
->CommitTransaction(wtx
, keyChange
, g_connman
.get(), state
)) {
1025 strFailReason
= strprintf("Transaction commit failed:: %s", state
.GetRejectReason());
1026 throw JSONRPCError(RPC_WALLET_ERROR
, strFailReason
);
1029 return wtx
.GetHash().GetHex();
1032 // Defined in rpc/misc.cpp
1033 extern CScript
_createmultisig_redeemScript(CWallet
* const pwallet
, const UniValue
& params
);
1035 UniValue
addmultisigaddress(const JSONRPCRequest
& request
)
1037 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1038 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1039 return NullUniValue
;
1042 if (request
.fHelp
|| request
.params
.size() < 2 || request
.params
.size() > 3)
1044 std::string msg
= "addmultisigaddress nrequired [\"key\",...] ( \"account\" )\n"
1045 "\nAdd a nrequired-to-sign multisignature address to the wallet.\n"
1046 "Each key is a Bitcoin address or hex-encoded public key.\n"
1047 "If 'account' is specified (DEPRECATED), assign address to that account.\n"
1050 "1. nrequired (numeric, required) The number of required signatures out of the n keys or addresses.\n"
1051 "2. \"keys\" (string, required) A json array of bitcoin addresses or hex-encoded public keys\n"
1053 " \"address\" (string) bitcoin address or hex-encoded public key\n"
1056 "3. \"account\" (string, optional) DEPRECATED. An account to assign the addresses to.\n"
1059 "\"address\" (string) A bitcoin address associated with the keys.\n"
1062 "\nAdd a multisig address from 2 addresses\n"
1063 + HelpExampleCli("addmultisigaddress", "2 \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"") +
1064 "\nAs json rpc call\n"
1065 + HelpExampleRpc("addmultisigaddress", "2, \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"")
1067 throw std::runtime_error(msg
);
1070 LOCK2(cs_main
, pwallet
->cs_wallet
);
1072 std::string strAccount
;
1073 if (request
.params
.size() > 2)
1074 strAccount
= AccountFromValue(request
.params
[2]);
1076 // Construct using pay-to-script-hash:
1077 CScript inner
= _createmultisig_redeemScript(pwallet
, request
.params
);
1078 CScriptID
innerID(inner
);
1079 pwallet
->AddCScript(inner
);
1081 pwallet
->SetAddressBook(innerID
, strAccount
, "send");
1082 return CBitcoinAddress(innerID
).ToString();
1085 class Witnessifier
: public boost::static_visitor
<bool>
1088 CWallet
* const pwallet
;
1091 Witnessifier(CWallet
*_pwallet
) : pwallet(_pwallet
) {}
1093 bool operator()(const CNoDestination
&dest
) const { return false; }
1095 bool operator()(const CKeyID
&keyID
) {
1098 CScript basescript
= GetScriptForDestination(keyID
);
1100 typ
= IsMine(*pwallet
, basescript
, SIGVERSION_WITNESS_V0
);
1101 if (typ
!= ISMINE_SPENDABLE
&& typ
!= ISMINE_WATCH_SOLVABLE
)
1103 CScript witscript
= GetScriptForWitness(basescript
);
1104 pwallet
->AddCScript(witscript
);
1105 result
= CScriptID(witscript
);
1111 bool operator()(const CScriptID
&scriptID
) {
1113 if (pwallet
&& pwallet
->GetCScript(scriptID
, subscript
)) {
1115 std::vector
<unsigned char> witprog
;
1116 if (subscript
.IsWitnessProgram(witnessversion
, witprog
)) {
1121 typ
= IsMine(*pwallet
, subscript
, SIGVERSION_WITNESS_V0
);
1122 if (typ
!= ISMINE_SPENDABLE
&& typ
!= ISMINE_WATCH_SOLVABLE
)
1124 CScript witscript
= GetScriptForWitness(subscript
);
1125 pwallet
->AddCScript(witscript
);
1126 result
= CScriptID(witscript
);
1133 UniValue
addwitnessaddress(const JSONRPCRequest
& request
)
1135 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1136 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1137 return NullUniValue
;
1140 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 1)
1142 std::string msg
= "addwitnessaddress \"address\"\n"
1143 "\nAdd a witness address for a script (with pubkey or redeemscript known).\n"
1144 "It returns the witness script.\n"
1147 "1. \"address\" (string, required) An address known to the wallet\n"
1150 "\"witnessaddress\", (string) The value of the new address (P2SH of witness script).\n"
1153 throw std::runtime_error(msg
);
1158 if (!IsWitnessEnabled(chainActive
.Tip(), Params().GetConsensus()) && !GetBoolArg("-walletprematurewitness", false)) {
1159 throw JSONRPCError(RPC_WALLET_ERROR
, "Segregated witness not enabled on network");
1163 CBitcoinAddress
address(request
.params
[0].get_str());
1164 if (!address
.IsValid())
1165 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid Bitcoin address");
1167 Witnessifier
w(pwallet
);
1168 CTxDestination dest
= address
.Get();
1169 bool ret
= boost::apply_visitor(w
, dest
);
1171 throw JSONRPCError(RPC_WALLET_ERROR
, "Public key or redeemscript not known to wallet, or the key is uncompressed");
1174 pwallet
->SetAddressBook(w
.result
, "", "receive");
1176 return CBitcoinAddress(w
.result
).ToString();
1183 std::vector
<uint256
> txids
;
1188 nConf
= std::numeric_limits
<int>::max();
1189 fIsWatchonly
= false;
1193 UniValue
ListReceived(CWallet
* const pwallet
, const UniValue
& params
, bool fByAccounts
)
1195 // Minimum confirmations
1197 if (params
.size() > 0)
1198 nMinDepth
= params
[0].get_int();
1200 // Whether to include empty accounts
1201 bool fIncludeEmpty
= false;
1202 if (params
.size() > 1)
1203 fIncludeEmpty
= params
[1].get_bool();
1205 isminefilter filter
= ISMINE_SPENDABLE
;
1206 if(params
.size() > 2)
1207 if(params
[2].get_bool())
1208 filter
= filter
| ISMINE_WATCH_ONLY
;
1211 std::map
<CBitcoinAddress
, tallyitem
> mapTally
;
1212 for (const std::pair
<uint256
, CWalletTx
>& pairWtx
: pwallet
->mapWallet
) {
1213 const CWalletTx
& wtx
= pairWtx
.second
;
1215 if (wtx
.IsCoinBase() || !CheckFinalTx(*wtx
.tx
))
1218 int nDepth
= wtx
.GetDepthInMainChain();
1219 if (nDepth
< nMinDepth
)
1222 BOOST_FOREACH(const CTxOut
& txout
, wtx
.tx
->vout
)
1224 CTxDestination address
;
1225 if (!ExtractDestination(txout
.scriptPubKey
, address
))
1228 isminefilter mine
= IsMine(*pwallet
, address
);
1229 if(!(mine
& filter
))
1232 tallyitem
& item
= mapTally
[address
];
1233 item
.nAmount
+= txout
.nValue
;
1234 item
.nConf
= std::min(item
.nConf
, nDepth
);
1235 item
.txids
.push_back(wtx
.GetHash());
1236 if (mine
& ISMINE_WATCH_ONLY
)
1237 item
.fIsWatchonly
= true;
1242 UniValue
ret(UniValue::VARR
);
1243 std::map
<std::string
, tallyitem
> mapAccountTally
;
1244 for (const std::pair
<CBitcoinAddress
, CAddressBookData
>& item
: pwallet
->mapAddressBook
) {
1245 const CBitcoinAddress
& address
= item
.first
;
1246 const std::string
& strAccount
= item
.second
.name
;
1247 std::map
<CBitcoinAddress
, tallyitem
>::iterator it
= mapTally
.find(address
);
1248 if (it
== mapTally
.end() && !fIncludeEmpty
)
1251 CAmount nAmount
= 0;
1252 int nConf
= std::numeric_limits
<int>::max();
1253 bool fIsWatchonly
= false;
1254 if (it
!= mapTally
.end())
1256 nAmount
= (*it
).second
.nAmount
;
1257 nConf
= (*it
).second
.nConf
;
1258 fIsWatchonly
= (*it
).second
.fIsWatchonly
;
1263 tallyitem
& _item
= mapAccountTally
[strAccount
];
1264 _item
.nAmount
+= nAmount
;
1265 _item
.nConf
= std::min(_item
.nConf
, nConf
);
1266 _item
.fIsWatchonly
= fIsWatchonly
;
1270 UniValue
obj(UniValue::VOBJ
);
1272 obj
.push_back(Pair("involvesWatchonly", true));
1273 obj
.push_back(Pair("address", address
.ToString()));
1274 obj
.push_back(Pair("account", strAccount
));
1275 obj
.push_back(Pair("amount", ValueFromAmount(nAmount
)));
1276 obj
.push_back(Pair("confirmations", (nConf
== std::numeric_limits
<int>::max() ? 0 : nConf
)));
1278 obj
.push_back(Pair("label", strAccount
));
1279 UniValue
transactions(UniValue::VARR
);
1280 if (it
!= mapTally
.end())
1282 BOOST_FOREACH(const uint256
& _item
, (*it
).second
.txids
)
1284 transactions
.push_back(_item
.GetHex());
1287 obj
.push_back(Pair("txids", transactions
));
1294 for (std::map
<std::string
, tallyitem
>::iterator it
= mapAccountTally
.begin(); it
!= mapAccountTally
.end(); ++it
)
1296 CAmount nAmount
= (*it
).second
.nAmount
;
1297 int nConf
= (*it
).second
.nConf
;
1298 UniValue
obj(UniValue::VOBJ
);
1299 if((*it
).second
.fIsWatchonly
)
1300 obj
.push_back(Pair("involvesWatchonly", true));
1301 obj
.push_back(Pair("account", (*it
).first
));
1302 obj
.push_back(Pair("amount", ValueFromAmount(nAmount
)));
1303 obj
.push_back(Pair("confirmations", (nConf
== std::numeric_limits
<int>::max() ? 0 : nConf
)));
1311 UniValue
listreceivedbyaddress(const JSONRPCRequest
& request
)
1313 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1314 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1315 return NullUniValue
;
1318 if (request
.fHelp
|| request
.params
.size() > 3)
1319 throw std::runtime_error(
1320 "listreceivedbyaddress ( minconf include_empty include_watchonly)\n"
1321 "\nList balances by receiving address.\n"
1323 "1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included.\n"
1324 "2. include_empty (bool, optional, default=false) Whether to include addresses that haven't received any payments.\n"
1325 "3. include_watchonly (bool, optional, default=false) Whether to include watch-only addresses (see 'importaddress').\n"
1330 " \"involvesWatchonly\" : true, (bool) Only returned if imported addresses were involved in transaction\n"
1331 " \"address\" : \"receivingaddress\", (string) The receiving address\n"
1332 " \"account\" : \"accountname\", (string) DEPRECATED. The account of the receiving address. The default account is \"\".\n"
1333 " \"amount\" : x.xxx, (numeric) The total amount in " + CURRENCY_UNIT
+ " received by the address\n"
1334 " \"confirmations\" : n, (numeric) The number of confirmations of the most recent transaction included\n"
1335 " \"label\" : \"label\", (string) A comment for the address/transaction, if any\n"
1337 " n, (numeric) The ids of transactions received with the address \n"
1345 + HelpExampleCli("listreceivedbyaddress", "")
1346 + HelpExampleCli("listreceivedbyaddress", "6 true")
1347 + HelpExampleRpc("listreceivedbyaddress", "6, true, true")
1350 LOCK2(cs_main
, pwallet
->cs_wallet
);
1352 return ListReceived(pwallet
, request
.params
, false);
1355 UniValue
listreceivedbyaccount(const JSONRPCRequest
& request
)
1357 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1358 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1359 return NullUniValue
;
1362 if (request
.fHelp
|| request
.params
.size() > 3)
1363 throw std::runtime_error(
1364 "listreceivedbyaccount ( minconf include_empty include_watchonly)\n"
1365 "\nDEPRECATED. List balances by account.\n"
1367 "1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included.\n"
1368 "2. include_empty (bool, optional, default=false) Whether to include accounts that haven't received any payments.\n"
1369 "3. include_watchonly (bool, optional, default=false) Whether to include watch-only addresses (see 'importaddress').\n"
1374 " \"involvesWatchonly\" : true, (bool) Only returned if imported addresses were involved in transaction\n"
1375 " \"account\" : \"accountname\", (string) The account name of the receiving account\n"
1376 " \"amount\" : x.xxx, (numeric) The total amount received by addresses with this account\n"
1377 " \"confirmations\" : n, (numeric) The number of confirmations of the most recent transaction included\n"
1378 " \"label\" : \"label\" (string) A comment for the address/transaction, if any\n"
1384 + HelpExampleCli("listreceivedbyaccount", "")
1385 + HelpExampleCli("listreceivedbyaccount", "6 true")
1386 + HelpExampleRpc("listreceivedbyaccount", "6, true, true")
1389 LOCK2(cs_main
, pwallet
->cs_wallet
);
1391 return ListReceived(pwallet
, request
.params
, true);
1394 static void MaybePushAddress(UniValue
& entry
, const CTxDestination
&dest
)
1396 CBitcoinAddress addr
;
1398 entry
.push_back(Pair("address", addr
.ToString()));
1401 void ListTransactions(CWallet
* const pwallet
, const CWalletTx
& wtx
, const std::string
& strAccount
, int nMinDepth
, bool fLong
, UniValue
& ret
, const isminefilter
& filter
)
1404 std::string strSentAccount
;
1405 std::list
<COutputEntry
> listReceived
;
1406 std::list
<COutputEntry
> listSent
;
1408 wtx
.GetAmounts(listReceived
, listSent
, nFee
, strSentAccount
, filter
);
1410 bool fAllAccounts
= (strAccount
== std::string("*"));
1411 bool involvesWatchonly
= wtx
.IsFromMe(ISMINE_WATCH_ONLY
);
1414 if ((!listSent
.empty() || nFee
!= 0) && (fAllAccounts
|| strAccount
== strSentAccount
))
1416 BOOST_FOREACH(const COutputEntry
& s
, listSent
)
1418 UniValue
entry(UniValue::VOBJ
);
1419 if (involvesWatchonly
|| (::IsMine(*pwallet
, s
.destination
) & ISMINE_WATCH_ONLY
)) {
1420 entry
.push_back(Pair("involvesWatchonly", true));
1422 entry
.push_back(Pair("account", strSentAccount
));
1423 MaybePushAddress(entry
, s
.destination
);
1424 entry
.push_back(Pair("category", "send"));
1425 entry
.push_back(Pair("amount", ValueFromAmount(-s
.amount
)));
1426 if (pwallet
->mapAddressBook
.count(s
.destination
)) {
1427 entry
.push_back(Pair("label", pwallet
->mapAddressBook
[s
.destination
].name
));
1429 entry
.push_back(Pair("vout", s
.vout
));
1430 entry
.push_back(Pair("fee", ValueFromAmount(-nFee
)));
1432 WalletTxToJSON(wtx
, entry
);
1433 entry
.push_back(Pair("abandoned", wtx
.isAbandoned()));
1434 ret
.push_back(entry
);
1439 if (listReceived
.size() > 0 && wtx
.GetDepthInMainChain() >= nMinDepth
)
1441 BOOST_FOREACH(const COutputEntry
& r
, listReceived
)
1443 std::string account
;
1444 if (pwallet
->mapAddressBook
.count(r
.destination
)) {
1445 account
= pwallet
->mapAddressBook
[r
.destination
].name
;
1447 if (fAllAccounts
|| (account
== strAccount
))
1449 UniValue
entry(UniValue::VOBJ
);
1450 if (involvesWatchonly
|| (::IsMine(*pwallet
, r
.destination
) & ISMINE_WATCH_ONLY
)) {
1451 entry
.push_back(Pair("involvesWatchonly", true));
1453 entry
.push_back(Pair("account", account
));
1454 MaybePushAddress(entry
, r
.destination
);
1455 if (wtx
.IsCoinBase())
1457 if (wtx
.GetDepthInMainChain() < 1)
1458 entry
.push_back(Pair("category", "orphan"));
1459 else if (wtx
.GetBlocksToMaturity() > 0)
1460 entry
.push_back(Pair("category", "immature"));
1462 entry
.push_back(Pair("category", "generate"));
1466 entry
.push_back(Pair("category", "receive"));
1468 entry
.push_back(Pair("amount", ValueFromAmount(r
.amount
)));
1469 if (pwallet
->mapAddressBook
.count(r
.destination
)) {
1470 entry
.push_back(Pair("label", account
));
1472 entry
.push_back(Pair("vout", r
.vout
));
1474 WalletTxToJSON(wtx
, entry
);
1475 ret
.push_back(entry
);
1481 void AcentryToJSON(const CAccountingEntry
& acentry
, const std::string
& strAccount
, UniValue
& ret
)
1483 bool fAllAccounts
= (strAccount
== std::string("*"));
1485 if (fAllAccounts
|| acentry
.strAccount
== strAccount
)
1487 UniValue
entry(UniValue::VOBJ
);
1488 entry
.push_back(Pair("account", acentry
.strAccount
));
1489 entry
.push_back(Pair("category", "move"));
1490 entry
.push_back(Pair("time", acentry
.nTime
));
1491 entry
.push_back(Pair("amount", ValueFromAmount(acentry
.nCreditDebit
)));
1492 entry
.push_back(Pair("otheraccount", acentry
.strOtherAccount
));
1493 entry
.push_back(Pair("comment", acentry
.strComment
));
1494 ret
.push_back(entry
);
1498 UniValue
listtransactions(const JSONRPCRequest
& request
)
1500 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1501 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1502 return NullUniValue
;
1505 if (request
.fHelp
|| request
.params
.size() > 4)
1506 throw std::runtime_error(
1507 "listtransactions ( \"account\" count skip include_watchonly)\n"
1508 "\nReturns up to 'count' most recent transactions skipping the first 'from' transactions for account 'account'.\n"
1510 "1. \"account\" (string, optional) DEPRECATED. The account name. Should be \"*\".\n"
1511 "2. count (numeric, optional, default=10) The number of transactions to return\n"
1512 "3. skip (numeric, optional, default=0) The number of transactions to skip\n"
1513 "4. include_watchonly (bool, optional, default=false) Include transactions to watch-only addresses (see 'importaddress')\n"
1517 " \"account\":\"accountname\", (string) DEPRECATED. The account name associated with the transaction. \n"
1518 " It will be \"\" for the default account.\n"
1519 " \"address\":\"address\", (string) The bitcoin address of the transaction. Not present for \n"
1520 " move transactions (category = move).\n"
1521 " \"category\":\"send|receive|move\", (string) The transaction category. 'move' is a local (off blockchain)\n"
1522 " transaction between accounts, and not associated with an address,\n"
1523 " transaction id or block. 'send' and 'receive' transactions are \n"
1524 " associated with an address, transaction id and block details\n"
1525 " \"amount\": x.xxx, (numeric) The amount in " + CURRENCY_UNIT
+ ". This is negative for the 'send' category, and for the\n"
1526 " 'move' category for moves outbound. It is positive for the 'receive' category,\n"
1527 " and for the 'move' category for inbound funds.\n"
1528 " \"label\": \"label\", (string) A comment for the address/transaction, if any\n"
1529 " \"vout\": n, (numeric) the vout value\n"
1530 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT
+ ". This is negative and only available for the \n"
1531 " 'send' category of transactions.\n"
1532 " \"confirmations\": n, (numeric) The number of confirmations for the transaction. Available for 'send' and \n"
1533 " 'receive' category of transactions. Negative confirmations indicate the\n"
1534 " transaction conflicts with the block chain\n"
1535 " \"trusted\": xxx, (bool) Whether we consider the outputs of this unconfirmed transaction safe to spend.\n"
1536 " \"blockhash\": \"hashvalue\", (string) The block hash containing the transaction. Available for 'send' and 'receive'\n"
1537 " category of transactions.\n"
1538 " \"blockindex\": n, (numeric) The index of the transaction in the block that includes it. Available for 'send' and 'receive'\n"
1539 " category of transactions.\n"
1540 " \"blocktime\": xxx, (numeric) The block time in seconds since epoch (1 Jan 1970 GMT).\n"
1541 " \"txid\": \"transactionid\", (string) The transaction id. Available for 'send' and 'receive' category of transactions.\n"
1542 " \"time\": xxx, (numeric) The transaction time in seconds since epoch (midnight Jan 1 1970 GMT).\n"
1543 " \"timereceived\": xxx, (numeric) The time received in seconds since epoch (midnight Jan 1 1970 GMT). Available \n"
1544 " for 'send' and 'receive' category of transactions.\n"
1545 " \"comment\": \"...\", (string) If a comment is associated with the transaction.\n"
1546 " \"otheraccount\": \"accountname\", (string) DEPRECATED. For the 'move' category of transactions, the account the funds came \n"
1547 " from (for receiving funds, positive amounts), or went to (for sending funds,\n"
1548 " negative amounts).\n"
1549 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1550 " may be unknown for unconfirmed transactions not in the mempool\n"
1551 " \"abandoned\": xxx (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
1552 " 'send' category of transactions.\n"
1557 "\nList the most recent 10 transactions in the systems\n"
1558 + HelpExampleCli("listtransactions", "") +
1559 "\nList transactions 100 to 120\n"
1560 + HelpExampleCli("listtransactions", "\"*\" 20 100") +
1561 "\nAs a json rpc call\n"
1562 + HelpExampleRpc("listtransactions", "\"*\", 20, 100")
1565 LOCK2(cs_main
, pwallet
->cs_wallet
);
1567 std::string strAccount
= "*";
1568 if (request
.params
.size() > 0)
1569 strAccount
= request
.params
[0].get_str();
1571 if (request
.params
.size() > 1)
1572 nCount
= request
.params
[1].get_int();
1574 if (request
.params
.size() > 2)
1575 nFrom
= request
.params
[2].get_int();
1576 isminefilter filter
= ISMINE_SPENDABLE
;
1577 if(request
.params
.size() > 3)
1578 if(request
.params
[3].get_bool())
1579 filter
= filter
| ISMINE_WATCH_ONLY
;
1582 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Negative count");
1584 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Negative from");
1586 UniValue
ret(UniValue::VARR
);
1588 const CWallet::TxItems
& txOrdered
= pwallet
->wtxOrdered
;
1590 // iterate backwards until we have nCount items to return:
1591 for (CWallet::TxItems::const_reverse_iterator it
= txOrdered
.rbegin(); it
!= txOrdered
.rend(); ++it
)
1593 CWalletTx
*const pwtx
= (*it
).second
.first
;
1595 ListTransactions(pwallet
, *pwtx
, strAccount
, 0, true, ret
, filter
);
1596 CAccountingEntry
*const pacentry
= (*it
).second
.second
;
1598 AcentryToJSON(*pacentry
, strAccount
, ret
);
1600 if ((int)ret
.size() >= (nCount
+nFrom
)) break;
1602 // ret is newest to oldest
1604 if (nFrom
> (int)ret
.size())
1606 if ((nFrom
+ nCount
) > (int)ret
.size())
1607 nCount
= ret
.size() - nFrom
;
1609 std::vector
<UniValue
> arrTmp
= ret
.getValues();
1611 std::vector
<UniValue
>::iterator first
= arrTmp
.begin();
1612 std::advance(first
, nFrom
);
1613 std::vector
<UniValue
>::iterator last
= arrTmp
.begin();
1614 std::advance(last
, nFrom
+nCount
);
1616 if (last
!= arrTmp
.end()) arrTmp
.erase(last
, arrTmp
.end());
1617 if (first
!= arrTmp
.begin()) arrTmp
.erase(arrTmp
.begin(), first
);
1619 std::reverse(arrTmp
.begin(), arrTmp
.end()); // Return oldest to newest
1623 ret
.push_backV(arrTmp
);
1628 UniValue
listaccounts(const JSONRPCRequest
& request
)
1630 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1631 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1632 return NullUniValue
;
1635 if (request
.fHelp
|| request
.params
.size() > 2)
1636 throw std::runtime_error(
1637 "listaccounts ( minconf include_watchonly)\n"
1638 "\nDEPRECATED. Returns Object that has account names as keys, account balances as values.\n"
1640 "1. minconf (numeric, optional, default=1) Only include transactions with at least this many confirmations\n"
1641 "2. include_watchonly (bool, optional, default=false) Include balances in watch-only addresses (see 'importaddress')\n"
1643 "{ (json object where keys are account names, and values are numeric balances\n"
1644 " \"account\": x.xxx, (numeric) The property name is the account name, and the value is the total balance for the account.\n"
1648 "\nList account balances where there at least 1 confirmation\n"
1649 + HelpExampleCli("listaccounts", "") +
1650 "\nList account balances including zero confirmation transactions\n"
1651 + HelpExampleCli("listaccounts", "0") +
1652 "\nList account balances for 6 or more confirmations\n"
1653 + HelpExampleCli("listaccounts", "6") +
1654 "\nAs json rpc call\n"
1655 + HelpExampleRpc("listaccounts", "6")
1658 LOCK2(cs_main
, pwallet
->cs_wallet
);
1661 if (request
.params
.size() > 0)
1662 nMinDepth
= request
.params
[0].get_int();
1663 isminefilter includeWatchonly
= ISMINE_SPENDABLE
;
1664 if(request
.params
.size() > 1)
1665 if(request
.params
[1].get_bool())
1666 includeWatchonly
= includeWatchonly
| ISMINE_WATCH_ONLY
;
1668 std::map
<std::string
, CAmount
> mapAccountBalances
;
1669 for (const std::pair
<CTxDestination
, CAddressBookData
>& entry
: pwallet
->mapAddressBook
) {
1670 if (IsMine(*pwallet
, entry
.first
) & includeWatchonly
) { // This address belongs to me
1671 mapAccountBalances
[entry
.second
.name
] = 0;
1675 for (const std::pair
<uint256
, CWalletTx
>& pairWtx
: pwallet
->mapWallet
) {
1676 const CWalletTx
& wtx
= pairWtx
.second
;
1678 std::string strSentAccount
;
1679 std::list
<COutputEntry
> listReceived
;
1680 std::list
<COutputEntry
> listSent
;
1681 int nDepth
= wtx
.GetDepthInMainChain();
1682 if (wtx
.GetBlocksToMaturity() > 0 || nDepth
< 0)
1684 wtx
.GetAmounts(listReceived
, listSent
, nFee
, strSentAccount
, includeWatchonly
);
1685 mapAccountBalances
[strSentAccount
] -= nFee
;
1686 BOOST_FOREACH(const COutputEntry
& s
, listSent
)
1687 mapAccountBalances
[strSentAccount
] -= s
.amount
;
1688 if (nDepth
>= nMinDepth
)
1690 BOOST_FOREACH(const COutputEntry
& r
, listReceived
)
1691 if (pwallet
->mapAddressBook
.count(r
.destination
)) {
1692 mapAccountBalances
[pwallet
->mapAddressBook
[r
.destination
].name
] += r
.amount
;
1695 mapAccountBalances
[""] += r
.amount
;
1699 const std::list
<CAccountingEntry
>& acentries
= pwallet
->laccentries
;
1700 BOOST_FOREACH(const CAccountingEntry
& entry
, acentries
)
1701 mapAccountBalances
[entry
.strAccount
] += entry
.nCreditDebit
;
1703 UniValue
ret(UniValue::VOBJ
);
1704 BOOST_FOREACH(const PAIRTYPE(std::string
, CAmount
)& accountBalance
, mapAccountBalances
) {
1705 ret
.push_back(Pair(accountBalance
.first
, ValueFromAmount(accountBalance
.second
)));
1710 UniValue
listsinceblock(const JSONRPCRequest
& request
)
1712 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1713 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1714 return NullUniValue
;
1718 throw std::runtime_error(
1719 "listsinceblock ( \"blockhash\" target_confirmations include_watchonly)\n"
1720 "\nGet all transactions in blocks since block [blockhash], or all transactions if omitted\n"
1722 "1. \"blockhash\" (string, optional) The block hash to list transactions since\n"
1723 "2. target_confirmations: (numeric, optional) The confirmations required, must be 1 or more\n"
1724 "3. include_watchonly: (bool, optional, default=false) Include transactions to watch-only addresses (see 'importaddress')"
1727 " \"transactions\": [\n"
1728 " \"account\":\"accountname\", (string) DEPRECATED. The account name associated with the transaction. Will be \"\" for the default account.\n"
1729 " \"address\":\"address\", (string) The bitcoin address of the transaction. Not present for move transactions (category = move).\n"
1730 " \"category\":\"send|receive\", (string) The transaction category. 'send' has negative amounts, 'receive' has positive amounts.\n"
1731 " \"amount\": x.xxx, (numeric) The amount in " + CURRENCY_UNIT
+ ". This is negative for the 'send' category, and for the 'move' category for moves \n"
1732 " outbound. It is positive for the 'receive' category, and for the 'move' category for inbound funds.\n"
1733 " \"vout\" : n, (numeric) the vout value\n"
1734 " \"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"
1735 " \"confirmations\": n, (numeric) The number of confirmations for the transaction. Available for 'send' and 'receive' category of transactions.\n"
1736 " When it's < 0, it means the transaction conflicted that many blocks ago.\n"
1737 " \"blockhash\": \"hashvalue\", (string) The block hash containing the transaction. Available for 'send' and 'receive' category of transactions.\n"
1738 " \"blockindex\": n, (numeric) The index of the transaction in the block that includes it. Available for 'send' and 'receive' category of transactions.\n"
1739 " \"blocktime\": xxx, (numeric) The block time in seconds since epoch (1 Jan 1970 GMT).\n"
1740 " \"txid\": \"transactionid\", (string) The transaction id. Available for 'send' and 'receive' category of transactions.\n"
1741 " \"time\": xxx, (numeric) The transaction time in seconds since epoch (Jan 1 1970 GMT).\n"
1742 " \"timereceived\": xxx, (numeric) The time received in seconds since epoch (Jan 1 1970 GMT). Available for 'send' and 'receive' category of transactions.\n"
1743 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1744 " may be unknown for unconfirmed transactions not in the mempool\n"
1745 " \"abandoned\": xxx, (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the 'send' category of transactions.\n"
1746 " \"comment\": \"...\", (string) If a comment is associated with the transaction.\n"
1747 " \"label\" : \"label\" (string) A comment for the address/transaction, if any\n"
1748 " \"to\": \"...\", (string) If a comment to is associated with the transaction.\n"
1750 " \"lastblock\": \"lastblockhash\" (string) The hash of the last block\n"
1753 + HelpExampleCli("listsinceblock", "")
1754 + HelpExampleCli("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\" 6")
1755 + HelpExampleRpc("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\", 6")
1758 LOCK2(cs_main
, pwallet
->cs_wallet
);
1760 const CBlockIndex
*pindex
= NULL
;
1761 int target_confirms
= 1;
1762 isminefilter filter
= ISMINE_SPENDABLE
;
1764 if (request
.params
.size() > 0)
1768 blockId
.SetHex(request
.params
[0].get_str());
1769 BlockMap::iterator it
= mapBlockIndex
.find(blockId
);
1770 if (it
!= mapBlockIndex
.end())
1772 pindex
= it
->second
;
1773 if (chainActive
[pindex
->nHeight
] != pindex
)
1775 // the block being asked for is a part of a deactivated chain;
1776 // we don't want to depend on its perceived height in the block
1777 // chain, we want to instead use the last common ancestor
1778 pindex
= chainActive
.FindFork(pindex
);
1783 if (request
.params
.size() > 1)
1785 target_confirms
= request
.params
[1].get_int();
1787 if (target_confirms
< 1)
1788 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter");
1791 if (request
.params
.size() > 2 && request
.params
[2].get_bool())
1793 filter
= filter
| ISMINE_WATCH_ONLY
;
1796 int depth
= pindex
? (1 + chainActive
.Height() - pindex
->nHeight
) : -1;
1798 UniValue
transactions(UniValue::VARR
);
1800 for (const std::pair
<uint256
, CWalletTx
>& pairWtx
: pwallet
->mapWallet
) {
1801 CWalletTx tx
= pairWtx
.second
;
1803 if (depth
== -1 || tx
.GetDepthInMainChain() < depth
)
1804 ListTransactions(pwallet
, tx
, "*", 0, true, transactions
, filter
);
1807 CBlockIndex
*pblockLast
= chainActive
[chainActive
.Height() + 1 - target_confirms
];
1808 uint256 lastblock
= pblockLast
? pblockLast
->GetBlockHash() : uint256();
1810 UniValue
ret(UniValue::VOBJ
);
1811 ret
.push_back(Pair("transactions", transactions
));
1812 ret
.push_back(Pair("lastblock", lastblock
.GetHex()));
1817 UniValue
gettransaction(const JSONRPCRequest
& request
)
1819 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1820 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1821 return NullUniValue
;
1824 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2)
1825 throw std::runtime_error(
1826 "gettransaction \"txid\" ( include_watchonly )\n"
1827 "\nGet detailed information about in-wallet transaction <txid>\n"
1829 "1. \"txid\" (string, required) The transaction id\n"
1830 "2. \"include_watchonly\" (bool, optional, default=false) Whether to include watch-only addresses in balance calculation and details[]\n"
1833 " \"amount\" : x.xxx, (numeric) The transaction amount in " + CURRENCY_UNIT
+ "\n"
1834 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT
+ ". This is negative and only available for the \n"
1835 " 'send' category of transactions.\n"
1836 " \"confirmations\" : n, (numeric) The number of confirmations\n"
1837 " \"blockhash\" : \"hash\", (string) The block hash\n"
1838 " \"blockindex\" : xx, (numeric) The index of the transaction in the block that includes it\n"
1839 " \"blocktime\" : ttt, (numeric) The time in seconds since epoch (1 Jan 1970 GMT)\n"
1840 " \"txid\" : \"transactionid\", (string) The transaction id.\n"
1841 " \"time\" : ttt, (numeric) The transaction time in seconds since epoch (1 Jan 1970 GMT)\n"
1842 " \"timereceived\" : ttt, (numeric) The time received in seconds since epoch (1 Jan 1970 GMT)\n"
1843 " \"bip125-replaceable\": \"yes|no|unknown\", (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee);\n"
1844 " may be unknown for unconfirmed transactions not in the mempool\n"
1845 " \"details\" : [\n"
1847 " \"account\" : \"accountname\", (string) DEPRECATED. The account name involved in the transaction, can be \"\" for the default account.\n"
1848 " \"address\" : \"address\", (string) The bitcoin address involved in the transaction\n"
1849 " \"category\" : \"send|receive\", (string) The category, either 'send' or 'receive'\n"
1850 " \"amount\" : x.xxx, (numeric) The amount in " + CURRENCY_UNIT
+ "\n"
1851 " \"label\" : \"label\", (string) A comment for the address/transaction, if any\n"
1852 " \"vout\" : n, (numeric) the vout value\n"
1853 " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT
+ ". This is negative and only available for the \n"
1854 " 'send' category of transactions.\n"
1855 " \"abandoned\": xxx (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n"
1856 " 'send' category of transactions.\n"
1860 " \"hex\" : \"data\" (string) Raw data for transaction\n"
1864 + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
1865 + HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\" true")
1866 + HelpExampleRpc("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
1869 LOCK2(cs_main
, pwallet
->cs_wallet
);
1872 hash
.SetHex(request
.params
[0].get_str());
1874 isminefilter filter
= ISMINE_SPENDABLE
;
1875 if(request
.params
.size() > 1)
1876 if(request
.params
[1].get_bool())
1877 filter
= filter
| ISMINE_WATCH_ONLY
;
1879 UniValue
entry(UniValue::VOBJ
);
1880 if (!pwallet
->mapWallet
.count(hash
)) {
1881 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid or non-wallet transaction id");
1883 const CWalletTx
& wtx
= pwallet
->mapWallet
[hash
];
1885 CAmount nCredit
= wtx
.GetCredit(filter
);
1886 CAmount nDebit
= wtx
.GetDebit(filter
);
1887 CAmount nNet
= nCredit
- nDebit
;
1888 CAmount nFee
= (wtx
.IsFromMe(filter
) ? wtx
.tx
->GetValueOut() - nDebit
: 0);
1890 entry
.push_back(Pair("amount", ValueFromAmount(nNet
- nFee
)));
1891 if (wtx
.IsFromMe(filter
))
1892 entry
.push_back(Pair("fee", ValueFromAmount(nFee
)));
1894 WalletTxToJSON(wtx
, entry
);
1896 UniValue
details(UniValue::VARR
);
1897 ListTransactions(pwallet
, wtx
, "*", 0, false, details
, filter
);
1898 entry
.push_back(Pair("details", details
));
1900 std::string strHex
= EncodeHexTx(static_cast<CTransaction
>(wtx
), RPCSerializationFlags());
1901 entry
.push_back(Pair("hex", strHex
));
1906 UniValue
abandontransaction(const JSONRPCRequest
& request
)
1908 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1909 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1910 return NullUniValue
;
1913 if (request
.fHelp
|| request
.params
.size() != 1)
1914 throw std::runtime_error(
1915 "abandontransaction \"txid\"\n"
1916 "\nMark in-wallet transaction <txid> as abandoned\n"
1917 "This will mark this transaction and all its in-wallet descendants as abandoned which will allow\n"
1918 "for their inputs to be respent. It can be used to replace \"stuck\" or evicted transactions.\n"
1919 "It only works on transactions which are not included in a block and are not currently in the mempool.\n"
1920 "It has no effect on transactions which are already conflicted or abandoned.\n"
1922 "1. \"txid\" (string, required) The transaction id\n"
1925 + HelpExampleCli("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
1926 + HelpExampleRpc("abandontransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
1929 LOCK2(cs_main
, pwallet
->cs_wallet
);
1932 hash
.SetHex(request
.params
[0].get_str());
1934 if (!pwallet
->mapWallet
.count(hash
)) {
1935 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Invalid or non-wallet transaction id");
1937 if (!pwallet
->AbandonTransaction(hash
)) {
1938 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "Transaction not eligible for abandonment");
1941 return NullUniValue
;
1945 UniValue
backupwallet(const JSONRPCRequest
& request
)
1947 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1948 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1949 return NullUniValue
;
1952 if (request
.fHelp
|| request
.params
.size() != 1)
1953 throw std::runtime_error(
1954 "backupwallet \"destination\"\n"
1955 "\nSafely copies current wallet file to destination, which can be a directory or a path with filename.\n"
1957 "1. \"destination\" (string) The destination directory or file\n"
1959 + HelpExampleCli("backupwallet", "\"backup.dat\"")
1960 + HelpExampleRpc("backupwallet", "\"backup.dat\"")
1963 LOCK2(cs_main
, pwallet
->cs_wallet
);
1965 std::string strDest
= request
.params
[0].get_str();
1966 if (!pwallet
->BackupWallet(strDest
)) {
1967 throw JSONRPCError(RPC_WALLET_ERROR
, "Error: Wallet backup failed!");
1970 return NullUniValue
;
1974 UniValue
keypoolrefill(const JSONRPCRequest
& request
)
1976 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
1977 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
1978 return NullUniValue
;
1981 if (request
.fHelp
|| request
.params
.size() > 1)
1982 throw std::runtime_error(
1983 "keypoolrefill ( newsize )\n"
1984 "\nFills the keypool."
1985 + HelpRequiringPassphrase(pwallet
) + "\n"
1987 "1. newsize (numeric, optional, default=100) The new keypool size\n"
1989 + HelpExampleCli("keypoolrefill", "")
1990 + HelpExampleRpc("keypoolrefill", "")
1993 LOCK2(cs_main
, pwallet
->cs_wallet
);
1995 // 0 is interpreted by TopUpKeyPool() as the default keypool size given by -keypool
1996 unsigned int kpSize
= 0;
1997 if (request
.params
.size() > 0) {
1998 if (request
.params
[0].get_int() < 0)
1999 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter, expected valid size.");
2000 kpSize
= (unsigned int)request
.params
[0].get_int();
2003 EnsureWalletIsUnlocked(pwallet
);
2004 pwallet
->TopUpKeyPool(kpSize
);
2006 if (pwallet
->GetKeyPoolSize() < kpSize
) {
2007 throw JSONRPCError(RPC_WALLET_ERROR
, "Error refreshing keypool.");
2010 return NullUniValue
;
2014 static void LockWallet(CWallet
* pWallet
)
2016 LOCK(pWallet
->cs_wallet
);
2017 pWallet
->nRelockTime
= 0;
2021 UniValue
walletpassphrase(const JSONRPCRequest
& request
)
2023 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2024 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2025 return NullUniValue
;
2028 if (pwallet
->IsCrypted() && (request
.fHelp
|| request
.params
.size() != 2)) {
2029 throw std::runtime_error(
2030 "walletpassphrase \"passphrase\" timeout\n"
2031 "\nStores the wallet decryption key in memory for 'timeout' seconds.\n"
2032 "This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
2034 "1. \"passphrase\" (string, required) The wallet passphrase\n"
2035 "2. timeout (numeric, required) The time to keep the decryption key in seconds.\n"
2037 "Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
2038 "time that overrides the old one.\n"
2040 "\nunlock the wallet for 60 seconds\n"
2041 + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 60") +
2042 "\nLock the wallet again (before 60 seconds)\n"
2043 + HelpExampleCli("walletlock", "") +
2044 "\nAs json rpc call\n"
2045 + HelpExampleRpc("walletpassphrase", "\"my pass phrase\", 60")
2049 LOCK2(cs_main
, pwallet
->cs_wallet
);
2053 if (!pwallet
->IsCrypted()) {
2054 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE
, "Error: running with an unencrypted wallet, but walletpassphrase was called.");
2057 // Note that the walletpassphrase is stored in request.params[0] which is not mlock()ed
2058 SecureString strWalletPass
;
2059 strWalletPass
.reserve(100);
2060 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
2061 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2062 strWalletPass
= request
.params
[0].get_str().c_str();
2064 if (strWalletPass
.length() > 0)
2066 if (!pwallet
->Unlock(strWalletPass
)) {
2067 throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT
, "Error: The wallet passphrase entered was incorrect.");
2071 throw std::runtime_error(
2072 "walletpassphrase <passphrase> <timeout>\n"
2073 "Stores the wallet decryption key in memory for <timeout> seconds.");
2075 pwallet
->TopUpKeyPool();
2077 int64_t nSleepTime
= request
.params
[1].get_int64();
2078 pwallet
->nRelockTime
= GetTime() + nSleepTime
;
2079 RPCRunLater(strprintf("lockwallet(%s)", pwallet
->strWalletFile
), boost::bind(LockWallet
, pwallet
), nSleepTime
);
2081 return NullUniValue
;
2085 UniValue
walletpassphrasechange(const JSONRPCRequest
& request
)
2087 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2088 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2089 return NullUniValue
;
2092 if (pwallet
->IsCrypted() && (request
.fHelp
|| request
.params
.size() != 2)) {
2093 throw std::runtime_error(
2094 "walletpassphrasechange \"oldpassphrase\" \"newpassphrase\"\n"
2095 "\nChanges the wallet passphrase from 'oldpassphrase' to 'newpassphrase'.\n"
2097 "1. \"oldpassphrase\" (string) The current passphrase\n"
2098 "2. \"newpassphrase\" (string) The new passphrase\n"
2100 + HelpExampleCli("walletpassphrasechange", "\"old one\" \"new one\"")
2101 + HelpExampleRpc("walletpassphrasechange", "\"old one\", \"new one\"")
2105 LOCK2(cs_main
, pwallet
->cs_wallet
);
2109 if (!pwallet
->IsCrypted()) {
2110 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE
, "Error: running with an unencrypted wallet, but walletpassphrasechange was called.");
2113 // TODO: get rid of these .c_str() calls by implementing SecureString::operator=(std::string)
2114 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2115 SecureString strOldWalletPass
;
2116 strOldWalletPass
.reserve(100);
2117 strOldWalletPass
= request
.params
[0].get_str().c_str();
2119 SecureString strNewWalletPass
;
2120 strNewWalletPass
.reserve(100);
2121 strNewWalletPass
= request
.params
[1].get_str().c_str();
2123 if (strOldWalletPass
.length() < 1 || strNewWalletPass
.length() < 1)
2124 throw std::runtime_error(
2125 "walletpassphrasechange <oldpassphrase> <newpassphrase>\n"
2126 "Changes the wallet passphrase from <oldpassphrase> to <newpassphrase>.");
2128 if (!pwallet
->ChangeWalletPassphrase(strOldWalletPass
, strNewWalletPass
)) {
2129 throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT
, "Error: The wallet passphrase entered was incorrect.");
2132 return NullUniValue
;
2136 UniValue
walletlock(const JSONRPCRequest
& request
)
2138 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2139 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2140 return NullUniValue
;
2143 if (pwallet
->IsCrypted() && (request
.fHelp
|| request
.params
.size() != 0)) {
2144 throw std::runtime_error(
2146 "\nRemoves the wallet encryption key from memory, locking the wallet.\n"
2147 "After calling this method, you will need to call walletpassphrase again\n"
2148 "before being able to call any methods which require the wallet to be unlocked.\n"
2150 "\nSet the passphrase for 2 minutes to perform a transaction\n"
2151 + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 120") +
2152 "\nPerform a send (requires passphrase set)\n"
2153 + HelpExampleCli("sendtoaddress", "\"1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd\" 1.0") +
2154 "\nClear the passphrase since we are done before 2 minutes is up\n"
2155 + HelpExampleCli("walletlock", "") +
2156 "\nAs json rpc call\n"
2157 + HelpExampleRpc("walletlock", "")
2161 LOCK2(cs_main
, pwallet
->cs_wallet
);
2165 if (!pwallet
->IsCrypted()) {
2166 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE
, "Error: running with an unencrypted wallet, but walletlock was called.");
2170 pwallet
->nRelockTime
= 0;
2172 return NullUniValue
;
2176 UniValue
encryptwallet(const JSONRPCRequest
& request
)
2178 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2179 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2180 return NullUniValue
;
2183 if (!pwallet
->IsCrypted() && (request
.fHelp
|| request
.params
.size() != 1)) {
2184 throw std::runtime_error(
2185 "encryptwallet \"passphrase\"\n"
2186 "\nEncrypts the wallet with 'passphrase'. This is for first time encryption.\n"
2187 "After this, any calls that interact with private keys such as sending or signing \n"
2188 "will require the passphrase to be set prior the making these calls.\n"
2189 "Use the walletpassphrase call for this, and then walletlock call.\n"
2190 "If the wallet is already encrypted, use the walletpassphrasechange call.\n"
2191 "Note that this will shutdown the server.\n"
2193 "1. \"passphrase\" (string) The pass phrase to encrypt the wallet with. It must be at least 1 character, but should be long.\n"
2195 "\nEncrypt you wallet\n"
2196 + HelpExampleCli("encryptwallet", "\"my pass phrase\"") +
2197 "\nNow set the passphrase to use the wallet, such as for signing or sending bitcoin\n"
2198 + HelpExampleCli("walletpassphrase", "\"my pass phrase\"") +
2199 "\nNow we can so something like sign\n"
2200 + HelpExampleCli("signmessage", "\"address\" \"test message\"") +
2201 "\nNow lock the wallet again by removing the passphrase\n"
2202 + HelpExampleCli("walletlock", "") +
2203 "\nAs a json rpc call\n"
2204 + HelpExampleRpc("encryptwallet", "\"my pass phrase\"")
2208 LOCK2(cs_main
, pwallet
->cs_wallet
);
2212 if (pwallet
->IsCrypted()) {
2213 throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE
, "Error: running with an encrypted wallet, but encryptwallet was called.");
2216 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
2217 // Alternately, find a way to make request.params[0] mlock()'d to begin with.
2218 SecureString strWalletPass
;
2219 strWalletPass
.reserve(100);
2220 strWalletPass
= request
.params
[0].get_str().c_str();
2222 if (strWalletPass
.length() < 1)
2223 throw std::runtime_error(
2224 "encryptwallet <passphrase>\n"
2225 "Encrypts the wallet with <passphrase>.");
2227 if (!pwallet
->EncryptWallet(strWalletPass
)) {
2228 throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED
, "Error: Failed to encrypt the wallet.");
2231 // BDB seems to have a bad habit of writing old data into
2232 // slack space in .dat files; that is bad if the old data is
2233 // unencrypted private keys. So:
2235 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.";
2238 UniValue
lockunspent(const JSONRPCRequest
& request
)
2240 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2241 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2242 return NullUniValue
;
2245 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2)
2246 throw std::runtime_error(
2247 "lockunspent unlock ([{\"txid\":\"txid\",\"vout\":n},...])\n"
2248 "\nUpdates list of temporarily unspendable outputs.\n"
2249 "Temporarily lock (unlock=false) or unlock (unlock=true) specified transaction outputs.\n"
2250 "If no transaction outputs are specified when unlocking then all current locked transaction outputs are unlocked.\n"
2251 "A locked transaction output will not be chosen by automatic coin selection, when spending bitcoins.\n"
2252 "Locks are stored in memory only. Nodes start with zero locked outputs, and the locked output list\n"
2253 "is always cleared (by virtue of process exit) when a node stops or fails.\n"
2254 "Also see the listunspent call\n"
2256 "1. unlock (boolean, required) Whether to unlock (true) or lock (false) the specified transactions\n"
2257 "2. \"transactions\" (string, optional) A json array of objects. Each object the txid (string) vout (numeric)\n"
2258 " [ (json array of json objects)\n"
2260 " \"txid\":\"id\", (string) The transaction id\n"
2261 " \"vout\": n (numeric) The output number\n"
2267 "true|false (boolean) Whether the command was successful or not\n"
2270 "\nList the unspent transactions\n"
2271 + HelpExampleCli("listunspent", "") +
2272 "\nLock an unspent transaction\n"
2273 + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2274 "\nList the locked transactions\n"
2275 + HelpExampleCli("listlockunspent", "") +
2276 "\nUnlock the transaction again\n"
2277 + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2278 "\nAs a json rpc call\n"
2279 + HelpExampleRpc("lockunspent", "false, \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"")
2282 LOCK2(cs_main
, pwallet
->cs_wallet
);
2284 if (request
.params
.size() == 1)
2285 RPCTypeCheck(request
.params
, boost::assign::list_of(UniValue::VBOOL
));
2287 RPCTypeCheck(request
.params
, boost::assign::list_of(UniValue::VBOOL
)(UniValue::VARR
));
2289 bool fUnlock
= request
.params
[0].get_bool();
2291 if (request
.params
.size() == 1) {
2293 pwallet
->UnlockAllCoins();
2297 UniValue outputs
= request
.params
[1].get_array();
2298 for (unsigned int idx
= 0; idx
< outputs
.size(); idx
++) {
2299 const UniValue
& output
= outputs
[idx
];
2300 if (!output
.isObject())
2301 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter, expected object");
2302 const UniValue
& o
= output
.get_obj();
2306 {"txid", UniValueType(UniValue::VSTR
)},
2307 {"vout", UniValueType(UniValue::VNUM
)},
2310 std::string txid
= find_value(o
, "txid").get_str();
2312 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter, expected hex txid");
2314 int nOutput
= find_value(o
, "vout").get_int();
2316 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid parameter, vout must be positive");
2318 COutPoint
outpt(uint256S(txid
), nOutput
);
2321 pwallet
->UnlockCoin(outpt
);
2323 pwallet
->LockCoin(outpt
);
2329 UniValue
listlockunspent(const JSONRPCRequest
& request
)
2331 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2332 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2333 return NullUniValue
;
2336 if (request
.fHelp
|| request
.params
.size() > 0)
2337 throw std::runtime_error(
2339 "\nReturns list of temporarily unspendable outputs.\n"
2340 "See the lockunspent call to lock and unlock transactions for spending.\n"
2344 " \"txid\" : \"transactionid\", (string) The transaction id locked\n"
2345 " \"vout\" : n (numeric) The vout value\n"
2350 "\nList the unspent transactions\n"
2351 + HelpExampleCli("listunspent", "") +
2352 "\nLock an unspent transaction\n"
2353 + HelpExampleCli("lockunspent", "false \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2354 "\nList the locked transactions\n"
2355 + HelpExampleCli("listlockunspent", "") +
2356 "\nUnlock the transaction again\n"
2357 + HelpExampleCli("lockunspent", "true \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"") +
2358 "\nAs a json rpc call\n"
2359 + HelpExampleRpc("listlockunspent", "")
2362 LOCK2(cs_main
, pwallet
->cs_wallet
);
2364 std::vector
<COutPoint
> vOutpts
;
2365 pwallet
->ListLockedCoins(vOutpts
);
2367 UniValue
ret(UniValue::VARR
);
2369 BOOST_FOREACH(COutPoint
&outpt
, vOutpts
) {
2370 UniValue
o(UniValue::VOBJ
);
2372 o
.push_back(Pair("txid", outpt
.hash
.GetHex()));
2373 o
.push_back(Pair("vout", (int)outpt
.n
));
2380 UniValue
settxfee(const JSONRPCRequest
& request
)
2382 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2383 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2384 return NullUniValue
;
2387 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 1)
2388 throw std::runtime_error(
2390 "\nSet the transaction fee per kB. Overwrites the paytxfee parameter.\n"
2392 "1. amount (numeric or string, required) The transaction fee in " + CURRENCY_UNIT
+ "/kB\n"
2394 "true|false (boolean) Returns true if successful\n"
2396 + HelpExampleCli("settxfee", "0.00001")
2397 + HelpExampleRpc("settxfee", "0.00001")
2400 LOCK2(cs_main
, pwallet
->cs_wallet
);
2403 CAmount nAmount
= AmountFromValue(request
.params
[0]);
2405 payTxFee
= CFeeRate(nAmount
, 1000);
2409 UniValue
getwalletinfo(const JSONRPCRequest
& request
)
2411 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2412 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2413 return NullUniValue
;
2416 if (request
.fHelp
|| request
.params
.size() != 0)
2417 throw std::runtime_error(
2419 "Returns an object containing various wallet state info.\n"
2422 " \"walletversion\": xxxxx, (numeric) the wallet version\n"
2423 " \"balance\": xxxxxxx, (numeric) the total confirmed balance of the wallet in " + CURRENCY_UNIT
+ "\n"
2424 " \"unconfirmed_balance\": xxx, (numeric) the total unconfirmed balance of the wallet in " + CURRENCY_UNIT
+ "\n"
2425 " \"immature_balance\": xxxxxx, (numeric) the total immature balance of the wallet in " + CURRENCY_UNIT
+ "\n"
2426 " \"txcount\": xxxxxxx, (numeric) the total number of transactions in the wallet\n"
2427 " \"keypoololdest\": xxxxxx, (numeric) the timestamp (seconds since Unix epoch) of the oldest pre-generated key in the key pool\n"
2428 " \"keypoolsize\": xxxx, (numeric) how many new keys are pre-generated (only counts external keys)\n"
2429 " \"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"
2430 " \"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"
2431 " \"paytxfee\": x.xxxx, (numeric) the transaction fee configuration, set in " + CURRENCY_UNIT
+ "/kB\n"
2432 " \"hdmasterkeyid\": \"<hash160>\" (string) the Hash160 of the HD master pubkey\n"
2435 + HelpExampleCli("getwalletinfo", "")
2436 + HelpExampleRpc("getwalletinfo", "")
2439 LOCK2(cs_main
, pwallet
->cs_wallet
);
2441 UniValue
obj(UniValue::VOBJ
);
2443 size_t kpExternalSize
= pwallet
->KeypoolCountExternalKeys();
2444 obj
.push_back(Pair("walletversion", pwallet
->GetVersion()));
2445 obj
.push_back(Pair("balance", ValueFromAmount(pwallet
->GetBalance())));
2446 obj
.push_back(Pair("unconfirmed_balance", ValueFromAmount(pwallet
->GetUnconfirmedBalance())));
2447 obj
.push_back(Pair("immature_balance", ValueFromAmount(pwallet
->GetImmatureBalance())));
2448 obj
.push_back(Pair("txcount", (int)pwallet
->mapWallet
.size()));
2449 obj
.push_back(Pair("keypoololdest", pwallet
->GetOldestKeyPoolTime()));
2450 obj
.push_back(Pair("keypoolsize", (int64_t)kpExternalSize
));
2451 CKeyID masterKeyID
= pwallet
->GetHDChain().masterKeyID
;
2452 if (!masterKeyID
.IsNull() && pwallet
->CanSupportFeature(FEATURE_HD_SPLIT
)) {
2453 obj
.push_back(Pair("keypoolsize_hd_internal", (int64_t)(pwallet
->GetKeyPoolSize() - kpExternalSize
)));
2455 if (pwallet
->IsCrypted()) {
2456 obj
.push_back(Pair("unlocked_until", pwallet
->nRelockTime
));
2458 obj
.push_back(Pair("paytxfee", ValueFromAmount(payTxFee
.GetFeePerK())));
2459 if (!masterKeyID
.IsNull())
2460 obj
.push_back(Pair("hdmasterkeyid", masterKeyID
.GetHex()));
2464 UniValue
resendwallettransactions(const JSONRPCRequest
& request
)
2466 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2467 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2468 return NullUniValue
;
2471 if (request
.fHelp
|| request
.params
.size() != 0)
2472 throw std::runtime_error(
2473 "resendwallettransactions\n"
2474 "Immediately re-broadcast unconfirmed wallet transactions to all peers.\n"
2475 "Intended only for testing; the wallet code periodically re-broadcasts\n"
2477 "Returns array of transaction ids that were re-broadcast.\n"
2481 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED
, "Error: Peer-to-peer functionality missing or disabled");
2483 LOCK2(cs_main
, pwallet
->cs_wallet
);
2485 std::vector
<uint256
> txids
= pwallet
->ResendWalletTransactionsBefore(GetTime(), g_connman
.get());
2486 UniValue
result(UniValue::VARR
);
2487 BOOST_FOREACH(const uint256
& txid
, txids
)
2489 result
.push_back(txid
.ToString());
2494 UniValue
listunspent(const JSONRPCRequest
& request
)
2496 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2497 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2498 return NullUniValue
;
2501 if (request
.fHelp
|| request
.params
.size() > 4)
2502 throw std::runtime_error(
2503 "listunspent ( minconf maxconf [\"addresses\",...] [include_unsafe] )\n"
2504 "\nReturns array of unspent transaction outputs\n"
2505 "with between minconf and maxconf (inclusive) confirmations.\n"
2506 "Optionally filter to only include txouts paid to specified addresses.\n"
2508 "1. minconf (numeric, optional, default=1) The minimum confirmations to filter\n"
2509 "2. maxconf (numeric, optional, default=9999999) The maximum confirmations to filter\n"
2510 "3. \"addresses\" (string) A json array of bitcoin addresses to filter\n"
2512 " \"address\" (string) bitcoin address\n"
2515 "4. include_unsafe (bool, optional, default=true) Include outputs that are not safe to spend\n"
2516 " See description of \"safe\" attribute below.\n"
2518 "[ (array of json object)\n"
2520 " \"txid\" : \"txid\", (string) the transaction id \n"
2521 " \"vout\" : n, (numeric) the vout value\n"
2522 " \"address\" : \"address\", (string) the bitcoin address\n"
2523 " \"account\" : \"account\", (string) DEPRECATED. The associated account, or \"\" for the default account\n"
2524 " \"scriptPubKey\" : \"key\", (string) the script key\n"
2525 " \"amount\" : x.xxx, (numeric) the transaction output amount in " + CURRENCY_UNIT
+ "\n"
2526 " \"confirmations\" : n, (numeric) The number of confirmations\n"
2527 " \"redeemScript\" : n (string) The redeemScript if scriptPubKey is P2SH\n"
2528 " \"spendable\" : xxx, (bool) Whether we have the private keys to spend this output\n"
2529 " \"solvable\" : xxx, (bool) Whether we know how to spend this output, ignoring the lack of keys\n"
2530 " \"safe\" : xxx (bool) Whether this output is considered safe to spend. Unconfirmed transactions\n"
2531 " from outside keys and unconfirmed replacement transactions are considered unsafe\n"
2532 " and are not eligible for spending by fundrawtransaction and sendtoaddress.\n"
2538 + HelpExampleCli("listunspent", "")
2539 + HelpExampleCli("listunspent", "6 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
2540 + HelpExampleRpc("listunspent", "6, 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
2544 if (request
.params
.size() > 0 && !request
.params
[0].isNull()) {
2545 RPCTypeCheckArgument(request
.params
[0], UniValue::VNUM
);
2546 nMinDepth
= request
.params
[0].get_int();
2549 int nMaxDepth
= 9999999;
2550 if (request
.params
.size() > 1 && !request
.params
[1].isNull()) {
2551 RPCTypeCheckArgument(request
.params
[1], UniValue::VNUM
);
2552 nMaxDepth
= request
.params
[1].get_int();
2555 std::set
<CBitcoinAddress
> setAddress
;
2556 if (request
.params
.size() > 2 && !request
.params
[2].isNull()) {
2557 RPCTypeCheckArgument(request
.params
[2], UniValue::VARR
);
2558 UniValue inputs
= request
.params
[2].get_array();
2559 for (unsigned int idx
= 0; idx
< inputs
.size(); idx
++) {
2560 const UniValue
& input
= inputs
[idx
];
2561 CBitcoinAddress
address(input
.get_str());
2562 if (!address
.IsValid())
2563 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, std::string("Invalid Bitcoin address: ")+input
.get_str());
2564 if (setAddress
.count(address
))
2565 throw JSONRPCError(RPC_INVALID_PARAMETER
, std::string("Invalid parameter, duplicated address: ")+input
.get_str());
2566 setAddress
.insert(address
);
2570 bool include_unsafe
= true;
2571 if (request
.params
.size() > 3 && !request
.params
[3].isNull()) {
2572 RPCTypeCheckArgument(request
.params
[3], UniValue::VBOOL
);
2573 include_unsafe
= request
.params
[3].get_bool();
2576 UniValue
results(UniValue::VARR
);
2577 std::vector
<COutput
> vecOutputs
;
2578 assert(pwallet
!= NULL
);
2579 LOCK2(cs_main
, pwallet
->cs_wallet
);
2580 pwallet
->AvailableCoins(vecOutputs
, !include_unsafe
, NULL
, true);
2581 BOOST_FOREACH(const COutput
& out
, vecOutputs
) {
2582 if (out
.nDepth
< nMinDepth
|| out
.nDepth
> nMaxDepth
)
2585 CTxDestination address
;
2586 const CScript
& scriptPubKey
= out
.tx
->tx
->vout
[out
.i
].scriptPubKey
;
2587 bool fValidAddress
= ExtractDestination(scriptPubKey
, address
);
2589 if (setAddress
.size() && (!fValidAddress
|| !setAddress
.count(address
)))
2592 UniValue
entry(UniValue::VOBJ
);
2593 entry
.push_back(Pair("txid", out
.tx
->GetHash().GetHex()));
2594 entry
.push_back(Pair("vout", out
.i
));
2596 if (fValidAddress
) {
2597 entry
.push_back(Pair("address", CBitcoinAddress(address
).ToString()));
2599 if (pwallet
->mapAddressBook
.count(address
)) {
2600 entry
.push_back(Pair("account", pwallet
->mapAddressBook
[address
].name
));
2603 if (scriptPubKey
.IsPayToScriptHash()) {
2604 const CScriptID
& hash
= boost::get
<CScriptID
>(address
);
2605 CScript redeemScript
;
2606 if (pwallet
->GetCScript(hash
, redeemScript
)) {
2607 entry
.push_back(Pair("redeemScript", HexStr(redeemScript
.begin(), redeemScript
.end())));
2612 entry
.push_back(Pair("scriptPubKey", HexStr(scriptPubKey
.begin(), scriptPubKey
.end())));
2613 entry
.push_back(Pair("amount", ValueFromAmount(out
.tx
->tx
->vout
[out
.i
].nValue
)));
2614 entry
.push_back(Pair("confirmations", out
.nDepth
));
2615 entry
.push_back(Pair("spendable", out
.fSpendable
));
2616 entry
.push_back(Pair("solvable", out
.fSolvable
));
2617 entry
.push_back(Pair("safe", out
.fSafe
));
2618 results
.push_back(entry
);
2624 UniValue
fundrawtransaction(const JSONRPCRequest
& request
)
2626 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2627 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
)) {
2628 return NullUniValue
;
2631 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2)
2632 throw std::runtime_error(
2633 "fundrawtransaction \"hexstring\" ( options )\n"
2634 "\nAdd inputs to a transaction until it has enough in value to meet its out value.\n"
2635 "This will not modify existing inputs, and will add at most one change output to the outputs.\n"
2636 "No existing outputs will be modified unless \"subtractFeeFromOutputs\" is specified.\n"
2637 "Note that inputs which were signed may need to be resigned after completion since in/outputs have been added.\n"
2638 "The inputs added will not be signed, use signrawtransaction for that.\n"
2639 "Note that all existing inputs must have their previous output transaction be in the wallet.\n"
2640 "Note that all inputs selected must be of standard form and P2SH scripts must be\n"
2641 "in the wallet using importaddress or addmultisigaddress (to calculate fees).\n"
2642 "You can see whether this is the case by checking the \"solvable\" field in the listunspent output.\n"
2643 "Only pay-to-pubkey, multisig, and P2SH versions thereof are currently supported for watch-only\n"
2645 "1. \"hexstring\" (string, required) The hex string of the raw transaction\n"
2646 "2. options (object, optional)\n"
2648 " \"changeAddress\" (string, optional, default pool address) The bitcoin address to receive the change\n"
2649 " \"changePosition\" (numeric, optional, default random) The index of the change output\n"
2650 " \"includeWatching\" (boolean, optional, default false) Also select inputs which are watch only\n"
2651 " \"lockUnspents\" (boolean, optional, default false) Lock selected unspent outputs\n"
2652 " \"reserveChangeKey\" (boolean, optional, default true) Reserves the change output key from the keypool\n"
2653 " \"feeRate\" (numeric, optional, default not set: makes wallet determine the fee) Set a specific feerate (" + CURRENCY_UNIT
+ " per KB)\n"
2654 " \"subtractFeeFromOutputs\" (array, optional) A json array of integers.\n"
2655 " The fee will be equally deducted from the amount of each specified output.\n"
2656 " The outputs are specified by their zero-based index, before any change output is added.\n"
2657 " Those recipients will receive less bitcoins than you enter in their corresponding amount field.\n"
2658 " If no outputs are specified here, the sender pays the fee.\n"
2659 " [vout_index,...]\n"
2661 " for backward compatibility: passing in a true instead of an object will result in {\"includeWatching\":true}\n"
2664 " \"hex\": \"value\", (string) The resulting raw transaction (hex-encoded string)\n"
2665 " \"fee\": n, (numeric) Fee in " + CURRENCY_UNIT
+ " the resulting transaction pays\n"
2666 " \"changepos\": n (numeric) The position of the added change output, or -1\n"
2669 "\nCreate a transaction with no inputs\n"
2670 + HelpExampleCli("createrawtransaction", "\"[]\" \"{\\\"myaddress\\\":0.01}\"") +
2671 "\nAdd sufficient unsigned inputs to meet the output value\n"
2672 + HelpExampleCli("fundrawtransaction", "\"rawtransactionhex\"") +
2673 "\nSign the transaction\n"
2674 + HelpExampleCli("signrawtransaction", "\"fundedtransactionhex\"") +
2675 "\nSend the transaction\n"
2676 + HelpExampleCli("sendrawtransaction", "\"signedtransactionhex\"")
2679 RPCTypeCheck(request
.params
, boost::assign::list_of(UniValue::VSTR
));
2681 CTxDestination changeAddress
= CNoDestination();
2682 int changePosition
= -1;
2683 bool includeWatching
= false;
2684 bool lockUnspents
= false;
2685 bool reserveChangeKey
= true;
2686 CFeeRate feeRate
= CFeeRate(0);
2687 bool overrideEstimatedFeerate
= false;
2688 UniValue subtractFeeFromOutputs
;
2689 std::set
<int> setSubtractFeeFromOutputs
;
2691 if (request
.params
.size() > 1) {
2692 if (request
.params
[1].type() == UniValue::VBOOL
) {
2693 // backward compatibility bool only fallback
2694 includeWatching
= request
.params
[1].get_bool();
2697 RPCTypeCheck(request
.params
, boost::assign::list_of(UniValue::VSTR
)(UniValue::VOBJ
));
2699 UniValue options
= request
.params
[1];
2701 RPCTypeCheckObj(options
,
2703 {"changeAddress", UniValueType(UniValue::VSTR
)},
2704 {"changePosition", UniValueType(UniValue::VNUM
)},
2705 {"includeWatching", UniValueType(UniValue::VBOOL
)},
2706 {"lockUnspents", UniValueType(UniValue::VBOOL
)},
2707 {"reserveChangeKey", UniValueType(UniValue::VBOOL
)},
2708 {"feeRate", UniValueType()}, // will be checked below
2709 {"subtractFeeFromOutputs", UniValueType(UniValue::VARR
)},
2713 if (options
.exists("changeAddress")) {
2714 CBitcoinAddress
address(options
["changeAddress"].get_str());
2716 if (!address
.IsValid())
2717 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, "changeAddress must be a valid bitcoin address");
2719 changeAddress
= address
.Get();
2722 if (options
.exists("changePosition"))
2723 changePosition
= options
["changePosition"].get_int();
2725 if (options
.exists("includeWatching"))
2726 includeWatching
= options
["includeWatching"].get_bool();
2728 if (options
.exists("lockUnspents"))
2729 lockUnspents
= options
["lockUnspents"].get_bool();
2731 if (options
.exists("reserveChangeKey"))
2732 reserveChangeKey
= options
["reserveChangeKey"].get_bool();
2734 if (options
.exists("feeRate"))
2736 feeRate
= CFeeRate(AmountFromValue(options
["feeRate"]));
2737 overrideEstimatedFeerate
= true;
2740 if (options
.exists("subtractFeeFromOutputs"))
2741 subtractFeeFromOutputs
= options
["subtractFeeFromOutputs"].get_array();
2745 // parse hex string from parameter
2746 CMutableTransaction tx
;
2747 if (!DecodeHexTx(tx
, request
.params
[0].get_str(), true))
2748 throw JSONRPCError(RPC_DESERIALIZATION_ERROR
, "TX decode failed");
2750 if (tx
.vout
.size() == 0)
2751 throw JSONRPCError(RPC_INVALID_PARAMETER
, "TX must have at least one output");
2753 if (changePosition
!= -1 && (changePosition
< 0 || (unsigned int)changePosition
> tx
.vout
.size()))
2754 throw JSONRPCError(RPC_INVALID_PARAMETER
, "changePosition out of bounds");
2756 for (unsigned int idx
= 0; idx
< subtractFeeFromOutputs
.size(); idx
++) {
2757 int pos
= subtractFeeFromOutputs
[idx
].get_int();
2758 if (setSubtractFeeFromOutputs
.count(pos
))
2759 throw JSONRPCError(RPC_INVALID_PARAMETER
, strprintf("Invalid parameter, duplicated position: %d", pos
));
2761 throw JSONRPCError(RPC_INVALID_PARAMETER
, strprintf("Invalid parameter, negative position: %d", pos
));
2762 if (pos
>= int(tx
.vout
.size()))
2763 throw JSONRPCError(RPC_INVALID_PARAMETER
, strprintf("Invalid parameter, position too large: %d", pos
));
2764 setSubtractFeeFromOutputs
.insert(pos
);
2768 std::string strFailReason
;
2770 if (!pwallet
->FundTransaction(tx
, nFeeOut
, overrideEstimatedFeerate
, feeRate
, changePosition
, strFailReason
, includeWatching
, lockUnspents
, setSubtractFeeFromOutputs
, reserveChangeKey
, changeAddress
)) {
2771 throw JSONRPCError(RPC_WALLET_ERROR
, strFailReason
);
2774 UniValue
result(UniValue::VOBJ
);
2775 result
.push_back(Pair("hex", EncodeHexTx(tx
)));
2776 result
.push_back(Pair("changepos", changePosition
));
2777 result
.push_back(Pair("fee", ValueFromAmount(nFeeOut
)));
2782 UniValue
bumpfee(const JSONRPCRequest
& request
)
2784 CWallet
* const pwallet
= GetWalletForJSONRPCRequest(request
);
2786 if (!EnsureWalletIsAvailable(pwallet
, request
.fHelp
))
2787 return NullUniValue
;
2789 if (request
.fHelp
|| request
.params
.size() < 1 || request
.params
.size() > 2) {
2790 throw std::runtime_error(
2791 "bumpfee \"txid\" ( options ) \n"
2792 "\nBumps the fee of an opt-in-RBF transaction T, replacing it with a new transaction B.\n"
2793 "An opt-in RBF transaction with the given txid must be in the wallet.\n"
2794 "The command will pay the additional fee by decreasing (or perhaps removing) its change output.\n"
2795 "If the change output is not big enough to cover the increased fee, the command will currently fail\n"
2796 "instead of adding new inputs to compensate. (A future implementation could improve this.)\n"
2797 "The command will fail if the wallet or mempool contains a transaction that spends one of T's outputs.\n"
2798 "By default, the new fee will be calculated automatically using estimatefee.\n"
2799 "The user can specify a confirmation target for estimatefee.\n"
2800 "Alternatively, the user can specify totalFee, or use RPC setpaytxfee to set a higher fee rate.\n"
2801 "At a minimum, the new fee rate must be high enough to pay an additional new relay fee (incrementalfee\n"
2802 "returned by getnetworkinfo) to enter the node's mempool.\n"
2804 "1. txid (string, required) The txid to be bumped\n"
2805 "2. options (object, optional)\n"
2807 " \"confTarget\" (numeric, optional) Confirmation target (in blocks)\n"
2808 " \"totalFee\" (numeric, optional) Total fee (NOT feerate) to pay, in satoshis.\n"
2809 " In rare cases, the actual fee paid might be slightly higher than the specified\n"
2810 " totalFee if the tx change output has to be removed because it is too close to\n"
2811 " the dust threshold.\n"
2812 " \"replaceable\" (boolean, optional, default true) Whether the new transaction should still be\n"
2813 " marked bip-125 replaceable. If true, the sequence numbers in the transaction will\n"
2814 " be left unchanged from the original. If false, any input sequence numbers in the\n"
2815 " original transaction that were less than 0xfffffffe will be increased to 0xfffffffe\n"
2816 " so the new transaction will not be explicitly bip-125 replaceable (though it may\n"
2817 " still be replaceable in practice, for example if it has unconfirmed ancestors which\n"
2818 " are replaceable).\n"
2822 " \"txid\": \"value\", (string) The id of the new transaction\n"
2823 " \"origfee\": n, (numeric) Fee of the replaced transaction\n"
2824 " \"fee\": n, (numeric) Fee of the new transaction\n"
2825 " \"errors\": [ str... ] (json array of strings) Errors encountered during processing (may be empty)\n"
2828 "\nBump the fee, get the new transaction\'s txid\n" +
2829 HelpExampleCli("bumpfee", "<txid>"));
2832 RPCTypeCheck(request
.params
, boost::assign::list_of(UniValue::VSTR
)(UniValue::VOBJ
));
2834 hash
.SetHex(request
.params
[0].get_str());
2836 // optional parameters
2837 bool specifiedConfirmTarget
= false;
2838 int newConfirmTarget
= nTxConfirmTarget
;
2839 CAmount totalFee
= 0;
2840 bool replaceable
= true;
2841 if (request
.params
.size() > 1) {
2842 UniValue options
= request
.params
[1];
2843 RPCTypeCheckObj(options
,
2845 {"confTarget", UniValueType(UniValue::VNUM
)},
2846 {"totalFee", UniValueType(UniValue::VNUM
)},
2847 {"replaceable", UniValueType(UniValue::VBOOL
)},
2851 if (options
.exists("confTarget") && options
.exists("totalFee")) {
2852 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.");
2853 } else if (options
.exists("confTarget")) {
2854 specifiedConfirmTarget
= true;
2855 newConfirmTarget
= options
["confTarget"].get_int();
2856 if (newConfirmTarget
<= 0) { // upper-bound will be checked by estimatefee/smartfee
2857 throw JSONRPCError(RPC_INVALID_PARAMETER
, "Invalid confTarget (cannot be <= 0)");
2859 } else if (options
.exists("totalFee")) {
2860 totalFee
= options
["totalFee"].get_int64();
2863 if (options
.exists("replaceable")) {
2864 replaceable
= options
["replaceable"].get_bool();
2868 LOCK2(cs_main
, pwallet
->cs_wallet
);
2869 EnsureWalletIsUnlocked(pwallet
);
2871 CFeeBumper
feeBump(pwallet
, hash
, newConfirmTarget
, specifiedConfirmTarget
, totalFee
, replaceable
);
2872 BumpFeeResult res
= feeBump
.getResult();
2873 if (res
!= BumpFeeResult::OK
)
2876 case BumpFeeResult::INVALID_ADDRESS_OR_KEY
:
2877 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY
, feeBump
.getErrors()[0]);
2879 case BumpFeeResult::INVALID_REQUEST
:
2880 throw JSONRPCError(RPC_INVALID_REQUEST
, feeBump
.getErrors()[0]);
2882 case BumpFeeResult::INVALID_PARAMETER
:
2883 throw JSONRPCError(RPC_INVALID_PARAMETER
, feeBump
.getErrors()[0]);
2885 case BumpFeeResult::WALLET_ERROR
:
2886 throw JSONRPCError(RPC_WALLET_ERROR
, feeBump
.getErrors()[0]);
2889 throw JSONRPCError(RPC_MISC_ERROR
, feeBump
.getErrors()[0]);
2894 // sign bumped transaction
2895 if (!pwallet
->SignTransaction(*feeBump
.getBumpedTxRef())) {
2896 throw JSONRPCError(RPC_WALLET_ERROR
, "Can't sign transaction.");
2898 // commit the bumped transaction
2899 if(!feeBump
.commit(pwallet
)) {
2900 throw JSONRPCError(RPC_WALLET_ERROR
, feeBump
.getErrors()[0]);
2902 UniValue
result(UniValue::VOBJ
);
2903 result
.push_back(Pair("txid", feeBump
.getBumpedTxId().GetHex()));
2904 result
.push_back(Pair("origfee", ValueFromAmount(feeBump
.getOldFee())));
2905 result
.push_back(Pair("fee", ValueFromAmount(feeBump
.getNewFee())));
2906 UniValue
errors(UniValue::VARR
);
2907 for (const std::string
& err
: feeBump
.getErrors())
2908 errors
.push_back(err
);
2909 result
.push_back(errors
);
2914 extern UniValue
dumpprivkey(const JSONRPCRequest
& request
); // in rpcdump.cpp
2915 extern UniValue
importprivkey(const JSONRPCRequest
& request
);
2916 extern UniValue
importaddress(const JSONRPCRequest
& request
);
2917 extern UniValue
importpubkey(const JSONRPCRequest
& request
);
2918 extern UniValue
dumpwallet(const JSONRPCRequest
& request
);
2919 extern UniValue
importwallet(const JSONRPCRequest
& request
);
2920 extern UniValue
importprunedfunds(const JSONRPCRequest
& request
);
2921 extern UniValue
removeprunedfunds(const JSONRPCRequest
& request
);
2922 extern UniValue
importmulti(const JSONRPCRequest
& request
);
2924 static const CRPCCommand commands
[] =
2925 { // category name actor (function) okSafeMode
2926 // --------------------- ------------------------ ----------------------- ----------
2927 { "rawtransactions", "fundrawtransaction", &fundrawtransaction
, false, {"hexstring","options"} },
2928 { "hidden", "resendwallettransactions", &resendwallettransactions
, true, {} },
2929 { "wallet", "abandontransaction", &abandontransaction
, false, {"txid"} },
2930 { "wallet", "addmultisigaddress", &addmultisigaddress
, true, {"nrequired","keys","account"} },
2931 { "wallet", "addwitnessaddress", &addwitnessaddress
, true, {"address"} },
2932 { "wallet", "backupwallet", &backupwallet
, true, {"destination"} },
2933 { "wallet", "bumpfee", &bumpfee
, true, {"txid", "options"} },
2934 { "wallet", "dumpprivkey", &dumpprivkey
, true, {"address"} },
2935 { "wallet", "dumpwallet", &dumpwallet
, true, {"filename"} },
2936 { "wallet", "encryptwallet", &encryptwallet
, true, {"passphrase"} },
2937 { "wallet", "getaccountaddress", &getaccountaddress
, true, {"account"} },
2938 { "wallet", "getaccount", &getaccount
, true, {"address"} },
2939 { "wallet", "getaddressesbyaccount", &getaddressesbyaccount
, true, {"account"} },
2940 { "wallet", "getbalance", &getbalance
, false, {"account","minconf","include_watchonly"} },
2941 { "wallet", "getnewaddress", &getnewaddress
, true, {"account"} },
2942 { "wallet", "getrawchangeaddress", &getrawchangeaddress
, true, {} },
2943 { "wallet", "getreceivedbyaccount", &getreceivedbyaccount
, false, {"account","minconf"} },
2944 { "wallet", "getreceivedbyaddress", &getreceivedbyaddress
, false, {"address","minconf"} },
2945 { "wallet", "gettransaction", &gettransaction
, false, {"txid","include_watchonly"} },
2946 { "wallet", "getunconfirmedbalance", &getunconfirmedbalance
, false, {} },
2947 { "wallet", "getwalletinfo", &getwalletinfo
, false, {} },
2948 { "wallet", "importmulti", &importmulti
, true, {"requests","options"} },
2949 { "wallet", "importprivkey", &importprivkey
, true, {"privkey","label","rescan"} },
2950 { "wallet", "importwallet", &importwallet
, true, {"filename"} },
2951 { "wallet", "importaddress", &importaddress
, true, {"address","label","rescan","p2sh"} },
2952 { "wallet", "importprunedfunds", &importprunedfunds
, true, {"rawtransaction","txoutproof"} },
2953 { "wallet", "importpubkey", &importpubkey
, true, {"pubkey","label","rescan"} },
2954 { "wallet", "keypoolrefill", &keypoolrefill
, true, {"newsize"} },
2955 { "wallet", "listaccounts", &listaccounts
, false, {"minconf","include_watchonly"} },
2956 { "wallet", "listaddressgroupings", &listaddressgroupings
, false, {} },
2957 { "wallet", "listlockunspent", &listlockunspent
, false, {} },
2958 { "wallet", "listreceivedbyaccount", &listreceivedbyaccount
, false, {"minconf","include_empty","include_watchonly"} },
2959 { "wallet", "listreceivedbyaddress", &listreceivedbyaddress
, false, {"minconf","include_empty","include_watchonly"} },
2960 { "wallet", "listsinceblock", &listsinceblock
, false, {"blockhash","target_confirmations","include_watchonly"} },
2961 { "wallet", "listtransactions", &listtransactions
, false, {"account","count","skip","include_watchonly"} },
2962 { "wallet", "listunspent", &listunspent
, false, {"minconf","maxconf","addresses","include_unsafe"} },
2963 { "wallet", "lockunspent", &lockunspent
, true, {"unlock","transactions"} },
2964 { "wallet", "move", &movecmd
, false, {"fromaccount","toaccount","amount","minconf","comment"} },
2965 { "wallet", "sendfrom", &sendfrom
, false, {"fromaccount","toaddress","amount","minconf","comment","comment_to"} },
2966 { "wallet", "sendmany", &sendmany
, false, {"fromaccount","amounts","minconf","comment","subtractfeefrom"} },
2967 { "wallet", "sendtoaddress", &sendtoaddress
, false, {"address","amount","comment","comment_to","subtractfeefromamount"} },
2968 { "wallet", "setaccount", &setaccount
, true, {"address","account"} },
2969 { "wallet", "settxfee", &settxfee
, true, {"amount"} },
2970 { "wallet", "signmessage", &signmessage
, true, {"address","message"} },
2971 { "wallet", "walletlock", &walletlock
, true, {} },
2972 { "wallet", "walletpassphrasechange", &walletpassphrasechange
, true, {"oldpassphrase","newpassphrase"} },
2973 { "wallet", "walletpassphrase", &walletpassphrase
, true, {"passphrase","timeout"} },
2974 { "wallet", "removeprunedfunds", &removeprunedfunds
, true, {"txid"} },
2977 void RegisterWalletRPCCommands(CRPCTable
&t
)
2979 if (GetBoolArg("-disablewallet", false))
2982 for (unsigned int vcidx
= 0; vcidx
< ARRAYLEN(commands
); vcidx
++)
2983 t
.appendCommand(commands
[vcidx
].name
, &commands
[vcidx
]);