Skip to content

Blockchain-based product authenticity verification system using BNB Greenfield for decentralized storage and BSC for immutable hash verification. Features QR code generation, supply chain tracking, and tamper-proof document verification.

Notifications You must be signed in to change notification settings

HashimCodeDev/ScanChain

Repository files navigation

ScanChain - Product Authenticity Verification

ScanChain is a blockchain-based product authenticity verification system built on BNB Greenfield for decentralized storage and BNB Smart Chain for immutable hash storage. The system allows manufacturers to upload product documents and generate QR codes for consumer verification.

🌟 Features

  • File Storage: Secure file storage on BNB Greenfield Testnet
  • Blockchain Verification: Immutable hash storage on BSC Testnet
  • QR Code Generation: Dynamic QR codes for easy product verification
  • REST API: Clean API endpoints for upload and verification
  • Hash Verification: SHA-256 hash comparison for tamper detection
  • Multi-format Support: PDF and JSON file support

πŸŽ₯ Demo Video

Watch ScanChain Demo

See ScanChain in action - from product upload to QR code verification!

πŸ› οΈ Tech Stack

  • Backend: Python with Flask
  • Blockchain: BNB Smart Chain (BSC) Testnet
  • Storage: BNB Greenfield Testnet
  • Hashing: SHA-256 via hashlib
  • QR Codes: qrcode library
  • Smart Contract: Solidity

πŸ“‹ Prerequisites

Before running this project, ensure you have:

  • Python (v3.8 or later)
  • pip
  • BSC Testnet account with test BNB
  • BNB Greenfield Testnet account
  • Git

πŸš€ Quick Start

1. Clone and Install

git clone https://github.com/HashimCodeDev/ScanChain.git
cd ScanChain
uv sync

2. Environment Setup

Copy the environment file and configure:

cp .env.example .env

Update .env with your credentials:

# BNB Greenfield Configuration
GREENFIELD_ADDRESS=your_testnet_address
GREENFIELD_PRIVATE_KEY=your_testnet_private_key

# BNB Smart Chain Configuration  
CONTRACT_ADDRESS=0x...
BSC_RPC_URL=https://data-seed-prebsc-1-s1.binance.org:8545
BSC_PRIVATE_KEY=your_bsc_private_key

# Server Configuration
PORT=3000
NODE_ENV=development

# Greenfield Testnet Configuration
GREENFIELD_RPC_URL=https://gnfd-testnet-fullnode-tendermint-us.bnbchain.org:443
GREENFIELD_CHAIN_ID=5600

3. Deploy Smart Contract

Deploy the contract using Remix IDE:

  • Contract code: contracts/ProductAuthenticity.sol
  • Network: BSC Testnet (Chain ID: 97)
  • Update CONTRACT_ADDRESS in .env after deployment

4. Run Tests

uv run python test_app.py

5. Start Server

uv run python app.py
# or
uv run python run.py

The server will start on http://localhost:5000

πŸ“‘ API Endpoints

Health Check

GET /api/health

Upload Product File

POST /api/upload
Content-Type: multipart/form-data

Body:
- productId: string (required)
- file: file (PDF or JSON, max 10MB)

Response:

{
  "success": true,
  "productId": "ABC123456789",
  "fileHash": "sha256_hash_here",
  "greenfieldUrl": "https://gnfd-testnet-sp1.bnbchain.org/bucket/file",
  "txHash": "0x...",
  "qrCode": "data:image/png;base64,..."
}

Verify Product

POST /api/verify
Content-Type: application/json

{
  "productId": "ABC123456789",
  "greenfieldUrl": "https://gnfd-testnet-sp1.bnbchain.org/bucket/file"
}

Response:

{
  "success": true,
  "isVerified": true,
  "productId": "ABC123456789",
  "storedHash": "original_hash",
  "currentHash": "current_hash",
  "message": "Product is authentic"
}

Get Product Info

GET /api/product/:productId

QR Code Operations

Generate QR Code

POST /api/qr/generate
{
  "productId": "ABC123456789",
  "contractAddress": "0x...",
  "metadata": {
    "manufacturer": "Company Name",
    "productName": "Product Name"
  }
}

Parse QR Code

POST /api/qr/parse
{
  "qrData": "qr_code_json_string"
}

