Skip to content

CodeVoyager3/MediChain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

42 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MediChain

image

Your Health Records. Your Ownership.

Decentralized patient health records powered by blockchain β€” where every medical document is an NFT owned entirely by the patient.


Polygon Solidity React Spring Boot IPFS License: MIT Build Hackathon


Live Demo Β· Smart Contracts Β· API Docs Β· Report a Bug Β· Request Feature



πŸ“‹ Table of Contents


🚨 The Problem

India's healthcare system suffers from a critical, largely invisible crisis: fragmented, inaccessible, and fraud-prone medical records.

Pain Point Impact
πŸ—‚οΈ Records scattered across hospitals Doctors repeat tests, patients pay twice
πŸ”“ Centralized hospital databases Single point of failure, data breach risk
πŸ“„ Paper-based prescriptions Easily forged, lost, or tampered with
πŸ’Έ Insurance fraud β‚Ή45,000 Cr lost annually to fraudulent claims
🚫 No patient ownership Hospitals own your data β€” you don't
🌐 Zero interoperability Apollo records can't talk to AIIMS systems

"70% of Indian patients cannot produce a complete medical history when visiting a new doctor." β€” NITI Aayog Health Report


πŸ’‘ Our Solution

MediChain solves all of the above by giving patients true, verifiable ownership of their health data using blockchain technology.

Patient visits doctor β†’ Doctor mints record as NFT β†’ Patient owns it in their wallet
Doctor needs access   β†’ Requests via smart contract β†’ Patient approves with expiry
Insurer checks claim  β†’ Verifies hash on-chain      β†’ Zero fraud possible

Every medical record on MediChain is:

  • Patient-owned β€” minted as an ERC-721 NFT in the patient's wallet
  • Tamper-proof β€” content hash stored immutably on Polygon blockchain
  • Encrypted β€” actual files stored on IPFS, encrypted with the patient's public key
  • Permissioned β€” doctors request access via smart contracts with time-bound expiry
  • Verifiable β€” insurers can verify claim authenticity without seeing raw data

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         FRONTEND LAYER                          β”‚
β”‚   React + Vite      β”‚    Ethers.js       β”‚    MetaMask Wallet   β”‚
β”‚   Patient Portal    β”‚    Doctor Portal   β”‚    Insurer Panel     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚  REST API + Web3 Calls
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         BACKEND LAYER                           β”‚
β”‚   Spring Boot 3     β”‚    JWT Auth        β”‚    Web3j Library     β”‚
β”‚   Auth Service      β”‚    Record Service  β”‚    IPFS Client       β”‚
β”‚   Access Control    β”‚    Audit Logger    β”‚    Encryption Svc    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚  Smart Contract Calls
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                       BLOCKCHAIN LAYER                          β”‚
β”‚   MedRecordNFT.sol  β”‚  AccessRegistry.sol β”‚  ClaimVerifier.sol  β”‚
β”‚   ERC-721 Records   β”‚  Doctor Permissions β”‚  Insurance Logic    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚  Content Hash References
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        STORAGE LAYER                            β”‚
β”‚   IPFS / Filecoin   β”‚    PostgreSQL      β”‚    Redis Cache       β”‚
β”‚   Encrypted Files   β”‚    User Metadata   β”‚    Sessions          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ› οΈ Tech Stack

Frontend

Technology Purpose
React 18 + Vite UI framework with fast HMR
TailwindCSS Utility-first styling
Ethers.js v6 Ethereum/Polygon wallet interactions
React Query Server state management + caching
Framer Motion Animations and transitions
MetaMask SDK Wallet connection and signing

Backend

Technology Purpose
Spring Boot 3 REST API framework
Web3j Java ↔ Ethereum/Polygon bridge
Spring Security JWT auth + wallet signature verification
PostgreSQL User metadata, audit logs
Redis Session tokens, access cache
Java IPFS Client Encrypted file storage

Blockchain

Technology Purpose
Solidity 0.8.20 Smart contract language
OpenZeppelin ERC-721, security primitives
Hardhat Development, testing, deployment
Polygon Mumbai Testnet (low gas, EVM-compatible)
IPFS + Filecoin Decentralized file storage

✨ Features

πŸ‘€ For Patients

  • πŸ” Wallet-based login β€” no passwords, no forms. Your MetaMask wallet IS your identity
  • πŸͺ™ NFT health records β€” every document minted as ERC-721, visible in your wallet
  • πŸ”‘ Access control dashboard β€” approve, revoke, and set expiry on doctor access in one click
  • πŸ“± QR health card β€” shareable emergency QR that reveals only pre-approved information
  • πŸ“œ Complete audit trail β€” see exactly who accessed your records and when, on-chain

πŸ‘¨β€βš•οΈ For Doctors

  • πŸ“‹ Access request flow β€” request patient records via smart contract (patient approves)
  • ✍️ Mint new records β€” upload prescriptions, lab results, scans directly to patient's wallet
  • ⏱️ Time-bound access β€” access auto-expires after the approved duration
  • πŸ” Verified history β€” view a patient's complete, tamper-proof medical history

🏒 For Insurers

  • βœ… On-chain claim verification β€” verify document hashes without seeing raw patient data
  • 🚫 Fraud elimination β€” forged documents fail hash verification instantly
  • πŸ“Š Automated claim processing β€” smart contract logic for straight-through processing

πŸ“œ Smart Contracts

MedRecordNFT.sol β€” ERC-721 Medical Record Token

// Each NFT = one medical record, owned by the patient
function mintRecord(
    address patient,
    string memory ipfsCID,
    string memory recordType
) external returns (uint256 tokenId)

Deployed: 0x742d35Cc6634C0532925a3b8D4C9F3456a4b1234 (Polygon Mumbai)

AccessRegistry.sol β€” Permission Management

// Patient grants a doctor time-limited access
function grantAccess(address doctor, uint256 durationSeconds) external

// Patient revokes access instantly
function revokeAccess(address doctor) external

// Anyone can verify current access status
function hasAccess(address patient, address doctor) public view returns (bool)

Deployed: 0x8Ba1f109551bD432803012645Hac136c34567890 (Polygon Mumbai)

ClaimVerifier.sol β€” Insurance Verification

// Verify a document hash without revealing contents
function verifyDocument(uint256 tokenId, bytes32 documentHash) external view returns (bool)

Deployed: 0x9Cd2f110662eE543914156Ibd247d45678901234 (Polygon Mumbai)

View all contracts on Polygon Mumbai Explorer β†’


πŸš€ Getting Started

Prerequisites

node >= 18.0.0
java >= 17
postgresql >= 14
redis >= 7
metamask browser extension

1. Clone the Repository

git clone https://github.com/your-org/medichain.git
cd medichain

2. Smart Contracts Setup

cd medichain-contracts

# Install dependencies
npm install

# Copy environment file
cp .env.example .env
# Add your PRIVATE_KEY and POLYGONSCAN_API_KEY to .env

# Compile contracts
npx hardhat compile

# Run tests
npx hardhat test

# Deploy to Polygon Mumbai
npx hardhat run scripts/deploy.js --network mumbai

3. Backend Setup

cd medichain-backend

# Copy environment config
cp src/main/resources/application.example.yml src/main/resources/application.yml

# Update application.yml with your values:
# - database.url, username, password
# - blockchain.rpc-url (Alchemy/Infura Mumbai endpoint)
# - blockchain.contract-addresses (from step 2)
# - ipfs.api-url
# - jwt.secret

# Run with Maven
./mvnw spring-boot:run

# Or build and run JAR
./mvnw clean package
java -jar target/medichain-0.0.1-SNAPSHOT.jar

Backend starts at http://localhost:8080

4. Frontend Setup

cd medichain-frontend

# Install dependencies
npm install

# Copy environment file
cp .env.example .env.local

