Last active
May 23, 2025 20:27
-
-
Save obycode/d28d247c464b8b5bb1cb92b9aa25f40f 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 | |
set -euo pipefail | |
IFS=$'\n\t' | |
API_BASE="https://api.hiro.so" | |
declare -a CURL_OPTS=(-sS --fail) | |
if [[ -n "${HIRO_API_KEY:-}" ]]; then | |
CURL_OPTS+=( -H "Authorization: Bearer $HIRO_API_KEY" ) | |
fi | |
http_get() { | |
local path=$1 | |
curl "${CURL_OPTS[@]}" "$API_BASE$path" | |
} | |
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 | |
} | |
while getopts "h" opt; do | |
case $opt in | |
h) usage ;; | |
*) usage ;; | |
esac | |
done | |
shift $((OPTIND -1)) | |
consensus2burn() { | |
local consensus_hash=$1 | |
[ -z "$consensus_hash" ] && usage | |
http_get "/v3/sortitions/consensus/$consensus_hash" | jq -r '.[] | .burn_block_height' | |
} | |
burn2consensus() { | |
local burn_height=$1 | |
[ -z "$burn_height" ] && usage | |
http_get "/v3/sortitions/burn_height/$burn_height" | jq -r '.[] | .consensus_hash | sub("^0x"; "")' | |
} | |
blockinfo() { | |
local lookup=$1 | |
[ -z "$lookup" ] && usage | |
http_get "/extended/v2/blocks/$lookup" | jq -r ' | |
"Stacks height: \(.height)", | |
"Stacks hash: \(.hash | sub("^0x"; ""))", | |
"Index hash: \(.index_block_hash | sub("^0x"; ""))", | |
"Burn height: \(.burn_block_height)", | |
"Burn hash: \(.burn_block_hash | sub("^0x"; ""))", | |
"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