Capability Code-Anchor Registry

capability-code-anchor-registry · discovery-registry · 1123 B runtime · registry-info

scores

source

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

contract CapabilityCodeAnchorRegistry {
    mapping(address => uint128) private meta; // low 64: used slot bits; high 64: count
    mapping(address => mapping(bytes4 => uint8)) private slotOf;
    mapping(address => mapping(uint8 => bytes32)) private hashOf;

    function register(bytes4 selector) external {
        require(msg.sender.code.length > 0, 'no code');
        require(slotOf[msg.sender][selector] == 0, 'exists');
        uint128 m = meta[msg.sender];
        uint64 count = uint64(m >> 64);
        require(count < 64, 'limit');
        uint8 slot = uint8(count) + 1;
        slotOf[msg.sender][selector] = slot;
        hashOf[msg.sender][slot] = msg.sender.codehash;
        meta[msg.sender] = (uint128(uint64(m) | (uint64(1) << count))) | (uint128(count + 1) << 64);
    }

    function verify(address agent, bytes4 selector) external view returns (bool) {
        uint8 slot = slotOf[agent][selector];
        if (slot == 0) return false;
        return hashOf[agent][slot] == agent.codehash;
    }

    function getCodeHash(address agent, bytes4 selector) external view returns (bytes32) {
        uint8 slot = slotOf[agent][selector];
        return slot == 0 ? bytes32(0) : hashOf[agent][slot];
    }
}

published

published on testnet · 0xCf85f3Ad69eF421359dED0a3fd30D24FE2d299Db · tx 0xe8c14b7caf16c3d3… · tempo (42431) · testnet