ERC-5732 · Final(확정) · ERC · 2022-09-29
ERC-5732: Commit Interface
commit-reveal 방식을 위한 최소 commit 인터페이스. 컨트랙트는 `commit`만 표준화하고 reveal은 원하는 메서드로 하며, 비공개 투표와 프런트러닝 방어에 도움이 된다.
어떤 문제를 해결하나요?
이더리움에 보내는 모든 것은 실행되기 전에 공개됩니다. 투표, 입찰, 등록하려는 도메인 이름을 제출하면 다른 사람들이 mempool에서 이를 보고 반응할 수 있습니다. 복사하거나, 프런트러닝하거나, 자신의 투표를 바꿀 수 있습니다.
고전적인 해결책은 commit-reveal입니다. 먼저 행동과 비밀 salt의 해시(커밋먼트)만 공개합니다. 나중에 행동과 salt를 공개하면 컨트랙트가 그것들이 해시와 일치하는지 확인합니다. ENS 등록은 오래전부터 이를 사용해 왔습니다. ERC-5732는 commit 절반을 표준화하여, 지갑과 도구가 여러 애플리케이션에서 같은 방식으로 커밋먼트를 지원할 수 있게 합니다.
어떻게 동작하나요?
이 표준은 의도적으로 commit 메서드만 정의하고 reveal 메서드는 정의하지 않습니다. reveal은 애플리케이션에 맡겨지므로 vote, transfer, register 또는 salt를 실을 수 있는 다른 무엇이든 될 수 있습니다.
인터페이스 두 개:
IERC_COMMIT_CORE(필수, ERC-165 id0xf14fcbc8):commit(bytes32 commitment) payable. 이 단일 인자 형태는 ENS가 이미 쓰던 것과 맞춘 것입니다.IERC_COMMIT_GENERAL(권장, id0x67b2ec2c):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).- reveal에는 비밀 salt를 전달할 방법이 있어야 합니다. 그 크기와 위치는 정해져 있지 않습니다.
샘플 코드
commit을 사용하고 vote로 reveal하는 봉인 투표 컨트랙트:
// 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):
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.
- salt는 길고 무작위여야 하며 절대 재사용하면 안 됩니다. reveal 전에 이를 알게 된 사람은 누구든 숨겨진 행동을 복원할 수 있습니다.
- 체인이나 포크 간 리플레이를 막기 위해 커밋먼트에 체인 id(가능하면 컨트랙트 주소도)를 포함하세요. 위험도가 높은 경우 사양은 EIP-712 스타일의 구조화된 해싱을 권합니다.
- 같은 블록의 커밋 두 개는 같은
block.number나block.timestamp를 가질 수 있습니다. 엄격하게 증가하는 time point가 필요하다면 자체 카운터를 유지하세요. - commit과 reveal 사이에 충분한 시간을 두세요. commit-reveal은 reveal이 commit과 같은 순간에 일어날 수 없을 때만 프런트러닝에 도움이 됩니다.