Skip to content

DigarSingh/Hack-The-Winter---prototype

Repository files navigation

Proximity-Verified Delivery — Prototype

Status: FULLY IMPLEMENTED AND TESTED (December 25, 2025)

Short description: A privacy-first, proximity-verified proof-of-delivery (PoD) prototype that lets a customer temporarily enable a short‑range signal at delivery time. Only an authenticated delivery partner within physical proximity can complete delivery. A tamper-resistant proof (event hash) is anchored on a blockchain for audit and dispute resolution.

What's Working:

  • ✓ Smart contract deployed on local blockchain (Hardhat)
  • ✓ Backend API with session management and challenge-response
  • ✓ ECDSA signature verification (secp256k1)
  • ✓ Blockchain anchoring of delivery events
  • ✓ CLI simulation tools for testing without hardware
  • ✓ End-to-end test successfully completed

Table of contents

  1. Overview & goals
  2. Threat model & assumptions
  3. High-level architecture
  4. Sequence diagrams / Flow charts (Mermaid)
  5. Data flow diagrams (DFD)
  6. Component design & interfaces (APIs, messages)
  7. Prototype implementation details (code snippets)
    • Smart contract (Solidity)
    • Backend (Node.js / Express) sample
    • Mobile (React Native) BLE challenge-response sample
    • CLI simulator to test without hardware
  8. How to run the prototype (local simulation)
  9. Audit & verification process
  10. Privacy & compliance notes
  11. Performance, scaling and operational considerations
  12. What we'll add / improve in Round 2 (mandatory)
  13. Appendix: data models, sample JSON, test vectors

1. Overview & goals

Problem: Current last-mile PoD methods (GPS geofencing, manual confirmation) are spoofable and lack doorstep-level assurance. Backends and logs can be contested in multi-stakeholder ecosystems.

Solution (prototype):

  • Customer activates a short-lived, short-range signal (BLE peripheral or UWB) for a delivery session (session_id) at the time of delivery.
  • Delivery Partner (DP) app scans and detects the customer's ephemeral session token only when physically proximate (BLE range or UWB). Detection alone is not enough—DP must perform an authenticated cryptographic challenge-response using the ephemeral token plus server-issued challenge.
  • Backend verifies DP signature and session token, records a delivery event containing minimal metadata (non-sensitive), publishes a hash of the event to a blockchain smart contract to create an immutable anchor.
  • The anchored transaction id (txHash) plus server-side secure logs form a verifiable, auditable proof-of-delivery.

Design goals:

  • Doorstep-level proximity (short-range) verification.
  • Explicit customer participation (they enable the proximity window).
  • Cryptographically verifiable proof that is tamper-resistant.
  • Privacy: avoid placing exact geolocation on-chain; use ephemeral session tokens and hash-only anchors.
  • Scalable: minimal blockchain writes (store hashes only), and optional batching.
  • Cross-platform: mobile apps for customer + DP (React Native sample); backend REST API; Solidity contract for anchoring.

2. Threat model & assumptions

Threats:

  • GPS spoofing or faked geofence events.
  • Replay attacks where a captured BLE token is reused.
  • Delivery partner fakes completion without being physically present.
  • Backend log tampering.

Assumptions:

  • Customer device supports BLE peripheral mode or can display a rotating QR code if peripheral mode isn't available.
  • Delivery partner app holds a long-term key pair (ECDSA) provisioned by the logistics operator (the DP private key is kept secure on device).
  • Network connectivity for final submission; offline buffering allowed but anchoring on-chain may be delayed (server records timestamped signed event locally and will anchor later while preserving tamper-evidence via local tamper-evident logs).

Mitigations:

  • Ephemeral session tokens with short TTL (e.g., 2–5 minutes).
  • Nonce/challenge issued by backend to avoid replay.
  • Signed delivery bundles from DP (signed with DP private key) and optional customer co-sign.
  • Blockchain hash anchoring for immutable audit trail.

3. High-level architecture

Components:

  • Customer App (Mobile) — toggles delivery window, acts as BLE peripheral or displays QR, collects customer confirmation (optional photo), sends session activation to Backend.
  • Delivery Partner App (Mobile) — scans for ephem. BLE session tokens, receives server challenge, performs cryptographic response, uploads signed event and optional delivery evidence (photo), receives proof-anchor id.
  • Backend (API + Auth) — issues session tokens and challenges, verifies DP signatures, stores event record in DB, queues anchor operations, interacts with Blockchain anchor contract, provides verification endpoints.
  • Blockchain Anchor Contract — simple on-chain registry storing event hashes + minimal metadata (timestamp, operator id). Only stores hashes to protect privacy.
  • Auditor / Verifier — a lightweight tool or LLM-based reviewer that can accept a package (event record + proofs + txHash) and verify all signatures and chain inclusion.

Graphically (simplified):

Customer App <--> Backend <---> Blockchain
   ^                 ^
   |                 |
DP App <-------------|

4. Sequence diagrams / Flow charts (Mermaid)

A. Session activation + proximity verification (happy path)

sequenceDiagram
    participant C as Customer App
    participant B as Backend
    participant DP as Delivery Partner App
    participant BC as Blockchain

    C->>B: Activate session (customer_id, order_id, TTL)
    B-->>C: session_id, ephemeral_token (HMAC or random), TTL

    DP->>DP: scan for BLE peripheral -> find ephemeral_token
    DP->>B: request challenge (session_id, dp_id)
    B-->>DP: challenge_nonce

    DP->>DP: create signed_response = Sign_SK_DP( session_id || ephemeral_token || challenge_nonce || timestamp || optional_photo_hash )
    DP->>B: submit signed_response + evidence

    B->>B: verify DP signature, verify ephemeral_token active, store event
    B->>BC: anchor_hash = SHA256(event_json) -> call smart contract storeAnchor(anchor_hash)
    BC-->>B: txHash

    B-->>DP: success + txHash
    B-->>C: notify customer + txHash
Loading

B. Alternate flow: customer uses QR because BLE unsupported

sequenceDiagram
    participant C
    participant DP
    participant B

    C->>B: Activate session -> B replies with session_id + session_token
    C->>C: Display rotating QR(session_token)
    DP->>DP: Scan QR -> extracts session_token
    DP->>B: same as above: request challenge, sign, submit
Loading

5. Data Flow Diagram (DFD) — Level 1

graph TD
    A[Customer Mobile] -->|Activate session| B(Backend API)
    C[Delivery App] -->|Scan ephemeral token & sign| B
    B -->|Store event| DB[(Event DB)]
    B -->|Anchor hash| BC[Blockchain]
    B -->|Notify| A
    B -->|Notify| C
Loading

6. Component design & interfaces (APIs, messages)

