Hash-Time-Locked Receipt

hash-time-locked-receipt · receipt-attestation · 1079 B runtime · registry-info

scores

source

pragma solidity ^0.8.13;

contract HashTimeLockedReceipt {
    struct Receipt {
        address counterparty;
        bytes32 hash;
        uint256 expiry;
    }

    mapping(address => Receipt) public receipts;

    event Locked(address indexed sender, address indexed counterparty, bytes32 indexed hash, uint256 expiry);
    event Unlocked(address indexed sender, address indexed counterparty, bytes32 secret);
    event Refunded(address indexed sender, address indexed counterparty, bytes32 hash);

    function lock(address counterparty, bytes32 hash, uint256 expiry) external {
        require(expiry > block.timestamp && receipts[msg.sender].expiry <= block.timestamp);
        receipts[msg.sender] = Receipt(counterparty, hash, expiry);
        emit Locked(msg.sender, counterparty, hash, expiry);
    }

    function unlock(address sender, bytes32 secret) external {
        Receipt memory r = receipts[sender];
        require(r.counterparty == msg.sender && block.timestamp < r.expiry && keccak256(abi.encodePacked(secret)) == r.hash);
        delete receipts[sender];
        emit Unlocked(sender, msg.sender, secret);
    }

    function refund() external {
        Receipt memory r = receipts[msg.sender];
        require(r.expiry != 0 && block.timestamp >= r.expiry);
        delete receipts[msg.sender];
        emit Refunded(msg.sender, r.counterparty, r.hash);
    }
}

published

published on testnet · 0x5C55ab622bC29638393ea018A3feA9FA8A9d14De · tx 0xc0dbee43bbe9fdf9… · tempo (42431) · testnet