---
eip: 5732
label: ERC-5732
title: Commit Interface
status: Final
type: Standards Track
category: ERC
created: 2022-09-29
requires: 165, 1271
official: https://eips.ethereum.org/EIPS/eip-5732
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-5732.md
canonical: https://eips.huatuo.xyz/zh/eip-5732/
lang: zh
tldr: 用于 commit-reveal 方案的最小 commit 接口：合约只标准化 `commit`，揭示则通过任何它们喜欢的方法完成，有助于私密投票和抵御抢跑。
tags: [commit-reveal, front-running, voting, privacy, interface]
---

# ERC-5732: Commit Interface

> A simple but general commit interface to support commit-reveal scheme.

**TL;DR:** 用于 commit-reveal 方案的最小 commit 接口：合约只标准化 `commit`，揭示则通过任何它们喜欢的方法完成，有助于私密投票和抵御抢跑。

## 它解决什么问题？

你发送到以太坊的一切在被执行之前都是公开的。如果你提交投票、出价或想注册的域名，其他人可以在 mempool 中看到并作出反应：复制它、抢跑，或改变自己的投票。

经典的解决办法是 **commit-reveal**。首先，你只公布你的操作加上一个秘密盐值的哈希（承诺）。之后，你公布操作和盐值，合约检查它们是否与哈希匹配。ENS 注册多年来一直使用这种方式。ERC-5732 标准化了 commit 这一半，让钱包和工具可以在许多应用中以相同方式支持承诺。

## 它如何运作

该标准有意**只定义 commit 方法，不定义 reveal 方法**。揭示留给应用自行实现，因此它可以是 `vote`、`transfer`、`register`，或任何其他携带盐值的方法。

两个接口：

- `IERC_COMMIT_CORE`（必需，ERC-165 id `0xf14fcbc8`）：`commit(bytes32 commitment) payable`。这种单参数形式与 ENS 已有的用法一致。
- `IERC_COMMIT_GENERAL`（推荐，id `0x67b2ec2c`）：`commitFrom(address from, bytes32 commitment, bytes extraData) payable returns (uint256 timePoint)` 以及 `Commit(timePoint, from, commitment, extraData)` 事件。

值得了解的规则：

- 如果合约实现了 general 接口，`commit(c)` 的行为必须与 `commitFrom(msg.sender, c, "")` 相同。
- `timePoint` 用于给承诺排序：较晚的承诺其值总是大于或等于较早的承诺。建议使用 `block.timestamp` 或 `block.number`。
- `commitFrom` 允许中继者代表他人提交承诺。当 `from` 不是交易发送者时，合约应当验证来自 `from` 的签名（智能合约钱包使用 [ERC-1271](/eip-1271/)）。
- 揭示必须有某种方式传递秘密盐值。其大小和位置不作规定。

## 示例代码

一个使用 `commit` 并通过 `vote` 揭示的密封投票合约：

```solidity
// 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;
    }
}
```

在链下构造承诺（ethers v6）：

```ts
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);
```

## 需要注意

- **状态：Final。**
- 盐值必须足够长、随机，并且绝不重复使用。任何在揭示前得知它的人都可以还原被隐藏的操作。
- 请在承诺中包含链 id（最好还有合约地址），以避免跨链或跨分叉的重放。对于风险较高的场景，规范建议使用 [EIP-712](/eip-712/) 风格的结构化哈希。
- 同一区块内的两次 commit 可能具有相同的 `block.number` 或 `block.timestamp`。如果需要严格递增的时间点，请自行维护计数器。
- 在 commit 与 reveal 之间留出足够的时间；只有当揭示不可能与承诺同时发生时，commit-reveal 才能抵御抢跑。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-5732
