Skip to content

Instantly share code, notes, and snippets.

@fivepiece
Last active November 4, 2020 01:50
Show Gist options
  • Save fivepiece/0c19166efa3fe3bc73c31d20a6a1df6e to your computer and use it in GitHub Desktop.
Save fivepiece/0c19166efa3fe3bc73c31d20a6a1df6e to your computer and use it in GitHub Desktop.
#!/bin/bash
export LANG=C.UTF-8
export LC_LANG=C.UTF-8
bhash="${1:-$(bitcoin-cli getbestblockhash)}"
hex_cb=( $( bitcoin-cli getrawtransaction $( bitcoin-cli getblock "${bhash}" | grep -m1 " \"[0-9a-f]\{64\}\"" | tr -d '", ') 1 2>/dev/null | grep coinbase | grep -o "[0-9a-f]\{3,\}" | sed -e 's/\([0-f]\{2\}\)/\1 /g' ) )
if [[ ${bhash} == '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' ]]; then
hex_cb=( $( echo '04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73' | sed -e 's/\([0-f]\{2\}\)/\1 /g' ) )
fi
echo -n "${hex_cb[0]} : $((16#${hex_cb[3]}${hex_cb[2]}${hex_cb[1]})) , "
elem_cb=()
for (( i=4,j=0; i<${#hex_cb[@]}; i++ )); do
if [[ "${hex_cb[$i]}" =~ 2f|0f ]]; then
(( j++ ))
else
elem_cb[$j]+="${hex_cb[$i]}"
fi
done
for elem in ${elem_cb[@]}; do
if [[ -z $(echo ${elem} | xxd -r -p | tr -d '[:print:]') ]]; then
echo -n "${elem}" | xxd -r -p | tr -d '\n\t\r\v\f\a'
elif [[ -n $(echo ${elem} | xxd -r -p | tr -d -C '[:print:]') ]]; then
echo -n "${elem}" | xxd -r -p | tr -d -C '[:print:]' # | tr -d '\n\t\r\v\f\a'
else
echo -n "${elem}"
fi
echo -n '/'
done
echo
#echo ${hex_cb[@]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment