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));
58 void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry)
60 entry.push_back(Pair("txid", tx.GetHash().GetHex()));
61 entry.push_back(Pair("version", tx.nVersion));
62 entry.push_back(Pair("locktime", (int64_t)tx.nLockTime));
64 BOOST_FOREACH(const CTxIn& txin, tx.vin) {
67 in.push_back(Pair("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
69 in.push_back(Pair("txid", txin.prevout.hash.GetHex()));
70 in.push_back(Pair("vout", (int64_t)txin.prevout.n));
72 o.push_back(Pair("asm", txin.scriptSig.ToString()));
73 o.push_back(Pair("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
74 in.push_back(Pair("scriptSig", o));
76 in.push_back(Pair("sequence", (int64_t)txin.nSequence));
79 entry.push_back(Pair("vin", vin));
81 for (unsigned int i = 0; i < tx.vout.size(); i++) {
82 const CTxOut& txout = tx.vout[i];
84 out.push_back(Pair("value", ValueFromAmount(txout.nValue)));
85 out.push_back(Pair("n", (int64_t)i));
87 ScriptPubKeyToJSON(txout.scriptPubKey, o, true);
88 out.push_back(Pair("scriptPubKey", o));
91 entry.push_back(Pair("vout", vout));
94 for (unsigned int i = 0; i < tx.vjoinsplit.size(); i++) {
95 const JSDescription& jsdescription = tx.vjoinsplit[i];
98 joinsplit.push_back(Pair("anchor", jsdescription.anchor.GetHex()));
102 BOOST_FOREACH(const uint256 nf, jsdescription.nullifiers) {
103 nullifiers.push_back(nf.GetHex());
105 joinsplit.push_back(Pair("nullifiers", nullifiers));
110 BOOST_FOREACH(const uint256 commitment, jsdescription.commitments) {
111 commitments.push_back(commitment.GetHex());
113 joinsplit.push_back(Pair("commitments", commitments));
118 BOOST_FOREACH(const uint256 mac, jsdescription.macs) {
119 macs.push_back(mac.GetHex());
121 joinsplit.push_back(Pair("macs", macs));
124 joinsplit.push_back(Pair("vpub_old", ValueFromAmount(jsdescription.vpub_old)));
125 joinsplit.push_back(Pair("vpub_new", ValueFromAmount(jsdescription.vpub_new)));
127 vjoinsplit.push_back(joinsplit);
130 entry.push_back(Pair("vjoinsplit", vjoinsplit));
132 if (!hashBlock.IsNull()) {
133 entry.push_back(Pair("blockhash", hashBlock.GetHex()));
134 BlockMap::iterator mi = mapBlockIndex.find(hashBlock);
135 if (mi != mapBlockIndex.end() && (*mi).second) {
136 CBlockIndex* pindex = (*mi).second;
137 if (chainActive.Contains(pindex)) {
138 entry.push_back(Pair("confirmations", 1 + chainActive.Height() - pindex->nHeight));
139 entry.push_back(Pair("time", pindex->GetBlockTime()));
140 entry.push_back(Pair("blocktime", pindex->GetBlockTime()));
143 entry.push_back(Pair("confirmations", 0));
148 Value getrawtransaction(const Array& params, bool fHelp)
150 if (fHelp || params.size() < 1 || params.size() > 2)
152 "getrawtransaction \"txid\" ( verbose )\n"
153 "\nNOTE: By default this function only works sometimes. This is when the tx is in the mempool\n"
154 "or there is an unspent output in the utxo for this transaction. To make it always work,\n"
155 "you need to maintain a transaction index, using the -txindex command line option.\n"
156 "\nReturn the raw transaction data.\n"
157 "\nIf verbose=0, returns a string that is serialized, hex-encoded data for 'txid'.\n"
158 "If verbose is non-zero, returns an Object with information about 'txid'.\n"
161 "1. \"txid\" (string, required) The transaction id\n"
162 "2. verbose (numeric, optional, default=0) If 0, return a string, other return a json object\n"
164 "\nResult (if verbose is not set or set to 0):\n"
165 "\"data\" (string) The serialized, hex-encoded data for 'txid'\n"
167 "\nResult (if verbose > 0):\n"
169 " \"hex\" : \"data\", (string) The serialized, hex-encoded data for 'txid'\n"
170 " \"txid\" : \"id\", (string) The transaction id (same as provided)\n"
171 " \"version\" : n, (numeric) The version\n"
172 " \"locktime\" : ttt, (numeric) The lock time\n"
173 " \"vin\" : [ (array of json objects)\n"
175 " \"txid\": \"id\", (string) The transaction id\n"
176 " \"vout\": n, (numeric) \n"
177 " \"scriptSig\": { (json object) The script\n"
178 " \"asm\": \"asm\", (string) asm\n"
179 " \"hex\": \"hex\" (string) hex\n"
181 " \"sequence\": n (numeric) The script sequence number\n"
185 " \"vout\" : [ (array of json objects)\n"
187 " \"value\" : x.xxx, (numeric) The value in btc\n"
188 " \"n\" : n, (numeric) index\n"
189 " \"scriptPubKey\" : { (json object)\n"
190 " \"asm\" : \"asm\", (string) the asm\n"
191 " \"hex\" : \"hex\", (string) the hex\n"
192 " \"reqSigs\" : n, (numeric) The required sigs\n"
193 " \"type\" : \"pubkeyhash\", (string) The type, eg 'pubkeyhash'\n"
194 " \"addresses\" : [ (json array of string)\n"
195 " \"bitcoinaddress\" (string) bitcoin address\n"
202 " \"blockhash\" : \"hash\", (string) the block hash\n"
203 " \"confirmations\" : n, (numeric) The confirmations\n"
204 " \"time\" : ttt, (numeric) The transaction time in seconds since epoch (Jan 1 1970 GMT)\n"
205 " \"blocktime\" : ttt (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n"
209 + HelpExampleCli("getrawtransaction", "\"mytxid\"")
210 + HelpExampleCli("getrawtransaction", "\"mytxid\" 1")
211 + HelpExampleRpc("getrawtransaction", "\"mytxid\", 1")
216 uint256 hash = ParseHashV(params[0], "parameter 1");
218 bool fVerbose = false;
219 if (params.size() > 1)
220 fVerbose = (params[1].get_int() != 0);
224 if (!GetTransaction(hash, tx, hashBlock, true))
225 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction");
227 string strHex = EncodeHexTx(tx);
233 result.push_back(Pair("hex", strHex));
234 TxToJSON(tx, hashBlock, result);
238 Value gettxoutproof(const Array& params, bool fHelp)
240 if (fHelp || (params.size() != 1 && params.size() != 2))
242 "gettxoutproof [\"txid\",...] ( blockhash )\n"
243 "\nReturns a hex-encoded proof that \"txid\" was included in a block.\n"
244 "\nNOTE: By default this function only works sometimes. This is when there is an\n"
245 "unspent output in the utxo for this transaction. To make it always work,\n"
246 "you need to maintain a transaction index, using the -txindex command line option or\n"
247 "specify the block in which the transaction is included in manually (by blockhash).\n"
248 "\nReturn the raw transaction data.\n"
250 "1. \"txids\" (string) A json array of txids to filter\n"
252 " \"txid\" (string) A transaction hash\n"
255 "2. \"block hash\" (string, optional) If specified, looks for txid in the block with this hash\n"
257 "\"data\" (string) A string that is a serialized, hex-encoded data for the proof.\n"
260 set<uint256> setTxids;
262 Array txids = params[0].get_array();
263 BOOST_FOREACH(Value& txid, txids) {
264 if (txid.get_str().length() != 64 || !IsHex(txid.get_str()))
265 throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid txid ")+txid.get_str());
266 uint256 hash(uint256S(txid.get_str()));
267 if (setTxids.count(hash))
268 throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated txid: ")+txid.get_str());
269 setTxids.insert(hash);
275 CBlockIndex* pblockindex = NULL;
278 if (params.size() > 1)
280 hashBlock = uint256S(params[1].get_str());
281 if (!mapBlockIndex.count(hashBlock))
282 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
283 pblockindex = mapBlockIndex[hashBlock];
286 if (pcoinsTip->GetCoins(oneTxid, coins) && coins.nHeight > 0 && coins.nHeight <= chainActive.Height())
287 pblockindex = chainActive[coins.nHeight];
290 if (pblockindex == NULL)
293 if (!GetTransaction(oneTxid, tx, hashBlock, false) || hashBlock.IsNull())
294 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not yet in block");
295 if (!mapBlockIndex.count(hashBlock))
296 throw JSONRPCError(RPC_INTERNAL_ERROR, "Transaction index corrupt");
297 pblockindex = mapBlockIndex[hashBlock];
301 if(!ReadBlockFromDisk(block, pblockindex))
302 throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
304 unsigned int ntxFound = 0;
305 BOOST_FOREACH(const CTransaction&tx, block.vtx)
306 if (setTxids.count(tx.GetHash()))
308 if (ntxFound != setTxids.size())
309 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "(Not all) transactions not found in specified block");
311 CDataStream ssMB(SER_NETWORK, PROTOCOL_VERSION);
312 CMerkleBlock mb(block, setTxids);
314 std::string strHex = HexStr(ssMB.begin(), ssMB.end());
318 Value verifytxoutproof(const Array& params, bool fHelp)
320 if (fHelp || params.size() != 1)
322 "verifytxoutproof \"proof\"\n"
323 "\nVerifies that a proof points to a transaction in a block, returning the transaction it commits to\n"
324 "and throwing an RPC error if the block is not in our best chain\n"
326 "1. \"proof\" (string, required) The hex-encoded proof generated by gettxoutproof\n"
328 "[\"txid\"] (array, strings) The txid(s) which the proof commits to, or empty array if the proof is invalid\n"
331 CDataStream ssMB(ParseHexV(params[0], "proof"), SER_NETWORK, PROTOCOL_VERSION);
332 CMerkleBlock merkleBlock;
337 vector<uint256> vMatch;
338 if (merkleBlock.txn.ExtractMatches(vMatch) != merkleBlock.header.hashMerkleRoot)
343 if (!mapBlockIndex.count(merkleBlock.header.GetHash()) || !chainActive.Contains(mapBlockIndex[merkleBlock.header.GetHash()]))
344 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
346 BOOST_FOREACH(const uint256& hash, vMatch)
347 res.push_back(hash.GetHex());
351 Value createrawtransaction(const Array& params, bool fHelp)
353 if (fHelp || params.size() != 2)
355 "createrawtransaction [{\"txid\":\"id\",\"vout\":n},...] {\"address\":amount,...}\n"
356 "\nCreate a transaction spending the given inputs and sending to the given addresses.\n"
357 "Returns hex-encoded raw transaction.\n"
358 "Note that the transaction's inputs are not signed, and\n"
359 "it is not stored in the wallet or transmitted to the network.\n"
362 "1. \"transactions\" (string, required) A json array of json objects\n"
365 " \"txid\":\"id\", (string, required) The transaction id\n"
366 " \"vout\":n (numeric, required) The output number\n"
370 "2. \"addresses\" (string, required) a json object with addresses as keys and amounts as values\n"
372 " \"address\": x.xxx (numeric, required) The key is the bitcoin address, the value is the btc amount\n"
377 "\"transaction\" (string) hex string of the transaction\n"
380 + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"{\\\"address\\\":0.01}\"")
381 + HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"{\\\"address\\\":0.01}\"")
385 RPCTypeCheck(params, boost::assign::list_of(array_type)(obj_type));
387 Array inputs = params[0].get_array();
388 Object sendTo = params[1].get_obj();
390 CMutableTransaction rawTx;
392 BOOST_FOREACH(const Value& input, inputs) {
393 const Object& o = input.get_obj();
395 uint256 txid = ParseHashO(o, "txid");
397 const Value& vout_v = find_value(o, "vout");
398 if (vout_v.type() != int_type)
399 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
400 int nOutput = vout_v.get_int();
402 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout must be positive");
404 CTxIn in(COutPoint(txid, nOutput));
405 rawTx.vin.push_back(in);
408 set<CBitcoinAddress> setAddress;
409 BOOST_FOREACH(const Pair& s, sendTo) {
410 CBitcoinAddress address(s.name_);
411 if (!address.IsValid())
412 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+s.name_);
414 if (setAddress.count(address))
415 throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+s.name_);
416 setAddress.insert(address);
418 CScript scriptPubKey = GetScriptForDestination(address.Get());
419 CAmount nAmount = AmountFromValue(s.value_);
421 CTxOut out(nAmount, scriptPubKey);
422 rawTx.vout.push_back(out);
425 return EncodeHexTx(rawTx);
428 Value decoderawtransaction(const Array& params, bool fHelp)
430 if (fHelp || params.size() != 1)
432 "decoderawtransaction \"hexstring\"\n"
433 "\nReturn a JSON object representing the serialized, hex-encoded transaction.\n"
436 "1. \"hex\" (string, required) The transaction hex string\n"
440 " \"txid\" : \"id\", (string) The transaction id\n"
441 " \"version\" : n, (numeric) The version\n"
442 " \"locktime\" : ttt, (numeric) The lock time\n"
443 " \"vin\" : [ (array of json objects)\n"
445 " \"txid\": \"id\", (string) The transaction id\n"
446 " \"vout\": n, (numeric) The output number\n"
447 " \"scriptSig\": { (json object) The script\n"
448 " \"asm\": \"asm\", (string) asm\n"
449 " \"hex\": \"hex\" (string) hex\n"
451 " \"sequence\": n (numeric) The script sequence number\n"
455 " \"vout\" : [ (array of json objects)\n"
457 " \"value\" : x.xxx, (numeric) The value in btc\n"
458 " \"n\" : n, (numeric) index\n"
459 " \"scriptPubKey\" : { (json object)\n"
460 " \"asm\" : \"asm\", (string) the asm\n"
461 " \"hex\" : \"hex\", (string) the hex\n"
462 " \"reqSigs\" : n, (numeric) The required sigs\n"
463 " \"type\" : \"pubkeyhash\", (string) The type, eg 'pubkeyhash'\n"
464 " \"addresses\" : [ (json array of string)\n"
465 " \"12tvKAXCxZjSmdNbao16dKXC8tRWfcF5oc\" (string) bitcoin address\n"
475 + HelpExampleCli("decoderawtransaction", "\"hexstring\"")
476 + HelpExampleRpc("decoderawtransaction", "\"hexstring\"")
480 RPCTypeCheck(params, boost::assign::list_of(str_type));
484 if (!DecodeHexTx(tx, params[0].get_str()))
485 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
488 TxToJSON(tx, uint256(), result);
493 Value decodescript(const Array& params, bool fHelp)
495 if (fHelp || params.size() != 1)
497 "decodescript \"hex\"\n"
498 "\nDecode a hex-encoded script.\n"
500 "1. \"hex\" (string) the hex encoded script\n"
503 " \"asm\":\"asm\", (string) Script public key\n"
504 " \"hex\":\"hex\", (string) hex encoded public key\n"
505 " \"type\":\"type\", (string) The output type\n"
506 " \"reqSigs\": n, (numeric) The required signatures\n"
507 " \"addresses\": [ (json array of string)\n"
508 " \"address\" (string) bitcoin address\n"
511 " \"p2sh\",\"address\" (string) script address\n"
514 + HelpExampleCli("decodescript", "\"hexstring\"")
515 + HelpExampleRpc("decodescript", "\"hexstring\"")
519 RPCTypeCheck(params, boost::assign::list_of(str_type));
523 if (params[0].get_str().size() > 0){
524 vector<unsigned char> scriptData(ParseHexV(params[0], "argument"));
525 script = CScript(scriptData.begin(), scriptData.end());
527 // Empty scripts are valid
529 ScriptPubKeyToJSON(script, r, false);
531 r.push_back(Pair("p2sh", CBitcoinAddress(CScriptID(script)).ToString()));
535 /** Pushes a JSON object for script verification or signing errors to vErrorsRet. */
536 static void TxInErrorToJSON(const CTxIn& txin, Array& vErrorsRet, const std::string& strMessage)
539 entry.push_back(Pair("txid", txin.prevout.hash.ToString()));
540 entry.push_back(Pair("vout", (uint64_t)txin.prevout.n));
541 entry.push_back(Pair("scriptSig", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
542 entry.push_back(Pair("sequence", (uint64_t)txin.nSequence));
543 entry.push_back(Pair("error", strMessage));
544 vErrorsRet.push_back(entry);
547 Value signrawtransaction(const Array& params, bool fHelp)
549 if (fHelp || params.size() < 1 || params.size() > 4)
551 "signrawtransaction \"hexstring\" ( [{\"txid\":\"id\",\"vout\":n,\"scriptPubKey\":\"hex\",\"redeemScript\":\"hex\"},...] [\"privatekey1\",...] sighashtype )\n"
552 "\nSign inputs for raw transaction (serialized, hex-encoded).\n"
553 "The second optional argument (may be null) is an array of previous transaction outputs that\n"
554 "this transaction depends on but may not yet be in the block chain.\n"
555 "The third optional argument (may be null) is an array of base58-encoded private\n"
556 "keys that, if given, will be the only keys used to sign the transaction.\n"
558 + HelpRequiringPassphrase() + "\n"
562 "1. \"hexstring\" (string, required) The transaction hex string\n"
563 "2. \"prevtxs\" (string, optional) An json array of previous dependent transaction outputs\n"
564 " [ (json array of json objects, or 'null' if none provided)\n"
566 " \"txid\":\"id\", (string, required) The transaction id\n"
567 " \"vout\":n, (numeric, required) The output number\n"
568 " \"scriptPubKey\": \"hex\", (string, required) script key\n"
569 " \"redeemScript\": \"hex\" (string, required for P2SH) redeem script\n"
573 "3. \"privatekeys\" (string, optional) A json array of base58-encoded private keys for signing\n"
574 " [ (json array of strings, or 'null' if none provided)\n"
575 " \"privatekey\" (string) private key in base58-encoding\n"
578 "4. \"sighashtype\" (string, optional, default=ALL) The signature hash type. Must be one of\n"
582 " \"ALL|ANYONECANPAY\"\n"
583 " \"NONE|ANYONECANPAY\"\n"
584 " \"SINGLE|ANYONECANPAY\"\n"
588 " \"hex\" : \"value\", (string) The hex-encoded raw transaction with signature(s)\n"
589 " \"complete\" : true|false, (boolean) If the transaction has a complete set of signatures\n"
590 " \"errors\" : [ (json array of objects) Script verification errors (if there are any)\n"
592 " \"txid\" : \"hash\", (string) The hash of the referenced, previous transaction\n"
593 " \"vout\" : n, (numeric) The index of the output to spent and used as input\n"
594 " \"scriptSig\" : \"hex\", (string) The hex-encoded signature script\n"
595 " \"sequence\" : n, (numeric) Script sequence number\n"
596 " \"error\" : \"text\" (string) Verification or signing error related to the input\n"
603 + HelpExampleCli("signrawtransaction", "\"myhex\"")
604 + HelpExampleRpc("signrawtransaction", "\"myhex\"")
608 LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
612 RPCTypeCheck(params, boost::assign::list_of(str_type)(array_type)(array_type)(str_type), true);
614 vector<unsigned char> txData(ParseHexV(params[0], "argument 1"));
615 CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
616 vector<CMutableTransaction> txVariants;
617 while (!ssData.empty()) {
619 CMutableTransaction tx;
621 txVariants.push_back(tx);
623 catch (const std::exception&) {
624 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
628 if (txVariants.empty())
629 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Missing transaction");
631 // mergedTx will end up with all the signatures; it
632 // starts as a clone of the rawtx:
633 CMutableTransaction mergedTx(txVariants[0]);
635 // Fetch previous transactions (inputs):
636 CCoinsView viewDummy;
637 CCoinsViewCache view(&viewDummy);
640 CCoinsViewCache &viewChain = *pcoinsTip;
641 CCoinsViewMemPool viewMempool(&viewChain, mempool);
642 view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
644 BOOST_FOREACH(const CTxIn& txin, mergedTx.vin) {
645 const uint256& prevHash = txin.prevout.hash;
647 view.AccessCoins(prevHash); // this is certainly allowed to fail
650 view.SetBackend(viewDummy); // switch back to avoid locking mempool for too long
653 bool fGivenKeys = false;
654 CBasicKeyStore tempKeystore;
655 if (params.size() > 2 && params[2].type() != null_type) {
657 Array keys = params[2].get_array();
658 BOOST_FOREACH(Value k, keys) {
659 CBitcoinSecret vchSecret;
660 bool fGood = vchSecret.SetString(k.get_str());
662 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
663 CKey key = vchSecret.GetKey();
665 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Private key outside allowed range");
666 tempKeystore.AddKey(key);
670 else if (pwalletMain)
671 EnsureWalletIsUnlocked();
674 // Add previous txouts given in the RPC call:
675 if (params.size() > 1 && params[1].type() != null_type) {
676 Array prevTxs = params[1].get_array();
677 BOOST_FOREACH(Value& p, prevTxs) {
678 if (p.type() != obj_type)
679 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}");
681 Object prevOut = p.get_obj();
683 RPCTypeCheck(prevOut, boost::assign::map_list_of("txid", str_type)("vout", int_type)("scriptPubKey", str_type));
685 uint256 txid = ParseHashO(prevOut, "txid");
687 int nOut = find_value(prevOut, "vout").get_int();
689 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout must be positive");
691 vector<unsigned char> pkData(ParseHexO(prevOut, "scriptPubKey"));
692 CScript scriptPubKey(pkData.begin(), pkData.end());
695 CCoinsModifier coins = view.ModifyCoins(txid);
696 if (coins->IsAvailable(nOut) && coins->vout[nOut].scriptPubKey != scriptPubKey) {
697 string err("Previous output scriptPubKey mismatch:\n");
698 err = err + coins->vout[nOut].scriptPubKey.ToString() + "\nvs:\n"+
699 scriptPubKey.ToString();
700 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, err);
702 if ((unsigned int)nOut >= coins->vout.size())
703 coins->vout.resize(nOut+1);
704 coins->vout[nOut].scriptPubKey = scriptPubKey;
705 coins->vout[nOut].nValue = 0; // we don't know the actual output value
708 // if redeemScript given and not using the local wallet (private keys
709 // given), add redeemScript to the tempKeystore so it can be signed:
710 if (fGivenKeys && scriptPubKey.IsPayToScriptHash()) {
711 RPCTypeCheck(prevOut, boost::assign::map_list_of("txid", str_type)("vout", int_type)("scriptPubKey", str_type)("redeemScript",str_type));
712 Value v = find_value(prevOut, "redeemScript");
713 if (!(v == Value::null)) {
714 vector<unsigned char> rsData(ParseHexV(v, "redeemScript"));
715 CScript redeemScript(rsData.begin(), rsData.end());
716 tempKeystore.AddCScript(redeemScript);
723 const CKeyStore& keystore = ((fGivenKeys || !pwalletMain) ? tempKeystore : *pwalletMain);
725 const CKeyStore& keystore = tempKeystore;
728 int nHashType = SIGHASH_ALL;
729 if (params.size() > 3 && params[3].type() != null_type) {
730 static map<string, int> mapSigHashValues =
731 boost::assign::map_list_of
732 (string("ALL"), int(SIGHASH_ALL))
733 (string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY))
734 (string("NONE"), int(SIGHASH_NONE))
735 (string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY))
736 (string("SINGLE"), int(SIGHASH_SINGLE))
737 (string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY))
739 string strHashType = params[3].get_str();
740 if (mapSigHashValues.count(strHashType))
741 nHashType = mapSigHashValues[strHashType];
743 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid sighash param");
746 bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
748 // Script verification errors
752 for (unsigned int i = 0; i < mergedTx.vin.size(); i++) {
753 CTxIn& txin = mergedTx.vin[i];
754 const CCoins* coins = view.AccessCoins(txin.prevout.hash);
755 if (coins == NULL || !coins->IsAvailable(txin.prevout.n)) {
756 TxInErrorToJSON(txin, vErrors, "Input not found or already spent");
759 const CScript& prevPubKey = coins->vout[txin.prevout.n].scriptPubKey;
761 txin.scriptSig.clear();
762 // Only sign SIGHASH_SINGLE if there's a corresponding output:
763 if (!fHashSingle || (i < mergedTx.vout.size()))
764 SignSignature(keystore, prevPubKey, mergedTx, i, nHashType);
766 // ... and merge in other signatures:
767 BOOST_FOREACH(const CMutableTransaction& txv, txVariants) {
768 txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig);
770 ScriptError serror = SCRIPT_ERR_OK;
771 if (!VerifyScript(txin.scriptSig, prevPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker(&mergedTx, i), &serror)) {
772 TxInErrorToJSON(txin, vErrors, ScriptErrorString(serror));
775 bool fComplete = vErrors.empty();
778 result.push_back(Pair("hex", EncodeHexTx(mergedTx)));
779 result.push_back(Pair("complete", fComplete));
780 if (!vErrors.empty()) {
781 result.push_back(Pair("errors", vErrors));
787 Value sendrawtransaction(const Array& params, bool fHelp)
789 if (fHelp || params.size() < 1 || params.size() > 2)
791 "sendrawtransaction \"hexstring\" ( allowhighfees )\n"
792 "\nSubmits raw transaction (serialized, hex-encoded) to local node and network.\n"
793 "\nAlso see createrawtransaction and signrawtransaction calls.\n"
795 "1. \"hexstring\" (string, required) The hex string of the raw transaction)\n"
796 "2. allowhighfees (boolean, optional, default=false) Allow high fees\n"
798 "\"hex\" (string) The transaction hash in hex\n"
800 "\nCreate a transaction\n"
801 + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\" : \\\"mytxid\\\",\\\"vout\\\":0}]\" \"{\\\"myaddress\\\":0.01}\"") +
802 "Sign the transaction, and get back the hex\n"
803 + HelpExampleCli("signrawtransaction", "\"myhex\"") +
804 "\nSend the transaction (signed hex)\n"
805 + HelpExampleCli("sendrawtransaction", "\"signedhex\"") +
806 "\nAs a json rpc call\n"
807 + HelpExampleRpc("sendrawtransaction", "\"signedhex\"")
811 RPCTypeCheck(params, boost::assign::list_of(str_type)(bool_type));
813 // parse hex string from parameter
815 if (!DecodeHexTx(tx, params[0].get_str()))
816 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
817 uint256 hashTx = tx.GetHash();
819 bool fOverrideFees = false;
820 if (params.size() > 1)
821 fOverrideFees = params[1].get_bool();
823 CCoinsViewCache &view = *pcoinsTip;
824 const CCoins* existingCoins = view.AccessCoins(hashTx);
825 bool fHaveMempool = mempool.exists(hashTx);
826 bool fHaveChain = existingCoins && existingCoins->nHeight < 1000000000;
827 if (!fHaveMempool && !fHaveChain) {
828 // push to local node and sync with wallets
829 CValidationState state;
831 if (!AcceptToMemoryPool(mempool, state, tx, false, &fMissingInputs, !fOverrideFees)) {
832 if (state.IsInvalid()) {
833 throw JSONRPCError(RPC_TRANSACTION_REJECTED, strprintf("%i: %s", state.GetRejectCode(), state.GetRejectReason()));
835 if (fMissingInputs) {
836 throw JSONRPCError(RPC_TRANSACTION_ERROR, "Missing inputs");
838 throw JSONRPCError(RPC_TRANSACTION_ERROR, state.GetRejectReason());
841 } else if (fHaveChain) {
842 throw JSONRPCError(RPC_TRANSACTION_ALREADY_IN_CHAIN, "transaction already in block chain");
844 RelayTransaction(tx);
846 return hashTx.GetHex();