PyChain is a fully functional, decentralized blockchain implementation designed for educational purposes. It features a robust Python backend node, a P2P consensus engine, and a modern Vue.js web wallet.
It demonstrates core blockchain concepts including Proof of Work, RSA Digital Signatures, Distributed Consensus (Longest Chain Rule), and Fault Tolerance.
- Proof of Work (PoW): Dynamic difficulty adjustment to keep block times stable.
- Cryptographic Security: Transactions are signed using RSA-2048 and SHA-256.
- P2P Gossip Protocol: Nodes automatically sync and propagate blocks to peers.
- Deep Validation: Full history replay to prevent "History Rewrite" attacks and double-spending.
- Persistence: The ledger is saved to disk (
chain_PORT.json), allowing nodes to survive restarts.
- Modern UI: Built with Vue.js 3 and TailwindCSS (Dark Mode).
- Client-Side Signing: Private keys never leave the browser. Transactions are signed in JS using
node-forge. - Block Explorer: Visual real-time feed of the blockchain state.
- Mining Interface: One-click mining to earn coins directly to your wallet address.
Ensure you have Python 3.8+ installed.
Install the required Python libraries, including Flask, Requests, and Cryptography.
pip install flask requests cryptography flask_corsTo simulate a decentralized network, we will run two separate nodes on the same computer using different ports.
Open a terminal and run:
python chain.py 50000Open a new terminal window and run:
python chain.py 50001The nodes will automatically discover each other if you use the provided interact.py or manually register them via the API.
- Locate the
index.htmlfile in your project folder. - Open it directly in your browser (Double click) OR serve it via a local server:
# Optional: Serve via python python -m http.server 8000 - Create Identity: Click "Create New" to generate your Public/Private key pair.
- Get Coins (Mining):
- Click the "Mine (Earn)" button.
- The web client sends a request to the node to mine a block and sets your address as the reward recipient.
- Wait a few seconds... Balance Updated!
- Send Coins:
- Open
index.htmlin a second tab (or Incognito window). - Create a second wallet.
- Copy the second wallet's address.
- Go back to the first wallet, click "Send", paste the address, and confirm.
- Open
You can use the included interact.py script to simulate a full lifecycle (Funding, Transaction, Consensus) automatically.
- Ensure Node A (50000) and Node B (50001) are running.
- Run the script:
python interact.pyWhat happens:
- Registers Node B with Node A.
- Generates wallets for Alice and Bob.
- Mines a block to fund Alice.
- Alice cryptographically signs a transaction to Bob.
- Node A mines the block.
- Node B automatically syncs and validates the transaction.
├── chain.py # The Core Blockchain Node (Flask API + Logic)
├── index.html # The Web Wallet & Explorer (Vue.js + Tailwind)
├── interact.py # Python automation script for testing
├── chain_50000.json # (Generated) Ledger storage for Node A
└── chain_50001.json # (Generated) Ledger storage for Node B
"Invalid Reward Amount" Error: If Node B refuses to sync with Node A, it might be due to a strict validation rule on the "Genesis Hack".
- Fix: Ensure you have cleaned up old JSON files (
rm chain_*.json) and restarted both nodes simultaneously before testing.
Web Wallet not connecting:
- Fix: Ensure you installed
flask_corsand addedCORS(app)to yourrobust_chain.py. Browsers block requests to localhost ports unless CORS is enabled.
This project is open-source and available under the MIT License. Created for educational purposes.

