chore: refactor logging module, update genesis timestamp, remove model relationships, and reorganize routers - Rename logging.py to logger.py and update import paths in poa.py and main.py - Update devnet genesis timestamp to 1766828620 - Remove SQLModel Relationship declarations from Block, Transaction, and Receipt models - Add SessionDep type alias and get_session dependency in coordinator-api deps - Reorganize coordinator-api routers: replace explorer/registry with exchange, users, marketplace
5.0 KiB
5.0 KiB
User Management System for AITBC Trade Exchange
Overview
The AITBC Trade Exchange now includes a complete user management system that allows individual users to have their own wallets, balances, and transaction history. Each user is identified by their wallet address and has a unique session for secure operations.
Features Implemented
1. User Registration & Login
- Wallet-based Authentication: Users connect with their wallet address
- Auto-registration: New wallets automatically create a user account
- Session Management: Secure token-based sessions (24-hour expiry)
- User Profiles: Each user has a unique ID, email, and username
2. Wallet Management
- Individual Wallets: Each user gets their own AITBC wallet
- Balance Tracking: Real-time balance updates
- Address Generation: Unique wallet addresses for each user
3. Transaction History
- Personal Transactions: Each user sees only their own transactions
- Transaction Types: Buy, sell, deposit, withdrawal tracking
- Status Updates: Real-time transaction status
API Endpoints
User Authentication
POST /api/users/login
{
"wallet_address": "aitbc1abc123..."
}
Response:
{
"user_id": "uuid",
"email": "wallet@aitbc.local",
"username": "user_abc123",
"created_at": "2025-12-28T...",
"session_token": "sha256_token"
}
User Profile
GET /api/users/me
Headers: X-Session-Token: <token>
User Balance
GET /api/users/{user_id}/balance
Headers: X-Session-Token: <token>
Response:
{
"user_id": "uuid",
"address": "aitbc_uuid123",
"balance": 1000.0,
"updated_at": "2025-12-28T..."
}
Transaction History
GET /api/users/{user_id}/transactions
Headers: X-Session-Token: <token>
Logout
POST /api/users/logout
Headers: X-Session-Token: <token>
Frontend Implementation
1. Connect Wallet Flow
- User clicks "Connect Wallet"
- Generates a demo wallet address
- Calls
/api/users/loginwith wallet address - Receives session token and user data
- Updates UI with user info
2. UI Components
- Wallet Section: Shows address, username, balance
- Connect Button: Visible when not logged in
- Logout Button: Clears session and resets UI
- Balance Display: Real-time AITBC balance
3. Session Management
- Session token stored in JavaScript variable
- Token sent with all API requests
- Automatic logout on token expiry
- Manual logout option
Database Schema
Users Table
id: UUID (Primary Key)email: Unique stringusername: Unique stringstatus: active/inactive/suspendedcreated_at: Timestamplast_login: Timestamp
Wallets Table
id: Integer (Primary Key)user_id: UUID (Foreign Key)address: Unique stringbalance: Floatcreated_at: Timestampupdated_at: Timestamp
Transactions Table
id: UUID (Primary Key)user_id: UUID (Foreign Key)wallet_id: Integer (Foreign Key)type: deposit/withdrawal/purchase/etc.status: pending/completed/failedamount: Floatfee: Floatcreated_at: Timestampconfirmed_at: Timestamp
Security Features
1. Session Security
- SHA-256 hashed tokens
- 24-hour automatic expiry
- Server-side session validation
- Secure token invalidation on logout
2. API Security
- Session token required for protected endpoints
- User isolation (users can only access their own data)
- Input validation and sanitization
3. Future Enhancements
- JWT tokens for better scalability
- Multi-factor authentication
- Biometric wallet support
- Hardware wallet integration
How It Works
1. First Time User
- User connects wallet
- System creates new user account
- Wallet is created and linked to user
- Session token issued
- User can start trading
2. Returning User
- User connects wallet
- System finds existing user
- Updates last login
- Issues new session token
- User sees their balance and history
3. Trading
- User initiates purchase
- Payment request created with user_id
- Bitcoin payment processed
- AITBC credited to user's wallet
- Transaction recorded
Testing
Test Users
Each wallet connection creates a unique user:
- Address:
aitbc1wallet_[random]x... - Email:
wallet@aitbc.local - Username:
user_[last_8_chars]
Demo Mode
- No real registration required
- Instant wallet creation
- Testnet Bitcoin support
- Simulated balance updates
Next Steps
1. Enhanced Features
- Email verification
- Password recovery
- 2FA authentication
- Profile customization
2. Advanced Trading
- Limit orders
- Stop-loss
- Trading history analytics
- Portfolio tracking
3. Integration
- MetaMask support
- WalletConnect protocol
- Hardware wallets (Ledger, Trezor)
- Mobile wallet apps
Support
For issues or questions:
- Check the logs:
journalctl -u aitbc-coordinator -f - API endpoints:
https://aitbc.bubuit.net/api/docs - Trade Exchange:
https://aitbc.bubuit.net/Exchange