Skip to content
Verified contractExactMIT

AgreementFactory

Address0xcf317fe605397bc3fae6dad06331ae5154f277ff
Balance

0 ETH

 

Transactions

12

 

Compiler

solc 0.8.30

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
Yes, 1,000 runs
EVM version
prague
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
24,334,395
Creation transaction
0x8f0a154f18…e6b86103c6

src/AgreementFactory.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;

import { Agreement } from "src/Agreement.sol";
import { AgreementDetails } from "src/types/AgreementTypes.sol";

/// @title Factory for creating Agreement contracts
/// @notice Uses CREATE2 with chain-specific salts to prevent address collisions across chains
contract AgreementFactory {
    // ----- EVENTS -----
    event AgreementCreated(address indexed agreement, address indexed owner, bytes32 salt);

    // ----- EXTERNAL FUNCTIONS -----

    /// @notice Creates an Agreement contract using CREATE2.
    /// @dev The salt includes block.chainid to ensure different addresses on different chains.
    /// @param details The agreement details
    /// @param chainValidator The Chain Validator contract address
    /// @param owner The owner of the agreement
    /// @param salt A user-provided salt for additional uniqueness
    /// @return agreementAddress The address of the created agreement
    function create(
        AgreementDetails memory details,
        address chainValidator,
        address owner,
        bytes32 salt
    )
        external
        returns (address agreementAddress)
    {
        // Include chainid in salt to prevent cross-chain address collisions
        bytes32 finalSalt;
        assembly {
            let ptr := mload(0x40)
            mstore(ptr, chainid())
            mstore(add(ptr, 0x20), caller())
            mstore(add(ptr, 0x40), salt)
            finalSalt := keccak256(ptr, 0x60)
            mstore(0x40, add(ptr, 0x60))
        }

        Agreement agreement = new Agreement{ salt: finalSalt }(details, chainValidator, owner);
        agreementAddress = address(agreement);

        emit AgreementCreated(agreementAddress, owner, finalSalt);
    }

    /// @notice Computes the address where an agreement would be deployed.
    /// @param details The agreement details
    /// @param chainValidator The Chain Validator contract address
    /// @param owner The owner of the agreement
    /// @param salt A user-provided salt
    /// @param deployer The address that will call create() (msg.sender)
    /// @return The predicted agreement address
    function computeAddress(
        AgreementDetails memory details,
        address chainValidator,
        address owner,
        bytes32 salt,
        address deployer
    )
        external
        view
        returns (address)
    {
        bytes32 finalSalt;
        assembly {
            let ptr := mload(0x40)
            mstore(ptr, chainid())
            mstore(add(ptr, 0x20), deployer)
            mstore(add(ptr, 0x40), salt)
            finalSalt := keccak256(ptr, 0x60)
            mstore(0x40, add(ptr, 0x60))
        }

        bytes memory initCode = bytes.concat(type(Agreement).creationCode, abi.encode(details, chainValidator, owner));
        bytes32 bytecodeHash;
        assembly {
            bytecodeHash := keccak256(add(initCode, 0x20), mload(initCode))
        }

        return address(
            uint160(uint256(keccak256(bytes.concat(bytes1(0xff), bytes20(address(this)), finalSalt, bytecodeHash))))
        );
    }
}

lib/openzeppelin-contracts/contracts/access/Ownable.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)

pragma solidity ^0.8.20;

import {Context} from "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * The initial owner is set to the address provided by the deployer. This can
 * later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    /**
     * @dev The caller account is not authorized to perform an operation.
     */
    error OwnableUnauthorizedAccount(address account);

    /**
     * @dev The owner is not a valid owner account. (eg. `address(0)`)
     */
    error OwnableInvalidOwner(address owner);

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the address provided by the deployer as the initial owner.
     */
    constructor(address initialOwner) {
        if (initialOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(initialOwner);
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        if (owner() != _msgSender()) {
            revert OwnableUnauthorizedAccount(_msgSender());
        }
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        if (newOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

lib/openzeppelin-contracts/contracts/utils/Context.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)

pragma solidity ^0.8.20;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}

src/Agreement.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;

import { AgreementDetails, Chain, Account, Contact, BountyTerms } from "src/types/AgreementTypes.sol";
import { IChainValidator } from "src/interface/IChainValidator.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { _hashString } from "src/utils/Utils.sol";

/// @notice Contract that contains the AgreementDetails that will be deployed by the Agreement Factory.
/**
 * @dev
 * This contract is Ownable and mutable. It is intended to be used by entities adopting the
 * Safe Harbor agreement that either need to frequently update their terms, have too many terms to
 * fit in a single transaction, or wish to delegate the management of their agreement to a different
 * address than the deployer.
 */
// aderyn-ignore-next-line(centralization-risk)
contract Agreement is Ownable {
    // ----- ERRORS -----
    error Agreement__CannotSetBothAggregateBountyCapUsdAndRetainable();
    error Agreement__ChainNotFoundByCaip2Id(string caip2ChainId);
    error Agreement__AccountNotFoundByAddress(string caip2ChainId, string accountAddress);
    error Agreement__DuplicateChainId(string caip2ChainId);
    error Agreement__InvalidChainId(string caip2ChainId);
    error Agreement__ZeroAccountsForChainId(string caip2ChainId);
    error Agreement__ChainIdHasZeroLength();
    error Agreement__InvalidAssetRecoveryAddress(string caip2ChainId);
    error Agreement__ZeroAddress();
    error Agreement__BountyPercentageExceedsMaximum(uint256 bountyPercentage, uint256 maxPercentage);
    error Agreement__AggregateBountyCapLessThanBountyCap(uint256 aggregateBountyCapUSD, uint256 bountyCapUSD);
    error Agreement__InvalidAccountAddress(string caip2ChainId, uint256 accountIndex);
    error Agreement__InvalidContactDetails(uint256 contactIndex);
    error Agreement__CannotRemoveAllAccounts(string caip2ChainId);

    // ----- CONSTANTS -----
    /// @notice Maximum allowed bounty percentage (100%)
    uint256 public constant MAX_BOUNTY_PERCENTAGE = 100;

    // ----- STATE VARIABLES -----
    /// @notice Slot used for transient storage duplicate chain ID checking
    bytes32 private constant _DUPLICATE_CHECK_SLOT = keccak256("Agreement.duplicateChainIdCheck");

    /// @notice The Chain Validator contract for validating CAIP-2 chain IDs
    IChainValidator private immutable CHAIN_VALIDATOR;

    // Agreement details stored as separate variables to avoid requiring via-ir compiler
    string private protocolName;
    Contact[] private contactDetails;
    BountyTerms private bountyTerms;
    string private agreementURI;

    // Chain data stored separately to avoid nested struct issues
    string[] private chainIds;
    mapping(string caip2ChainId => string assetRecoveryAddress) private assetRecoveryAddresses;
    mapping(string caip2ChainId => Account[]) private accounts;

    // ----- EVENTS -----
    event ProtocolNameSet(string newName);
    event ContactDetailsSet(Contact[] newContactDetails);
    event ChainAdded(string caip2ChainId, string assetRecoveryAddress, Account[] accounts);
    event ChainSet(string caip2ChainId, string assetRecoveryAddress, Account[] accounts);
    event ChainRemoved(string caip2ChainId);
    event AccountAdded(string caip2ChainId, Account account);
    event AccountRemoved(string caip2ChainId, string accountAddress);
    event BountyTermsSet(BountyTerms newBountyTerms);

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

    /// @notice Constructor that sets the details of the agreement.
    /// @param _details The details of the agreement.
    /// @param _chainValidator The address of the Chain Validator contract
    /// @param _initialOwner The owner of the agreement
    constructor(
        AgreementDetails memory _details,
        address _chainValidator,
        address _initialOwner
    )
        Ownable(_initialOwner)
    {
        if (_chainValidator == address(0)) {
            revert Agreement__ZeroAddress();
        }
        CHAIN_VALIDATOR = IChainValidator(_chainValidator);
        _validateBountyTerms(_details.bountyTerms);
        _validateContactDetails(_details.contactDetails);
        _validateChains(_details.chains);
        _setDetails(_details);
    }

    // ----- USER-FACING STATE-CHANGING FUNCTIONS -----

    /// @notice Function that sets the protocol name
    // aderyn-ignore-next-line(centralization-risk)
    function setProtocolName(string calldata _protocolName) external onlyOwner {
        emit ProtocolNameSet(_protocolName);
        protocolName = _protocolName;
    }

    /// @notice Function that sets the agreement contact details.
    // aderyn-ignore-next-line(centralization-risk)
    function setContactDetails(Contact[] calldata _contactDetails) external onlyOwner {
        _validateContactDetails(_contactDetails);
        emit ContactDetailsSet(_contactDetails);
        delete contactDetails;
        // aderyn-ignore-next-line(costly-loop)
        for (uint256 i; i < _contactDetails.length; i++) {
            contactDetails.push(_contactDetails[i]);
        }
    }

    /// @notice Function that adds multiple chains to the agreement.
    // aderyn-ignore-next-line(centralization-risk)
    function addChains(Chain[] memory _chains) external onlyOwner {
        _validateChains(_chains);
        // aderyn-ignore-next-line(costly-loop)
        for (uint256 i; i < _chains.length; i++) {
            string memory chainId = _chains[i].caip2ChainId;
            if (_chainExists(chainId)) {
                revert Agreement__DuplicateChainId(chainId);
            }
            emit ChainAdded(chainId, _chains[i].assetRecoveryAddress, _chains[i].accounts);
            chainIds.push(chainId);
            assetRecoveryAddresses[chainId] = _chains[i].assetRecoveryAddress;
            // aderyn-ignore-next-line(costly-loop)
            for (uint256 j; j < _chains[i].accounts.length; j++) {
                accounts[chainId].push(_chains[i].accounts[j]);
            }
        }
    }

    /// @notice Adds or updates chains in the agreement
    // aderyn-ignore-next-line(centralization-risk)
    function addOrSetChains(Chain[] memory _chains) external onlyOwner {
        _validateChains(_chains);
        // aderyn-ignore-next-line(costly-loop)
        for (uint256 i; i < _chains.length; i++) {
            string memory chainId = _chains[i].caip2ChainId;
            bool exists = _chainExists(chainId);
            if (!exists) {
                chainIds.push(chainId);
                emit ChainAdded(chainId, _chains[i].assetRecoveryAddress, _chains[i].accounts);
            } else {
                emit ChainSet(chainId, _chains[i].assetRecoveryAddress, _chains[i].accounts);
            }
            assetRecoveryAddresses[chainId] = _chains[i].assetRecoveryAddress;
            delete accounts[chainId];
            // aderyn-ignore-next-line(costly-loop)
            for (uint256 j; j < _chains[i].accounts.length; j++) {
                accounts[chainId].push(_chains[i].accounts[j]);
            }
        }
    }

    /// @notice Function that sets multiple chains in the agreement, keeping existing chains.
    /// @dev This function replaces the existing chains with the new ones.
    // aderyn-ignore-next-line(centralization-risk)
    function setChains(Chain[] memory _chains) external onlyOwner {
        _validateChains(_chains);
        // aderyn-ignore-next-line(costly-loop)
        for (uint256 i; i < _chains.length; i++) {
            string memory chainId = _chains[i].caip2ChainId;
            if (!_chainExists(chainId)) {
                revert Agreement__ChainNotFoundByCaip2Id(chainId);
            }
            emit ChainSet(chainId, _chains[i].assetRecoveryAddress, _chains[i].accounts);
            assetRecoveryAddresses[chainId] = _chains[i].assetRecoveryAddress;
            delete accounts[chainId];
            // aderyn-ignore-next-line(costly-loop)
            for (uint256 j; j < _chains[i].accounts.length; j++) {
                accounts[chainId].push(_chains[i].accounts[j]);
            }
        }
    }

    /// @notice Removes multiple chains from the agreement by CAIP-2 IDs.
    /// @param _caip2ChainIds Array of CAIP-2 IDs of the chains to remove
    // aderyn-ignore-next-line(centralization-risk)
    function removeChains(string[] memory _caip2ChainIds) external onlyOwner {
        // aderyn-ignore-next-line(costly-loop)
        for (uint256 i; i < _caip2ChainIds.length; i++) {
            string memory chainId = _caip2ChainIds[i];
            uint256 idx = _findChainIndex(chainId);
            emit ChainRemoved(chainId);
            delete assetRecoveryAddresses[chainId];
            delete accounts[chainId];
            uint256 lastIdx = chainIds.length - 1;
            if (idx != lastIdx) {
                chainIds[idx] = chainIds[lastIdx];
            }
            chainIds.pop();
        }
    }

    /// @notice Function that adds multiple accounts to the agreement.
    /// @param _caip2ChainId The CAIP-2 ID of the chain
    /// @param _accounts Array of accounts to add
    // aderyn-ignore-next-line(centralization-risk)
    function addAccounts(string memory _caip2ChainId, Account[] calldata _accounts) external onlyOwner {
        if (!_chainExists(_caip2ChainId)) {
            revert Agreement__ChainNotFoundByCaip2Id(_caip2ChainId);
        }
        // Validate each account has a non-empty address
        for (uint256 i; i < _accounts.length; i++) {
            if (bytes(_accounts[i].accountAddress).length == 0) {
                revert Agreement__InvalidAccountAddress(_caip2ChainId, i);
            }
        }
        // aderyn-ignore-next-line(costly-loop)
        for (uint256 i; i < _accounts.length; i++) {
            emit AccountAdded(_caip2ChainId, _accounts[i]);
            accounts[_caip2ChainId].push(_accounts[i]);
        }
    }

    /// @notice Function that removes multiple accounts from the agreement by addresses.
    /// @param _caip2ChainId The CAIP-2 ID of the chain containing the accounts
    /// @param _accountAddresses Array of account addresses to remove
    // aderyn-ignore-next-line(centralization-risk)
    function removeAccounts(string memory _caip2ChainId, string[] memory _accountAddresses) external onlyOwner {
        if (!_chainExists(_caip2ChainId)) {
            revert Agreement__ChainNotFoundByCaip2Id(_caip2ChainId);
        }
        // Ensure at least one account remains after removal
        if (_accountAddresses.length >= accounts[_caip2ChainId].length) {
            revert Agreement__CannotRemoveAllAccounts(_caip2ChainId);
        }
        // aderyn-ignore-next-line(costly-loop)
        for (uint256 i; i < _accountAddresses.length; i++) {
            uint256 accountIndex = _findAccountIndex(_caip2ChainId, _accountAddresses[i]);
            emit AccountRemoved(_caip2ChainId, _accountAddresses[i]);

            uint256 lastAccountId = accounts[_caip2ChainId].length - 1;
            accounts[_caip2ChainId][accountIndex] = accounts[_caip2ChainId][lastAccountId];
            accounts[_caip2ChainId].pop();
        }
    }

    /// @notice Function that sets the bounty terms of the agreement.
    // aderyn-ignore-next-line(centralization-risk)
    function setBountyTerms(BountyTerms memory _bountyTerms) external onlyOwner {
        _validateBountyTerms(_bountyTerms);
        emit BountyTermsSet(_bountyTerms);
        bountyTerms = _bountyTerms;
    }

    // ----- INTERNAL STATE-CHANGING FUNCTIONS -----

    /// @notice Internal function to set all agreement details
    function _setDetails(AgreementDetails memory _details) internal {
        protocolName = _details.protocolName;
        agreementURI = _details.agreementURI;
        bountyTerms = _details.bountyTerms;

        // Copy contact details
        delete contactDetails;
        for (uint256 i; i < _details.contactDetails.length; ++i) {
            contactDetails.push(_details.contactDetails[i]);
        }

        // Copy chains
        delete chainIds;
        for (uint256 i; i < _details.chains.length; ++i) {
            string memory chainId = _details.chains[i].caip2ChainId;
            chainIds.push(chainId);
            assetRecoveryAddresses[chainId] = _details.chains[i].assetRecoveryAddress;

            delete accounts[chainId];
            for (uint256 j; j < _details.chains[i].accounts.length; ++j) {
                accounts[chainId].push(_details.chains[i].accounts[j]);
            }
        }
    }

    /// @notice Internal function to validate chains (IDs, duplicates, accounts, recovery addresses)
    /// @dev Uses transient storage (tstore/tload) for duplicate checking
    function _validateChains(Chain[] memory _chains) internal {
        // Validate all chain data and check for duplicates in a single pass
        for (uint256 i; i < _chains.length; i++) {
            // Validate chain ID
            if (bytes(_chains[i].caip2ChainId).length == 0) {
                revert Agreement__ChainIdHasZeroLength();
            }
            if (!CHAIN_VALIDATOR.isChainValid(_chains[i].caip2ChainId)) {
                revert Agreement__InvalidChainId(_chains[i].caip2ChainId);
            }
            // Validate accounts
            if (_chains[i].accounts.length == 0) {
                revert Agreement__ZeroAccountsForChainId(_chains[i].caip2ChainId);
            }
            // Validate each account has a non-empty address
            for (uint256 j; j < _chains[i].accounts.length; j++) {
                if (bytes(_chains[i].accounts[j].accountAddress).length == 0) {
                    revert Agreement__InvalidAccountAddress(_chains[i].caip2ChainId, j);
                }
            }
            // Validate asset recovery address
            if (bytes(_chains[i].assetRecoveryAddress).length == 0) {
                revert Agreement__InvalidAssetRecoveryAddress(_chains[i].caip2ChainId);
            }
            // Check for duplicates using transient storage
            bytes32 slot = _duplicateCheckSlot(_chains[i].caip2ChainId);
            bool seen;
            assembly {
                seen := tload(slot)
            }
            if (seen) {
                revert Agreement__DuplicateChainId(_chains[i].caip2ChainId);
            }
            assembly {
                tstore(slot, 1)
            }
        }

        // Clear the transient storage in case this is within a batched transaction
        for (uint256 i; i < _chains.length; i++) {
            bytes32 slot = _duplicateCheckSlot(_chains[i].caip2ChainId);
            assembly {
                tstore(slot, 0)
            }
        }
    }

    // ----- USER-FACING READ-ONLY FUNCTIONS -----

    /// @notice Function that returns the agreement details
    function getDetails() external view returns (AgreementDetails memory _details) {
        _details.protocolName = protocolName;
        _details.agreementURI = agreementURI;
        _details.bountyTerms = bountyTerms;

        // Copy contact details
        uint256 contactsLength = contactDetails.length;
        _details.contactDetails = new Contact[](contactsLength);
        for (uint256 i; i < contactsLength; ++i) {
            _details.contactDetails[i] = contactDetails[i];
        }

        // Reconstruct chains
        uint256 chainsLength = chainIds.length;
        _details.chains = new Chain[](chainsLength);
        for (uint256 i; i < chainsLength; ++i) {
            string memory chainId = chainIds[i];
            _details.chains[i].caip2ChainId = chainId;
            _details.chains[i].assetRecoveryAddress = assetRecoveryAddresses[chainId];

            Account[] storage accts = accounts[chainId];
            uint256 acctsLength = accts.length;
            _details.chains[i].accounts = new Account[](acctsLength);
            for (uint256 j; j < acctsLength; ++j) {
                _details.chains[i].accounts[j] = accts[j];
            }
        }
    }

    /// @notice Returns the protocol name
    function getProtocolName() external view returns (string memory) {
        return protocolName;
    }

    /// @notice Returns the bounty terms
    function getBountyTerms() external view returns (BountyTerms memory) {
        return bountyTerms;
    }

    /// @notice Returns the agreement URI
    function getAgreementURI() external view returns (string memory) {
        return agreementURI;
    }

    /// @notice Returns the chain validator address
    function getChainValidator() external view returns (address) {
        return address(CHAIN_VALIDATOR);
    }

    /// @notice Returns all chain IDs
    function getChainIds() external view returns (string[] memory) {
        return chainIds;
    }

    // ----- INTERNAL READ-ONLY FUNCTIONS -----

    /// @notice Internal function to validate bounty terms
    /// @dev Validates:
    ///      1. bountyPercentage <= MAX_BOUNTY_PERCENTAGE (100%)
    ///      2. aggregateBountyCapUSD >= bountyCapUSD when aggregateBountyCapUSD is set
    ///      3. Cannot set both aggregateBountyCapUSD and retainable
    function _validateBountyTerms(BountyTerms memory _bountyTerms) internal pure {
        // Validate bounty percentage does not exceed maximum (100%)
        if (_bountyTerms.bountyPercentage > MAX_BOUNTY_PERCENTAGE) {
            revert Agreement__BountyPercentageExceedsMaximum(_bountyTerms.bountyPercentage, MAX_BOUNTY_PERCENTAGE);
        }

        // Validate aggregate cap is >= individual cap when aggregate cap is set
        // Note: aggregateBountyCapUSD == 0 means no aggregate cap applies
        if (_bountyTerms.aggregateBountyCapUSD > 0 && _bountyTerms.aggregateBountyCapUSD < _bountyTerms.bountyCapUSD) {
            revert Agreement__AggregateBountyCapLessThanBountyCap(
                _bountyTerms.aggregateBountyCapUSD, _bountyTerms.bountyCapUSD
            );
        }

        // Cannot set both aggregate bounty cap and retainable
        if (_bountyTerms.aggregateBountyCapUSD > 0 && _bountyTerms.retainable) {
            revert Agreement__CannotSetBothAggregateBountyCapUsdAndRetainable();
        }
    }

    /// @notice Internal function to validate contact details
    /// @dev Validates that each contact has non-empty name and contact fields
    function _validateContactDetails(Contact[] memory _contactDetails) internal pure {
        for (uint256 i; i < _contactDetails.length; i++) {
            if (bytes(_contactDetails[i].name).length == 0 || bytes(_contactDetails[i].contact).length == 0) {
                revert Agreement__InvalidContactDetails(i);
            }
        }
    }

    /// @notice Checks if a chain exists
    function _chainExists(string memory _caip2ChainId) internal view returns (bool) {
        bytes32 targetHash = _hashString(_caip2ChainId);
        uint256 length = chainIds.length;
        for (uint256 i; i < length; ++i) {
            // aderyn-ignore-next-line(storage-array-memory-edit)
            if (_hashString(chainIds[i]) == targetHash) {
                return true;
            }
        }
        return false;
    }

    /// @notice Internal function to find chain index by CAIP-2 ID
    function _findChainIndex(string memory _caip2ChainId) internal view returns (uint256 chainIndex) {
        bytes32 targetHash = _hashString(_caip2ChainId);
        uint256 length = chainIds.length;
        for (uint256 i; i < length; i++) {
            // aderyn-ignore-next-line(storage-array-memory-edit)
            if (_hashString(chainIds[i]) == targetHash) {
                return i;
            }
        }
        revert Agreement__ChainNotFoundByCaip2Id(_caip2ChainId);
    }

    /// @notice Internal function to find account index by address within a chain
    function _findAccountIndex(
        string memory _caip2ChainId,
        string memory _accountAddress
    )
        internal
        view
        returns (uint256 accountIndex)
    {
        bytes32 targetHash = _hashString(_accountAddress);
        Account[] storage chainAccounts = accounts[_caip2ChainId];
        uint256 chainAccountsLength = chainAccounts.length;
        for (uint256 i; i < chainAccountsLength; i++) {
            // aderyn-ignore-next-line(storage-array-memory-edit)
            if (_hashString(chainAccounts[i].accountAddress) == targetHash) {
                return i;
            }
        }
        revert Agreement__AccountNotFoundByAddress(_caip2ChainId, _accountAddress);
    }

    /// @notice Computes the transient storage slot for duplicate chain ID checking
    function _duplicateCheckSlot(string memory _chainId) internal pure returns (bytes32 result) {
        bytes32 slot = _DUPLICATE_CHECK_SLOT;
        bytes32 chainIdHash = _hashString(_chainId);
        assembly {
            mstore(0x00, slot)
            mstore(0x20, chainIdHash)
            result := keccak256(0x00, 0x40)
        }
    }
}

src/interface/IChainValidator.sol

// SPDX-License-Identifier: MIT
// aderyn-ignore-next-line(unspecific-solidity-pragma,push-zero-opcode)
pragma solidity ^0.8.24;

interface IChainValidator {
    /// @notice Check if a chain ID is valid.
    /// @param caip2ChainId The CAIP-2 ID of the chain to check.
    /// @return bool True if the chain is valid, false otherwise.
    function isChainValid(string calldata caip2ChainId) external view returns (bool);

    /// @notice Get all valid chain IDs.
    /// @return string[] Array of all valid CAIP-2 chain IDs.
    function getValidChains() external view returns (string[] memory);
}

src/types/AgreementTypes.sol

// SPDX-License-Identifier: MIT
// aderyn-ignore-next-line(unspecific-solidity-pragma,push-zero-opcode)
pragma solidity ^0.8.24;

/// @notice Struct that contains the details of the agreement.
struct AgreementDetails {
    // The name of the protocol adopting the agreement.
    string protocolName;
    // The contact details (required for pre-notifying).
    Contact[] contactDetails;
    // The scope and recovery address by chain.
    Chain[] chains;
    // The terms of the agreement.
    BountyTerms bountyTerms;
    // IPFS hash or other URI of the actual agreement document, which confirms all terms.
    string agreementURI;
}

/// @notice Struct that contains the contact details of the agreement.
struct Contact {
    string name;
    // This person's contact details (email, phone, telegram handle, etc.)
    string contact;
}

/// @notice Struct that contains the details of an agreement by chain.
struct Chain {
    // The address to which recovered assets will be sent.
    string assetRecoveryAddress;
    // The accounts in scope for the agreement.
    Account[] accounts;
    // The CAIP-2 chain ID. Please refer to the CAIP-2 standard for more details.
    // https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md
    string caip2ChainId;
}

/// @notice Struct that contains the details of an account in an agreement.
struct Account {
    // The address of the account (EOA or smart contract).
    string accountAddress;
    // The scope of child contracts included in the agreement.
    ChildContractScope childContractScope;
}

/// @notice Enum that defines the inclusion of child contracts in an agreement.
enum ChildContractScope {
    // No child contracts are included.
    None,
    // Only child contracts that were created before the time of this agreement are included.
    ExistingOnly,
    // All child contracts, both existing and new, are included.
    All,
    // Only child contracts that were created after the time of this agreement are included.
    FutureOnly
}

/// @notice Struct that contains the terms of the bounty for the agreement.
struct BountyTerms {
    // Percentage of the recovered funds a Whitehat receives as their bounty (0-100).
    uint256 bountyPercentage;
    // The maximum bounty in USD.
    uint256 bountyCapUSD;
    // Whether the whitehat can retain their bounty or must return all funds to
    // the asset recovery address.
    bool retainable;
    // The identity verification requirements on the whitehat.
    IdentityRequirements identity;
    // The diligence requirements placed on eligible whitehats. Only applicable for Named whitehats.
    string diligenceRequirements;
    // Optional. Caps the total USD value of bounties paid across all whitehats for a single exploit.
    // If set to 0, no aggregate cap applies and each whitehat may receive up to bountyCapUSD individually.
    uint256 aggregateBountyCapUSD;
}

/// @notice Whitehat identity verification requirements.
enum IdentityRequirements {
    // The whitehat will be subject to no KYC requirements.
    Anonymous,
    // The whitehat must provide a pseudonym.
    Pseudonymous,
    // The whitehat must confirm their legal name.
    Named
}

src/utils/Utils.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;

/// @notice Hashes a string using inline assembly for gas efficiency
function _hashString(string memory str) pure returns (bytes32 result) {
    assembly {
        result := keccak256(add(str, 0x20), mload(str))
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 1000,
    "enabled": true
  },
  "evmVersion": "prague",
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
    "createx/=lib/createx/src/",
    "ds-test/=lib/createx/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-std/=lib/forge-std/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "openzeppelin/=lib/createx/lib/openzeppelin-contracts/contracts/",
    "solady/=lib/createx/lib/solady/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.