
Complete rewrite from Python/FastAPI to Node.js stack: Features implemented: - User authentication with JWT tokens and role-based access (DEVELOPER/BUYER) - Blockchain wallet linking and management with Ethereum integration - Carbon project creation and management for developers - Marketplace for browsing and purchasing carbon offsets - Transaction tracking with blockchain integration - Comprehensive input validation with Joi - Advanced security with Helmet, CORS, and rate limiting - Error handling and logging middleware - Health check endpoint with service monitoring Technical stack: - Node.js with Express.js and TypeScript - Prisma ORM with SQLite database - Web3.js and Ethers.js for blockchain integration - JWT authentication with bcrypt password hashing - Comprehensive validation and security middleware - Production-ready error handling and logging Database schema: - Users with wallet linking capabilities - Carbon projects with verification status - Carbon offsets with blockchain token tracking - Transactions with confirmation details Environment variables required: - JWT_SECRET (required) - DATABASE_URL (optional, defaults to SQLite) - BLOCKCHAIN_RPC_URL (optional, defaults to localhost) - NODE_ENV, PORT, CORS_ORIGIN (optional) Run with: npm install && npm run db:generate && npm run db:migrate && npm run dev
7.2 KiB
7.2 KiB
Carbon Offset Trading Platform
A blockchain-enabled carbon offset trading platform that connects project developers with willing buyers. This platform allows project developers to list their carbon offset projects and enables buyers to purchase verified carbon credits through blockchain transactions.
Features
For Project Developers
- Register as a project developer
- Create and manage carbon offset projects
- Upload project verification documents
- Track project performance and sales
- Integrate with blockchain for tokenization
For Buyers
- Register as a buyer
- Browse verified carbon offset projects
- Purchase carbon credits
- Link blockchain wallets for transactions
- Track purchase history and carbon offset ownership
Blockchain Integration
- Wallet linking support (Ethereum-compatible)
- Smart contract integration for carbon credit tokens
- Transaction verification and tracking
- Secure blockchain-based ownership records
Tech Stack
- Backend: Node.js with Express.js and TypeScript
- Database: SQLite with Prisma ORM
- Authentication: JWT tokens with bcrypt password hashing
- Blockchain: Web3.js and Ethers.js for Ethereum integration
- Validation: Joi for request validation
- Security: Helmet, CORS, Rate limiting
Project Structure
├── src/
│ ├── server.ts # Express application entry point
│ ├── types/ # TypeScript type definitions
│ │ └── index.ts # Main types and interfaces
│ ├── routes/ # API endpoints
│ │ ├── auth.ts # Authentication endpoints
│ │ ├── wallet.ts # Wallet management endpoints
│ │ ├── projects.ts # Project management endpoints
│ │ └── trading.ts # Trading and marketplace endpoints
│ ├── middleware/ # Express middleware
│ │ ├── auth.ts # Authentication middleware
│ │ ├── validation.ts # Request validation middleware
│ │ ├── security.ts # Security middleware
│ │ └── error.ts # Error handling middleware
│ ├── services/ # Business logic services
│ │ ├── blockchain.ts # Blockchain integration
│ │ └── wallet.ts # Wallet management
│ └── utils/ # Utility functions
│ ├── database.ts # Database connection and utilities
│ └── auth.ts # Authentication utilities
├── prisma/
│ └── schema.prisma # Database schema
├── storage/ # Application storage directory
│ └── db/ # SQLite database files
├── package.json # Node.js dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── .env.example # Environment variables template
Installation
- Install dependencies:
npm install
- Set up environment variables:
cp .env.example .env
# Edit .env with your configuration
- Generate Prisma client and run migrations:
npm run db:generate
npm run db:migrate
Running the Application
Development
Start the development server with hot reload:
npm run dev
Production
Build and start the production server:
npm run build
npm start
The application will be available at:
- API: http://localhost:8000
- Health Check: http://localhost:8000/health
- API Documentation: http://localhost:8000/api/docs
Environment Variables
Variable | Description | Required | Default |
---|---|---|---|
NODE_ENV |
Environment mode | No | development |
PORT |
Server port | No | 8000 |
JWT_SECRET |
JWT token secret | Yes | - |
DATABASE_URL |
SQLite database file path | No | file:./storage/db/database.db |
BLOCKCHAIN_RPC_URL |
Ethereum RPC endpoint | No | http://localhost:8545 |
CORS_ORIGIN |
CORS allowed origins | No | * |
API Endpoints
Authentication
POST /api/auth/register
- Register new user (developer or buyer)POST /api/auth/login
- User login
Wallet Management
POST /api/wallet/link
- Link blockchain wallet to user accountDELETE /api/wallet/unlink
- Unlink wallet from user accountGET /api/wallet/info
- Get wallet information and balancePOST /api/wallet/generate-test-wallet
- Generate test wallet for development
Project Management (Developers)
POST /api/projects/
- Create new carbon offset projectGET /api/projects/my-projects
- Get developer's projectsPUT /api/projects/{project_id}
- Update projectDELETE /api/projects/{project_id}
- Delete project
Marketplace & Trading
GET /api/projects/
- Browse all available projectsGET /api/projects/{project_id}
- Get project detailsPOST /api/trading/purchase
- Purchase carbon offsetsGET /api/trading/my-transactions
- Get user's transactionsGET /api/trading/marketplace/stats
- Get marketplace statistics
System
GET /
- Platform informationGET /health
- Health check endpoint
Database Schema
Users
- User authentication and profile information
- Wallet linking for blockchain integration
- User types: DEVELOPER or BUYER
Carbon Projects
- Project details and metadata
- Verification status and documents
- Credit availability and pricing
- Blockchain contract information
Carbon Offsets
- Individual carbon credit tokens
- Serial numbers and vintage years
- Blockchain token IDs and hashes
- Ownership tracking
Transactions
- Purchase records and blockchain transactions
- Transaction status and confirmation details
- Gas usage and block information
Development
Database Migrations
Generate Prisma client:
npm run db:generate
Create and apply migrations:
npm run db:migrate
View database in Prisma Studio:
npm run db:studio
Code Quality
Run linting:
npm run lint
Fix linting issues:
npm run lint:fix
Build TypeScript:
npm run build
Testing Wallet Integration
Use the test wallet generation endpoint to create wallets for development:
curl -X POST http://localhost:8000/api/wallet/generate-test-wallet
Security Features
- JWT-based authentication with secure token generation
- Password hashing with bcrypt (12 rounds)
- Role-based access control (DEVELOPER/BUYER)
- Rate limiting and CORS protection
- Helmet security headers
- Input validation with Joi
- Blockchain wallet verification
- Transaction signing and verification
Production Deployment
- Set production environment variables in
.env
- Build the application:
npm run build
- Start the production server:
npm start
- Ensure database is properly migrated
- Configure reverse proxy (nginx/Apache)
- Set up SSL certificates
- Configure firewall and security groups
Contributing
- Follow TypeScript best practices
- Use the existing code structure and patterns
- Add comprehensive input validation
- Include proper error handling
- Update documentation for API changes
- Test blockchain integration thoroughly
- Follow the established commit message format
License
This project is part of a carbon offset trading platform implementation.