]> Git Repo - VerusCoin.git/blob - src/bitcoin-tx.cpp
Merge pull request #6034
[VerusCoin.git] / src / bitcoin-tx.cpp
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.
4
5 #include "base58.h"
6 #include "clientversion.h"
7 #include "coins.h"
8 #include "consensus/consensus.h"
9 #include "core_io.h"
10 #include "keystore.h"
11 #include "primitives/transaction.h"
12 #include "script/script.h"
13 #include "script/sign.h"
14 #include "univalue/univalue.h"
15 #include "util.h"
16 #include "utilmoneystr.h"
17 #include "utilstrencodings.h"
18
19 #include <stdio.h>
20
21 #include <boost/algorithm/string.hpp>
22 #include <boost/assign/list_of.hpp>
23
24 using namespace std;
25
26 static bool fCreateBlank;
27 static map<string,UniValue> registers;
28
29 static bool AppInitRawTx(int argc, char* argv[])
30 {
31     //
32     // Parameters
33     //
34     ParseParameters(argc, argv);
35
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");
39         return false;
40     }
41
42     fCreateBlank = GetBoolArg("-create", false);
43
44     if (argc<2 || mapArgs.count("-?") || mapArgs.count("-help"))
45     {
46         // First part of help message is specific to this utility
47         std::string strUsage = _("Bitcoin Core bitcoin-tx utility version") + " " + FormatFullVersion() + "\n\n" +
48             _("Usage:") + "\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" +
51               "\n";
52
53         fprintf(stdout, "%s", strUsage.c_str());
54
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"));
62
63         fprintf(stdout, "%s", strUsage.c_str());
64
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());
79
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());
84
85         return false;
86     }
87     return true;
88 }
89
90 static void RegisterSetJson(const string& key, const string& rawJson)
91 {
92     UniValue val;
93     if (!val.read(rawJson)) {
94         string strErr = "Cannot parse JSON for key " + key;
95         throw runtime_error(strErr);
96     }
97
98     registers[key] = val;
99 }
100
101 static void RegisterSet(const string& strInput)
102 {
103     // separate NAME:VALUE in string
104     size_t pos = strInput.find(':');
105     if ((pos == string::npos) ||
106         (pos == 0) ||
107         (pos == (strInput.size() - 1)))
108         throw runtime_error("Register input requires NAME:VALUE");
109
110     string key = strInput.substr(0, pos);
111     string valStr = strInput.substr(pos + 1, string::npos);
112
113     RegisterSetJson(key, valStr);
114 }
115
116 static void RegisterLoad(const string& strInput)
117 {
118     // separate NAME:FILENAME in string
119     size_t pos = strInput.find(':');
120     if ((pos == string::npos) ||
121         (pos == 0) ||
122         (pos == (strInput.size() - 1)))
123         throw runtime_error("Register load requires NAME:FILENAME");
124
125     string key = strInput.substr(0, pos);
126     string filename = strInput.substr(pos + 1, string::npos);
127
128     FILE *f = fopen(filename.c_str(), "r");
129     if (!f) {
130         string strErr = "Cannot open file " + filename;
131         throw runtime_error(strErr);
132     }
133
134     // load file chunks into one big buffer
135     string valStr;
136     while ((!feof(f)) && (!ferror(f))) {
137         char buf[4096];
138         int bread = fread(buf, 1, sizeof(buf), f);
139         if (bread <= 0)
140             break;
141
142         valStr.insert(valStr.size(), buf, bread);
143     }
144
145     if (ferror(f)) {
146         string strErr = "Error reading file " + filename;
147         throw runtime_error(strErr);
148     }
149
150     fclose(f);
151
152     // evaluate as JSON buffer register
153     RegisterSetJson(key, valStr);
154 }
155
156 static void MutateTxVersion(CMutableTransaction& tx, const string& cmdVal)
157 {
158     int64_t newVersion = atoi64(cmdVal);
159     if (newVersion < 1 || newVersion > CTransaction::CURRENT_VERSION)
160         throw runtime_error("Invalid TX version requested");
161
162     tx.nVersion = (int) newVersion;
163 }
164
165 static void MutateTxLocktime(CMutableTransaction& tx, const string& cmdVal)
166 {
167     int64_t newLocktime = atoi64(cmdVal);
168     if (newLocktime < 0LL || newLocktime > 0xffffffffLL)
169         throw runtime_error("Invalid TX locktime requested");
170
171     tx.nLockTime = (unsigned int) newLocktime;
172 }
173
174 static void MutateTxAddInput(CMutableTransaction& tx, const string& strInput)
175 {
176     // separate TXID:VOUT in string
177     size_t pos = strInput.find(':');
178     if ((pos == string::npos) ||
179         (pos == 0) ||
180         (pos == (strInput.size() - 1)))
181         throw runtime_error("TX input missing separator");
182
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));
188
189     static const unsigned int minTxOutSz = 9;
190     static const unsigned int maxVout = MAX_BLOCK_SIZE / minTxOutSz;
191
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");
197
198     // append to transaction input list
199     CTxIn txin(txid, vout);
200     tx.vin.push_back(txin);
201 }
202
203 static void MutateTxAddOutAddr(CMutableTransaction& tx, const string& strInput)
204 {
205     // separate VALUE:ADDRESS in string
206     size_t pos = strInput.find(':');
207     if ((pos == string::npos) ||
208         (pos == 0) ||
209         (pos == (strInput.size() - 1)))
210         throw runtime_error("TX output missing separator");
211
212     // extract and validate VALUE
213     string strValue = strInput.substr(0, pos);
214     CAmount value;
215     if (!ParseMoney(strValue, value))
216         throw runtime_error("invalid TX output value");
217
218     // extract and validate ADDRESS
219     string strAddr = strInput.substr(pos + 1, string::npos);
220     CBitcoinAddress addr(strAddr);
221     if (!addr.IsValid())
222         throw runtime_error("invalid TX output address");
223
224     // build standard output script via GetScriptForDestination()
225     CScript scriptPubKey = GetScriptForDestination(addr.Get());
226
227     // construct TxOut, append to transaction output list
228     CTxOut txout(value, scriptPubKey);
229     tx.vout.push_back(txout);
230 }
231
232 static void MutateTxAddOutScript(CMutableTransaction& tx, const string& strInput)
233 {
234     // separate VALUE:SCRIPT in string
235     size_t pos = strInput.find(':');
236     if ((pos == string::npos) ||
237         (pos == 0))
238         throw runtime_error("TX output missing separator");
239
240     // extract and validate VALUE
241     string strValue = strInput.substr(0, pos);
242     CAmount value;
243     if (!ParseMoney(strValue, value))
244         throw runtime_error("invalid TX output value");
245
246     // extract and validate script
247     string strScript = strInput.substr(pos + 1, string::npos);
248     CScript scriptPubKey = ParseScript(strScript); // throws on err
249
250     // construct TxOut, append to transaction output list
251     CTxOut txout(value, scriptPubKey);
252     tx.vout.push_back(txout);
253 }
254
255 static void MutateTxDelInput(CMutableTransaction& tx, const string& strInIdx)
256 {
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());
262     }
263
264     // delete input from transaction
265     tx.vin.erase(tx.vin.begin() + inIdx);
266 }
267
268 static void MutateTxDelOutput(CMutableTransaction& tx, const string& strOutIdx)
269 {
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());
275     }
276
277     // delete output from transaction
278     tx.vout.erase(tx.vout.begin() + outIdx);
279 }
280
281 static const unsigned int N_SIGHASH_OPTS = 6;
282 static const struct {
283     const char *flagStr;
284     int flags;
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},
292 };
293
294 static bool findSighashFlags(int& flags, const string& flagStr)
295 {
296     flags = 0;
297
298     for (unsigned int i = 0; i < N_SIGHASH_OPTS; i++) {
299         if (flagStr == sighashOptions[i].flagStr) {
300             flags = sighashOptions[i].flags;
301             return true;
302         }
303     }
304
305     return false;
306 }
307
308 uint256 ParseHashUO(map<string,UniValue>& o, string strKey)
309 {
310     if (!o.count(strKey))
311         return uint256();
312     return ParseHashUV(o[strKey], strKey);
313 }
314
315 vector<unsigned char> ParseHexUO(map<string,UniValue>& o, string strKey)
316 {
317     if (!o.count(strKey)) {
318         vector<unsigned char> emptyVec;
319         return emptyVec;
320     }
321     return ParseHexUV(o[strKey], strKey);
322 }
323
324 static void MutateTxSign(CMutableTransaction& tx, const string& flagStr)
325 {
326     int nHashType = SIGHASH_ALL;
327
328     if (flagStr.size() > 0)
329         if (!findSighashFlags(nHashType, flagStr))
330             throw runtime_error("unknown sighash flag/sign option");
331
332     vector<CTransaction> txVariants;
333     txVariants.push_back(tx);
334
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);
341
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"];
347     fGivenKeys = true;
348
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());
354         if (!fGood)
355             throw runtime_error("privatekey not valid");
356
357         CKey key = vchSecret.GetKey();
358         tempKeystore.AddKey(key);
359     }
360
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"];
365     {
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");
370
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");
374
375             uint256 txid = ParseHashUV(prevOut["txid"], "txid");
376
377             int nOut = atoi(prevOut["vout"].getValStr());
378             if (nOut < 0)
379                 throw runtime_error("vout must be positive");
380
381             vector<unsigned char> pkData(ParseHexUV(prevOut["scriptPubKey"], "scriptPubKey"));
382             CScript scriptPubKey(pkData.begin(), pkData.end());
383
384             {
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);
391                 }
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
396             }
397
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);
406             }
407         }
408     }
409
410     const CKeyStore& keystore = tempKeystore;
411
412     bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
413
414     // Sign what we can:
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)) {
419             fComplete = false;
420             continue;
421         }
422         const CScript& prevPubKey = coins->vout[txin.prevout.n].scriptPubKey;
423
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);
428
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);
432         }
433         if (!VerifyScript(txin.scriptSig, prevPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker(&mergedTx, i)))
434             fComplete = false;
435     }
436
437     if (fComplete) {
438         // do nothing... for now
439         // perhaps store this for later optional JSON output
440     }
441
442     tx = mergedTx;
443 }
444
445 class Secp256k1Init
446 {
447 public:
448     Secp256k1Init() { ECC_Start(); }
449     ~Secp256k1Init() { ECC_Stop(); }
450 };
451
452 static void MutateTx(CMutableTransaction& tx, const string& command,
453                      const string& commandVal)
454 {
455     boost::scoped_ptr<Secp256k1Init> ecc;
456
457     if (command == "nversion")
458         MutateTxVersion(tx, commandVal);
459     else if (command == "locktime")
460         MutateTxLocktime(tx, commandVal);
461
462     else if (command == "delin")
463         MutateTxDelInput(tx, commandVal);
464     else if (command == "in")
465         MutateTxAddInput(tx, commandVal);
466
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);
473
474     else if (command == "sign") {
475         if (!ecc) { ecc.reset(new Secp256k1Init()); }
476         MutateTxSign(tx, commandVal);
477     }
478
479     else if (command == "load")
480         RegisterLoad(commandVal);
481
482     else if (command == "set")
483         RegisterSet(commandVal);
484
485     else
486         throw runtime_error("unknown command");
487 }
488
489 static void OutputTxJSON(const CTransaction& tx)
490 {
491     UniValue entry(UniValue::VOBJ);
492     TxToUniv(tx, uint256(), entry);
493
494     string jsonOutput = entry.write(4);
495     fprintf(stdout, "%s\n", jsonOutput.c_str());
496 }
497
498 static void OutputTxHash(const CTransaction& tx)
499 {
500     string strHexHash = tx.GetHash().GetHex(); // the hex-encoded transaction hash (aka the transaction id)
501
502     fprintf(stdout, "%s\n", strHexHash.c_str());
503 }
504
505 static void OutputTxHex(const CTransaction& tx)
506 {
507     string strHex = EncodeHexTx(tx);
508
509     fprintf(stdout, "%s\n", strHex.c_str());
510 }
511
512 static void OutputTx(const CTransaction& tx)
513 {
514     if (GetBoolArg("-json", false))
515         OutputTxJSON(tx);
516     else if (GetBoolArg("-txid", false))
517         OutputTxHash(tx);
518     else
519         OutputTxHex(tx);
520 }
521
522 static string readStdin()
523 {
524     char buf[4096];
525     string ret;
526
527     while (!feof(stdin)) {
528         size_t bread = fread(buf, 1, sizeof(buf), stdin);
529         ret.append(buf, bread);
530         if (bread < sizeof(buf))
531             break;
532     }
533
534     if (ferror(stdin))
535         throw runtime_error("error reading stdin");
536
537     boost::algorithm::trim_right(ret);
538
539     return ret;
540 }
541
542 static int CommandLineRawTx(int argc, char* argv[])
543 {
544     string strPrint;
545     int nRet = 0;
546     try {
547         // Skip switches; Permit common stdin convention "-"
548         while (argc > 1 && IsSwitchChar(argv[1][0]) &&
549                (argv[1][1] != 0)) {
550             argc--;
551             argv++;
552         }
553
554         CTransaction txDecodeTmp;
555         int startArg;
556
557         if (!fCreateBlank) {
558             // require at least one param
559             if (argc < 2)
560                 throw runtime_error("too few parameters");
561
562             // param: hex-encoded bitcoin transaction
563             string strHexTx(argv[1]);
564             if (strHexTx == "-")                 // "-" implies standard input
565                 strHexTx = readStdin();
566
567             if (!DecodeHexTx(txDecodeTmp, strHexTx))
568                 throw runtime_error("invalid transaction encoding");
569
570             startArg = 2;
571         } else
572             startArg = 1;
573
574         CMutableTransaction tx(txDecodeTmp);
575
576         for (int i = startArg; i < argc; i++) {
577             string arg = argv[i];
578             string key, value;
579             size_t eqpos = arg.find('=');
580             if (eqpos == string::npos)
581                 key = arg;
582             else {
583                 key = arg.substr(0, eqpos);
584                 value = arg.substr(eqpos + 1);
585             }
586
587             MutateTx(tx, key, value);
588         }
589
590         OutputTx(tx);
591     }
592
593     catch (const boost::thread_interrupted&) {
594         throw;
595     }
596     catch (const std::exception& e) {
597         strPrint = string("error: ") + e.what();
598         nRet = EXIT_FAILURE;
599     }
600     catch (...) {
601         PrintExceptionContinue(NULL, "CommandLineRawTx()");
602         throw;
603     }
604
605     if (strPrint != "") {
606         fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str());
607     }
608     return nRet;
609 }
610
611 int main(int argc, char* argv[])
612 {
613     SetupEnvironment();
614
615     try {
616         if(!AppInitRawTx(argc, argv))
617             return EXIT_FAILURE;
618     }
619     catch (const std::exception& e) {
620         PrintExceptionContinue(&e, "AppInitRawTx()");
621         return EXIT_FAILURE;
622     } catch (...) {
623         PrintExceptionContinue(NULL, "AppInitRawTx()");
624         return EXIT_FAILURE;
625     }
626
627     int ret = EXIT_FAILURE;
628     try {
629         ret = CommandLineRawTx(argc, argv);
630     }
631     catch (const std::exception& e) {
632         PrintExceptionContinue(&e, "CommandLineRawTx()");
633     } catch (...) {
634         PrintExceptionContinue(NULL, "CommandLineRawTx()");
635     }
636     return ret;
637 }
This page took 0.065055 seconds and 4 git commands to generate.