Skip to content

Latest commit

 

History

History
465 lines (328 loc) · 11.1 KB

File metadata and controls

465 lines (328 loc) · 11.1 KB

QR Code System Implementation

Date: October 17, 2025
Phase: 2A - QR Code Generation & Verification

Overview

Implemented complete QR code system for ticket authentication with generation, scanning, and blockchain verification capabilities.

Features Implemented

1. QR Code Generation (useQRCode hook)

File: frontend/ticketchain/src/hooks/useQRCode.ts

Features:

  • Dynamic QR Code Generation - Creates QR codes with embedded ticket data
  • Data Integrity - Simple hash signature to prevent tampering
  • High Error Correction - Level H for better scanning reliability
  • Download Functionality - Export QR codes as PNG images
  • Expiration Checking - QR codes expire after 24 hours
  • Format Validation - Verifies QR code structure and signature

Data Structure:

interface QRCodeData {
  tokenId: bigint; // NFT token ID
  eventId: bigint; // Event ID
  owner: string; // Owner's wallet address
  contractAddress: string; // TicketNFT contract address
  chainId: number; // 42101 (Push Chain Testnet)
  timestamp: number; // Generation timestamp
}

Security:

  • Simple hash signature: generateSimpleHash(data)
  • Signature verification on scan
  • Timestamp validation (24-hour expiry)
  • Prevents QR code replay attacks

Note: In production, replace simple hash with proper cryptographic signing (e.g., ECDSA with private key).

2. Enhanced QR Code Display Component

File: frontend/ticketchain/src/components/QRCodeDisplay.tsx

Props:

{
  tokenId: bigint;
  eventId: bigint;
  owner: string;
  showDownload?: boolean;  // Default: true
  size?: 'sm' | 'md' | 'lg';  // Default: 'md'
  className?: string;
}

Features:

  • Three Size Options:

    • Small: 128x128px (w-32)
    • Medium: 192x192px (w-48)
    • Large: 256x256px (w-64)
  • Loading State - Animated spinner during generation

  • Error Handling - User-friendly error messages

  • Download Button - One-click QR code export

  • Info Display - Shows ticket and event IDs

  • Styling - White background, gray border, shadow effects

Usage Example:

<QRCodeDisplay
  tokenId={ticketData.tokenId}
  eventId={ticketData.eventId}
  owner={userAddress}
  size="lg"
  showDownload={true}
/>

3. QR Code Scanner Component

File: frontend/ticketchain/src/components/QRScanner.tsx

Technology:

  • Library: html5-qrcode
  • Camera: Back-facing (environment mode)
  • FPS: 10 frames per second
  • Scan Box: 250x250px

Features:

  • Auto-start/Stop - Manual camera controls
  • Real-time Scanning - Continuous QR code detection
  • Success Feedback - Green overlay + check icon
  • Error Handling - Shows camera/scan errors
  • Auto-cleanup - Properly releases camera on unmount
  • Responsive Design - Works on mobile and desktop

States:

  • Idle (camera off)
  • Scanning (camera active)
  • Success (QR code detected)
  • Error (camera/scan failure)

Events:

  • onScan(decodedText) - Called when QR code is successfully scanned
  • onError(error) - Called on camera or scanning errors

4. Organizer Verification Page

File: frontend/ticketchain/src/pages/OrganizerVerificationPage.tsx

Purpose:

Complete ticket verification system for event organizers to validate tickets at entry.

Verification Steps:

  1. QR Code Scan - Decode ticket data
  2. Format Validation - Verify JSON structure and signature
  3. Blockchain Verification:
    • Read ticket details from TicketNFT contract
    • Verify current owner matches QR code owner
    • Check if ticket has been used
    • Confirm event ID matches
  4. Display Result - Show verification outcome with color coding

Verification Results:

✅ Success (Green Border):

  • Owner address matches blockchain
  • Ticket is not marked as used
  • Event ID matches
  • All verifications pass

⚠️ Warning (Yellow Border):

  • Owner mismatch (ticket may have been transferred)
  • QR code still valid but requires manual check

❌ Error (Red Border):

  • Ticket already used
  • Invalid signature
  • Expired QR code
  • Event ID mismatch
  • Invalid format

UI Components:

  • Header - Shows organizer address and instructions
  • Scanner Section - Camera view with controls
  • Verification Result Card:
    • Status icon (check/warning/error)
    • Status message
    • Ticket details (ID, event, owner)
    • Verification checklist
    • "Scan Next Ticket" button

Data Displayed:

  • Ticket ID with # prefix
  • Event ID with # prefix
  • Full owner address (truncated in some views)
  • Owner verification status
  • Used ticket status

5. Router Integration

File: frontend/ticketchain/src/App.tsx

Added new route:

<Route path="/organizer-scan" element={<OrganizerVerificationPage />} />

6. Navigation Integration

File: frontend/ticketchain/src/components/Header.tsx

Added "Scan Tickets" link:

  • Only visible when wallet is connected
  • Located next to "My Tickets" in navigation
  • Directs to /organizer-scan

Libraries Installed

{
  "qrcode": "^1.5.4", // QR code generation
  "@types/qrcode": "^1.5.5", // TypeScript types
  "qrcode.react": "^4.2.0", // React QR code components
  "html5-qrcode": "latest" // QR code scanning
}

Removed: react-qr-scanner (React 19 compatibility issues)

Integration Points

