Revocable Receipt Anchor

revocable-receipt-anchor · receipt-attestation · 618 B runtime · registry-info

scores

source

pragma solidity ^0.8.13;

contract RevocableReceiptAnchor {
    mapping(bytes32 => uint256) private receipts; // low 160 bits issuer, bit 255 valid

    event Issued(bytes32 indexed receiptHash, address indexed issuer);
    event Revoked(bytes32 indexed receiptHash, address indexed issuer);

    function issue(bytes32 receiptHash) external {
        require(receipts[receiptHash] == 0, "already used");
        receipts[receiptHash] = uint256(uint160(msg.sender)) | (1 << 255);
        emit Issued(receiptHash, msg.sender);
    }

    function revoke(bytes32 receiptHash) external {
        uint256 packed = receipts[receiptHash];
        address issuer = address(uint160(packed));
        require(issuer == msg.sender, "not issuer");
        require(packed & (1 << 255) != 0, "not valid");
        receipts[receiptHash] = uint256(uint160(msg.sender));
        emit Revoked(receiptHash, msg.sender);
    }

    function isValid(bytes32 receiptHash) external view returns (bool) {
        return receipts[receiptHash] & (1 << 255) != 0;
    }
}