Skip to content

eth-ecuador/hygiea

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

17 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿฅ Confidential Medical Records System

Blockchain medical records system with end-to-end encryption using Oasis Sapphire.

๐ŸŽฏ Features

โœ… Automatic Encryption: All data is encrypted on blockchain โœ… Access Control: Patients authorize which doctors can view their data โœ… Guaranteed Privacy: Data inaccessible without permissions โœ… Immutable: Permanent and verifiable medical history โœ… Easy to Use: Intuitive web interface


๐Ÿ“Š Stored Data (Encrypted)

1. Affiliation Data

  • Full name
  • Age
  • Gender
  • Blood type
  • Address
  • Phone
  • Email
  • Social Security Number

2. Existing Diseases

  • Disease name
  • Diagnosis description
  • Diagnosis date
  • Severity (Mild, Moderate, Severe)
  • Status (Active/Inactive)
  • Treatment

3. Medical History

  • Personal: Surgeries, past illnesses, allergies
  • Family: Hereditary conditions, family history

๐Ÿš€ Installation and Setup

Prerequisites

  • Node.js v18+
  • npm or yarn
  • MetaMask installed
  • TEST tokens from Sapphire Testnet

1. Clone and Install Backend

cd medical-records-sapphire
npm install

2. Configure Environment Variables

cp .env.example .env

Edit .env and add your private key:

PRIVATE_KEY=0xtu_clave_privada_aqui

โš ๏ธ NEVER share your private key or upload it to git.

3. Get Testnet Tokens

  1. Go to https://faucet.testnet.oasis.io/
  2. Select "Sapphire"
  3. Enter your address
  4. Get 100 TEST tokens

4. Compile Contracts

npx hardhat compile

5. Run Tests (Optional)

# Option 1: Tests on Hardhat Network
npx hardhat test

# Option 2: Tests on Sapphire Localnet (more realistic)
# Terminal 1: Start localnet
docker run -it -p 8545:8545 ghcr.io/oasisprotocol/sapphire-localnet

# Terminal 2: Run tests
npx hardhat test --network sapphire-localnet

6. Deploy to Testnet

npx hardhat run scripts/deploy.js --network sapphire-testnet

Save the contract address that appears in the console.

7. Configure Frontend

cd frontend
npm install

Create the .env file:

cp .env.example .env

Edit frontend/.env:

VITE_CONTRACT_ADDRESS=0xDireccion_Del_Contrato_Desplegado
VITE_NETWORK=testnet

8. Start Frontend

npm run dev

Open http://localhost:3000 in your browser.


๐Ÿ”ง Configure MetaMask

Add Sapphire Testnet manually:

  1. Open MetaMask
  2. Click on the network selector
  3. "Add Network" โ†’ "Add Network Manually"
  4. Enter:
Network Name: Sapphire Testnet
RPC URL: https://testnet.sapphire.oasis.io
Chain ID: 23295
Currency Symbol: TEST
Block Explorer: https://explorer.oasis.io/testnet/sapphire

๐Ÿ“– User Guide

As a Patient

1. Register

  1. Connect your wallet
  2. Go to the "๐Ÿ“‹ Registro" tab
  3. Fill out the form with your data
  4. Click "Registrar Paciente"
  5. Confirm the transaction in MetaMask

2. Add Diseases

  1. Go to "๐Ÿฉบ Enfermedades"
  2. Fill in the disease data
  3. Save (encrypted transaction)

3. Add Medical History

  1. Go to "๐Ÿ“š Antecedentes"
  2. Select type (Personal/Family)
  3. Add the condition and details

4. View Your Records

  1. Go to "๐Ÿ‘๏ธ Ver Registros"
  2. Leave your address (appears automatically)
  3. You will see all your encrypted data

5. Authorize Doctors

  1. Go to "๐Ÿ” Permisos"
  2. Enter the doctor's address
  3. Click "Autorizar Mรฉdico"
  4. The doctor can now view your history

As a Doctor

1. Register as a Doctor

  1. Connect your wallet
  2. Go to "๐Ÿ“‹ Registro"
  3. Enter your name in "Registrar Mรฉdico"

2. Access Patient Records

  1. The patient must authorize you first
  2. Go to "๐Ÿ‘๏ธ Ver Registros"
  3. Enter the patient's address
  4. You will see their complete history

3. Add Diagnoses

  1. Go to "๐Ÿฉบ Enfermedades"
  2. Enter the patient's address (who authorized you)
  3. Add the disease and treatment

๐Ÿ—๏ธ System Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚         FRONTEND (React)            โ”‚
โ”‚   - Medical forms                   โ”‚
โ”‚   - Data visualization              โ”‚
โ”‚   - Permission management           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
               โ”‚ Wagmi + Sapphire Wrapper
               โ”‚ (Automatic encryption)
               โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   SAPPHIRE BLOCKCHAIN (Testnet)     โ”‚
โ”‚                                     โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚  Smart Contract               โ”‚ โ”‚
โ”‚  โ”‚  MedicalRecords.sol           โ”‚ โ”‚
โ”‚  โ”‚                               โ”‚ โ”‚
โ”‚  โ”‚  - Encrypted data in storage  โ”‚ โ”‚
โ”‚  โ”‚  - Access control             โ”‚ โ”‚
โ”‚  โ”‚  - Granular permissions       โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ”’ Security and Privacy

