-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added script to fetch tx hasg from block height & tx index
- Loading branch information
1 parent
8a3c954
commit 15decee
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/bin/bash | ||
# | ||
# This script fetches a transaction hash using block height and transaction index | ||
# Usage: ./get_tx_from_block.sh <block_height> <tx_index> | ||
|
||
if [ "$#" -ne 2 ]; then | ||
echo "Usage: $0 <block_height> <tx_index>" | ||
echo "Example: $0 869322 1" | ||
exit 1 | ||
fi | ||
|
||
BLOCK_HEIGHT=$1 | ||
TX_INDEX=$2 | ||
|
||
RPC_API="https://bitcoin-mainnet.public.blastapi.io" | ||
|
||
# First, get the block hash for the given height | ||
BLOCK_HASH_RES=$(curl -s -X POST -H "Content-Type: application/json" \ | ||
-d "{\"jsonrpc\":\"1.0\",\"id\":0,\"method\":\"getblockhash\",\"params\":[$BLOCK_HEIGHT]}" \ | ||
$RPC_API) | ||
|
||
BLOCK_HASH=$(echo $BLOCK_HASH_RES | jq -r '.result') | ||
|
||
if [ -z "$BLOCK_HASH" ] || [ "$BLOCK_HASH" = "null" ]; then | ||
echo "Error: Could not fetch block hash for height $BLOCK_HEIGHT" | ||
exit 1 | ||
fi | ||
|
||
# Then, get the block data which includes all transaction hashes | ||
BLOCK_DATA_RES=$(curl -s -X POST -H "Content-Type: application/json" \ | ||
-d "{\"jsonrpc\":\"1.0\",\"id\":0,\"method\":\"getblock\",\"params\":[\"$BLOCK_HASH\"]}" \ | ||
$RPC_API) | ||
|
||
# Extract the transaction hash at the specified index | ||
TX_HASH=$(echo $BLOCK_DATA_RES | jq -r ".result.tx[$TX_INDEX]") | ||
|
||
if [ -z "$TX_HASH" ] || [ "$TX_HASH" = "null" ]; then | ||
echo "Error: Could not find transaction at index $TX_INDEX in block $BLOCK_HEIGHT" | ||
exit 1 | ||
fi | ||
|
||
echo $TX_HASH |