Scan QR Code

POST /api/qr/scan
{
  "qrData": "qr_code_json_string"
}

πŸ—οΈ Project Structure

ScanChain/
β”œβ”€β”€ contracts/
β”‚   └── ProductAuthenticity.sol      # Smart contract
β”œβ”€β”€ services/
β”‚   β”œβ”€β”€ greenfield_service.py        # BNB Greenfield integration
β”‚   β”œβ”€β”€ blockchain_service.py        # BSC integration
β”‚   β”œβ”€β”€ qr_service.py                # QR code utilities
β”‚   β”œβ”€β”€ auth_service.py              # Authentication service
β”‚   └── database_service.py          # Database operations
β”œβ”€β”€ routes/
β”‚   └── auth_routes.py               # Authentication routes
β”œβ”€β”€ public/
β”‚   β”œβ”€β”€ *.html                       # Frontend files
β”œβ”€β”€ data/
β”‚   └── *.json                       # Data storage files
β”œβ”€β”€ app.py                           # Main Flask application
β”œβ”€β”€ run.py                           # Server runner
β”œβ”€β”€ test_app.py                      # Test suite
β”œβ”€β”€ pyproject.toml                   # Python dependencies (uv)
β”œβ”€β”€ .env
└── README.md

πŸ”§ Smart Contract Functions

The ProductAuthenticity.sol contract provides:

  • storeProductHash(string productId, string fileHash) - Store product hash
  • getProductHash(string productId) - Retrieve product hash
  • getProductInfo(string productId) - Get full product information
  • productExists(string productId) - Check if product exists

🌐 Network Configuration

BSC Testnet

BNB Greenfield Testnet

πŸ§ͺ Testing

Run the test suite to verify functionality:

uv run python test_app.py

This will test:

  • Environment variables
  • Python dependencies
  • Hash generation
  • JSON processing
  • Service availability

πŸ”’ Security Features

  • Input Validation: All inputs are validated and sanitized
  • File Type Restrictions: Only PDF and JSON files allowed
  • Size Limits: 10MB maximum file size
  • Error Handling: Comprehensive error handling and logging
  • Environment Variables: Sensitive data stored in environment variables

🚨 Error Handling

The API returns consistent error responses:

{
  "success": false,
  "error": "Error message description"
}

Common error codes:

  • 400: Bad Request (validation errors)
  • 404: Not Found (product not found)
  • 500: Internal Server Error
  • 503: Service Unavailable (blockchain/storage issues)

πŸ“ˆ Development

Adding New Features

  1. Create new service files in services/
  2. Add routes in routes/
  3. Update validation in Flask app
  4. Add tests in test_app.py

Environment Variables

Required variables:

  • GREENFIELD_ADDRESS - Your Greenfield address
  • GREENFIELD_PRIVATE_KEY - Greenfield private key
  • CONTRACT_ADDRESS - Deployed contract address
  • BSC_PRIVATE_KEY - BSC account private key

Optional variables:

  • PORT - Server port (default: 3000)
  • NODE_ENV - Environment mode

πŸ› Troubleshooting

Common Issues

  1. Contract not deployed: Deploy using Remix IDE with the provided Solidity contract
  2. Network issues: Check RPC URLs and internet connection
  3. Private key errors: Ensure keys are valid and have test funds
  4. File upload fails: Check file size and type restrictions

Development Mode

The application includes fallbacks for development:

  • Mock Greenfield URLs when service unavailable
  • Mock blockchain transactions when contract not deployed
  • Detailed error messages in development mode

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

πŸ“„ License

This project is licensed under the MIT License.

πŸ†˜ Support

For support and questions:

  • Check the troubleshooting section
  • Run uv run python test_app.py to verify setup
  • Review server logs for error details

🎯 Roadmap

Future enhancements:

  • Frontend web application
  • Mobile app for QR scanning
  • Batch upload functionality
  • Advanced analytics dashboard
  • Multi-chain support
  • IPFS integration as backup storage

Built with ❀️ for the BNB Chain ecosystem

About

Blockchain-based product authenticity verification system using BNB Greenfield for decentralized storage and BSC for immutable hash verification. Features QR code generation, supply chain tracking, and tamper-proof document verification.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published