This application combines a blockchain node and a client wallet into a single Flask application.
- Blockchain Node: Mine blocks, view the blockchain, and manage nodes
- Wallet Client: Generate wallets, create and sign transactions
- Unified Interface: Access both services from a single web interface
The easiest way to run the application is using Docker:
# Build the Docker image
docker build -t blockchain-app .
# Run the container
docker run -p 5000:5000 blockchain-app
Then access the application at http://localhost:5000
If you prefer to run the application locally:
-
Install the dependencies:
pip install -r requirements.txt
-
Run the application:
python app.py
/blockchain
: Contains the blockchain implementation/client
: Contains the wallet client implementation/templates
: Contains the HTML templates for the integrated app/static
: Contains CSS and JavaScript files
- Start by visiting the home page at http://localhost:5000
- From there, you can:
- Visit the Blockchain Node to mine blocks and view the blockchain
- Visit the Wallet Client to create wallets and make transactions
The application exposes various API endpoints:
GET /blockchain/chain
: Get the full blockchainGET /blockchain/mine
: Mine a new blockPOST /blockchain/transactions/new
: Submit a new transactionGET /blockchain/transactions/get
: Get all pending transactionsPOST /blockchain/nodes/register
: Register new nodesGET /blockchain/nodes/resolve
: Resolve conflicts between nodesGET /blockchain/nodes/get
: Get all registered nodes
GET /client/wallet/new
: Generate a new wallet (public/private key pair)POST /client/generate/transaction
: Generate and sign a transaction
This is a implementation of a basic blockchain structure in python, with all the description, and documentation of it's working and things.
NOTE: It used to be a basic interaction API for finding the hashs, POW and the info. Currently it's revamped into a full stack website with dummy payments mining and a better UI.
Here is the representation of a transaction in blockchain in Python.
block = {
'index': 1,
'timestamp': 1506057125.900785,
'transactions': [
{
'sender': "8527147fe1f5426f9dd545de4b27ee00",
'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f",
'amount': 5,
}
],
'proof': 324984774000,
'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
}
Each new block contains within itself, the hash of the previous Block. This is crucial because it's what gives blockchains immutability. If an attacker corrupted an earlier Block in the chain then all subsequent blocks will contain incorrect hashes.
When our Blockchain is instantiated we'll need to seed it with a genesis block, a block with no predecessors. We'll also need to add a "proof" to our genesis block which is the result of mining (or proof of work).
A Proof of Work algorithm (PoW) is how new Blocks are created or mined on the blockchain. The goal of PoW is to discover a number which solves a problem. The number must be difficult to find but easy to verify computationally speaking by anyone on the network. This is the core idea behind Proof of Work.
Let's decide that the hash of some integer x multiplied by another y must end in 0.
So, hash(x * y) = ac23dc...0
And for this simplified example, let's fix x = 5
.
Implementing this in Python:
from hashlib import sha256
x = 5
y = 0 # Y needs to be calculated
while sha256(f'{x*y}'.encode()).hexdigest()[-1] != "0":
y += 1
print(f'The solution is y = {y}')
The solution here is y = 21
. Since, the produced hash ends in 0
:
hash(5 * 21) = 1253e9373e...5e3600155e860
In Bitcoin, the Proof of Work algorithm is called Hashcash. And it's not too different from our basic example above. It's the algorithm that miners race to solve in order to create a new block. In general, the difficulty is determined by the number of characters searched for in a string. The miners are then rewarded for their solution by receiving a coin in a transaction.
The network is able to easily verify their solution.
This is what the request for a transaction will look like. It's what the user sends to the server:
{
"sender": "my address",
"recipient": "someone else's address",
"amount": 5
}
Flask
- A HTTP Gateway to expose our blockchain structure externally.Requests
- A medium to check the HTTP Endpoint request and return JSON response.Pycryptodome
- For cryptographic operations like generating keys and signing transactions