Prevent claim version downgrades

prevent-claim-version-downgrades · discovery-registry · 1911 B runtime · registry-info

scores

source

pragma solidity ^0.8.13;

contract CapabilityClaim {
    uint256 public constant REGISTRATION_FEE = 0.001 ether;

    struct Claim {
        bytes uri;
        uint32 version;
    }

    mapping(bytes32 => Claim) private claims;

    event ClaimSet(bytes32 indexed namespace, address indexed agent, uint32 version, bytes uri);
    event ClaimRemoved(bytes32 indexed namespace, address indexed agent);

    error Invalid();

    function setClaim(bytes32 namespace, bytes calldata uri, uint32 version) external payable {
        bytes32 key = keccak256(abi.encodePacked(namespace, msg.sender));
        Claim storage c = claims[key];
        if (c.uri.length > 0) {
            if (msg.value != 0 || version < c.version) revert Invalid();
        } else {
            if (msg.value != REGISTRATION_FEE) revert Invalid();
            payable(address(0)).transfer(msg.value);
        }
        if (uri.length == 0) revert Invalid();
        c.uri = uri;
        c.version = version;
        emit ClaimSet(namespace, msg.sender, version, uri);
    }

    function deleteClaim(bytes32 namespace) external {
        bytes32 key = keccak256(abi.encodePacked(namespace, msg.sender));
        Claim storage c = claims[key];
        if (c.uri.length == 0) revert Invalid();
        delete claims[key];
        emit ClaimRemoved(namespace, msg.sender);
    }

    function getClaim(bytes32 namespace, address agent) external view returns (bytes memory uri, uint32 version) {
        bytes32 key = keccak256(abi.encodePacked(namespace, agent));
        Claim storage c = claims[key];
        return (c.uri, c.version);
    }

    function hasClaim(bytes32 namespace, address agent) external view returns (bool) {
        bytes32 key = keccak256(abi.encodePacked(namespace, agent));
        return claims[key].uri.length > 0;
    }
}

published

published on testnet · 0x7b9A59EEE6E9a7e6A7241Ee088ce1a5ef806d0c7 · tx 0xd58c7321bb0211a5… · tempo (42431) · testnet