Skip to content
Verified contractPartialMIT

ENSOwnership

Address0x56e1f8f8f1bb507a464e8e9e706f2693d48f8348
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.37

 

Same code

1 more contract

 

Details

Compiler
solc 0.8.37+commit.f401782d
Language
solidity
Optimization
Yes, 40 runs
EVM version
shanghai
Via IR
yes
License
MIT
Match
Partialcreation code: no, runtime code: yes
Verified on Sourcify
Created in block
26,213,989
Creation transaction
0xe0415ef1b9…3c45ff00bf

project/contracts/utils/ENSOwnership.sol

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

import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
import "./EnsLabelUtils.sol";

library ENSOwnership {
    // Legacy note: keep this library limited to deterministic ownership checks used by
    // AGIJobManager routing (`verifyENSOwnership` + `verifyMerkleOwnership`).
    // Keep a fixed staticcall cap: enough for normal ENS stack reads while bounding griefing surface.
    uint256 private constant ENS_STATICCALL_GAS_LIMIT = 80_000;
    bytes4 private constant OWNER_OF_SELECTOR = 0x6352211e;
    bytes4 private constant OWNER_SELECTOR = bytes4(keccak256("owner(bytes32)"));
    bytes4 private constant GET_APPROVED_SELECTOR = 0x081812fc;
    bytes4 private constant IS_APPROVED_FOR_ALL_SELECTOR = 0xe985e9c5;
    bytes4 private constant RESOLVER_SELECTOR = 0x0178b8bf;
    bytes4 private constant ADDR_SELECTOR = 0x3b3b57de;

    function _verifyByRoot(
        address ensAddress,
        address nameWrapperAddress,
        address claimant,
        string memory subdomain,
        bytes32 rootNode
    ) private view returns (bool) {
        if (rootNode == bytes32(0)) return false;
        bytes32 subnode = keccak256(abi.encodePacked(rootNode, keccak256(bytes(subdomain))));
        if (_verifyNameWrapperOwnership(ensAddress, nameWrapperAddress, claimant, subnode)) {
            return true;
        }
        return _verifyResolverOwnership(ensAddress, nameWrapperAddress, claimant, subnode);
    }
    function verifyENSOwnership(
        address ensAddress,
        address nameWrapperAddress,
        address claimant,
        string memory subdomain,
        bytes32 rootNode
    ) external view returns (bool) {
        EnsLabelUtils.requireValidLabel(subdomain);
        if (rootNode == bytes32(0)) return false;
        bytes32 subnode = keccak256(abi.encodePacked(rootNode, keccak256(bytes(subdomain))));
        if (_verifyNameWrapperOwnership(ensAddress, nameWrapperAddress, claimant, subnode)) {
            return true;
        }
        return _verifyResolverOwnership(ensAddress, nameWrapperAddress, claimant, subnode);
    }

    function verifyENSOwnership(
        address ensAddress,
        address nameWrapperAddress,
        address claimant,
        string memory subdomain,
        bytes32 rootNode,
        bytes32 alphaRootNode
    ) external view returns (bool) {
        EnsLabelUtils.requireValidLabel(subdomain);
        return _verifyByRoot(ensAddress, nameWrapperAddress, claimant, subdomain, rootNode)
            || _verifyByRoot(ensAddress, nameWrapperAddress, claimant, subdomain, alphaRootNode);
    }

    function verifyMerkleOwnership(address claimant, bytes32[] calldata proof, bytes32 merkleRoot)
        external
        pure
        returns (bool)
    {
        return MerkleProof.verifyCalldata(proof, merkleRoot, keccak256(abi.encodePacked(claimant)));
    }

    /// @notice Stable ENS credential and its controller for per-job voting uniqueness.
    function validatorCredential(
        address ensAddress, address wrapper, address claimant, string memory label,
        bytes32 primaryRoot, bytes32 secondaryRoot
    ) external view returns (bytes32 node, address controller) {
        EnsLabelUtils.requireValidLabel(label);
        (node, controller) = _credential(ensAddress, wrapper, claimant, label, primaryRoot);
        if (node == bytes32(0)) return _credential(ensAddress, wrapper, claimant, label, secondaryRoot);
    }

    function _credential(
        address ensAddress, address wrapper, address claimant, string memory label, bytes32 root
    ) private view returns (bytes32 node, address controller) {
        if (root == bytes32(0)) return (bytes32(0), address(0));
        node = keccak256(abi.encodePacked(root, keccak256(bytes(label))));
        if (_verifyNameWrapperOwnership(ensAddress, wrapper, claimant, node)) {
            (, controller) = _staticcallAddress(wrapper, abi.encodeWithSelector(OWNER_OF_SELECTOR, uint256(node)));
        } else {
            controller = _resolverController(ensAddress, wrapper, node);
            if (controller == address(0) || !_resolverMatches(ensAddress, claimant, node)) {
                return (bytes32(0), address(0));
            }
        }
        if (controller == address(0)) return (bytes32(0), address(0));
    }

    function _verifyNameWrapperOwnership(
        address ensAddress,
        address nameWrapperAddress,
        address claimant,
        bytes32 subnode
    ) private view returns (bool) {
        if (nameWrapperAddress == address(0)) return false;
        // An unwrapped parent can revoke/reassign the registry node without clearing wrapper data.
        (bool registryOk, address registryOwner) = _staticcallAddress(ensAddress, abi.encodeWithSelector(OWNER_SELECTOR, subnode));
        if (!registryOk || registryOwner != nameWrapperAddress) return false;
        (bool ok, address owner) = _staticcallAddress(
            nameWrapperAddress,
            abi.encodeWithSelector(OWNER_OF_SELECTOR, uint256(subnode))
        );
        if (!ok || owner == address(0)) return false;
        if (owner == claimant) return true;

        address approved;
        (ok, approved) = _staticcallAddress(
            nameWrapperAddress,
            abi.encodeWithSelector(GET_APPROVED_SELECTOR, uint256(subnode))
        );
        if (ok && approved == claimant) return true;

        bool approvedForAll;
        (ok, approvedForAll) = _staticcallBool(
            nameWrapperAddress,
            abi.encodeWithSelector(IS_APPROVED_FOR_ALL_SELECTOR, owner, claimant)
        );
        return ok && approvedForAll;
    }

    function _verifyResolverOwnership(
        address ensAddress,
        address nameWrapperAddress,
        address claimant,
        bytes32 subnode
    ) private view returns (bool) {
        return _resolverController(ensAddress, nameWrapperAddress, subnode) != address(0)
            && _resolverMatches(ensAddress, claimant, subnode);
    }

    /// @dev ENS retains resolver records when a name is revoked or its wrapped owner expires.
    /// A stale address record is never a membership credential without a live controller.
    function _resolverController(address ensAddress, address wrapper, bytes32 node)
        private view returns (address controller)
    {
        (bool ok, address registryOwner) = _staticcallAddress(ensAddress, abi.encodeWithSelector(OWNER_SELECTOR, node));
        if (!ok || registryOwner == address(0)) return address(0);
        if (registryOwner != wrapper) return registryOwner;
        (ok, controller) = _staticcallAddress(wrapper, abi.encodeWithSelector(OWNER_OF_SELECTOR, uint256(node)));
        if (!ok) return address(0);
    }

    function _resolverMatches(address ensAddress, address claimant, bytes32 subnode) private view returns (bool) {
        if (ensAddress == address(0)) return false;
        (bool ok, address resolverAddress) = _staticcallAddress(
            ensAddress,
            abi.encodeWithSelector(RESOLVER_SELECTOR, subnode)
        );
        if (!ok || resolverAddress == address(0)) return false;
        address resolvedAddress;
        (ok, resolvedAddress) = _staticcallAddress(
            resolverAddress,
            abi.encodeWithSelector(ADDR_SELECTOR, subnode)
        );
        return ok && resolvedAddress == claimant;
    }

    function _staticcallAddress(address target, bytes memory payload) private view returns (bool ok, address result) {
        uint256 decoded;
        (ok, decoded) = _staticcallWord(target, payload);
        if (!ok || decoded > type(uint160).max) return (false, address(0));
        // The preceding bound rejects malformed ABI address words instead of truncating them.
        // forge-lint: disable-next-line(unsafe-typecast)
        result = address(uint160(decoded));
    }

    function _staticcallBool(address target, bytes memory payload) private view returns (bool ok, bool result) {
        uint256 decoded;
        (ok, decoded) = _staticcallWord(target, payload);
        if (!ok) return (false, false);
        if (decoded > 1) return (false, false);
        result = decoded == 1;
    }

    function _staticcallWord(address target, bytes memory payload) private view returns (bool ok, uint256 word) {
        assembly {
            ok := staticcall(ENS_STATICCALL_GAS_LIMIT, target, add(payload, 0x20), mload(payload), 0x00, 0x20)
            if lt(returndatasize(), 0x20) {
                ok := 0
            }
            if ok {
                returndatacopy(0x00, 0x00, 0x20)
                word := mload(0x00)
            }
        }
    }
}

npm/@openzeppelin/[email protected]/utils/cryptography/MerkleProof.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.2) (utils/cryptography/MerkleProof.sol)

pragma solidity ^0.8.0;

/**
 * @dev These functions deal with verification of Merkle Tree proofs.
 *
 * The tree and the proofs can be generated using our
 * https://github.com/OpenZeppelin/merkle-tree[JavaScript library].
 * You will find a quickstart guide in the readme.
 *
 * WARNING: You should avoid using leaf values that are 64 bytes long prior to
 * hashing, or use a hash function other than keccak256 for hashing leaves.
 * This is because the concatenation of a sorted pair of internal nodes in
 * the merkle tree could be reinterpreted as a leaf value.
 * OpenZeppelin's JavaScript library generates merkle trees that are safe
 * against this attack out of the box.
 */
library MerkleProof {
    /**
     * @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
     * defined by `root`. For this, a `proof` must be provided, containing
     * sibling hashes on the branch from the leaf to the root of the tree. Each
     * pair of leaves and each pair of pre-images are assumed to be sorted.
     */
    function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
        return processProof(proof, leaf) == root;
    }

    /**
     * @dev Calldata version of {verify}
     *
     * _Available since v4.7._
     */
    function verifyCalldata(bytes32[] calldata proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
        return processProofCalldata(proof, leaf) == root;
    }

    /**
     * @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
     * from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
     * hash matches the root of the tree. When processing the proof, the pairs
     * of leafs & pre-images are assumed to be sorted.
     *
     * _Available since v4.4._
     */
    function processProof(bytes32[] memory proof, bytes32 leaf) internal pure returns (bytes32) {
        bytes32 computedHash = leaf;
        for (uint256 i = 0; i < proof.length; i++) {
            computedHash = _hashPair(computedHash, proof[i]);
        }
        return computedHash;
    }

    /**
     * @dev Calldata version of {processProof}
     *
     * _Available since v4.7._
     */
    function processProofCalldata(bytes32[] calldata proof, bytes32 leaf) internal pure returns (bytes32) {
        bytes32 computedHash = leaf;
        for (uint256 i = 0; i < proof.length; i++) {
            computedHash = _hashPair(computedHash, proof[i]);
        }
        return computedHash;
    }

    /**
     * @dev Returns true if the `leaves` can be simultaneously proven to be a part of a merkle tree defined by
     * `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
     *
     * CAUTION: Not all merkle trees admit multiproofs. See {processMultiProof} for details.
     *
     * _Available since v4.7._
     */
    function multiProofVerify(
        bytes32[] memory proof,
        bool[] memory proofFlags,
        bytes32 root,
        bytes32[] memory leaves
    ) internal pure returns (bool) {
        return processMultiProof(proof, proofFlags, leaves) == root;
    }

    /**
     * @dev Calldata version of {multiProofVerify}
     *
     * CAUTION: Not all merkle trees admit multiproofs. See {processMultiProof} for details.
     *
     * _Available since v4.7._
     */
    function multiProofVerifyCalldata(
        bytes32[] calldata proof,
        bool[] calldata proofFlags,
        bytes32 root,
        bytes32[] memory leaves
    ) internal pure returns (bool) {
        return processMultiProofCalldata(proof, proofFlags, leaves) == root;
    }

    /**
     * @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
     * proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
     * leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
     * respectively.
     *
     * CAUTION: Not all merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
     * is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
     * tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
     *
     * _Available since v4.7._
     */
    function processMultiProof(
        bytes32[] memory proof,
        bool[] memory proofFlags,
        bytes32[] memory leaves
    ) internal pure returns (bytes32 merkleRoot) {
        // This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
        // consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
        // `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
        // the merkle tree.
        uint256 leavesLen = leaves.length;
        uint256 proofLen = proof.length;
        uint256 totalHashes = proofFlags.length;

        // Check proof validity.
        require(leavesLen + proofLen - 1 == totalHashes, "MerkleProof: invalid multiproof");

        // The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
        // `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
        bytes32[] memory hashes = new bytes32[](totalHashes);
        uint256 leafPos = 0;
        uint256 hashPos = 0;
        uint256 proofPos = 0;
        // At each step, we compute the next hash using two values:
        // - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
        //   get the next hash.
        // - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
        //   `proof` array.
        for (uint256 i = 0; i < totalHashes; i++) {
            bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
            bytes32 b = proofFlags[i]
                ? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
                : proof[proofPos++];
            hashes[i] = _hashPair(a, b);
        }

        if (totalHashes > 0) {
            require(proofPos == proofLen, "MerkleProof: invalid multiproof");
            unchecked {
                return hashes[totalHashes - 1];
            }
        } else if (leavesLen > 0) {
            return leaves[0];
        } else {
            return proof[0];
        }
    }

    /**
     * @dev Calldata version of {processMultiProof}.
     *
     * CAUTION: Not all merkle trees admit multiproofs. See {processMultiProof} for details.
     *
     * _Available since v4.7._
     */
    function processMultiProofCalldata(
        bytes32[] calldata proof,
        bool[] calldata proofFlags,
        bytes32[] memory leaves
    ) internal pure returns (bytes32 merkleRoot) {
        // This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
        // consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
        // `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
        // the merkle tree.
        uint256 leavesLen = leaves.length;
        uint256 proofLen = proof.length;
        uint256 totalHashes = proofFlags.length;

        // Check proof validity.
        require(leavesLen + proofLen - 1 == totalHashes, "MerkleProof: invalid multiproof");

        // The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
        // `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
        bytes32[] memory hashes = new bytes32[](totalHashes);
        uint256 leafPos = 0;
        uint256 hashPos = 0;
        uint256 proofPos = 0;
        // At each step, we compute the next hash using two values:
        // - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
        //   get the next hash.
        // - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
        //   `proof` array.
        for (uint256 i = 0; i < totalHashes; i++) {
            bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
            bytes32 b = proofFlags[i]
                ? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
                : proof[proofPos++];
            hashes[i] = _hashPair(a, b);
        }

        if (totalHashes > 0) {
            require(proofPos == proofLen, "MerkleProof: invalid multiproof");
            unchecked {
                return hashes[totalHashes - 1];
            }
        } else if (leavesLen > 0) {
            return leaves[0];
        } else {
            return proof[0];
        }
    }

    function _hashPair(bytes32 a, bytes32 b) private pure returns (bytes32) {
        return a < b ? _efficientHash(a, b) : _efficientHash(b, a);
    }

    function _efficientHash(bytes32 a, bytes32 b) private pure returns (bytes32 value) {
        assembly ("memory-safe") {
            mstore(0x00, a)
            mstore(0x20, b)
            value := keccak256(0x00, 0x40)
        }
    }
}

