Sunday, June 24, 2018

Sven Mollinga

Sven Mollinga

This is my personal tech blog. This is the place where I put up updates about my current projects.

A little project of mine to read the Bitcoin chain. You can read every block directly from the binary files of the blockchain.

Git repo:
Programming Language: Perl
Operating system: Debian/Ubuntu

How to install

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

git clone
cd FollowtheChain
perl {bitcoin file}

As an example you can use the bitcoin.part file. This is a 6 MB export from the blk00065.dat of the chain.

perl bitcoin.part

How does it work?

A block is made up from two parts. The block header and the transactions. Because the chain is a binary file you need to know what to expect from the next bytes. I found the next two images from the bitcoin wiki. This was a start to setup the reader. I started with the block header.

Source =

Every block starts with the same Magic Number f9beb4d9. I use this to start reading the block. From this point the block header is pretty easy to read. It’ s all a fixed size. The real trouble starts with the transaction counter. This uses a varint (Variable Integer). I was quite difficult to find the right values for this. I ended up with the following possibilities.

fd = 2 bytes
fe = 4 bytes
ff = 8 bytes
if it isn’t one of these outputs the program wil use the variable itself.

Source =

The transaction itself is build up from inputs and outputs. A loop will go through every input/ouput and print out variables like the script and the amount of Bitcoin. I’ve enabled the option to show hex to ascii of the first (Genesis) block in the example below. This can be enabled in the program itself. It shows a message from the builder of the Bitcoin.

Output of the program

I’ve used the program on the genesis block and it printed out the following information:

### Block number: 1 ###
Magicnr = f9beb4d9
Blocksize = 285
Version = 1
Hash Prev Block = 0000000000000000000000000000000000000000000000000000000000000000
Hash Merkel Root = 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
Time = 2009-01-03 19:15:05
Difficulty (Bits) = 1d00ffff
Nonce = 2083236893

Transaction counter = 1

### Transaction number = 1 ###
Transaction Version = 1
Input counter = 1

Transaction in number: 1
Transaction out hash = 0000000000000000000000000000000000000000000000000000000000000000
Transaction out index = ffffffff
Scripts length = 77
Script = 04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73
Script Ascii = EThe Times 03/Jan/2009 Chancellor on brink of second bailout for banks
Sequence = ffffffff

Output counter = 1

Transaction out number: 1
Bitcoin = 50
Bitcoin smallest unit: 5000000000
PK Script length = 67
PK Script = 4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac
Locktime = 00000000


It was a fun project to improve my coding and learn about the Bitcoin chain. I think my next project wil be something with Ethereum. This is a Blockchain network which can be used to setup your own cryptocurrency and make serverless programs.