20 #include <boost/assign/list_of.hpp>
21 #include "json/json_spirit_utils.h"
22 #include "json/json_spirit_value.h"
25 using namespace boost;
32 vector<CTxDestination> addresses;
35 out.push_back(Pair(
"asm", scriptPubKey.
ToString()));
37 out.push_back(Pair(
"hex",
HexStr(scriptPubKey.begin(), scriptPubKey.end())));
45 out.push_back(Pair(
"reqSigs", nRequired));
51 out.push_back(Pair(
"addresses", a));
57 entry.push_back(Pair(
"version", tx.
nVersion));
58 entry.push_back(Pair(
"locktime", (int64_t)tx.
nLockTime));
60 BOOST_FOREACH(
const CTxIn& txin, tx.
vin)
68 in.push_back(Pair(
"vout", (int64_t)txin.
prevout.
n));
72 in.push_back(Pair(
"scriptSig", o));
74 in.push_back(Pair(
"sequence", (int64_t)txin.
nSequence));
77 entry.push_back(Pair(
"vin", vin));
79 for (
unsigned int i = 0; i < tx.
vout.size(); i++)
84 out.push_back(Pair(
"n", (int64_t)i));
87 out.push_back(Pair(
"scriptPubKey", o));
90 entry.push_back(Pair(
"vout", vout));
94 entry.push_back(Pair(
"blockhash", hashBlock.
GetHex()));
95 map<uint256, CBlockIndex*>::iterator mi =
mapBlockIndex.find(hashBlock);
102 entry.push_back(Pair(
"time", (int64_t)pindex->
nTime));
103 entry.push_back(Pair(
"blocktime", (int64_t)pindex->
nTime));
106 entry.push_back(Pair(
"confirmations", 0));
113 if (fHelp || params.size() < 1 || params.size() > 2)
115 "getrawtransaction \"txid\" ( verbose )\n"
116 "\nReturn the raw transaction data.\n"
117 "\nIf verbose=0, returns a string that is serialized, hex-encoded data for 'txid'.\n"
118 "If verbose is non-zero, returns an Object with information about 'txid'.\n"
121 "1. \"txid\" (string, required) The transaction id\n"
122 "2. verbose (numeric, optional, default=0) If 0, return a string, other return a json object\n"
124 "\nResult (if verbose is not set or set to 0):\n"
125 "\"data\" (string) The serialized, hex-encoded data for 'txid'\n"
127 "\nResult (if verbose > 0):\n"
129 " \"hex\" : \"data\", (string) The serialized, hex-encoded data for 'txid'\n"
130 " \"txid\" : \"id\", (string) The transaction id (same as provided)\n"
131 " \"version\" : n, (numeric) The version\n"
132 " \"locktime\" : ttt, (numeric) The lock time\n"
133 " \"vin\" : [ (array of json objects)\n"
135 " \"txid\": \"id\", (string) The transaction id\n"
136 " \"vout\": n, (numeric) \n"
137 " \"scriptSig\": { (json object) The script\n"
138 " \"asm\": \"asm\", (string) asm\n"
139 " \"hex\": \"hex\" (string) hex\n"
141 " \"sequence\": n (numeric) The script sequence number\n"
145 " \"vout\" : [ (array of json objects)\n"
147 " \"value\" : x.xxx, (numeric) The value in btc\n"
148 " \"n\" : n, (numeric) index\n"
149 " \"scriptPubKey\" : { (json object)\n"
150 " \"asm\" : \"asm\", (string) the asm\n"
151 " \"hex\" : \"hex\", (string) the hex\n"
152 " \"reqSigs\" : n, (numeric) The required sigs\n"
153 " \"type\" : \"pubkeyhash\", (string) The type, eg 'pubkeyhash'\n"
154 " \"addresses\" : [ (json array of string)\n"
155 " \"bitcoinaddress\" (string) bitcoin address\n"
162 " \"blockhash\" : \"hash\", (string) the block hash\n"
163 " \"confirmations\" : n, (numeric) The confirmations\n"
164 " \"time\" : ttt, (numeric) The transaction time in seconds since epoch (Jan 1 1970 GMT)\n"
165 " \"blocktime\" : ttt (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n"
176 bool fVerbose =
false;
177 if (params.size() > 1)
178 fVerbose = (params[1].get_int() != 0);
193 result.push_back(Pair(
"hex", strHex));
201 if (fHelp || params.size() > 3)
203 "listunspent ( minconf maxconf [\"address\",...] )\n"
204 "\nReturns array of unspent transaction outputs\n"
205 "with between minconf and maxconf (inclusive) confirmations.\n"
206 "Optionally filter to only include txouts paid to specified addresses.\n"
207 "Results are an array of Objects, each of which has:\n"
208 "{txid, vout, scriptPubKey, amount, confirmations}\n"
210 "1. minconf (numeric, optional, default=1) The minimum confirmationsi to filter\n"
211 "2. maxconf (numeric, optional, default=9999999) The maximum confirmations to filter\n"
212 "3. \"addresses\" (string) A json array of bitcoin addresses to filter\n"
214 " \"address\" (string) bitcoin address\n"
218 "[ (array of json object)\n"
220 " \"txid\" : \"txid\", (string) the transaction id \n"
221 " \"vout\" : n, (numeric) the vout value\n"
222 " \"address\" : \"address\", (string) the bitcoin address\n"
223 " \"account\" : \"account\", (string) The associated account, or \"\" for the default account\n"
224 " \"scriptPubKey\" : \"key\", (string) the script key\n"
225 " \"amount\" : x.xxx, (numeric) the transaction amount in btc\n"
226 " \"confirmations\" : n (numeric) The number of confirmations\n"
233 +
HelpExampleCli(
"listunspent",
"6 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
234 +
HelpExampleRpc(
"listunspent",
"6, 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
237 RPCTypeCheck(params, list_of(int_type)(int_type)(array_type));
240 if (params.size() > 0)
241 nMinDepth = params[0].get_int();
243 int nMaxDepth = 9999999;
244 if (params.size() > 1)
245 nMaxDepth = params[1].get_int();
247 set<CBitcoinAddress> setAddress;
248 if (params.size() > 2)
250 Array inputs = params[2].get_array();
251 BOOST_FOREACH(Value& input, inputs)
254 if (!address.IsValid())
256 if (setAddress.count(address))
258 setAddress.insert(address);
263 vector<COutput> vecOutputs;
266 BOOST_FOREACH(
const COutput& out, vecOutputs)
271 if (setAddress.size())
277 if (!setAddress.count(address))
281 int64_t nValue = out.
tx->
vout[out.
i].nValue;
285 entry.push_back(Pair(
"vout", out.
i));
293 entry.push_back(Pair(
"scriptPubKey",
HexStr(pk.begin(), pk.end())));
299 const CScriptID& hash = boost::get<const CScriptID&>(address);
302 entry.push_back(Pair(
"redeemScript",
HexStr(redeemScript.begin(), redeemScript.end())));
306 entry.push_back(Pair(
"confirmations",out.
nDepth));
307 results.push_back(entry);
316 if (fHelp || params.size() != 2)
318 "createrawtransaction [{\"txid\":\"id\",\"vout\":n},...] {\"address\":amount,...}\n"
319 "\nCreate a transaction spending the given inputs and sending to the given addresses.\n"
320 "Returns hex-encoded raw transaction.\n"
321 "Note that the transaction's inputs are not signed, and\n"
322 "it is not stored in the wallet or transmitted to the network.\n"
325 "1. \"transactions\" (string, required) A json array of json objects\n"
328 " \"txid\":\"id\", (string, required) The transaction id\n"
329 " \"vout\":n (numeric, required) The output number\n"
333 "2. \"addresses\" (string, required) a json object with addresses as keys and amounts as values\n"
335 " \"address\": x.xxx (numeric, required) The key is the bitcoin address, the value is the btc amount\n"
340 "\"transaction\" (string) hex string of the transaction\n"
343 +
HelpExampleCli(
"createrawtransaction",
"\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"{\\\"address\\\":0.01}\"")
344 +
HelpExampleRpc(
"createrawtransaction",
"\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"{\\\"address\\\":0.01}\"")
349 Array inputs = params[0].get_array();
350 Object sendTo = params[1].get_obj();
354 BOOST_FOREACH(
const Value& input, inputs)
356 const Object& o = input.get_obj();
360 const Value& vout_v = find_value(o,
"vout");
361 if (vout_v.type() != int_type)
363 int nOutput = vout_v.get_int();
368 rawTx.
vin.push_back(in);
371 set<CBitcoinAddress> setAddress;
372 BOOST_FOREACH(
const Pair& s, sendTo)
378 if (setAddress.count(address))
380 setAddress.insert(address);
386 CTxOut out(nAmount, scriptPubKey);
387 rawTx.
vout.push_back(out);
397 if (fHelp || params.size() != 1)
399 "decoderawtransaction \"hexstring\"\n"
400 "\nReturn a JSON object representing the serialized, hex-encoded transaction.\n"
403 "1. \"hex\" (string, required) The transaction hex string\n"
407 " \"txid\" : \"id\", (string) The transaction id\n"
408 " \"version\" : n, (numeric) The version\n"
409 " \"locktime\" : ttt, (numeric) The lock time\n"
410 " \"vin\" : [ (array of json objects)\n"
412 " \"txid\": \"id\", (string) The transaction id\n"
413 " \"vout\": n, (numeric) The output number\n"
414 " \"scriptSig\": { (json object) The script\n"
415 " \"asm\": \"asm\", (string) asm\n"
416 " \"hex\": \"hex\" (string) hex\n"
418 " \"sequence\": n (numeric) The script sequence number\n"
422 " \"vout\" : [ (array of json objects)\n"
424 " \"value\" : x.xxx, (numeric) The value in btc\n"
425 " \"n\" : n, (numeric) index\n"
426 " \"scriptPubKey\" : { (json object)\n"
427 " \"asm\" : \"asm\", (string) the asm\n"
428 " \"hex\" : \"hex\", (string) the hex\n"
429 " \"reqSigs\" : n, (numeric) The required sigs\n"
430 " \"type\" : \"pubkeyhash\", (string) The type, eg 'pubkeyhash'\n"
431 " \"addresses\" : [ (json array of string)\n"
432 " \"12tvKAXCxZjSmdNbao16dKXC8tRWfcF5oc\" (string) bitcoin address\n"
446 vector<unsigned char> txData(
ParseHexV(params[0],
"argument"));
452 catch (std::exception &e) {
464 if (fHelp || params.size() != 1)
466 "decodescript \"hex\"\n"
467 "\nDecode a hex-encoded script.\n"
469 "1. \"hex\" (string) the hex encoded script\n"
472 " \"asm\":\"asm\", (string) Script public key\n"
473 " \"hex\":\"hex\", (string) hex encoded public key\n"
474 " \"type\":\"type\", (string) The output type\n"
475 " \"reqSigs\": n, (numeric) The required signatures\n"
476 " \"addresses\": [ (json array of string)\n"
477 " \"address\" (string) bitcoin address\n"
480 " \"p2sh\",\"address\" (string) script address\n"
491 if (params[0].get_str().size() > 0){
492 vector<unsigned char> scriptData(
ParseHexV(params[0],
"argument"));
493 script =
CScript(scriptData.begin(), scriptData.end());
505 if (fHelp || params.size() < 1 || params.size() > 4)
507 "signrawtransaction \"hexstring\" ( [{\"txid\":\"id\",\"vout\":n,\"scriptPubKey\":\"hex\",\"redeemScript\":\"hex\"},...] [\"privatekey1\",...] sighashtype )\n"
508 "\nSign inputs for raw transaction (serialized, hex-encoded).\n"
509 "The second optional argument (may be null) is an array of previous transaction outputs that\n"
510 "this transaction depends on but may not yet be in the block chain.\n"
511 "The third optional argument (may be null) is an array of base58-encoded private\n"
512 "keys that, if given, will be the only keys used to sign the transaction.\n"
518 "1. \"hexstring\" (string, required) The transaction hex string\n"
519 "2. \"prevtxs\" (string, optional) An json array of previous dependent transaction outputs\n"
520 " [ (json array of json objects, or 'null' if none provided)\n"
522 " \"txid\":\"id\", (string, required) The transaction id\n"
523 " \"vout\":n, (numeric, required) The output number\n"
524 " \"scriptPubKey\": \"hex\", (string, required) script key\n"
525 " \"redeemScript\": \"hex\" (string, required for P2SH) redeem script\n"
529 "3. \"privatekeys\" (string, optional) A json array of base58-encoded private keys for signing\n"
530 " [ (json array of strings, or 'null' if none provided)\n"
531 " \"privatekey\" (string) private key in base58-encoding\n"
534 "4. \"sighashtype\" (string, optional, default=ALL) The signature hash type. Must be one of\n"
538 " \"ALL|ANYONECANPAY\"\n"
539 " \"NONE|ANYONECANPAY\"\n"
540 " \"SINGLE|ANYONECANPAY\"\n"
544 " \"hex\": \"value\", (string) The raw transaction with signature(s) (hex-encoded string)\n"
545 " \"complete\": n (numeric) if transaction has a complete set of signature (0 if not)\n"
553 RPCTypeCheck(params, list_of(str_type)(array_type)(array_type)(str_type),
true);
555 vector<unsigned char> txData(
ParseHexV(params[0],
"argument 1"));
557 vector<CTransaction> txVariants;
558 while (!ssData.
empty())
563 txVariants.push_back(tx);
565 catch (std::exception &e) {
570 if (txVariants.empty())
576 bool fComplete =
true;
587 BOOST_FOREACH(
const CTxIn& txin, mergedTx.
vin) {
596 bool fGivenKeys =
false;
598 if (params.size() > 2 && params[2].type() != null_type)
601 Array keys = params[2].get_array();
602 BOOST_FOREACH(Value k, keys)
605 bool fGood = vchSecret.
SetString(k.get_str());
618 if (params.size() > 1 && params[1].type() != null_type)
620 Array prevTxs = params[1].get_array();
621 BOOST_FOREACH(Value& p, prevTxs)
623 if (p.type() != obj_type)
626 Object prevOut = p.get_obj();
628 RPCTypeCheck(prevOut, map_list_of(
"txid", str_type)(
"vout", int_type)(
"scriptPubKey", str_type));
632 int nOut = find_value(prevOut,
"vout").get_int();
636 vector<unsigned char> pkData(
ParseHexO(prevOut,
"scriptPubKey"));
637 CScript scriptPubKey(pkData.begin(), pkData.end());
641 if (coins.IsAvailable(nOut) && coins.vout[nOut].scriptPubKey != scriptPubKey) {
642 string err(
"Previous output scriptPubKey mismatch:\n");
643 err = err + coins.vout[nOut].scriptPubKey.ToString() +
"\nvs:\n"+
644 scriptPubKey.ToString();
649 if ((
unsigned int)nOut >= coins.vout.size())
650 coins.vout.resize(nOut+1);
651 coins.vout[nOut].scriptPubKey = scriptPubKey;
652 coins.vout[nOut].nValue = 0;
657 if (fGivenKeys && scriptPubKey.IsPayToScriptHash())
659 RPCTypeCheck(prevOut, map_list_of(
"txid", str_type)(
"vout", int_type)(
"scriptPubKey", str_type)(
"redeemScript",str_type));
660 Value v = find_value(prevOut,
"redeemScript");
661 if (!(v == Value::null))
663 vector<unsigned char> rsData(
ParseHexV(v,
"redeemScript"));
664 CScript redeemScript(rsData.begin(), rsData.end());
674 const CKeyStore& keystore = tempKeystore;
678 if (params.size() > 3 && params[3].type() != null_type)
680 static map<string, int> mapSigHashValues =
681 boost::assign::map_list_of
689 string strHashType = params[3].get_str();
690 if (mapSigHashValues.count(strHashType))
691 nHashType = mapSigHashValues[strHashType];
699 for (
unsigned int i = 0; i < mergedTx.
vin.size(); i++)
712 if (!fHashSingle || (i < mergedTx.
vout.size()))
728 result.push_back(Pair(
"complete", fComplete));
735 if (fHelp || params.size() < 1 || params.size() > 2)
737 "sendrawtransaction \"hexstring\" ( allowhighfees )\n"
738 "\nSubmits raw transaction (serialized, hex-encoded) to local node and network.\n"
739 "\nAlso see createrawtransaction and signrawtransaction calls.\n"
741 "1. \"hexstring\" (string, required) The hex string of the raw transaction)\n"
742 "2. allowhighfees (boolean, optional, default=false) Allow high fees\n"
744 "\"hex\" (string) The transaction hash in hex\n"
746 "\nCreate a transaction\n"
747 +
HelpExampleCli(
"createrawtransaction",
"\"[{\\\"txid\\\" : \\\"mytxid\\\",\\\"vout\\\":0}]\" \"{\\\"myaddress\\\":0.01}\"") +
748 "Sign the transaction, and get back the hex\n"
750 "\nSend the transaction (signed hex)\n"
752 "\nAs a json rpc call\n"
758 vector<unsigned char> txData(
ParseHexV(params[0],
"parameter"));
762 bool fOverrideFees =
false;
763 if (params.size() > 1)
764 fOverrideFees = params[1].get_bool();
770 catch (std::exception &e) {
778 bool fHaveChain = view.
GetCoins(hashTx, existingCoins) && existingCoins.
nHeight < 1000000000;
779 if (!fHaveMempool && !fHaveChain) {
790 }
else if (fHaveChain) {
Value createrawtransaction(const Array ¶ms, bool fHelp)
std::string HelpRequiringPassphrase()
const_iterator begin() const
void SetBackend(CCoinsView &viewIn)
static CScript CombineSignatures(CScript scriptPubKey, const CTransaction &txTo, unsigned int nIn, const txnouttype txType, const vector< valtype > &vSolutions, vector< valtype > &sigs1, vector< valtype > &sigs2)
vector< unsigned char > ParseHexO(const Object &o, string strKey)
uint256 ParseHashO(const Object &o, string strKey)
std::vector< CTxOut > vout
std::map< CTxDestination, CAddressBookData > mapAddressBook
bool IsPayToScriptHash() const
std::string HelpExampleRpc(string methodname, string args)
Value sendrawtransaction(const Array ¶ms, bool fHelp)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
bool SignSignature(const CKeyStore &keystore, const CScript &fromPubKey, CTransaction &txTo, unsigned int nIn, int nHashType)
const char * GetTxnOutputType(txnouttype t)
Double ended buffer combining vector and stream-like interfaces.
Object JSONRPCError(int code, const string &message)
base58-encoded Bitcoin addresses.
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
void RPCTypeCheck(const Array ¶ms, const list< Value_type > &typesExpected, bool fAllowNull)
CTxDestination Get() const
CChain chainActive
The currently-connected chain of blocks.
Value decodescript(const Array ¶ms, bool fHelp)
bool GetCoins(const uint256 &txid, CCoins &coins)
std::string ToString() const
bool IsAvailable(unsigned int nPos) const
bool SetString(const char *pszSecret)
void SetDestination(const CTxDestination &address)
virtual bool AddCScript(const CScript &redeemScript)
json_spirit::Value listunspent(const json_spirit::Array ¶ms, bool fHelp)
bool AcceptToMemoryPool(CTxMemPool &pool, CValidationState &state, const CTransaction &tx, bool fLimitFree, bool *pfMissingInputs, bool fRejectInsaneFee)
(try to) add transaction to memory pool
void ScriptPubKeyToJSON(const CScript &scriptPubKey, Object &out, bool fIncludeHex)
Value decoderawtransaction(const Array ¶ms, bool fHelp)
int Height() const
Return the maximal height in the chain.
Value ValueFromAmount(int64_t amount)
Abstract view on the open txout dataset.
An input of a transaction.
void EnsureWalletIsUnlocked()
A base58-encoded secret key.
std::vector< CTxOut > vout
void TxToJSON(const CTransaction &tx, const uint256 hashBlock, Object &entry)
void RelayTransaction(const CTransaction &tx, const uint256 &hash)
An output of a transaction.
void AvailableCoins(std::vector< COutput > &vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl=NULL) const
An outpoint - a combination of a transaction hash and an index n into its vout.
CCoinsViewCache * pcoinsTip
Global variable that points to the active CCoinsView (protected by cs_main)
virtual bool AddKey(const CKey &key)
std::string GetHex() const
std::string GetRejectReason() const
unsigned char GetRejectCode() const
Capture information about block/transaction validation.
void SyncWithWallets(const uint256 &hash, const CTransaction &tx, const CBlock *pblock)
Push an updated transaction to all registered wallets.
bool ExtractDestinations(const CScript &scriptPubKey, txnouttype &typeRet, vector< CTxDestination > &addressRet, int &nRequiredRet)
The block chain is a tree shaped structure starting with the genesis block at the root...
Serialized script, used inside transaction inputs and outputs.
static const int PROTOCOL_VERSION
vector< unsigned char > ParseHexV(const Value &v, string strName)
A virtual base class for key stores.
Value signrawtransaction(const Array ¶ms, bool fHelp)
bool exists(uint256 hash)
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow)
Retrieve a transaction (from memory pool, or from disk, if possible)
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const
int64_t AmountFromValue(const Value &value)
Value getrawtransaction(const Array ¶ms, bool fHelp)
A reference to a CScript: the Hash160 of its serialization (see script.h)
bool SetCoins(const uint256 &txid, const CCoins &coins)
uint256 ParseHashV(const Value &v, string strName)
std::string HelpExampleCli(string methodname, string args)
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
An encapsulated private key.
The basic transaction that is broadcasted on the network and contained in blocks. ...
CCoinsView that adds a memory cache for transactions to another CCoinsView.
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
CCoinsView that brings transactions from a memorypool into view.
map< uint256, CBlockIndex * > mapBlockIndex
Basic key store, that keeps keys in an address->secret map.
const_iterator end() const