project/contracts/utils/EnsLabelUtils.sol

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

library EnsLabelUtils {
    error InvalidENSLabel();

    bytes1 private constant DASH = 0x2d;
    bytes1 private constant DOT = 0x2e;
    bytes1 private constant ZERO = 0x30;
    bytes1 private constant NINE = 0x39;
    bytes1 private constant LOWER_A = 0x61;
    bytes1 private constant LOWER_Z = 0x7a;

    /// @dev Strict ASCII label policy for predictable ENS auth routing:
    /// 1..63 chars, [a-z0-9-], no dots, no leading/trailing dash.
    function requireValidLabel(string memory label) internal pure {
        bytes memory b = bytes(label);
        uint256 len = b.length;
        if (len == 0 || len > 63) revert InvalidENSLabel();
        if (b[0] == DASH || b[len - 1] == DASH) revert InvalidENSLabel();

        for (uint256 i = 0; i < len;) {
            bytes1 c = b[i];
            if (c == DOT) revert InvalidENSLabel();

            bool isDigit = c >= ZERO && c <= NINE;
            bool isLower = c >= LOWER_A && c <= LOWER_Z;
            if (!(isDigit || isLower || c == DASH)) revert InvalidENSLabel();

            unchecked {
                ++i;
            }
        }
    }
}
Compiler settingsShow
{
  "debug": {
    "revertStrings": "strip"
  },
  "viaIR": true,
  "metadata": {
    "bytecodeHash": "none"
  },
  "optimizer": {
    "runs": 40,
    "enabled": true
  },
  "evmVersion": "shanghai",
  "remappings": [
    "project/:@openzeppelin/contracts/=npm/@openzeppelin/[email protected]/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.