How Encryption Works

  1. Encrypted Storage: All contract storage is encrypted by default in Sapphire
  2. Encrypted Transactions: Wagmi wrapper automatically encrypts transactions
  3. TEE (Trusted Execution Environment): Data is only decrypted inside the TEE
  4. Access Control: Solidity modifiers prevent unauthorized access

What is NOT Encrypted

โš ๏ธ Events are NOT encrypted. That's why the contract only emits:

  • User addresses
  • Timestamps
  • Action confirmations

Sensitive data like names, diagnoses, etc. are NEVER emitted.

Immutable/Constant Variables

โš ๏ธ IMPORTANT: immutable and constant variables are NOT encrypted because they are stored in the bytecode.

// โŒ BAD - Visible data
bytes32 private constant SECRET = "mi_secreto";

// โœ… GOOD - Encrypted data
bytes32 private secret;

Testnet vs Mainnet

Feature Testnet Mainnet
Confidentiality โš ๏ธ NOT guaranteed โœ… Guaranteed
Cost ๐Ÿ†“ Free ๐Ÿ’ฐ ROSE tokens
Data ๐Ÿงช Testing only ๐Ÿ”’ Real data

๐Ÿงช Testing

Unit Tests

npx hardhat test

Tests with Coverage

npx hardhat coverage

Tests on Sapphire Localnet

# Terminal 1
docker run -it -p 8545:8545 ghcr.io/oasisprotocol/sapphire-localnet

# Terminal 2
npx hardhat test --network sapphire-localnet

๐Ÿ“‚ Project Structure

medical-records-sapphire/
โ”œโ”€โ”€ contracts/
โ”‚   โ””โ”€โ”€ MedicalRecords.sol        # Main contract
โ”œโ”€โ”€ scripts/
โ”‚   โ””โ”€โ”€ deploy.js                  # Deployment script
โ”œโ”€โ”€ test/
โ”‚   โ””โ”€โ”€ MedicalRecords.test.js     # Tests
โ”œโ”€โ”€ frontend/
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ components/            # React components
โ”‚   โ”‚   โ”œโ”€โ”€ config/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ wagmi.js           # Wagmi configuration
โ”‚   โ”‚   โ”œโ”€โ”€ contracts/             # ABIs (generated)
โ”‚   โ”‚   โ”œโ”€โ”€ styles/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ App.css
โ”‚   โ”‚   โ”œโ”€โ”€ App.jsx
โ”‚   โ”‚   โ””โ”€โ”€ main.jsx
โ”‚   โ”œโ”€โ”€ index.html
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ””โ”€โ”€ vite.config.js
โ”œโ”€โ”€ hardhat.config.js
โ”œโ”€โ”€ package.json
โ””โ”€โ”€ README.md

๐ŸŽจ Screenshots

Patient Registration

Form with all encrypted affiliation fields.

Disease Management

Add diagnoses with severity and treatment.

Permission Control

Authorize/Revoke access to specific doctors.

Records View

Complete encrypted history accessible only with permissions.


๐Ÿšจ Troubleshooting

Error: "Already registered"

  • You can only register once per address
  • Use another wallet to create another patient

Error: "No tienes permiso"

  • Verify that the patient has authorized you
  • Confirm that you are registered as a doctor

Transactions fail

  • Verify that you have TEST tokens
  • Confirm that you are on Sapphire Testnet
  • Check MetaMask configuration

Deployed contract not visible

  • Copy the correct address to frontend/.env
  • Restart the frontend server (npm run dev)

๐Ÿ”ฎ Upcoming Features

Ideas to expand the system:

  • Medical Prescriptions: Issue encrypted prescriptions
  • Medical Appointments: Scheduling system
  • Notifications: Alerts for new diagnoses
  • Export PDF: Generate medical reports
  • Medical Images: Store X-rays, MRIs (IPFS + encryption)
  • Laboratories: Integrate test results
  • Pharmacies: Prescription verification
  • Insurance: Controlled access for insurers
  • Data Update: Modify existing diseases
  • Search: Filter by disease type, date, etc.
  • Multi-language: Support for multiple languages
  • Audit: Log of who accessed what data

๐Ÿ“š Additional Documentation

Sapphire

Wagmi

Hardhat


๐Ÿค Contributing

Contributions are welcome. Please:

  1. Fork the project
  2. Create a branch for your feature (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

โš–๏ธ License

MIT License - Free to use for educational and commercial projects.


โš ๏ธ Disclaimer

This project is an educational demonstration. For production use with real medical data:

  1. โœ… Audit the smart contract
  2. โœ… Deploy to Sapphire Mainnet (NOT testnet)
  3. โœ… Comply with regulations (HIPAA, GDPR, etc.)
  4. โœ… Implement robust authentication
  5. โœ… Add additional encryption on the frontend
  6. โœ… Backups and disaster recovery
  7. โœ… Consult with legal and medical experts

๐Ÿ“ž Support

Questions or problems?


๐ŸŽ“ Built With

  • Oasis Sapphire - Confidential blockchain
  • Solidity - Smart contracts
  • Hardhat - Development framework
  • React - Frontend
  • Wagmi - Ethereum library for React
  • Viem - TypeScript Ethereum interface
  • Vite - Build tool

Made with โค๏ธ to improve privacy in digital health

About

Private sensible health data network

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages