2018-01-02 18:12:05 +01:00
// Copyright (c) 2009-2017 The Bitcoin Core developers
2014-12-13 05:09:33 +01:00
// Distributed under the MIT software license, see the accompanying
2014-07-29 17:12:44 +02:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2015-12-09 11:53:12 +01:00
# if defined(HAVE_CONFIG_H)
2017-11-10 01:57:53 +01:00
# include <config/bitcoin-config.h>
2015-12-09 11:53:12 +01:00
# endif
2017-11-10 01:57:53 +01:00
# include <base58.h>
# include <clientversion.h>
# include <coins.h>
# include <consensus/consensus.h>
# include <core_io.h>
# include <keystore.h>
# include <policy/policy.h>
# include <policy/rbf.h>
# include <primitives/transaction.h>
# include <script/script.h>
# include <script/sign.h>
2015-09-04 16:11:34 +02:00
# include <univalue.h>
2017-11-10 01:57:53 +01:00
# include <util.h>
# include <utilmoneystr.h>
# include <utilstrencodings.h>
2014-07-29 17:12:44 +02:00
# include <stdio.h>
2014-09-14 12:43:56 +02:00
2014-08-19 05:14:29 +02:00
# include <boost/algorithm/string.hpp>
2014-07-29 17:12:44 +02:00
static bool fCreateBlank ;
2016-11-25 09:17:57 +01:00
static std : : map < std : : string , UniValue > registers ;
2016-11-02 19:59:09 +01:00
static const int CONTINUE_EXECUTION = - 1 ;
2014-07-29 17:12:44 +02:00
2016-11-02 19:59:09 +01:00
//
// This function returns either one of EXIT_ codes when it's expected to stop the process or
// CONTINUE_EXECUTION when it's expected to continue further.
//
static int AppInitRawTx ( int argc , char * argv [ ] )
2014-07-29 17:12:44 +02:00
{
//
// Parameters
//
2017-08-01 21:17:40 +02:00
gArgs . ParseParameters ( argc , argv ) ;
2014-07-29 17:12:44 +02:00
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
2015-05-25 09:00:17 +02:00
try {
SelectParams ( ChainNameFromCommandLine ( ) ) ;
2015-10-27 17:39:42 +01:00
} catch ( const std : : exception & e ) {
2015-05-25 09:00:17 +02:00
fprintf ( stderr , " Error: %s \n " , e . what ( ) ) ;
2016-11-09 22:53:55 +01:00
return EXIT_FAILURE ;
2014-07-29 17:12:44 +02:00
}
2017-08-01 21:17:40 +02:00
fCreateBlank = gArgs . GetBoolArg ( " -create " , false ) ;
2014-07-29 17:12:44 +02:00
2017-08-01 21:17:40 +02:00
if ( argc < 2 | | gArgs . IsArgSet ( " -? " ) | | gArgs . IsArgSet ( " -h " ) | | gArgs . IsArgSet ( " -help " ) )
2014-07-29 17:12:44 +02:00
{
// First part of help message is specific to this utility
2015-12-09 11:53:12 +01:00
std : : string strUsage = strprintf ( _ ( " %s bitcoin-tx utility version " ) , _ ( PACKAGE_NAME ) ) + " " + FormatFullVersion ( ) + " \n \n " +
2014-07-29 17:12:44 +02:00
_ ( " Usage: " ) + " \n " +
" bitcoin-tx [options] <hex-tx> [commands] " + _ ( " Update hex-encoded bitcoin transaction " ) + " \n " +
" bitcoin-tx [options] -create [commands] " + _ ( " Create hex-encoded bitcoin transaction " ) + " \n " +
" \n " ;
fprintf ( stdout , " %s " , strUsage . c_str ( ) ) ;
2015-02-04 09:11:49 +01:00
strUsage = HelpMessageGroup ( _ ( " Options: " ) ) ;
strUsage + = HelpMessageOpt ( " -? " , _ ( " This help message " ) ) ;
strUsage + = HelpMessageOpt ( " -create " , _ ( " Create new, empty TX. " ) ) ;
strUsage + = HelpMessageOpt ( " -json " , _ ( " Select JSON output " ) ) ;
strUsage + = HelpMessageOpt ( " -txid " , _ ( " Output only the hex-encoded transaction id of the resultant transaction. " ) ) ;
2015-05-25 09:00:17 +02:00
AppendParamsHelpMessages ( strUsage ) ;
2014-07-29 17:12:44 +02:00
fprintf ( stdout , " %s " , strUsage . c_str ( ) ) ;
2015-02-04 09:11:49 +01:00
strUsage = HelpMessageGroup ( _ ( " Commands: " ) ) ;
strUsage + = HelpMessageOpt ( " delin=N " , _ ( " Delete input N from TX " ) ) ;
strUsage + = HelpMessageOpt ( " delout=N " , _ ( " Delete output N from TX " ) ) ;
2015-12-03 16:29:45 +01:00
strUsage + = HelpMessageOpt ( " in=TXID:VOUT(:SEQUENCE_NUMBER) " , _ ( " Add input to TX " ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " locktime=N " , _ ( " Set TX lock time to N " ) ) ;
strUsage + = HelpMessageOpt ( " nversion=N " , _ ( " Set TX version to N " ) ) ;
strUsage + = HelpMessageOpt ( " outaddr=VALUE:ADDRESS " , _ ( " Add address-based output to TX " ) ) ;
2018-02-02 08:19:41 +01:00
strUsage + = HelpMessageOpt ( " outdata=[VALUE:]DATA " , _ ( " Add data-based output to TX " ) ) ;
strUsage + = HelpMessageOpt ( " outmultisig=VALUE:REQUIRED:PUBKEYS:PUBKEY1:PUBKEY2:....[:FLAGS] " , _ ( " Add Pay To n-of-m Multi-sig output to TX. n = REQUIRED, m = PUBKEYS " ) + " . " +
_ ( " Optionally add the \" W \" flag to produce a pay-to-witness-script-hash output " ) + " . " +
_ ( " Optionally add the \" S \" flag to wrap the output in a pay-to-script-hash. " ) ) ;
2016-10-04 20:21:25 +02:00
strUsage + = HelpMessageOpt ( " outpubkey=VALUE:PUBKEY[:FLAGS] " , _ ( " Add pay-to-pubkey output to TX " ) + " . " +
_ ( " Optionally add the \" W \" flag to produce a pay-to-witness-pubkey-hash output " ) + " . " +
_ ( " Optionally add the \" S \" flag to wrap the output in a pay-to-script-hash. " ) ) ;
strUsage + = HelpMessageOpt ( " outscript=VALUE:SCRIPT[:FLAGS] " , _ ( " Add raw script output to TX " ) + " . " +
_ ( " Optionally add the \" W \" flag to produce a pay-to-witness-script-hash output " ) + " . " +
_ ( " Optionally add the \" S \" flag to wrap the output in a pay-to-script-hash. " ) ) ;
2018-02-02 08:19:41 +01:00
strUsage + = HelpMessageOpt ( " replaceable(=N) " , _ ( " Set RBF opt-in sequence number for input N (if not provided, opt-in all available inputs) " ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " sign=SIGHASH-FLAGS " , _ ( " Add zero or more signatures to transaction " ) + " . " +
_ ( " This command requires JSON registers: " ) +
_ ( " prevtxs=JSON object " ) + " , " +
_ ( " privatekeys=JSON object " ) + " . " +
_ ( " See signrawtransaction docs for format of sighash flags, JSON objects. " ) ) ;
2014-07-29 17:12:44 +02:00
fprintf ( stdout , " %s " , strUsage . c_str ( ) ) ;
2015-02-04 09:11:49 +01:00
strUsage = HelpMessageGroup ( _ ( " Register Commands: " ) ) ;
strUsage + = HelpMessageOpt ( " load=NAME:FILENAME " , _ ( " Load JSON file FILENAME into register NAME " ) ) ;
strUsage + = HelpMessageOpt ( " set=NAME:JSON-STRING " , _ ( " Set register NAME to given JSON-STRING " ) ) ;
2014-07-29 17:12:44 +02:00
fprintf ( stdout , " %s " , strUsage . c_str ( ) ) ;
2016-11-02 19:59:09 +01:00
if ( argc < 2 ) {
fprintf ( stderr , " Error: too few parameters \n " ) ;
return EXIT_FAILURE ;
}
return EXIT_SUCCESS ;
2014-07-29 17:12:44 +02:00
}
2016-11-02 19:59:09 +01:00
return CONTINUE_EXECUTION ;
2014-07-29 17:12:44 +02:00
}
2016-11-25 09:17:57 +01:00
static void RegisterSetJson ( const std : : string & key , const std : : string & rawJson )
2014-07-29 17:12:44 +02:00
{
UniValue val ;
if ( ! val . read ( rawJson ) ) {
2016-11-25 09:17:57 +01:00
std : : string strErr = " Cannot parse JSON for key " + key ;
throw std : : runtime_error ( strErr ) ;
2014-07-29 17:12:44 +02:00
}
registers [ key ] = val ;
}
2016-11-25 09:17:57 +01:00
static void RegisterSet ( const std : : string & strInput )
2014-07-29 17:12:44 +02:00
{
// separate NAME:VALUE in string
size_t pos = strInput . find ( ' : ' ) ;
2016-11-25 09:17:57 +01:00
if ( ( pos = = std : : string : : npos ) | |
2014-07-29 17:12:44 +02:00
( pos = = 0 ) | |
( pos = = ( strInput . size ( ) - 1 ) ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " Register input requires NAME:VALUE " ) ;
2014-07-29 17:12:44 +02:00
2016-11-25 09:17:57 +01:00
std : : string key = strInput . substr ( 0 , pos ) ;
std : : string valStr = strInput . substr ( pos + 1 , std : : string : : npos ) ;
2014-07-29 17:12:44 +02:00
RegisterSetJson ( key , valStr ) ;
}
2016-11-25 09:17:57 +01:00
static void RegisterLoad ( const std : : string & strInput )
2014-07-29 17:12:44 +02:00
{
// separate NAME:FILENAME in string
size_t pos = strInput . find ( ' : ' ) ;
2016-11-25 09:17:57 +01:00
if ( ( pos = = std : : string : : npos ) | |
2014-07-29 17:12:44 +02:00
( pos = = 0 ) | |
( pos = = ( strInput . size ( ) - 1 ) ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " Register load requires NAME:FILENAME " ) ;
2014-07-29 17:12:44 +02:00
2016-11-25 09:17:57 +01:00
std : : string key = strInput . substr ( 0 , pos ) ;
std : : string filename = strInput . substr ( pos + 1 , std : : string : : npos ) ;
2014-07-29 17:12:44 +02:00
FILE * f = fopen ( filename . c_str ( ) , " r " ) ;
if ( ! f ) {
2016-11-25 09:17:57 +01:00
std : : string strErr = " Cannot open file " + filename ;
throw std : : runtime_error ( strErr ) ;
2014-07-29 17:12:44 +02:00
}
// load file chunks into one big buffer
2016-11-25 09:17:57 +01:00
std : : string valStr ;
2014-07-29 17:12:44 +02:00
while ( ( ! feof ( f ) ) & & ( ! ferror ( f ) ) ) {
char buf [ 4096 ] ;
int bread = fread ( buf , 1 , sizeof ( buf ) , f ) ;
if ( bread < = 0 )
break ;
valStr . insert ( valStr . size ( ) , buf , bread ) ;
}
2015-07-28 20:01:00 +02:00
int error = ferror ( f ) ;
fclose ( f ) ;
if ( error ) {
2016-11-25 09:17:57 +01:00
std : : string strErr = " Error reading file " + filename ;
throw std : : runtime_error ( strErr ) ;
2014-07-29 17:12:44 +02:00
}
// evaluate as JSON buffer register
RegisterSetJson ( key , valStr ) ;
}
2016-10-04 20:21:25 +02:00
static CAmount ExtractAndValidateValue ( const std : : string & strValue )
{
CAmount value ;
if ( ! ParseMoney ( strValue , value ) )
throw std : : runtime_error ( " invalid TX output value " ) ;
return value ;
}
2016-11-25 09:17:57 +01:00
static void MutateTxVersion ( CMutableTransaction & tx , const std : : string & cmdVal )
2014-07-29 17:12:44 +02:00
{
int64_t newVersion = atoi64 ( cmdVal ) ;
2016-08-26 23:35:46 +02:00
if ( newVersion < 1 | | newVersion > CTransaction : : MAX_STANDARD_VERSION )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " Invalid TX version requested " ) ;
2014-07-29 17:12:44 +02:00
tx . nVersion = ( int ) newVersion ;
}
2016-11-25 09:17:57 +01:00
static void MutateTxLocktime ( CMutableTransaction & tx , const std : : string & cmdVal )
2014-07-29 17:12:44 +02:00
{
int64_t newLocktime = atoi64 ( cmdVal ) ;
if ( newLocktime < 0LL | | newLocktime > 0xffffffffLL )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " Invalid TX locktime requested " ) ;
2014-07-29 17:12:44 +02:00
tx . nLockTime = ( unsigned int ) newLocktime ;
}
2015-12-03 16:42:01 +01:00
static void MutateTxRBFOptIn ( CMutableTransaction & tx , const std : : string & strInIdx )
{
// parse requested index
int inIdx = atoi ( strInIdx ) ;
if ( inIdx < 0 | | inIdx > = ( int ) tx . vin . size ( ) ) {
throw std : : runtime_error ( " Invalid TX input index ' " + strInIdx + " ' " ) ;
}
// set the nSequence to MAX_INT - 2 (= RBF opt in flag)
int cnt = 0 ;
for ( CTxIn & txin : tx . vin ) {
if ( strInIdx = = " " | | cnt = = inIdx ) {
2017-02-03 20:14:02 +01:00
if ( txin . nSequence > MAX_BIP125_RBF_SEQUENCE ) {
txin . nSequence = MAX_BIP125_RBF_SEQUENCE ;
}
2015-12-03 16:42:01 +01:00
}
+ + cnt ;
}
}
2016-11-25 09:17:57 +01:00
static void MutateTxAddInput ( CMutableTransaction & tx , const std : : string & strInput )
2014-07-29 17:12:44 +02:00
{
2015-12-03 16:29:45 +01:00
std : : vector < std : : string > vStrInputParts ;
boost : : split ( vStrInputParts , strInput , boost : : is_any_of ( " : " ) ) ;
2014-07-29 17:12:44 +02:00
// separate TXID:VOUT in string
2015-12-03 16:29:45 +01:00
if ( vStrInputParts . size ( ) < 2 )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " TX input missing separator " ) ;
2014-07-29 17:12:44 +02:00
// extract and validate TXID
2016-11-25 09:17:57 +01:00
std : : string strTxid = vStrInputParts [ 0 ] ;
2014-07-29 17:12:44 +02:00
if ( ( strTxid . size ( ) ! = 64 ) | | ! IsHex ( strTxid ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " invalid TX input txid " ) ;
2014-12-16 14:50:05 +01:00
uint256 txid ( uint256S ( strTxid ) ) ;
2014-07-29 17:12:44 +02:00
static const unsigned int minTxOutSz = 9 ;
2017-06-17 02:18:42 +02:00
static const unsigned int maxVout = MAX_BLOCK_WEIGHT / ( WITNESS_SCALE_FACTOR * minTxOutSz ) ;
2014-07-29 17:12:44 +02:00
// extract and validate vout
2016-11-25 09:17:57 +01:00
std : : string strVout = vStrInputParts [ 1 ] ;
2014-07-29 17:12:44 +02:00
int vout = atoi ( strVout ) ;
if ( ( vout < 0 ) | | ( vout > ( int ) maxVout ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " invalid TX input vout " ) ;
2014-07-29 17:12:44 +02:00
2015-12-03 16:29:45 +01:00
// extract the optional sequence number
uint32_t nSequenceIn = std : : numeric_limits < unsigned int > : : max ( ) ;
if ( vStrInputParts . size ( ) > 2 )
2016-06-07 19:57:54 +02:00
nSequenceIn = std : : stoul ( vStrInputParts [ 2 ] ) ;
2015-12-03 16:29:45 +01:00
2014-07-29 17:12:44 +02:00
// append to transaction input list
2015-12-03 16:29:45 +01:00
CTxIn txin ( txid , vout , CScript ( ) , nSequenceIn ) ;
2014-07-29 17:12:44 +02:00
tx . vin . push_back ( txin ) ;
}
2016-11-25 09:17:57 +01:00
static void MutateTxAddOutAddr ( CMutableTransaction & tx , const std : : string & strInput )
2014-07-29 17:12:44 +02:00
{
2016-10-04 20:21:25 +02:00
// Separate into VALUE:ADDRESS
std : : vector < std : : string > vStrInputParts ;
boost : : split ( vStrInputParts , strInput , boost : : is_any_of ( " : " ) ) ;
2014-07-29 17:12:44 +02:00
2017-03-28 14:52:59 +02:00
if ( vStrInputParts . size ( ) ! = 2 )
throw std : : runtime_error ( " TX output missing or too many separators " ) ;
2016-10-04 20:21:25 +02:00
// Extract and validate VALUE
CAmount value = ExtractAndValidateValue ( vStrInputParts [ 0 ] ) ;
2014-07-29 17:12:44 +02:00
// extract and validate ADDRESS
2016-10-04 20:21:25 +02:00
std : : string strAddr = vStrInputParts [ 1 ] ;
2017-08-23 03:02:33 +02:00
CTxDestination destination = DecodeDestination ( strAddr ) ;
if ( ! IsValidDestination ( destination ) ) {
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " invalid TX output address " ) ;
2017-08-23 03:02:33 +02:00
}
CScript scriptPubKey = GetScriptForDestination ( destination ) ;
2014-07-29 17:12:44 +02:00
// construct TxOut, append to transaction output list
CTxOut txout ( value , scriptPubKey ) ;
tx . vout . push_back ( txout ) ;
}
2016-10-04 20:21:25 +02:00
static void MutateTxAddOutPubKey ( CMutableTransaction & tx , const std : : string & strInput )
{
// Separate into VALUE:PUBKEY[:FLAGS]
std : : vector < std : : string > vStrInputParts ;
boost : : split ( vStrInputParts , strInput , boost : : is_any_of ( " : " ) ) ;
2017-03-28 14:52:59 +02:00
if ( vStrInputParts . size ( ) < 2 | | vStrInputParts . size ( ) > 3 )
throw std : : runtime_error ( " TX output missing or too many separators " ) ;
2016-10-04 20:21:25 +02:00
// Extract and validate VALUE
CAmount value = ExtractAndValidateValue ( vStrInputParts [ 0 ] ) ;
// Extract and validate PUBKEY
CPubKey pubkey ( ParseHex ( vStrInputParts [ 1 ] ) ) ;
if ( ! pubkey . IsFullyValid ( ) )
throw std : : runtime_error ( " invalid TX output pubkey " ) ;
CScript scriptPubKey = GetScriptForRawPubKey ( pubkey ) ;
// Extract and validate FLAGS
bool bSegWit = false ;
bool bScriptHash = false ;
if ( vStrInputParts . size ( ) = = 3 ) {
std : : string flags = vStrInputParts [ 2 ] ;
2018-01-11 21:40:51 +01:00
bSegWit = ( flags . find ( ' W ' ) ! = std : : string : : npos ) ;
bScriptHash = ( flags . find ( ' S ' ) ! = std : : string : : npos ) ;
2016-10-04 20:21:25 +02:00
}
if ( bSegWit ) {
2017-09-21 02:19:14 +02:00
if ( ! pubkey . IsCompressed ( ) ) {
throw std : : runtime_error ( " Uncompressed pubkeys are not useable for SegWit outputs " ) ;
}
2016-10-04 20:21:25 +02:00
// Call GetScriptForWitness() to build a P2WSH scriptPubKey
scriptPubKey = GetScriptForWitness ( scriptPubKey ) ;
}
if ( bScriptHash ) {
2017-08-23 03:02:33 +02:00
// Get the ID for the script, and then construct a P2SH destination for it.
scriptPubKey = GetScriptForDestination ( CScriptID ( scriptPubKey ) ) ;
2016-10-04 20:21:25 +02:00
}
// construct TxOut, append to transaction output list
CTxOut txout ( value , scriptPubKey ) ;
tx . vout . push_back ( txout ) ;
}
static void MutateTxAddOutMultiSig ( CMutableTransaction & tx , const std : : string & strInput )
{
// Separate into VALUE:REQUIRED:NUMKEYS:PUBKEY1:PUBKEY2:....[:FLAGS]
std : : vector < std : : string > vStrInputParts ;
boost : : split ( vStrInputParts , strInput , boost : : is_any_of ( " : " ) ) ;
// Check that there are enough parameters
if ( vStrInputParts . size ( ) < 3 )
throw std : : runtime_error ( " Not enough multisig parameters " ) ;
// Extract and validate VALUE
CAmount value = ExtractAndValidateValue ( vStrInputParts [ 0 ] ) ;
// Extract REQUIRED
uint32_t required = stoul ( vStrInputParts [ 1 ] ) ;
// Extract NUMKEYS
uint32_t numkeys = stoul ( vStrInputParts [ 2 ] ) ;
// Validate there are the correct number of pubkeys
if ( vStrInputParts . size ( ) < numkeys + 3 )
throw std : : runtime_error ( " incorrect number of multisig pubkeys " ) ;
if ( required < 1 | | required > 20 | | numkeys < 1 | | numkeys > 20 | | numkeys < required )
throw std : : runtime_error ( " multisig parameter mismatch. Required " \
+ std : : to_string ( required ) + " of " + std : : to_string ( numkeys ) + " signatures. " ) ;
// extract and validate PUBKEYs
std : : vector < CPubKey > pubkeys ;
for ( int pos = 1 ; pos < = int ( numkeys ) ; pos + + ) {
CPubKey pubkey ( ParseHex ( vStrInputParts [ pos + 2 ] ) ) ;
if ( ! pubkey . IsFullyValid ( ) )
throw std : : runtime_error ( " invalid TX output pubkey " ) ;
pubkeys . push_back ( pubkey ) ;
}
// Extract FLAGS
bool bSegWit = false ;
bool bScriptHash = false ;
if ( vStrInputParts . size ( ) = = numkeys + 4 ) {
std : : string flags = vStrInputParts . back ( ) ;
2018-01-11 21:40:51 +01:00
bSegWit = ( flags . find ( ' W ' ) ! = std : : string : : npos ) ;
bScriptHash = ( flags . find ( ' S ' ) ! = std : : string : : npos ) ;
2016-10-04 20:21:25 +02:00
}
else if ( vStrInputParts . size ( ) > numkeys + 4 ) {
// Validate that there were no more parameters passed
throw std : : runtime_error ( " Too many parameters " ) ;
}
CScript scriptPubKey = GetScriptForMultisig ( required , pubkeys ) ;
if ( bSegWit ) {
2017-09-21 02:19:14 +02:00
for ( CPubKey & pubkey : pubkeys ) {
if ( ! pubkey . IsCompressed ( ) ) {
throw std : : runtime_error ( " Uncompressed pubkeys are not useable for SegWit outputs " ) ;
}
}
2016-10-04 20:21:25 +02:00
// Call GetScriptForWitness() to build a P2WSH scriptPubKey
scriptPubKey = GetScriptForWitness ( scriptPubKey ) ;
}
if ( bScriptHash ) {
2017-10-24 20:11:21 +02:00
if ( scriptPubKey . size ( ) > MAX_SCRIPT_ELEMENT_SIZE ) {
throw std : : runtime_error ( strprintf (
" redeemScript exceeds size limit: %d > %d " , scriptPubKey . size ( ) , MAX_SCRIPT_ELEMENT_SIZE ) ) ;
}
2017-08-23 03:02:33 +02:00
// Get the ID for the script, and then construct a P2SH destination for it.
scriptPubKey = GetScriptForDestination ( CScriptID ( scriptPubKey ) ) ;
2016-10-04 20:21:25 +02:00
}
// construct TxOut, append to transaction output list
CTxOut txout ( value , scriptPubKey ) ;
tx . vout . push_back ( txout ) ;
}
2016-11-25 09:17:57 +01:00
static void MutateTxAddOutData ( CMutableTransaction & tx , const std : : string & strInput )
2015-06-29 20:14:43 +02:00
{
CAmount value = 0 ;
// separate [VALUE:]DATA in string
size_t pos = strInput . find ( ' : ' ) ;
if ( pos = = 0 )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " TX output value not specified " ) ;
2015-06-29 20:14:43 +02:00
2016-11-25 09:17:57 +01:00
if ( pos ! = std : : string : : npos ) {
2016-10-04 20:21:25 +02:00
// Extract and validate VALUE
value = ExtractAndValidateValue ( strInput . substr ( 0 , pos ) ) ;
2015-06-29 20:14:43 +02:00
}
// extract and validate DATA
2016-11-25 09:17:57 +01:00
std : : string strData = strInput . substr ( pos + 1 , std : : string : : npos ) ;
2015-06-29 20:14:43 +02:00
if ( ! IsHex ( strData ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " invalid TX output data " ) ;
2015-06-29 20:14:43 +02:00
std : : vector < unsigned char > data = ParseHex ( strData ) ;
CTxOut txout ( value , CScript ( ) < < OP_RETURN < < data ) ;
tx . vout . push_back ( txout ) ;
}
2016-11-25 09:17:57 +01:00
static void MutateTxAddOutScript ( CMutableTransaction & tx , const std : : string & strInput )
2014-07-29 17:12:44 +02:00
{
2016-10-04 20:21:25 +02:00
// separate VALUE:SCRIPT[:FLAGS]
std : : vector < std : : string > vStrInputParts ;
boost : : split ( vStrInputParts , strInput , boost : : is_any_of ( " : " ) ) ;
if ( vStrInputParts . size ( ) < 2 )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " TX output missing separator " ) ;
2014-07-29 17:12:44 +02:00
2016-10-04 20:21:25 +02:00
// Extract and validate VALUE
CAmount value = ExtractAndValidateValue ( vStrInputParts [ 0 ] ) ;
2014-07-29 17:12:44 +02:00
// extract and validate script
2016-10-04 20:21:25 +02:00
std : : string strScript = vStrInputParts [ 1 ] ;
CScript scriptPubKey = ParseScript ( strScript ) ;
// Extract FLAGS
bool bSegWit = false ;
bool bScriptHash = false ;
if ( vStrInputParts . size ( ) = = 3 ) {
std : : string flags = vStrInputParts . back ( ) ;
2018-01-11 21:40:51 +01:00
bSegWit = ( flags . find ( ' W ' ) ! = std : : string : : npos ) ;
bScriptHash = ( flags . find ( ' S ' ) ! = std : : string : : npos ) ;
2016-10-04 20:21:25 +02:00
}
2014-07-29 17:12:44 +02:00
2017-10-24 20:11:21 +02:00
if ( scriptPubKey . size ( ) > MAX_SCRIPT_SIZE ) {
throw std : : runtime_error ( strprintf (
" script exceeds size limit: %d > %d " , scriptPubKey . size ( ) , MAX_SCRIPT_SIZE ) ) ;
}
2016-10-04 20:21:25 +02:00
if ( bSegWit ) {
2017-08-23 03:02:33 +02:00
scriptPubKey = GetScriptForWitness ( scriptPubKey ) ;
2016-09-30 21:29:45 +02:00
}
2016-10-04 20:21:25 +02:00
if ( bScriptHash ) {
2017-10-24 20:11:21 +02:00
if ( scriptPubKey . size ( ) > MAX_SCRIPT_ELEMENT_SIZE ) {
throw std : : runtime_error ( strprintf (
" redeemScript exceeds size limit: %d > %d " , scriptPubKey . size ( ) , MAX_SCRIPT_ELEMENT_SIZE ) ) ;
}
2017-08-23 03:02:33 +02:00
scriptPubKey = GetScriptForDestination ( CScriptID ( scriptPubKey ) ) ;
2016-09-30 21:29:45 +02:00
}
2014-07-29 17:12:44 +02:00
// construct TxOut, append to transaction output list
CTxOut txout ( value , scriptPubKey ) ;
tx . vout . push_back ( txout ) ;
}
2016-11-25 09:17:57 +01:00
static void MutateTxDelInput ( CMutableTransaction & tx , const std : : string & strInIdx )
2014-07-29 17:12:44 +02:00
{
// parse requested deletion index
int inIdx = atoi ( strInIdx ) ;
if ( inIdx < 0 | | inIdx > = ( int ) tx . vin . size ( ) ) {
2016-11-25 09:17:57 +01:00
std : : string strErr = " Invalid TX input index ' " + strInIdx + " ' " ;
throw std : : runtime_error ( strErr . c_str ( ) ) ;
2014-07-29 17:12:44 +02:00
}
// delete input from transaction
tx . vin . erase ( tx . vin . begin ( ) + inIdx ) ;
}
2016-11-25 09:17:57 +01:00
static void MutateTxDelOutput ( CMutableTransaction & tx , const std : : string & strOutIdx )
2014-07-29 17:12:44 +02:00
{
// parse requested deletion index
int outIdx = atoi ( strOutIdx ) ;
if ( outIdx < 0 | | outIdx > = ( int ) tx . vout . size ( ) ) {
2016-11-25 09:17:57 +01:00
std : : string strErr = " Invalid TX output index ' " + strOutIdx + " ' " ;
throw std : : runtime_error ( strErr . c_str ( ) ) ;
2014-07-29 17:12:44 +02:00
}
// delete output from transaction
tx . vout . erase ( tx . vout . begin ( ) + outIdx ) ;
}
static const unsigned int N_SIGHASH_OPTS = 6 ;
static const struct {
const char * flagStr ;
int flags ;
} sighashOptions [ N_SIGHASH_OPTS ] = {
2014-08-13 17:53:39 +02:00
{ " ALL " , SIGHASH_ALL } ,
{ " NONE " , SIGHASH_NONE } ,
{ " SINGLE " , SIGHASH_SINGLE } ,
{ " ALL|ANYONECANPAY " , SIGHASH_ALL | SIGHASH_ANYONECANPAY } ,
{ " NONE|ANYONECANPAY " , SIGHASH_NONE | SIGHASH_ANYONECANPAY } ,
{ " SINGLE|ANYONECANPAY " , SIGHASH_SINGLE | SIGHASH_ANYONECANPAY } ,
2014-07-29 17:12:44 +02:00
} ;
2016-11-25 09:17:57 +01:00
static bool findSighashFlags ( int & flags , const std : : string & flagStr )
2014-07-29 17:12:44 +02:00
{
flags = 0 ;
for ( unsigned int i = 0 ; i < N_SIGHASH_OPTS ; i + + ) {
if ( flagStr = = sighashOptions [ i ] . flagStr ) {
flags = sighashOptions [ i ] . flags ;
return true ;
}
}
return false ;
}
2016-03-31 14:54:58 +02:00
static CAmount AmountFromValue ( const UniValue & value )
{
if ( ! value . isNum ( ) & & ! value . isStr ( ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " Amount is not a number or string " ) ;
2016-03-31 14:54:58 +02:00
CAmount amount ;
if ( ! ParseFixedPoint ( value . getValStr ( ) , 8 , & amount ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " Invalid amount " ) ;
2016-03-31 14:54:58 +02:00
if ( ! MoneyRange ( amount ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " Amount out of range " ) ;
2016-03-31 14:54:58 +02:00
return amount ;
}
2016-11-25 09:17:57 +01:00
static void MutateTxSign ( CMutableTransaction & tx , const std : : string & flagStr )
2014-07-29 17:12:44 +02:00
{
int nHashType = SIGHASH_ALL ;
if ( flagStr . size ( ) > 0 )
if ( ! findSighashFlags ( nHashType , flagStr ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " unknown sighash flag/sign option " ) ;
2014-07-29 17:12:44 +02:00
2016-11-25 09:17:57 +01:00
std : : vector < CTransaction > txVariants ;
2014-07-29 17:12:44 +02:00
txVariants . push_back ( tx ) ;
// mergedTx will end up with all the signatures; it
// starts as a clone of the raw tx:
CMutableTransaction mergedTx ( txVariants [ 0 ] ) ;
bool fComplete = true ;
CCoinsView viewDummy ;
2014-09-24 03:19:04 +02:00
CCoinsViewCache view ( & viewDummy ) ;
2014-07-29 17:12:44 +02:00
if ( ! registers . count ( " privatekeys " ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " privatekeys register variable must be set. " ) ;
2014-07-29 17:12:44 +02:00
CBasicKeyStore tempKeystore ;
UniValue keysObj = registers [ " privatekeys " ] ;
2014-08-20 15:59:31 +02:00
for ( unsigned int kidx = 0 ; kidx < keysObj . size ( ) ; kidx + + ) {
2014-07-29 17:12:44 +02:00
if ( ! keysObj [ kidx ] . isStr ( ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " privatekey not a std::string " ) ;
2017-09-20 01:49:52 +02:00
CKey key = DecodeSecret ( keysObj [ kidx ] . getValStr ( ) ) ;
if ( ! key . IsValid ( ) ) {
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " privatekey not valid " ) ;
2017-09-20 01:49:52 +02:00
}
2014-07-29 17:12:44 +02:00
tempKeystore . AddKey ( key ) ;
}
// Add previous txouts given in the RPC call:
if ( ! registers . count ( " prevtxs " ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " prevtxs register variable must be set. " ) ;
2014-12-23 03:04:53 +01:00
UniValue prevtxsObj = registers [ " prevtxs " ] ;
2014-07-29 17:12:44 +02:00
{
2014-08-20 15:59:31 +02:00
for ( unsigned int previdx = 0 ; previdx < prevtxsObj . size ( ) ; previdx + + ) {
2014-07-29 17:12:44 +02:00
UniValue prevOut = prevtxsObj [ previdx ] ;
if ( ! prevOut . isObject ( ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " expected prevtxs internal object " ) ;
2014-07-29 17:12:44 +02:00
2017-06-06 21:15:28 +02:00
std : : map < std : : string , UniValue : : VType > types = {
{ " txid " , UniValue : : VSTR } ,
{ " vout " , UniValue : : VNUM } ,
{ " scriptPubKey " , UniValue : : VSTR } ,
} ;
2014-07-29 17:12:44 +02:00
if ( ! prevOut . checkObject ( types ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " prevtxs internal object typecheck fail " ) ;
2014-07-29 17:12:44 +02:00
2014-12-23 03:59:00 +01:00
uint256 txid = ParseHashUV ( prevOut [ " txid " ] , " txid " ) ;
2014-07-29 17:12:44 +02:00
int nOut = atoi ( prevOut [ " vout " ] . getValStr ( ) ) ;
if ( nOut < 0 )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " vout must be positive " ) ;
2014-07-29 17:12:44 +02:00
2017-04-25 20:29:30 +02:00
COutPoint out ( txid , nOut ) ;
2016-11-25 09:17:57 +01:00
std : : vector < unsigned char > pkData ( ParseHexUV ( prevOut [ " scriptPubKey " ] , " scriptPubKey " ) ) ;
2014-07-29 17:12:44 +02:00
CScript scriptPubKey ( pkData . begin ( ) , pkData . end ( ) ) ;
2014-09-03 09:01:24 +02:00
{
2017-04-25 20:29:30 +02:00
const Coin & coin = view . AccessCoin ( out ) ;
2017-05-31 02:58:54 +02:00
if ( ! coin . IsSpent ( ) & & coin . out . scriptPubKey ! = scriptPubKey ) {
2016-11-25 09:17:57 +01:00
std : : string err ( " Previous output scriptPubKey mismatch: \n " ) ;
2017-04-25 20:29:30 +02:00
err = err + ScriptToAsmStr ( coin . out . scriptPubKey ) + " \n vs: \n " +
2015-07-31 01:56:00 +02:00
ScriptToAsmStr ( scriptPubKey ) ;
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( err ) ;
2014-07-29 17:12:44 +02:00
}
2017-04-25 20:29:30 +02:00
Coin newcoin ;
newcoin . out . scriptPubKey = scriptPubKey ;
newcoin . out . nValue = 0 ;
2016-03-31 14:54:58 +02:00
if ( prevOut . exists ( " amount " ) ) {
2017-04-25 20:29:30 +02:00
newcoin . out . nValue = AmountFromValue ( prevOut [ " amount " ] ) ;
2016-03-31 14:54:58 +02:00
}
2017-04-25 20:29:30 +02:00
newcoin . nHeight = 1 ;
view . AddCoin ( out , std : : move ( newcoin ) , true ) ;
2014-07-29 17:12:44 +02:00
}
// if redeemScript given and private keys given,
// add redeemScript to the tempKeystore so it can be signed:
2016-08-25 12:48:23 +02:00
if ( ( scriptPubKey . IsPayToScriptHash ( ) | | scriptPubKey . IsPayToWitnessScriptHash ( ) ) & &
2014-07-29 17:12:44 +02:00
prevOut . exists ( " redeemScript " ) ) {
UniValue v = prevOut [ " redeemScript " ] ;
2016-11-25 09:17:57 +01:00
std : : vector < unsigned char > rsData ( ParseHexUV ( v , " redeemScript " ) ) ;
2014-07-29 17:12:44 +02:00
CScript redeemScript ( rsData . begin ( ) , rsData . end ( ) ) ;
tempKeystore . AddCScript ( redeemScript ) ;
}
}
}
const CKeyStore & keystore = tempKeystore ;
bool fHashSingle = ( ( nHashType & ~ SIGHASH_ANYONECANPAY ) = = SIGHASH_SINGLE ) ;
// Sign what we can:
for ( unsigned int i = 0 ; i < mergedTx . vin . size ( ) ; i + + ) {
CTxIn & txin = mergedTx . vin [ i ] ;
2017-04-25 20:29:30 +02:00
const Coin & coin = view . AccessCoin ( txin . prevout ) ;
2017-05-31 02:58:54 +02:00
if ( coin . IsSpent ( ) ) {
2014-07-29 17:12:44 +02:00
fComplete = false ;
continue ;
}
2017-04-25 20:29:30 +02:00
const CScript & prevPubKey = coin . out . scriptPubKey ;
const CAmount & amount = coin . out . nValue ;
2014-07-29 17:12:44 +02:00
2016-03-31 14:54:58 +02:00
SignatureData sigdata ;
2014-07-29 17:12:44 +02:00
// Only sign SIGHASH_SINGLE if there's a corresponding output:
if ( ! fHashSingle | | ( i < mergedTx . vout . size ( ) ) )
2016-03-31 14:54:58 +02:00
ProduceSignature ( MutableTransactionSignatureCreator ( & keystore , & mergedTx , i , amount , nHashType ) , prevPubKey , sigdata ) ;
2014-07-29 17:12:44 +02:00
// ... and merge in other signatures:
2017-06-02 03:18:57 +02:00
for ( const CTransaction & txv : txVariants )
2016-03-31 14:54:58 +02:00
sigdata = CombineSignatures ( prevPubKey , MutableTransactionSignatureChecker ( & mergedTx , i , amount ) , sigdata , DataFromTransaction ( txv , i ) ) ;
UpdateTransaction ( mergedTx , i , sigdata ) ;
2016-08-04 02:49:16 +02:00
if ( ! VerifyScript ( txin . scriptSig , prevPubKey , & txin . scriptWitness , STANDARD_SCRIPT_VERIFY_FLAGS , MutableTransactionSignatureChecker ( & mergedTx , i , amount ) ) )
2014-07-29 17:12:44 +02:00
fComplete = false ;
}
if ( fComplete ) {
// do nothing... for now
// perhaps store this for later optional JSON output
}
tx = mergedTx ;
}
Update key.cpp to use new libsecp256k1
libsecp256k1's API changed, so update key.cpp to use it.
Libsecp256k1 now has explicit context objects, which makes it completely thread-safe.
In turn, keep an explicit context object in key.cpp, which is explicitly initialized
destroyed. This is not really pretty now, but it's more efficient than the static
initialized object in key.cpp (which made for example bitcoin-tx slow, as for most of
its calls, libsecp256k1 wasn't actually needed).
This also brings in the new blinding support in libsecp256k1. By passing in a random
seed, temporary variables during the elliptic curve computations are altered, in such
a way that if an attacker does not know the blind, observing the internal operations
leaks less information about the keys used. This was implemented by Greg Maxwell.
2015-04-22 23:28:26 +02:00
class Secp256k1Init
{
2015-07-28 20:11:20 +02:00
ECCVerifyHandle globalVerifyHandle ;
Update key.cpp to use new libsecp256k1
libsecp256k1's API changed, so update key.cpp to use it.
Libsecp256k1 now has explicit context objects, which makes it completely thread-safe.
In turn, keep an explicit context object in key.cpp, which is explicitly initialized
destroyed. This is not really pretty now, but it's more efficient than the static
initialized object in key.cpp (which made for example bitcoin-tx slow, as for most of
its calls, libsecp256k1 wasn't actually needed).
This also brings in the new blinding support in libsecp256k1. By passing in a random
seed, temporary variables during the elliptic curve computations are altered, in such
a way that if an attacker does not know the blind, observing the internal operations
leaks less information about the keys used. This was implemented by Greg Maxwell.
2015-04-22 23:28:26 +02:00
public :
2015-07-28 20:11:20 +02:00
Secp256k1Init ( ) {
ECC_Start ( ) ;
}
~ Secp256k1Init ( ) {
ECC_Stop ( ) ;
}
Update key.cpp to use new libsecp256k1
libsecp256k1's API changed, so update key.cpp to use it.
Libsecp256k1 now has explicit context objects, which makes it completely thread-safe.
In turn, keep an explicit context object in key.cpp, which is explicitly initialized
destroyed. This is not really pretty now, but it's more efficient than the static
initialized object in key.cpp (which made for example bitcoin-tx slow, as for most of
its calls, libsecp256k1 wasn't actually needed).
This also brings in the new blinding support in libsecp256k1. By passing in a random
seed, temporary variables during the elliptic curve computations are altered, in such
a way that if an attacker does not know the blind, observing the internal operations
leaks less information about the keys used. This was implemented by Greg Maxwell.
2015-04-22 23:28:26 +02:00
} ;
2016-11-25 09:17:57 +01:00
static void MutateTx ( CMutableTransaction & tx , const std : : string & command ,
const std : : string & commandVal )
2014-07-29 17:12:44 +02:00
{
2016-08-30 22:41:56 +02:00
std : : unique_ptr < Secp256k1Init > ecc ;
Update key.cpp to use new libsecp256k1
libsecp256k1's API changed, so update key.cpp to use it.
Libsecp256k1 now has explicit context objects, which makes it completely thread-safe.
In turn, keep an explicit context object in key.cpp, which is explicitly initialized
destroyed. This is not really pretty now, but it's more efficient than the static
initialized object in key.cpp (which made for example bitcoin-tx slow, as for most of
its calls, libsecp256k1 wasn't actually needed).
This also brings in the new blinding support in libsecp256k1. By passing in a random
seed, temporary variables during the elliptic curve computations are altered, in such
a way that if an attacker does not know the blind, observing the internal operations
leaks less information about the keys used. This was implemented by Greg Maxwell.
2015-04-22 23:28:26 +02:00
2014-07-29 17:12:44 +02:00
if ( command = = " nversion " )
MutateTxVersion ( tx , commandVal ) ;
else if ( command = = " locktime " )
MutateTxLocktime ( tx , commandVal ) ;
2017-06-28 23:53:44 +02:00
else if ( command = = " replaceable " ) {
2015-12-03 16:42:01 +01:00
MutateTxRBFOptIn ( tx , commandVal ) ;
}
2014-07-29 17:12:44 +02:00
else if ( command = = " delin " )
MutateTxDelInput ( tx , commandVal ) ;
else if ( command = = " in " )
MutateTxAddInput ( tx , commandVal ) ;
else if ( command = = " delout " )
MutateTxDelOutput ( tx , commandVal ) ;
else if ( command = = " outaddr " )
MutateTxAddOutAddr ( tx , commandVal ) ;
2017-02-11 03:03:14 +01:00
else if ( command = = " outpubkey " ) {
2017-07-21 18:42:22 +02:00
ecc . reset ( new Secp256k1Init ( ) ) ;
2016-10-04 20:21:25 +02:00
MutateTxAddOutPubKey ( tx , commandVal ) ;
2017-02-11 03:03:14 +01:00
} else if ( command = = " outmultisig " ) {
2017-07-21 18:42:22 +02:00
ecc . reset ( new Secp256k1Init ( ) ) ;
2016-10-04 20:21:25 +02:00
MutateTxAddOutMultiSig ( tx , commandVal ) ;
2017-02-11 03:03:14 +01:00
} else if ( command = = " outscript " )
2014-07-29 17:12:44 +02:00
MutateTxAddOutScript ( tx , commandVal ) ;
2016-10-04 20:21:25 +02:00
else if ( command = = " outdata " )
MutateTxAddOutData ( tx , commandVal ) ;
2014-07-29 17:12:44 +02:00
Update key.cpp to use new libsecp256k1
libsecp256k1's API changed, so update key.cpp to use it.
Libsecp256k1 now has explicit context objects, which makes it completely thread-safe.
In turn, keep an explicit context object in key.cpp, which is explicitly initialized
destroyed. This is not really pretty now, but it's more efficient than the static
initialized object in key.cpp (which made for example bitcoin-tx slow, as for most of
its calls, libsecp256k1 wasn't actually needed).
This also brings in the new blinding support in libsecp256k1. By passing in a random
seed, temporary variables during the elliptic curve computations are altered, in such
a way that if an attacker does not know the blind, observing the internal operations
leaks less information about the keys used. This was implemented by Greg Maxwell.
2015-04-22 23:28:26 +02:00
else if ( command = = " sign " ) {
2017-07-21 18:42:22 +02:00
ecc . reset ( new Secp256k1Init ( ) ) ;
2014-07-29 17:12:44 +02:00
MutateTxSign ( tx , commandVal ) ;
Update key.cpp to use new libsecp256k1
libsecp256k1's API changed, so update key.cpp to use it.
Libsecp256k1 now has explicit context objects, which makes it completely thread-safe.
In turn, keep an explicit context object in key.cpp, which is explicitly initialized
destroyed. This is not really pretty now, but it's more efficient than the static
initialized object in key.cpp (which made for example bitcoin-tx slow, as for most of
its calls, libsecp256k1 wasn't actually needed).
This also brings in the new blinding support in libsecp256k1. By passing in a random
seed, temporary variables during the elliptic curve computations are altered, in such
a way that if an attacker does not know the blind, observing the internal operations
leaks less information about the keys used. This was implemented by Greg Maxwell.
2015-04-22 23:28:26 +02:00
}
2014-07-29 17:12:44 +02:00
else if ( command = = " load " )
RegisterLoad ( commandVal ) ;
else if ( command = = " set " )
RegisterSet ( commandVal ) ;
else
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " unknown command " ) ;
2014-07-29 17:12:44 +02:00
}
static void OutputTxJSON ( const CTransaction & tx )
{
UniValue entry ( UniValue : : VOBJ ) ;
2014-12-15 09:11:16 +01:00
TxToUniv ( tx , uint256 ( ) , entry ) ;
2014-07-29 17:12:44 +02:00
2016-11-25 09:17:57 +01:00
std : : string jsonOutput = entry . write ( 4 ) ;
2014-07-29 17:12:44 +02:00
fprintf ( stdout , " %s \n " , jsonOutput . c_str ( ) ) ;
}
2014-11-04 19:01:41 +01:00
static void OutputTxHash ( const CTransaction & tx )
{
2016-11-25 09:17:57 +01:00
std : : string strHexHash = tx . GetHash ( ) . GetHex ( ) ; // the hex-encoded transaction hash (aka the transaction id)
2014-11-04 19:01:41 +01:00
fprintf ( stdout , " %s \n " , strHexHash . c_str ( ) ) ;
}
2014-07-29 17:12:44 +02:00
static void OutputTxHex ( const CTransaction & tx )
{
2016-11-25 09:17:57 +01:00
std : : string strHex = EncodeHexTx ( tx ) ;
2014-07-29 17:12:44 +02:00
fprintf ( stdout , " %s \n " , strHex . c_str ( ) ) ;
}
static void OutputTx ( const CTransaction & tx )
{
2017-08-01 21:17:40 +02:00
if ( gArgs . GetBoolArg ( " -json " , false ) )
2014-07-29 17:12:44 +02:00
OutputTxJSON ( tx ) ;
2017-08-01 21:17:40 +02:00
else if ( gArgs . GetBoolArg ( " -txid " , false ) )
2014-11-04 19:01:41 +01:00
OutputTxHash ( tx ) ;
2014-07-29 17:12:44 +02:00
else
OutputTxHex ( tx ) ;
}
2016-11-25 09:17:57 +01:00
static std : : string readStdin ( )
2014-08-19 05:14:29 +02:00
{
char buf [ 4096 ] ;
2016-11-25 09:17:57 +01:00
std : : string ret ;
2014-08-19 05:14:29 +02:00
while ( ! feof ( stdin ) ) {
size_t bread = fread ( buf , 1 , sizeof ( buf ) , stdin ) ;
ret . append ( buf , bread ) ;
if ( bread < sizeof ( buf ) )
break ;
}
if ( ferror ( stdin ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " error reading stdin " ) ;
2014-08-19 05:14:29 +02:00
boost : : algorithm : : trim_right ( ret ) ;
return ret ;
}
2014-07-29 17:12:44 +02:00
static int CommandLineRawTx ( int argc , char * argv [ ] )
{
2016-11-25 09:17:57 +01:00
std : : string strPrint ;
2014-07-29 17:12:44 +02:00
int nRet = 0 ;
try {
2014-08-19 05:14:29 +02:00
// Skip switches; Permit common stdin convention "-"
while ( argc > 1 & & IsSwitchChar ( argv [ 1 ] [ 0 ] ) & &
( argv [ 1 ] [ 1 ] ! = 0 ) ) {
2014-07-29 17:12:44 +02:00
argc - - ;
argv + + ;
}
2016-11-30 23:50:20 +01:00
CMutableTransaction tx ;
2014-07-29 17:12:44 +02:00
int startArg ;
if ( ! fCreateBlank ) {
// require at least one param
if ( argc < 2 )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " too few parameters " ) ;
2014-07-29 17:12:44 +02:00
// param: hex-encoded bitcoin transaction
2016-11-25 09:17:57 +01:00
std : : string strHexTx ( argv [ 1 ] ) ;
2014-08-19 05:14:29 +02:00
if ( strHexTx = = " - " ) // "-" implies standard input
strHexTx = readStdin ( ) ;
2014-07-29 17:12:44 +02:00
2016-11-30 23:50:20 +01:00
if ( ! DecodeHexTx ( tx , strHexTx , true ) )
2016-11-25 09:17:57 +01:00
throw std : : runtime_error ( " invalid transaction encoding " ) ;
2014-07-29 17:12:44 +02:00
startArg = 2 ;
} else
startArg = 1 ;
for ( int i = startArg ; i < argc ; i + + ) {
2016-11-25 09:17:57 +01:00
std : : string arg = argv [ i ] ;
std : : string key , value ;
2014-07-29 17:12:44 +02:00
size_t eqpos = arg . find ( ' = ' ) ;
2016-11-25 09:17:57 +01:00
if ( eqpos = = std : : string : : npos )
2014-07-29 17:12:44 +02:00
key = arg ;
else {
key = arg . substr ( 0 , eqpos ) ;
value = arg . substr ( eqpos + 1 ) ;
}
MutateTx ( tx , key , value ) ;
}
OutputTx ( tx ) ;
}
2014-12-07 13:29:06 +01:00
catch ( const boost : : thread_interrupted & ) {
2014-07-29 17:12:44 +02:00
throw ;
}
2014-12-07 13:29:06 +01:00
catch ( const std : : exception & e ) {
2016-11-25 09:17:57 +01:00
strPrint = std : : string ( " error: " ) + e . what ( ) ;
2014-07-29 17:12:44 +02:00
nRet = EXIT_FAILURE ;
}
catch ( . . . ) {
2017-08-07 07:36:37 +02:00
PrintExceptionContinue ( nullptr , " CommandLineRawTx() " ) ;
2014-07-29 17:12:44 +02:00
throw ;
}
if ( strPrint ! = " " ) {
fprintf ( ( nRet = = 0 ? stdout : stderr ) , " %s \n " , strPrint . c_str ( ) ) ;
}
return nRet ;
}
int main ( int argc , char * argv [ ] )
{
SetupEnvironment ( ) ;
try {
2016-11-02 19:59:09 +01:00
int ret = AppInitRawTx ( argc , argv ) ;
if ( ret ! = CONTINUE_EXECUTION )
return ret ;
2014-07-29 17:12:44 +02:00
}
2014-12-07 13:29:06 +01:00
catch ( const std : : exception & e ) {
2014-07-29 17:12:44 +02:00
PrintExceptionContinue ( & e , " AppInitRawTx() " ) ;
return EXIT_FAILURE ;
} catch ( . . . ) {
2017-08-07 07:36:37 +02:00
PrintExceptionContinue ( nullptr , " AppInitRawTx() " ) ;
2014-07-29 17:12:44 +02:00
return EXIT_FAILURE ;
}
int ret = EXIT_FAILURE ;
try {
ret = CommandLineRawTx ( argc , argv ) ;
}
2014-12-07 13:29:06 +01:00
catch ( const std : : exception & e ) {
2014-07-29 17:12:44 +02:00
PrintExceptionContinue ( & e , " CommandLineRawTx() " ) ;
} catch ( . . . ) {
2017-08-07 07:36:37 +02:00
PrintExceptionContinue ( nullptr , " CommandLineRawTx() " ) ;
2014-07-29 17:12:44 +02:00
}
return ret ;
}