I recently decided to dig down into the whole thing about blockchains, because I thought it sounded really interesting. I never actually bothered with trying to understand more than the very basic concept of BitCoin, but until now I also never understood what it actually is.

The core concept behind any crypto currency is something called a *block chain*.

A *block* is basically a chunk of information - say, a transaction of BitCoin between two parties. Nothing really special about that, you might think. Well, no. There’s nothing special about it. What makes the block chain so special and secure, is that *each block also contains the hash of the previous block*. This is where the chain concept comes in. Having each previous block verify the validity of the next block means that in order for anyone to manipulate any one block in a chain, they must manipulate every single previous block - and replace them in the block chain datastream.

In a block chain such as BitCoin, which currently has close to 550,000 blocks of information, this would mean a massive amount of time to recalculate all the hashes - somewhere around 10.5 years. Since the avarage calculation time of a block hash is roughly 10 minutes, the original block chain would have moved well past the altered branch of a chain.

## So What’s In The Chain?

As I mentioned above, the block is basically a chunk of information + the hash value of the previous block. This could be expressed as such:

{ |

In order to create a chain we would first need something called a *genesis block*. Let us just use the one above.

We also need something called *difficulty* and *nonce* to calculate the hash.

And of course functions to generate the hash and the block.

We also need a set of rules that determine whether or not the calculated hash meets our requirements. This is called the *difficulty*.

What we do is, we generate a hash, then we compare it to the difficulty. If the difficulty is not met, then we add a number to the block, generate a new hash, and try again. This number is called the *nonce*. We simply start with a nonce of 0, and then add 1 with every attempt.

Let’s say that we want the hash hexidecimal number to be less than or equal to a constant decimal. Now we can determine if a hash is valid or not:

const validHash = hash => parseInt(hash, 16) <= 1.6532065611505255e+71; |

This is not exactly how it’s done with BitCoin, but for the sake of this demonstration it will do just fine.

We will also need to create the *genesis block* including it’s hash. To generate the hash I will use node.js’ crypto library and the hashing algorithm *sha256*.

const crypto = require('crypto'); |

Finally, in order to add a new block to the chain, we need to be able to get the last block of the chain and make a function which adds the new block to the chain.

const getLastBlock = chain => chain.slice(-1)[0]; |

Let’s wrap it all up nicely in a module and expose only the functions needed.

const crypto = require('crypto'); |

And, of course, test it out:

const chain = require('./chain'); |

That should output something along the lines of this:

[ { hash: '00000959b93d4093ce9275fb4f2388a7aaf247e86b6a5179569e33a3970b4254', |

3 blocks, each hashed and verified by the previous block in the chain.

If you take a look at the `nonce`

value in eacvh block, you can see how many guesses were needed until an appropriate hash could be generated. Remember that really large exponent decimal in the `validHash`

-function? The lower that decimal is, the more difficult it is to generate a hash.

I have certainly learned a lot about block chain theory writing this post up. I hope you enjoyed reading it. If you did, or if you found an error or a typo somewhere, please leave a comment and a share below.

I wish you a very happy New Year!