Where to Use QRCodeDisplay:

  1. My Tickets Page - Show QR code for each ticket

    {
      tickets.map((ticket) => (
        <div key={ticket.tokenId}>
          <TicketCard {...ticket} />
          <QRCodeDisplay
            tokenId={ticket.tokenId}
            eventId={ticket.eventId}
            owner={userAddress!}
          />
        </div>
      ));
    }
  2. Ticket Detail Modal - Full-size QR code with download

    <QRCodeDisplay
      tokenId={selectedTicket.tokenId}
      eventId={selectedTicket.eventId}
      owner={userAddress!}
      size="lg"
      showDownload={true}
    />
  3. Verification Page - Display after ticket lookup

Where Organizers Access Scanner:

  1. Navigation Header - "Scan Tickets" link (when connected)
  2. Direct URL - /organizer-scan
  3. Event Detail Page - Could add "Verify Tickets" button for event owners

Mobile Optimization

QR Code Generation:

  • ✅ Responsive sizing (sm/md/lg options)
  • ✅ Touch-friendly download button
  • ✅ Readable on small screens

QR Code Scanner:

  • ✅ Uses device back camera
  • ✅ Full-width scanner box
  • ✅ Large touch targets for controls
  • ✅ Mobile-optimized scan box (250x250px)
  • ⚠️ Note: Camera permissions must be granted on mobile

Verification Page:

  • ✅ Responsive layout
  • ✅ Full-screen on mobile
  • ✅ Large result cards
  • ✅ Touch-friendly buttons
  • ✅ Readable text sizes

Testing Checklist

QR Code Generation:

  • Generate QR code for ticket
  • Download QR code as PNG
  • Verify QR code contains correct data
  • Check signature validation
  • Test expiration (wait 24 hours)
  • Test with different ticket IDs

QR Code Scanner:

  • Grant camera permissions
  • Start scanner successfully
  • Scan valid ticket QR code
  • Verify success animation shows
  • Stop scanner successfully
  • Test on mobile device
  • Test in low light conditions

Verification System:

  • Scan valid ticket (should pass)
  • Scan used ticket (should fail)
  • Scan ticket with wrong owner (should warn)
  • Scan expired QR code (should fail)
  • Scan invalid/tampered QR code (should fail)
  • Scan ticket for different event (should fail)
  • Verify blockchain data loads correctly
  • Test "Scan Next Ticket" button

Mobile Testing:

  • Scanner works on iOS Safari
  • Scanner works on Android Chrome
  • QR code displays correctly on phone
  • Download works on mobile
  • Verification page responsive
  • Camera switches correctly

Security Considerations

Current Implementation:

  • Simple hash-based signature
  • 24-hour QR code expiration
  • Blockchain verification of ownership and status

Production Recommendations:

  1. Cryptographic Signing:

    // Replace generateSimpleHash with ECDSA
    import { ethers } from "ethers";
    
    const signer = new ethers.Wallet(privateKey);
    const signature = await signer.signMessage(JSON.stringify(data));
  2. Backend Verification:

    • Move signature generation to backend API
    • Verify signatures server-side
    • Prevent client-side tampering
  3. Rate Limiting:

    • Limit QR code generation requests
    • Prevent spam scanning attempts
  4. Audit Trail:

    • Log all scan attempts
    • Track ticket usage on-chain
    • Create verification history
  5. Token Expiration:

    • Consider shorter expiration (e.g., event day only)
    • Implement dynamic expiration based on event date

Known Limitations

  1. Camera Permissions:

    • User must grant camera access
    • No fallback for denied permissions (yet)
  2. QR Code Expiration:

    • Fixed 24-hour expiration
    • Should be tied to event date
  3. Offline Scanning:

    • Requires network connection for blockchain verification
    • No offline mode implemented
  4. Simple Signature:

    • Not cryptographically secure
    • Should be upgraded for production
  5. No Ticket Marking:

    • Verification doesn't mark ticket as used
    • Requires separate transaction (future feature)

Future Enhancements

Phase 2B Potential Features:

  1. Mark Ticket as Used:

    function markTicketUsed(uint256 tokenId) external onlyOrganizer
  2. Offline Verification:

    • Cache ticket data
    • Sync when online
  3. Batch Scanning:

    • Scan multiple tickets quickly
    • Queue verification requests
  4. Analytics Dashboard:

    • Show scan statistics
    • Entry flow metrics
    • Real-time attendance
  5. Access Control:

    • Only event organizers can verify
    • Role-based permissions
  6. Multi-Device Sync:

    • Multiple scanners per event
    • Real-time verification sync

File Summary

New Files Created (7):

  1. hooks/useQRCode.ts (147 lines) - QR generation and verification
  2. components/QRCodeDisplay.tsx (97 lines) - Enhanced display component
  3. components/QRScanner.tsx (160 lines) - Camera scanner component
  4. pages/OrganizerVerificationPage.tsx (261 lines) - Full verification page

Modified Files (3):

  1. App.tsx - Added /organizer-scan route
  2. Header.tsx - Added "Scan Tickets" navigation link
  3. package.json - Updated dependencies (removed react-qr-scanner, added html5-qrcode)

Documentation:

  1. docs/qr-code-implementation.md - This file

Quick Start Guide

For Ticket Holders:

  1. Go to "My Tickets"
  2. View QR code for your ticket
  3. Download or show on phone
  4. Present at event entry

For Organizers:

  1. Connect wallet
  2. Click "Scan Tickets" in header
  3. Click "Start Scanning"
  4. Point camera at ticket QR code
  5. Wait for verification result
  6. Grant entry if green (success)
  7. Deny entry if red (error/used)
  8. Manually verify if yellow (warning)

Status: ✅ Complete - QR code system fully functional and integrated Next Steps: Mobile optimization testing and end-to-end flow testing