You can take advantage of Blockchain.com's API (formerly Blockchain.info).
You can access BTC blocks based on their block height via a URL of the form https://blockchain.info/rawblock/<height+1>
. We use <height+1>
because 1 is used as the index of the genesis block, which is instead usually referred to as block 0, as it is defined to have a height of 0.
By default, this endpoint provides a JSON dump of the data in the block, but you can get the raw block data as a hexadecimal string instead by appending the query string ?format=hex
. For example, https://blockchain.info/rawblock/2?format=hex
will give you the data in the block after the genesis block (the block with height 1) as a hexadecimal string.
The block header is 80 bytes, or 160 nibbles (hexadecimal characters), so you can e.g. use curl
to fetch the data, and then head
to truncate it to just the header, and then save that hex string to a file:
curl 'https://blockchain.info/rawblock/2?format=hex' | head -c 160 > block-1.txt
You can use a simple Bash script to automate this for all blocks:
#!/bin/bash
latest_block_height=670288
for ((i=0; i <= latest_block_height; i++)); do
if ! [ -f "block-$i.txt" ]; then
curl 'https://blockchain.info/rawblock/'"$(($i + 1))"'?format=hex' | head -c 160 > "block-$i.txt"
fi
echo "Downloaded block $i."
done
version = xxx, prevHash = xxx, ...
? – Jivan Pal Feb 12 at 13:36