A Basic Electronic Medical Records Management using Blockchain
In this blogpost we will develop a very basic blockchain-based system that securely manages and shares electronic medical records (EMRs), ensuring the authenticity, confidentiality, and integrity of patient data while allowing authorized access to healthcare information.
Problem Statement:
Managing and sharing electronic medical records (EMRs) securely and efficiently among healthcare providers is a significant challenge. A reliable system is required to ensure the authenticity, confidentiality, and integrity of medical records while allowing authorized access to patient’s health information.
Motivation: The healthcare industry faces ongoing challenges related to data breaches, unauthorized access, and inefficiencies in record management. A blockchain-based solution offers a secure, immutable, and transparent way to manage and share EMRs, addressing these critical issues effectively.
Chaincode Development:
• Language: Java
• Blockchain: Hyperledger Fabric
• Build Automation Tool: Gradle
Tasks:
- Create New Medical Records: Allows doctors to create new medical records with details such as patient name, ID, diagnosis, treatment, and date of visit.
- Update Existing Medical Records: Enables doctors to update existing records with new information, ensuring that the most current data is always available.
- View Medical Record Details: Allows authorized personnel to retrieve and view the complete details of a medical record from the blockchain.
Code Repo: https://github.com/RanjanaJha/electronic-health-record
Steps:
- Bring the fabric network down — sudo ./network.sh down
- Bring the fabric network up with certificate authority and world state to be store in couchdb : sudo ./network.sh up -ca -s couchdb
- Create Channel
- For org 1 in one terminal set env vars -
nano lifecycle_setup_org1.sh
source ./lifecycle_setup_org1.sh
#!/bin/sh
export PATH=${PWD}/../bin:${PWD}:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
export ORDERER_CA=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem- Package the chain code -
peer lifecycle chaincode package ElectronicHealthRecord.tar.gz — path /mnt/d/WorkDir/ElectronicHealthRecord/build/install/ElectronicHealthRecord/lib — lang java — label ElectronicHealthRecord_1
- Install the chain code in org1 -
sudo chmod -R 777 .
peer lifecycle chaincode install ElectronicHealthRecord.tar.gz — peerAddresses localhost:7051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE
Get Ranjana Jha’s stories in your inbox
Join Medium for free to get updates from this writer.
Chaincode code package identifier: ElectronicHealthRecord_1:a11b964ddc3e248956c0d2439d8172188ed4b06327f75cfa496a0a3b251301fc
- Now open a new terminal and set the env vars in org2 -
nano lifecycle_setup_org2.sh
source ./lifecycle_setup_org2.sh
#!/bin/sh
export PATH=${PWD}/../bin:${PWD}:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
# Environment variables for Org2
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051
export ORDERER_CA=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem- Install the chain code in org2 -
peer lifecycle chaincode install ElectronicHealthRecord.tar.gz — peerAddresses localhost:9051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE - Chaincode code package identifier: ElectronicHealthRecord_1:a11b964ddc3e248956c0d2439d8172188ed4b06327f75cfa496a0a3b251301fc
- Approve the chain code from both the orgs- (the local host here is the orderer port)
peer lifecycle chaincode approveformyorg -o localhost:7050 — ordererTLSHostnameOverride orderer.example.com — tls — cafile $ORDERER_CA -C mychannel — name ElectronicHealthRecord — version 1.0 — init-required — package-id ElectronicHealthRecord_1:a11b964ddc3e248956c0d2439d8172188ed4b06327f75cfa496a0a3b251301fc — sequence 1
Org1 -
Org2 -
- Set the env vars for committing the chain code -
nano lifecycle_setup_Channel_commit.sh
source ./lifecycle_setup_Channel_commit.sh
#!/bin/sh
export PATH=${PWD}/../bin:${PWD}:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE_ORG1=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_TLS_ROOTCERT_FILE_ORG2=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
export ORDERER_CA=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem- Commit to peers of both the org -
peer lifecycle chaincode commit -o localhost:7050 — ordererTLSHostnameOverride orderer.example.com — tls $CORE_PEER_TLS_ENABLED — cafile $ORDERER_CA -C mychannel — name ElectronicHealthRecord — peerAddresses localhost:7051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE_ORG1 — peerAddresses localhost:9051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE_ORG2 — version 1.0 — sequence 1 — init-required
- Interact with the chain code-
peer chaincode invoke -o localhost:7050 — ordererTLSHostnameOverride orderer.example.com — tls $CORE_PEER_TLS_ENABLED — cafile $ORDERER_CA -C mychannel -n ElectronicHealthRecord — peerAddresses localhost:7051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE_ORG1 — peerAddresses localhost:9051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE_ORG2 — isInit -c ‘{“Args”:[“initLedger”]}’
- peer chaincode invoke -o localhost:7050 — ordererTLSHostnameOverride orderer.example.com — tls $CORE_PEER_TLS_ENABLED — cafile $ORDERER_CA -C mychannel -n ElectronicHealthRecord — peerAddresses localhost:7051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE_ORG1 — peerAddresses localhost:9051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE_ORG2 -c ‘{“Args”:[“createHealthRecord”, “PT0001”, “Alice J”,”Hypertension”,”BP Medicine”,”26/01/2026",”Dr.John”,”6767787878"]}’
- peer chaincode invoke -o localhost:7050 — ordererTLSHostnameOverride orderer.example.com — tls $CORE_PEER_TLS_ENABLED — cafile $ORDERER_CA -C mychannel -n ElectronicHealthRecord — peerAddresses localhost:7051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE_ORG1 — peerAddresses localhost:9051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE_ORG2 -c ‘{“Args”:[“createHealthRecord”, “PT0002”, “Bob J”,”Boderline Diabetes”,”Metphormin Medicine”,”26/01/2026",”Dr.Watson”,”7878787878787"]}’
- peer chaincode invoke -o localhost:7050 — ordererTLSHostnameOverride orderer.example.com — tls $CORE_PEER_TLS_ENABLED — cafile $ORDERER_CA -C mychannel -n ElectronicHealthRecord — peerAddresses localhost:7051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE_ORG1 — peerAddresses localhost:9051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE_ORG2 -c ‘{“Args”:[“updateHealthRecord”, “PT0002”, “Bob J”,”Boderline Diabetes and Hypertension”,”Metphormin and BP Medicine”,”26/01/2026",”Dr.Watson”,”7878787878787"]}’
- peer chaincode invoke -o localhost:7050 — ordererTLSHostnameOverride orderer.example.com — tls $CORE_PEER_TLS_ENABLED — cafile $ORDERER_CA -C mychannel -n ElectronicHealthRecord — peerAddresses localhost:7051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE_ORG1 — peerAddresses localhost:9051 — tlsRootCertFiles $CORE_PEER_TLS_ROOTCERT_FILE_ORG2 -c ‘{“Args”:[“viewHealthRecord”, “PT0002”]}’









