Skip to content

Rarees404/BlockChain

Repository files navigation

Healthcare Digital Identity Platform

A decentralized digital identity and data sharing platform for healthcare, built on Ethereum-compatible smart contracts. Patients retain control over access to their medical records, and healthcare providers can only view data with explicit, time-bound consent.

Table of Contents

Prerequisites

Before installing, ensure you have the following installed on your system:

Required Software

  1. Node.js (v18.0.0 or higher)

  2. Git

  3. A modern web browser (Chrome, Firefox, Safari, or Edge)

System Requirements

  • Operating System: macOS, Linux, or Windows
  • RAM: Minimum 4GB (8GB recommended)
  • Disk Space: At least 500MB free space
  • Internet Connection: Required for initial installation

Installation

Step 1: Clone the Repository-SSH

git clone git@github.com:Rarees404/BlockChain.git
cd BlockChain

Step 2: Install Root Dependencies

Install all required packages for the smart contracts and Hardhat:

npm install

This will install:

  • Hardhat and development tools
  • TypeScript and type definitions
  • Viem for Ethereum interactions
  • Testing frameworks

Step 3: Install Frontend Dependencies

Navigate to the frontend directory and install frontend dependencies:

cd frontend
npm install
cd ..

This will install:

  • Next.js framework
  • React and React DOM
  • Wagmi for wallet connections
  • UI components (shadcn/ui, Radix UI)
  • Tailwind CSS

Step 4: Verify Installation

Verify that all dependencies are installed correctly:

# Check Hardhat installation
npx hardhat --version

# Check frontend dependencies
cd frontend
npm list --depth=0
cd ..

Configuration

Contract Addresses

After deploying contracts, update the contract addresses in:

File: frontend/lib/contracts/config.ts

The default addresses are for Hardhat's local network. After deployment, update them with the actual deployed addresses from the deployment output.

Note: The project is configured for local development using Hardhat's local network (Chain ID: 31337).

Running the Application

The application requires three separate terminal windows/tabs to run properly.

Terminal 1: Start Hardhat Node

Start the local Hardhat blockchain network:

npx hardhat node

Keep this terminal running. You should see:

  • Account addresses and private keys
  • Network information (Chain ID: 31337)
  • Listening on http://127.0.0.1:8545

Terminal 2: Deploy Contracts

In a new terminal, deploy the smart contracts to the local network:

npx hardhat ignition deploy ignition/modules/Deploy.ts --network localhost

Important: Copy the deployed contract addresses from the output. You'll need to update frontend/lib/contracts/config.ts with these addresses.

The deployment will create:

  • DigitalIdentity.sol - Patient identity management
  • ConsentManager.sol - Consent lifecycle management
  • DataSharing.sol - Data access control and audit logging
  • HealthToken.sol - ERC-20 token for rewards

Terminal 3: Start Frontend

In a third terminal, start the Next.js development server:

cd frontend
npm run dev

The frontend will be available at: http://localhost:3000

Accessing the Application

Open your browser and navigate to:

http://localhost:3000

Available Pages:

  • / - Home page
  • /register - Patient registration
  • /consents - Consent management
  • /data - Data registration
  • /audit - Audit log viewer
  • /wallet - Token wallet and rewards
  • /doctor - Doctor/provider view
  • /accounts - Account management

Testing

Run All Tests

Execute the complete test suite:

npm test

Run Tests in Watch Mode

Run tests in watch mode for development:

npm run test:watch

Generate Gas Report

Generate a detailed gas usage report:

npm run gas-report

Test Coverage

The test suite includes:

  • Unit Tests: Individual contract functionality
  • Integration Tests: End-to-end user workflows
  • Gas Measurements: Cost analysis for each function

Test Files:

  • test/DigitalIdentity.test.ts - Identity registration tests
  • test/ConsentManager.test.ts - Consent management tests
  • test/DataSharing.test.ts - Data access tests
  • test/HealthToken.test.ts - Token functionality tests
  • test/Integration.test.ts - Complete workflow tests

Project Structure

