Skip to content

Latest commit

Β 

History

59 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CropChain: Blockchain-Powered Agricultural Traceability

Hyperledger Fabric Node.js React Docker

CropChain is a full-stack blockchain application demonstrating an agricultural product traceability solution using a hybrid approach. It leverages Hyperledger Fabric for an immutable audit trail, MySQL for scalable off-chain data storage, and IPFS for decentralized file storage.

πŸ“Έ Application Screenshots

Dashboard View

CropChain Dashboard

Main dashboard showing product overview and blockchain connectivity status.

Product Management Interface

Product Management

Product management interface for creating, shipping, and tracking agricultural products.

🌟 Features

  • βœ… Immutable Product Tracking - Track agricultural products on the blockchain
  • πŸ“Š Complete History - View full product lifecycle and ownership changes
  • πŸ“¦ Supply Chain Management - Ship and receive products between parties
  • πŸ” Decentralized Storage - Upload certificates and documents to IPFS
  • πŸ“± QR Code Generation - Generate QR codes for product traceability
  • 🎨 Modern UI - React-based responsive user interface with Tailwind CSS
  • πŸ”— Hybrid Architecture - Combines blockchain immutability with database scalability

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   React UI      β”‚
β”‚  (Port 3000)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Node.js/Express Backend             β”‚
β”‚  β€’ REST API                                 β”‚
β”‚  β€’ Fabric Gateway SDK                       β”‚
β”‚  β€’ MySQL Connection Pool                    β”‚
β”‚  β€’ IPFS Client                              β”‚
β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚         β”‚            β”‚
    β”‚    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
    β”‚    β”‚  MySQL   β”‚  β”‚   IPFS   β”‚
    β”‚    β”‚  (3306)  β”‚  β”‚  (5001)  β”‚
    β”‚    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
β”Œβ”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      Hyperledger Fabric Network             β”‚
β”‚  β€’ Orderer (7050)                           β”‚
β”‚  β€’ Peer (7051)                              β”‚
β”‚  β€’ CA (7054)                                β”‚
β”‚  β€’ Chaincode (Go)                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Core Technologies

Component Technology Version
Blockchain Hyperledger Fabric 2.2.0
Backend Node.js + Express.js 18.x
Frontend React + Tailwind CSS 18.2.0
Database MySQL 8.0
File Storage IPFS Latest
Containerization Docker + Docker Compose Latest
Smart Contract Go (Golang) 1.x

πŸ“ Project Structure

cropchain-project/
β”œβ”€β”€ app.js                      # Main Express application
β”œβ”€β”€ package.json                # Backend dependencies
β”œβ”€β”€ Dockerfile                  # Application container
β”œβ”€β”€ docker-compose.yml          # Multi-container orchestration
β”œβ”€β”€ start-network.ps1           # Windows startup script
β”œβ”€β”€ start-network.sh            # Linux/Mac startup script
β”œβ”€β”€ TROUBLESHOOTING.md          # Detailed troubleshooting guide
β”‚
β”œβ”€β”€ chaincode/                  # Hyperledger Fabric Smart Contract
β”‚   β”œβ”€β”€ cropchain.go           # Go chaincode implementation
β”‚   └── go.mod                 # Go module dependencies
β”‚
β”œβ”€β”€ fabric-network/            # Fabric network configuration
β”‚   β”œβ”€β”€ configtx.yaml         # Channel configuration
β”‚   β”œβ”€β”€ crypto-config.yaml    # Crypto material configuration
β”‚   β”œβ”€β”€ connection-org1.yaml  # Fabric SDK connection profile
β”‚   β”œβ”€β”€ crypto-config/        # Generated certificates and keys
β”‚   └── channel-artifacts/    # Channel configuration files
β”‚
β”œβ”€β”€ scripts/                   # Setup and deployment scripts
β”‚   β”œβ”€β”€ setup-fabric.sh       # Network initialization
β”‚   β”œβ”€β”€ setup-channel.sh      # Channel setup
β”‚   β”œβ”€β”€ deploy-chaincode.sh   # Chaincode deployment
β”‚   └── generate-artifacts.sh # Crypto material generation
β”‚
β”œβ”€β”€ ui/                        # React frontend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ App.jsx           # Main React component
β”‚   β”‚   β”œβ”€β”€ main.jsx          # React entry point
β”‚   β”‚   └── index.css         # Tailwind CSS styles
β”‚   β”œβ”€β”€ vite.config.js        # Vite build configuration
β”‚   β”œβ”€β”€ tailwind.config.js    # Tailwind configuration
β”‚   └── package.json          # Frontend dependencies
β”‚
β”œβ”€β”€ db/
β”‚   └── init.sql              # MySQL database initialization
β”‚
β”œβ”€β”€ bin/                       # Fabric binaries
β”‚   β”œβ”€β”€ cryptogen             # Certificate generator
β”‚   β”œβ”€β”€ configtxgen           # Configuration generator
β”‚   └── peer                  # Peer CLI tool
β”‚
└── wallet/                    # Fabric identity wallet (generated)

πŸ”§ Prerequisites

  • Docker Desktop (Windows/Mac) or Docker Engine (Linux)
  • Git Bash or WSL (for Windows users)
  • 8GB RAM minimum
  • 20GB free disk space

πŸš€ Quick Start (Windows)