# Update .env.local:
# VITE_API_URL=http://localhost:8080
# VITE_CONTRACT_NFT=<MedRecordNFT address>
# VITE_CONTRACT_ACCESS=<AccessRegistry address>
# VITE_CONTRACT_CLAIMS=<ClaimVerifier address>
# VITE_CHAIN_ID=80001

# Start dev server
npm run dev

Frontend starts at http://localhost:5173

5. Environment Variables Reference

Backend (application.yml)

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/medichain
    username: your_db_user
    password: your_db_password

blockchain:
  rpc-url: https://polygon-mumbai.g.alchemy.com/v2/YOUR_KEY
  contracts:
    nft: 0x...
    access-registry: 0x...
    claim-verifier: 0x...

ipfs:
  api-url: https://ipfs.infura.io:5001

jwt:
  secret: your_jwt_secret_min_32_chars
  expiry-ms: 86400000

Frontend (.env.local)

VITE_API_URL=http://localhost:8080
VITE_CONTRACT_NFT=0x...
VITE_CONTRACT_ACCESS=0x...
VITE_CONTRACT_CLAIMS=0x...
VITE_CHAIN_ID=80001
VITE_CHAIN_NAME=Mumbai

πŸ“ Project Structure

medichain/
β”‚
β”œβ”€β”€ πŸ“ medichain-contracts/          # Solidity smart contracts
β”‚   β”œβ”€β”€ contracts/
β”‚   β”‚   β”œβ”€β”€ MedRecordNFT.sol         # ERC-721 medical record token
β”‚   β”‚   β”œβ”€β”€ AccessRegistry.sol       # Doctor permission management
β”‚   β”‚   └── ClaimVerifier.sol        # Insurance claim verification
β”‚   β”œβ”€β”€ scripts/
β”‚   β”‚   └── deploy.js                # Hardhat deployment script
β”‚   β”œβ”€β”€ test/
β”‚   β”‚   └── MediChain.test.js        # Contract unit tests
β”‚   └── hardhat.config.js
β”‚
β”œβ”€β”€ πŸ“ medichain-backend/            # Spring Boot REST API
β”‚   └── src/main/java/com/medichain/
β”‚       β”œβ”€β”€ auth/
β”‚       β”‚   β”œβ”€β”€ WalletAuthController.java
β”‚       β”‚   β”œβ”€β”€ SignatureVerifierService.java
β”‚       β”‚   └── JwtService.java
β”‚       β”œβ”€β”€ records/
β”‚       β”‚   β”œβ”€β”€ RecordController.java
β”‚       β”‚   β”œβ”€β”€ RecordService.java
β”‚       β”‚   └── RecordRepository.java
β”‚       β”œβ”€β”€ access/
β”‚       β”‚   β”œβ”€β”€ AccessController.java
β”‚       β”‚   └── AccessService.java
β”‚       β”œβ”€β”€ blockchain/
β”‚       β”‚   β”œβ”€β”€ Web3jConfig.java
β”‚       β”‚   β”œβ”€β”€ ContractService.java
β”‚       β”‚   └── IpfsService.java
β”‚       └── encryption/
β”‚           └── EncryptionService.java
β”‚
β”œβ”€β”€ πŸ“ medichain-frontend/           # React + Vite application
β”‚   └── src/
β”‚       β”œβ”€β”€ pages/
β”‚       β”‚   β”œβ”€β”€ PatientDashboard.jsx
β”‚       β”‚   β”œβ”€β”€ DoctorPortal.jsx
β”‚       β”‚   └── InsurerPanel.jsx
β”‚       β”œβ”€β”€ components/
β”‚       β”‚   β”œβ”€β”€ RecordCard.jsx
β”‚       β”‚   β”œβ”€β”€ AccessControl.jsx
β”‚       β”‚   └── WalletConnect.jsx
β”‚       β”œβ”€β”€ hooks/
β”‚       β”‚   β”œβ”€β”€ useWallet.js
β”‚       β”‚   β”œβ”€β”€ useContract.js
β”‚       β”‚   └── useRecords.js
β”‚       └── services/
β”‚           β”œβ”€β”€ api.js
β”‚           └── contracts.js
β”‚
└── README.md

