You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bitcoin-tx.cpp 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. // Copyright (c) 2009-2015 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. #include "base58.h"
  5. #include "clientversion.h"
  6. #include "coins.h"
  7. #include "consensus/consensus.h"
  8. #include "core_io.h"
  9. #include "keystore.h"
  10. #include "policy/policy.h"
  11. #include "primitives/transaction.h"
  12. #include "script/script.h"
  13. #include "script/sign.h"
  14. #include <univalue.h>
  15. #include "util.h"
  16. #include "utilmoneystr.h"
  17. #include "utilstrencodings.h"
  18. #include <stdio.h>
  19. #include <boost/algorithm/string.hpp>
  20. #include <boost/assign/list_of.hpp>
  21. using namespace std;
  22. static bool fCreateBlank;
  23. static map<string,UniValue> registers;
  24. static bool AppInitRawTx(int argc, char* argv[])
  25. {
  26. //
  27. // Parameters
  28. //
  29. ParseParameters(argc, argv);
  30. // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
  31. try {
  32. SelectParams(ChainNameFromCommandLine());
  33. } catch (const std::exception& e) {
  34. fprintf(stderr, "Error: %s\n", e.what());
  35. return false;
  36. }
  37. fCreateBlank = GetBoolArg("-create", false);
  38. if (argc<2 || mapArgs.count("-?") || mapArgs.count("-h") || mapArgs.count("-help"))
  39. {
  40. // First part of help message is specific to this utility
  41. std::string strUsage = _("Bitcoin Core bitcoin-tx utility version") + " " + FormatFullVersion() + "\n\n" +
  42. _("Usage:") + "\n" +
  43. " bitcoin-tx [options] <hex-tx> [commands] " + _("Update hex-encoded bitcoin transaction") + "\n" +
  44. " bitcoin-tx [options] -create [commands] " + _("Create hex-encoded bitcoin transaction") + "\n" +
  45. "\n";
  46. fprintf(stdout, "%s", strUsage.c_str());
  47. strUsage = HelpMessageGroup(_("Options:"));
  48. strUsage += HelpMessageOpt("-?", _("This help message"));
  49. strUsage += HelpMessageOpt("-create", _("Create new, empty TX."));
  50. strUsage += HelpMessageOpt("-json", _("Select JSON output"));
  51. strUsage += HelpMessageOpt("-txid", _("Output only the hex-encoded transaction id of the resultant transaction."));
  52. AppendParamsHelpMessages(strUsage);
  53. fprintf(stdout, "%s", strUsage.c_str());
  54. strUsage = HelpMessageGroup(_("Commands:"));
  55. strUsage += HelpMessageOpt("delin=N", _("Delete input N from TX"));
  56. strUsage += HelpMessageOpt("delout=N", _("Delete output N from TX"));
  57. strUsage += HelpMessageOpt("in=TXID:VOUT", _("Add input to TX"));
  58. strUsage += HelpMessageOpt("locktime=N", _("Set TX lock time to N"));
  59. strUsage += HelpMessageOpt("nversion=N", _("Set TX version to N"));
  60. strUsage += HelpMessageOpt("outaddr=VALUE:ADDRESS", _("Add address-based output to TX"));
  61. strUsage += HelpMessageOpt("outdata=[VALUE:]DATA", _("Add data-based output to TX"));
  62. strUsage += HelpMessageOpt("outscript=VALUE:SCRIPT", _("Add raw script output to TX"));
  63. strUsage += HelpMessageOpt("sign=SIGHASH-FLAGS", _("Add zero or more signatures to transaction") + ". " +
  64. _("This command requires JSON registers:") +
  65. _("prevtxs=JSON object") + ", " +
  66. _("privatekeys=JSON object") + ". " +
  67. _("See signrawtransaction docs for format of sighash flags, JSON objects."));
  68. fprintf(stdout, "%s", strUsage.c_str());
  69. strUsage = HelpMessageGroup(_("Register Commands:"));
  70. strUsage += HelpMessageOpt("load=NAME:FILENAME", _("Load JSON file FILENAME into register NAME"));
  71. strUsage += HelpMessageOpt("set=NAME:JSON-STRING", _("Set register NAME to given JSON-STRING"));
  72. fprintf(stdout, "%s", strUsage.c_str());
  73. return false;
  74. }
  75. return true;
  76. }
  77. static void RegisterSetJson(const string& key, const string& rawJson)
  78. {
  79. UniValue val;
  80. if (!val.read(rawJson)) {
  81. string strErr = "Cannot parse JSON for key " + key;
  82. throw runtime_error(strErr);
  83. }
  84. registers[key] = val;
  85. }
  86. static void RegisterSet(const string& strInput)
  87. {
  88. // separate NAME:VALUE in string
  89. size_t pos = strInput.find(':');
  90. if ((pos == string::npos) ||
  91. (pos == 0) ||
  92. (pos == (strInput.size() - 1)))
  93. throw runtime_error("Register input requires NAME:VALUE");
  94. string key = strInput.substr(0, pos);
  95. string valStr = strInput.substr(pos + 1, string::npos);
  96. RegisterSetJson(key, valStr);
  97. }
  98. static void RegisterLoad(const string& strInput)
  99. {
  100. // separate NAME:FILENAME in string
  101. size_t pos = strInput.find(':');
  102. if ((pos == string::npos) ||
  103. (pos == 0) ||
  104. (pos == (strInput.size() - 1)))
  105. throw runtime_error("Register load requires NAME:FILENAME");
  106. string key = strInput.substr(0, pos);
  107. string filename = strInput.substr(pos + 1, string::npos);
  108. FILE *f = fopen(filename.c_str(), "r");
  109. if (!f) {
  110. string strErr = "Cannot open file " + filename;
  111. throw runtime_error(strErr);
  112. }
  113. // load file chunks into one big buffer
  114. string valStr;
  115. while ((!feof(f)) && (!ferror(f))) {
  116. char buf[4096];
  117. int bread = fread(buf, 1, sizeof(buf), f);
  118. if (bread <= 0)
  119. break;
  120. valStr.insert(valStr.size(), buf, bread);
  121. }
  122. int error = ferror(f);
  123. fclose(f);
  124. if (error) {
  125. string strErr = "Error reading file " + filename;
  126. throw runtime_error(strErr);
  127. }
  128. // evaluate as JSON buffer register
  129. RegisterSetJson(key, valStr);
  130. }
  131. static void MutateTxVersion(CMutableTransaction& tx, const string& cmdVal)
  132. {
  133. int64_t newVersion = atoi64(cmdVal);
  134. if (newVersion < 1 || newVersion > CTransaction::CURRENT_VERSION)
  135. throw runtime_error("Invalid TX version requested");
  136. tx.nVersion = (int) newVersion;
  137. }
  138. static void MutateTxLocktime(CMutableTransaction& tx, const string& cmdVal)
  139. {
  140. int64_t newLocktime = atoi64(cmdVal);
  141. if (newLocktime < 0LL || newLocktime > 0xffffffffLL)
  142. throw runtime_error("Invalid TX locktime requested");
  143. tx.nLockTime = (unsigned int) newLocktime;
  144. }
  145. static void MutateTxAddInput(CMutableTransaction& tx, const string& strInput)
  146. {
  147. // separate TXID:VOUT in string
  148. size_t pos = strInput.find(':');
  149. if ((pos == string::npos) ||
  150. (pos == 0) ||
  151. (pos == (strInput.size() - 1)))
  152. throw runtime_error("TX input missing separator");
  153. // extract and validate TXID
  154. string strTxid = strInput.substr(0, pos);
  155. if ((strTxid.size() != 64) || !IsHex(strTxid))
  156. throw runtime_error("invalid TX input txid");
  157. uint256 txid(uint256S(strTxid));
  158. static const unsigned int minTxOutSz = 9;
  159. static const unsigned int maxVout = MAX_BLOCK_SIZE / minTxOutSz;
  160. // extract and validate vout
  161. string strVout = strInput.substr(pos + 1, string::npos);
  162. int vout = atoi(strVout);
  163. if ((vout < 0) || (vout > (int)maxVout))
  164. throw runtime_error("invalid TX input vout");
  165. // append to transaction input list
  166. CTxIn txin(txid, vout);
  167. tx.vin.push_back(txin);
  168. }
  169. static void MutateTxAddOutAddr(CMutableTransaction& tx, const string& strInput)
  170. {
  171. // separate VALUE:ADDRESS in string
  172. size_t pos = strInput.find(':');
  173. if ((pos == string::npos) ||
  174. (pos == 0) ||
  175. (pos == (strInput.size() - 1)))
  176. throw runtime_error("TX output missing separator");
  177. // extract and validate VALUE
  178. string strValue = strInput.substr(0, pos);
  179. CAmount value;
  180. if (!ParseMoney(strValue, value))
  181. throw runtime_error("invalid TX output value");
  182. // extract and validate ADDRESS
  183. string strAddr = strInput.substr(pos + 1, string::npos);
  184. CBitcoinAddress addr(strAddr);
  185. if (!addr.IsValid())
  186. throw runtime_error("invalid TX output address");
  187. // build standard output script via GetScriptForDestination()
  188. CScript scriptPubKey = GetScriptForDestination(addr.Get());
  189. // construct TxOut, append to transaction output list
  190. CTxOut txout(value, scriptPubKey);
  191. tx.vout.push_back(txout);
  192. }
  193. static void MutateTxAddOutData(CMutableTransaction& tx, const string& strInput)
  194. {
  195. CAmount value = 0;
  196. // separate [VALUE:]DATA in string
  197. size_t pos = strInput.find(':');
  198. if (pos==0)
  199. throw runtime_error("TX output value not specified");
  200. if (pos != string::npos) {
  201. // extract and validate VALUE
  202. string strValue = strInput.substr(0, pos);
  203. if (!ParseMoney(strValue, value))
  204. throw runtime_error("invalid TX output value");
  205. }
  206. // extract and validate DATA
  207. string strData = strInput.substr(pos + 1, string::npos);
  208. if (!IsHex(strData))
  209. throw runtime_error("invalid TX output data");
  210. std::vector<unsigned char> data = ParseHex(strData);
  211. CTxOut txout(value, CScript() << OP_RETURN << data);
  212. tx.vout.push_back(txout);
  213. }
  214. static void MutateTxAddOutScript(CMutableTransaction& tx, const string& strInput)
  215. {
  216. // separate VALUE:SCRIPT in string
  217. size_t pos = strInput.find(':');
  218. if ((pos == string::npos) ||
  219. (pos == 0))
  220. throw runtime_error("TX output missing separator");
  221. // extract and validate VALUE
  222. string strValue = strInput.substr(0, pos);
  223. CAmount value;
  224. if (!ParseMoney(strValue, value))
  225. throw runtime_error("invalid TX output value");
  226. // extract and validate script
  227. string strScript = strInput.substr(pos + 1, string::npos);
  228. CScript scriptPubKey = ParseScript(strScript); // throws on err
  229. // construct TxOut, append to transaction output list
  230. CTxOut txout(value, scriptPubKey);
  231. tx.vout.push_back(txout);
  232. }
  233. static void MutateTxDelInput(CMutableTransaction& tx, const string& strInIdx)
  234. {
  235. // parse requested deletion index
  236. int inIdx = atoi(strInIdx);
  237. if (inIdx < 0 || inIdx >= (int)tx.vin.size()) {
  238. string strErr = "Invalid TX input index '" + strInIdx + "'";
  239. throw runtime_error(strErr.c_str());
  240. }
  241. // delete input from transaction
  242. tx.vin.erase(tx.vin.begin() + inIdx);
  243. }
  244. static void MutateTxDelOutput(CMutableTransaction& tx, const string& strOutIdx)
  245. {
  246. // parse requested deletion index
  247. int outIdx = atoi(strOutIdx);
  248. if (outIdx < 0 || outIdx >= (int)tx.vout.size()) {
  249. string strErr = "Invalid TX output index '" + strOutIdx + "'";
  250. throw runtime_error(strErr.c_str());
  251. }
  252. // delete output from transaction
  253. tx.vout.erase(tx.vout.begin() + outIdx);
  254. }
  255. static const unsigned int N_SIGHASH_OPTS = 6;
  256. static const struct {
  257. const char *flagStr;
  258. int flags;
  259. } sighashOptions[N_SIGHASH_OPTS] = {
  260. {"ALL", SIGHASH_ALL},
  261. {"NONE", SIGHASH_NONE},
  262. {"SINGLE", SIGHASH_SINGLE},
  263. {"ALL|ANYONECANPAY", SIGHASH_ALL|SIGHASH_ANYONECANPAY},
  264. {"NONE|ANYONECANPAY", SIGHASH_NONE|SIGHASH_ANYONECANPAY},
  265. {"SINGLE|ANYONECANPAY", SIGHASH_SINGLE|SIGHASH_ANYONECANPAY},
  266. };
  267. static bool findSighashFlags(int& flags, const string& flagStr)
  268. {
  269. flags = 0;
  270. for (unsigned int i = 0; i < N_SIGHASH_OPTS; i++) {
  271. if (flagStr == sighashOptions[i].flagStr) {
  272. flags = sighashOptions[i].flags;
  273. return true;
  274. }
  275. }
  276. return false;
  277. }
  278. uint256 ParseHashUO(map<string,UniValue>& o, string strKey)
  279. {
  280. if (!o.count(strKey))
  281. return uint256();
  282. return ParseHashUV(o[strKey], strKey);
  283. }
  284. vector<unsigned char> ParseHexUO(map<string,UniValue>& o, string strKey)
  285. {
  286. if (!o.count(strKey)) {
  287. vector<unsigned char> emptyVec;
  288. return emptyVec;
  289. }
  290. return ParseHexUV(o[strKey], strKey);
  291. }
  292. static void MutateTxSign(CMutableTransaction& tx, const string& flagStr)
  293. {
  294. int nHashType = SIGHASH_ALL;
  295. if (flagStr.size() > 0)
  296. if (!findSighashFlags(nHashType, flagStr))
  297. throw runtime_error("unknown sighash flag/sign option");
  298. vector<CTransaction> txVariants;
  299. txVariants.push_back(tx);
  300. // mergedTx will end up with all the signatures; it
  301. // starts as a clone of the raw tx:
  302. CMutableTransaction mergedTx(txVariants[0]);
  303. bool fComplete = true;
  304. CCoinsView viewDummy;
  305. CCoinsViewCache view(&viewDummy);
  306. if (!registers.count("privatekeys"))
  307. throw runtime_error("privatekeys register variable must be set.");
  308. bool fGivenKeys = false;
  309. CBasicKeyStore tempKeystore;
  310. UniValue keysObj = registers["privatekeys"];
  311. fGivenKeys = true;
  312. for (unsigned int kidx = 0; kidx < keysObj.size(); kidx++) {
  313. if (!keysObj[kidx].isStr())
  314. throw runtime_error("privatekey not a string");
  315. CBitcoinSecret vchSecret;
  316. bool fGood = vchSecret.SetString(keysObj[kidx].getValStr());
  317. if (!fGood)
  318. throw runtime_error("privatekey not valid");
  319. CKey key = vchSecret.GetKey();
  320. tempKeystore.AddKey(key);
  321. }
  322. // Add previous txouts given in the RPC call:
  323. if (!registers.count("prevtxs"))
  324. throw runtime_error("prevtxs register variable must be set.");
  325. UniValue prevtxsObj = registers["prevtxs"];
  326. {
  327. for (unsigned int previdx = 0; previdx < prevtxsObj.size(); previdx++) {
  328. UniValue prevOut = prevtxsObj[previdx];
  329. if (!prevOut.isObject())
  330. throw runtime_error("expected prevtxs internal object");
  331. map<string,UniValue::VType> types = boost::assign::map_list_of("txid", UniValue::VSTR)("vout",UniValue::VNUM)("scriptPubKey",UniValue::VSTR);
  332. if (!prevOut.checkObject(types))
  333. throw runtime_error("prevtxs internal object typecheck fail");
  334. uint256 txid = ParseHashUV(prevOut["txid"], "txid");
  335. int nOut = atoi(prevOut["vout"].getValStr());
  336. if (nOut < 0)
  337. throw runtime_error("vout must be positive");
  338. vector<unsigned char> pkData(ParseHexUV(prevOut["scriptPubKey"], "scriptPubKey"));
  339. CScript scriptPubKey(pkData.begin(), pkData.end());
  340. {
  341. CCoinsModifier coins = view.ModifyCoins(txid);
  342. if (coins->IsAvailable(nOut) && coins->vout[nOut].scriptPubKey != scriptPubKey) {
  343. string err("Previous output scriptPubKey mismatch:\n");
  344. err = err + ScriptToAsmStr(coins->vout[nOut].scriptPubKey) + "\nvs:\n"+
  345. ScriptToAsmStr(scriptPubKey);
  346. throw runtime_error(err);
  347. }
  348. if ((unsigned int)nOut >= coins->vout.size())
  349. coins->vout.resize(nOut+1);
  350. coins->vout[nOut].scriptPubKey = scriptPubKey;
  351. coins->vout[nOut].nValue = 0; // we don't know the actual output value
  352. }
  353. // if redeemScript given and private keys given,
  354. // add redeemScript to the tempKeystore so it can be signed:
  355. if (fGivenKeys && scriptPubKey.IsPayToScriptHash() &&
  356. prevOut.exists("redeemScript")) {
  357. UniValue v = prevOut["redeemScript"];
  358. vector<unsigned char> rsData(ParseHexUV(v, "redeemScript"));
  359. CScript redeemScript(rsData.begin(), rsData.end());
  360. tempKeystore.AddCScript(redeemScript);
  361. }
  362. }
  363. }
  364. const CKeyStore& keystore = tempKeystore;
  365. bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
  366. // Sign what we can:
  367. for (unsigned int i = 0; i < mergedTx.vin.size(); i++) {
  368. CTxIn& txin = mergedTx.vin[i];
  369. const CCoins* coins = view.AccessCoins(txin.prevout.hash);
  370. if (!coins || !coins->IsAvailable(txin.prevout.n)) {
  371. fComplete = false;
  372. continue;
  373. }
  374. const CScript& prevPubKey = coins->vout[txin.prevout.n].scriptPubKey;
  375. txin.scriptSig.clear();
  376. // Only sign SIGHASH_SINGLE if there's a corresponding output:
  377. if (!fHashSingle || (i < mergedTx.vout.size()))
  378. SignSignature(keystore, prevPubKey, mergedTx, i, nHashType);
  379. // ... and merge in other signatures:
  380. BOOST_FOREACH(const CTransaction& txv, txVariants) {
  381. txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig);
  382. }
  383. if (!VerifyScript(txin.scriptSig, prevPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker(&mergedTx, i)))
  384. fComplete = false;
  385. }
  386. if (fComplete) {
  387. // do nothing... for now
  388. // perhaps store this for later optional JSON output
  389. }
  390. tx = mergedTx;
  391. }
  392. class Secp256k1Init
  393. {
  394. ECCVerifyHandle globalVerifyHandle;
  395. public:
  396. Secp256k1Init() {
  397. ECC_Start();
  398. }
  399. ~Secp256k1Init() {
  400. ECC_Stop();
  401. }
  402. };
  403. static void MutateTx(CMutableTransaction& tx, const string& command,
  404. const string& commandVal)
  405. {
  406. boost::scoped_ptr<Secp256k1Init> ecc;
  407. if (command == "nversion")
  408. MutateTxVersion(tx, commandVal);
  409. else if (command == "locktime")
  410. MutateTxLocktime(tx, commandVal);
  411. else if (command == "delin")
  412. MutateTxDelInput(tx, commandVal);
  413. else if (command == "in")
  414. MutateTxAddInput(tx, commandVal);
  415. else if (command == "delout")
  416. MutateTxDelOutput(tx, commandVal);
  417. else if (command == "outaddr")
  418. MutateTxAddOutAddr(tx, commandVal);
  419. else if (command == "outdata")
  420. MutateTxAddOutData(tx, commandVal);
  421. else if (command == "outscript")
  422. MutateTxAddOutScript(tx, commandVal);
  423. else if (command == "sign") {
  424. if (!ecc) { ecc.reset(new Secp256k1Init()); }
  425. MutateTxSign(tx, commandVal);
  426. }
  427. else if (command == "load")
  428. RegisterLoad(commandVal);
  429. else if (command == "set")
  430. RegisterSet(commandVal);
  431. else
  432. throw runtime_error("unknown command");
  433. }
  434. static void OutputTxJSON(const CTransaction& tx)
  435. {
  436. UniValue entry(UniValue::VOBJ);
  437. TxToUniv(tx, uint256(), entry);
  438. string jsonOutput = entry.write(4);
  439. fprintf(stdout, "%s\n", jsonOutput.c_str());
  440. }
  441. static void OutputTxHash(const CTransaction& tx)
  442. {
  443. string strHexHash = tx.GetHash().GetHex(); // the hex-encoded transaction hash (aka the transaction id)
  444. fprintf(stdout, "%s\n", strHexHash.c_str());
  445. }
  446. static void OutputTxHex(const CTransaction& tx)
  447. {
  448. string strHex = EncodeHexTx(tx);
  449. fprintf(stdout, "%s\n", strHex.c_str());
  450. }
  451. static void OutputTx(const CTransaction& tx)
  452. {
  453. if (GetBoolArg("-json", false))
  454. OutputTxJSON(tx);
  455. else if (GetBoolArg("-txid", false))
  456. OutputTxHash(tx);
  457. else
  458. OutputTxHex(tx);
  459. }
  460. static string readStdin()
  461. {
  462. char buf[4096];
  463. string ret;
  464. while (!feof(stdin)) {
  465. size_t bread = fread(buf, 1, sizeof(buf), stdin);
  466. ret.append(buf, bread);
  467. if (bread < sizeof(buf))
  468. break;
  469. }
  470. if (ferror(stdin))
  471. throw runtime_error("error reading stdin");
  472. boost::algorithm::trim_right(ret);
  473. return ret;
  474. }
  475. static int CommandLineRawTx(int argc, char* argv[])
  476. {
  477. string strPrint;
  478. int nRet = 0;
  479. try {
  480. // Skip switches; Permit common stdin convention "-"
  481. while (argc > 1 && IsSwitchChar(argv[1][0]) &&
  482. (argv[1][1] != 0)) {
  483. argc--;
  484. argv++;
  485. }
  486. CTransaction txDecodeTmp;
  487. int startArg;
  488. if (!fCreateBlank) {
  489. // require at least one param
  490. if (argc < 2)
  491. throw runtime_error("too few parameters");
  492. // param: hex-encoded bitcoin transaction
  493. string strHexTx(argv[1]);
  494. if (strHexTx == "-") // "-" implies standard input
  495. strHexTx = readStdin();
  496. if (!DecodeHexTx(txDecodeTmp, strHexTx))
  497. throw runtime_error("invalid transaction encoding");
  498. startArg = 2;
  499. } else
  500. startArg = 1;
  501. CMutableTransaction tx(txDecodeTmp);
  502. for (int i = startArg; i < argc; i++) {
  503. string arg = argv[i];
  504. string key, value;
  505. size_t eqpos = arg.find('=');
  506. if (eqpos == string::npos)
  507. key = arg;
  508. else {
  509. key = arg.substr(0, eqpos);
  510. value = arg.substr(eqpos + 1);
  511. }
  512. MutateTx(tx, key, value);
  513. }
  514. OutputTx(tx);
  515. }
  516. catch (const boost::thread_interrupted&) {
  517. throw;
  518. }
  519. catch (const std::exception& e) {
  520. strPrint = string("error: ") + e.what();
  521. nRet = EXIT_FAILURE;
  522. }
  523. catch (...) {
  524. PrintExceptionContinue(NULL, "CommandLineRawTx()");
  525. throw;
  526. }
  527. if (strPrint != "") {
  528. fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str());
  529. }
  530. return nRet;
  531. }
  532. int main(int argc, char* argv[])
  533. {
  534. SetupEnvironment();
  535. try {
  536. if(!AppInitRawTx(argc, argv))
  537. return EXIT_FAILURE;
  538. }
  539. catch (const std::exception& e) {
  540. PrintExceptionContinue(&e, "AppInitRawTx()");
  541. return EXIT_FAILURE;
  542. } catch (...) {
  543. PrintExceptionContinue(NULL, "AppInitRawTx()");
  544. return EXIT_FAILURE;
  545. }
  546. int ret = EXIT_FAILURE;
  547. try {
  548. ret = CommandLineRawTx(argc, argv);
  549. }
  550. catch (const std::exception& e) {
  551. PrintExceptionContinue(&e, "CommandLineRawTx()");
  552. } catch (...) {
  553. PrintExceptionContinue(NULL, "CommandLineRawTx()");
  554. }
  555. return ret;
  556. }