5#if defined(HAVE_CONFIG_H)
6#include <config/bitcoin-config.h>
60 "Output only the hex-encoded transaction id of the resultant "
67 argsman.
AddArg(
"delout=N",
"Delete output N from TX",
69 argsman.
AddArg(
"in=TXID:VOUT(:SEQUENCE_NUMBER)",
"Add input to TX",
71 argsman.
AddArg(
"locktime=N",
"Set TX lock time to N",
75 argsman.
AddArg(
"outaddr=VALUE:ADDRESS",
"Add address-based output to TX",
77 argsman.
AddArg(
"outpubkey=VALUE:PUBKEY[:FLAGS]",
78 "Add pay-to-pubkey output to TX. "
79 "Optionally add the \"S\" flag to wrap the output in a "
80 "pay-to-script-hash.",
82 argsman.
AddArg(
"outdata=[VALUE:]DATA",
"Add data-based output to TX",
84 argsman.
AddArg(
"outscript=VALUE:SCRIPT[:FLAGS]",
85 "Add raw script output to TX. "
86 "Optionally add the \"S\" flag to wrap the output in a "
87 "pay-to-script-hash.",
90 "outmultisig=VALUE:REQUIRED:PUBKEYS:PUBKEY1:PUBKEY2:....[:FLAGS]",
91 "Add Pay To n-of-m Multi-sig output to TX. n = REQUIRED, m = PUBKEYS. "
92 "Optionally add the \"S\" flag to wrap the output in a "
93 "pay-to-script-hash.",
95 argsman.
AddArg(
"sign=SIGHASH-FLAGS",
96 "Add zero or more signatures to transaction. "
97 "This command requires JSON registers:"
98 "prevtxs=JSON object, "
99 "privatekeys=JSON object. "
100 "See signrawtransactionwithkey docs for format of sighash "
101 "flags, JSON objects.",
104 argsman.
AddArg(
"load=NAME:FILENAME",
105 "Load JSON file FILENAME into register NAME",
107 argsman.
AddArg(
"set=NAME:JSON-STRING",
108 "Set register NAME to given JSON-STRING",
123 tfm::format(std::cerr,
"Error parsing command line arguments: %s\n",
132 }
catch (
const std::exception &e) {
141 std::string strUsage = PACKAGE_NAME
" bitcoin-tx utility version " +
149 "Usage: bitcoin-tx [options] <hex-tx> [commands] Update "
150 "hex-encoded bitcoin transaction\n"
151 "or: bitcoin-tx [options] -create [commands] Create "
152 "hex-encoded bitcoin transaction\n"
160 tfm::format(std::cerr,
"Error: too few parameters\n");
171 const std::string &rawJson) {
173 if (!val.
read(rawJson)) {
174 std::string strErr =
"Cannot parse JSON for key " + key;
175 throw std::runtime_error(strErr);
183 size_t pos = strInput.find(
':');
184 if ((pos == std::string::npos) || (pos == 0) ||
185 (pos == (strInput.size() - 1))) {
186 throw std::runtime_error(
"Register input requires NAME:VALUE");
189 std::string key = strInput.substr(0, pos);
190 std::string valStr = strInput.substr(pos + 1, std::string::npos);
197 size_t pos = strInput.find(
':');
198 if ((pos == std::string::npos) || (pos == 0) ||
199 (pos == (strInput.size() - 1))) {
200 throw std::runtime_error(
"Register load requires NAME:FILENAME");
203 std::string key = strInput.substr(0, pos);
204 std::string filename = strInput.substr(pos + 1, std::string::npos);
208 std::string strErr =
"Cannot open file " + filename;
209 throw std::runtime_error(strErr);
214 while ((!feof(f)) && (!ferror(f))) {
216 int bread = fread(buf, 1,
sizeof(buf), f);
221 valStr.insert(valStr.size(), buf, bread);
224 int error = ferror(f);
228 std::string strErr =
"Error reading file " + filename;
229 throw std::runtime_error(strErr);
239 throw std::runtime_error(
"invalid TX output value");
246 const std::string &cmdVal) {
251 throw std::runtime_error(
"Invalid TX version requested: '" + cmdVal +
259 const std::string &cmdVal) {
261 if (!
ParseInt64(cmdVal, &newLocktime) || newLocktime < 0LL ||
262 newLocktime > 0xffffffffLL) {
263 throw std::runtime_error(
"Invalid TX locktime requested: '" + cmdVal +
267 tx.
nLockTime = (
unsigned int)newLocktime;
271 const std::string &strInput) {
272 std::vector<std::string> vStrInputParts =
SplitString(strInput,
':');
275 if (vStrInputParts.size() < 2) {
276 throw std::runtime_error(
"TX input missing separator");
282 throw std::runtime_error(
"invalid TX input txid");
287 static const unsigned int minTxOutSz = 9;
288 static const unsigned int maxVout =
MAX_TX_SIZE / minTxOutSz;
291 const std::string &strVout = vStrInputParts[1];
293 if (!
ParseInt64(strVout, &vout) || vout < 0 ||
294 vout >
static_cast<int64_t
>(maxVout)) {
295 throw std::runtime_error(
"invalid TX input vout '" + strVout +
"'");
299 uint32_t nSequenceIn = std::numeric_limits<unsigned int>::max();
300 if (vStrInputParts.size() > 2) {
301 nSequenceIn = std::stoul(vStrInputParts[2]);
305 CTxIn txin(txid, vout, CScript(), nSequenceIn);
306 tx.
vin.push_back(txin);
310 const std::string &strInput,
313 std::vector<std::string> vStrInputParts =
SplitString(strInput,
':');
315 if (vStrInputParts.size() != 2) {
316 throw std::runtime_error(
"TX output missing or too many separators");
323 const std::string &strAddr = vStrInputParts[1];
326 throw std::runtime_error(
"invalid TX output address");
331 CTxOut txout(value, scriptPubKey);
332 tx.
vout.push_back(txout);
336 const std::string &strInput) {
338 std::vector<std::string> vStrInputParts =
SplitString(strInput,
':');
340 if (vStrInputParts.size() < 2 || vStrInputParts.size() > 3) {
341 throw std::runtime_error(
"TX output missing or too many separators");
350 throw std::runtime_error(
"invalid TX output pubkey");
356 bool bScriptHash =
false;
357 if (vStrInputParts.size() == 3) {
358 const std::string &
flags = vStrInputParts[2];
359 bScriptHash = (
flags.find(
'S') != std::string::npos);
369 CTxOut txout(value, scriptPubKey);
370 tx.
vout.push_back(txout);
374 const std::string &strInput) {
376 std::vector<std::string> vStrInputParts =
SplitString(strInput,
':');
379 if (vStrInputParts.size() < 3) {
380 throw std::runtime_error(
"Not enough multisig parameters");
387 uint32_t required = stoul(vStrInputParts[1]);
390 uint32_t numkeys = stoul(vStrInputParts[2]);
393 if (vStrInputParts.size() < numkeys + 3) {
394 throw std::runtime_error(
"incorrect number of multisig pubkeys");
399 throw std::runtime_error(
"multisig parameter mismatch. Required " +
405 std::vector<CPubKey> pubkeys;
406 for (
int pos = 1; pos <= int(numkeys); pos++) {
409 throw std::runtime_error(
"invalid TX output pubkey");
412 pubkeys.push_back(pubkey);
416 bool bScriptHash =
false;
417 if (vStrInputParts.size() == numkeys + 4) {
418 const std::string &
flags = vStrInputParts.back();
419 bScriptHash = (
flags.find(
'S') != std::string::npos);
420 }
else if (vStrInputParts.size() > numkeys + 4) {
422 throw std::runtime_error(
"Too many parameters");
429 throw std::runtime_error(
430 strprintf(
"redeemScript exceeds size limit: %d > %d",
439 CTxOut txout(value, scriptPubKey);
440 tx.
vout.push_back(txout);
444 const std::string &strInput) {
448 size_t pos = strInput.find(
':');
451 throw std::runtime_error(
"TX output value not specified");
454 if (pos == std::string::npos) {
463 const std::string strData{strInput.substr(pos, std::string::npos)};
465 if (!
IsHex(strData)) {
466 throw std::runtime_error(
"invalid TX output data");
469 std::vector<uint8_t> data =
ParseHex(strData);
472 tx.
vout.push_back(txout);
476 const std::string &strInput) {
478 std::vector<std::string> vStrInputParts =
SplitString(strInput,
':');
479 if (vStrInputParts.size() < 2) {
480 throw std::runtime_error(
"TX output missing separator");
487 const std::string &strScript = vStrInputParts[1];
491 bool bScriptHash =
false;
492 if (vStrInputParts.size() == 3) {
493 const std::string &
flags = vStrInputParts.back();
494 bScriptHash = (
flags.find(
'S') != std::string::npos);
498 throw std::runtime_error(
strprintf(
"script exceeds size limit: %d > %d",
505 throw std::runtime_error(
506 strprintf(
"redeemScript exceeds size limit: %d > %d",
513 CTxOut txout(value, scriptPubKey);
514 tx.
vout.push_back(txout);
518 const std::string &strInIdx) {
521 if (!
ParseInt64(strInIdx, &inIdx) || inIdx < 0 ||
522 inIdx >=
static_cast<int64_t
>(tx.
vin.size())) {
523 throw std::runtime_error(
"Invalid TX input index '" + strInIdx +
"'");
527 tx.
vin.erase(tx.
vin.begin() + inIdx);
531 const std::string &strOutIdx) {
534 if (!
ParseInt64(strOutIdx, &outIdx) || outIdx < 0 ||
535 outIdx >=
static_cast<int64_t
>(tx.
vout.size())) {
536 throw std::runtime_error(
"Invalid TX output index '" + strOutIdx +
"'");
540 tx.
vout.erase(tx.
vout.begin() + outIdx);
557 {
"ALL|FORKID|ANYONECANPAY",
559 {
"NONE|FORKID|ANYONECANPAY",
561 {
"SINGLE|FORKID|ANYONECANPAY",
583 throw std::runtime_error(
"unknown sighash flag/sign option");
595 throw std::runtime_error(
"privatekeys register variable must be set.");
601 for (
unsigned int kidx = 0; kidx < keysObj.
size(); kidx++) {
602 if (!keysObj[kidx].isStr()) {
603 throw std::runtime_error(
"privatekey not a std::string");
608 throw std::runtime_error(
"privatekey not valid");
615 throw std::runtime_error(
"prevtxs register variable must be set.");
620 for (
unsigned int previdx = 0; previdx < prevtxsObj.
size(); previdx++) {
621 const UniValue &prevOut = prevtxsObj[previdx];
623 throw std::runtime_error(
"expected prevtxs internal object");
626 std::map<std::string, UniValue::VType> types = {
631 throw std::runtime_error(
"prevtxs internal object typecheck fail");
636 throw std::runtime_error(
"txid must be hexadecimal string (not '" +
637 prevOut[
"txid"].get_str() +
"')");
642 const int nOut = prevOut[
"vout"].
getInt<
int>();
644 throw std::runtime_error(
"vout cannot be negative");
647 COutPoint out(txid, nOut);
648 std::vector<uint8_t> pkData(
649 ParseHexUV(prevOut[
"scriptPubKey"],
"scriptPubKey"));
650 CScript scriptPubKey(pkData.begin(), pkData.end());
656 std::string err(
"Previous output scriptPubKey mismatch:\n");
659 throw std::runtime_error(err);
665 if (prevOut.
exists(
"amount")) {
674 if (scriptPubKey.IsPayToScriptHash() &&
675 prevOut.
exists(
"redeemScript")) {
676 UniValue v = prevOut[
"redeemScript"];
677 std::vector<uint8_t> rsData(
ParseHexUV(v,
"redeemScript"));
686 for (
size_t i = 0; i < mergedTx.vin.size(); i++) {
687 CTxIn &txin = mergedTx.vin[i];
700 (i < mergedTx.vout.size())) {
703 &mergedTx, i, amount, sigHashType),
704 prevPubKey, sigdata);
720 const std::string &commandVal,
722 std::unique_ptr<Secp256k1Init> ecc;
724 if (command ==
"nversion") {
726 }
else if (command ==
"locktime") {
728 }
else if (command ==
"delin") {
730 }
else if (command ==
"in") {
732 }
else if (command ==
"delout") {
734 }
else if (command ==
"outaddr") {
736 }
else if (command ==
"outpubkey") {
739 }
else if (command ==
"outmultisig") {
742 }
else if (command ==
"outscript") {
744 }
else if (command ==
"outdata") {
746 }
else if (command ==
"sign") {
749 }
else if (command ==
"load") {
751 }
else if (command ==
"set") {
754 throw std::runtime_error(
"unknown command");
762 std::string jsonOutput = entry.
write(4);
768 std::string strHexHash = tx.GetId().GetHex();
793 while (!feof(stdin)) {
794 size_t bread = fread(buf, 1,
sizeof(buf), stdin);
795 ret.append(buf, bread);
796 if (bread <
sizeof(buf)) {
802 throw std::runtime_error(
"error reading stdin");
810 std::string strPrint;
814 while (argc > 1 &&
IsSwitchChar(argv[1][0]) && (argv[1][1] != 0)) {
825 throw std::runtime_error(
"too few parameters");
829 std::string strHexTx(argv[1]);
832 if (strHexTx ==
"-") {
837 throw std::runtime_error(
"invalid transaction encoding");
845 for (
int i = startArg; i < argc; i++) {
846 std::string arg = argv[i];
847 std::string key, value;
848 size_t eqpos = arg.find(
'=');
849 if (eqpos == std::string::npos) {
852 key = arg.substr(0, eqpos);
853 value = arg.substr(eqpos + 1);
856 MutateTx(tx, key, value, chainParams);
860 }
catch (
const std::exception &e) {
861 strPrint = std::string(
"error: ") + e.what();
864 strPrint = std::string(
"error code: ") + e[
"code"].getValStr() +
865 " message: " + e[
"message"].getValStr();
872 if (strPrint !=
"") {
873 tfm::format(nRet == 0 ? std::cout : std::cerr,
"%s\n", strPrint);
879int main(
int argc,
char *argv[]) {
887 }
catch (
const std::exception &e) {
895 int ret = EXIT_FAILURE;
898 }
catch (
const std::exception &e) {
bool HelpRequested(const ArgsManager &args)
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
bool IsSwitchChar(char c)
int main(int argc, char *argv[])
static void OutputTxHash(const CTransaction &tx)
static const unsigned int N_SIGHASH_OPTS
static void MutateTxSign(CMutableTransaction &tx, const std::string &flagStr)
static const int CONTINUE_EXECUTION
static const struct @0 sigHashOptions[N_SIGHASH_OPTS]
static std::string readStdin()
static int CommandLineRawTx(int argc, char *argv[], const CChainParams &chainParams)
static void OutputTxJSON(const CTransaction &tx)
static void RegisterSet(const std::string &strInput)
static void RegisterSetJson(const std::string &key, const std::string &rawJson)
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate string to current locale using Qt.
static void MutateTxDelOutput(CMutableTransaction &tx, const std::string &strOutIdx)
static Amount ExtractAndValidateValue(const std::string &strValue)
static std::map< std::string, UniValue > registers
static void MutateTxAddOutAddr(CMutableTransaction &tx, const std::string &strInput, const CChainParams &chainParams)
static void MutateTxAddOutPubKey(CMutableTransaction &tx, const std::string &strInput)
static void MutateTxAddOutData(CMutableTransaction &tx, const std::string &strInput)
static void MutateTxVersion(CMutableTransaction &tx, const std::string &cmdVal)
static void OutputTxHex(const CTransaction &tx)
static void RegisterLoad(const std::string &strInput)
static void MutateTxDelInput(CMutableTransaction &tx, const std::string &strInIdx)
static int AppInitRawTx(int argc, char *argv[])
static void MutateTxAddInput(CMutableTransaction &tx, const std::string &strInput)
static bool findSigHashFlags(SigHashType &sigHashType, const std::string &flagStr)
static void SetupBitcoinTxArgs(ArgsManager &argsman)
static void MutateTxAddOutMultiSig(CMutableTransaction &tx, const std::string &strInput)
static void MutateTx(CMutableTransaction &tx, const std::string &command, const std::string &commandVal, const CChainParams &chainParams)
static void MutateTxAddOutScript(CMutableTransaction &tx, const std::string &strInput)
static void MutateTxLocktime(CMutableTransaction &tx, const std::string &cmdVal)
static void OutputTx(const CTransaction &tx)
const CScript redeemScript
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given BIP70 chain name.
const CChainParams & Params()
Return the currently selected parameters.
void SetupChainParamsBaseOptions(ArgsManager &argsman)
Set the arguments for chainparams.
@ ALLOW_ANY
disable validation
ChainType GetChainType() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
bool ParseParameters(int argc, const char *const argv[], std::string &error)
std::string GetHelpMessage() const
Get the help string.
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
void AddCoin(const COutPoint &outpoint, Coin coin, bool possible_overwrite)
Add a coin.
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
Abstract view on the open txout dataset.
An encapsulated secp256k1 private key.
bool IsValid() const
Check whether this private key is valid.
A mutable version of CTransaction.
std::vector< CTxOut > vout
An encapsulated public key.
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid())
static constexpr int32_t MAX_VERSION
static constexpr int32_t MIN_VERSION
An output of a transaction.
Fillable signing provider that keeps keys in an address->secret map.
virtual bool AddCScript(const CScript &redeemScript)
virtual bool AddKey(const CKey &key)
A signature creator for transactions.
Signature hash type wrapper class.
BaseSigHashType getBaseType() const
SigHashType withForkId(bool forkId=true) const
bool checkObject(const std::map< std::string, UniValue::VType > &memberTypes) const
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
bool read(std::string_view raw)
bool exists(const std::string &key) const
std::string FormatFullVersion()
std::string LicenseInfo()
Returns licensing information (for -version)
static const uint64_t MAX_TX_SIZE
The maximum allowed size for a transaction, in bytes.
std::string EncodeHexTx(const CTransaction &tx)
CScript ParseScript(const std::string &s)
bool DecodeHexTx(CMutableTransaction &tx, const std::string &strHexTx)
std::vector< uint8_t > ParseHexUV(const UniValue &v, const std::string &strName)
bool ParseHashStr(const std::string &strHex, uint256 &result)
Parse a hex string into 256 bits.
std::string ScriptToAsmStr(const CScript &script, const bool fAttemptSighashDecode=false)
Create the assembly string representation of a CScript object.
void TxToUniv(const CTransaction &tx, const BlockHash &hashBlock, UniValue &entry, bool include_hex=true, const CTxUndo *txundo=nullptr, TxVerbosity verbosity=TxVerbosity::SHOW_DETAILS, std::function< bool(const CTxOut &)> is_change_func={})
void SetupCurrencyUnitOptions(ArgsManager &argsman)
void PrintExceptionContinue(const std::exception *pex, std::string_view thread_name)
void ECC_Start()
Initialize the elliptic curve support.
void ECC_Stop()
Deinitialize the elliptic curve support.
CTxDestination DecodeDestination(const std::string &addr, const CChainParams ¶ms)
CKey DecodeSecret(const std::string &str)
bool ParseMoney(const std::string &money_string, Amount &nRet)
Parse an amount denoted in full coins.
FILE * fopen(const fs::path &p, const char *mode)
std::vector< std::string > SplitString(std::string_view str, char sep)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Amount AmountFromValue(const UniValue &value)
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
static const int MAX_SCRIPT_SIZE
static const int MAX_PUBKEYS_PER_MULTISIG
static std::string ToString(const CService &ip)
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
void UpdateInput(CTxIn &input, const SignatureData &data)
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
static constexpr Amount zero() noexcept
A BlockHash is a unqiue identifier for a block.
A TxId is the identifier of a transaction.
template std::vector< std::byte > ParseHex(std::string_view)
bool ParseInt64(std::string_view str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool IsHex(std::string_view str)
Returns true if each character in str is a hex character, and has an even number of hex digits.
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.