Since I do think this is
useful to someone curious about touching an electrumx server, crossposted below.
Been wondering how to make use of the electrumx server, but in bash. First I needed to connect to the electrumx server.
#!/bin/bash
echo "get block header"
(echo '{"method" : "blockchain.block.header", "params": ["1"], "id": "msg_id"}'; sleep 1) | ncat --ssl electrumx1.denarius.pro 50002
This outputs.
block header
{"jsonrpc": "2.0", "result": "06000000cd8f82c4c28201fd89def0dba541d66432bff9c1bb16fc1c6201dabb5d0d0000e4abd1522f390c5a2b83add7c8d29b875e1e0691dc43267f0406e9ddfea329c76ab74159ffff0f1e000d8425", "id": "msg_id"}
So now to get a balance. The thing with electrumx server is that you need a scripthash to call any address functions. But I don't have that. All I have is a bash terminal and a Denarius address. So what do I do? Line by line dissect how people create an address and then work backwards to the scripthash.
I need this image, but down to up. source:Â
https://learnmeabitcoin.com/guide/p2pkh This is not so easy as I can't find anyone doing this backwards in bash. But I found lots of posts how to go forwards.
Here is how to get it before converting to big endian.
#!/bin/bash
. denarius.sh
#DUP HASH160
begin="76A914"
echo $begin
#EQUALVERIFY CHECKSIG
end="88AC"
echo $end
echo "decodeBase58"
decoded="$(decodeBase58 DCMRvR6MUppPgP8vrMKuni4FL5de8SjicG)"
echo "Remove 00 bytes"
echo ${decoded#??}
removefront=$(echo "${decoded#??}")
echo $removefront
echo "Remove checksum"
removeback=$(echo "${removefront%????????}")
echo "95 characters base58"
echo $removeback
echo "now what?"
echo $begin$removeback$end
echo "convert to scripthash"
echo -n $begin$removeback$end | xxd -r -p | sha256sum | cut -d' ' -f1
scripthash=$(echo -n $begin$removeback$end | xxd -r -p | sha256sum | cut -d' ' -f1)
echo $scripthash
Will also need grondilu bitcoin-bash-tools.Â
https://github.com/grondilu/bitcoin-bash-tools Get the bitcoin.sh and throw that into the same directory you are going to test this out in, for obvious reasons I renamed bitcoin.sh to denarius.sh.
First I am assuming a standard P2PKH address with OP_DUP OP_HASH160 hashedpublickey OP_EQUALVERIFY OP_CHECKSIG
#DUP HASH160
begin="76A914"
echo $begin
#EQUALVERIFY CHECKSIG
end="88AC"
echo $end
Then decodeBase58 of our Denarius address, works on bitcoin obviously.
echo "decodeBase58"
decoded="$(decodeBase58 DCMRvR6MUppPgP8vrMKuni4FL5de8SjicG)"
Then remove 00 bytes from the front of this. The #?? removes 2 characters from the left of a string.
echo "Remove 00 bytes"
echo ${decoded#??}
removefront=$(echo "${decoded#??}")
echo $removefront
Now remove the checksum. The %?
? removes 8 characters from the right of the string.
echo "Remove checksum"
removeback=$(echo "${removefront%????????}")
echo "95 characters base58"
echo $removeback
Now what? How to convert to scripthash? We can echo the the uncompressed public key, kind of.
echo "now what?"
echo $begin$removeback$end
echo "convert to scripthash"
sha256 this in proper format, using xxd and then sha256 that.
echo -n $begin$removeback$end | xxd -r -p | sha256sum | cut -d' ' -f1
scripthash=$(echo -n $begin$removeback$end | xxd -r -p | sha256sum | cut -d' ' -f1)
echo $scripthash
At the end from that address I get.
3735fe8239322122222f8a8f42d9b6545bc517e2086116e57953fdc9cc7f6115
For now I have a separate function I found to convert to big endian, since electrumx server wants it this way.
source:Â
https://electrumx.readthedocs.io/en/latest/protocol-basics.html#script-hashes#!/bin/bash
#echo 6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b | ./bigendian.sh
#8b01df4e368ea28f8dc0423bcf7a4923e3a12d307c875e47a0cfbf90b5c39161
# check stdin
if [ -t 0 ]; then exit; fi
v=`cat /dev/stdin`
i=${#v}
while [ $i -gt 0 ]
do
i=$[$i-2]
echo -n ${v:$i:2}
done
echo
I run this like
echo 3735fe8239322122222f8a8f42d9b6545bc517e2086116e57953fdc9cc7f6115 | ./bigendian.sh
and get the scripthash electrumx server wants
15617fccc9fd5379e5166108e217c55b54b6d9428f8a2f222221323982fe3537
Like bash magic we can use that scripthash to talk to the electrumx server to get a balance.
#!/bin/bash
echo "get balance"
(echo '{"method" : "blockchain.scripthash.get_balance", "params": ["15617fccc9fd5379e5166108e217c55b54b6d9428f8a2f222221323982fe3537"], "id": "msg_id"}'; sleep 1) | ncat --ssl electrumx1.denarius.pro 50002
And we get the balance.
get balance
{"jsonrpc": "2.0", "result": {"confirmed": 125740854, "unconfirmed": 0}, "id": "msg_id"}
And a large step forward in creating a basic terminal wallet for any device that can use bash, ncat and has port 50002 open.