EIPs Explained

ERC-5732 · Final · ERC · 2022-09-29

ERC-5732: Commit Interface

A minimal commit interface for commit-reveal schemes: contracts standardize only `commit`, and reveal through any method they like, which helps with private voting and resisting front-running.

What problem does it solve?

Everything you send to Ethereum is public before it is executed. If you submit a vote, a bid, or a domain name you want to register, others can see it in the mempool and react: copy it, front-run it, or change their own vote.

The classic fix is commit-reveal. First you publish only a hash of your action plus a secret salt (the commitment). Later you publish the action and the salt, and the contract checks they match the hash. ENS registration has used this for years. ERC-5732 standardizes the commit half, so wallets and tools can support commitments in the same way across many applications.

How it works

The standard deliberately defines only a commit method, no reveal method. Revealing is left to the application, so it can be a vote, transfer, register or anything else that carries the salt.

Two interfaces:

  • IERC_COMMIT_CORE (required, ERC-165 id 0xf14fcbc8): commit(bytes32 commitment) payable. This single-argument form matches what ENS already used.
  • IERC_COMMIT_GENERAL (recommended, id 0x67b2ec2c): commitFrom(address from, bytes32 commitment, bytes extraData) payable returns (uint256 timePoint) and a Commit(timePoint, from, commitment, extraData) event.

Rules worth knowing:

  • If a contract implements the general interface, commit(c) must behave like commitFrom(msg.sender, c, "").
  • timePoint orders commitments: a later commitment always has a value greater than or equal to an earlier one. block.timestamp or block.number are suggested.
  • commitFrom lets a relayer submit a commitment on someone else's behalf. When from is not the transaction sender, the contract should verify a signature from from (ERC-1271 for smart-contract wallets).
  • The reveal must have some way to pass the secret salt. Its size and position are left open.

Sample code

A sealed-vote contract that uses commit and reveals through vote:

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

contract SealedVote {
    event Commit(uint256 indexed timePoint, address indexed from, bytes32 indexed commitment, bytes extraData);

    mapping(address => bytes32) public commitments;
    mapping(uint8 => uint256) public tally;

    function commit(bytes32 commitment) external payable {
        commitments[msg.sender] = commitment;
        emit Commit(block.number, msg.sender, commitment, "");
    }

    // Reveal: the hash must match what was committed earlier.
    function vote(uint8 option, bytes32 salt) external {
        bytes32 c = keccak256(abi.encode(block.chainid, address(this), msg.sender, option, salt));
        require(c != bytes32(0) && commitments[msg.sender] == c, "bad reveal");
        delete commitments[msg.sender]; // prevent replay
        tally[option] += 1;
    }
}

Building the commitment off-chain (ethers v6):

import { AbiCoder, keccak256, hexlify, randomBytes } from "ethers";

const salt = hexlify(randomBytes(32)); // keep secret until reveal
const commitment = keccak256(
  AbiCoder.defaultAbiCoder().encode(
    ["uint256", "address", "address", "uint8", "bytes32"],
    [chainId, contractAddress, voterAddress, 2, salt],
  ),
);
await sealedVote.commit(commitment);

Things to know

  • Status: Final.
  • The salt must be long, random and never reused. Anyone who learns it before the reveal can recover the hidden action.
  • Include the chain id (and ideally the contract address) in the commitment to avoid replay across chains or forks. The spec suggests EIP-712 style structured hashing for higher-risk cases.
  • Two commits in the same block can share the same block.number or block.timestamp. If you need strictly increasing time points, keep your own counter.
  • Leave enough time between commit and reveal; commit-reveal only helps against front-running if the reveal cannot happen in the same moment as the commit.

#commit-reveal #front-running #voting #privacy #interface

Details

Authors
Zainan Victor Zhou (@xinbenlv), Matt Stam (@mattstam)
Status
Final
Type
Standards Track / ERC
Created
2022-09-29
Requires

Machine-readable: Markdown · JSON