Python Blockchain: Introduction
In this article on Blockchain, we will learn in detail about the theory behind blockchain.
The blockchain is the fundamental building block behind the world’s most popular digital currency Bitcoin. The article deeply dealt with the intricacies of Bitcoin explaining fully the blockchain architecture. The next step is to build our own blockchain.
Satoshi Nakamoto created the first virtual currency in the world called Bitcoin. Looking at the success of Bitcoin, many others created their own virtual currencies. To name a few - Litecoin, Zcash, and so on.
Now, you may also like to launch your own currency. Let us call this as TPCoin (TutorialsPoint Coin). You will write a blockchain to record all transactions that deal with TPCoin. The TPCoin can be used for buying Pizzas, Burgers, Salads, etc. There may be other service providers who would join your network and start accepting TPCoin as the currency for giving out their services. The possibilities are endless.
In this Article , let us understand how to construct such a system and launch your own digital currency in the market.
Components Involved in Blockchain Project Development:
The entire blockchain project development consists of three major components:
The Client is the one who will buy goods from other vendors. The client himself may become a vendor and will accept money from others against the goods he supplies. We assume here that the client can both be a supplier and a recipient of TPCoins. Thus, we will create a client class in our code that has the ability to send and receive money.
The Miner is the one who picks up the transactions from a transaction pool and assembles them in a block. The miner has to provide a valid proof-of-work to get the mining reward. All the money that miner collects as a fee will be for him to keep. He may spend that money on buying goods or services from other registered vendors on the network, just the way a Client described above does.
Finally, a Blockchain is a data structure that chains all the mined blocks in a chronological order. This chain is immutable and thus temper-proof.
Python Block chain – Developing Client
A client is somebody who holds TPCoins and transacts those for goods/services from other vendors on the network including his own. We should define a Client class for this purpose. To create a globally unique identification for the client, we use PKI (Public Key Infrastructure). In this chapter, let us talk about this in detail.
The client should be able to send money from his wallet to another known person. Similarly, the client should be able to accept money from a third party. For spending money, the client would create a transaction specifying the sender’s name and the amount to be paid. For receiving money, the client will provide his identity to the third party - essentially a sender of the money. We do not store the balance amount of money the client holds in his wallet. During a transaction, we will compute the actual balance to ensure that the client has sufficient balance to make the payment.
To develop the Client class and for the rest of the code in the project, we will need to import many Python libraries. These are listed below:
# import libraries
import numpy as np
import pandas as pd
import pylab as pl
In addition to the above standard libraries, we are going to sign our transactions, create hash of the objects, etc.
For this, you will need to import the following libraries:
# following imports are required by PKI
import Crypto.Random from Crypto.
Hash import SHA from Crypto.PublicKey
import RSA from Crypto.Signature