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"
16 #include "utilmoneystr.h"
17 #include "utilstrencodings.h"
21 #include <boost/algorithm/string.hpp>
22 #include <boost/assign/list_of.hpp>
27 #include "arith_uint256.h"
28 #include "komodo_structs.h"
29 #include "komodo_globals.h"
30 #include "komodo_defs.h"
32 #include "komodo_interest.h"
34 uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 hash,int32_t n,int32_t checkheight,uint64_t checkvalue)
39 static bool fCreateBlank;
40 static map<string,UniValue> registers;
42 static bool AppInitRawTx(int argc, char* argv[])
47 ParseParameters(argc, argv);
49 // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
50 if (!SelectParamsFromCommandLine()) {
51 fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
55 fCreateBlank = GetBoolArg("-create", false);
57 if (argc<2 || mapArgs.count("-?") || mapArgs.count("-h") || mapArgs.count("-help"))
59 // First part of help message is specific to this utility
60 std::string strUsage = _("Zcash zcash-tx utility version") + " " + FormatFullVersion() + "\n\n" +
62 " zcash-tx [options] <hex-tx> [commands] " + _("Update hex-encoded zcash transaction") + "\n" +
63 " zcash-tx [options] -create [commands] " + _("Create hex-encoded zcash transaction") + "\n" +
66 fprintf(stdout, "%s", strUsage.c_str());
68 strUsage = HelpMessageGroup(_("Options:"));
69 strUsage += HelpMessageOpt("-?", _("This help message"));
70 strUsage += HelpMessageOpt("-create", _("Create new, empty TX."));
71 strUsage += HelpMessageOpt("-json", _("Select JSON output"));
72 strUsage += HelpMessageOpt("-txid", _("Output only the hex-encoded transaction id of the resultant transaction."));
73 strUsage += HelpMessageOpt("-regtest", _("Enter regression test mode, which uses a special chain in which blocks can be solved instantly."));
74 strUsage += HelpMessageOpt("-testnet", _("Use the test network"));
76 fprintf(stdout, "%s", strUsage.c_str());
78 strUsage = HelpMessageGroup(_("Commands:"));
79 strUsage += HelpMessageOpt("delin=N", _("Delete input N from TX"));
80 strUsage += HelpMessageOpt("delout=N", _("Delete output N from TX"));
81 strUsage += HelpMessageOpt("in=TXID:VOUT", _("Add input to TX"));
82 strUsage += HelpMessageOpt("locktime=N", _("Set TX lock time to N"));
83 strUsage += HelpMessageOpt("nversion=N", _("Set TX version to N"));
84 strUsage += HelpMessageOpt("outaddr=VALUE:ADDRESS", _("Add address-based output to TX"));
85 strUsage += HelpMessageOpt("outscript=VALUE:SCRIPT", _("Add raw script output to TX"));
86 strUsage += HelpMessageOpt("sign=SIGHASH-FLAGS", _("Add zero or more signatures to transaction") + ". " +
87 _("This command requires JSON registers:") +
88 _("prevtxs=JSON object") + ", " +
89 _("privatekeys=JSON object") + ". " +
90 _("See signrawtransaction docs for format of sighash flags, JSON objects."));
91 fprintf(stdout, "%s", strUsage.c_str());
93 strUsage = HelpMessageGroup(_("Register Commands:"));
94 strUsage += HelpMessageOpt("load=NAME:FILENAME", _("Load JSON file FILENAME into register NAME"));
95 strUsage += HelpMessageOpt("set=NAME:JSON-STRING", _("Set register NAME to given JSON-STRING"));
96 fprintf(stdout, "%s", strUsage.c_str());
103 static void RegisterSetJson(const string& key, const string& rawJson)
106 if (!val.read(rawJson)) {
107 string strErr = "Cannot parse JSON for key " + key;
108 throw runtime_error(strErr);
111 registers[key] = val;
114 static void RegisterSet(const string& strInput)
116 // separate NAME:VALUE in string
117 size_t pos = strInput.find(':');
118 if ((pos == string::npos) ||
120 (pos == (strInput.size() - 1)))
121 throw runtime_error("Register input requires NAME:VALUE");
123 string key = strInput.substr(0, pos);
124 string valStr = strInput.substr(pos + 1, string::npos);
126 RegisterSetJson(key, valStr);
129 static void RegisterLoad(const string& strInput)
131 // separate NAME:FILENAME in string
132 size_t pos = strInput.find(':');
133 if ((pos == string::npos) ||
135 (pos == (strInput.size() - 1)))
136 throw runtime_error("Register load requires NAME:FILENAME");
138 string key = strInput.substr(0, pos);
139 string filename = strInput.substr(pos + 1, string::npos);
141 FILE *f = fopen(filename.c_str(), "r");
143 string strErr = "Cannot open file " + filename;
144 throw runtime_error(strErr);
147 // load file chunks into one big buffer
149 while ((!feof(f)) && (!ferror(f))) {
151 int bread = fread(buf, 1, sizeof(buf), f);
155 valStr.insert(valStr.size(), buf, bread);
158 int error = ferror(f);
162 string strErr = "Error reading file " + filename;
163 throw runtime_error(strErr);
166 // evaluate as JSON buffer register
167 RegisterSetJson(key, valStr);
170 static void MutateTxVersion(CMutableTransaction& tx, const string& cmdVal)
172 int64_t newVersion = atoi64(cmdVal);
173 if (newVersion < CTransaction::MIN_CURRENT_VERSION || newVersion > CTransaction::MAX_CURRENT_VERSION)
174 throw runtime_error("Invalid TX version requested");
176 tx.nVersion = (int) newVersion;
179 static void MutateTxLocktime(CMutableTransaction& tx, const string& cmdVal)
181 int64_t newLocktime = atoi64(cmdVal);
182 if (newLocktime < 0LL || newLocktime > 0xffffffffLL)
183 throw runtime_error("Invalid TX locktime requested");
185 tx.nLockTime = (unsigned int) newLocktime;
188 static void MutateTxAddInput(CMutableTransaction& tx, const string& strInput)
190 // separate TXID:VOUT in string
191 size_t pos = strInput.find(':');
192 if ((pos == string::npos) ||
194 (pos == (strInput.size() - 1)))
195 throw runtime_error("TX input missing separator");
197 // extract and validate TXID
198 string strTxid = strInput.substr(0, pos);
199 if ((strTxid.size() != 64) || !IsHex(strTxid))
200 throw runtime_error("invalid TX input txid");
201 uint256 txid(uint256S(strTxid));
203 static const unsigned int minTxOutSz = 9;
204 static const unsigned int maxVout = MAX_BLOCK_SIZE / minTxOutSz;
206 // extract and validate vout
207 string strVout = strInput.substr(pos + 1, string::npos);
208 int vout = atoi(strVout);
209 if ((vout < 0) || (vout > (int)maxVout))
210 throw runtime_error("invalid TX input vout");
212 // append to transaction input list
213 CTxIn txin(txid, vout);
214 tx.vin.push_back(txin);
217 static void MutateTxAddOutAddr(CMutableTransaction& tx, const string& strInput)
219 // separate VALUE:ADDRESS in string
220 size_t pos = strInput.find(':');
221 if ((pos == string::npos) ||
223 (pos == (strInput.size() - 1)))
224 throw runtime_error("TX output missing separator");
226 // extract and validate VALUE
227 string strValue = strInput.substr(0, pos);
229 if (!ParseMoney(strValue, value))
230 throw runtime_error("invalid TX output value");
232 // extract and validate ADDRESS
233 string strAddr = strInput.substr(pos + 1, string::npos);
234 CBitcoinAddress addr(strAddr);
236 throw runtime_error("invalid TX output address");
238 // build standard output script via GetScriptForDestination()
239 CScript scriptPubKey = GetScriptForDestination(addr.Get());
241 // construct TxOut, append to transaction output list
242 CTxOut txout(value, scriptPubKey);
243 tx.vout.push_back(txout);
246 static void MutateTxAddOutScript(CMutableTransaction& tx, const string& strInput)
248 // separate VALUE:SCRIPT in string
249 size_t pos = strInput.find(':');
250 if ((pos == string::npos) ||
252 throw runtime_error("TX output missing separator");
254 // extract and validate VALUE
255 string strValue = strInput.substr(0, pos);
257 if (!ParseMoney(strValue, value))
258 throw runtime_error("invalid TX output value");
260 // extract and validate script
261 string strScript = strInput.substr(pos + 1, string::npos);
262 CScript scriptPubKey = ParseScript(strScript); // throws on err
264 // construct TxOut, append to transaction output list
265 CTxOut txout(value, scriptPubKey);
266 tx.vout.push_back(txout);
269 static void MutateTxDelInput(CMutableTransaction& tx, const string& strInIdx)
271 // parse requested deletion index
272 int inIdx = atoi(strInIdx);
273 if (inIdx < 0 || inIdx >= (int)tx.vin.size()) {
274 string strErr = "Invalid TX input index '" + strInIdx + "'";
275 throw runtime_error(strErr.c_str());
278 // delete input from transaction
279 tx.vin.erase(tx.vin.begin() + inIdx);
282 static void MutateTxDelOutput(CMutableTransaction& tx, const string& strOutIdx)
284 // parse requested deletion index
285 int outIdx = atoi(strOutIdx);
286 if (outIdx < 0 || outIdx >= (int)tx.vout.size()) {
287 string strErr = "Invalid TX output index '" + strOutIdx + "'";
288 throw runtime_error(strErr.c_str());
291 // delete output from transaction
292 tx.vout.erase(tx.vout.begin() + outIdx);
295 static const unsigned int N_SIGHASH_OPTS = 6;
296 static const struct {
299 } sighashOptions[N_SIGHASH_OPTS] = {
300 {"ALL", SIGHASH_ALL},
301 {"NONE", SIGHASH_NONE},
302 {"SINGLE", SIGHASH_SINGLE},
303 {"ALL|ANYONECANPAY", SIGHASH_ALL|SIGHASH_ANYONECANPAY},
304 {"NONE|ANYONECANPAY", SIGHASH_NONE|SIGHASH_ANYONECANPAY},
305 {"SINGLE|ANYONECANPAY", SIGHASH_SINGLE|SIGHASH_ANYONECANPAY},
308 static bool findSighashFlags(int& flags, const string& flagStr)
312 for (unsigned int i = 0; i < N_SIGHASH_OPTS; i++) {
313 if (flagStr == sighashOptions[i].flagStr) {
314 flags = sighashOptions[i].flags;
322 uint256 ParseHashUO(map<string,UniValue>& o, string strKey)
324 if (!o.count(strKey))
326 return ParseHashUV(o[strKey], strKey);
329 vector<unsigned char> ParseHexUO(map<string,UniValue>& o, string strKey)
331 if (!o.count(strKey)) {
332 vector<unsigned char> emptyVec;
335 return ParseHexUV(o[strKey], strKey);
339 static void MutateTxSign(CMutableTransaction& tx, const string& flagStr)
341 int nHashType = SIGHASH_ALL;
343 if (flagStr.size() > 0)
344 if (!findSighashFlags(nHashType, flagStr))
345 throw runtime_error("unknown sighash flag/sign option");
347 vector<CTransaction> txVariants;
348 txVariants.push_back(tx);
350 // mergedTx will end up with all the signatures; it
351 // starts as a clone of the raw tx:
352 CMutableTransaction mergedTx(txVariants[0]);
353 bool fComplete = true;
354 CCoinsView viewDummy;
355 CCoinsViewCache view(&viewDummy);
357 if (!registers.count("privatekeys"))
358 throw runtime_error("privatekeys register variable must be set.");
359 bool fGivenKeys = false;
360 CBasicKeyStore tempKeystore;
361 UniValue keysObj = registers["privatekeys"];
364 for (size_t kidx = 0; kidx < keysObj.size(); kidx++) {
365 if (!keysObj[kidx].isStr())
366 throw runtime_error("privatekey not a string");
367 CBitcoinSecret vchSecret;
368 bool fGood = vchSecret.SetString(keysObj[kidx].getValStr());
370 throw runtime_error("privatekey not valid");
372 CKey key = vchSecret.GetKey();
373 tempKeystore.AddKey(key);
376 // Add previous txouts given in the RPC call:
377 if (!registers.count("prevtxs"))
378 throw runtime_error("prevtxs register variable must be set.");
379 UniValue prevtxsObj = registers["prevtxs"];
381 for (size_t previdx = 0; previdx < prevtxsObj.size(); previdx++) {
382 UniValue prevOut = prevtxsObj[previdx];
383 if (!prevOut.isObject())
384 throw runtime_error("expected prevtxs internal object");
386 map<string,UniValue::VType> types = boost::assign::map_list_of("txid", UniValue::VSTR)("vout",UniValue::VNUM)("scriptPubKey",UniValue::VSTR);
387 if (!prevOut.checkObject(types))
388 throw runtime_error("prevtxs internal object typecheck fail");
390 uint256 txid = ParseHashUV(prevOut["txid"], "txid");
392 int nOut = atoi(prevOut["vout"].getValStr());
394 throw runtime_error("vout must be positive");
396 vector<unsigned char> pkData(ParseHexUV(prevOut["scriptPubKey"], "scriptPubKey"));
397 CScript scriptPubKey(pkData.begin(), pkData.end());
400 CCoinsModifier coins = view.ModifyCoins(txid);
401 if (coins->IsAvailable(nOut) && coins->vout[nOut].scriptPubKey != scriptPubKey) {
402 string err("Previous output scriptPubKey mismatch:\n");
403 err = err + coins->vout[nOut].scriptPubKey.ToString() + "\nvs:\n"+
404 scriptPubKey.ToString();
405 throw runtime_error(err);
407 if ((unsigned int)nOut >= coins->vout.size())
408 coins->vout.resize(nOut+1);
409 coins->vout[nOut].scriptPubKey = scriptPubKey;
410 coins->vout[nOut].nValue = 0; // we don't know the actual output value
413 // if redeemScript given and private keys given,
414 // add redeemScript to the tempKeystore so it can be signed:
415 if (fGivenKeys && scriptPubKey.IsPayToScriptHash() &&
416 prevOut.exists("redeemScript")) {
417 UniValue v = prevOut["redeemScript"];
418 vector<unsigned char> rsData(ParseHexUV(v, "redeemScript"));
419 CScript redeemScript(rsData.begin(), rsData.end());
420 tempKeystore.AddCScript(redeemScript);
425 const CKeyStore& keystore = tempKeystore;
427 bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
430 for (unsigned int i = 0; i < mergedTx.vin.size(); i++) {
431 CTxIn& txin = mergedTx.vin[i];
432 const CCoins* coins = view.AccessCoins(txin.prevout.hash);
433 if (!coins || !coins->IsAvailable(txin.prevout.n)) {
437 const CScript& prevPubKey = coins->vout[txin.prevout.n].scriptPubKey;
439 txin.scriptSig.clear();
440 // Only sign SIGHASH_SINGLE if there's a corresponding output:
441 if (!fHashSingle || (i < mergedTx.vout.size()))
442 SignSignature(keystore, prevPubKey, mergedTx, i, nHashType);
444 // ... and merge in other signatures:
445 BOOST_FOREACH(const CTransaction& txv, txVariants) {
446 txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig);
448 if (!VerifyScript(txin.scriptSig, prevPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker(&mergedTx, i)))
453 // do nothing... for now
454 // perhaps store this for later optional JSON output
463 Secp256k1Init() { ECC_Start(); }
464 ~Secp256k1Init() { ECC_Stop(); }
467 static void MutateTx(CMutableTransaction& tx, const string& command,
468 const string& commandVal)
470 boost::scoped_ptr<Secp256k1Init> ecc;
472 if (command == "nversion")
473 MutateTxVersion(tx, commandVal);
474 else if (command == "locktime")
475 MutateTxLocktime(tx, commandVal);
477 else if (command == "delin")
478 MutateTxDelInput(tx, commandVal);
479 else if (command == "in")
480 MutateTxAddInput(tx, commandVal);
482 else if (command == "delout")
483 MutateTxDelOutput(tx, commandVal);
484 else if (command == "outaddr")
485 MutateTxAddOutAddr(tx, commandVal);
486 else if (command == "outscript")
487 MutateTxAddOutScript(tx, commandVal);
489 else if (command == "sign") {
490 if (!ecc) { ecc.reset(new Secp256k1Init()); }
491 MutateTxSign(tx, commandVal);
494 else if (command == "load")
495 RegisterLoad(commandVal);
497 else if (command == "set")
498 RegisterSet(commandVal);
501 throw runtime_error("unknown command");
504 static void OutputTxJSON(const CTransaction& tx)
506 UniValue entry(UniValue::VOBJ);
507 TxToUniv(tx, uint256(), entry);
509 string jsonOutput = entry.write(4);
510 fprintf(stdout, "%s\n", jsonOutput.c_str());
513 static void OutputTxHash(const CTransaction& tx)
515 string strHexHash = tx.GetHash().GetHex(); // the hex-encoded transaction hash (aka the transaction id)
517 fprintf(stdout, "%s\n", strHexHash.c_str());
520 static void OutputTxHex(const CTransaction& tx)
522 string strHex = EncodeHexTx(tx);
524 fprintf(stdout, "%s\n", strHex.c_str());
527 static void OutputTx(const CTransaction& tx)
529 if (GetBoolArg("-json", false))
531 else if (GetBoolArg("-txid", false))
537 static string readStdin()
542 while (!feof(stdin)) {
543 size_t bread = fread(buf, 1, sizeof(buf), stdin);
544 ret.append(buf, bread);
545 if (bread < sizeof(buf))
550 throw runtime_error("error reading stdin");
552 boost::algorithm::trim_right(ret);
557 static int CommandLineRawTx(int argc, char* argv[])
562 // Skip switches; Permit common stdin convention "-"
563 while (argc > 1 && IsSwitchChar(argv[1][0]) &&
569 CTransaction txDecodeTmp;
573 // require at least one param
575 throw runtime_error("too few parameters");
577 // param: hex-encoded bitcoin transaction
578 string strHexTx(argv[1]);
579 if (strHexTx == "-") // "-" implies standard input
580 strHexTx = readStdin();
582 if (!DecodeHexTx(txDecodeTmp, strHexTx))
583 throw runtime_error("invalid transaction encoding");
589 CMutableTransaction tx(txDecodeTmp);
591 for (int i = startArg; i < argc; i++) {
592 string arg = argv[i];
594 size_t eqpos = arg.find('=');
595 if (eqpos == string::npos)
598 key = arg.substr(0, eqpos);
599 value = arg.substr(eqpos + 1);
602 MutateTx(tx, key, value);
608 catch (const boost::thread_interrupted&) {
611 catch (const std::exception& e) {
612 strPrint = string("error: ") + e.what();
616 PrintExceptionContinue(NULL, "CommandLineRawTx()");
620 if (strPrint != "") {
621 fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str());
626 int main(int argc, char* argv[])
631 if(!AppInitRawTx(argc, argv))
634 catch (const std::exception& e) {
635 PrintExceptionContinue(&e, "AppInitRawTx()");
638 PrintExceptionContinue(NULL, "AppInitRawTx()");
642 int ret = EXIT_FAILURE;
644 ret = CommandLineRawTx(argc, argv);
646 catch (const std::exception& e) {
647 PrintExceptionContinue(&e, "CommandLineRawTx()");
649 PrintExceptionContinue(NULL, "CommandLineRawTx()");