-
-
Save wileyj/55d9d400a3ca25619b6a60a8116b1d37 to your computer and use it in GitHub Desktop.
Tools for dealing with blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
API_BASE="https://api.hiro.so" | |
# Check if HIRO_API_KEY is set | |
if [ -n "$HIRO_API_KEY" ]; then | |
AUTH_HEADER="-H \"Authorization: Bearer $HIRO_API_KEY\"" | |
else | |
AUTH_HEADER="" | |
fi | |
usage() { | |
echo "Usage: $0 <command> <argument>" | |
echo "Commands:" | |
echo " c2b <consensus_hash> - Get burn block height from consensus hash" | |
echo " b2c <burn_height> - Get consensus hash from burn chain height" | |
echo " block <hash_or_height> - Get Stacks block info" | |
exit 1 | |
} | |
consensus2burn() { | |
local consensus_hash=$1 | |
[ -z "$consensus_hash" ] && usage | |
eval "curl -s $AUTH_HEADER \"$API_BASE/v3/sortitions/consensus/$consensus_hash\"" | \ | |
jq -r '.[] | .burn_block_height' | |
} | |
burn2consensus() { | |
local burn_height=$1 | |
[ -z "$burn_height" ] && usage | |
eval "curl -s $AUTH_HEADER \"$API_BASE/v3/sortitions/burn_height/$burn_height\"" | \ | |
jq -r '.[] | .consensus_hash' | sed 's/0x//' | |
} | |
blockinfo() { | |
local lookup=$1 | |
[ -z "$lookup" ] && usage | |
eval "curl -s $AUTH_HEADER \"$API_BASE/extended/v2/blocks/$lookup\"" | \ | |
jq -r '"Stacks height: \(.height)\n" + | |
"Stacks hash: \(.hash | sub("0x"; ""))\n" + | |
"Index hash: \(.index_block_hash | sub("0x"; ""))\n" + | |
"Burn height: \(.burn_block_height)\n" + | |
"Burn hash: \(.burn_block_hash | sub("0x"; ""))\n" + | |
"Burn timestamp: \(.burn_block_time_iso)"' | |
} | |
case $1 in | |
c2b) | |
consensus2burn "$2" | |
;; | |
b2c) | |
burn2consensus "$2" | |
;; | |
block) | |
blockinfo "$2" | |
;; | |
*) | |
usage | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment