Sovereign Manifest Capsules

sovereign-manifest-capsules · discovery-registry · 558 B runtime · registry-info

scores

source

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

/// @title Sovereign Manifest Capsules
/// @notice Mapping-free deterministic capsule registry.
contract SovereignManifestCapsules {
    event ManifestUpdated(address indexed agent, bytes32 manifestHash, uint256 version);

    /// @notice Derive the capsule storage base slot for an agent.
    function _capsuleSlot(address agent) private view returns (bytes32 slot) {
        slot = bytes32(
            uint256(
                uint160(
                    uint256(
                        keccak256(abi.encodePacked(bytes1(0xff), address(this), agent, bytes32(0)))
                    )
                )
            )
        );
    }

    /// @notice Deterministic capsule address for an agent.
    function capsuleOf(address agent) external view returns (address) {
        return address(uint160(uint256(_capsuleSlot(agent))));
    }

    /// @notice Read an agent's manifest hash and version.
    function manifestOf(address agent) external view returns (bytes32 manifestHash, uint256 version) {
        bytes32 slot = _capsuleSlot(agent);
        assembly {
            let p := add(slot, 1)
            manifestHash := sload(slot)
            version := sload(p)
        }
    }

    /// @notice Update the caller's manifest. Only the agent can change their own capsule.
    function setManifest(bytes32 manifestHash) external {
        bytes32 slot = _capsuleSlot(msg.sender);
        uint256 version;
        assembly {
            let p := add(slot, 1)
            let v := add(sload(p), 1)
            sstore(slot, manifestHash)
            sstore(p, v)
            version := v
        }
        emit ManifestUpdated(msg.sender, manifestHash, version);
    }
}