# BSV Bible Reader - Read the Bible directly from the blockchain # Usage: .\read-bible.ps1 "Genesis" 1 # Usage: .\read-bible.ps1 "John" 3 # Usage: .\read-bible.ps1 -ListBooks param( [string]$Book, [int]$Chapter, [switch]$ListBooks, [switch]$Raw ) # Master index transaction ID (contains all chapter -> txid mappings) $MasterIndexTxId = "cf53cae0cf36ffe8333c7ada3416e897ae8ff11d6acc7c1905cabd725a089157" # Function to fetch transaction from WhatsOnChain function Get-BsvTransaction { param([string]$TxId) $url = "https://api.whatsonchain.com/v1/bsv/main/tx/$TxId" $response = Invoke-WebRequest -Uri $url -UseBasicParsing return $response.Content | ConvertFrom-Json } # Function to decode hex to string function Convert-HexToString { param([string]$Hex) $bytes = [byte[]]::new($Hex.Length / 2) for ($i = 0; $i -lt $Hex.Length; $i += 2) { $bytes[$i / 2] = [Convert]::ToByte($Hex.Substring($i, 2), 16) } return [System.Text.Encoding]::UTF8.GetString($bytes) } # Function to extract OP_RETURN data from transaction function Get-OpReturnData { param($Transaction) foreach ($vout in $Transaction.vout) { if ($vout.value -eq 0 -and $vout.scriptPubKey.hex) { $hex = $vout.scriptPubKey.hex # Skip OP_FALSE (00) if present $pos = 0 if ($hex.Substring($pos, 2) -eq "00") { $pos += 2 } # Skip OP_RETURN (6a) if ($hex.Substring($pos, 2) -eq "6a") { $pos += 2 } # Skip protocol prefix pushdata (BIBLE1) $prefixPush = [Convert]::ToInt32($hex.Substring($pos, 2), 16) $pos += 2 if ($prefixPush -le 0x4b) { $pos += $prefixPush * 2 } elseif ($prefixPush -eq 0x4c) { $len = [Convert]::ToInt32($hex.Substring($pos, 2), 16) $pos += 2 + $len * 2 } # Read data length $dataPush = [Convert]::ToInt32($hex.Substring($pos, 2), 16) $pos += 2 if ($dataPush -le 0x4b) { $dataLen = $dataPush } elseif ($dataPush -eq 0x4c) { $dataLen = [Convert]::ToInt32($hex.Substring($pos, 2), 16) $pos += 2 } elseif ($dataPush -eq 0x4d) { $low = [Convert]::ToInt32($hex.Substring($pos, 2), 16) $high = [Convert]::ToInt32($hex.Substring($pos + 2, 2), 16) $dataLen = $low + ($high -shl 8) $pos += 4 } elseif ($dataPush -eq 0x4e) { $b0 = [Convert]::ToInt32($hex.Substring($pos, 2), 16) $b1 = [Convert]::ToInt32($hex.Substring($pos + 2, 2), 16) $b2 = [Convert]::ToInt32($hex.Substring($pos + 4, 2), 16) $b3 = [Convert]::ToInt32($hex.Substring($pos + 6, 2), 16) $dataLen = $b0 + ($b1 -shl 8) + ($b2 -shl 16) + ($b3 -shl 24) $pos += 8 } # Extract and decode the data $dataHex = $hex.Substring($pos, $dataLen * 2) return Convert-HexToString -Hex $dataHex } } return $null } # Fetch and cache the master index function Get-MasterIndex { Write-Host "Fetching master index from blockchain..." -ForegroundColor Cyan $tx = Get-BsvTransaction -TxId $MasterIndexTxId $data = Get-OpReturnData -Transaction $tx $json = $data | ConvertFrom-Json return $json } # Main logic if ($ListBooks) { Write-Host "`n=== BSV BIBLE - Books Available ===" -ForegroundColor Green Write-Host "`nOLD TESTAMENT:" -ForegroundColor Yellow Write-Host "Genesis, Exodus, Leviticus, Numbers, Deuteronomy, Joshua, Judges, Ruth," Write-Host "1 Samuel, 2 Samuel, 1 Kings, 2 Kings, 1 Chronicles, 2 Chronicles," Write-Host "Ezra, Nehemiah, Esther, Job, Psalms, Proverbs, Ecclesiastes," Write-Host "Song of Solomon, Isaiah, Jeremiah, Lamentations, Ezekiel, Daniel," Write-Host "Hosea, Joel, Amos, Obadiah, Jonah, Micah, Nahum, Habakkuk," Write-Host "Zephaniah, Haggai, Zechariah, Malachi" Write-Host "`nNEW TESTAMENT:" -ForegroundColor Yellow Write-Host "Matthew, Mark, Luke, John, Acts, Romans, 1 Corinthians, 2 Corinthians," Write-Host "Galatians, Ephesians, Philippians, Colossians, 1 Thessalonians," Write-Host "2 Thessalonians, 1 Timothy, 2 Timothy, Titus, Philemon, Hebrews," Write-Host "James, 1 Peter, 2 Peter, 1 John, 2 John, 3 John, Jude, Revelation" Write-Host "`nUsage: .\read-bible.ps1 `"John`" 3" -ForegroundColor Cyan exit } if (-not $Book -or -not $Chapter) { Write-Host "`n=== BSV BIBLE READER ===" -ForegroundColor Green Write-Host "Read the King James Bible directly from the BSV blockchain.`n" Write-Host "Usage:" -ForegroundColor Yellow Write-Host " .\read-bible.ps1 `"Genesis`" 1 # Read Genesis chapter 1" Write-Host " .\read-bible.ps1 `"John`" 3 # Read John chapter 3" Write-Host " .\read-bible.ps1 -ListBooks # List all books" Write-Host " .\read-bible.ps1 `"Psalms`" 23 -Raw # Output raw JSON`n" Write-Host "Source: https://bsvbible.club" -ForegroundColor Cyan Write-Host "Master Index TxId: $MasterIndexTxId" -ForegroundColor DarkGray exit } # Known chapter transaction IDs (subset - you can expand this or fetch from master index) # For now, let's fetch from the tx-index.json on the site Write-Host "`n=== BSV BIBLE ===" -ForegroundColor Green Write-Host "Fetching chapter index..." -ForegroundColor Cyan try { $indexUrl = "https://bsvbible.club/tx-index.json" $indexResponse = Invoke-WebRequest -Uri $indexUrl -UseBasicParsing $txIndex = $indexResponse.Content | ConvertFrom-Json $chapterKey = "${Book}_${Chapter}" $txId = $txIndex.$chapterKey if (-not $txId) { Write-Host "Chapter not found: $chapterKey" -ForegroundColor Red Write-Host "Use -ListBooks to see available books" -ForegroundColor Yellow exit 1 } Write-Host "Found: $chapterKey" -ForegroundColor Green Write-Host "TxId: $txId" -ForegroundColor DarkGray Write-Host "Fetching from blockchain..." -ForegroundColor Cyan $tx = Get-BsvTransaction -TxId $txId $data = Get-OpReturnData -Transaction $tx if ($Raw) { Write-Host $data exit } $json = $data | ConvertFrom-Json Write-Host "`n=== $($json.book) Chapter $($json.chapter) ===" -ForegroundColor Yellow Write-Host "Source: BSV Blockchain (immutable)" -ForegroundColor DarkGray Write-Host "Verify: https://whatsonchain.com/tx/$txId`n" -ForegroundColor DarkGray $verseNum = 1 foreach ($verse in $json.verses) { Write-Host "$verseNum " -ForegroundColor Cyan -NoNewline Write-Host $verse $verseNum++ } Write-Host "`n[End of $($json.book) $($json.chapter)]" -ForegroundColor DarkGray } catch { Write-Host "Error: $_" -ForegroundColor Red exit 1 }