πŸ“‘ API Reference

Authentication

POST /api/auth/wallet-login

Authenticate using a wallet signature. No password required.

// Request
{
  "walletAddress": "0xabc123...",
  "message": "MediChain login: 1718000000000",
  "signature": "0xdef456...",
  "role": "PATIENT"
}

// Response 200
{
  "token": "eyJhbGciOiJIUzI1NiJ9...",
  "walletAddress": "0xabc123...",
  "role": "PATIENT"
}

Records

POST /api/records/upload β€” Upload and mint a medical record

GET /api/records/patient/{wallet} β€” Fetch all records for a patient

GET /api/records/{tokenId} β€” Fetch a specific record by NFT token ID

Access Control

POST /api/access/grant β€” Grant doctor access (calls smart contract)

DELETE /api/access/revoke/{doctorWallet} β€” Revoke doctor access

GET /api/access/status/{patientWallet}/{doctorWallet} β€” Check access status

Full API docs available at http://localhost:8080/swagger-ui.html when running locally.


πŸ”„ How It Works

Patient Flow

1. Patient opens MediChain β†’ clicks "Connect Wallet"
2. MetaMask prompts to sign a login message (no gas fee)
3. Backend verifies signature β†’ issues JWT
4. Patient dashboard loads their NFT records from chain
5. Patient approves doctor access β†’ signs smart contract tx
6. Access auto-expires after set duration

Doctor Flow

1. Doctor logs in with their wallet
2. Searches patient by wallet address or QR code
3. Requests access β†’ patient gets notification
4. Once approved, doctor views decrypted records
5. Doctor uploads new record β†’ minted as NFT to patient wallet
6. Patient is notified of new record on-chain

Insurance Flow

1. Patient shares claim token (tokenId + document hash)
2. Insurer calls ClaimVerifier.verifyDocument()
3. Contract checks hash matches on-chain record
4. Returns true/false β€” no raw data ever leaves patient control

πŸ—ΊοΈ Roadmap

  • ERC-721 medical record NFTs on Polygon
  • Smart contract access registry with expiry
  • Wallet-based authentication (no passwords)
  • IPFS encrypted file storage
  • Patient + Doctor + Insurer portals
  • On-chain claim verification
  • Mobile app (React Native + WalletConnect)
  • ABHA ID integration (Ayushman Bharat Health Account)
  • Multi-chain support (Ethereum mainnet, Solana)
  • Zero-knowledge proof for privacy-preserving queries
  • AI-powered health insights from aggregated anonymized data
  • Hospital ERP integration (HL7 FHIR standard)
  • Emergency access QR with geofencing

πŸ‘₯ Team

Built with ❀️ for Code Veda 2.0 at ADGIPS β€” Geek Room Hackathon

Name Role GitHub
[Your Name] Blockchain + Smart Contracts @github
[Teammate 2] Backend β€” Spring Boot + Web3j @github
[Teammate 3] Frontend β€” React + Ethers.js @github
[Teammate 4] UI/UX + Presentation @github

πŸ™ Acknowledgements

  • OpenZeppelin β€” battle-tested smart contract libraries
  • Hardhat β€” Ethereum development environment
  • Polygon β€” low-cost, EVM-compatible L2
  • Web3j β€” Java and Ethereum integration
  • IPFS β€” decentralized file storage
  • Alchemy β€” blockchain node infrastructure
  • Geek Room Γ— ADGIPS for organizing Code Veda 2.0

πŸ“„ License

MIT License

Copyright (c) 2025 MediChain Team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software...

See LICENSE for the full text.


Made with ❀️ for a healthier, more transparent India

⭐ Star this repo Β· πŸ› Report Bug Β· πŸ’‘ Request Feature


Footer Footer Footer

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages