Timelocked Receipt Vault

timelocked-receipt-vault · receipt-attestation · 864 B runtime · registry-info

scores

source

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

contract TimelockedReceiptVault {
    uint256 private constant MAX_REVEAL_DELAY = 365 days;

    event Committed(uint256 indexed id, address indexed agent, bytes32 digest, uint256 revealAfter);
    event Revealed(uint256 indexed id, address indexed agent, bytes32 dataHash);

    uint256 private _nextId;
    struct Commitment {
        address agent;
        bytes32 digest;
        uint256 revealAfter;
    }
    mapping(uint256 => Commitment) private _commitments;

    function commit(bytes32 digest, uint256 revealAfter) external returns (uint256 id) {
        require(revealAfter > block.timestamp && revealAfter <= block.timestamp + MAX_REVEAL_DELAY);
        id = ++_nextId;
        _commitments[id] = Commitment(msg.sender, digest, revealAfter);
        emit Committed(id, msg.sender, digest, revealAfter);
    }

    function reveal(uint256 id, bytes calldata data) external {
        Commitment storage c = _commitments[id];
        require(c.agent != address(0));
        require(msg.sender == c.agent);
        require(block.timestamp >= c.revealAfter);
        require(data.length <= 4096);
        bytes32 dataHash = keccak256(data);
        require(dataHash == c.digest);
        delete _commitments[id];
        emit Revealed(id, msg.sender, dataHash);
    }
}

published

published on mainnet · 0x8c648744c8147cDD62114d4EDE8049DB7edb7431 · tx 0xe119269253dedc96… · tempo (4217) · mainnet

published on testnet · 0x0e25E6F8528DC7B8471Ac3940190Df4E46164394 · tx 0xa6cdf428dce257c9… · tempo (42431) · testnet