Proof-of-Work Manifest Namespace

proof-of-work-manifest-namespace · discovery-registry · 854 B runtime · funds-movement

scores

source

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

contract PoWManifestRegistry {
    error AlreadyRegistered();
    error NotOwner();
    error PoWFailed();

    event Registered(bytes32 indexed namespace, address indexed owner, bytes32 manifestRoot, uint256 salt);
    event ManifestUpdated(bytes32 indexed namespace, bytes32 manifestRoot, uint256 salt);

    struct Record {
        address owner;
        bytes32 manifestRoot;
    }

    uint256 public immutable difficulty;

    mapping(bytes32 => Record) private records;

    constructor(uint256 _difficulty) {
        difficulty = _difficulty;
    }

    function register(bytes32 namespace, bytes32 manifestRoot, uint256 salt) external {
        if (records[namespace].owner != address(0)) revert AlreadyRegistered();
        if (uint256(keccak256(abi.encodePacked(namespace, msg.sender, salt))) >= difficulty) revert PoWFailed();
        records[namespace] = Record(msg.sender, manifestRoot);
        emit Registered(namespace, msg.sender, manifestRoot, salt);
    }

    function update(bytes32 namespace, bytes32 newRoot, uint256 salt) external {
        if (records[namespace].owner != msg.sender) revert NotOwner();
        records[namespace].manifestRoot = newRoot;
        emit ManifestUpdated(namespace, newRoot, salt);
    }

    function resolve(bytes32 namespace) external view returns (address, bytes32) {
        Record memory r = records[namespace];
        return (r.owner, r.manifestRoot);
    }
}