Blockchain explained using C# implementation

Dhiraj Khodade
4 min readMar 1, 2021

In earlier post we saw what Blockchain and Bitcoin Mining is and how it works. In this post we will be implementing our own simple crypto currency using blockchain algorithm which will have following features. (Find the link to complete source code at the end of this article.)

  • Create Blockchain
  • Mining Rewards & Transactions
  • Validate and Sign Transactions (Elliptic curve signature algorithm)
  • Proof-of-Work
  • Blockchain Explorer UI front-end in aspnet core for easy understanding of Blockchain functionality

I have added this project’s docker image to DockerHub. You can directly run this image as container on you machine using below docker command and access it from browser — http://localhost:8080

docker run -p 8080:80 dhirajkhodade/mudracoin-blockchain-app

Block

As we saw blockchain consists of multiple blocks and each block has 3 main elements

  1. The Data in block which will be list of transactions in our case.
  2. Nonce. It's a random integer number adjusted by miners to get the desired hash value of the block. Once the valid nonce is found it gets added to block along with generated hash for that block. Generated hash is compared with targeted hash. In below code observe the method MineBlock(int difficulty) this will keep calculating the hash of the block until it matches the targeted value. For Example if difficulty is 4 then it will keep adjusting nonce value till it gets the hash value which has 4 zeros at the start of the hash similar to this 0000eb6f460221dd465688be5d7a1e955088e46ada2f083c3d801c3b7c8aac34
  3. The Hash is 256 bit number with lots of zeros at the start of it as shown above.

Transaction

As we know blocks are formed by grouping pending transactions over certain period of time. To make sure transactions are valid we are going to sign transactions in our blockchain with ECC – Elliptic Curve Cryptography – secp256k1. Signing transactions will make sure only you can spend coins in your wallet. We also have validate transaction method which will make sure transactions are signed by owner of the wallet. So if anyone else tries to make a transaction/spend your coins on your behalf that will be marked as invalid and will not be recorded in blockchain system.

Signing Transactions

So how does this signing work? this is pretty simple every user on network has their own pair of public and private key. In fact the address of you wallet is your public key which you share with everyone to receive coins and there is a private key associated with this public key which you use to sign the transactions. Look at the SignTransaction(string secretKey) method this extracts the public key from your secretKey and then compares it with your wallet address i.e. your public key. If it matches that means transaction is coming from owner of the wallet and is valid but that is not enough you need to protect your transaction even after it is created and put into transaction pool so for this we are going to sign it with our private key for this we calculate the hash of the transaction and sign it with secret key and then the signature is attached with transaction to validate it anytime you want.

To validate transaction we have IsValid() method where we check if the signature that transaction hold is really a valid signature for the transaction for that we will recalculate the hash of the transaction and verify if that signature belongs the same hash. If somehow some hacker changed your transaction the hash of the transaction will also change and this will invalidate the signature, as that hash is not what we signed for it will fail the signature verification and will me marked as invalid transaction.

Blockchain

Now that we have all the parts of blockchain ready lets create blockchain. We will need a method to add newly created transactions to pending transaction’s pool. Observe AddTransactionToPool(Transaction transaction) method below we are checking if the transaction is valid or not using IsValid() method that we created earlier in Transaction class. Also observe the Blockchain constructor which adds a first block to the Blockchain. This is a special block which does not have any data or transactions and it does not refer to the previous block as it is the first block of blockchain. Its called Genesis Block.

We will need mining functionality in our blockchain. Remember miner of the block gets paid a reward in BTC. So if you observe the method MinePendingTransactions() it accepts miner’s wallet address to receive his/her mining reward which I have set to 12.5 BTC by default and mining difficulty to 4. When MinePendingTransactions() method is called it will create one more transaction which will not have FromAddress as that is reward transaction and ToAddress will be the miner’s address and we will add this reward transaction to the pool and create a block of all the pending transactions along with the reward transaction and call MineBlock() method of the Block class that we created earlier by passing the mining difficulty as parameter. Once the block is mined we will add it to the our blockchain as it has passed all the checks on its way to the blockchain making it the tamper proof, valid block of verified transactions.

UI Dashboard - Blockchain Explorer

Let's use all the core functionality that we have created for our blockchain and create UI dashboard. Let's call it Blockchain Explorer. This explorer will help us create and sign transactions, see them being added to transaction pool and then mine those into a block, explore the blockchain block by block and have a look at transactions inside it, set mining reward and mining difficulty, check the balance of users who made transactions; of-course we can do that after all its a public ledger.

This is how our Bitcoin Explorer UI looks like. You can find complete source code on GitHub -

--

--