Skip to content
Verified contractExactMIT

ProductVerification

Address0xdac403d5bb082996b6eb8158439423cfd5dbfbaa
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.28

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.28+commit.7893614a
Language
solidity
Optimization
No
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,324,799
Creation transaction
0xeb4b18d237…bebd165928

contracts/Productverification.sol

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

import "./StakeholderManagement.sol";
import "./ProductManagement.sol";
import "./TrackingStorage.sol";
import "./TraceabilityStorage.sol";

/**
 * @title ProductVerification
 * @dev Main contract that orchestrates stakeholder, product,
 * tracking, and traceability management. It contains the core
 * verification logic and cross-dependent registration functions.
 */
contract ProductVerification is StakeholderManagement, ProductManagement, TrackingStorage, TraceabilityStorage {

    // The constructor from Owned.sol is automatically called, setting the deployer as owner.

    error LotExists();
    error SerialExists();
    error SerialDoesNotExist();
    error SerialLotIdMismatch();
    error SerialLotProductMismatch();
    error SerialLotManufacturerMismatch();
    error SerialLotDistributorMismatch();

    /**
     * @dev Registers a new lot and a batch of serials associated with it.
     */
    function registerSerialsBatch(Lot memory lot, Serial[] memory serialList) public onlyOwner {
        // 1. Register the Lot
        if (lots[lot.id].id != 0) revert LotExists();
        if (products[lot.productId].id == 0) revert ProductDoesNotExist();

        lots[lot.id] = lot;
        emit LotRegistered(lot.id, lot.productId, lot.manufacturerId);

        // 2. Register the Serials
        uint64[] memory ids = new uint64[](serialList.length);
        bytes32[] memory serialHashes = new bytes32[](serialList.length);

        for (uint256 i = 0; i < serialList.length; i++) {
            Serial memory serial = serialList[i];
            if (serials[serial.serialHash].serialHash != 0) revert SerialExists();

            // Check that serial data matches the lot data it claims to be part of
            if (serial.lotId != lot.id) revert SerialLotIdMismatch();

            serials[serial.serialHash] = serial;
            emit SerialRegistered(serial.serialHash, serial.lotId);
        }

        emit SerialBatchRegistered(ids, serialHashes, lot.id, lot.productId);
    }

    /**
     * @dev The primary public-facing function to verify a product's authenticity.
     * @param serialNumber The serial number encoded (directly or via a lookup URL) in the product's QR code.
     * registered with, letting callers detect a cloned QR code printed with a genuine serial number.
     */
    function verifyProduct(string memory serialNumber) public view returns (
        bool verified,
        uint256 scanCount,
        Serial memory serial
    ) {
        serial = serials[keccak256(bytes(serialNumber))];
        if (serial.serialHash == 0) {
            return (false, 0, serial);
        }

        return (true, scanHistory[serial.serialHash].length, serial);
    }

    /**
     * @dev Records a custody handoff for a serial as it moves through the supply chain
     * (e.g. manufacturer -> distributor -> retailer -> consumer). Append-only history;
     * the contract does not judge or reject transfers, it only records them.
     */
    function recordCustodyTransfer(CustodyEvent memory cusdtodyEvent) public onlyOwner {
        if (serials[cusdtodyEvent.serialHash].serialHash == 0) revert SerialDoesNotExist();

        uint64 id = nextCustodyEventId++;
        custodyHistory[cusdtodyEvent.serialHash].push(CustodyEvent(id, cusdtodyEvent.serialHash, cusdtodyEvent.actorId, cusdtodyEvent.actorRole, cusdtodyEvent.location, cusdtodyEvent.timestamp));
        emit CustodyTransferRecorded(id, cusdtodyEvent.serialHash, cusdtodyEvent.actorId, cusdtodyEvent.actorRole, cusdtodyEvent.location, cusdtodyEvent.timestamp);
    }

    /**
     * @dev Records a QR-code scan/verification attempt for a serial. Append-only history
     * used to spot anti-counterfeit red flags (e.g. the same serial scanned in
     * implausibly distant locations in a short window) - that analysis happens off-chain.
     */
    function recordScan(ScanEvent memory scanEvent) public {
        if (serials[scanEvent.serialHash].serialHash == 0) revert SerialDoesNotExist();

        uint64 id = nextScanEventId++;
        scanHistory[scanEvent.serialHash].push(ScanEvent(id, scanEvent.serialHash, scanEvent.locationHash, scanEvent.timestamp, msg.sender));
        emit ProductScanned(id, scanEvent.serialHash, msg.sender, scanEvent.locationHash, scanEvent.timestamp);
    }
}

contracts/Owned.sol

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

/**
 * @title Owned
 * @dev Provides basic authorization control.
 */
abstract contract Owned {
    address public owner;

    error NotOwner();

    modifier onlyOwner() {
        if (msg.sender != owner) revert NotOwner();
        _;
    }

    constructor() {
        owner = msg.sender;
    }
}

contracts/ProductManagement.sol

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

import "./Owned.sol";
import "./StorageStructs.sol";

/**
 * @title ProductManagement
 * @dev Manages the registration and storage of products and certifications.
 */
abstract contract ProductManagement is Owned, StorageStructs {
    mapping(uint32 => Product) internal products;
    mapping(uint32 => Certification) internal certifications;

    event ProductRegistered(uint32 indexed id);
    event CertificationRegistered(uint32 indexed id, uint32 indexed productId);

    error ProductExists();
    error CertificationExists();
    error ProductDoesNotExist();

    function registerProduct(Product memory product) public onlyOwner {
        if (products[product.id].id != 0) revert ProductExists();
        products[product.id] = product;
        emit ProductRegistered(product.id);
    }

    function registerCertification(Certification memory certification) public onlyOwner {
        if (certifications[certification.id].id != 0) revert CertificationExists();
        if (products[certification.productId].id == 0) revert ProductDoesNotExist();
        certifications[certification.id] = certification;
        emit CertificationRegistered(certification.id, certification.productId);
    }

    // --- Getters ---
    function getProduct(uint32 id) public view returns (Product memory) { return products[id]; }
    function getCertification(uint32 id) public view returns (Certification memory) { return certifications[id]; }
}

contracts/StakeholderManagement.sol

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

import "./Owned.sol";
import "./StorageStructs.sol";

/**
 * @title StakeholderManagement
 * @dev Manages the registration and storage of manufacturers,
 * distributors, and brand owners.
 */
abstract contract StakeholderManagement is Owned, StorageStructs {
    mapping(uint32 => Stakeholder) internal stakeholders;

    event StakeholderRegistered(uint32 indexed id);
    event StakeholderUpdated(uint32 indexed id);
    error StakeholderExists();
    error StakeholderDoesNotExist();

    function registerStakeholder(Stakeholder memory stakeholder) public onlyOwner {
        if (stakeholders[stakeholder.id].id != 0) revert StakeholderExists();
        stakeholders[stakeholder.id] = stakeholder;
        emit StakeholderRegistered(stakeholder.id);
    }

    function updateStakeholder(Stakeholder memory stakeholder) public onlyOwner {
        if (stakeholders[stakeholder.id].id == 0) revert StakeholderDoesNotExist();
        stakeholders[stakeholder.id] = stakeholder;
        emit StakeholderUpdated(stakeholder.id);
    }

    function getStakeholder(uint32 id) public view returns (Stakeholder memory) { return stakeholders[id]; }
}

contracts/StorageStructs.sol

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

/**
 * @title StorageStructs
 * @dev Defines all data structures used across the product verification system.
 */
contract StorageStructs {
    // Manufacturers, distributors, and brand owners share this identical shape;
    // using one struct lets the compiler reuse a single set of ABI encode/decode
    // routines for all three instead of generating one per type.
    struct Stakeholder {
        uint32 id;
        string role;
        string name;
        string location;
        string road;
        string subdistrict;
        string district;
        string province;
        string postalCode;
        string country;
        string phone;
        string email;
    }

    struct Product {
        uint32 id;
        string code;
        string name;
        uint32 brandId;
        string brand;
        uint256 price;
        uint8 decimals;
        string currency;
        string description;
        string ingredients;
        string instructions;
        string precautions;
        uint32 quantity;
        string unit;
        string customFields; // JSON object string
    }

    struct Lot {
        uint64 id;
        uint32 productId;
        string lotNumber;
        uint32 quantity;
        uint32 manufacturerId;
        int64 manufacturingDate;
        int64 expirationDate;
        string countryOfOrigin;
        uint32[] certificationIds;
    }

    struct Serial {
        bytes32 serialHash;
        string status;
        int64 claimedDate;
        uint64 lotId;
        string lotNumber;
    }

    struct Certification {
        uint32 id;
        string registrationNumber;
        string name;
        string description;
        uint32 productId;
        string status;
        int64 registrationDate;
        int64 expirationDate;
    }

    struct CustodyEvent {
        uint64 id;
        bytes32 serialHash;
        uint32 actorId;
        string actorRole;
        string location;
        int64 timestamp;
    }

    struct ScanEvent {
        uint64 id;
        bytes32 serialHash;
        bytes32 locationHash;
        int64 timestamp;
        address scanner;
    }
}

contracts/TraceabilityStorage.sol

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

import "./StorageStructs.sol";

/**
 * @title TraceabilityStorage
 * @dev Defines storage, events, and getters for the anti-counterfeit
 * traceability history of a serial: custody handoffs across the supply
 * chain and QR-code verification scans. Recording logic lives in the
 * main contract due to dependencies on serial registration data.
 */
abstract contract TraceabilityStorage is StorageStructs {
    mapping(bytes32 => CustodyEvent[]) internal custodyHistory;
    mapping(bytes32 => ScanEvent[]) internal scanHistory;

    uint64 internal nextCustodyEventId = 1;
    uint64 internal nextScanEventId = 1;

    event CustodyTransferRecorded(
        uint64 indexed id,
        bytes32 serialHash,
        uint32 indexed actorId,
        string actorRole,
        string location,
        int64 timestamp
    );

    event ProductScanned(
        uint64 indexed id,
        bytes32 serialHash,
        address indexed scanner,
        bytes32 locationHash,
        int64 timestamp
    );

    // --- Getters ---
    function getCustodyHistory(bytes32 serialHash) public view returns (CustodyEvent[] memory) {
        return custodyHistory[serialHash];
    }

    function getScanHistory(bytes32 serialHash) public view returns (ScanEvent[] memory) {
        return scanHistory[serialHash];
    }

    function getScanCount(bytes32 serialHash) public view returns (uint256) {
        return scanHistory[serialHash].length;
    }
}

contracts/TrackingStorage.sol

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

import "./StorageStructs.sol";

/**
 * @title TrackingStorage
 * @dev Defines storage, events, and getters for lots and serials.
 * Registration logic is handled in the main contract due to
 * dependencies on stakeholder and product data.
 */
abstract contract TrackingStorage is StorageStructs {
    mapping(uint64 => Lot) internal lots;
    mapping(bytes32 => Serial) internal serials;

    event LotRegistered(uint64 indexed id, uint32 indexed productId, uint32 indexed manufacturerId);
    event SerialRegistered(bytes32 serialHash, uint64 indexed lotId);
    event SerialBatchRegistered(uint64[] ids, bytes32[] serialHashes, uint64 indexed lotId, uint32 indexed productId);

    // --- Getters ---
    function getLot(uint64 id) public view returns (Lot memory) { return lots[id]; }
    function getSerial(string memory serialNumber) public view returns (Serial memory) { return serials[keccak256(bytes(serialNumber))]; }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.