Author

Topic: bitcoin-cli: createrawtransaction using data files as input (Read 272 times)

HCP
legendary
Activity: 2086
Merit: 4361
Awesome... glad you got it sorted. I was fairly sure it would be something silly like that. Honestly, sometimes Linux/Unix does some really unexpected stuff Roll Eyes

At least Windows is just stupid all the time Tongue
legendary
Activity: 3430
Merit: 3080
Thanks arulbero & HCP.

So I removed all quote marks around the inputs and around the outputs from the .json text files, and removed all spaces except for the space between the inputs and outputs. I also removed all array brackets (square brackets like these [ ] ) from around the outputs.


tx.json:
Code:
[{"txid":"notarealtxid78ahfjb73qhb54jhb6fe7f7efkjbekshc9jh38394394hddskdskflnotarealtxid","vout":0}] {"1NotARealBitcoinAddress":0.01}

command:
Code:
bitcoin-cli createrawtransaction $(cat ./tx.json)

...and it works fine.



To send yourself change, the outputs must be as below

anotherTx.json:
Code:
[{"txid":"notarealtxid78ahfjb73qhb54jhb6fe7f7efkjbekshc9jh38394394hddskdskflnotarealtxid","vout":0}] {"1NotARealBitcoinAddress":0.01,"1NotMyChangeBitcoinAddress":0.0005}

2 inputs instead of 2 outputs goes like

yetAnotherTx.json:
Code:
[{"txid":"notarealtxid78ahfjb73qhb54jhb6fe7f7efkjbekshc9jh38394394hddskdskflnotarealtxid","vout":0},{"txid":"anothertxidthatsnotreal47fhsifb8e928s8hdjpququ7sdmwisw5lttyp","vout":0}] {"1NotARealBitcoinAddress":0.01}

The same command works for both: bitcoin-cli createrawtransaction $(cat anotherTx.json)


...again, 1 space only in the entire string that is between the ins and the outs, no returns, and no quotes around anything except the fields and their values. These magic spells on the linux CLI Smiley
legendary
Activity: 1932
Merit: 2077
... and have tried using the escaped quote marks (\") way also, same error. Certainly bitcoin-cli needs double quotes around $(cat filename), else space characters stop any data after a space character from being supplied to bitcoin-cli.

It works this way:

Code:
$ ./bitcoin-cli createrawtransaction '[{"txid":"f0b40f9b0feb8de8cf9e4d717d5a965ee42a15497398e66b780f3e9582b15b10","vout":0}]' '{"1PtfkLyz1Pi3UFcVy8Pvv5ioS4wYxcWXCJ":0.01}'

0200000001105bb182953e0f786be6987349152ae45e965a7d714d9ecfe88deb0f9b0fb4f00000000000ffffffff0140420f00000000001976a914fb18536ffff4130d5d92bf6887b7532b569ea0e888ac00000000

Code:
$ ./bitcoin-cli createrawtransaction $(cat ./tx.json)

0200000001105bb182953e0f786be6987349152ae45e965a7d714d9ecfe88deb0f9b0fb4f00000000000ffffffff0140420f00000000001976a914fb18536ffff4130d5d92bf6887b7532b569ea0e888ac00000000

where tx.json is:

Code:
[{"txid":"f0b40f9b0feb8de8cf9e4d717d5a965ee42a15497398e66b780f3e9582b15b10","vout":0}] {"1PtfkLyz1Pi3UFcVy8Pvv5ioS4wYxcWXCJ":0.01}
legendary
Activity: 1932
Merit: 2077
So, issues

Here's what I'm trying:

Code:
$ bitcoin-cli createrawtransaction "$(cat ./tx.json)"
error: Error parsing JSON:'''[{"txid":"notarealtxid78ahfjb73qhb54jhb6fe7f7efkjbekshc9jh38394394hddskdskflnotarealtxid","vout":0}]''' '[{"1NotARealBitcoinAddress":0.01}]'

It should work, but doesn't (sendrawtransaction does work this way, eg. bitcoin-cli sendrawtransaction $(cat hextxsavedtoafile)

Hi, I did a bash script to create and send a raw tx with multiple inputs/outputs.

Usage:  ./maketx.sh input-file output-file

I used it with this file:
input-file = in-addresses.txt  and  output-file = out-addresses.txt
to do this transaction:
https://www.blockchain.com/btc/tx/4b275328fdccea39f1da33802aca6c81949eea61fbd2341bc599f648b78e1408

Maybe it can help you.

I get the rawtx in this way:

Code:
space=" "
rawtx=$($bitcoincli createrawtransaction $input $space $output)



maketx.sh
Code:
#! /bin/bash

###################################################################################
# Usage:
# ./maketx.sh input-file output-file
#
# input-file cointains data to build up the input: UTXO and sender addresses
# TXID VOUT (ADDRESS)
#
# output-file cointains data to build up the output: destination addresses and amount
# ADDRESS AMOUNT

# you first need run the Bitcoin Core (bitcoind) in order to use this script!!!
# the private keys to the sender addresses must be in your wallet.dat!
# you don't need to be online to create and sign the transaction, but you have to be to send it
#####################################################################################

bitcoincli="/home/****/bitcoin-0.16.1/src/bitcoin/src/bitcoin-cli"


###################READ INPUT UTXOTXID, VOUT AND ADDRESSES###########################
j=1  
while IFS='' read -r line || [[ -n "$line" ]]; do
    #echo "Text read from file: $line"
    utxotxid[$j]=$(echo $line | awk -F ' ' '{print $1}')
    utxovout[$j]=$(echo $line | awk -F ' ' '{print $2}')
    #utxoaddress[$j]=$(echo $line | awk -F ' ' '{print $3}')
    let j=j+1
done < "$1"

let j=j-1
COUNTER1=$j
j=1

input=""
while [  $j -lt $COUNTER1 ]; do
input=$input\{\"txid\":\"${utxotxid[$j]}\",\"vout\":${utxovout[$j]}\},
let j=j+1
done
input=\[$input\{\"txid\":\"${utxotxid[$j]}\",\"vout\":${utxovout[$j]}\}\]
#echo $input


#################READ OUTPUT ADDRESSES AND AMOUNT TO SEND############################
i=1
while IFS='' read -r line || [[ -n "$line" ]]; do
    #echo "Text read from file: $line"
    address[$i]=$(echo $line | awk -F ' ' '{print $1}')
    amount[$i]=$(echo $line | awk -F ' ' '{print $2}')
    let i=i+1
done < "$2"

let i=i-1
COUNTER2=$i
i=1

output=""
while [  $i -lt $COUNTER2 ]; do
output=$output\"${address[$i]}\":${amount[$i]},
let i=i+1
done
output=\{$output\"${address[$i]}\":${amount[$i]}\}
#echo $output



############CREATE RAWTRANSACTION#####################################################
space=" "
rawtx=$($bitcoincli createrawtransaction $input $space $output)
#echo $rawtx


############SIGN TRANSACTION##########################################################
echo "Insert the passphrase for your wallet.dat: "
read passphrase
echo ""

$bitcoincli walletpassphrase $passphrase 10

partsignedrawtx=$($bitcoincli signrawtransaction $rawtx | grep hex | awk -F '"' '{print $4}')
j=2
while [  $j -le $COUNTER1 ]; do
partsignedrawtx=$($bitcoincli signrawtransaction $partsignedrawtx | grep hex | awk -F '"' '{print $4}')
let j=j+1
done
finaltx=$partsignedrawtx
#echo $finaltx

$bitcoincli walletlock

############LAST CHECK AND SENDING#####################################################

if [ $rawtx == $finaltx ]
then
printf  "\nError!!!\n"
else
echo "Do you want to check the transaction before sending it? (y/n)"
read answer
echo ""
if [ $answer == "y" ]
then
$bitcoincli decoderawtransaction $finaltx
fi
echo "Do you want to send the transaction? (y/n)"
echo "Remember you have to be online to perform this operation"
read answer2
echo ""
if [ $answer2 == "y" ]
then
txid=$($bitcoincli sendrawtransaction $finaltx)
echo "The txid is: $txid --> https://www.blockchain.com/btc/tx/$txid"
echo ""
fi
fi

in-addresses.txt
Code:
f0b40f9b0feb8de8cf9e4d717d5a965ee42a15497398e66b780f3e9582b15b10 0 1Q7FLLT4vpunCV8H5ohMhwqUfZe23LksZS
f0b40f9b0feb8de8cf9e4d717d5a965ee42a15497398e66b780f3e9582b15b10 1 1JtNUZBEndno4kHkHuXvbD1KBKBTWbVJUJ
f0b40f9b0feb8de8cf9e4d717d5a965ee42a15497398e66b780f3e9582b15b10 2 1F1hHnUMtgdCUqrQkDBVV8WxMJojUGnhTg

out-addresses.txt
Code:
1PtfkLyz1Pi3UFcVy8Pvv5ioS4wYxcWXCJ 0.0003
13rpg2mhVoJYxa4sgAKM1hMLJwMz1jZawp 0.0006




legendary
Activity: 3430
Merit: 3080
Is it happening because the shell is interpreting all the " and ' characters in an unexpected way?... whereas with the sendrawtransaction, you are just dealing with hex chars.

Well, I'd considered that, and have tried using the escaped quote marks (\") way also, same error. Certainly bitcoin-cli needs double quotes around $(cat filename), else space characters stop any data after a space character from being supplied to bitcoin-cli.

Like I said, the error message returns the exact sequence of characters that does work if used as a literal string, so you're probably right that there is something in that string of characters that is being handled in an unexpected way.

I've done some research on the cat command today, nothing obvious stood out as being relevant.
HCP
legendary
Activity: 2086
Merit: 4361
Is it happening because the shell is interpreting all the " and ' characters in an unexpected way?... whereas with the sendrawtransaction, you are just dealing with hex chars.
legendary
Activity: 3430
Merit: 3080
So, issues

Here's what I'm trying:

Code:
$ bitcoin-cli createrawtransaction "$(cat ./tx.json)"
error: Error parsing JSON:'''[{"txid":"notarealtxid78ahfjb73qhb54jhb6fe7f7efkjbekshc9jh38394394hddskdskflnotarealtxid","vout":0}]''' '[{"1NotARealBitcoinAddress":0.01}]'

It should work, but doesn't (sendrawtransaction does work this way, eg. bitcoin-cli sendrawtransaction $(cat hextxsavedtoafile)


However, it works if I supply the json directly to the command:

Code:
$ bitcoin-cli createrawtransaction '''[{"txid":"notarealtxid78ahfjb73qhbjhbfefefkjbekshc9jh38394394hddskdskflnotarealtxid","vout":0}]''' '[{"1NotARealBitcoinAddress":0.01}]'

That's the same json that gets rejected when trying to use "$(cat file)" to supply the json data


Is this a bug in bitcoin-cli, or am I being naive about what the cat command is actually sending to the createrawtransaction command? Seems strange that sendrawtransaction can work this way, but createrawtransaction will not.
Jump to: