citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

btc-fetchblocks (833B)


      1 #!/usr/bin/env bash
      2 
      3 BITCOIN_RPCUSER=rpcuser
      4 BITCOIN_RPCPASS=rpcpass
      5 BITCOIN_RPCPORT=${BITCOIN_RPCPORT:-8332}
      6 BITCOIN_HOST=${BITCOIN_HOST:-127.0.0.1}
      7 
      8 mkreq () {
      9     (
     10         printf '['
     11         echo ${1#","}
     12         printf ']'
     13     )
     14 }
     15 
     16 doreq() {
     17     local host="http://${BITCOIN_HOST}:${BITCOIN_RPCPORT}"
     18     mkreq "$1" |
     19     curl -s -u $BITCOIN_RPCUSER:$BITCOIN_RPCPASS \
     20         --data-binary @- \
     21         -H 'content-type: text/plain;' "$host"
     22 }
     23 
     24 make_hash_reqs() {
     25         xargs printf ',{"jsonrpc": "1.0", "id":"blockhashes", "method": "getblockhash", "params": [%d] }\n'
     26 }
     27 
     28 make_block_reqs() {
     29         xargs printf ',{"jsonrpc": "1.0", "id":"blocks", "method": "getblock", "params": ["%s", 2] }\n'
     30 }
     31 
     32 block_reqs=$(
     33 doreq "$(make_hash_reqs)" |
     34 jq -cr '.[].result' |
     35 make_block_reqs
     36 )
     37 
     38 doreq "$block_reqs" | jq -cr '.[].result'