Skip to content

PyChain is a fully functional, decentralized blockchain implementation designed for educational purposes.

Notifications You must be signed in to change notification settings

maxylev/PyChain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⛓️ PyChain: A Full-Stack Python Blockchain

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.

License Python Vue.js


✨ Features

🧠 Backend (The Node)

  • 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.

💻 Frontend (The Wallet)

  • 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.

🛠️ Installation

1. Prerequisites

Ensure you have Python 3.8+ installed.

2. Install Dependencies

Install the required Python libraries, including Flask, Requests, and Cryptography.

pip install flask requests cryptography flask_cors

🚀 Running the Network

To simulate a decentralized network, we will run two separate nodes on the same computer using different ports.

Step 1: Start Node A (Port 50000)

Open a terminal and run:

python chain.py 50000

Step 2: Start Node B (Port 50001)

Open a new terminal window and run:

python chain.py 50001

The nodes will automatically discover each other if you use the provided interact.py or manually register them via the API.


💳 Using the Web Wallet

  1. Locate the index.html file in your project folder.
  2. Open it directly in your browser (Double click) OR serve it via a local server:
    # Optional: Serve via python
    python -m http.server 8000
  3. Create Identity: Click "Create New" to generate your Public/Private key pair.
  4. 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!
  5. Send Coins:
    • Open index.html in 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.

🤖 Automating with CLI

You can use the included interact.py script to simulate a full lifecycle (Funding, Transaction, Consensus) automatically.

  1. Ensure Node A (50000) and Node B (50001) are running.
  2. Run the script:
python interact.py

What happens:

  1. Registers Node B with Node A.
  2. Generates wallets for Alice and Bob.
  3. Mines a block to fund Alice.
  4. Alice cryptographically signs a transaction to Bob.
  5. Node A mines the block.
  6. Node B automatically syncs and validates the transaction.

📂 Project Structure

├── 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

⚠️ Troubleshooting

"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_cors and added CORS(app) to your robust_chain.py. Browsers block requests to localhost ports unless CORS is enabled.

📜 License

This project is open-source and available under the MIT License. Created for educational purposes.

About

PyChain is a fully functional, decentralized blockchain implementation designed for educational purposes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published