Building a Blockchain for more than cryptocurrency

Cryptocurrencies have taken the world by storm recently, and with good reason: the appeal of a currency which is controlled by no centralized authority is compelling, though a more likely reason for its prominence is Bitcoin’s astronomic rise in value. But the technology that powers Bitcoin and other cryptocurrencies, the Blockchain, is arguably a more compelling technology to examine. What we’ll be focusing on here is building the blockchain itself — not just a cryptocurrency (although, the blockchain could easily be used to create one).

The first thing we need to establish is what exactly a blockchain is.

A blockchain is what it sounds like: a chain of blocks. At this point, you’re likely thinking something along the lines of “wow, I sure am glad I pay for an internet connection and went to this article so some stuck-up nerd could tell me that a block-chain is a chain made of blocks!” Bear with me, however, as I’ll try to impress the fundamental concept of a blockchain.

It’s important to note that a blockchain being a chain of blocks is a very abstract concept: it can be implemented in numerous different ways. As long as there is a chain of blocks that form an immutable ledger, no matter which programming language it is implemented in, it is still a blockchain.

Now that you know that a blockchain is a chain of blocks, the important word to define here is a “block”, as most people know what a chain is. Just to make sure, here is a picture of a chain.

(Although, in this sense, a chain is more symbolic — we’ll get to that later. The chain metaphor still works though, in that the blocks are all linked together).

What is a “block?” Simply put, a block is an object that holds a few important pieces of data. These pieces of data are:

  1. A hash — we’ll explain how these are made in a second
  2. The hash of the previous block
  3. The block’s index (what position it holds in the blockchain)
  4. A nonce, which is an arbitrary number that can be used exactly once (which I’ll explain more as we get to hashes)
  5. A timestamp, usually the number of milliseconds after January 1, 1970 (the Unix epoch) that the block was created
  6. A hash of the data to be stored in the block

So what is a hash? A hash is a number (though usually represented as a base-36 number, so it has letters and numbers) generated by putting any data through a hashing algorithm (the specifics of this algorithm are not within the scope of this article, but readers are encouraged to read the Wikipedia page on SHA256 should they feel curious). The difference between hashing and encrypting a message is that hashing is a one-way process. Unlike encryption, where the encrypted data can be returned to its original state with a decryption (private) key, a hash cannot be reversed. So what’s the use for them? They can verify that any data is the same as data given at an earlier time. To simplify, anyone can verify the data in the blockchain by hashing the data. If the two hashes match, it’s the same data.

How do we make a hash for a block? We combine the data and run it through a hashing algorithm. Here’s a JavaScript representation.

Now we have our block’s hash! Similarly, the data’s hash is created by running a String representation of the data through the SHA256 algorithm.

Now, we have everything to create our block, and therefore it can be added to the blockchain. Because the block’s hash contains the hash of the previous block, no-one can change the contents of a previous block without invalidating all subsequent blocks, which then alerts everyone using the blockchain that the block has been tampered with, and therefore should be rejected. Because no person can change the blockchain, it is referred to as immutable. This is the key feature of the blockchain.

Now, if you’re thinking, “that was kind of convoluted, but it cannot possibly be that simple,” then congratulations, because you’re right.

Allow me to throw you a scenario. You have created a blockchain to use for a cryptocurrency, and the transactions are the data stored in the blocks. Someone decides to create a massive amount of blocks to say that everybody on the blockchain decided to transfer all of their money to them. How do you stop them?

The answer lies in making the blocks difficult to create. If everyone has to work hard to create blocks, then one bad actor would have to have more computer power than all of the legitimate actors on the network combined to create false blocks. This is known as proof-of-work: showing that you have spent computer resources to create a block, which in turn shuts out malicious actors.

Note: should a bad actor gain more computing power than everyone else on the blockchain, then the blockchain is still vulnerable! This is called a “51% attack”, owing to the fact that should any one person have 51% or more — a majority — of the computing power, they would be able to dictate which blocks go on the blockchain. The best way to prevent this is through widespread use — the more people on the blockchain, the more power needed to reach 51%, which in turn makes attacks prohibitively expensive for any would-be attacker.

How do we make blocks hard to create? There are many different algorithms in use, but the one Bitcoin uses is simple: the block’s hash must start with a certain number of zeroes. For our purposes, let’s say four. (The more zeroes, the harder it is to find a matching hash).

So let’s say we have our block, and we run it through the hash function, and… nope. The hash doesn’t start with four zeroes. It’s no good. What now?

To change the hash, we have to change the data in the block. This is where the nonce comes in to play. (Did you forget about the nonce? Haha, the nonce does not forgive and certainly does not forget. This injustice shall not go unpunished.)

Futile attempts at comedy aside, This is the purpose of the nonce. It’s just a number, so if all of the block’s data plus a nonce of zero didn’t work, we just increment the nonce by one and try again until we get a working hash. This is what mining a block is. A handy JavaScript implementation:

So there we have it! Once a working hash is found, the block is complete and can become part of the blockchain.

Now, we’re going to bring out the Hackerman inside all of us and create an implementation of the blockchain. I’m going to do this in Node.js, but any programming language should work (not verbatim, obviously, but the principles should be similar). Let’s get to it!

That’s Python, not Node.js, but it’s a stock image, so whatever. You’re very pretentious, did you know that?

The first thing we need to do is create a representation of a Block, which we will use a class for.

Notice the constructor requires three parameters — index, previousHash, and data. These all need to be provided by the Blockchain class, which will be a bit more in-depth, but we can get through it.

To start: the constructor. This sets up an array (this.blocks) to store the blocks in, then adds the first (genesis) block. The addBlock function takes the inputted data, mines a hash for the block, and adds it to the blockchain. And just like that, we have a blockchain!

To really make a viable blockchain, we have to get this into a server so people can set up multiple nodes (local copies of the blockchain), which I will cover in a later tutorial.

So now you have a blockchain. What now? For many, the answer is ‘cryptocurrency’. However, this is not the only option! If you can think of a situation where having a permanent record of any data possible, the blockchain has a potential use case. Let’s say that we wanted to communicate and share data, but didn’t trust a big company (like Facebook, Twitter, Reddit, etc) not to alter it. We could store the data in a blockchain, and that way everyone would have a permanent record of the data.

To create our hypothetical messaging app, we first need to get set up. While this will be a bare-bones implementation, it will be a working, non-cryptocurrency based application of the blockchain. To do this, we will set up a simple Express server, with a /addMessage route and a /viewMessages route. I implemented this in Node below:

What this does is whenever a user sends a HTTP POST request to localhost:3000/addMessage, the data in the message field of the request is added in a new block, which is then added in the blockchain. Additionally, a HTTP GET request to localhost:3000/getMessages shows the entire blockchain, including the messages. If you have Block.js, Blockchain.js, and Messager.js in the same folder, you can run the following commands and send the requests — this is a working app!

npm install js-sha256 express

node Messager.js

And there you go!

Now, to add a new message, send a HTTP POST request with the message field being the message to add (though you will have to wait a little bit to mine the block). This is how I did it, with cURL:

curl localhost:3000/addMessage -X POST -d 'message="Hello world!"'

Thank you so much for reading! Next time, we’ll discuss some more applications, and continue setting up the blockchain server to support multiple nodes. Until then, have some virtual cake — you’ve earned it!

If you enjoyed this story, or have any questions or suggestions, please leave a comment — I enjoy the feedback.

I write about technology, politics, and things that interest me. I think I'm funny. I'm not that funny.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store