A full-stack web application simulating a digital currency ecosystem with wallets, transactions, and blockchain mining functionality.
- Node.js (v16 or higher)
- npm
- Clone the repository:
git clone https://github.com/arthurr455565/KCoin-Project.git cd KCoin-Project
-
Navigate to backend directory:
cd Kcoin-backend -
Install dependencies:
npm install
-
Configure environment variables: Create a
.envfile in the backend directory:PORT=5000 JWT_SECRET=your-secret-key-here POW_DIFFICULTY_PREFIX=00 MINER_REWARD=10 TRANSACTION_FEE_PERCENT=1 COLLECT_TX_FEES=true TARGET_BLOCK_TIME_MS=5000 DIFFICULTY_WINDOW=5
-
Start the backend server:
npm run dev
The backend will run on
http://localhost:5000
-
Navigate to frontend directory:
cd Kcoin-frontend -
Install dependencies:
npm install
-
Start the frontend development server:
npm run dev
The frontend will run on
http://localhost:5173
Core Components:
- Express.js Server with TypeScript for type safety
- JWT Authentication for secure user sessions
- WebSocket (Socket.IO) for real-time updates
- File-based Storage (JSON files) for simplicity
- Modular Route Structure for maintainability
Key Services:
Blockchain Service: Handles block creation, mining, and chain validationWallet Service: Manages balance calculations and transaction historyAuth Middleware: Protects routes and validates JWT tokens
Data Models:
- User:
id,email,username,passwordHash,walletAddress,createdAt - Transaction:
id,sender,recipient,amount,fee,timestamp - Block:
index,timestamp,transactions,previousHash,hash,nonce
Core Components:
- React 19 with TypeScript for type safety
- React Router for navigation
- Tailwind CSS for modern, responsive styling
- SweetAlert2 for user notifications
- Axios for API communication
- Socket.IO Client for real-time updates
Key Features:
- Authentication Pages: Login and Registration with form validation
- Wallet Dashboard: Balance display, transaction sending, and mining interface
- Blockchain Explorer: Visual representation of the entire blockchain
- Real-time Updates: Automatic balance updates via WebSocket
-
User/Wallet Management
- User registration and authentication
- Unique wallet address generation
- Balance calculation by iterating through blockchain
-
Transaction System
- Create transactions with validation
- Pending transaction pool
- Transaction fee support (configurable)
- Balance validation to prevent overspending
-
Blockchain & Mining
- Proof-of-Work algorithm with configurable difficulty
- Dynamic difficulty adjustment based on block times
- Miner rewards (10 KCoin per block + transaction fees)
- Genesis block creation
-
Real-time Updates
- WebSocket integration for live balance updates
- Automatic UI refresh when blocks are mined
- Dynamic Difficulty: Adjusts mining difficulty based on recent block times
- Transaction Fees: Optional fee system for miners
- Real-time Updates: WebSocket-based live updates
- Modern UI/UX: Responsive design with Tailwind CSS
- Error Handling: Comprehensive error handling with user-friendly messages
Decision: Used JSON files for data persistence Rationale:
- Simpler setup and deployment
- No database configuration required
- Sufficient for demonstration purposes
- Easy to inspect and debug
Decision: Implemented Socket.IO for real-time communication Rationale:
- Provides instant feedback to users
- Enhances user experience
- Demonstrates modern web development practices
- Required for bonus features
Decision: Replaced toast notifications with SweetAlert2 Rationale:
- Better React 19 compatibility
- More reliable than toast libraries
- Better user experience with modal dialogs
- Consistent cross-browser support
Decision: Implemented adaptive mining difficulty Rationale:
- Maintains consistent block times
- Demonstrates advanced blockchain concepts
- Improves system scalability
- Shows understanding of PoW mechanics
Decision: Separated concerns into services, routes, and models Rationale:
- Better code maintainability
- Easier testing and debugging
- Follows SOLID principles
- Scalable for future enhancements
- Password Hashing: bcrypt with salt rounds
- JWT Authentication: Secure token-based auth
- Input Validation: Server-side validation for all inputs
- CORS Configuration: Properly configured for frontend-backend communication
- Error Handling: No sensitive information leaked in error messages
KCoin Project/
βββ Kcoin-backend/
β βββ src/
β β βββ data/ # JSON data files
β β βββ models/ # TypeScript interfaces
β β βββ routes/ # API endpoints
β β βββ services/ # Business logic
β β βββ utils/ # Helper functions
β β βββ realtime/ # WebSocket setup
β βββ package.json
βββ Kcoin-frontend/
β βββ src/
β β βββ components/ # Reusable components
β β βββ pages/ # Page components
β β βββ hooks/ # Custom React hooks
β β βββ api/ # API client setup
β βββ package.json
βββ README.md
- Register Accounts: Create two user accounts
- Get Initial KCoin: Mine a block to receive 10 KCoin
- Send Transactions: Transfer KCoin between accounts
- Mine Blocks: Process pending transactions and earn rewards
- Explore Blockchain: View the complete transaction history
| Variable | Default | Description |
|---|---|---|
PORT |
5000 | Backend server port |
JWT_SECRET |
- | Secret key for JWT tokens |
POW_DIFFICULTY_PREFIX |
"00" | Mining difficulty (leading zeros) |
MINER_REWARD |
10 | KCoin reward per mined block |
TRANSACTION_FEE_PERCENT |
1 | Transaction fee percentage |
COLLECT_TX_FEES |
true | Whether to collect transaction fees |
TARGET_BLOCK_TIME_MS |
5000 | Target time per block (ms) |
DIFFICULTY_WINDOW |
5 | Blocks to average for difficulty adjustment |
This project is created for educational and demonstration purposes.