ManifestAnchor

manifestanchor · discovery-registry · 972 B runtime · funds-movement

scores

source

pragma solidity ^0.8.13;

interface IERC1271 {
    function isValidSignature(bytes32 hash, bytes calldata signature) external view returns (bytes4);
}

contract ManifestAnchor {
    event ManifestUpdated(address indexed agent, bytes32 manifestHash, uint64 timestamp);

    mapping(address => bytes32) public manifest;
    mapping(address => uint64) public updatedAt;

    bytes4 private constant MAGIC_VALUE = 0x1626ba7e;

    function register(address agent, bytes32 manifestHash, bytes calldata signature) external {
        _set(agent, manifestHash, signature);
    }

    function update(address agent, bytes32 newHash, bytes calldata signature) external {
        _set(agent, newHash, signature);
    }

    function _set(address agent, bytes32 manifestHash, bytes calldata signature) internal {
        bytes32 challenge = keccak256(abi.encodePacked(manifestHash, address(this), manifest[agent]));
        require(IERC1271(agent).isValidSignature(challenge, signature) == MAGIC_VALUE, '!auth');
        manifest[agent] = manifestHash;
        updatedAt[agent] = uint64(block.timestamp);
        emit ManifestUpdated(agent, manifestHash, updatedAt[agent]);
    }
}