API endpoints (REST, JSON over HTTPS)

  1. POST /api/v1/sessions — Customer activates session

    • Body: { customer_id, order_id, ttl_seconds, optional_policy }
    • Response: { session_id, ephemeral_token_id, token_type: "BLE|QR", expires_at }
  2. POST /api/v1/sessions/:session_id/challenge — DP requests challenge

    • Body: { dp_id } (DP's operator id / device id)
    • Response: { challenge_nonce, expires_at }
  3. POST /api/v1/deliveries — DP submits signed delivery

    • Body: { session_id, dp_id, signed_blob, evidence_refs[] }
    • signed_blob is a compact object: base64( ECDSA_sign( SK_DP, canonical_event_json ) )
    • Response: { status: verified|rejected, event_id, txHash (if anchored) }
  4. GET /api/v1/deliveries/:event_id/verify — returns verification status and blockchain anchor if present.

Event canonical JSON (what gets hashed)

{
  "session_id": "...",
  "order_id": "...",
  "customer_id": "...",
  "dp_id": "...",
  "ephemeral_token_hash": "sha256-of-token",
  "challenge_nonce": "...",
  "dp_signature": "base64(...)",
  "evidence_hashes": ["sha256(...)"],
  "timestamp": "ISO8601",
  "backend_received_at": "ISO8601"
}

Only sha256 hashes of ephemeral tokens/evidence go on-chain (via a single anchored sha256(event_json)). The event JSON (clear) is kept in backend DB for dispute resolution.


7. Prototype implementation details

✅ FULLY IMPLEMENTED - All components below are complete and working. See SETUP_AND_TESTING.md for detailed setup instructions.

7.1 Smart contract (Solidity) — AnchorRegistry.sol

Location: contracts/contracts/AnchorRegistry.sol
Status: ✅ Deployed and tested on local Hardhat network

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract AnchorRegistry {
    event AnchorStored(bytes32 indexed anchorHash, address indexed actor, uint256 timestamp, string eventId);
    mapping(bytes32 => bool) public anchored;

    function storeAnchor(bytes32 anchorHash, string memory eventId) external {
        require(!anchored[anchorHash], "Anchor already exists");
        anchored[anchorHash] = true;
        emit AnchorStored(anchorHash, msg.sender, block.timestamp, eventId);
    }

    function storeAnchorBatch(bytes32[] memory anchorHashes, string[] memory eventIds) external {
        require(anchorHashes.length == eventIds.length, "Array length mismatch");
        for (uint256 i = 0; i < anchorHashes.length; i++) {
            if (!anchored[anchorHashes[i]]) {
                anchored[anchorHashes[i]] = true;
                emit AnchorStored(anchorHashes[i], msg.sender, block.timestamp, eventIds[i]);
            }
        }
    }

    function isAnchored(bytes32 anchorHash) external view returns (bool) {
        return anchored[anchorHash];
    }
}

Implementation Notes:

  • ✅ Emits events for gas efficiency
  • ✅ Includes duplicate prevention mapping
  • ✅ Supports batch anchoring for future gas optimization
  • ✅ Deployed at: 0x5FbDB2315678afecb367f032d93F642f64180aa3 (local Hardhat)
  • ✅ Successfully tested with anchoring delivery events

Notes: This contract only emits an event (gas-cheap) and does not store data in contract storage (minimizes cost). Off-chain indexers or the backend can watch events to tie the anchor to the delivery event.

7.2 Backend (Node.js / Express) — Full Implementation

Location: backend/server.js (435 lines)
Database: backend/db.js (SQLite with 4 tables)
Status: ✅ Fully functional REST API

Implemented Endpoints:

  • POST /api/v1/dp/register - Register delivery partner with public key
  • POST /api/v1/sessions - Customer activates delivery session
  • POST /api/v1/sessions/:session_id/challenge - DP requests challenge nonce
  • POST /api/v1/deliveries - DP submits signed delivery proof
  • GET /api/v1/deliveries/:event_id/verify - Verify delivery event
  • GET /health - Health check endpoint

Key Features Implemented:

  • ✅ Session management with ephemeral tokens (SHA256-based)
  • ✅ Challenge-response authentication with nonce validation
  • ✅ ECDSA signature verification using elliptic curve (secp256k1)
  • ✅ SQLite database with 4 tables (delivery_partners, sessions, challenges, delivery_events)
  • ✅ Blockchain integration via ethers.js
  • ✅ Automatic anchoring to smart contract
  • ✅ Complete error handling and validation
  • ✅ Event canonicalization for hashing

Server Output:

Connected to SQLite database
Database tables created/verified
✅ Blockchain initialized. Contract at: 0x5FbDB2315678afecb367f032d93F642f64180aa3

🚀 Proximity-Verified Delivery Backend
📡 Server running on http://localhost:3000

7.3 Mobile (React Native) — BLE peripheral & scanner approach

Status: 📋 Planned for Round 2

Current Alternative: CLI simulation tools fully functional (see 7.4)

Planned Implementation:

  • Customer app (peripheral): advertise service_uuid with characteristic session_token set to ephemeral token. Show UI: Activate delivery window — 5 minutes.
  • DP app (scanner): scan for service_uuid, read session_token, call server to get challenge, compute signature over canonical structure and send.

Technical Approach:

  • Use react-native-ble-plx for BLE communication
  • Customer device acts as BLE peripheral (advertising mode)
  • DP device acts as BLE central (scanning mode)
  • Automatic fallback to QR code if BLE peripheral mode unsupported

7.4 CLI Simulator (no BLE hardware)

Location: sim/ directory
Status: ✅ Fully implemented and tested

Tools:

  • simulate_customer_activate.js — Creates session and displays ephemeral token
  • simulate_dp_submit.js — Simulates DP scanning, signing, and submission

Usage:

# Customer activates session
cd sim
node simulate_customer_activate.js --customer cust_123 --order order_789

# DP submits delivery
node simulate_dp_submit.js --session <session_id> --token <ephemeral_token>

Purpose: Essential for automated testing, CI/CD pipelines, and demos without requiring mobile hardware. Successfully tested in production environment.


8. How to run the prototype (local simulation)

Prerequisites

  • Node.js v22+ installed
  • Git for cloning
  • 3 terminal windows

Installation

# Clone and install all dependencies
git clone <repo-url>
cd proximity_verified_delivery_prototype
npm run setup  # Installs contracts, backend, and sim dependencies

Running the Prototype

Terminal 1: Start Blockchain Node

cd contracts
npx hardhat node

Keep running. You'll see 20 test accounts with 10000 ETH each.

Terminal 2: Deploy Contract & Start Backend

# Deploy smart contract
cd contracts
npm run deploy
# Output: AnchorRegistry deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3

# Start backend API
cd ../backend
node server.js
# Output: 📡 Server running on http://localhost:3000

Terminal 3: Run End-to-End Test

node scripts/test_e2e.js

Expected Output

🚚 Proximity-Verified Delivery - End-to-End Test
======================================================================

📋 Step 1: Register Delivery Partner
✅ DP Registered

👤 Step 2: Customer Activates Delivery Session
✅ Session Created: s_14e68495ce4d5cc8

📡 Step 3: Delivery Partner Scans BLE (Simulated)
✅ Scanned ephemeral token

🔐 Step 4: Delivery Partner Requests Challenge
✅ Challenge Received: 7ae2fa1ee781cf5d5ba29f6f8a598627

✍️  Step 5: Delivery Partner Creates Signed Proof
✅ Signed Event Created

📤 Step 6: Submit Delivery with Proof
✅ Delivery Submitted
   Event ID: evt_d2eeeb7f72fcc44b
   Status: verified
   Blockchain TxHash: 0x0f417f00bd6be1bc7396079a33d67f8db0bf07b3f3bf32c0a92e4814340f798c

🔍 Step 7: Verify Delivery Event
✅ Verification Result
{
  "blockchain_verified": true,
  "status": "anchored",
  "tx_hash": "0x0f417f00bd6be1bc..."
}

🎉 END-TO-END TEST COMPLETED SUCCESSFULLY!

Alternative: Quick Test (PowerShell)

# One-command test (requires Hardhat node running)
$job = Start-Job -ScriptBlock { Set-Location 'd:\proximity_verified_delivery_prototype\backend'; node server.js }
Start-Sleep -Seconds 3
node scripts/test_e2e.js
Stop-Job $job

Manual Testing with CLI Tools

# Activate session as customer
cd sim
node simulate_customer_activate.js --customer cust_456 --order order_999

# Submit delivery as DP
node simulate_dp_submit.js --session <session_id> --token <ephemeral_token>

For detailed troubleshooting and advanced usage, see SETUP_AND_TESTING.md.


9. Audit & verification process

To verify a delivery event:

  1. Obtain event_json from backend (signed by backend or served over HTTPS).
  2. Recompute anchor_hash = sha256(event_json) and check the on-chain AnchorStored event with same anchorHash and timestamp.
  3. Verify DP signature using the DP's registered public key.
  4. Verify ephemeral token hash in the event matches the session record (backend has token stored server-side).
  5. Optionally verify evidence hashes (photo, QR) match stored S3/IPFS references.

Automated LLM-based filtering: The prototype includes a format that can be fed to off-chain LLM checks for content anomalies (e.g., mismatching timestamps, duplicate anchors, blacklisted dp_id), but the core cryptographic checks remain authoritative.


10. Privacy & compliance notes

  • Do not store raw GPS coordinates on-chain. If location is needed, store hashed short-range evidence or store WKT only in encrypted backend.
  • Minimal data on-chain: only sha256(event_json).
  • Data retention: backend policy to purge PII after retention period and keep hashed anchors for audit.
  • Consent: customer explicitly enables session; evidence upload (photo) requires customer consent in-app.
  • Regulatory: for regulated goods (pharma, high-value), integrate with eKYC and tamper-evident packaging metadata.

11. Performance, scaling and operational considerations

  • Blockchain anchoring cost: batch multiple anchors in a Merkle tree root to reduce gas (Round 2 improvement).
  • High throughput: use queueing (RabbitMQ/Kafka) for anchor jobs and use an indexer to watch anchor events.
  • Offline mode: DP app should locally store signed events and attempt to deliver when online; backend should record submitted_at and anchored_at separately.
  • Key management: DP private keys should be stored in secure storage on device (Android Keystore / iOS Keychain) and optionally protected by hardware-backed keys.

12. What we'll add / improve in Round 2

Round 2 adds enterprise-grade features, increased attacker-resilience, and stronger privacy:

  1. UWB integration (centimeter accuracy)

    • Add UWB support where available (phones with UWB or aftermarket UWB dongles) for true doorstep-level accuracy; fallback to BLE/QR.
  2. Merkle-batching of anchors

    • Instead of writing each event to-chain, collect events into a Merkle tree periodically and anchor the root to chain (reduces gas cost by orders of magnitude). Provide Merkle proofs per event.
  3. Zero-Knowledge proofs for privacy

    • Explore using zk-SNARKs to prove proximity/time without revealing PII; e.g., prove possession of ephemeral token and valid challenge response.
  4. Hardware-backed DP identity & attestation

    • Integrate device attestation (SafetyNet/Play Integrity, iOS DeviceCheck) and optional TPM-based signing for stronger non-repudiation.
  5. IPFS / permanent evidence store

    • Store evidence (images) on IPFS/Arweave and store content-address in backend and anchor hash to blockchain; allows long-term, tamper-evident evidence.
  6. Legal & compliance mode

    • Add optional rich logs (encrypted at rest) for legal/regulatory audits (pharma), with multi-key escrow for magistrate or compliance bodies.
  7. Anti-replay & liveness enhancements

    • Add sequence numbers, per-device counters, and challenge TTL & nonce reuse detection.
  8. LLM-assisted anomaly detection & triage

    • Build a secure LLM pipeline to flag suspicious delivery events (e.g., many deliveries anchored in quick succession by same dp_id) for manual audit.
  9. UX improvements

    • One-touch customer confirmation with biometric co-sign (optional), and accessibility improvements for elderly users (large buttons, voice prompts).
  10. Operator dashboard

    • Audit UI showing on-chain anchors, event metadata, verification tools, and dispute workflows integrated with messaging to DP/customer.
  11. Interoperability & standards

    • Build standard event format (JSON-LD or W3C VC-compatible) to support cross-provider verification and third-party auditors.
  12. Penetration testing & formal verification

    • Formal model-checking of the protocol and smart contract audits; fuzzing delivery submissions for corner-case robustness.

13. Appendix: data models, sample JSON, test vectors

Sample session creation response:

{
  "session_id": "s_5a3f9b",
  "ephemeral_token": "6a7f9c...",
  "token_type": "BLE",
  "expires_at": "2025-12-25T12:34:56Z"
}

Sample canonical event (already canonicalized):

{
  "session_id": "s_5a3f9b",
  "order_id": "ord_99",
  "customer_id": "cus_123",
  "dp_id": "dp_09",
  "ephemeral_token_hash": "sha256:abcd...",
  "challenge_nonce": "nonce_77",
  "dp_signature": "base64:MEUCIQ...",
  "evidence_hashes": ["sha256:imghash..."],
  "timestamp": "2025-12-25T12:36:05Z",
  "backend_received_at": "2025-12-25T12:36:07Z"
}

Test vector for hashing:

  • sha256(JSON.stringify(canonical_event)) -> anchorHash (store this in contract event log)

Actual Repository Layout (Implemented)

proximity_verified_delivery_prototype/
├─ contracts/                           # Smart contract & blockchain
│  ├─ contracts/
│  │  └─ AnchorRegistry.sol            # ✅ Main smart contract
│  ├─ scripts/
│  │  └─ deploy.js                     # ✅ Deployment script
│  ├─ hardhat.config.js                # ✅ Hardhat configuration
│  ├─ deployment.json                  # ✅ Generated after deployment
│  └─ package.json
│
├─ backend/                             # REST API server
│  ├─ server.js                        # ✅ Express API (435 lines)
│  ├─ db.js                            # ✅ SQLite database layer
│  ├─ delivery.db                      # ✅ SQLite database (auto-created)
│  └─ package.json
│
├─ sim/                                 # CLI simulation tools
│  ├─ simulate_customer_activate.js    # ✅ Customer simulator
│  ├─ simulate_dp_submit.js            # ✅ DP simulator
│  └─ package.json
│
├─ scripts/                             # Testing & automation
│  ├─ test_e2e.js                      # ✅ End-to-end test (PASSING)
│  └─ run_demo.js                      # ✅ Automated demo
│
├─ proximity_verified_delivery_prototype_readme.md  # This document
├─ SETUP_AND_TESTING.md                # ✅ Detailed setup guide
├─ QUICK_REFERENCE.md                  # ✅ Quick reference card
├─ .env.example                        # Environment template
├─ .gitignore                          # Git ignore rules
└─ package.json                        # Root package config

Implementation Status: ✅ All core components implemented and tested
Mobile Apps: 📋 Planned for Round 2 (CLI simulation working now)
Test Coverage: ✅ End-to-end test passing with blockchain verification

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •