How Cryptocurrency works, let’s find it through demo

Share with friends

Reading Time: 6 minutes

 What is Cryptocurrency?

Cryptocurrency is mathematical protected distributed digital file (ledger) that lists accounts and money and it is maintained by the network of peers.

Cryptocurrency is not controlled by any government or a central authority like Bank whereas fiat currency is issued and controlled by a government.

Cryptocurrency and Blockchain-

Blockchain enables cryptocurrency to move from one person to another person.

Let’s understand cryptocurrency concepts and how it works through a demo app.


Cryptocurrency like Bitcoin is based on Blockchain, I would highly recommend you to read my another article on Blockchain basic concept first. That will help you to understand this article easily.

Cryptocurrency is based on the following concepts

  • Digital Signature – It is used to authorize the sender of the cryptocurrency and verify the transaction (message) integrity.
  • Transaction chains – To verify the sender’s balance. It is different from blockchain and it keeps track of the owner.
  • Blockchain – Hold transaction order and prevent double spending fraud.

We will discuss each point in details and how it is implemented by cryptocurrency in the next section.

How does Cryptocurrency work?

Let’s create a dummy digital cryptocurrency to understand it better, we can assume its name is Pi(π).

So,  how someone can transfer our new cryptocurrency Pi(π).  They need to broadcast a message (transaction) using a crypto wallet and that message will have sender’s account detail, receiver’s account detail and amount.

Remember, in distributed blockchain demo app, we did not add anything in the data section of the blocks, click here to view it. But data section can have anything depending on the application type.

In our case, let’s replace the data section with the transaction (Tx)  for the cryptocurrency Pi (π),  Click here to open the demo app.

But first, the sender should have Pi(π) in their wallet. First, you need to buy cryptocurrency using cryptocurrency exchange or you have to do mining to earn the cryptocurrency.

In this demo app case, Anders has bought 100 Pi(π) from the exchange using $100? I am assuming there is no transaction fee and Pi(π) and dollar($) have the same exchange rate. For e.g. please check row ‘Exchange’ of the first block in the demo app or below yellow highlighted section.


Please note that there is no transaction in the first block of blockchain because nobody was having Pi(π) currency initially.

Now Andres has 100 Pi(π)  so he can send it to someone else to spend it. Please check the Block number 2, 3, 4 and 5 in the demo app. Each block can have multiple immutable transactions for e.g. Jackson has sent 2π to Alexander, Ryan has sent 6π to Carter as below.


Sufficient balance check using ‘transaction chain’

Now check the last block transactions. Grace has sent 2 Pi(π) to Katherine but was Grace having 4 Pi(π)?

The account balance is not maintained, there is no record of account balance. So how it can be verified?

It is verified using the link to previous transactions and find the un-spent transactions. If you will go to block#3 using previous block hash value, you will find Lucas sent 20π to Grace and it is not spent so Lucas has sufficient balance.

Security issues

There are two major security issues – first, anyone can broadcast a message (transaction) to spend somebody else money. And second, anyone can read sender and receiver name. How is it prevented?

It is protected using Asymmetric cryptography. Click here to read how does asymmetric cryptography’s digital signing work.

  • Protect personal details

Public keys are used to send and receive money to protect sender and receiver details, instead of actual name like below-



  • Authorize transactions (messages) using ‘digital signing’

Asymmetric cryptography’s digital signing is used to authorize the broadcasted transaction by the sender. Digital signing is used to authenticate the sender and message integrity.

So actual block’s data section contains the amount, sender’s public key, receiver’s public key and message signature.

Message signature is calculated using sender’s private key and added to the block like below. Click here to open the demo app and use Sign button to get the message signature.



Same message signature is used by the receiver to verify the sender and message integrity. The receiver uses message signature and sender public key to verify it like below. Click here to play with this process.



Please note that, if you will lose your private key, the fund will be lost associated with that private key.

Cryptocurrency blockchain

Now let’s update the block to include the public keys and message signature as below-


Click here to open the sample app to understand it through the demo.  Now try to modify the amount, the block will be invalidated but miner can mine it and make it valid.

Click on the mine button, mining will make block valid again but ‘Sig‘ row is still red because the amount is changed and signature verification is still failing. Miner will not have sender’s private key so they can’t generate the signature again.

Is double spending possible?

Let’s check, Blockchain always accepts then longest chain and rejects the other chain.

So if I want to buy a car from a car dealer who accepts the Pi(π) currency, I need to send a transaction to the car dealer. The transaction will be part of un-confirmed transactions pool initially in the Blockchain. Later my transaction will be picked by a miner and will be added to a block in the Blockchain.

Now my transaction is confirmed and added to the Blockchain so car dealer will get the money. The dealer will deliver the car after transaction confirmation but I have my private key so let’s try to buy something again using this key. I will create a new transaction using the same private key and will try to buy something. Below are the possible scenarios in this case-

Scenarios 1 – My second transaction will be rejected because it is already marked as a spent.  It is identified using transaction chain.

Scenarios 2– Blockchain always switches to the longer branch so I will create a chain longer than other peers chains and I will add my second transaction in the new chain. Blockchain will accept my longest branch and my second transaction will be confirmed and I will be able to spend same Pi money again. My first transaction will be returned to the unconfirmed transaction pool because the old chain will be rejected by the blockchain. Later my dealer’s transaction will be picked by a minor again from unconfirmed transaction pool but dealer’s transaction will be rejected because it is already marked as a spent due to my second transaction.

So dealer will lose car and money both in this case.

How is double spending fraud prevented?

It is prevented by math puzzle (Hashing and Nonce), remember mining and immutability concepts from my blockchain article.

I have to create a longer chain that other peers and to do that I have to mine the blocks faster than all other peers. Mining takes some time to confirm a block because it has to find the Nonce.

To successfully spend same money again,  first I have to wait for confirmation of my dealer’s transaction. After confirmation, I have to create a new block with my second transaction and I have to add this block to the chain. My chain should be longer than all the other peer’s chain and my chain will not have dealer’s transaction.

To do that, I have to race with entire network and I need almost same computing resources as the rest of blockchain peers. But still, it is not guaranteed to win the race so it is very hard and almost impossible to do this.

You can’t change any past transaction because blockchain is immutable.  And it is required for currency to prevent the tampering. Immutability is very important to keep the track of past transactions history, click here to read more about blockchain immutability.

Transactions in the last block of the blockchain are less secure than previous block’s  transactions because an attacker has to re-mine current and all child blocks.

Problem is solved by cryptocurrency –

  • Money transfer  is easy
  • Low transaction fees
  • Faster, cheaper and without 3rd party

And sample code can be downloaded from GitHub.

Share with friends

Leave a Reply

Your email address will not be published. Required fields are marked *