A complete rewrite of the EHR system implementing blockchain-based health records with ECIES encryption, EIP-712 signatures, and FHIR R4 support.
- KeyRegistry: On-chain public key management with rotation support
- PatientHealthRecords: Individual patient record contract with permissions
- PatientRecordsFactory: Factory pattern for deploying patient contracts
- Controllers: Auth, Records, Permissions, Keys, Emergency
- Services: Blockchain, Storage (IPFS/S3), Database
- Middleware: Authentication (JWT), Validation, Error Handling
- Database: PostgreSQL with dual ORM support (pg-promise + Prisma)
- Repository Pattern: Clean Architecture example included
- ECIES (secp256k1): Public key encryption for key wrapping
- AES-256-GCM: Symmetric encryption with AEAD for records
- EIP-712: Typed structured data signatures
- SHA-256: Content integrity verification
- Node.js >= 18.0.0
- PostgreSQL >= 14
- IPFS node (optional)
- Hardhat local network OR Ethereum testnet
- Clone and install dependencies
npm install --legacy-peer-deps- Configure environment
cp .env.example .env
# Edit .env with your configuration- Create database
psql -U postgres -c "CREATE DATABASE \"Blockchain-Healthcare\";"- Run migrations
npm run migrate- Generate Prisma Client (optional - if using Prisma ORM)
npm run prisma:generate- Compile smart contracts
npm run compile- Start local blockchain
# Terminal 1
npm run node- Deploy smart contracts
# Terminal 2
npm run deploy- Update .env with deployed contract addresses
# Copy addresses from deployment output to .env
KEY_REGISTRY_ADDRESS=0x...
FACTORY_CONTRACT_ADDRESS=0x...- Start backend server
npm run devServer will be running at http://localhost:3000
# Smart contract tests (15 tests)
npm run test:contracts
# Run specific test files
npx hardhat test test/contracts/BasicTest.test.js
npx hardhat test test/services/BlockchainServices.test.js
npx hardhat test test/utils/CryptoUtils.test.js
npx hardhat test test/middleware/Auth.test.js- β Smart Contracts: 15/15 (100%)
- β Crypto Utilities: 15/15 (100%)
- β Blockchain Services: 23/25 (92%)
- β Middleware Patterns: 21/21 (100%)
- β Database Layer: All passing
- Overall: 74/76 tests (97.4%)
Base URL: http://localhost:3000/api
Register new user account
{
"name": "Dr. John Doe",
"email": "[email protected]",
"password": "SecurePass123",
"role": "doctor",
"publicKey": "0x04...",
"privateKey": "0x..." // optional, will generate if not provided
}Sign in and get tokens
{
"email": "[email protected]",
"password": "SecurePass123",
"privateKey": "0x..." // optional but recommended
}Refresh access token
{
"refreshToken": "eyJhbGciOiJIUzI1NiIs..."
}Add new health record (Patient only)
{
"fhirData": {
"resourceType": "Observation",
"status": "final",
"code": { ... }
},
"recipientPublicKeys": [
{
"address": "0x...",
"publicKey": "0x04..."
}
]
}Get single record (requires access)
List all accessible records
Grant permission to access records (Patient only)
{
"grantedTo": "0x...",
"recordIds": [0, 1, 2],
"wrappedKey": "0x...",
"expirationTime": 1735689600
}Revoke permission (Patient only)
List all permissions granted by patient
Request emergency access (Doctor only)
{
"patientAddress": "0x...",
"recordId": 0,
"justificationCode": 1
}Justification codes: 1=Trauma, 2=Unconscious, 3=Critical
- users: User accounts (patients, doctors, admins)
- records: Health records metadata
- permissions: Access permissions cache
- access_logs: Record access tracking
- emergency_grants: Emergency access requests
- sessions: User session management
- audit_log: Blockchain transaction audit trail
- Encryption: ECIES + AES-256-GCM with authentication tags
- Key Management: On-chain public key registry with rotation
- Access Control: Blockchain-based permissions with expiration
- Signatures: EIP-712 typed structured data
- Content Integrity: SHA-256 digest verification
- JWT Authentication: Access + refresh token pattern
- Password Hashing: bcrypt with configurable rounds
Backend/
βββ contracts/ # Smart contracts
β βββ KeyRegistry.sol
β βββ PatientHealthRecords.sol
β βββ PatientRecordsFactory.sol
βββ src/
β βββ controllers/ # API controllers
β βββ routes/ # API routes
β βββ services/ # Business logic
β βββ middleware/ # Express middleware
β βββ utils/ # Cryptography utilities
β βββ app.ts # Express app
β βββ server.ts # Server entry point
βββ test/ # Test suites
βββ migrations/ # Database migrations
βββ scripts/ # Deployment scripts
npm run devnpm run build
npm startMIT License