Dogecoin Core  1.14.2
P2P Digital Currency
bitcoin-tx.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #if defined(HAVE_CONFIG_H)
6 #include "config/bitcoin-config.h"
7 #endif
8 
9 #include "base58.h"
10 #include "clientversion.h"
11 #include "coins.h"
12 #include "consensus/consensus.h"
13 #include "core_io.h"
14 #include "keystore.h"
15 #include "policy/policy.h"
16 #include "primitives/transaction.h"
17 #include "script/script.h"
18 #include "script/sign.h"
19 #include <univalue.h>
20 #include "util.h"
21 #include "utilmoneystr.h"
22 #include "utilstrencodings.h"
23 
24 #include <stdio.h>
25 
26 #include <boost/algorithm/string.hpp>
27 #include <boost/assign/list_of.hpp>
28 
29 static bool fCreateBlank;
30 static std::map<std::string,UniValue> registers;
31 static const int CONTINUE_EXECUTION=-1;
32 
33 //
34 // This function returns either one of EXIT_ codes when it's expected to stop the process or
35 // CONTINUE_EXECUTION when it's expected to continue further.
36 //
37 static int AppInitRawTx(int argc, char* argv[])
38 {
39  //
40  // Parameters
41  //
42  ParseParameters(argc, argv);
43 
44  // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
45  try {
47  } catch (const std::exception& e) {
48  fprintf(stderr, "Error: %s\n", e.what());
49  return EXIT_FAILURE;
50  }
51 
52  fCreateBlank = GetBoolArg("-create", false);
53 
54  if (argc<2 || IsArgSet("-?") || IsArgSet("-h") || IsArgSet("-help"))
55  {
56  // First part of help message is specific to this utility
57  std::string strUsage = strprintf(_("%s dogecoin-tx utility version"), _(PACKAGE_NAME)) + " " + FormatFullVersion() + "\n\n" +
58  _("Usage:") + "\n" +
59  " dogecoin-tx [options] <hex-tx> [commands] " + _("Update hex-encoded dogecoin transaction") + "\n" +
60  " dogecoin-tx [options] -create [commands] " + _("Create hex-encoded dogecoin transaction") + "\n" +
61  "\n";
62 
63  fprintf(stdout, "%s", strUsage.c_str());
64 
65  strUsage = HelpMessageGroup(_("Options:"));
66  strUsage += HelpMessageOpt("-?", _("This help message"));
67  strUsage += HelpMessageOpt("-create", _("Create new, empty TX."));
68  strUsage += HelpMessageOpt("-json", _("Select JSON output"));
69  strUsage += HelpMessageOpt("-txid", _("Output only the hex-encoded transaction id of the resultant transaction."));
70  AppendParamsHelpMessages(strUsage);
71 
72  fprintf(stdout, "%s", strUsage.c_str());
73 
74  strUsage = HelpMessageGroup(_("Commands:"));
75  strUsage += HelpMessageOpt("delin=N", _("Delete input N from TX"));
76  strUsage += HelpMessageOpt("delout=N", _("Delete output N from TX"));
77  strUsage += HelpMessageOpt("in=TXID:VOUT(:SEQUENCE_NUMBER)", _("Add input to TX"));
78  strUsage += HelpMessageOpt("locktime=N", _("Set TX lock time to N"));
79  strUsage += HelpMessageOpt("nversion=N", _("Set TX version to N"));
80  strUsage += HelpMessageOpt("outaddr=VALUE:ADDRESS", _("Add address-based output to TX"));
81  strUsage += HelpMessageOpt("outpubkey=VALUE:PUBKEY[:FLAGS]", _("Add pay-to-pubkey output to TX") + ". " +
82  _("Optionally add the \"W\" flag to produce a pay-to-witness-pubkey-hash output") + ". " +
83  _("Optionally add the \"S\" flag to wrap the output in a pay-to-script-hash."));
84  strUsage += HelpMessageOpt("outdata=[VALUE:]DATA", _("Add data-based output to TX"));
85  strUsage += HelpMessageOpt("outscript=VALUE:SCRIPT[:FLAGS]", _("Add raw script output to TX") + ". " +
86  _("Optionally add the \"W\" flag to produce a pay-to-witness-script-hash output") + ". " +
87  _("Optionally add the \"S\" flag to wrap the output in a pay-to-script-hash."));
88  strUsage += HelpMessageOpt("outmultisig=VALUE:REQUIRED:PUBKEYS:PUBKEY1:PUBKEY2:....[:FLAGS]", _("Add Pay To n-of-m Multi-sig output to TX. n = REQUIRED, m = PUBKEYS") + ". " +
89  _("Optionally add the \"W\" flag to produce a pay-to-witness-script-hash output") + ". " +
90  _("Optionally add the \"S\" flag to wrap the output in a pay-to-script-hash."));
91  strUsage += HelpMessageOpt("sign=SIGHASH-FLAGS", _("Add zero or more signatures to transaction") + ". " +
92  _("This command requires JSON registers:") +
93  _("prevtxs=JSON object") + ", " +
94  _("privatekeys=JSON object") + ". " +
95  _("See signrawtransaction docs for format of sighash flags, JSON objects."));
96  fprintf(stdout, "%s", strUsage.c_str());
97 
98  strUsage = HelpMessageGroup(_("Register Commands:"));
99  strUsage += HelpMessageOpt("load=NAME:FILENAME", _("Load JSON file FILENAME into register NAME"));
100  strUsage += HelpMessageOpt("set=NAME:JSON-STRING", _("Set register NAME to given JSON-STRING"));
101  fprintf(stdout, "%s", strUsage.c_str());
102 
103  if (argc < 2) {
104  fprintf(stderr, "Error: too few parameters\n");
105  return EXIT_FAILURE;
106  }
107  return EXIT_SUCCESS;
108  }
109  return CONTINUE_EXECUTION;
110 }
111 
112 static void RegisterSetJson(const std::string& key, const std::string& rawJson)
113 {
114  UniValue val;
115  if (!val.read(rawJson)) {
116  std::string strErr = "Cannot parse JSON for key " + key;
117  throw std::runtime_error(strErr);
118  }
119 
120  registers[key] = val;
121 }
122 
123 static void RegisterSet(const std::string& strInput)
124 {
125  // separate NAME:VALUE in string
126  size_t pos = strInput.find(':');
127  if ((pos == std::string::npos) ||
128  (pos == 0) ||
129  (pos == (strInput.size() - 1)))
130  throw std::runtime_error("Register input requires NAME:VALUE");
131 
132  std::string key = strInput.substr(0, pos);
133  std::string valStr = strInput.substr(pos + 1, std::string::npos);
134 
135  RegisterSetJson(key, valStr);
136 }
137 
138 static void RegisterLoad(const std::string& strInput)
139 {
140  // separate NAME:FILENAME in string
141  size_t pos = strInput.find(':');
142  if ((pos == std::string::npos) ||
143  (pos == 0) ||
144  (pos == (strInput.size() - 1)))
145  throw std::runtime_error("Register load requires NAME:FILENAME");
146 
147  std::string key = strInput.substr(0, pos);
148  std::string filename = strInput.substr(pos + 1, std::string::npos);
149 
150  FILE *f = fopen(filename.c_str(), "r");
151  if (!f) {
152  std::string strErr = "Cannot open file " + filename;
153  throw std::runtime_error(strErr);
154  }
155 
156  // load file chunks into one big buffer
157  std::string valStr;
158  while ((!feof(f)) && (!ferror(f))) {
159  char buf[4096];
160  int bread = fread(buf, 1, sizeof(buf), f);
161  if (bread <= 0)
162  break;
163 
164  valStr.insert(valStr.size(), buf, bread);
165  }
166 
167  int error = ferror(f);
168  fclose(f);
169 
170  if (error) {
171  std::string strErr = "Error reading file " + filename;
172  throw std::runtime_error(strErr);
173  }
174 
175  // evaluate as JSON buffer register
176  RegisterSetJson(key, valStr);
177 }
178 
179 static CAmount ExtractAndValidateValue(const std::string& strValue)
180 {
181  CAmount value;
182  if (!ParseMoney(strValue, value))
183  throw std::runtime_error("invalid TX output value");
184  return value;
185 }
186 
187 static void MutateTxVersion(CMutableTransaction& tx, const std::string& cmdVal)
188 {
189  int64_t newVersion = atoi64(cmdVal);
190  if (newVersion < 1 || newVersion > CTransaction::MAX_STANDARD_VERSION)
191  throw std::runtime_error("Invalid TX version requested");
192 
193  tx.nVersion = (int) newVersion;
194 }
195 
196 static void MutateTxLocktime(CMutableTransaction& tx, const std::string& cmdVal)
197 {
198  int64_t newLocktime = atoi64(cmdVal);
199  if (newLocktime < 0LL || newLocktime > 0xffffffffLL)
200  throw std::runtime_error("Invalid TX locktime requested");
201 
202  tx.nLockTime = (unsigned int) newLocktime;
203 }
204 
205 static void MutateTxAddInput(CMutableTransaction& tx, const std::string& strInput)
206 {
207  std::vector<std::string> vStrInputParts;
208  boost::split(vStrInputParts, strInput, boost::is_any_of(":"));
209 
210  // separate TXID:VOUT in string
211  if (vStrInputParts.size()<2)
212  throw std::runtime_error("TX input missing separator");
213 
214  // extract and validate TXID
215  std::string strTxid = vStrInputParts[0];
216  if ((strTxid.size() != 64) || !IsHex(strTxid))
217  throw std::runtime_error("invalid TX input txid");
218  uint256 txid(uint256S(strTxid));
219 
220  static const unsigned int minTxOutSz = 9;
221  static const unsigned int maxVout = MAX_BLOCK_BASE_SIZE / minTxOutSz;
222 
223  // extract and validate vout
224  std::string strVout = vStrInputParts[1];
225  int vout = atoi(strVout);
226  if ((vout < 0) || (vout > (int)maxVout))
227  throw std::runtime_error("invalid TX input vout");
228 
229  // extract the optional sequence number
230  uint32_t nSequenceIn=std::numeric_limits<unsigned int>::max();
231  if (vStrInputParts.size() > 2)
232  nSequenceIn = std::stoul(vStrInputParts[2]);
233 
234  // append to transaction input list
235  CTxIn txin(txid, vout, CScript(), nSequenceIn);
236  tx.vin.push_back(txin);
237 }
238 
239 static void MutateTxAddOutAddr(CMutableTransaction& tx, const std::string& strInput)
240 {
241  // Separate into VALUE:ADDRESS
242  std::vector<std::string> vStrInputParts;
243  boost::split(vStrInputParts, strInput, boost::is_any_of(":"));
244 
245  if (vStrInputParts.size() != 2)
246  throw std::runtime_error("TX output missing or too many separators");
247 
248  // Extract and validate VALUE
249  CAmount value = ExtractAndValidateValue(vStrInputParts[0]);
250 
251  // extract and validate ADDRESS
252  std::string strAddr = vStrInputParts[1];
253  CBitcoinAddress addr(strAddr);
254  if (!addr.IsValid())
255  throw std::runtime_error("invalid TX output address");
256  // build standard output script via GetScriptForDestination()
257  CScript scriptPubKey = GetScriptForDestination(addr.Get());
258 
259  // construct TxOut, append to transaction output list
260  CTxOut txout(value, scriptPubKey);
261  tx.vout.push_back(txout);
262 }
263 
264 static void MutateTxAddOutPubKey(CMutableTransaction& tx, const std::string& strInput)
265 {
266  // Separate into VALUE:PUBKEY[:FLAGS]
267  std::vector<std::string> vStrInputParts;
268  boost::split(vStrInputParts, strInput, boost::is_any_of(":"));
269 
270  if (vStrInputParts.size() < 2 || vStrInputParts.size() > 3)
271  throw std::runtime_error("TX output missing or too many separators");
272 
273  // Extract and validate VALUE
274  CAmount value = ExtractAndValidateValue(vStrInputParts[0]);
275 
276  // Extract and validate PUBKEY
277  CPubKey pubkey(ParseHex(vStrInputParts[1]));
278  if (!pubkey.IsFullyValid())
279  throw std::runtime_error("invalid TX output pubkey");
280  CScript scriptPubKey = GetScriptForRawPubKey(pubkey);
281  CBitcoinAddress addr(scriptPubKey);
282 
283  // Extract and validate FLAGS
284  bool bSegWit = false;
285  bool bScriptHash = false;
286  if (vStrInputParts.size() == 3) {
287  std::string flags = vStrInputParts[2];
288  bSegWit = (flags.find("W") != std::string::npos);
289  bScriptHash = (flags.find("S") != std::string::npos);
290  }
291 
292  if (bSegWit) {
293  // Call GetScriptForWitness() to build a P2WSH scriptPubKey
294  scriptPubKey = GetScriptForWitness(scriptPubKey);
295  }
296  if (bScriptHash) {
297  // Get the address for the redeem script, then call
298  // GetScriptForDestination() to construct a P2SH scriptPubKey.
299  CBitcoinAddress redeemScriptAddr(scriptPubKey);
300  scriptPubKey = GetScriptForDestination(redeemScriptAddr.Get());
301  }
302 
303  // construct TxOut, append to transaction output list
304  CTxOut txout(value, scriptPubKey);
305  tx.vout.push_back(txout);
306 }
307 
308 static void MutateTxAddOutMultiSig(CMutableTransaction& tx, const std::string& strInput)
309 {
310  // Separate into VALUE:REQUIRED:NUMKEYS:PUBKEY1:PUBKEY2:....[:FLAGS]
311  std::vector<std::string> vStrInputParts;
312  boost::split(vStrInputParts, strInput, boost::is_any_of(":"));
313 
314  // Check that there are enough parameters
315  if (vStrInputParts.size()<3)
316  throw std::runtime_error("Not enough multisig parameters");
317 
318  // Extract and validate VALUE
319  CAmount value = ExtractAndValidateValue(vStrInputParts[0]);
320 
321  // Extract REQUIRED
322  uint32_t required = stoul(vStrInputParts[1]);
323 
324  // Extract NUMKEYS
325  uint32_t numkeys = stoul(vStrInputParts[2]);
326 
327  // Validate there are the correct number of pubkeys
328  if (vStrInputParts.size() < numkeys + 3)
329  throw std::runtime_error("incorrect number of multisig pubkeys");
330 
331  if (required < 1 || required > 20 || numkeys < 1 || numkeys > 20 || numkeys < required)
332  throw std::runtime_error("multisig parameter mismatch. Required " \
333  + std::to_string(required) + " of " + std::to_string(numkeys) + "signatures.");
334 
335  // extract and validate PUBKEYs
336  std::vector<CPubKey> pubkeys;
337  for(int pos = 1; pos <= int(numkeys); pos++) {
338  CPubKey pubkey(ParseHex(vStrInputParts[pos + 2]));
339  if (!pubkey.IsFullyValid())
340  throw std::runtime_error("invalid TX output pubkey");
341  pubkeys.push_back(pubkey);
342  }
343 
344  // Extract FLAGS
345  bool bSegWit = false;
346  bool bScriptHash = false;
347  if (vStrInputParts.size() == numkeys + 4) {
348  std::string flags = vStrInputParts.back();
349  bSegWit = (flags.find("W") != std::string::npos);
350  bScriptHash = (flags.find("S") != std::string::npos);
351  }
352  else if (vStrInputParts.size() > numkeys + 4) {
353  // Validate that there were no more parameters passed
354  throw std::runtime_error("Too many parameters");
355  }
356 
357  CScript scriptPubKey = GetScriptForMultisig(required, pubkeys);
358 
359  if (bSegWit) {
360  // Call GetScriptForWitness() to build a P2WSH scriptPubKey
361  scriptPubKey = GetScriptForWitness(scriptPubKey);
362  }
363  if (bScriptHash) {
364  // Get the address for the redeem script, then call
365  // GetScriptForDestination() to construct a P2SH scriptPubKey.
366  CBitcoinAddress addr(scriptPubKey);
367  scriptPubKey = GetScriptForDestination(addr.Get());
368  }
369 
370  // construct TxOut, append to transaction output list
371  CTxOut txout(value, scriptPubKey);
372  tx.vout.push_back(txout);
373 }
374 
375 static void MutateTxAddOutData(CMutableTransaction& tx, const std::string& strInput)
376 {
377  CAmount value = 0;
378 
379  // separate [VALUE:]DATA in string
380  size_t pos = strInput.find(':');
381 
382  if (pos==0)
383  throw std::runtime_error("TX output value not specified");
384 
385  if (pos != std::string::npos) {
386  // Extract and validate VALUE
387  value = ExtractAndValidateValue(strInput.substr(0, pos));
388  }
389 
390  // extract and validate DATA
391  std::string strData = strInput.substr(pos + 1, std::string::npos);
392 
393  if (!IsHex(strData))
394  throw std::runtime_error("invalid TX output data");
395 
396  std::vector<unsigned char> data = ParseHex(strData);
397 
398  CTxOut txout(value, CScript() << OP_RETURN << data);
399  tx.vout.push_back(txout);
400 }
401 
402 static void MutateTxAddOutScript(CMutableTransaction& tx, const std::string& strInput)
403 {
404  // separate VALUE:SCRIPT[:FLAGS]
405  std::vector<std::string> vStrInputParts;
406  boost::split(vStrInputParts, strInput, boost::is_any_of(":"));
407  if (vStrInputParts.size() < 2)
408  throw std::runtime_error("TX output missing separator");
409 
410  // Extract and validate VALUE
411  CAmount value = ExtractAndValidateValue(vStrInputParts[0]);
412 
413  // extract and validate script
414  std::string strScript = vStrInputParts[1];
415  CScript scriptPubKey = ParseScript(strScript);
416 
417  // Extract FLAGS
418  bool bSegWit = false;
419  bool bScriptHash = false;
420  if (vStrInputParts.size() == 3) {
421  std::string flags = vStrInputParts.back();
422  bSegWit = (flags.find("W") != std::string::npos);
423  bScriptHash = (flags.find("S") != std::string::npos);
424  }
425 
426  if (bSegWit) {
427  scriptPubKey = GetScriptForWitness(scriptPubKey);
428  }
429  if (bScriptHash) {
430  CBitcoinAddress addr(scriptPubKey);
431  scriptPubKey = GetScriptForDestination(addr.Get());
432  }
433 
434  // construct TxOut, append to transaction output list
435  CTxOut txout(value, scriptPubKey);
436  tx.vout.push_back(txout);
437 }
438 
439 static void MutateTxDelInput(CMutableTransaction& tx, const std::string& strInIdx)
440 {
441  // parse requested deletion index
442  int inIdx = atoi(strInIdx);
443  if (inIdx < 0 || inIdx >= (int)tx.vin.size()) {
444  std::string strErr = "Invalid TX input index '" + strInIdx + "'";
445  throw std::runtime_error(strErr.c_str());
446  }
447 
448  // delete input from transaction
449  tx.vin.erase(tx.vin.begin() + inIdx);
450 }
451 
452 static void MutateTxDelOutput(CMutableTransaction& tx, const std::string& strOutIdx)
453 {
454  // parse requested deletion index
455  int outIdx = atoi(strOutIdx);
456  if (outIdx < 0 || outIdx >= (int)tx.vout.size()) {
457  std::string strErr = "Invalid TX output index '" + strOutIdx + "'";
458  throw std::runtime_error(strErr.c_str());
459  }
460 
461  // delete output from transaction
462  tx.vout.erase(tx.vout.begin() + outIdx);
463 }
464 
465 static const unsigned int N_SIGHASH_OPTS = 6;
466 static const struct {
467  const char *flagStr;
468  int flags;
469 } sighashOptions[N_SIGHASH_OPTS] = {
470  {"ALL", SIGHASH_ALL},
471  {"NONE", SIGHASH_NONE},
472  {"SINGLE", SIGHASH_SINGLE},
473  {"ALL|ANYONECANPAY", SIGHASH_ALL|SIGHASH_ANYONECANPAY},
474  {"NONE|ANYONECANPAY", SIGHASH_NONE|SIGHASH_ANYONECANPAY},
475  {"SINGLE|ANYONECANPAY", SIGHASH_SINGLE|SIGHASH_ANYONECANPAY},
476 };
477 
478 static bool findSighashFlags(int& flags, const std::string& flagStr)
479 {
480  flags = 0;
481 
482  for (unsigned int i = 0; i < N_SIGHASH_OPTS; i++) {
483  if (flagStr == sighashOptions[i].flagStr) {
484  flags = sighashOptions[i].flags;
485  return true;
486  }
487  }
488 
489  return false;
490 }
491 
492 uint256 ParseHashUO(std::map<std::string,UniValue>& o, std::string strKey)
493 {
494  if (!o.count(strKey))
495  return uint256();
496  return ParseHashUV(o[strKey], strKey);
497 }
498 
499 std::vector<unsigned char> ParseHexUO(std::map<std::string,UniValue>& o, std::string strKey)
500 {
501  if (!o.count(strKey)) {
502  std::vector<unsigned char> emptyVec;
503  return emptyVec;
504  }
505  return ParseHexUV(o[strKey], strKey);
506 }
507 
508 static CAmount AmountFromValue(const UniValue& value)
509 {
510  if (!value.isNum() && !value.isStr())
511  throw std::runtime_error("Amount is not a number or string");
512  CAmount amount;
513  if (!ParseFixedPoint(value.getValStr(), 8, &amount))
514  throw std::runtime_error("Invalid amount");
515  if (!MoneyRange(amount))
516  throw std::runtime_error("Amount out of range");
517  return amount;
518 }
519 
520 static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr)
521 {
522  int nHashType = SIGHASH_ALL;
523 
524  if (flagStr.size() > 0)
525  if (!findSighashFlags(nHashType, flagStr))
526  throw std::runtime_error("unknown sighash flag/sign option");
527 
528  std::vector<CTransaction> txVariants;
529  txVariants.push_back(tx);
530 
531  // mergedTx will end up with all the signatures; it
532  // starts as a clone of the raw tx:
533  CMutableTransaction mergedTx(txVariants[0]);
534  bool fComplete = true;
535  CCoinsView viewDummy;
536  CCoinsViewCache view(&viewDummy);
537 
538  if (!registers.count("privatekeys"))
539  throw std::runtime_error("privatekeys register variable must be set.");
540  CBasicKeyStore tempKeystore;
541  UniValue keysObj = registers["privatekeys"];
542 
543  for (unsigned int kidx = 0; kidx < keysObj.size(); kidx++) {
544  if (!keysObj[kidx].isStr())
545  throw std::runtime_error("privatekey not a std::string");
546  CBitcoinSecret vchSecret;
547  bool fGood = vchSecret.SetString(keysObj[kidx].getValStr());
548  if (!fGood)
549  throw std::runtime_error("privatekey not valid");
550 
551  CKey key = vchSecret.GetKey();
552  tempKeystore.AddKey(key);
553  }
554 
555  // Add previous txouts given in the RPC call:
556  if (!registers.count("prevtxs"))
557  throw std::runtime_error("prevtxs register variable must be set.");
558  UniValue prevtxsObj = registers["prevtxs"];
559  {
560  for (unsigned int previdx = 0; previdx < prevtxsObj.size(); previdx++) {
561  UniValue prevOut = prevtxsObj[previdx];
562  if (!prevOut.isObject())
563  throw std::runtime_error("expected prevtxs internal object");
564 
565  std::map<std::string,UniValue::VType> types = boost::assign::map_list_of("txid", UniValue::VSTR)("vout",UniValue::VNUM)("scriptPubKey",UniValue::VSTR);
566  if (!prevOut.checkObject(types))
567  throw std::runtime_error("prevtxs internal object typecheck fail");
568 
569  uint256 txid = ParseHashUV(prevOut["txid"], "txid");
570 
571  int nOut = atoi(prevOut["vout"].getValStr());
572  if (nOut < 0)
573  throw std::runtime_error("vout must be positive");
574 
575  std::vector<unsigned char> pkData(ParseHexUV(prevOut["scriptPubKey"], "scriptPubKey"));
576  CScript scriptPubKey(pkData.begin(), pkData.end());
577 
578  {
579  CCoinsModifier coins = view.ModifyCoins(txid);
580  if (coins->IsAvailable(nOut) && coins->vout[nOut].scriptPubKey != scriptPubKey) {
581  std::string err("Previous output scriptPubKey mismatch:\n");
582  err = err + ScriptToAsmStr(coins->vout[nOut].scriptPubKey) + "\nvs:\n"+
583  ScriptToAsmStr(scriptPubKey);
584  throw std::runtime_error(err);
585  }
586  if ((unsigned int)nOut >= coins->vout.size())
587  coins->vout.resize(nOut+1);
588  coins->vout[nOut].scriptPubKey = scriptPubKey;
589  coins->vout[nOut].nValue = 0;
590  if (prevOut.exists("amount")) {
591  coins->vout[nOut].nValue = AmountFromValue(prevOut["amount"]);
592  }
593  }
594 
595  // if redeemScript given and private keys given,
596  // add redeemScript to the tempKeystore so it can be signed:
597  if ((scriptPubKey.IsPayToScriptHash() || scriptPubKey.IsPayToWitnessScriptHash()) &&
598  prevOut.exists("redeemScript")) {
599  UniValue v = prevOut["redeemScript"];
600  std::vector<unsigned char> rsData(ParseHexUV(v, "redeemScript"));
601  CScript redeemScript(rsData.begin(), rsData.end());
602  tempKeystore.AddCScript(redeemScript);
603  }
604  }
605  }
606 
607  const CKeyStore& keystore = tempKeystore;
608 
609  bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
610 
611  // Sign what we can:
612  for (unsigned int i = 0; i < mergedTx.vin.size(); i++) {
613  CTxIn& txin = mergedTx.vin[i];
614  const CCoins* coins = view.AccessCoins(txin.prevout.hash);
615  if (!coins || !coins->IsAvailable(txin.prevout.n)) {
616  fComplete = false;
617  continue;
618  }
619  const CScript& prevPubKey = coins->vout[txin.prevout.n].scriptPubKey;
620  const CAmount& amount = coins->vout[txin.prevout.n].nValue;
621 
622  SignatureData sigdata;
623  // Only sign SIGHASH_SINGLE if there's a corresponding output:
624  if (!fHashSingle || (i < mergedTx.vout.size()))
625  ProduceSignature(MutableTransactionSignatureCreator(&keystore, &mergedTx, i, amount, nHashType), prevPubKey, sigdata);
626 
627  // ... and merge in other signatures:
628  BOOST_FOREACH(const CTransaction& txv, txVariants)
629  sigdata = CombineSignatures(prevPubKey, MutableTransactionSignatureChecker(&mergedTx, i, amount), sigdata, DataFromTransaction(txv, i));
630  UpdateTransaction(mergedTx, i, sigdata);
631 
632  if (!VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker(&mergedTx, i, amount)))
633  fComplete = false;
634  }
635 
636  if (fComplete) {
637  // do nothing... for now
638  // perhaps store this for later optional JSON output
639  }
640 
641  tx = mergedTx;
642 }
643 
645 {
647 
648 public:
650  ECC_Start();
651  }
653  ECC_Stop();
654  }
655 };
656 
657 static void MutateTx(CMutableTransaction& tx, const std::string& command,
658  const std::string& commandVal)
659 {
660  std::unique_ptr<Secp256k1Init> ecc;
661 
662  if (command == "nversion")
663  MutateTxVersion(tx, commandVal);
664  else if (command == "locktime")
665  MutateTxLocktime(tx, commandVal);
666 
667  else if (command == "delin")
668  MutateTxDelInput(tx, commandVal);
669  else if (command == "in")
670  MutateTxAddInput(tx, commandVal);
671 
672  else if (command == "delout")
673  MutateTxDelOutput(tx, commandVal);
674  else if (command == "outaddr")
675  MutateTxAddOutAddr(tx, commandVal);
676  else if (command == "outpubkey")
677  MutateTxAddOutPubKey(tx, commandVal);
678  else if (command == "outmultisig")
679  MutateTxAddOutMultiSig(tx, commandVal);
680  else if (command == "outscript")
681  MutateTxAddOutScript(tx, commandVal);
682  else if (command == "outdata")
683  MutateTxAddOutData(tx, commandVal);
684 
685  else if (command == "sign") {
686  if (!ecc) { ecc.reset(new Secp256k1Init()); }
687  MutateTxSign(tx, commandVal);
688  }
689 
690  else if (command == "load")
691  RegisterLoad(commandVal);
692 
693  else if (command == "set")
694  RegisterSet(commandVal);
695 
696  else
697  throw std::runtime_error("unknown command");
698 }
699 
700 static void OutputTxJSON(const CTransaction& tx)
701 {
702  UniValue entry(UniValue::VOBJ);
703  TxToUniv(tx, uint256(), entry);
704 
705  std::string jsonOutput = entry.write(4);
706  fprintf(stdout, "%s\n", jsonOutput.c_str());
707 }
708 
709 static void OutputTxHash(const CTransaction& tx)
710 {
711  std::string strHexHash = tx.GetHash().GetHex(); // the hex-encoded transaction hash (aka the transaction id)
712 
713  fprintf(stdout, "%s\n", strHexHash.c_str());
714 }
715 
716 static void OutputTxHex(const CTransaction& tx)
717 {
718  std::string strHex = EncodeHexTx(tx);
719 
720  fprintf(stdout, "%s\n", strHex.c_str());
721 }
722 
723 static void OutputTx(const CTransaction& tx)
724 {
725  if (GetBoolArg("-json", false))
726  OutputTxJSON(tx);
727  else if (GetBoolArg("-txid", false))
728  OutputTxHash(tx);
729  else
730  OutputTxHex(tx);
731 }
732 
733 static std::string readStdin()
734 {
735  char buf[4096];
736  std::string ret;
737 
738  while (!feof(stdin)) {
739  size_t bread = fread(buf, 1, sizeof(buf), stdin);
740  ret.append(buf, bread);
741  if (bread < sizeof(buf))
742  break;
743  }
744 
745  if (ferror(stdin))
746  throw std::runtime_error("error reading stdin");
747 
748  boost::algorithm::trim_right(ret);
749 
750  return ret;
751 }
752 
753 static int CommandLineRawTx(int argc, char* argv[])
754 {
755  std::string strPrint;
756  int nRet = 0;
757  try {
758  // Skip switches; Permit common stdin convention "-"
759  while (argc > 1 && IsSwitchChar(argv[1][0]) &&
760  (argv[1][1] != 0)) {
761  argc--;
762  argv++;
763  }
764 
766  int startArg;
767 
768  if (!fCreateBlank) {
769  // require at least one param
770  if (argc < 2)
771  throw std::runtime_error("too few parameters");
772 
773  // param: hex-encoded bitcoin transaction
774  std::string strHexTx(argv[1]);
775  if (strHexTx == "-") // "-" implies standard input
776  strHexTx = readStdin();
777 
778  if (!DecodeHexTx(tx, strHexTx, true))
779  throw std::runtime_error("invalid transaction encoding");
780 
781  startArg = 2;
782  } else
783  startArg = 1;
784 
785  for (int i = startArg; i < argc; i++) {
786  std::string arg = argv[i];
787  std::string key, value;
788  size_t eqpos = arg.find('=');
789  if (eqpos == std::string::npos)
790  key = arg;
791  else {
792  key = arg.substr(0, eqpos);
793  value = arg.substr(eqpos + 1);
794  }
795 
796  MutateTx(tx, key, value);
797  }
798 
799  OutputTx(tx);
800  }
801 
802  catch (const boost::thread_interrupted&) {
803  throw;
804  }
805  catch (const std::exception& e) {
806  strPrint = std::string("error: ") + e.what();
807  nRet = EXIT_FAILURE;
808  }
809  catch (...) {
810  PrintExceptionContinue(NULL, "CommandLineRawTx()");
811  throw;
812  }
813 
814  if (strPrint != "") {
815  fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str());
816  }
817  return nRet;
818 }
819 
820 int main(int argc, char* argv[])
821 {
823 
824  try {
825  int ret = AppInitRawTx(argc, argv);
826  if (ret != CONTINUE_EXECUTION)
827  return ret;
828  }
829  catch (const std::exception& e) {
830  PrintExceptionContinue(&e, "AppInitRawTx()");
831  return EXIT_FAILURE;
832  } catch (...) {
833  PrintExceptionContinue(NULL, "AppInitRawTx()");
834  return EXIT_FAILURE;
835  }
836 
837  int ret = EXIT_FAILURE;
838  try {
839  ret = CommandLineRawTx(argc, argv);
840  }
841  catch (const std::exception& e) {
842  PrintExceptionContinue(&e, "CommandLineRawTx()");
843  } catch (...) {
844  PrintExceptionContinue(NULL, "CommandLineRawTx()");
845  }
846  return ret;
847 }
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:32
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:15
int main(int argc, char *argv[])
Definition: bitcoin-tx.cpp:820
const char * flagStr
Definition: bitcoin-tx.cpp:467
uint256 ParseHashUO(std::map< std::string, UniValue > &o, std::string strKey)
Definition: bitcoin-tx.cpp:492
int flags
Definition: bitcoin-tx.cpp:468
std::vector< unsigned char > ParseHexUO(std::map< std::string, UniValue > &o, std::string strKey)
Definition: bitcoin-tx.cpp:499
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given BIP70 chain name.
std::string ChainNameFromCommandLine()
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
void AppendParamsHelpMessages(std::string &strUsage, bool debugHelp)
Append the help messages for the chainparams options to the parameter string.
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:56
virtual bool AddCScript(const CScript &redeemScript)
Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki.
Definition: keystore.cpp:41
base58-encoded Bitcoin addresses.
Definition: base58.h:104
A base58-encoded secret key.
Definition: base58.h:126
CKey GetKey()
Definition: base58.cpp:297
bool SetString(const char *pszSecret)
Definition: base58.cpp:312
Pruned version of CTransaction: only retains metadata and unspent transaction outputs.
Definition: coins.h:75
std::vector< CTxOut > vout
unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are d...
Definition: coins.h:81
bool IsAvailable(unsigned int nPos) const
check whether a particular output is still available
Definition: coins.h:223
A reference to a mutable cache entry.
Definition: coins.h:356
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:372
Abstract view on the open txout dataset.
Definition: coins.h:307
An encapsulated private key.
Definition: key.h:36
A virtual base class for key stores.
Definition: keystore.h:20
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:14
uint32_t n
Definition: transaction.h:23
uint256 hash
Definition: transaction.h:22
An encapsulated public key.
Definition: pubkey.h:40
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:377
bool IsPayToScriptHash() const
Definition: script.cpp:204
bool IsPayToWitnessScriptHash() const
Definition: script.cpp:213
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:308
static const int32_t MAX_STANDARD_VERSION
Definition: transaction.h:318
const uint256 & GetHash() const
Definition: transaction.h:358
An input of a transaction.
Definition: transaction.h:63
CScript scriptSig
Definition: transaction.h:66
CScriptWitness scriptWitness
Definition: transaction.h:68
COutPoint prevout
Definition: transaction.h:65
An output of a transaction.
Definition: transaction.h:133
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:245
ECCVerifyHandle globalVerifyHandle
Definition: bitcoin-tx.cpp:646
bool checkObject(const std::map< std::string, UniValue::VType > &memberTypes)
Definition: univalue.cpp:225
@ VOBJ
Definition: univalue.h:21
@ VSTR
Definition: univalue.h:21
@ VNUM
Definition: univalue.h:21
const std::string & getValStr() const
Definition: univalue.h:66
size_t size() const
Definition: univalue.h:69
bool read(const char *raw)
bool isStr() const
Definition: univalue.h:81
bool exists(const std::string &key) const
Definition: univalue.h:75
bool isNum() const
Definition: univalue.h:82
bool isObject() const
Definition: univalue.h:84
std::string GetHex() const
Definition: uint256.cpp:21
256-bit opaque blob.
Definition: uint256.h:123
std::string FormatFullVersion()
CScript ParseScript(const std::string &s)
Definition: core_read.cpp:23
std::vector< unsigned char > ParseHexUV(const UniValue &v, const std::string &strName)
Definition: core_read.cpp:159
std::string ScriptToAsmStr(const CScript &script, const bool fAttemptSighashDecode=false)
Create the assembly string representation of a CScript object.
Definition: core_write.cpp:73
std::string EncodeHexTx(const CTransaction &tx, const int serializeFlags=0)
Definition: core_write.cpp:117
bool DecodeHexTx(CMutableTransaction &tx, const std::string &strHexTx, bool fTryNoWitness=false)
Definition: core_read.cpp:91
uint256 ParseHashUV(const UniValue &v, const std::string &strName)
Definition: core_read.cpp:141
void TxToUniv(const CTransaction &tx, const uint256 &hashBlock, UniValue &entry)
Definition: core_write.cpp:149
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
@ SIGHASH_ANYONECANPAY
Definition: interpreter.h:27
@ SIGHASH_ALL
Definition: interpreter.h:24
@ SIGHASH_NONE
Definition: interpreter.h:25
@ SIGHASH_SINGLE
Definition: interpreter.h:26
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:299
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:316
@ OP_RETURN
Definition: script.h:82
void UpdateTransaction(CMutableTransaction &tx, unsigned int nIn, const SignatureData &data)
Definition: sign.cpp:201
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn)
Extract signature data from a transaction, and insert it.
Definition: sign.cpp:192
SignatureData CombineSignatures(const CScript &scriptPubKey, const BaseSignatureChecker &checker, const SignatureData &scriptSig1, const SignatureData &scriptSig2)
Combine two script signatures using a generic signature checker, intelligently, possibly with OP_0 pl...
Definition: sign.cpp:382
bool ProduceSignature(const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
Definition: sign.cpp:143
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Definition: standard.cpp:293
CScript GetScriptForWitness(const CScript &redeemscript)
Definition: standard.cpp:304
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Definition: standard.cpp:288
CScript GetScriptForDestination(const CTxDestination &dest)
Definition: standard.cpp:280
A mutable version of CTransaction.
Definition: transaction.h:413
std::vector< CTxOut > vout
Definition: transaction.h:416
std::vector< CTxIn > vin
Definition: transaction.h:415
#define strprintf
Definition: tinyformat.h:1047
uint256 uint256S(const char *str)
Definition: uint256.h:144
void ParseParameters(int argc, const char *const argv[])
Definition: util.cpp:353
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
Definition: util.cpp:411
void SetupEnvironment()
Definition: util.cpp:797
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: util.cpp:475
std::string HelpMessageGroup(const std::string &message)
Format a string to be used as group of options in help messages.
Definition: util.cpp:448
bool IsArgSet(const std::string &strArg)
Return true if the given argument has been manually set.
Definition: util.cpp:389
std::string HelpMessageOpt(const std::string &option, const std::string &message)
Format a string to be used as option description in help messages.
Definition: util.cpp:452
bool error(const char *fmt, const Args &... args)
Definition: util.h:87
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result.
Definition: util.h:62
bool IsSwitchChar(char c)
Definition: util.h:117
bool ParseMoney(const string &str, CAmount &nRet)
int64_t atoi64(const char *psz)
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool IsHex(const string &str)
int atoi(const std::string &str)
vector< unsigned char > ParseHex(const char *psz)