Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
rpcrawtransaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "base58.h"
7 #include "core.h"
8 #include "init.h"
9 #include "keystore.h"
10 #include "main.h"
11 #include "net.h"
12 #include "rpcserver.h"
13 #include "uint256.h"
14 #ifdef ENABLE_WALLET
15 #include "wallet.h"
16 #endif
17 
18 #include <stdint.h>
19 
20 #include <boost/assign/list_of.hpp>
21 #include "json/json_spirit_utils.h"
22 #include "json/json_spirit_value.h"
23 
24 using namespace std;
25 using namespace boost;
26 using namespace boost::assign;
27 using namespace json_spirit;
28 
29 void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out, bool fIncludeHex)
30 {
31  txnouttype type;
32  vector<CTxDestination> addresses;
33  int nRequired;
34 
35  out.push_back(Pair("asm", scriptPubKey.ToString()));
36  if (fIncludeHex)
37  out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
38 
39  if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired))
40  {
41  out.push_back(Pair("type", GetTxnOutputType(type)));
42  return;
43  }
44 
45  out.push_back(Pair("reqSigs", nRequired));
46  out.push_back(Pair("type", GetTxnOutputType(type)));
47 
48  Array a;
49  BOOST_FOREACH(const CTxDestination& addr, addresses)
50  a.push_back(CBitcoinAddress(addr).ToString());
51  out.push_back(Pair("addresses", a));
52 }
53 
54 void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry)
55 {
56  entry.push_back(Pair("txid", tx.GetHash().GetHex()));
57  entry.push_back(Pair("version", tx.nVersion));
58  entry.push_back(Pair("locktime", (int64_t)tx.nLockTime));
59  Array vin;
60  BOOST_FOREACH(const CTxIn& txin, tx.vin)
61  {
62  Object in;
63  if (tx.IsCoinBase())
64  in.push_back(Pair("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
65  else
66  {
67  in.push_back(Pair("txid", txin.prevout.hash.GetHex()));
68  in.push_back(Pair("vout", (int64_t)txin.prevout.n));
69  Object o;
70  o.push_back(Pair("asm", txin.scriptSig.ToString()));
71  o.push_back(Pair("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
72  in.push_back(Pair("scriptSig", o));
73  }
74  in.push_back(Pair("sequence", (int64_t)txin.nSequence));
75  vin.push_back(in);
76  }
77  entry.push_back(Pair("vin", vin));
78  Array vout;
79  for (unsigned int i = 0; i < tx.vout.size(); i++)
80  {
81  const CTxOut& txout = tx.vout[i];
82  Object out;
83  out.push_back(Pair("value", ValueFromAmount(txout.nValue)));
84  out.push_back(Pair("n", (int64_t)i));
85  Object o;
86  ScriptPubKeyToJSON(txout.scriptPubKey, o, true);
87  out.push_back(Pair("scriptPubKey", o));
88  vout.push_back(out);
89  }
90  entry.push_back(Pair("vout", vout));
91 
92  if (hashBlock != 0)
93  {
94  entry.push_back(Pair("blockhash", hashBlock.GetHex()));
95  map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
96  if (mi != mapBlockIndex.end() && (*mi).second)
97  {
98  CBlockIndex* pindex = (*mi).second;
99  if (chainActive.Contains(pindex))
100  {
101  entry.push_back(Pair("confirmations", 1 + chainActive.Height() - pindex->nHeight));
102  entry.push_back(Pair("time", (int64_t)pindex->nTime));
103  entry.push_back(Pair("blocktime", (int64_t)pindex->nTime));
104  }
105  else
106  entry.push_back(Pair("confirmations", 0));
107  }
108  }
109 }
110 
111 Value getrawtransaction(const Array& params, bool fHelp)
112 {
113  if (fHelp || params.size() < 1 || params.size() > 2)
114  throw runtime_error(
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"
119 
120  "\nArguments:\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"
123 
124  "\nResult (if verbose is not set or set to 0):\n"
125  "\"data\" (string) The serialized, hex-encoded data for 'txid'\n"
126 
127  "\nResult (if verbose > 0):\n"
128  "{\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"
134  " {\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"
140  " },\n"
141  " \"sequence\": n (numeric) The script sequence number\n"
142  " }\n"
143  " ,...\n"
144  " ],\n"
145  " \"vout\" : [ (array of json objects)\n"
146  " {\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"
156  " ,...\n"
157  " ]\n"
158  " }\n"
159  " }\n"
160  " ,...\n"
161  " ],\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"
166  "}\n"
167 
168  "\nExamples:\n"
169  + HelpExampleCli("getrawtransaction", "\"mytxid\"")
170  + HelpExampleCli("getrawtransaction", "\"mytxid\" 1")
171  + HelpExampleRpc("getrawtransaction", "\"mytxid\", 1")
172  );
173 
174  uint256 hash = ParseHashV(params[0], "parameter 1");
175 
176  bool fVerbose = false;
177  if (params.size() > 1)
178  fVerbose = (params[1].get_int() != 0);
179 
180  CTransaction tx;
181  uint256 hashBlock = 0;
182  if (!GetTransaction(hash, tx, hashBlock, true))
183  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction");
184 
186  ssTx << tx;
187  string strHex = HexStr(ssTx.begin(), ssTx.end());
188 
189  if (!fVerbose)
190  return strHex;
191 
192  Object result;
193  result.push_back(Pair("hex", strHex));
194  TxToJSON(tx, hashBlock, result);
195  return result;
196 }
197 
198 #ifdef ENABLE_WALLET
199 Value listunspent(const Array& params, bool fHelp)
200 {
201  if (fHelp || params.size() > 3)
202  throw runtime_error(
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"
209  "\nArguments:\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"
213  " [\n"
214  " \"address\" (string) bitcoin address\n"
215  " ,...\n"
216  " ]\n"
217  "\nResult\n"
218  "[ (array of json object)\n"
219  " {\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"
227  " }\n"
228  " ,...\n"
229  "]\n"
230 
231  "\nExamples\n"
232  + HelpExampleCli("listunspent", "")
233  + HelpExampleCli("listunspent", "6 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
234  + HelpExampleRpc("listunspent", "6, 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
235  );
236 
237  RPCTypeCheck(params, list_of(int_type)(int_type)(array_type));
238 
239  int nMinDepth = 1;
240  if (params.size() > 0)
241  nMinDepth = params[0].get_int();
242 
243  int nMaxDepth = 9999999;
244  if (params.size() > 1)
245  nMaxDepth = params[1].get_int();
246 
247  set<CBitcoinAddress> setAddress;
248  if (params.size() > 2)
249  {
250  Array inputs = params[2].get_array();
251  BOOST_FOREACH(Value& input, inputs)
252  {
253  CBitcoinAddress address(input.get_str());
254  if (!address.IsValid())
255  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+input.get_str());
256  if (setAddress.count(address))
257  throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+input.get_str());
258  setAddress.insert(address);
259  }
260  }
261 
262  Array results;
263  vector<COutput> vecOutputs;
264  assert(pwalletMain != NULL);
265  pwalletMain->AvailableCoins(vecOutputs, false);
266  BOOST_FOREACH(const COutput& out, vecOutputs)
267  {
268  if (out.nDepth < nMinDepth || out.nDepth > nMaxDepth)
269  continue;
270 
271  if (setAddress.size())
272  {
273  CTxDestination address;
274  if (!ExtractDestination(out.tx->vout[out.i].scriptPubKey, address))
275  continue;
276 
277  if (!setAddress.count(address))
278  continue;
279  }
280 
281  int64_t nValue = out.tx->vout[out.i].nValue;
282  const CScript& pk = out.tx->vout[out.i].scriptPubKey;
283  Object entry;
284  entry.push_back(Pair("txid", out.tx->GetHash().GetHex()));
285  entry.push_back(Pair("vout", out.i));
286  CTxDestination address;
287  if (ExtractDestination(out.tx->vout[out.i].scriptPubKey, address))
288  {
289  entry.push_back(Pair("address", CBitcoinAddress(address).ToString()));
290  if (pwalletMain->mapAddressBook.count(address))
291  entry.push_back(Pair("account", pwalletMain->mapAddressBook[address].name));
292  }
293  entry.push_back(Pair("scriptPubKey", HexStr(pk.begin(), pk.end())));
294  if (pk.IsPayToScriptHash())
295  {
296  CTxDestination address;
297  if (ExtractDestination(pk, address))
298  {
299  const CScriptID& hash = boost::get<const CScriptID&>(address);
300  CScript redeemScript;
301  if (pwalletMain->GetCScript(hash, redeemScript))
302  entry.push_back(Pair("redeemScript", HexStr(redeemScript.begin(), redeemScript.end())));
303  }
304  }
305  entry.push_back(Pair("amount",ValueFromAmount(nValue)));
306  entry.push_back(Pair("confirmations",out.nDepth));
307  results.push_back(entry);
308  }
309 
310  return results;
311 }
312 #endif
313 
314 Value createrawtransaction(const Array& params, bool fHelp)
315 {
316  if (fHelp || params.size() != 2)
317  throw runtime_error(
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"
323 
324  "\nArguments:\n"
325  "1. \"transactions\" (string, required) A json array of json objects\n"
326  " [\n"
327  " {\n"
328  " \"txid\":\"id\", (string, required) The transaction id\n"
329  " \"vout\":n (numeric, required) The output number\n"
330  " }\n"
331  " ,...\n"
332  " ]\n"
333  "2. \"addresses\" (string, required) a json object with addresses as keys and amounts as values\n"
334  " {\n"
335  " \"address\": x.xxx (numeric, required) The key is the bitcoin address, the value is the btc amount\n"
336  " ,...\n"
337  " }\n"
338 
339  "\nResult:\n"
340  "\"transaction\" (string) hex string of the transaction\n"
341 
342  "\nExamples\n"
343  + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"{\\\"address\\\":0.01}\"")
344  + HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"{\\\"address\\\":0.01}\"")
345  );
346 
347  RPCTypeCheck(params, list_of(array_type)(obj_type));
348 
349  Array inputs = params[0].get_array();
350  Object sendTo = params[1].get_obj();
351 
352  CTransaction rawTx;
353 
354  BOOST_FOREACH(const Value& input, inputs)
355  {
356  const Object& o = input.get_obj();
357 
358  uint256 txid = ParseHashO(o, "txid");
359 
360  const Value& vout_v = find_value(o, "vout");
361  if (vout_v.type() != int_type)
362  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
363  int nOutput = vout_v.get_int();
364  if (nOutput < 0)
365  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout must be positive");
366 
367  CTxIn in(COutPoint(txid, nOutput));
368  rawTx.vin.push_back(in);
369  }
370 
371  set<CBitcoinAddress> setAddress;
372  BOOST_FOREACH(const Pair& s, sendTo)
373  {
374  CBitcoinAddress address(s.name_);
375  if (!address.IsValid())
376  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+s.name_);
377 
378  if (setAddress.count(address))
379  throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+s.name_);
380  setAddress.insert(address);
381 
382  CScript scriptPubKey;
383  scriptPubKey.SetDestination(address.Get());
384  int64_t nAmount = AmountFromValue(s.value_);
385 
386  CTxOut out(nAmount, scriptPubKey);
387  rawTx.vout.push_back(out);
388  }
389 
391  ss << rawTx;
392  return HexStr(ss.begin(), ss.end());
393 }
394 
395 Value decoderawtransaction(const Array& params, bool fHelp)
396 {
397  if (fHelp || params.size() != 1)
398  throw runtime_error(
399  "decoderawtransaction \"hexstring\"\n"
400  "\nReturn a JSON object representing the serialized, hex-encoded transaction.\n"
401 
402  "\nArguments:\n"
403  "1. \"hex\" (string, required) The transaction hex string\n"
404 
405  "\nResult:\n"
406  "{\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"
411  " {\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"
417  " },\n"
418  " \"sequence\": n (numeric) The script sequence number\n"
419  " }\n"
420  " ,...\n"
421  " ],\n"
422  " \"vout\" : [ (array of json objects)\n"
423  " {\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"
433  " ,...\n"
434  " ]\n"
435  " }\n"
436  " }\n"
437  " ,...\n"
438  " ],\n"
439  "}\n"
440 
441  "\nExamples:\n"
442  + HelpExampleCli("decoderawtransaction", "\"hexstring\"")
443  + HelpExampleRpc("decoderawtransaction", "\"hexstring\"")
444  );
445 
446  vector<unsigned char> txData(ParseHexV(params[0], "argument"));
447  CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
448  CTransaction tx;
449  try {
450  ssData >> tx;
451  }
452  catch (std::exception &e) {
453  throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
454  }
455 
456  Object result;
457  TxToJSON(tx, 0, result);
458 
459  return result;
460 }
461 
462 Value decodescript(const Array& params, bool fHelp)
463 {
464  if (fHelp || params.size() != 1)
465  throw runtime_error(
466  "decodescript \"hex\"\n"
467  "\nDecode a hex-encoded script.\n"
468  "\nArguments:\n"
469  "1. \"hex\" (string) the hex encoded script\n"
470  "\nResult:\n"
471  "{\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"
478  " ,...\n"
479  " ],\n"
480  " \"p2sh\",\"address\" (string) script address\n"
481  "}\n"
482  "\nExamples:\n"
483  + HelpExampleCli("decodescript", "\"hexstring\"")
484  + HelpExampleRpc("decodescript", "\"hexstring\"")
485  );
486 
487  RPCTypeCheck(params, list_of(str_type));
488 
489  Object r;
490  CScript script;
491  if (params[0].get_str().size() > 0){
492  vector<unsigned char> scriptData(ParseHexV(params[0], "argument"));
493  script = CScript(scriptData.begin(), scriptData.end());
494  } else {
495  // Empty scripts are valid
496  }
497  ScriptPubKeyToJSON(script, r, false);
498 
499  r.push_back(Pair("p2sh", CBitcoinAddress(script.GetID()).ToString()));
500  return r;
501 }
502 
503 Value signrawtransaction(const Array& params, bool fHelp)
504 {
505  if (fHelp || params.size() < 1 || params.size() > 4)
506  throw runtime_error(
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"
513 #ifdef ENABLE_WALLET
514  + HelpRequiringPassphrase() + "\n"
515 #endif
516 
517  "\nArguments:\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"
521  " {\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"
526  " }\n"
527  " ,...\n"
528  " ]\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"
532  " ,...\n"
533  " ]\n"
534  "4. \"sighashtype\" (string, optional, default=ALL) The signature hash type. Must be one of\n"
535  " \"ALL\"\n"
536  " \"NONE\"\n"
537  " \"SINGLE\"\n"
538  " \"ALL|ANYONECANPAY\"\n"
539  " \"NONE|ANYONECANPAY\"\n"
540  " \"SINGLE|ANYONECANPAY\"\n"
541 
542  "\nResult:\n"
543  "{\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"
546  "}\n"
547 
548  "\nExamples:\n"
549  + HelpExampleCli("signrawtransaction", "\"myhex\"")
550  + HelpExampleRpc("signrawtransaction", "\"myhex\"")
551  );
552 
553  RPCTypeCheck(params, list_of(str_type)(array_type)(array_type)(str_type), true);
554 
555  vector<unsigned char> txData(ParseHexV(params[0], "argument 1"));
556  CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
557  vector<CTransaction> txVariants;
558  while (!ssData.empty())
559  {
560  try {
561  CTransaction tx;
562  ssData >> tx;
563  txVariants.push_back(tx);
564  }
565  catch (std::exception &e) {
566  throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
567  }
568  }
569 
570  if (txVariants.empty())
571  throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Missing transaction");
572 
573  // mergedTx will end up with all the signatures; it
574  // starts as a clone of the rawtx:
575  CTransaction mergedTx(txVariants[0]);
576  bool fComplete = true;
577 
578  // Fetch previous transactions (inputs):
579  CCoinsView viewDummy;
580  CCoinsViewCache view(viewDummy);
581  {
582  LOCK(mempool.cs);
583  CCoinsViewCache &viewChain = *pcoinsTip;
584  CCoinsViewMemPool viewMempool(viewChain, mempool);
585  view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
586 
587  BOOST_FOREACH(const CTxIn& txin, mergedTx.vin) {
588  const uint256& prevHash = txin.prevout.hash;
589  CCoins coins;
590  view.GetCoins(prevHash, coins); // this is certainly allowed to fail
591  }
592 
593  view.SetBackend(viewDummy); // switch back to avoid locking mempool for too long
594  }
595 
596  bool fGivenKeys = false;
597  CBasicKeyStore tempKeystore;
598  if (params.size() > 2 && params[2].type() != null_type)
599  {
600  fGivenKeys = true;
601  Array keys = params[2].get_array();
602  BOOST_FOREACH(Value k, keys)
603  {
604  CBitcoinSecret vchSecret;
605  bool fGood = vchSecret.SetString(k.get_str());
606  if (!fGood)
607  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
608  CKey key = vchSecret.GetKey();
609  tempKeystore.AddKey(key);
610  }
611  }
612 #ifdef ENABLE_WALLET
613  else
615 #endif
616 
617  // Add previous txouts given in the RPC call:
618  if (params.size() > 1 && params[1].type() != null_type)
619  {
620  Array prevTxs = params[1].get_array();
621  BOOST_FOREACH(Value& p, prevTxs)
622  {
623  if (p.type() != obj_type)
624  throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}");
625 
626  Object prevOut = p.get_obj();
627 
628  RPCTypeCheck(prevOut, map_list_of("txid", str_type)("vout", int_type)("scriptPubKey", str_type));
629 
630  uint256 txid = ParseHashO(prevOut, "txid");
631 
632  int nOut = find_value(prevOut, "vout").get_int();
633  if (nOut < 0)
634  throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout must be positive");
635 
636  vector<unsigned char> pkData(ParseHexO(prevOut, "scriptPubKey"));
637  CScript scriptPubKey(pkData.begin(), pkData.end());
638 
639  CCoins coins;
640  if (view.GetCoins(txid, coins)) {
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();
646  }
647  // what todo if txid is known, but the actual output isn't?
648  }
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; // we don't know the actual output value
653  view.SetCoins(txid, coins);
654 
655  // if redeemScript given and not using the local wallet (private keys
656  // given), add redeemScript to the tempKeystore so it can be signed:
657  if (fGivenKeys && scriptPubKey.IsPayToScriptHash())
658  {
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))
662  {
663  vector<unsigned char> rsData(ParseHexV(v, "redeemScript"));
664  CScript redeemScript(rsData.begin(), rsData.end());
665  tempKeystore.AddCScript(redeemScript);
666  }
667  }
668  }
669  }
670 
671 #ifdef ENABLE_WALLET
672  const CKeyStore& keystore = ((fGivenKeys || !pwalletMain) ? tempKeystore : *pwalletMain);
673 #else
674  const CKeyStore& keystore = tempKeystore;
675 #endif
676 
677  int nHashType = SIGHASH_ALL;
678  if (params.size() > 3 && params[3].type() != null_type)
679  {
680  static map<string, int> mapSigHashValues =
681  boost::assign::map_list_of
682  (string("ALL"), int(SIGHASH_ALL))
683  (string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY))
684  (string("NONE"), int(SIGHASH_NONE))
685  (string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY))
686  (string("SINGLE"), int(SIGHASH_SINGLE))
687  (string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY))
688  ;
689  string strHashType = params[3].get_str();
690  if (mapSigHashValues.count(strHashType))
691  nHashType = mapSigHashValues[strHashType];
692  else
693  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid sighash param");
694  }
695 
696  bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
697 
698  // Sign what we can:
699  for (unsigned int i = 0; i < mergedTx.vin.size(); i++)
700  {
701  CTxIn& txin = mergedTx.vin[i];
702  CCoins coins;
703  if (!view.GetCoins(txin.prevout.hash, coins) || !coins.IsAvailable(txin.prevout.n))
704  {
705  fComplete = false;
706  continue;
707  }
708  const CScript& prevPubKey = coins.vout[txin.prevout.n].scriptPubKey;
709 
710  txin.scriptSig.clear();
711  // Only sign SIGHASH_SINGLE if there's a corresponding output:
712  if (!fHashSingle || (i < mergedTx.vout.size()))
713  SignSignature(keystore, prevPubKey, mergedTx, i, nHashType);
714 
715  // ... and merge in other signatures:
716  BOOST_FOREACH(const CTransaction& txv, txVariants)
717  {
718  txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig);
719  }
720  if (!VerifyScript(txin.scriptSig, prevPubKey, mergedTx, i, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC, 0))
721  fComplete = false;
722  }
723 
724  Object result;
726  ssTx << mergedTx;
727  result.push_back(Pair("hex", HexStr(ssTx.begin(), ssTx.end())));
728  result.push_back(Pair("complete", fComplete));
729 
730  return result;
731 }
732 
733 Value sendrawtransaction(const Array& params, bool fHelp)
734 {
735  if (fHelp || params.size() < 1 || params.size() > 2)
736  throw runtime_error(
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"
740  "\nArguments:\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"
743  "\nResult:\n"
744  "\"hex\" (string) The transaction hash in hex\n"
745  "\nExamples:\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"
749  + HelpExampleCli("signrawtransaction", "\"myhex\"") +
750  "\nSend the transaction (signed hex)\n"
751  + HelpExampleCli("sendrawtransaction", "\"signedhex\"") +
752  "\nAs a json rpc call\n"
753  + HelpExampleRpc("sendrawtransaction", "\"signedhex\"")
754  );
755 
756 
757  // parse hex string from parameter
758  vector<unsigned char> txData(ParseHexV(params[0], "parameter"));
759  CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
760  CTransaction tx;
761 
762  bool fOverrideFees = false;
763  if (params.size() > 1)
764  fOverrideFees = params[1].get_bool();
765 
766  // deserialize binary data stream
767  try {
768  ssData >> tx;
769  }
770  catch (std::exception &e) {
771  throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
772  }
773  uint256 hashTx = tx.GetHash();
774 
775  CCoinsViewCache &view = *pcoinsTip;
776  CCoins existingCoins;
777  bool fHaveMempool = mempool.exists(hashTx);
778  bool fHaveChain = view.GetCoins(hashTx, existingCoins) && existingCoins.nHeight < 1000000000;
779  if (!fHaveMempool && !fHaveChain) {
780  // push to local node and sync with wallets
781  CValidationState state;
782  if (AcceptToMemoryPool(mempool, state, tx, false, NULL, !fOverrideFees))
783  SyncWithWallets(hashTx, tx, NULL);
784  else {
785  if(state.IsInvalid())
787  else
789  }
790  } else if (fHaveChain) {
791  throw JSONRPCError(RPC_TRANSACTION_ALREADY_IN_CHAIN, "transaction already in block chain");
792  }
793  RelayTransaction(tx, hashTx);
794 
795  return hashTx.GetHex();
796 }
bool IsValid() const
Definition: base58.cpp:215
int nVersion
Definition: core.h:189
int i
Definition: wallet.h:713
Value createrawtransaction(const Array &params, bool fHelp)
std::string HelpRequiringPassphrase()
Definition: rpcwallet.cpp:29
const_iterator begin() const
Definition: serialize.h:924
CScript scriptPubKey
Definition: core.h:123
Definition: init.h:13
void SetBackend(CCoinsView &viewIn)
Definition: coins.cpp:68
static CScript CombineSignatures(CScript scriptPubKey, const CTransaction &txTo, unsigned int nIn, const txnouttype txType, const vector< valtype > &vSolutions, vector< valtype > &sigs1, vector< valtype > &sigs2)
Definition: script.cpp:1738
CKey GetKey()
Definition: base58.cpp:255
vector< unsigned char > ParseHexO(const Object &o, string strKey)
Definition: rpcserver.cpp:133
uint256 ParseHashO(const Object &o, string strKey)
Definition: rpcserver.cpp:120
std::vector< CTxOut > vout
Definition: coins.h:75
std::map< CTxDestination, CAddressBookData > mapAddressBook
Definition: wallet.h:173
bool IsPayToScriptHash() const
Definition: script.cpp:1846
std::string HelpExampleRpc(string methodname, string args)
Definition: rpcserver.cpp:923
Value sendrawtransaction(const Array &params, bool fHelp)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
Definition: script.cpp:1588
bool SignSignature(const CKeyStore &keystore, const CScript &fromPubKey, CTransaction &txTo, unsigned int nIn, int nHashType)
Definition: script.cpp:1630
int nHeight
Definition: coins.h:78
uint256 GetHash() const
Definition: core.cpp:75
#define strprintf
Definition: util.h:116
STL namespace.
const char * GetTxnOutputType(txnouttype t)
Definition: script.cpp:65
Double ended buffer combining vector and stream-like interfaces.
Definition: serialize.h:839
Object JSONRPCError(int code, const string &message)
base58-encoded Bitcoin addresses.
Definition: base58.h:101
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
Definition: coins.h:68
unsigned int n
Definition: core.h:26
void RPCTypeCheck(const Array &params, const list< Value_type > &typesExpected, bool fAllowNull)
Definition: rpcserver.cpp:43
CTxDestination Get() const
Definition: base58.cpp:222
CChain chainActive
The currently-connected chain of blocks.
Definition: main.cpp:48
Value decodescript(const Array &params, bool fHelp)
bool GetCoins(const uint256 &txid, CCoins &coins)
Definition: coins.cpp:74
std::string ToString() const
Definition: script.h:680
bool IsAvailable(unsigned int nPos) const
Definition: coins.h:228
bool SetString(const char *pszSecret)
Definition: base58.cpp:267
void SetDestination(const CTxDestination &address)
Definition: script.cpp:1925
virtual bool AddCScript(const CScript &redeemScript)
Definition: keystore.cpp:34
json_spirit::Value listunspent(const json_spirit::Array &params, bool fHelp)
bool AcceptToMemoryPool(CTxMemPool &pool, CValidationState &state, const CTransaction &tx, bool fLimitFree, bool *pfMissingInputs, bool fRejectInsaneFee)
(try to) add transaction to memory pool
Definition: main.cpp:852
void ScriptPubKeyToJSON(const CScript &scriptPubKey, Object &out, bool fIncludeHex)
Value decoderawtransaction(const Array &params, bool fHelp)
int Height() const
Return the maximal height in the chain.
Definition: main.h:1043
unsigned int nTime
Definition: main.h:725
unsigned int nLockTime
Definition: core.h:192
Value ValueFromAmount(int64_t amount)
Definition: rpcserver.cpp:94
int nDepth
Definition: wallet.h:714
Abstract view on the open txout dataset.
Definition: coins.h:258
An input of a transaction.
Definition: core.h:70
void EnsureWalletIsUnlocked()
Definition: rpcwallet.cpp:36
#define LOCK(cs)
Definition: sync.h:156
A base58-encoded secret key.
Definition: base58.h:121
std::vector< CTxOut > vout
Definition: core.h:191
txnouttype
Definition: script.h:195
void TxToJSON(const CTransaction &tx, const uint256 hashBlock, Object &entry)
std::vector< CTxIn > vin
Definition: core.h:190
void RelayTransaction(const CTransaction &tx, const uint256 &hash)
Definition: net.cpp:1807
An output of a transaction.
Definition: core.h:119
void AvailableCoins(std::vector< COutput > &vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl=NULL) const
Definition: wallet.cpp:1026
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: core.h:22
CCoinsViewCache * pcoinsTip
Global variable that points to the active CCoinsView (protected by cs_main)
Definition: main.cpp:413
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:23
std::string GetHex() const
Definition: uint256.h:297
CCriticalSection cs
Definition: txmempool.h:61
CTxMemPool mempool
Definition: main.cpp:45
bool IsInvalid() const
Definition: main.h:980
std::string GetRejectReason() const
Definition: main.h:997
unsigned char GetRejectCode() const
Definition: main.h:996
CScript scriptSig
Definition: core.h:74
unsigned int nSequence
Definition: core.h:75
Capture information about block/transaction validation.
Definition: main.h:938
256-bit unsigned integer
Definition: uint256.h:531
#define ENABLE_WALLET
void SyncWithWallets(const uint256 &hash, const CTransaction &tx, const CBlock *pblock)
Push an updated transaction to all registered wallets.
Definition: main.cpp:187
bool ExtractDestinations(const CScript &scriptPubKey, txnouttype &typeRet, vector< CTxDestination > &addressRet, int &nRequiredRet)
Definition: script.cpp:1519
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: main.h:688
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:401
static const int PROTOCOL_VERSION
Definition: version.h:29
vector< unsigned char > ParseHexV(const Value &v, string strName)
Definition: rpcserver.cpp:124
A virtual base class for key stores.
Definition: keystore.h:17
Value signrawtransaction(const Array &params, bool fHelp)
bool exists(uint256 hash)
Definition: txmempool.h:91
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Definition: script.cpp:1493
const CWalletTx * tx
Definition: wallet.h:712
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: main.h:1030
CScriptID GetID() const
Definition: script.h:708
bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow)
Retrieve a transaction (from memory pool, or from disk, if possible)
Definition: main.cpp:1049
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const
Definition: keystore.cpp:50
int64_t AmountFromValue(const Value &value)
Definition: rpcserver.cpp:83
Value getrawtransaction(const Array &params, bool fHelp)
bool IsCoinBase() const
Definition: core.h:232
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: key.h:34
bool SetCoins(const uint256 &txid, const CCoins &coins)
Definition: coins.cpp:104
uint256 ParseHashV(const Value &v, string strName)
Definition: rpcserver.cpp:109
std::string HelpExampleCli(string methodname, string args)
Definition: rpcserver.cpp:919
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: script.h:218
An encapsulated private key.
Definition: key.h:179
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: core.h:183
int nHeight
Definition: main.h:698
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:308
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
Definition: util.h:263
COutPoint prevout
Definition: core.h:73
CCoinsView that brings transactions from a memorypool into view.
Definition: txmempool.h:102
map< uint256, CBlockIndex * > mapBlockIndex
Definition: main.cpp:47
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:45
CWallet * pwalletMain
bool empty() const
Definition: serialize.h:929
int64_t nValue
Definition: core.h:122
const_iterator end() const
Definition: serialize.h:926
uint256 hash
Definition: core.h:25