3

I am simulating a bitcoin network on my machine for which I have written a bash script to simulate the transactions between the nodes. When I use the sendmany option of bitcoin-cli to send transactions I am getting an json parsing error reported by bitcoin-cli.

Code:

#!/bin/bash
MAX_NODES=2
MY_PATH=/home/ubuntu/test
CLIENT=/usr/local/bin/bitcoin-cli

declare -a addr

function fcomp() {
        /usr/bin/awk -v n1=$1 -v n2=$2 'BEGIN{ if (n1>n2) exit 0; exit 1}'
}

json="'{"

#get addresses to send
for ((i = 1; i <= MAX_NODES; i++));
do
    addr[$i-1]="$($CLIENT -regtest -rpcport=$((16500 + $i)) -datadir=$MY_PATH/$i/ getnewaddress myaccount)"
    if [ "$i" -lt "$MAX_NODES" ]
    then
        json="$json\"${addr[$i-1]}\":0.00001, "
    else
        json="$json\"${addr[$i-1]}\":0.00001"
    fi
done

json="$json}'"
echo $json

#loop to send money to other nodes
for ((i = 1; i <= MAX_NODES; i++));
do
        balance=`$CLIENT -regtest -rpcport=$((16500 + $i)) -datadir=$MY_PATH/$i/ getbalance`
        if fcomp $balance 0.002; then
                $CLIENT -regtest -rpcport=$((16500 + $i)) -datadir=$MY_PATH/$i sendmany myaccount $json
    fi
done

echo json output:

'{"mj2FrDhEomSzyQtRoGY78oVRPcQs5L5o95":0.00001, "mkxnkT3kx9dsFS8V3qYydpL1o5F5MfwCvM":0.00001}'

This gives me an error as:

error: Error parsing JSON:'{"mj2FrDhEomSzyQtRoGY78oVRPcQs5L5o95":0.00001,

I tried all possible combinations of quotes, double quotes and escape sequences but failed. If I copy paste the output of echo $json to a manual bitcoin-cli sendmany command it works perfectly fine.

1
  • 1) Are you using the same shell to run the when you're copy/pasting as when you're using the script? 2) Try removing the space after the comma on the line json="$json\"${addr[$i-1]}\":0.00001, " That seemed to help for me.
    – Nick ODell
    Nov 6, 2015 at 11:55

1 Answer 1

1

If this is your echo output:

'{"mj2FrDhEomSzyQtRoGY78oVRPcQs5L5o95":0.00001, "mkxnkT3kx9dsFS8V3qYydpL1o5F5MfwCvM":0.00001}'

Then you have too many quotes. When you run echo, you should see:

{"mj2FrDhEomSzyQtRoGY78oVRPcQs5L5o95":0.00001, "mkxnkT3kx9dsFS8V3qYydpL1o5F5MfwCvM":0.00001}

That's because the shell strips off the outer set of quotes; it does that when you send the JSON to bitcoin-cli too. Because you have the quotes there in the echo output, it implies that they're there in the bitcoin-cli command, so what you're sending Bitcoin Core looks like a JSON string rather than a JSON object.


This isn't asked in your question, but you might also want to look into the Debian/Ubuntu packages and commands named jshon and jq. These help you process JSON in shell scripts. Because they understand JSON, they tend to be a lot more robust the sed and awk. I don't know if you need them for this script, however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.