This project implements a distributed health record system using blockchain to ensure data integrity, security, and controlled access with patient approval. Built with Python, it offers a simple interface for managing medical records and demonstrates how blockchain can securely integrate healthcare data.
- Features
- Medical Record Creation & Verification Process
- Project Structure
- Blockchain Architecture
- On-Chain Data Model
- Server-Blockchain Integration
- API Overview
- Setup Instructions
- Testing
- Limitations
- Create health records (doctor) with dynamic fields; server encrypts and stores; integrity hash written to blockchain
- Patient manages access requests from doctors; shares per‑record secret keys
- Patient views and verifies records via blockchain
- Doctor searches patients, requests access, reads records when approved
- Proof of work conscensus mechanism
- Record Creation: When a doctor creates a new medical record, the server first encrypts the record’s data and nodes in blockchain are computing a unique canonical hash from the encrypted payload.
- Storage: The encrypted record is then saved to MongoDB, and the hash is stored on the blockchain to provide immutable proof of authenticity.
- Patient Verification: Later, when a patient wants to confirm the integrity of their record, they submit a verification request using the record’s unique
secret_key. - Integrity Check: The server decrypts the stored record, recalculates its hash, and compares it with the on-chain
health_record_hash. - Verification Result: The system returns a verification response indicating whether the record remains authentic or has been altered since its creation.
client/– React front‑end (MUI, Tailwind)backend/server/– Flask APIbackend/blockchain/– WebSocket peer, PoW chain, JSON persistencedb/– MongoDB dump
Core components (see backend/blockchain/backend/...):
- Chain: manages difficulty, mining control, validation, and persistence (
chain.py) - BlockHeader:
height,difficulty,miner,previous_block_hash,timestamp,block_hash(block_header.py) - Block: contains a
Transactionand header; performs PoW mining and hash computation (block.py) - TransactionBody:
creator,patient,health_record_id,date,health_record_hash(transaction_body.py) - Peer: Node in blockchain network (
network/peer.py)
- Proof‑of‑Work with
difficultyvariable - Genesis block created at startup with fixed previous hash and id
create_new_block()mines a block from the current pending transaction- Chain validity: re‑computes block hashes and verifies link to previous hash
- Each node writes JSON files under
backend/blockchain/db/ - Chain is stored on
<PORT>_chain.json - Accounts (public and private keys of accounts) are stored on
<PORT>_accounts.json
- Each node runs a WebSocket peer
- Nodes can bootstrap from a known peer and then connect to additional peers
- Example:
python backend/blockchain/run.py 5001(first node);python backend/blockchain/run.py 5002 5001(second node joins 5001)
TransactionBody fields:
creator: public key of the authority/doctor creating the record entrypatient: patient’s public keyhealth_record_id: identifier linking blockchain entry to off‑chain encrypted recorddate: ISO timestamp for creationhealth_record_hash: canonical SHA‑256 hash of the off‑chain record payload
- The Flask server communicates with a blockchain node over WebSocket
- Env
PEER_FOR_COMMUNICATIONpoints to a node URI (e.g.,ws://localhost:5001) - Utility
send_to_blockchain_and_wait_response(seebackend/server/util/util.py) sends a message and awaits response - On record creation, server computes the record’s canonical hash and submits a transaction; on verification, server fetches the on‑chain hash and compares
Auth
POST /api/login– returns JWT withuser_typeclaimPOST /api/auth/verify– validate/inspect token
Entities (provisioning/lookup)
POST /api/patientsPOST /api/health-authorityPOST /api/doctorsGET /api/doctors/:idGET /api/health_authority/:idGET /api/patients/:idGET /api/patients/personal_id/:pidGET /api/central-authority/:id
Health Records
POST /api/health-records– create (doctor)GET /api/health-records– list (patient)GET /api/health-records/secret_key/:hr_id– get key if authorizedGET /api/health-records/patient/:personal_id– doctor view by patient PIDPOST /api/health-records/verify/:hr_id– verify integrity withsecret_keyPOST /api/health-records/decrypt/:hr_id– server‑side decrypt helper
Access Requests
POST /api/requests– doctor → patientGET /api/requests/patient– patient inboxGET /api/requests/doctors– doctor outboxPATCH /api/requests/:id– patient accepts; attachessecret_keyDELETE /api/requests/:id
To run the entire system successfully, make sure the following dependencies are installed on your computer:
-
Node.js ( 20+ ) – required to run the client, manage packages via npm, and serve the frontend application.
-
Python ( 3.13 ) – required to run the server and blockchain nodes, since those components are written in Python.
-
Mongo DB - document-based database for storing data.
-
MongoDB Command Line Database Tools (Optional) - required for restoring database from a dump.
- First change folder to backend
cd backend
- Create virtual enviroment for python
python -m venv venv
- Activate virtual enviroment
.\venv\Scripts\activate
- Install dependencies
pip install -r requirements.txt
To ensure network consensus and proper blockchain synchronization, the system requires at least three active nodes, and the total number of nodes must be odd (e.g., 3, 5, 7) to prevent tie votes during validation.
In this project there is already blockchain network that consist of nodes on ports 5001, 5002, 5003. You can remove this network by deleting everything inside the backend/blockchain/db directory. (Note: Deleting this network will invalidate the medical records stored in MongoDB for this project!).
- Start first node (port 5001):
python blockchain/run.py 5001
- Start second node (port 5002) and connect to 5001:
python blockchain/run.py 5002 5001
- Start third node (port 5003) and connect to 5002 and 5001:
python blockchain/run.py 5003 5002 5001
Before starting the Flask API, you need to set up MongoDB. In the db folder, there is a MongoDB dump that will be used to create the database.
- Go to dump folder
cd db
- Restore database
mongorestore --db health-system --drop dump/health-system
- Create .env file and copy content from
.env.example
JWT_SECRET=<secret_key>
DB_NAME=<database_name>
DB=<connection_url>
PEER_FOR_COMMUNICATION=<websocket_url>
- Run API
python server/server.py
Server will run on port 5000 by default.
- Change folder to backend
cd client
- Install dependencies
npm install
- Create .env file and copy content from
.env.example
VITE_API_URL=<api_url>
- Run client
npm run dev
Client will run on port 5173 on default.
demo_users.md file contains demo users for testing this system.
- Educational PoW chain without production‑grade consensus or networking hardening
- JSON persistence for simplicity; consider proper databases
- Improve key management and end‑to‑end encryption strategy