Creating a CryptoCurrency-Blockchain App

A Technical Overview

Swanand Kavitkar
10 min readJan 11, 2021

Hello and Welcome fellow developers, In this article I will be giving a technical overview about creation of a CryptoCurrency-Blockchain App.

The Article is divided into 5 parts :

  • Blockchain overview
  • CryptoCurrency overview
  • REST APIs and Redis Publish Subscribe Model
  • Working application flow
  • References

Feel free to skip the parts you’re not interested in…

The Blockchain overview

Blockchain technology is an open distributed ledger that can record transactions of two parties securely and efficiently.

As the name suggests, a blockchain is simply a Chain of blocks which is visible to everyone on the blockchain network.

distributed ledger : Visible to everyone

Each block contains a lot of different fields including the transaction data. Whenever there is a transaction between 2 parties it is recorded inside a block. Now this block gets attached to the exisitng blockchain on the network and everybody gets to see it.

That completes the basic part about blockchain,
now we will dive deeper into it.

1. Different fields inside a block :

Let’s have a look at different fields inside a single block.

Block ID : a unique id given to every block.
Timestamp : the exact timestamp when the block was created.
Data : This field consists of all the data for transaction in that block.
LastHash : The hash value of the previous block
Hash
and Nonce : These two fields are a little bit complex so allow me to explain them in detail along with related terminologies.

2. Hash and Nonce Working :

For the application used in this project, a SHA256 cryptographic hash function is used to calculate the Hash.
Nonce is such a value that on calculating the hash of the whole block we get a certain number of leading zeroes(based on the current difficulty value) in the result.

Let us study the related terms with hash and nonce to understand above statement.
i) Mining Rate :
The speed with which the blocks are added in a blockchain is called as Mining Rate. For Bitcoin cryptocurrency the Mining rate is 10 minutes i.e. a new block is added every 10 minutes.

ii) SHA256 :
SHA256 cryptographic function takes any data as input and returns an encrypted 256 bit binary hash value.
It is an irreversible chunk of data meaning one cannot get the original input from the hash value i.e.
for input value ‘12345’ using SHA256 function we get,
SHA256(‘12345’) =>
5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5

Now it is impossible to get the input value ‘12345’ from above hash.

SHA256 is a cryptographic function which is designed in such a way that even if we change a single character from the input we will get an entirely new hash value.
the hash value for ‘12245’ (we changed a ‘3’ to ‘2’)
SHA256(‘12245’) =>
bec376434eff85274c752aa7064dbd0faba56e7e0651415ff23ba4742528ec0f
Notice how we have an entirely new value for just one field change.
This is the base of our blockchain security.

iii)Hash :
A hash value is calculated by applying SHA256 cryptographic function on the contents of a block.
Hash = SHA256({Id,Timestamp,LastHash,Nonce,data});

iv) Difficulty :
At any given point of time, our blockchain will have a certain difficulty level i.e. 3 ,4, 5 etc…
this is number of leading zeroes required in the hash.
suppose the difficulty is 4.
if below hashes are valid then we will check the difficulty i.e. leading zeroes
Unacceptable Valid hash -> bec376434eff85274c752aa7064dbd0faba56e7e0651415ff23ba4742528ec0f
Acceptable Valid hash ->
0000d6cd692137b657fa6c97c80dcb717e60e0663c40a9a2c5a01cb911390d11

Please note that the hash should be valid and also with leading zeroes as per the difficulty. Any invalid hash with 4 leading zeroes will still be considered as Invalid

v) Nonce :
As per above explanations we have Hash and Difficulty
Hash = SHA256({Id,Timestamp,LastHash,Nonce,data});
Nonce is any such value due to which our hash will satisfy the difficulty condition.
E.g. if difficulty is 3, we will calculate a nonce value in such a way that the resultant hash has 3 leading zeroes.
Nonce =1 -> SHA256({Id,Timestamp,LastHash,1,data}) ->
b8a72255aedf3a01cb4aece302283ce9ef5df5c36290a93f324ab30af75c3be8
Nonce =2 -> SHA256({Id,Timestamp,LastHash,2,data}) ->
f5095cd644fbe157a3ebc71d3e3212530f58e6d8a88c400c811314f217278c5
Nonce =3 -> SHA256({Id,Timestamp,LastHash,3,data}) ->
318aee3fed8c9d040d35a7fc1fa776fb31303833aa2de885354ddf3d44d8fb6
.
.
.
.
Nonce=133456 ->
SHA256({Id,Timestamp,LastHash,133456,data}) ->
000ee3fed8c9d040d35a7fc1fa776fb31303833aa2de885354ddf3d44d8fb6
now since we have got 3 leading zeroes in the Hash we will set the nonce value as 133456.
this is how we calculate the nonce value for every block.

vi) Mining :
Whenever a new block is to be added it needs to have a valid nonce value which satisfies difficulty requirement(No. of leading zeroes in hash).
The process of calculating a nonce value is called Mining. If the leading zeroes does not match the difficulty then the block is deemed invalid.

vii) Miners :
Miners are the users who calculate the nonce value of a block. Bitcoin system has an inbuilt mechanism to reward a miner for every block they mine.

3. BlockChain in Action :

Let’s see how all these fields work together.
for every blockchain we have a genesis block which is the first block of a blockchain with some hardcoded data and a hash value.

Genesis — Block no. 1

After that everytime a new block is created, it needs to be mined i.e. a nonce value needs to be calculated.
Calculating a nonce value is a very expensive task and it can be calculated only using a brute force.
Once the block is mined it is attached to the blockchain.

Blockchain in action

Now at any given time we have a difficulty value which is the number of leading zeroes in the valid hash value.
Whenever a block is mined the time required for mining it is calculated and if is greater than the Mining rate the difficulty is decreased and vice-versa.

This is how the difficulty value helps in maintaining an average mining rate.

Now if an attacker changes the data of any particular block, the hash value changes.
Which means the hash will not have required leading number zeroes. So the attacker will have to calculate the nonce value again.

Every block in the blockchain has the value of previous block’s hash

Now the next block has the hash value of the current block so the attacker will have to repeat the same process for the next blocks as well.

This computationally is a very expensive process and in turn makes the blockchain a very secure technology.
This completes the major part of this blog!!!

CryptoCurrency Overview :

Now lets see where the Cryptocurrency comes into play with blockchain.

1. Wallet :

Every user in the blockchain has a wallet. It cotains a
i) Private Key : The key used to sign the transactions.
ii) Public Key : The address used to make transactions with you and to verify if a transaction is signed by you.
ii) Balance : The overall balance of a user

2. Transaction :

A transaction is transfer of cryptocurrency between 2 users. It is signed by the sender and the public key of the sender is used to verify if the transaction is authentic.

3. Transaction Pool :

Every time a new transaction takes place it is added into the transaction pool. The miners then take the transactions from the pool and add a block with valid transactions into the blockchain.

If an invalid transaction is found it is discarded from the transaction pool.

REST APIs and Redis Publish Subscribe Model :

1. REST APIs using Express JS

Our application will be running on several ports.
Each port in the system denotes a user.

The application is coded into javascript and we will be using Express JS for creating the REST APIs.
Express JS provides a powerful express object which can create Restful APIs.
We will be creating following endpoints using this :

  1. GET /blocks : to get the current blockchain blocks
  2. POST /transact : to make a transaction between 2 peers
  3. GET /transaction-pool-map : to get the transactions pool map
  4. GET /mine-transactions : to mine the transactions in the transactions pool and create a block to the blockchain

2. Redis Publish Subscribe Model

We have the REST APIs in place but these will be used to only query a particular peer’s data. We wont be able to get the data of another peer through this endpoints. We need a way to keep our blockchain and transaction pool to be synchronized between all the peers.

All peers in Sync

There are mainly 2 ways for communication between peers.
i) Point to Point :
In a P2P network the messages are sent from one peer to another through a queue. In this implementation we will have to manually send the Blockchain and transaction pool.

point to point VS publish-subscribe

ii) Publish Subscribe Model Using Redis :
In this model we will have separate channels for blockchain and transaction pool.
Our peers will subscribe to these channels and whenever there is a change in any of the peer’s Blockchain or transaction pool it will be published to these channels which will then be reflected in all the peers.

Publish Subscribe Model

Redis willl give us a server where we can create channels for blockchain and transaction pool and also subscribe to channels as well as publish our blockchain and transaction-pool data on the channels.

Working application flow :

1. Start UP :

We will start our redis server at first.
Then we will start off our application on different ports. Each of the peer resides on separate port and will have a blockchain with just the genesis block.
Let’s say we have 4 users,
1. Adam
2. Eva
3. Jonas
4. Martha
5. Claudia (Miner)

All of them would be subscribed to the
Blockchain channel
Transaction-Pool channel

2. Creating a transaction :

Then we will call transact api endpoint to create a transaction.
We will create a transaction between Adam and the mentioned receiver by Adam let’s say Eva for the amount 100.

Adam’s transaction pool will have this transaction but other peers wont have any knowledge about it.
So now Adam will Publish his transaction-pool on the transaction-pool channel.
Now all the subscriber will receive that and everybody will be in sync with the latest transaction pool.

3. Mining of a Transaction :

We also have a miner in our network.
Claudia is a miner and she will take a look at the transaction pool and mine the transactions.
She will check if all the transactions are valid or not and discard invalid transactions.
Then she will create a new block with this transaction data.

4. Adding a block to the blockchain :

Let’s say the difficulty when claudia is mining the block is 6.
Then Claudia will use her computational resources(CPU Power etc) and find a nonce value in such a way that the resultant hash of the whole block has 6 leading zeroes.

once she finds the correct nonce value that block will be appended to the current blockchain.

5. Synchronization of Blockchain :

Right now only claudia has the updated blockchain. So she will publish it on the Blockchain channel and everybody will be in sync with the latest blockchain.

It may happen that peers are getting several blockchains on the channel since all of the users might be simultaneously making transactions.
So our algorithm will always take the longest chain received by the peers.

if the incoming chain is shorter than the current chain then it will not be accepted.

We also have a check if a block is valid or not.

If a block is added by an attacker then it will be deemed invalid and rejected.

References

This marks the end of this blog and I hope you have gained a bit of a knowledge about blockchain from this.

The implementation details have been taken from the Udemy course of Building your Blockchain from scratch

I would like to share couple of more resources :

Blockchain White Paper

That’s it for today, Happy Mining !!!

--

--

Swanand Kavitkar

Mobile & Web App Developer | Google Certified Android Developer | AWS certified Solutions Architect