---
eip: 7758
label: ERC-7758
title: Transfer With Authorization
status: Review
type: Standards Track
category: ERC
created: 2020-09-28
requires: 20, 712
official: https://eips.ethereum.org/EIPS/eip-7758
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7758.md
canonical: https://eips.huatuo.xyz/zh/eip-7758/
lang: zh
tldr: 用链下签名而不是交易来转移 ERC-20 代币：任何人都可以提交签名，gas 可由中继者或用代币本身支付，随机 nonce 让多个授权可以并行。ERC-3009 的后继，USDC 已实现。
tags: [token, meta-transaction, gasless, eip-712, usdc, erc-20]
---

# ERC-7758: Transfer With Authorization

> Transfer fungible assets via a signed authorization.

**TL;DR:** 用链下签名而不是交易来转移 ERC-20 代币：任何人都可以提交签名，gas 可由中继者或用代币本身支付，随机 nonce 让多个授权可以并行。ERC-3009 的后继，USDC 已实现。

## 它解决什么问题？

要发送 [ERC-20](/eip-20/) 代币，通常同一账户里必须有 ETH 付 gas。一个刚收到 50 USDC、别无他物的用户就动弹不得。协议方也不得不让用户先做两笔交易（`approve`，再 `transferFrom`）才能收款，而 allowance 模式又助长了"无限授权"的习惯和多重提款攻击。

ERC-7758 让代币持有者在链下**签署**一份转账授权。任何人（中继者、收款方、Paymaster）都可以把这个签名提交给代币合约，由合约执行转账。持有者永远不需要 ETH，也永远不需要授予 allowance。

[ERC-2612](/eip-2612/) 的 `permit` 解决了其中一部分，但保留了 allowance 模型，并使用*顺序* nonce，用户无法同时发出多个授权。ERC-7758 使用**随机 32 字节 nonce**，授权彼此独立，数量和顺序都不受限。

ERC-7758 是 [ERC-3009](/eip-3009/) 以相同规范重新提交到当前评审流程的版本。USDC 自 2020 年起就实现了它，因此很大一部分稳定币"无 gas"流程已经运行在这个接口之上。

## 它如何运作

代币公开三个由签名驱动的函数。所有消息都是基于代币域（`name`、`version`、`chainId`、`verifyingContract`）的 [EIP-712](/eip-712/) 类型化数据。

| 函数 | 谁可以调用 | 用途 |
| --- | --- | --- |
| `transferWithAuthorization(from, to, value, validAfter, validBefore, nonce, v, r, s)` | 任何人 | 执行由 `from` 签名的转账 |
| `receiveWithAuthorization(...)` | 仅 `to` | 同上，但收款方必须是调用者，防止监听者抢跑并劫持入金流程 |
| `cancelAuthorization(authorizer, nonce, v, r, s)` | 任何人 | 作废一个未使用的 nonce（可选） |
| `authorizationState(authorizer, nonce)` | view | nonce 已使用或已取消时返回 `true` |

每个授权都有有效期窗口（`validAfter` / `validBefore`，unix 时间）和唯一的 `nonce`。一旦被消费，合约发出 `AuthorizationUsed(authorizer, nonce)` 并拒绝再次使用同一 nonce。

## 示例代码

在浏览器或后端签署授权（ethers v6）：

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

const holder = new Wallet(HOLDER_KEY);          // 持有代币，没有 ETH
const now = Math.floor(Date.now() / 1000);

const domain = {
  name: "USD Coin",
  version: "2",
  chainId: 1,
  verifyingContract: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
};
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: holder.address,
  to: MERCHANT,
  value: parseUnits("50", 6),
  validAfter: 0,
  validBefore: now + 3600,            // 一小时内有效
  nonce: hexlify(randomBytes(32)),    // 随机而非顺序
};

const sig = Signature.from(await holder.signTypedData(domain, types, message));
// 把 { message, v: sig.v, r: sig.r, s: sig.s } 交给中继者
```

中继者（任何有 ETH 的账户）提交：

```ts
const usdc = new Contract(domain.verifyingContract, [
  "function transferWithAuthorization(address from,address to,uint256 value,uint256 validAfter,uint256 validBefore,bytes32 nonce,uint8 v,bytes32 r,bytes32 s)",
], relayerWallet);

await usdc.transferWithAuthorization(
  message.from, message.to, message.value,
  message.validAfter, message.validBefore, message.nonce,
  sig.v, sig.r, sig.s,
);
```

一个原子化接收代币存款的合约，使用 `receiveWithAuthorization` 保证签名只能被本合约使用：

```solidity
interface IERC7758 {
    function receiveWithAuthorization(
        address from, address to, uint256 value,
        uint256 validAfter, uint256 validBefore, bytes32 nonce,
        uint8 v, bytes32 r, bytes32 s
    ) external;
}

contract Vault {
    IERC7758 public immutable token;
    mapping(address => uint256) public deposits;

    constructor(IERC7758 _token) { token = _token; }

    function deposit(
        address from, uint256 value, uint256 validAfter, uint256 validBefore,
        bytes32 nonce, uint8 v, bytes32 r, bytes32 s
    ) external {
        // to 必须是本合约；代币会强制 msg.sender == to。
        token.receiveWithAuthorization(from, address(this), value, validAfter, validBefore, nonce, v, r, s);
        deposits[from] += value;
    }
}
```

## 需要注意

- 当收款方是合约时，一律使用 `receiveWithAuthorization`。若用 `transferWithAuthorization`，机器人可以抢先提交签名：代币照样到账，但合约自己的记账调用永远不会执行。
- nonce 是随机 `bytes32`，请用密码学安全的随机数生成，并在复用前检查 `authorizationState`。同一 nonce 对每个授权者只能用一次。
- 由于 EIP-712 域的存在，签名与链和合约绑定；请核对代币的 `name` 和 `version`（USDC 使用 version `"2"`）。
- 因为签名者不付 gas，它天然适合与 [ERC-4337](/eip-4337/) 的 Paymaster 以及 [EIP-7702](/eip-7702/) 的代付交易组合使用。


---

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