1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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.
7 #include "consensus/validation.h"
12 #include "merkleblock.h"
14 #include "primitives/transaction.h"
15 #include "rpcserver.h"
16 #include "script/script.h"
17 #include "script/script_error.h"
18 #include "script/sign.h"
19 #include "script/standard.h"
22 #include "wallet/wallet.h"
27 #include <boost/assign/list_of.hpp>
28 #include "json/json_spirit_utils.h"
29 #include "json/json_spirit_value.h"
31 using namespace json_spirit;
34 void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out, bool fIncludeHex)
37 vector<CTxDestination> addresses;
40 out.push_back(Pair("asm", scriptPubKey.ToString()));
42 out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
44 if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
45 out.push_back(Pair("type", GetTxnOutputType(type)));
49 out.push_back(Pair("reqSigs", nRequired));
50 out.push_back(Pair("type", GetTxnOutputType(type)));
53 BOOST_FOREACH(const CTxDestination& addr, addresses)
54 a.push_back(CBitcoinAddress(addr).ToString());
55 out.push_back(Pair("addresses", a));
59 Array TxJoinSplitToJSON(const CTransaction& tx) {
61 for (unsigned int i = 0; i < tx.vjoinsplit.size(); i++) {
62 const JSDescription& jsdescription = tx.vjoinsplit[i];
65 joinsplit.push_back(Pair("anchor", jsdescription.anchor.GetHex()));
69 BOOST_FOREACH(const uint256 nf, jsdescription.nullifiers) {
70 nullifiers.push_back(nf.GetHex());
72 joinsplit.push_back(Pair("nullifiers", nullifiers));
77 BOOST_FOREACH(const uint256 commitment, jsdescription.commitments) {
78 commitments.push_back(commitment.GetHex());
80 joinsplit.push_back(Pair("commitments", commitments));
85 BOOST_FOREACH(const uint256 mac, jsdescription.macs) {
86 macs.push_back(mac.GetHex());
88 joinsplit.push_back(Pair("macs", macs));
91 joinsplit.push_back(Pair("vpub_old", ValueFromAmount(jsdescription.vpub_old)));
92 joinsplit.push_back(Pair("vpub_new", ValueFromAmount(jsdescription.vpub_new)));
94 vjoinsplit.push_back(joinsplit);
99 void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry)
101 entry.push_back(Pair("txid", tx.GetHash().GetHex()));
102 entry.push_back(Pair("version", tx.nVersion));
103 entry.push_back(Pair("locktime", (int64_t)tx.nLockTime));
105 BOOST_FOREACH(const CTxIn& txin, tx.vin) {
108 in.push_back(Pair("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
110 in.push_back(Pair("txid", txin.prevout.hash.GetHex()));
111 in.push_back(Pair("vout", (int64_t)txin.prevout.n));
113 o.push_back(Pair("asm", txin.scriptSig.ToString()));
114 o.push_back(Pair("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
115 in.push_back(Pair("scriptSig", o));
117 in.push_back(Pair("sequence", (int64_t)txin.nSequence));
120 entry.push_back(Pair("vin", vin));
122 for (unsigned int i = 0; i < tx.vout.size(); i++) {
123 const CTxOut& txout = tx.vout[i];
125 out.push_back(Pair("value", ValueFromAmount(txout.nValue)));
126 out.push_back(Pair("n", (int64_t)i));
128 ScriptPubKeyToJSON(txout.scriptPubKey, o, true);
129 out.push_back(Pair("scriptPubKey", o));
132 entry.push_back(Pair("vout", vout));
134 Array vjoinsplit = TxJoinSplitToJSON(tx);
135 entry.push_back(Pair("vjoinsplit", vjoinsplit));
137 if (!hashBlock.IsNull()) {
138 entry.push_back(Pair("blockhash", hashBlock.GetHex()));
139 BlockMap::iterator mi = mapBlockIndex.find(hashBlock);
140 if (mi != mapBlockIndex.end() && (*mi).second) {
141 CBlockIndex* pindex = (*mi).second;
142 if (chainActive.Contains(pindex)) {
143 entry.push_back(Pair("confirmations", 1 + chainActive.Height() - pindex->nHeight));
144 entry.push_back(Pair("time", pindex->GetBlockTime()));
145 entry.push_back(Pair("blocktime", pindex->GetBlockTime()));
148 entry.push_back(Pair("confirmations", 0));
153 Value getrawtransaction(const Array& params, bool fHelp)
155 if (fHelp || params.size() < 1 || params.size() > 2)
157 "getrawtransaction \"txid\" ( verbose )\n"
158 "\nNOTE: By default this function only works sometimes. This is when the tx is in the mempool\n"
159 "or there is an unspent output in the utxo for this transaction. To make it always work,\n"
160 "you need to maintain a transaction index, using the -txindex command line option.\n"
161 "\nReturn the raw transaction data.\n"
162 "\nIf verbose=0, returns a string that is serialized, hex-encoded data for 'txid'.\n"
163 "If verbose is non-zero, returns an Object with information about 'txid'.\n"
166 "1. \"txid\" (string, required) The transaction id\n"
167 "2. verbose (numeric, optional, default=0) If 0, return a string, other return a json object\n"
169 "\nResult (if verbose is not set or set to 0):\n"
170 "\"data\" (string) The serialized, hex-encoded data for 'txid'\n"
172 "\nResult (if verbose > 0):\n"
174 " \"hex\" : \"data\", (string) The serialized, hex-encoded data for 'txid'\n"
175 " \"txid\" : \"id\", (string) The transaction id (same as provided)\n"
176 " \"version\" : n, (numeric) The version\n"
177 " \"locktime\" : ttt, (numeric) The lock time\n"
178 " \"vin\" : [ (array of json objects)\n"
180 " \"txid\": \"id\", (string) The transaction id\n"
181 " \"vout\": n, (numeric) \n"
182 " \"scriptSig\": { (json object) The script\n"
183 " \"asm\": \"asm\", (string) asm\n"
184 " \"hex\": \"hex\" (string) hex\n"
186 " \"sequence\": n (numeric) The script sequence number\n"
190 " \"vout\" : [ (array of json objects)\n"
192 " \"value\" : x.xxx, (numeric) The value in btc\n"
193 " \"n\" : n, (numeric) index\n"
194 " \"scriptPubKey\" : { (json object)\n"
195 " \"asm\" : \"asm\", (string) the asm\n"
196 " \"hex\" : \"hex\", (string) the hex\n"
197 " \"reqSigs\" : n, (numeric) The required sigs\n"
198 " \"type\" : \"pubkeyhash\", (string) The type, eg 'pubkeyhash'\n"
199 " \"addresses\" : [ (json array of string)\n"
200 " \"bitcoinaddress\" (string) bitcoin address\n"
207 " \"blockhash\" : \"hash\", (string) the block hash\n"
208 " \"confirmations\" : n, (numeric) The confirmations\n"
209 " \"time\" : ttt, (numeric) The transaction time in seconds since epoch (Jan 1 1970 GMT)\n"
210 " \"blocktime\" : ttt (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n"
214 + HelpExampleCli("getrawtransaction", "\"mytxid\"")
215 + HelpExampleCli("getrawtransaction", "\"mytxid\" 1")
216 + HelpExampleRpc("getrawtransaction", "\"mytxid\", 1")
221 uint256 hash = ParseHashV(params[0], "parameter 1");
223 bool fVerbose = false;
224 if (params.size() > 1)
225 fVerbose = (params[1].get_int() != 0);
229 if (!GetTransaction(hash, tx, hashBlock, true))
230 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction");
232 string strHex = EncodeHexTx(tx);
238 result.push_back(Pair("hex", strHex));
239 TxToJSON(tx, hashBlock, result);
243 Value gettxoutproof(const Array& params, bool fHelp)
245 if (fHelp || (params.size() != 1 && params.size() != 2))
247 "gettxoutproof [\"txid\",...] ( blockhash )\n"
248 "\nReturns a hex-encoded proof that \"txid\" was included in a block.\n"
249 "\nNOTE: By default this function only works sometimes. This is when there is an\n"
250 "unspent output in the utxo for this transaction. To make it always work,\n"
251 "you need to maintain a transaction index, using the -txindex command line option or\n"
252 "specify the block in which the transaction is included in manually (by blockhash).\n"
253 "\nReturn the raw transaction data.\n"
255 "1. \"txids\" (string) A json array of txids to filter\n"
257 " \"txid\" (string) A transaction hash\n"
260 "2. \"block hash\" (string, optional) If specified, looks for txid in the block with this hash\n"
262 "\"data\" (string) A string that is a serialized, hex-encoded data for the proof.\n"
265 set<uint256> setTxids;
267 Array txids = params[0].get_array();
268 BOOST_FOREACH(Value& txid, txids) {
269 if (txid.get_str().length() != 64 || !IsHex(txid.get_str()))
270 throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid txid ")+txid.get_str());
271 uint256 hash(uint256S(txid.get_str()));
272 if (setTxids.count(hash))
273 throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated txid: ")+txid.get_str());
274 setTxids.insert(hash);
280 CBlockIndex* pblockindex = NULL;
283 if (params.size() > 1)
285 hashBlock = uint256S(params[1].get_str());
286 if (!mapBlockIndex.count(hashBlock))
287 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
288 pblockindex = mapBlockIndex[hashBlock];
291 if (pcoinsTip->GetCoins(oneTxid, coins) && coins.nHeight > 0 && coins.nHeight <= chainActive.Height())
292 pblockindex = chainActive[coins.nHeight];
295 if (pblockindex == NULL)
298 if (!GetTransaction(oneTxid, tx, hashBlock, false) || hashBlock.IsNull())
299 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not yet in block");
300 if (!mapBlockIndex.count(hashBlock))
301 throw JSONRPCError(RPC_INTERNAL_ERROR, "Transaction index corrupt");
302 pblockindex = mapBlockIndex[hashBlock];
306 if(!ReadBlockFromDisk(block, pblockindex))
307 throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
309 unsigned int ntxFound = 0;
310 BOOST_FOREACH(const CTransaction&tx, block.vtx)
311 if (setTxids.count(tx.GetHash()))
313 if (ntxFound != setTxids.size())
314 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "(Not all) transactions not found in specified block");
316 CDataStream ssMB(SER_NETWORK, PROTOCOL_VERSION);
317 CMerkleBlock mb(block, setTxids);
319 std::string strHex = HexStr(ssMB.begin(), ssMB.end());
323 Value verifytxoutproof(const Array& params, bool fHelp)
325 if (fHelp || params.size() != 1)
327 "verifytxoutproof \"proof\"\n"
328 "\nVerifies that a proof points to a transaction in a block, returning the transaction it commits to\n"
329 "and throwing an RPC error if the block is not in our best chain\n"
331 "1. \"proof\" (string, required) The hex-encoded proof generated by gettxoutproof\n"
333 "[\"txid\"] (array, strings) The txid(s) which the proof commits to, or empty array if the proof is invalid\n"
336 CDataStream ssMB(ParseHexV(params[0], "proof"), SER_NETWORK, PROTOCOL_VERSION);
337 CMerkleBlock merkleBlock;
342 vector<uint256> vMatch;
343 if (merkleBlock.txn.ExtractMatches(vMatch) != merkleBlock.header.hashMerkleRoot)
348 if (!mapBlockIndex.count(merkleBlock.header.GetHash()) || !chainActive.Contains(mapBlockIndex[merkleBlock.header.GetHash()]))
349 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
351 BOOST_FOREACH(const uint256& hash, vMatch)
352 res.push_back(hash.GetHex());
356 Value createrawtransaction(const Array& params, bool fHelp)
358 if (fHelp || params.size() != 2)
360 "createrawtransaction [{\"txid\":\"id\",\"vout\":n},...] {\"address\":amount,...}\n"
361 "\nCreate a transaction spending the given inputs and sending to the given addresses.\n"
362 "Returns hex-encoded raw transaction.\n"
363 "Note that the transaction's inputs are not signed, and\n"
364 "it is not stored in the wallet or transmitted to the network.\n"
367 "1. \"transactions\" (string, required) A json array of json objects\n"
370 " \"txid\":\"id\", (string, required) The transaction id\n"
371 " \"vout\":n (numeric, required) The output number\n"
375 "2. \"addresses\" (string, required) a json object with addresses as keys and amounts as values\n"
377 " \"address\": x.xxx (numeric, required) The key is the bitcoin address, the value is the btc amount\n"
382 "\"transaction\" (string) hex string of the transaction\n"
385 + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"{\\\"address\\\":0.01}\"")
386 + HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"{\\\"address\\\":0.01}\"")
390 RPCTypeCheck(params, boost::assign::list_of(array_type)(obj_type));
392 Array inputs = params[0].get_array();
393 Object sendTo = params[1].get_obj();
395 CMutableTransaction rawTx;
397 BOOST_FOREACH(const Value& input, inputs) {
398 const Object& o = input.get_obj();
400 uint256 txid = ParseHashO(o, "txid");
402 const Value& vout_v = find_value(o, "vout");
403 if (vout_v.type() != int_type)
404 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
405 int nOutput = vout_v.get_int();
407 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout must be positive");
409 CTxIn in(COutPoint(txid, nOutput));
410 rawTx.vin.push_back(in);
413 set<CBitcoinAddress> setAddress;
414 BOOST_FOREACH(const Pair& s, sendTo) {
415 CBitcoinAddress address(s.name_);
416 if (!address.IsValid())
417 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+s.name_);
419 if (setAddress.count(address))
420 throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+s.name_);
421 setAddress.insert(address);
423 CScript scriptPubKey = GetScriptForDestination(address.Get());
424 CAmount nAmount = AmountFromValue(s.value_);
426 CTxOut out(nAmount, scriptPubKey);
427 rawTx.vout.push_back(out);
430 return EncodeHexTx(rawTx);
433 Value decoderawtransaction(const Array& params, bool fHelp)
435 if (fHelp || params.size() != 1)
437 "decoderawtransaction \"hexstring\"\n"
438 "\nReturn a JSON object representing the serialized, hex-encoded transaction.\n"
441 "1. \"hex\" (string, required) The transaction hex string\n"
445 " \"txid\" : \"id\", (string) The transaction id\n"
446 " \"version\" : n, (numeric) The version\n"
447 " \"locktime\" : ttt, (numeric) The lock time\n"
448 " \"vin\" : [ (array of json objects)\n"
450 " \"txid\": \"id\", (string) The transaction id\n"
451 " \"vout\": n, (numeric) The output number\n"
452 " \"scriptSig\": { (json object) The script\n"
453 " \"asm\": \"asm\", (string) asm\n"
454 " \"hex\": \"hex\" (string) hex\n"
456 " \"sequence\": n (numeric) The script sequence number\n"
460 " \"vout\" : [ (array of json objects)\n"
462 " \"value\" : x.xxx, (numeric) The value in btc\n"
463 " \"n\" : n, (numeric) index\n"
464 " \"scriptPubKey\" : { (json object)\n"
465 " \"asm\" : \"asm\", (string) the asm\n"
466 " \"hex\" : \"hex\", (string) the hex\n"
467 " \"reqSigs\" : n, (numeric) The required sigs\n"
468 " \"type\" : \"pubkeyhash\", (string) The type, eg 'pubkeyhash'\n"
469 " \"addresses\" : [ (json array of string)\n"
470 " \"12tvKAXCxZjSmdNbao16dKXC8tRWfcF5oc\" (string) bitcoin address\n"
480 + HelpExampleCli("decoderawtransaction", "\"hexstring\"")
481 + HelpExampleRpc("decoderawtransaction", "\"hexstring\"")
485 RPCTypeCheck(params, boost::assign::list_of(str_type));
489 if (!DecodeHexTx(tx, params[0].get_str()))
490 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
493 TxToJSON(tx, uint256(), result);
498 Value decodescript(const Array& params, bool fHelp)
500 if (fHelp || params.size() != 1)
502 "decodescript \"hex\"\n"
503 "\nDecode a hex-encoded script.\n"
505 "1. \"hex\" (string) the hex encoded script\n"
508 " \"asm\":\"asm\", (string) Script public key\n"
509 " \"hex\":\"hex\", (string) hex encoded public key\n"
510 " \"type\":\"type\", (string) The output type\n"
511 " \"reqSigs\": n, (numeric) The required signatures\n"
512 " \"addresses\": [ (json array of string)\n"
513 " \"address\" (string) bitcoin address\n"
516 " \"p2sh\",\"address\" (string) script address\n"
519 + HelpExampleCli("decodescript", "\"hexstring\"")
520 + HelpExampleRpc("decodescript", "\"hexstring\"")
524 RPCTypeCheck(params, boost::assign::list_of(str_type));
528 if (params[0].get_str().size() > 0){
529 vector<unsigned char> scriptData(ParseHexV(params[0], "argument"));
530 script = CScript(scriptData.begin(), scriptData.end());
532 // Empty scripts are valid
534 ScriptPubKeyToJSON(script, r, false);
536 r.push_back(Pair("p2sh", CBitcoinAddress(CScriptID(script)).ToString()));
540 /** Pushes a JSON object for script verification or signing errors to vErrorsRet. */
541 static void TxInErrorToJSON(const CTxIn& txin, Array& vErrorsRet, const std::string& strMessage)
544 entry.push_back(Pair("txid", txin.prevout.hash.ToString()));
545 entry.push_back(Pair("vout", (uint64_t)txin.prevout.n));
546 entry.push_back(Pair("scriptSig", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
547 entry.push_back(Pair("sequence", (uint64_t)txin.nSequence));
548 entry.push_back(Pair("error", strMessage));
549 vErrorsRet.push_back(entry);
552 Value signrawtransaction(const Array& params, bool fHelp)
554 if (fHelp || params.size() < 1 || params.size() > 4)
556 "signrawtransaction \"hexstring\" ( [{\"txid\":\"id\",\"vout\":n,\"scriptPubKey\":\"hex\",\"redeemScript\":\"hex\"},...] [\"privatekey1\",...] sighashtype )\n"
557 "\nSign inputs for raw transaction (serialized, hex-encoded).\n"
558 "The second optional argument (may be null) is an array of previous transaction outputs that\n"
559 "this transaction depends on but may not yet be in the block chain.\n"
560 "The third optional argument (may be null) is an array of base58-encoded private\n"
561 "keys that, if given, will be the only keys used to sign the transaction.\n"
563 + HelpRequiringPassphrase() + "\n"
567 "1. \"hexstring\" (string, required) The transaction hex string\n"
568 "2. \"prevtxs\" (string, optional) An json array of previous dependent transaction outputs\n"
569 " [ (json array of json objects, or 'null' if none provided)\n"
571 " \"txid\":\"id\", (string, required) The transaction id\n"
572 " \"vout\":n, (numeric, required) The output number\n"
573 " \"scriptPubKey\": \"hex\", (string, required) script key\n"
574 " \"redeemScript\": \"hex\" (string, required for P2SH) redeem script\n"
578 "3. \"privatekeys\" (string, optional) A json array of base58-encoded private keys for signing\n"
579 " [ (json array of strings, or 'null' if none provided)\n"
580 " \"privatekey\" (string) private key in base58-encoding\n"
583 "4. \"sighashtype\" (string, optional, default=ALL) The signature hash type. Must be one of\n"
587 " \"ALL|ANYONECANPAY\"\n"
588 " \"NONE|ANYONECANPAY\"\n"
589 " \"SINGLE|ANYONECANPAY\"\n"
593 " \"hex\" : \"value\", (string) The hex-encoded raw transaction with signature(s)\n"
594 " \"complete\" : true|false, (boolean) If the transaction has a complete set of signatures\n"
595 " \"errors\" : [ (json array of objects) Script verification errors (if there are any)\n"
597 " \"txid\" : \"hash\", (string) The hash of the referenced, previous transaction\n"
598 " \"vout\" : n, (numeric) The index of the output to spent and used as input\n"
599 " \"scriptSig\" : \"hex\", (string) The hex-encoded signature script\n"
600 " \"sequence\" : n, (numeric) Script sequence number\n"
601 " \"error\" : \"text\" (string) Verification or signing error related to the input\n"
608 + HelpExampleCli("signrawtransaction", "\"myhex\"")
609 + HelpExampleRpc("signrawtransaction", "\"myhex\"")
613 LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
617 RPCTypeCheck(params, boost::assign::list_of(str_type)(array_type)(array_type)(str_type), true);
619 vector<unsigned char> txData(ParseHexV(params[0], "argument 1"));
620 CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
621 vector<CMutableTransaction> txVariants;
622 while (!ssData.empty()) {
624 CMutableTransaction tx;
626 txVariants.push_back(tx);
628 catch (const std::exception&) {
629 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
633 if (txVariants.empty())
634 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Missing transaction");
636 // mergedTx will end up with all the signatures; it
637 // starts as a clone of the rawtx:
638 CMutableTransaction mergedTx(txVariants[0]);
640 // Fetch previous transactions (inputs):
641 CCoinsView viewDummy;
642 CCoinsViewCache view(&viewDummy);
645 CCoinsViewCache &viewChain = *pcoinsTip;
646 CCoinsViewMemPool viewMempool(&viewChain, mempool);
647 view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
649 BOOST_FOREACH(const CTxIn& txin, mergedTx.vin) {
650 const uint256& prevHash = txin.prevout.hash;
652 view.AccessCoins(prevHash); // this is certainly allowed to fail
655 view.SetBackend(viewDummy); // switch back to avoid locking mempool for too long
658 bool fGivenKeys = false;
659 CBasicKeyStore tempKeystore;
660 if (params.size() > 2 && params[2].type() != null_type) {
662 Array keys = params[2].get_array();
663 BOOST_FOREACH(Value k, keys) {
664 CBitcoinSecret vchSecret;
665 bool fGood = vchSecret.SetString(k.get_str());
667 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
668 CKey key = vchSecret.GetKey();
670 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Private key outside allowed range");
671 tempKeystore.AddKey(key);
675 else if (pwalletMain)
676 EnsureWalletIsUnlocked();
679 // Add previous txouts given in the RPC call:
680 if (params.size() > 1 && params[1].type() != null_type) {
681 Array prevTxs = params[1].get_array();
682 BOOST_FOREACH(Value& p, prevTxs) {
683 if (p.type() != obj_type)
684 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}");
686 Object prevOut = p.get_obj();
688 RPCTypeCheck(prevOut, boost::assign::map_list_of("txid", str_type)("vout", int_type)("scriptPubKey", str_type));
690 uint256 txid = ParseHashO(prevOut, "txid");
692 int nOut = find_value(prevOut, "vout").get_int();
694 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout must be positive");
696 vector<unsigned char> pkData(ParseHexO(prevOut, "scriptPubKey"));
697 CScript scriptPubKey(pkData.begin(), pkData.end());
700 CCoinsModifier coins = view.ModifyCoins(txid);
701 if (coins->IsAvailable(nOut) && coins->vout[nOut].scriptPubKey != scriptPubKey) {
702 string err("Previous output scriptPubKey mismatch:\n");
703 err = err + coins->vout[nOut].scriptPubKey.ToString() + "\nvs:\n"+
704 scriptPubKey.ToString();
705 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, err);
707 if ((unsigned int)nOut >= coins->vout.size())
708 coins->vout.resize(nOut+1);
709 coins->vout[nOut].scriptPubKey = scriptPubKey;
710 coins->vout[nOut].nValue = 0; // we don't know the actual output value
713 // if redeemScript given and not using the local wallet (private keys
714 // given), add redeemScript to the tempKeystore so it can be signed:
715 if (fGivenKeys && scriptPubKey.IsPayToScriptHash()) {
716 RPCTypeCheck(prevOut, boost::assign::map_list_of("txid", str_type)("vout", int_type)("scriptPubKey", str_type)("redeemScript",str_type));
717 Value v = find_value(prevOut, "redeemScript");
718 if (!(v == Value::null)) {
719 vector<unsigned char> rsData(ParseHexV(v, "redeemScript"));
720 CScript redeemScript(rsData.begin(), rsData.end());
721 tempKeystore.AddCScript(redeemScript);
728 const CKeyStore& keystore = ((fGivenKeys || !pwalletMain) ? tempKeystore : *pwalletMain);
730 const CKeyStore& keystore = tempKeystore;
733 int nHashType = SIGHASH_ALL;
734 if (params.size() > 3 && params[3].type() != null_type) {
735 static map<string, int> mapSigHashValues =
736 boost::assign::map_list_of
737 (string("ALL"), int(SIGHASH_ALL))
738 (string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY))
739 (string("NONE"), int(SIGHASH_NONE))
740 (string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY))
741 (string("SINGLE"), int(SIGHASH_SINGLE))
742 (string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY))
744 string strHashType = params[3].get_str();
745 if (mapSigHashValues.count(strHashType))
746 nHashType = mapSigHashValues[strHashType];
748 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid sighash param");
751 bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
753 // Script verification errors
757 for (unsigned int i = 0; i < mergedTx.vin.size(); i++) {
758 CTxIn& txin = mergedTx.vin[i];
759 const CCoins* coins = view.AccessCoins(txin.prevout.hash);
760 if (coins == NULL || !coins->IsAvailable(txin.prevout.n)) {
761 TxInErrorToJSON(txin, vErrors, "Input not found or already spent");
764 const CScript& prevPubKey = coins->vout[txin.prevout.n].scriptPubKey;
766 txin.scriptSig.clear();
767 // Only sign SIGHASH_SINGLE if there's a corresponding output:
768 if (!fHashSingle || (i < mergedTx.vout.size()))
769 SignSignature(keystore, prevPubKey, mergedTx, i, nHashType);
771 // ... and merge in other signatures:
772 BOOST_FOREACH(const CMutableTransaction& txv, txVariants) {
773 txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig);
775 ScriptError serror = SCRIPT_ERR_OK;
776 if (!VerifyScript(txin.scriptSig, prevPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker(&mergedTx, i), &serror)) {
777 TxInErrorToJSON(txin, vErrors, ScriptErrorString(serror));
780 bool fComplete = vErrors.empty();
783 result.push_back(Pair("hex", EncodeHexTx(mergedTx)));
784 result.push_back(Pair("complete", fComplete));
785 if (!vErrors.empty()) {
786 result.push_back(Pair("errors", vErrors));
792 Value sendrawtransaction(const Array& params, bool fHelp)
794 if (fHelp || params.size() < 1 || params.size() > 2)
796 "sendrawtransaction \"hexstring\" ( allowhighfees )\n"
797 "\nSubmits raw transaction (serialized, hex-encoded) to local node and network.\n"
798 "\nAlso see createrawtransaction and signrawtransaction calls.\n"
800 "1. \"hexstring\" (string, required) The hex string of the raw transaction)\n"
801 "2. allowhighfees (boolean, optional, default=false) Allow high fees\n"
803 "\"hex\" (string) The transaction hash in hex\n"
805 "\nCreate a transaction\n"
806 + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\" : \\\"mytxid\\\",\\\"vout\\\":0}]\" \"{\\\"myaddress\\\":0.01}\"") +
807 "Sign the transaction, and get back the hex\n"
808 + HelpExampleCli("signrawtransaction", "\"myhex\"") +
809 "\nSend the transaction (signed hex)\n"
810 + HelpExampleCli("sendrawtransaction", "\"signedhex\"") +
811 "\nAs a json rpc call\n"
812 + HelpExampleRpc("sendrawtransaction", "\"signedhex\"")
816 RPCTypeCheck(params, boost::assign::list_of(str_type)(bool_type));
818 // parse hex string from parameter
820 if (!DecodeHexTx(tx, params[0].get_str()))
821 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
822 uint256 hashTx = tx.GetHash();
824 bool fOverrideFees = false;
825 if (params.size() > 1)
826 fOverrideFees = params[1].get_bool();
828 CCoinsViewCache &view = *pcoinsTip;
829 const CCoins* existingCoins = view.AccessCoins(hashTx);
830 bool fHaveMempool = mempool.exists(hashTx);
831 bool fHaveChain = existingCoins && existingCoins->nHeight < 1000000000;
832 if (!fHaveMempool && !fHaveChain) {
833 // push to local node and sync with wallets
834 CValidationState state;
836 if (!AcceptToMemoryPool(mempool, state, tx, false, &fMissingInputs, !fOverrideFees)) {
837 if (state.IsInvalid()) {
838 throw JSONRPCError(RPC_TRANSACTION_REJECTED, strprintf("%i: %s", state.GetRejectCode(), state.GetRejectReason()));
840 if (fMissingInputs) {
841 throw JSONRPCError(RPC_TRANSACTION_ERROR, "Missing inputs");
843 throw JSONRPCError(RPC_TRANSACTION_ERROR, state.GetRejectReason());
846 } else if (fHaveChain) {
847 throw JSONRPCError(RPC_TRANSACTION_ALREADY_IN_CHAIN, "transaction already in block chain");
849 RelayTransaction(tx);
851 return hashTx.GetHex();