---
eip: 1337
label: ERC-1337
title: Subscriptions on the blockchain
status: Stagnant
type: Standards Track
category: ERC
created: 2018-08-01
requires: 20, 165
official: https://eips.ethereum.org/EIPS/eip-1337
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-1337.md
canonical: https://eips.huatuo.xyz/zh/eip-1337/
lang: zh
tldr: 一项关于定期加密货币支付的标准提案：用户只需签署一次订阅，商家之后提交该签名来提取每个周期的付款。
tags: [subscriptions, payments, meta-transactions, erc20]
---

# ERC-1337: Subscriptions on the blockchain

> Monthly subscriptions are a key monetization channel for legacy web, and arguably they are the most healthy monetization channel for businesses on the legacy web (especially when compared to ad/surveillance) based models.

**TL;DR:** 一项关于定期加密货币支付的标准提案：用户只需签署一次订阅，商家之后提交该签名来提取每个周期的付款。

## 它解决什么问题？

按月订阅是网络上的核心商业模式，但在以太坊上，每次付款都需要付款人签署一笔新的交易。指望用户每月回来手动发送代币，对于 SaaS 类产品行不通。

ERC-1337 提出了一种实现"拉取"支付的标准方式：用户只签署一次订阅条款，商家把该签名保存在链下，每个周期由商家（或中继方）提交它来收款。作者将其比作支付处理商保存的卡片令牌（card-on-file token）。

## 它如何运作

订阅者的合约（规范设想的是智能合约钱包）公开一个小型接口，并支持 [ERC-165](/eip-165/) 接口检测。

1. **构造条款。** `getSubscriptionHash(recipient, value, data, operation, txGas, dataGas, gasPrice, gasToken, meta)` 返回所有参数连同合约地址的哈希。`meta` 打包了额外字段，例如周期（`DAY`、`WEEK`、`MONTH`）和可选的到期时间。
2. **签署一次。** 所有者对该哈希签名，并把签名和输入交给商家。
3. **每个周期收款。** 商家用相同的输入加上签名调用 `executeSubscription(...)`。合约检查签名以及周期是否已经过去，然后执行付款（ETH 转账、[ERC-20](/eip-20/) 转账、调用等，由 `operation` 选择）。
4. **管理订阅。** `getSubscriptionStatus` 返回状态和下一次提取时间。所有者可以对从 `getModifyStatusHash` 获得的状态变更（暂停、取消）签名，并通过 `modifyStatus` 提交。

状态有 `ACTIVE`、`PAUSED`、`CANCELLED` 和 `EXPIRED`。gas 字段使执行者可以就提交交易的花费获得补偿，并可选择以代币支付。

## 示例代码

订阅者一侧，仅作说明（ethers v6）。规范没有固定确切的签名格式，因此请核实你的合约验证的是什么：

```ts
import { Contract, Wallet, getBytes, ZeroAddress } from "ethers";

const sub = new Contract(SUBSCRIPTION_CONTRACT, [
  "function getSubscriptionHash(address,uint256,bytes,uint8,uint256,uint256,uint256,address,bytes) view returns (bytes32)",
], provider);

const ERC20_OP = 3; // Enum.Operation.ERC20
const hash = await sub.getSubscriptionHash(
  MERCHANT, 10_000_000n, TOKEN_CALLDATA, ERC20_OP,
  100_000n, 0n, 0n, ZeroAddress, META_BYTES,
);

const signature = await owner.signMessage(getBytes(hash));
// send { inputs, signature } to the merchant, who later calls executeSubscription(...)
```

## 需要注意

- **状态：Stagnant。** 测试用例（Test Cases）和实现（Implementation）等章节仍标注为 TBD，规范中的一些函数签名也不完整（例如缺少逗号），因此无法照搬实现。
- 正如规范本身所指出的，除非其哈希被记录到链上并更改了状态，否则已签名的订阅仍然可以执行。取消意味着提交一个已签名的状态变更，而不只是在链下停止。
- 如今，定期支付和委托支付更多是用智能账户加会话密钥，或用代币 allowance 加 [ERC-2612](/eip-2612/) 之类的签名授权来构建，而不是使用这个接口。


---

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