#!/bin/bash # BSV Bible Reader - Read the Bible directly from the BSV blockchain # Works on Linux/Mac with curl and jq installed # # Usage: ./read-bible.sh "Genesis" 1 # Usage: ./read-bible.sh "John" 3 # Usage: ./read-bible.sh --list-books # # Requirements: curl, jq, xxd # Install jq: brew install jq (Mac) or apt install jq (Linux) set -e # Colors CYAN='\033[0;36m' GREEN='\033[0;32m' YELLOW='\033[1;33m' GRAY='\033[0;90m' NC='\033[0m' # No Color # Master index transaction ID MASTER_INDEX_TXID="cf53cae0cf36ffe8333c7ada3416e897ae8ff11d6acc7c1905cabd725a089157" # Function to fetch transaction from WhatsOnChain fetch_tx() { local txid=$1 curl -s "https://api.whatsonchain.com/v1/bsv/main/tx/${txid}" } # Function to decode OP_RETURN data from transaction decode_op_return() { local tx_json=$1 # Get the OP_RETURN hex (output with value 0) local hex=$(echo "$tx_json" | jq -r '.vout[] | select(.value == 0) | .scriptPubKey.hex') if [ -z "$hex" ]; then echo "No OP_RETURN found" >&2 return 1 fi # Parse the script local pos=0 # Skip OP_FALSE (00) if present if [ "${hex:$pos:2}" == "00" ]; then pos=$((pos + 2)) fi # Skip OP_RETURN (6a) if [ "${hex:$pos:2}" == "6a" ]; then pos=$((pos + 2)) fi # Skip protocol prefix pushdata (BIBLE1) local prefix_push=$((16#${hex:$pos:2})) pos=$((pos + 2)) if [ $prefix_push -le 75 ]; then pos=$((pos + prefix_push * 2)) elif [ $prefix_push -eq 76 ]; then local len=$((16#${hex:$pos:2})) pos=$((pos + 2 + len * 2)) fi # Read data length pushdata local data_push=$((16#${hex:$pos:2})) pos=$((pos + 2)) local data_len=0 if [ $data_push -le 75 ]; then data_len=$data_push elif [ $data_push -eq 76 ]; then # OP_PUSHDATA1 data_len=$((16#${hex:$pos:2})) pos=$((pos + 2)) elif [ $data_push -eq 77 ]; then # OP_PUSHDATA2 (little-endian) local low=$((16#${hex:$pos:2})) local high=$((16#${hex:$((pos+2)):2})) data_len=$((low + high * 256)) pos=$((pos + 4)) elif [ $data_push -eq 78 ]; then # OP_PUSHDATA4 (little-endian) local b0=$((16#${hex:$pos:2})) local b1=$((16#${hex:$((pos+2)):2})) local b2=$((16#${hex:$((pos+4)):2})) local b3=$((16#${hex:$((pos+6)):2})) data_len=$((b0 + b1 * 256 + b2 * 65536 + b3 * 16777216)) pos=$((pos + 8)) fi # Extract the hex data and decode to UTF-8 local data_hex=${hex:$pos:$((data_len * 2))} echo "$data_hex" | xxd -r -p } # Show usage show_usage() { echo -e "${GREEN}=== BSV BIBLE READER ===${NC}" echo "Read the King James Bible directly from the BSV blockchain." echo "" echo -e "${YELLOW}Usage:${NC}" echo " ./read-bible.sh \"Genesis\" 1 # Read Genesis chapter 1" echo " ./read-bible.sh \"John\" 3 # Read John chapter 3" echo " ./read-bible.sh --list-books # List all books" echo " ./read-bible.sh \"Psalms\" 23 --raw # Output raw JSON" echo "" echo -e "${CYAN}Source: https://bsvbible.club${NC}" echo -e "${GRAY}Master Index TxId: ${MASTER_INDEX_TXID}${NC}" } # List available books list_books() { echo -e "\n${GREEN}=== BSV BIBLE - Books Available ===${NC}" echo -e "\n${YELLOW}OLD TESTAMENT:${NC}" echo "Genesis, Exodus, Leviticus, Numbers, Deuteronomy, Joshua, Judges, Ruth," echo "1 Samuel, 2 Samuel, 1 Kings, 2 Kings, 1 Chronicles, 2 Chronicles," echo "Ezra, Nehemiah, Esther, Job, Psalms, Proverbs, Ecclesiastes," echo "Song of Solomon, Isaiah, Jeremiah, Lamentations, Ezekiel, Daniel," echo "Hosea, Joel, Amos, Obadiah, Jonah, Micah, Nahum, Habakkuk," echo "Zephaniah, Haggai, Zechariah, Malachi" echo -e "\n${YELLOW}NEW TESTAMENT:${NC}" echo "Matthew, Mark, Luke, John, Acts, Romans, 1 Corinthians, 2 Corinthians," echo "Galatians, Ephesians, Philippians, Colossians, 1 Thessalonians," echo "2 Thessalonians, 1 Timothy, 2 Timothy, Titus, Philemon, Hebrews," echo "James, 1 Peter, 2 Peter, 1 John, 2 John, 3 John, Jude, Revelation" echo -e "\n${CYAN}Usage: ./read-bible.sh \"John\" 3${NC}" } # Check dependencies check_deps() { for cmd in curl jq xxd; do if ! command -v $cmd &> /dev/null; then echo "Error: $cmd is required but not installed." >&2 if [ "$cmd" == "jq" ]; then echo "Install with: brew install jq (Mac) or apt install jq (Linux)" >&2 fi exit 1 fi done } # Main check_deps if [ "$1" == "--list-books" ] || [ "$1" == "-l" ]; then list_books exit 0 fi if [ -z "$1" ] || [ -z "$2" ]; then show_usage exit 0 fi BOOK=$1 CHAPTER=$2 RAW=false if [ "$3" == "--raw" ] || [ "$3" == "-r" ]; then RAW=true fi echo -e "\n${GREEN}=== BSV BIBLE ===${NC}" echo -e "${CYAN}Fetching chapter index...${NC}" # Fetch the transaction index TX_INDEX=$(curl -s "https://bsvbible.club/tx-index.json") CHAPTER_KEY="${BOOK}_${CHAPTER}" TXID=$(echo "$TX_INDEX" | jq -r ".\"${CHAPTER_KEY}\"") if [ "$TXID" == "null" ] || [ -z "$TXID" ]; then echo -e "${YELLOW}Chapter not found: ${CHAPTER_KEY}${NC}" >&2 echo "Use --list-books to see available books" >&2 exit 1 fi echo -e "${GREEN}Found: ${CHAPTER_KEY}${NC}" echo -e "${GRAY}TxId: ${TXID}${NC}" echo -e "${CYAN}Fetching from blockchain...${NC}" # Fetch the transaction TX_JSON=$(fetch_tx "$TXID") # Decode the OP_RETURN data DATA=$(decode_op_return "$TX_JSON") if [ "$RAW" == true ]; then echo "$DATA" exit 0 fi # Parse and display BOOK_NAME=$(echo "$DATA" | jq -r '.book') CHAPTER_NUM=$(echo "$DATA" | jq -r '.chapter') echo -e "\n${YELLOW}=== ${BOOK_NAME} Chapter ${CHAPTER_NUM} ===${NC}" echo -e "${GRAY}Source: BSV Blockchain (immutable)${NC}" echo -e "${GRAY}Verify: https://whatsonchain.com/tx/${TXID}${NC}\n" # Display verses echo "$DATA" | jq -r '.verses | to_entries[] | "\(.key + 1) \(.value)"' | while read -r line; do VERSE_NUM=$(echo "$line" | cut -d' ' -f1) VERSE_TEXT=$(echo "$line" | cut -d' ' -f2-) echo -e "${CYAN}${VERSE_NUM}${NC} ${VERSE_TEXT}" done echo -e "\n${GRAY}[End of ${BOOK_NAME} ${CHAPTER_NUM}]${NC}"