Organ Donation Registry Contract

What it does:
Registers and manages verified organ donation consent on-chain, allowing authorized medical authorities to check donor status instantly during critical situations.

Why it matters:
Eliminates ambiguity, delays, and disputes around organ donation consent, ensuring patient wishes are honored while saving lives through trusted, real-time verification.

How it works:

  • Individuals register their organ donation consent on-chain

  • Donors specify which organs are eligible for donation

  • Consent status can be updated or revoked at any time

  • Authorized hospitals can verify donor consent instantly

  • Emergency access is restricted and fully auditable

  • All consent actions are immutably recorded on-chain

      // SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

/**
 * @title OrganDonationRegistry
 * @author Nam
 * @notice On-chain registry for organ donation consent
 */
contract OrganDonationRegistry {

    // -------------------- ROLES --------------------

    address public admin;

    mapping(address => bool) public authorizedHospitals;

    // -------------------- DONOR STRUCT --------------------

    struct DonorConsent {
        bool registered;
        bool donateHeart;
        bool donateLiver;
        bool donateKidney;
        bool donateLungs;
        uint256 lastUpdated;
    }

    // donor => consent
    mapping(address => DonorConsent) public donorConsents;

    // -------------------- EVENTS --------------------

    event HospitalAuthorized(address indexed hospital);
    event ConsentRegistered(address indexed donor);
    event ConsentUpdated(address indexed donor);
    event ConsentRevoked(address indexed donor);

    // -------------------- MODIFIERS --------------------

    modifier onlyAdmin() {
        require(msg.sender == admin, "Not admin");
        _;
    }

    modifier onlyHospital() {
        require(authorizedHospitals[msg.sender], "Not authorized hospital");
        _;
    }

    // -------------------- CONSTRUCTOR --------------------

    constructor() {
        admin = msg.sender;
    }

    // -------------------- HOSPITAL AUTHORIZATION --------------------

    /**
     * @notice Authorize a hospital
     */
    function authorizeHospital(address _hospital)
        external
        onlyAdmin
    {
        require(_hospital != address(0), "Invalid hospital");
        authorizedHospitals[_hospital] = true;
        emit HospitalAuthorized(_hospital);
    }

    // -------------------- DONOR CONSENT --------------------

    /**
     * @notice Register organ donation consent
     */
    function registerConsent(
        bool _heart,
        bool _liver,
        bool _kidney,
        bool _lungs
    )
        external
    {
        donorConsents[msg.sender] = DonorConsent({
            registered: true,
            donateHeart: _heart,
            donateLiver: _liver,
            donateKidney: _kidney,
            donateLungs: _lungs,
            lastUpdated: block.timestamp
        });

        emit ConsentRegistered(msg.sender);
    }

    /**
     * @notice Update organ donation consent
     */
    function updateConsent(
        bool _heart,
        bool _liver,
        bool _kidney,
        bool _lungs
    )
        external
    {
        require(donorConsents[msg.sender].registered, "Not registered");

        donorConsents[msg.sender].donateHeart = _heart;
        donorConsents[msg.sender].donateLiver = _liver;
        donorConsents[msg.sender].donateKidney = _kidney;
        donorConsents[msg.sender].donateLungs = _lungs;
        donorConsents[msg.sender].lastUpdated = block.timestamp;

        emit ConsentUpdated(msg.sender);
    }

    /**
     * @notice Revoke donation consent
     */
    function revokeConsent() external {
        require(donorConsents[msg.sender].registered, "Not registered");

        delete donorConsents[msg.sender];
        emit ConsentRevoked(msg.sender);
    }

    // -------------------- VERIFICATION --------------------

    /**
     * @notice Verify donor consent (read-only)
     */
    function verifyConsent(address _donor)
        external
        view
        onlyHospital
        returns (
            bool registered,
            bool heart,
            bool liver,
            bool kidney,
            bool lungs,
            uint256 lastUpdated
        )
    {
        DonorConsent memory d = donorConsents[_donor];

        return (
            d.registered,
            d.donateHeart,
            d.donateLiver,
            d.donateKidney,
            d.donateLungs,
            d.lastUpdated
        );
    }
}