Pharmacy Authenticity Verification

What it does:
Verifies that a pharmacy is licensed, authentic, and authorized to dispense medication by validating its credentials on-chain.

Why it matters:
Prevents counterfeit drug distribution, protects patients from fraudulent pharmacies, and creates a trusted pharmaceutical network.

How it works:

  • Regulatory authority authorizes pharmacy issuers

  • Pharmacies register with verified credentials

  • License data is stored as cryptographic hashes

  • Patients and providers can verify pharmacy status

  • Licenses can be renewed or revoked on-chain

  • All verification actions are publicly auditable

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

/**
 * @title PharmacyAuthenticityVerification
 * @author Nam
 * @notice Verifies licensed pharmacies on-chain
 */
contract PharmacyAuthenticityVerification {

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

    address public regulator;

    // -------------------- STRUCTS --------------------

    struct Pharmacy {
        bool registered;
        string licenseHash;   // IPFS / encrypted license
        uint256 validUntil;
        bool active;
    }

    // -------------------- STORAGE --------------------

    mapping(address => Pharmacy) public pharmacies;

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

    event PharmacyRegistered(address indexed pharmacy);
    event LicenseRenewed(address indexed pharmacy, uint256 validUntil);
    event PharmacyRevoked(address indexed pharmacy);

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

    modifier onlyRegulator() {
        require(msg.sender == regulator, "Not regulator");
        _;
    }

    modifier pharmacyExists(address _pharmacy) {
        require(pharmacies[_pharmacy].registered, "Pharmacy not registered");
        _;
    }

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

    constructor() {
        regulator = msg.sender;
    }

    // -------------------- REGISTRATION --------------------

    /**
     * @notice Register a licensed pharmacy
     */
    function registerPharmacy(
        address _pharmacy,
        string calldata _licenseHash,
        uint256 _validUntil
    )
        external
        onlyRegulator
    {
        require(_pharmacy != address(0), "Invalid address");
        require(_validUntil > block.timestamp, "Invalid expiry");

        pharmacies[_pharmacy] = Pharmacy({
            registered: true,
            licenseHash: _licenseHash,
            validUntil: _validUntil,
            active: true
        });

        emit PharmacyRegistered(_pharmacy);
    }

    // -------------------- LICENSE MANAGEMENT --------------------

    /**
     * @notice Renew pharmacy license
     */
    function renewLicense(
        address _pharmacy,
        uint256 _newValidUntil
    )
        external
        onlyRegulator
        pharmacyExists(_pharmacy)
    {
        require(_newValidUntil > pharmacies[_pharmacy].validUntil, "Invalid renewal");

        pharmacies[_pharmacy].validUntil = _newValidUntil;
        pharmacies[_pharmacy].active = true;

        emit LicenseRenewed(_pharmacy, _newValidUntil);
    }

    /**
     * @notice Revoke pharmacy authorization
     */
    function revokePharmacy(address _pharmacy)
        external
        onlyRegulator
        pharmacyExists(_pharmacy)
    {
        pharmacies[_pharmacy].active = false;
        emit PharmacyRevoked(_pharmacy);
    }

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

    /**
     * @notice Verify pharmacy authenticity
     */
    function verifyPharmacy(address _pharmacy)
        external
        view
        returns (
            bool active,
            uint256 validUntil,
            string memory licenseHash
        )
    {
        Pharmacy memory p = pharmacies[_pharmacy];
        require(p.registered, "Not registered");

        return (
            p.active && p.validUntil >= block.timestamp,
            p.validUntil,
            p.licenseHash
        );
    }
}