Bitcoin Forum
April 23, 2024, 09:10:19 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: bitcoin-cli: createrawtransaction using data files as input  (Read 250 times)
Carlton Banks (OP)
Legendary
*
Offline Offline

Activity: 3430
Merit: 3071



View Profile
December 01, 2018, 01:56:20 AM
Last edit: December 01, 2018, 11:08:49 AM by Carlton Banks
 #1

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.

Vires in numeris
1713906619
Hero Member
*
Offline Offline

Posts: 1713906619

View Profile Personal Message (Offline)

Ignore
1713906619
Reply with quote  #2

1713906619
Report to moderator
"If you don't want people to know you're a scumbag then don't be a scumbag." -- margaritahuyan
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1713906619
Hero Member
*
Offline Offline

Posts: 1713906619

View Profile Personal Message (Offline)

Ignore
1713906619
Reply with quote  #2

1713906619
Report to moderator
1713906619
Hero Member
*
Offline Offline

Posts: 1713906619

View Profile Personal Message (Offline)

Ignore
1713906619
Reply with quote  #2

1713906619
Report to moderator
HCP
Legendary
*
Offline Offline

Activity: 2086
Merit: 4316

<insert witty quote here>


View Profile
December 01, 2018, 03:44:07 AM
Merited by Carlton Banks (5)
 #2

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.

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
BC.GAME
▄▄░░░▄▀▀▄████████
▄▄▄
██████████████
█████░░▄▄▄▄████████
▄▄▄▄▄▄▄▄▄██▄██████▄▄▄▄████
▄███▄█▄▄██████████▄████▄████
███████████████████████████▀███
▀████▄██▄██▄░░░░▄████████████
▀▀▀█████▄▄▄███████████▀██
███████████████████▀██
███████████████████▄██
▄███████████████████▄██
█████████████████████▀██
██████████████████████▄
.
..CASINO....SPORTS....RACING..
█░░░░░░█░░░░░░█
▀███▀░░▀███▀░░▀███▀
▀░▀░░░░▀░▀░░░░▀░▀
░░░░░░░░░░░░
▀██████████
░░░░░███░░░░
░░█░░░███▄█░░░
░░██▌░░███░▀░░██▌
░█░██░░███░░░█░██
░█▀▀▀█▌░███░░█▀▀▀█▌
▄█▄░░░██▄███▄█▄░░▄██▄
▄███▄
░░░░▀██▄▀


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
Carlton Banks (OP)
Legendary
*
Offline Offline

Activity: 3430
Merit: 3071



View Profile
December 01, 2018, 11:06:13 AM
 #3

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.

Vires in numeris
arulbero
Legendary
*
Offline Offline

Activity: 1914
Merit: 2071


View Profile
December 01, 2018, 01:49:51 PM
Last edit: December 01, 2018, 03:23:08 PM by arulbero
Merited by Carlton Banks (5), HCP (1)
 #4

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




arulbero
Legendary
*
Offline Offline

Activity: 1914
Merit: 2071


View Profile
December 01, 2018, 03:20:34 PM
Merited by Carlton Banks (5), HCP (1)
 #5

... 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}
Carlton Banks (OP)
Legendary
*
Offline Offline

Activity: 3430
Merit: 3071



View Profile
December 01, 2018, 09:54:42 PM
Merited by elliottflz65 (2), HCP (1)
 #6

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

Vires in numeris
HCP
Legendary
*
Offline Offline

Activity: 2086
Merit: 4316

<insert witty quote here>


View Profile
December 02, 2018, 01:34:45 AM
 #7

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

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
BC.GAME
▄▄░░░▄▀▀▄████████
▄▄▄
██████████████
█████░░▄▄▄▄████████
▄▄▄▄▄▄▄▄▄██▄██████▄▄▄▄████
▄███▄█▄▄██████████▄████▄████
███████████████████████████▀███
▀████▄██▄██▄░░░░▄████████████
▀▀▀█████▄▄▄███████████▀██
███████████████████▀██
███████████████████▄██
▄███████████████████▄██
█████████████████████▀██
██████████████████████▄
.
..CASINO....SPORTS....RACING..
█░░░░░░█░░░░░░█
▀███▀░░▀███▀░░▀███▀
▀░▀░░░░▀░▀░░░░▀░▀
░░░░░░░░░░░░
▀██████████
░░░░░███░░░░
░░█░░░███▄█░░░
░░██▌░░███░▀░░██▌
░█░██░░███░░░█░██
░█▀▀▀█▌░███░░█▀▀▀█▌
▄█▄░░░██▄███▄█▄░░▄██▄
▄███▄
░░░░▀██▄▀


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!