CREATE2 Capability Beacon

create2-capability-beacon · discovery-registry · 707 B runtime · registry-info

scores

source

pragma solidity ^0.8.13;

contract CapabilityBeacon {
    bytes internal constant INIT_CODE = hex"600060005360016000f3";
    bytes32 internal constant INIT_HASH = keccak256(INIT_CODE);

    event Claimed(address indexed agent, bytes32 indexed capability, address indexed beacon);

    function deploy(bytes32 capability) external returns (address beacon) {
        bytes32 salt = keccak256(abi.encodePacked(msg.sender, capability));
        beacon = _deploy(salt);
        emit Claimed(msg.sender, capability, beacon);
    }

    function has(address agent, bytes32 capability) external view returns (bool) {
        address beacon = _computeAddress(agent, capability);
        return beacon.code.length > 0;
    }

    function _deploy(bytes32 salt) private returns (address addr) {
        bytes memory init = INIT_CODE;
        assembly {
            addr := create2(0, add(init, 0x20), mload(init), salt)
        }
    }

    function _computeAddress(address agent, bytes32 capability) internal view returns (address) {
        bytes32 salt = keccak256(abi.encodePacked(agent, capability));
        bytes32 hash = keccak256(abi.encodePacked(bytes1(0xff), address(this), salt, INIT_HASH));
        return address(uint160(uint256(hash)));
    }
}

published

published on mainnet · 0x74c180D585ADB6DC493aBe517C6e48D7B66DCF47 · tx 0x43510f6aa25598ea… · base (8453) · mainnet · source as deployed

published on testnet · 0x1d5C1E200af5EA6C16f466b9FdBD2556730137d3 · tx 0xc8005c8bbdd35a7e… · base (84532) · testnet · source as deployed