Step 1: Start the Blockchain Network

.\start-network.ps1

This script will:

  1. Stop and clean existing containers
  2. Start orderer, peer, and CA services
  3. Create the channel cropchainchannel
  4. Join the peer to the channel

Step 2: Deploy the Chaincode

# Use Git Bash or WSL
bash scripts/deploy-chaincode.sh

Or manually:

# Package chaincode
docker exec cli peer lifecycle chaincode package cropchain.tar.gz --path /opt/gopath/src/github.com/hyperledger/fabric/peer/chaincode --lang golang --label cropchain_1

# Install chaincode
docker exec cli peer lifecycle chaincode install cropchain.tar.gz

# Approve chaincode (replace PACKAGE_ID with actual value from install output)
docker exec cli peer lifecycle chaincode approveformyorg -o orderer.example.com:7050 --channelID cropchainchannel --name cropchain --version 1.0 --package-id PACKAGE_ID --sequence 1

# Commit chaincode
docker exec cli peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID cropchainchannel --name cropchain --version 1.0 --sequence 1 --peerAddresses peer0.org1.example.com:7051

Step 3: Start Application Services

docker-compose up -d app mysql ipfs

Step 4: Access the Application

Open your browser and navigate to: http://localhost:3000

🎯 Usage

1. Connect to Blockchain

Click the "Connect to Blockchain" button in the header to initialize the Fabric connection.

2. Create a Product

Navigate to the "Create Product" tab:

  • Enter Product ID (e.g., RICE001)
  • Select Product Type (Rice, Wheat, Coffee, etc.)
  • Enter Farmer Name
  • Add Description
  • Optionally upload a certificate to IPFS

3. Manage Products

Use the "Manage Products" tab to:

  • Ship products to a new owner
  • Receive products and update ownership

4. View History

Access the "View History" tab to:

  • Query product history by ID
  • See complete ownership and status changes
  • View timestamps and transaction IDs

5. Browse All Products

The "All Products" tab displays:

  • All products on the blockchain
  • Search and filter capabilities
  • Current product status

πŸ”Œ API Endpoints

Blockchain Connection

POST /api/connect-blockchain

Initialize connection to the Fabric network.

Product Management

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

{
  "id": "RICE001",
  "type": "Rice",
  "farmerName": "John Farmer",
  "description": "Premium Basmati Rice",
  "ipfsHash": "QmXxx...",
  "fileName": "certificate.pdf"
}

Ship Product

POST /api/products/:id/ship
Content-Type: application/json

{
  "newOwner": "Distributor A"
}

Receive Product

POST /api/products/:id/receive
Content-Type: application/json

{
  "newOwner": "Retailer B"
}

Get Product History

GET /api/products/:id/history

Generate QR Code

GET /api/products/:id/qrcode

Query All Products

GET /api/products/queryAll

Upload to IPFS

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

document: [file]

πŸ› οΈ Common Commands

View Container Status

docker ps

Check Application Logs

docker logs cropchain_app --tail 50 -f

Check Peer Logs

docker logs peer0.org1.example.com --tail 50 -f

Query Chaincode Directly

docker exec cli peer chaincode query -C cropchainchannel -n cropchain -c '{"Args":["QueryAllProducts"]}'

Invoke Chaincode Directly

docker exec cli peer chaincode invoke -o orderer.example.com:7050 -C cropchainchannel -n cropchain -c '{"function":"CreateProduct","Args":["TEST001","Rice","TestFarmer","hash1","hash2"]}'

Stop All Services

docker-compose down

Clean Restart (Remove All Data)

docker-compose down -v
docker system prune -f
.\start-network.ps1
bash scripts/deploy-chaincode.sh
docker-compose up -d app mysql ipfs

πŸ› Troubleshooting

For detailed troubleshooting, see TROUBLESHOOTING.md

Common Issues

Containers not starting:

docker-compose down -v
.\start-network.ps1

Blockchain connection fails:

  • Ensure all containers are running: docker ps
  • Check logs: docker logs cropchain_app
  • Click "Connect to Blockchain" in the UI

Chaincode errors:

  • Verify chaincode is committed: docker exec cli peer lifecycle chaincode querycommitted --channelID cropchainchannel
  • Check chaincode container: docker ps | grep dev-peer

πŸ“ Key Fixes Applied

  1. βœ… Fixed CA container configuration
  2. βœ… Updated connection profile for container networking
  3. βœ… Changed to cryptogen-based admin identity
  4. βœ… Disabled discovery service for compatibility
  5. βœ… Fixed markdown linting issues
  6. βœ… Added comprehensive spell checker dictionary
  7. βœ… Simplified JavaScript code patterns

πŸŽ“ Learning Resources

πŸ“„ License

This project is created for educational purposes.

πŸ‘₯ Contributing

This is an educational project. Feel free to fork and experiment!

πŸ™ Acknowledgments

  • Hyperledger Fabric Community
  • Docker Community
  • IPFS Community

Built with ❀️ for Agricultural Supply Chain Traceability

About

CropChain is a hybrid blockchain solution for agricultural traceability. It uses Hyperledger Fabric for an immutable audit trail, MySQL for scalable data, and IPFS for decentralized file storage. The entire stack is containerized with Docker Compose for easy local deployment.

Resources

Stars

7 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages