---
eip: 712
label: EIP-712
title: Typed structured data hashing and signing
status: Final
type: Standards Track
category: Interface
created: 2017-09-12
requires: 155, 191
official: https://eips.ethereum.org/EIPS/eip-712
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md
canonical: https://eips.huatuo.xyz/ko/eip-712/
lang: ko
tldr: 타입이 있는 구조화 데이터를 해시하고 서명하는 표준 방식. 지갑은 16진수 덩어리 대신 읽기 쉬운 필드를 사용자에게 보여 줄 수 있고, 컨트랙트는 서명을 저렴하게 검증할 수 있다.
tags: [signatures, wallet, offchain, permit, interface]
---

# EIP-712: Typed structured data hashing and signing

> A procedure for hashing and signing of typed structured data as opposed to just bytestrings.

**TL;DR:** 타입이 있는 구조화 데이터를 해시하고 서명하는 표준 방식. 지갑은 16진수 덩어리 대신 읽기 쉬운 필드를 사용자에게 보여 줄 수 있고, 컨트랙트는 서명을 저렴하게 검증할 수 있다.

## 어떤 문제를 해결하나요?

오프체인 서명은 gas를 아낍니다. 메시지에 서명해 두면 다른 누군가가 나중에 그것을 컨트랙트에 제출합니다. gas 없는 승인, 오더북, 메타 트랜잭션은 모두 이 방식에 의존합니다. 하지만 일반 `eth_sign`에서는 사용자에게 알아볼 수 없는 16진수 문자열만 보이므로 무엇을 승인하는지 알 수 없습니다. 또 앱마다 자기만의 임시방편으로 데이터를 해시해, 실수하기 쉬웠습니다.

EIP-712는 구조화 데이터를 위한, 동료 검토를 거친 단일 인코딩을 정의합니다. 지갑은 필드("Permit: spender X, value 100, deadline …")를 표시할 수 있고, 한 앱이나 체인의 서명을 다른 곳에서 재사용할 수 없습니다.

## 어떻게 동작하나요?

최종적으로 서명하는 다이제스트는 다음과 같습니다.

```
keccak256("\x19\x01" ‖ domainSeparator ‖ hashStruct(message))
```

`0x19 0x01` 접두사는 [ERC-191](/eip-191/)을 따르며, RLP 트랜잭션으로 오인될 일이 없습니다.

- **`hashStruct(s) = keccak256(typeHash ‖ encodeData(s))`**
- **`typeHash`**는 타입 문자열의 `keccak256`입니다. 예: `Mail(address from,address to,string contents)`. 참조되는 구조체 타입은 이름순으로 정렬해 뒤에 붙입니다.
- **`encodeData`**는 각 멤버를 32바이트로 인코딩합니다. 원자 타입은 ABI 방식입니다. `string`과 `bytes`는 해시됩니다. 배열도 해시됩니다. 중첩 구조체는 각자의 `hashStruct`로 인코딩됩니다.
- **`domainSeparator`**는 `name`, `version`, `chainId`([EIP-155](/eip-155/)), `verifyingContract`, `salt` 중 일부 필드를 가진 `EIP712Domain`의 `hashStruct`입니다. 서명을 하나의 앱, 버전, 체인, 컨트랙트에 묶습니다.

RPC 메서드는 `eth_signTypedData`이며 `{ types, primaryType, domain, message }`를 받습니다.

## 샘플 코드

ethers v6로 서명하고 검증하기:

```ts
import { Wallet, verifyTypedData } from "ethers";

const wallet = Wallet.createRandom();

const domain = {
  name: "Ether Mail",
  version: "1",
  chainId: 1,
  verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
};
const types = {
  Person: [{ name: "name", type: "string" }, { name: "wallet", type: "address" }],
  Mail: [
    { name: "from", type: "Person" },
    { name: "to", type: "Person" },
    { name: "contents", type: "string" },
  ],
};
const message = {
  from: { name: "Cow", wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" },
  to: { name: "Bob", wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" },
  contents: "Hello, Bob!",
};

const sig = await wallet.signTypedData(domain, types, message);
console.log(verifyTypedData(domain, types, message, sig) === wallet.address); // true
```

온체인에서는 Solidity가 같은 다이제스트를 계산합니다.

```solidity
bytes32 constant MAIL_TYPEHASH = keccak256("Mail(Person from,Person to,string contents)Person(string name,address wallet)");
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR, structHash));
address signer = ecrecover(digest, v, r, s);
```

## 알아둘 점

- **리플레이 방지가 내장되어 있지 않습니다.** 구조체에 nonce와 deadline을 넣고, 온체인에서 사용 완료로 표시하세요.
- 지갑에서는 보통 `eth_signTypedData_v4`로 노출됩니다. 이전의 "v1/v3" 변형은 배열과 중첩 구조체를 처리하는 방식이 다릅니다.
- 잘 알려진 사용처로는 [ERC-2612](/eip-2612/)의 `permit`과 여러 DEX의 주문 형식이 있습니다. 스마트 컨트랙트 지갑은 [ERC-1271](/eip-1271/)로 이 서명들을 검증합니다. [ERC-5267](/eip-5267/)은 컨트랙트가 자신의 도메인을 공개할 수 있게 합니다.
- 타입 데이터 서명은 피싱에 즐겨 쓰입니다. 서명된 `Permit` 하나로 토큰을 모두 빼앗길 수 있기 때문입니다. 서명하는 내용을 항상 읽으세요.
- 인코딩을 직접 구현하지 말고 감사를 받은 헬퍼(OpenZeppelin의 `EIP712`)를 사용하세요. 이 EIP는 Final 상태입니다.


---

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