1 // Copyright (c) 2009-2014 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.
6 #include "clientversion.h"
8 #include "consensus/consensus.h"
11 #include "primitives/transaction.h"
12 #include "script/script.h"
13 #include "script/sign.h"
14 #include "univalue/univalue.h"
16 #include "utilmoneystr.h"
17 #include "utilstrencodings.h"
21 #include <boost/algorithm/string.hpp>
22 #include <boost/assign/list_of.hpp>
26 static bool fCreateBlank;
27 static map<string,UniValue> registers;
29 static bool AppInitRawTx(int argc, char* argv[])
34 ParseParameters(argc, argv);
36 // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
37 if (!SelectParamsFromCommandLine()) {
38 fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
42 fCreateBlank = GetBoolArg("-create", false);
44 if (argc<2 || mapArgs.count("-?") || mapArgs.count("-help"))
46 // First part of help message is specific to this utility
47 std::string strUsage = _("Bitcoin Core bitcoin-tx utility version") + " " + FormatFullVersion() + "\n\n" +
49 " bitcoin-tx [options] <hex-tx> [commands] " + _("Update hex-encoded bitcoin transaction") + "\n" +
50 " bitcoin-tx [options] -create [commands] " + _("Create hex-encoded bitcoin transaction") + "\n" +
53 fprintf(stdout, "%s", strUsage.c_str());
55 strUsage = HelpMessageGroup(_("Options:"));
56 strUsage += HelpMessageOpt("-?", _("This help message"));
57 strUsage += HelpMessageOpt("-create", _("Create new, empty TX."));
58 strUsage += HelpMessageOpt("-json", _("Select JSON output"));
59 strUsage += HelpMessageOpt("-txid", _("Output only the hex-encoded transaction id of the resultant transaction."));
60 strUsage += HelpMessageOpt("-regtest", _("Enter regression test mode, which uses a special chain in which blocks can be solved instantly."));
61 strUsage += HelpMessageOpt("-testnet", _("Use the test network"));
63 fprintf(stdout, "%s", strUsage.c_str());
65 strUsage = HelpMessageGroup(_("Commands:"));
66 strUsage += HelpMessageOpt("delin=N", _("Delete input N from TX"));
67 strUsage += HelpMessageOpt("delout=N", _("Delete output N from TX"));
68 strUsage += HelpMessageOpt("in=TXID:VOUT", _("Add input to TX"));
69 strUsage += HelpMessageOpt("locktime=N", _("Set TX lock time to N"));
70 strUsage += HelpMessageOpt("nversion=N", _("Set TX version to N"));
71 strUsage += HelpMessageOpt("outaddr=VALUE:ADDRESS", _("Add address-based output to TX"));
72 strUsage += HelpMessageOpt("outscript=VALUE:SCRIPT", _("Add raw script output to TX"));
73 strUsage += HelpMessageOpt("sign=SIGHASH-FLAGS", _("Add zero or more signatures to transaction") + ". " +
74 _("This command requires JSON registers:") +
75 _("prevtxs=JSON object") + ", " +
76 _("privatekeys=JSON object") + ". " +
77 _("See signrawtransaction docs for format of sighash flags, JSON objects."));
78 fprintf(stdout, "%s", strUsage.c_str());
80 strUsage = HelpMessageGroup(_("Register Commands:"));
81 strUsage += HelpMessageOpt("load=NAME:FILENAME", _("Load JSON file FILENAME into register NAME"));
82 strUsage += HelpMessageOpt("set=NAME:JSON-STRING", _("Set register NAME to given JSON-STRING"));
83 fprintf(stdout, "%s", strUsage.c_str());
90 static void RegisterSetJson(const string& key, const string& rawJson)
93 if (!val.read(rawJson)) {
94 string strErr = "Cannot parse JSON for key " + key;
95 throw runtime_error(strErr);
101 static void RegisterSet(const string& strInput)
103 // separate NAME:VALUE in string
104 size_t pos = strInput.find(':');
105 if ((pos == string::npos) ||
107 (pos == (strInput.size() - 1)))
108 throw runtime_error("Register input requires NAME:VALUE");
110 string key = strInput.substr(0, pos);
111 string valStr = strInput.substr(pos + 1, string::npos);
113 RegisterSetJson(key, valStr);
116 static void RegisterLoad(const string& strInput)
118 // separate NAME:FILENAME in string
119 size_t pos = strInput.find(':');
120 if ((pos == string::npos) ||
122 (pos == (strInput.size() - 1)))
123 throw runtime_error("Register load requires NAME:FILENAME");
125 string key = strInput.substr(0, pos);
126 string filename = strInput.substr(pos + 1, string::npos);
128 FILE *f = fopen(filename.c_str(), "r");
130 string strErr = "Cannot open file " + filename;
131 throw runtime_error(strErr);
134 // load file chunks into one big buffer
136 while ((!feof(f)) && (!ferror(f))) {
138 int bread = fread(buf, 1, sizeof(buf), f);
142 valStr.insert(valStr.size(), buf, bread);
146 string strErr = "Error reading file " + filename;
147 throw runtime_error(strErr);
152 // evaluate as JSON buffer register
153 RegisterSetJson(key, valStr);
156 static void MutateTxVersion(CMutableTransaction& tx, const string& cmdVal)
158 int64_t newVersion = atoi64(cmdVal);
159 if (newVersion < 1 || newVersion > CTransaction::CURRENT_VERSION)
160 throw runtime_error("Invalid TX version requested");
162 tx.nVersion = (int) newVersion;
165 static void MutateTxLocktime(CMutableTransaction& tx, const string& cmdVal)
167 int64_t newLocktime = atoi64(cmdVal);
168 if (newLocktime < 0LL || newLocktime > 0xffffffffLL)
169 throw runtime_error("Invalid TX locktime requested");
171 tx.nLockTime = (unsigned int) newLocktime;
174 static void MutateTxAddInput(CMutableTransaction& tx, const string& strInput)
176 // separate TXID:VOUT in string
177 size_t pos = strInput.find(':');
178 if ((pos == string::npos) ||
180 (pos == (strInput.size() - 1)))
181 throw runtime_error("TX input missing separator");
183 // extract and validate TXID
184 string strTxid = strInput.substr(0, pos);
185 if ((strTxid.size() != 64) || !IsHex(strTxid))
186 throw runtime_error("invalid TX input txid");
187 uint256 txid(uint256S(strTxid));
189 static const unsigned int minTxOutSz = 9;
190 static const unsigned int maxVout = MAX_BLOCK_SIZE / minTxOutSz;
192 // extract and validate vout
193 string strVout = strInput.substr(pos + 1, string::npos);
194 int vout = atoi(strVout);
195 if ((vout < 0) || (vout > (int)maxVout))
196 throw runtime_error("invalid TX input vout");
198 // append to transaction input list
199 CTxIn txin(txid, vout);
200 tx.vin.push_back(txin);
203 static void MutateTxAddOutAddr(CMutableTransaction& tx, const string& strInput)
205 // separate VALUE:ADDRESS in string
206 size_t pos = strInput.find(':');
207 if ((pos == string::npos) ||
209 (pos == (strInput.size() - 1)))
210 throw runtime_error("TX output missing separator");
212 // extract and validate VALUE
213 string strValue = strInput.substr(0, pos);
215 if (!ParseMoney(strValue, value))
216 throw runtime_error("invalid TX output value");
218 // extract and validate ADDRESS
219 string strAddr = strInput.substr(pos + 1, string::npos);
220 CBitcoinAddress addr(strAddr);
222 throw runtime_error("invalid TX output address");
224 // build standard output script via GetScriptForDestination()
225 CScript scriptPubKey = GetScriptForDestination(addr.Get());
227 // construct TxOut, append to transaction output list
228 CTxOut txout(value, scriptPubKey);
229 tx.vout.push_back(txout);
232 static void MutateTxAddOutScript(CMutableTransaction& tx, const string& strInput)
234 // separate VALUE:SCRIPT in string
235 size_t pos = strInput.find(':');
236 if ((pos == string::npos) ||
238 throw runtime_error("TX output missing separator");
240 // extract and validate VALUE
241 string strValue = strInput.substr(0, pos);
243 if (!ParseMoney(strValue, value))
244 throw runtime_error("invalid TX output value");
246 // extract and validate script
247 string strScript = strInput.substr(pos + 1, string::npos);
248 CScript scriptPubKey = ParseScript(strScript); // throws on err
250 // construct TxOut, append to transaction output list
251 CTxOut txout(value, scriptPubKey);
252 tx.vout.push_back(txout);
255 static void MutateTxDelInput(CMutableTransaction& tx, const string& strInIdx)
257 // parse requested deletion index
258 int inIdx = atoi(strInIdx);
259 if (inIdx < 0 || inIdx >= (int)tx.vin.size()) {
260 string strErr = "Invalid TX input index '" + strInIdx + "'";
261 throw runtime_error(strErr.c_str());
264 // delete input from transaction
265 tx.vin.erase(tx.vin.begin() + inIdx);
268 static void MutateTxDelOutput(CMutableTransaction& tx, const string& strOutIdx)
270 // parse requested deletion index
271 int outIdx = atoi(strOutIdx);
272 if (outIdx < 0 || outIdx >= (int)tx.vout.size()) {
273 string strErr = "Invalid TX output index '" + strOutIdx + "'";
274 throw runtime_error(strErr.c_str());
277 // delete output from transaction
278 tx.vout.erase(tx.vout.begin() + outIdx);
281 static const unsigned int N_SIGHASH_OPTS = 6;
282 static const struct {
285 } sighashOptions[N_SIGHASH_OPTS] = {
286 {"ALL", SIGHASH_ALL},
287 {"NONE", SIGHASH_NONE},
288 {"SINGLE", SIGHASH_SINGLE},
289 {"ALL|ANYONECANPAY", SIGHASH_ALL|SIGHASH_ANYONECANPAY},
290 {"NONE|ANYONECANPAY", SIGHASH_NONE|SIGHASH_ANYONECANPAY},
291 {"SINGLE|ANYONECANPAY", SIGHASH_SINGLE|SIGHASH_ANYONECANPAY},
294 static bool findSighashFlags(int& flags, const string& flagStr)
298 for (unsigned int i = 0; i < N_SIGHASH_OPTS; i++) {
299 if (flagStr == sighashOptions[i].flagStr) {
300 flags = sighashOptions[i].flags;
308 uint256 ParseHashUO(map<string,UniValue>& o, string strKey)
310 if (!o.count(strKey))
312 return ParseHashUV(o[strKey], strKey);
315 vector<unsigned char> ParseHexUO(map<string,UniValue>& o, string strKey)
317 if (!o.count(strKey)) {
318 vector<unsigned char> emptyVec;
321 return ParseHexUV(o[strKey], strKey);
324 static void MutateTxSign(CMutableTransaction& tx, const string& flagStr)
326 int nHashType = SIGHASH_ALL;
328 if (flagStr.size() > 0)
329 if (!findSighashFlags(nHashType, flagStr))
330 throw runtime_error("unknown sighash flag/sign option");
332 vector<CTransaction> txVariants;
333 txVariants.push_back(tx);
335 // mergedTx will end up with all the signatures; it
336 // starts as a clone of the raw tx:
337 CMutableTransaction mergedTx(txVariants[0]);
338 bool fComplete = true;
339 CCoinsView viewDummy;
340 CCoinsViewCache view(&viewDummy);
342 if (!registers.count("privatekeys"))
343 throw runtime_error("privatekeys register variable must be set.");
344 bool fGivenKeys = false;
345 CBasicKeyStore tempKeystore;
346 UniValue keysObj = registers["privatekeys"];
349 for (unsigned int kidx = 0; kidx < keysObj.count(); kidx++) {
350 if (!keysObj[kidx].isStr())
351 throw runtime_error("privatekey not a string");
352 CBitcoinSecret vchSecret;
353 bool fGood = vchSecret.SetString(keysObj[kidx].getValStr());
355 throw runtime_error("privatekey not valid");
357 CKey key = vchSecret.GetKey();
358 tempKeystore.AddKey(key);
361 // Add previous txouts given in the RPC call:
362 if (!registers.count("prevtxs"))
363 throw runtime_error("prevtxs register variable must be set.");
364 UniValue prevtxsObj = registers["prevtxs"];
366 for (unsigned int previdx = 0; previdx < prevtxsObj.count(); previdx++) {
367 UniValue prevOut = prevtxsObj[previdx];
368 if (!prevOut.isObject())
369 throw runtime_error("expected prevtxs internal object");
371 map<string,UniValue::VType> types = boost::assign::map_list_of("txid", UniValue::VSTR)("vout",UniValue::VNUM)("scriptPubKey",UniValue::VSTR);
372 if (!prevOut.checkObject(types))
373 throw runtime_error("prevtxs internal object typecheck fail");
375 uint256 txid = ParseHashUV(prevOut["txid"], "txid");
377 int nOut = atoi(prevOut["vout"].getValStr());
379 throw runtime_error("vout must be positive");
381 vector<unsigned char> pkData(ParseHexUV(prevOut["scriptPubKey"], "scriptPubKey"));
382 CScript scriptPubKey(pkData.begin(), pkData.end());
385 CCoinsModifier coins = view.ModifyCoins(txid);
386 if (coins->IsAvailable(nOut) && coins->vout[nOut].scriptPubKey != scriptPubKey) {
387 string err("Previous output scriptPubKey mismatch:\n");
388 err = err + coins->vout[nOut].scriptPubKey.ToString() + "\nvs:\n"+
389 scriptPubKey.ToString();
390 throw runtime_error(err);
392 if ((unsigned int)nOut >= coins->vout.size())
393 coins->vout.resize(nOut+1);
394 coins->vout[nOut].scriptPubKey = scriptPubKey;
395 coins->vout[nOut].nValue = 0; // we don't know the actual output value
398 // if redeemScript given and private keys given,
399 // add redeemScript to the tempKeystore so it can be signed:
400 if (fGivenKeys && scriptPubKey.IsPayToScriptHash() &&
401 prevOut.exists("redeemScript")) {
402 UniValue v = prevOut["redeemScript"];
403 vector<unsigned char> rsData(ParseHexUV(v, "redeemScript"));
404 CScript redeemScript(rsData.begin(), rsData.end());
405 tempKeystore.AddCScript(redeemScript);
410 const CKeyStore& keystore = tempKeystore;
412 bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
415 for (unsigned int i = 0; i < mergedTx.vin.size(); i++) {
416 CTxIn& txin = mergedTx.vin[i];
417 const CCoins* coins = view.AccessCoins(txin.prevout.hash);
418 if (!coins || !coins->IsAvailable(txin.prevout.n)) {
422 const CScript& prevPubKey = coins->vout[txin.prevout.n].scriptPubKey;
424 txin.scriptSig.clear();
425 // Only sign SIGHASH_SINGLE if there's a corresponding output:
426 if (!fHashSingle || (i < mergedTx.vout.size()))
427 SignSignature(keystore, prevPubKey, mergedTx, i, nHashType);
429 // ... and merge in other signatures:
430 BOOST_FOREACH(const CTransaction& txv, txVariants) {
431 txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig);
433 if (!VerifyScript(txin.scriptSig, prevPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker(&mergedTx, i)))
438 // do nothing... for now
439 // perhaps store this for later optional JSON output
448 Secp256k1Init() { ECC_Start(); }
449 ~Secp256k1Init() { ECC_Stop(); }
452 static void MutateTx(CMutableTransaction& tx, const string& command,
453 const string& commandVal)
455 boost::scoped_ptr<Secp256k1Init> ecc;
457 if (command == "nversion")
458 MutateTxVersion(tx, commandVal);
459 else if (command == "locktime")
460 MutateTxLocktime(tx, commandVal);
462 else if (command == "delin")
463 MutateTxDelInput(tx, commandVal);
464 else if (command == "in")
465 MutateTxAddInput(tx, commandVal);
467 else if (command == "delout")
468 MutateTxDelOutput(tx, commandVal);
469 else if (command == "outaddr")
470 MutateTxAddOutAddr(tx, commandVal);
471 else if (command == "outscript")
472 MutateTxAddOutScript(tx, commandVal);
474 else if (command == "sign") {
475 if (!ecc) { ecc.reset(new Secp256k1Init()); }
476 MutateTxSign(tx, commandVal);
479 else if (command == "load")
480 RegisterLoad(commandVal);
482 else if (command == "set")
483 RegisterSet(commandVal);
486 throw runtime_error("unknown command");
489 static void OutputTxJSON(const CTransaction& tx)
491 UniValue entry(UniValue::VOBJ);
492 TxToUniv(tx, uint256(), entry);
494 string jsonOutput = entry.write(4);
495 fprintf(stdout, "%s\n", jsonOutput.c_str());
498 static void OutputTxHash(const CTransaction& tx)
500 string strHexHash = tx.GetHash().GetHex(); // the hex-encoded transaction hash (aka the transaction id)
502 fprintf(stdout, "%s\n", strHexHash.c_str());
505 static void OutputTxHex(const CTransaction& tx)
507 string strHex = EncodeHexTx(tx);
509 fprintf(stdout, "%s\n", strHex.c_str());
512 static void OutputTx(const CTransaction& tx)
514 if (GetBoolArg("-json", false))
516 else if (GetBoolArg("-txid", false))
522 static string readStdin()
527 while (!feof(stdin)) {
528 size_t bread = fread(buf, 1, sizeof(buf), stdin);
529 ret.append(buf, bread);
530 if (bread < sizeof(buf))
535 throw runtime_error("error reading stdin");
537 boost::algorithm::trim_right(ret);
542 static int CommandLineRawTx(int argc, char* argv[])
547 // Skip switches; Permit common stdin convention "-"
548 while (argc > 1 && IsSwitchChar(argv[1][0]) &&
554 CTransaction txDecodeTmp;
558 // require at least one param
560 throw runtime_error("too few parameters");
562 // param: hex-encoded bitcoin transaction
563 string strHexTx(argv[1]);
564 if (strHexTx == "-") // "-" implies standard input
565 strHexTx = readStdin();
567 if (!DecodeHexTx(txDecodeTmp, strHexTx))
568 throw runtime_error("invalid transaction encoding");
574 CMutableTransaction tx(txDecodeTmp);
576 for (int i = startArg; i < argc; i++) {
577 string arg = argv[i];
579 size_t eqpos = arg.find('=');
580 if (eqpos == string::npos)
583 key = arg.substr(0, eqpos);
584 value = arg.substr(eqpos + 1);
587 MutateTx(tx, key, value);
593 catch (const boost::thread_interrupted&) {
596 catch (const std::exception& e) {
597 strPrint = string("error: ") + e.what();
601 PrintExceptionContinue(NULL, "CommandLineRawTx()");
605 if (strPrint != "") {
606 fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str());
611 int main(int argc, char* argv[])
616 if(!AppInitRawTx(argc, argv))
619 catch (const std::exception& e) {
620 PrintExceptionContinue(&e, "AppInitRawTx()");
623 PrintExceptionContinue(NULL, "AppInitRawTx()");
627 int ret = EXIT_FAILURE;
629 ret = CommandLineRawTx(argc, argv);
631 catch (const std::exception& e) {
632 PrintExceptionContinue(&e, "CommandLineRawTx()");
634 PrintExceptionContinue(NULL, "CommandLineRawTx()");