---
eip: 3009
label: ERC-3009
title: Transfer With Authorization
status: Draft
type: Standards Track
category: ERC
created: 2020-09-28
requires: 20, 712
official: https://eips.ethereum.org/EIPS/eip-3009
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-3009.md
canonical: https://eips.huatuo.xyz/zh/eip-3009/
lang: zh
tldr: 允许 ERC-20 持有者在链下签署一次性的转账授权，任何人都可以提交；使用随机 nonce 和有效期窗口，无需 allowance 即可实现免 gas 且原子的代币支付。
tags: [token, erc20, meta-transactions, gasless, eip712, payments]
---

# ERC-3009: Transfer With Authorization

> Transfer fungible assets via a signed authorization

**TL;DR:** 允许 ERC-20 持有者在链下签署一次性的转账授权，任何人都可以提交；使用随机 nonce 和有效期窗口，无需 allowance 即可实现免 gas 且原子的代币支付。

## 它解决什么问题？

要转移 [ERC-20](/eip-20/) 代币，持有者通常需要发送一笔交易，因此需要 ETH 来支付 gas。让合约代你转移代币则需要先 `approve`，这会多出一笔交易，还常常导致有风险的"无限"授权额度。

[ERC-2612](/eip-2612/) 的 `permit` 通过签名授权解决了其中一部分问题，但它使用的是**顺序**的 nonce。如果用户在两个不同的应用中分别签了 nonce 5 和 nonce 6，或者中继者把它们以错误的顺序上链，就会有一个失败。而且它仍然依赖 allowance 模式。

ERC-3009 让持有者签署**转账本身**。由中继者、收款方或合约提交并支付 gas。每个授权都带有一个随机的 32 字节 nonce，因此用户可以并行创建许多授权，不存在顺序问题。

## 它如何运作

持有者签署一条 [EIP-712](/eip-712/) 类型化消息：

```text
TransferWithAuthorization(address from,address to,uint256 value,
                          uint256 validAfter,uint256 validBefore,bytes32 nonce)
```

代币合约公开以下函数：

| 函数 | 用途 |
| --- | --- |
| `transferWithAuthorization(from, to, value, validAfter, validBefore, nonce, v, r, s)` | 任何人都可以提交；把 `value` 从 `from` 转给 `to` |
| `receiveWithAuthorization(...)`（参数相同） | 同上，但要求 `msg.sender == to`；合约应使用这个 |
| `authorizationState(authorizer, nonce)` | 某个 nonce 是否已被使用 |
| `cancelAuthorization(authorizer, nonce, v, r, s)` | 可选：在 nonce 被使用之前将其作废 |

合约会检查 `validAfter < now < validBefore`、该 nonce 对 `from` 尚未使用，以及签名能恢复出 `from`。然后把 nonce 标记为已使用，触发 `AuthorizationUsed(authorizer, nonce)` 事件，并执行转账。

当提交时机由中继者掌控时，有效期窗口可以保护用户。EIP-712 域（包括 `chainId` 和代币地址）防止了在其他链或其他代币上的重放。

## 示例代码

签署授权并以中继者身份提交（ethers v6）：

```ts
import { Wallet, Contract, hexlify, randomBytes, Signature, parseUnits } from "ethers";

const domain = { name: "USD Coin", version: "2", chainId, verifyingContract: tokenAddress };
const types = {
  TransferWithAuthorization: [
    { name: "from", type: "address" },
    { name: "to", type: "address" },
    { name: "value", type: "uint256" },
    { name: "validAfter", type: "uint256" },
    { name: "validBefore", type: "uint256" },
    { name: "nonce", type: "bytes32" },
  ],
};
const message = {
  from: payer.address,
  to: merchant,
  value: parseUnits("10", 6),
  validAfter: 0,
  validBefore: Math.floor(Date.now() / 1000) + 3600, // 1 hour
  nonce: hexlify(randomBytes(32)),
};

const sig = Signature.from(await payer.signTypedData(domain, types, message)); // off-chain

// Relayer pays the gas:
const token = new Contract(tokenAddress, [
  "function transferWithAuthorization(address,address,uint256,uint256,uint256,bytes32,uint8,bytes32,bytes32)",
], relayer);
await token.transferWithAuthorization(
  message.from, message.to, message.value, message.validAfter,
  message.validBefore, message.nonce, sig.v, sig.r, sig.s,
);
```

域中的 `name` 和 `version` 必须与代币合约使用的一致；请从合约中读取，而不要硬编码。

## 需要注意

- 状态为 **Draft**，不过该接口已在生产环境的代币中实现，最著名的是 USDC。
- **抢跑（front-running）：** 如果合约调用 `transferWithAuthorization`，观察者可以从内存池中复制签名并直接提交，这样转账就会在你合约的逻辑没有运行的情况下发生。合约应使用只有收款方才能调用的 `receiveWithAuthorization`。
- 它是为外部拥有账户设计的。合约账户直接调用 `transfer` 即可。
- 实现必须拒绝 `ecrecover` 返回零地址的结果；格式错误的签名会恢复为 `address(0)`。
- 为了获得最大的兼容性，鼓励代币同时实现 ERC-3009 和 [ERC-2612](/eip-2612/)。


---

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