BlockChain/
├── contracts/              # Solidity smart contracts
│   ├── DigitalIdentity.sol
│   ├── ConsentManager.sol
│   ├── DataSharing.sol
│   └── HealthToken.sol
├── test/                   # Test files
│   ├── DigitalIdentity.test.ts
│   ├── ConsentManager.test.ts
│   ├── DataSharing.test.ts
│   ├── HealthToken.test.ts
│   └── Integration.test.ts
├── scripts/                # Utility scripts
│   ├── distribute-tokens.ts
│   └── generate-gas-report.ts
├── ignition/               # Hardhat Ignition deployment
│   └── modules/
│       └── Deploy.ts
├── frontend/               # Next.js frontend application
│   ├── app/                # Next.js app router pages
│   ├── components/         # React components
│   ├── lib/                # Utilities and configurations
│   └── public/             # Static assets
├── artifacts/              # Compiled contracts (generated)
├── cache/                  # Hardhat cache (generated)
├── hardhat.config.ts       # Hardhat configuration
├── package.json            # Root dependencies
└── README.md               # This file

Technology Stack

Smart Contracts

  • Solidity: 0.8.30
  • Hardhat: Development environment and testing framework
  • Viem: TypeScript Ethereum library
  • Hardhat Ignition: Deployment framework

Frontend

  • Next.js: 16.0.3 (React framework)
  • React: 19.2.0
  • TypeScript: 5.x
  • Wagmi: 2.9.0 (React hooks for Ethereum)
  • Tailwind CSS: 4.x (Styling)
  • shadcn/ui: Component library
  • RainbowKit: Wallet connection UI

Development Tools

  • TypeScript: Type-safe development
  • ESLint: Code linting
  • Node.js: Runtime environment

Cleanup

Clean Build Artifacts

After each development session, clean generated files:

npm run clean

This removes:

  • artifacts/ directory (compiled contracts)
  • cache/ directory (Hardhat cache)
  • ignition/deployments/chain-* directories (deployment artifacts)

Reset Everything (Fresh Start)

To completely reset the blockchain state:

  1. Stop Hardhat node (Ctrl+C in Terminal 1)

  2. Clean deployment artifacts:

    npm run clean
  3. Clear browser localStorage (accounts created during testing):

    • Open browser console (F12 or Cmd+Option+I)
    • Run: localStorage.removeItem("local_wallet_accounts")
    • Or clear all: localStorage.clear()
    • Refresh the page
  4. Restart Hardhat node:

    npx hardhat node

Note:

  • Accounts are generated fresh each time you start Hardhat node
  • The blockchain state resets when you stop/restart the node
  • Only deployment artifacts persist and need to be cleaned
  • Accounts created via UI are stored in browser localStorage and persist until cleared

Troubleshooting

Common Issues

1. next: command not found

Problem: Frontend dependencies not installed.

Solution:

cd frontend
npm install

2. Error: Cannot find module

Problem: Dependencies not installed or node_modules corrupted.

Solution:

# Root directory
rm -rf node_modules package-lock.json
npm install

# Frontend directory
cd frontend
rm -rf node_modules package-lock.json
npm install

3. Port 8545 already in use

Problem: Another Hardhat node is already running.

Solution:

  • Find and kill the process: lsof -ti:8545 | xargs kill
  • Or use a different port in hardhat.config.ts

4. Port 3000 already in use

Problem: Another Next.js app is running on port 3000.

Solution:

  • Kill the process: lsof -ti:3000 | xargs kill
  • Or run on a different port: PORT=3001 npm run dev

5. Contract addresses not found

Problem: Contracts not deployed or addresses not updated.

Solution:

  1. Deploy contracts: npx hardhat ignition deploy ignition/modules/Deploy.ts --network localhost
  2. Copy addresses from output
  3. Update frontend/lib/contracts/config.ts

6. Transaction failed errors

Problem: Insufficient balance or network mismatch.

Solution:

  • Ensure Hardhat node is running
  • Check that you're connected to the correct network (Chain ID: 31337)
  • Hardhat node provides test accounts with ETH balance

7. Tests failing

Problem: Contracts not compiled or outdated artifacts.

Solution:

npx hardhat compile
npm test

Getting Help

If you encounter issues not covered here:

  1. Check the terminal output for detailed error messages
  2. Verify all prerequisites are installed correctly
  3. Ensure all three terminals are running (node, deploy, frontend)
  4. Check that contract addresses match in config.ts
  5. Clear browser cache and localStorage
  6. Restart all processes

Additional Commands

Compile Contracts

npx hardhat compile

Run Specific Test File

npx hardhat test test/Integration.test.ts

Build Frontend for Production

cd frontend
npm run build
npm start

Lint Frontend Code

cd frontend
npm run lint

Project: Decentralized Digital Identity and Data Sharing Platform
Use Case: Healthcare - Patient-Controlled Medical Records
License: MIT

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors