Trang chủ‎ > ‎IT‎ > ‎Blockchain‎ > ‎

HackerNoon - A Beginner’s Guide to Blockchain Programming

A Beginner’s Guide to Blockchain Programming

I had a lot of questions on my quest to understand how Blockchain works. The important one was “How do I build applications on it?”. It took a few weeks of digging up, reading and experimenting to finally get it. I couldn’t find a short but comprehensive guide. Now, that I have some decent understanding, I thought of writing one that could help others. This is a light speed guide, I have kept only the important parts in order to reduce the learning curve.


  1. The Purpose of Blockchain
  2. How was the Blockchain invented?
  3. Introduction to Ethereum & Smart Contracts
  4. Programming Smart Contracts on Ethereum
  5. The Way Ahead

The Purpose of Blockchain

Roopa lives in one of the remote areas of Delhi. The government of India has allocated her few resources of food every month. Since she belongs to the BPL(Below Poverty Line) category. The government uses a middleman to distribute these food resources. Only one-third of the allocated food resources reach people like Roopa, the rest is sold by the middleman for profit.

Sara writes fiction books, she publishes them on Amazon. She’s upset because Amazon takes 50% of the sale as commission. That’s unfair because she alone has put in the efforts of writing and marketing.

The problem is middleman is hungry for power and money. Their motto has become “profit at any cost”, to support producers and to empower the poor, we need middlemen to act ethically. That’s almost impossible to achieve, but what if we can replace middleman with an autonomous system?

Since computers don’t have biases they neither need money or power. This could have been Satoshi Namakato’s thought when he invented bitcoin using the blockchain technology in 2008.

How was the Blockchain Invented?

Money has evolved over time, each evolution reduced its cost of production and made their transactions more convenient. Gold coins were costly to produce. The invention of paper currency solved this problem. But, after the invention of computers and internet, people found a better way to make transactions convenient and faster.

To safe keep our lifetime’s earnings and to facilitate digital transactions we need a middleman(bank). This made banks powerful, they can impose a heavy fee on our withdrawals /transactions, sell our private information, etc

Banks hunger for money caused the financial crisis in 2008. Banks failed to respect their customer’s privacy. Their weak security systems gave rise to digital fraud.

The next evolution of money had to solve the following problems.

  1. It shouldn’t be stored with a central entity.
  2. It needs to be highly secure.
  3. It should ensure privacy.

Since fiat currency is controlled by the government, Satoshi had no option but to invent a new currency (Bitcoin). He solved these problems with the help of peer-to-peer networks and cryptography.


Credits : xkcd

Torrents use peer-to-peer technology to share files. The torrent app doesn’t download the file from a central server or a single computer, instead, it connects to the people in its network, finds out who has the file and download it from their computers.

You get the pieces of a file from different computers around the world. If one person in the network goes away, your download isn’t affected because there are other people who can share the file.

Satoshi adopted this technology since it stores money in a decentralized way. No single entity will have control over it.


Credits : xkcd

In cryptography, one can digitally sign a message. In order to do this we need three things a public key, private key, and message.

The public key and private key are a set of long characters that are mathematically connected. A public key is public like your username, and the private key is a secret like your password.

A message is an information you want to authorise, ex: “I authorise you to pay John $100”.

If you input the algorithm with a public key, private key and message. The cryptographic algorithm will produce a signature. That is another set of characters unique to the contents of that message.

Public Key 
-----BEGIN EC PUBLIC KEY----- MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE50uE+YSxqDgMkFByhpcgTVqXCqHO h68Ljt1z0jklDff/WV7xo+U6o3REBtK/C0/LM+Ef3FB3wR9aXMGNMLb9EA== -----END EC PUBLIC KEY-----
Private Key 
Hello World

To verify the message, one has to input the public key, the message, and the signature. The cryptographic algorithm can verify if the message was signed by the owner of the public key.

It would take 1000’s of years to break the cryptographic algorithm. This cannot be done faster because of the computational limits we have. Quantum computers in the future might challenge this. But, the Bitcoin system can be upgraded to ensure security.

Satoshi incorporated cryptography in his system to help people authorise bitcoin transactions from their wallets.


Credits : xkcd

You register to bitcoin by generating a wallet (public key/private key). The systems collect no information such as email id, full name, etc. Hence you are anonymous unless you announce your public key.

Putting it Together

Satoshi built a shared ledger using cryptography and peer-to-peer networks. When someone sends bitcoins to someone, a message is cryptographically signed and broadcasted to the entire people in the network. They update their ledger, hence everyone in the network knows who owns what.


Every ten minutes transactions are grouped together into a block and linked back to the previous blocks. This process makes a continuous blockchain. Mining is a process by which a block is confirmed, this involves computers in the network to solve a mathematical problem. The first computer/miner who solves the problem gets rewarded in bitcoins made out of thin air.

Once the block is confirmed and added to the network, it is replicated across the entire network. Blockchain was invented in the process of making the autonomous Bitcoin system which confirmed transactions without human intervention. If you are looking for a simplified explanation for blockchain, here’s a story.

Introduction to Ethereum & Smart Contracts

Earlier we talked about replacing middleman with autonomous systems. This can be done through programming. Bitcoin’s system was difficult for people to code autonomous systems.

Hence Vitalik Buterin built a new cryptocurrency called Ethereum. It was not only a decentralised cryptocurrency but a network of computers which can host code in the form of smart contracts.

In smart contracts, we can program conditions. If you want to build a decentralised bookstore. You write instructions to help authors add new books, send the download link to an ebook once the customer makes a transaction etc.

Smart contracts not only store conditions but also data. A smart contract of a decentralised bookstore in itself stores the book listing, purchases, etc.

However, we should admit the limitations of the smart contract. Some systems need human support, computers can’t handle it. In the real world implementing smart contracts isn’t easy. Smart contracts once published cannot be altered, silly mistakes can be costly.

Programming Smart Contracts on Ethereum

Credits : xkcd

We will build a simple smart contract that stores and retrieves grades of students. We will be coding the contract in solidity. Here’s the github repo.

pragma solidity ^0.4.18;
contract Grades{

The first line tells the compiler which version of solidity we are using. Then we define the contract grades.