---
eip: 627
label: EIP-627
title: Whisper Specification
status: Final
type: Standards Track
category: Networking
created: 2017-05-05
official: https://eips.ethereum.org/EIPS/eip-627
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-627.md
canonical: https://eips.huatuo.xyz/zh/eip-627/
lang: zh
tldr: 规定 Whisper v6 的线路格式。Whisper 是以太坊早期的点对点消息协议，涉及数据包代码、加密信封、工作量证明和主题布隆过滤器。
tags: [networking, p2p, messaging, devp2p, whisper]
---

# EIP-627: Whisper Specification

> This EIP describes the format of Whisper messages within the ÐΞVp2p Wire Protocol. This EIP should substitute the existing specification. More detailed documentation on Whisper could be found here.

**TL;DR:** 规定 Whisper v6 的线路格式。Whisper 是以太坊早期的点对点消息协议，涉及数据包代码、加密信封、工作量证明和主题布隆过滤器。

## 它解决什么问题？

在最初的“Web3”构想中，以太坊有三大支柱：用于共识的区块链、用于存储的 Swarm，以及用于消息传递的 **Whisper**。Whisper 让 dapp 之间可以通过节点已在使用的同一个点对点网络，发送小型的、加密的、短期有效的消息，而无需中心化服务器。

不同的客户端（go-ethereum、Parity）都在开发 Whisper，因此需要一份书面规范来保持兼容。EIP-627 就是 **Whisper v6** 的这份规范。它作为 devp2p（ÐΞVp2p）线路协议的子协议运行。

## 它如何运作

每个数据包都是一个 RLP 列表：`[packet_code, payload]`。代码 0–127 为 Whisper 保留，节点**必须忽略**未知的代码，以免新版本破坏旧的对等节点。

| 代码 | 数据包 | 载荷 |
| --- | --- | --- |
| 0 | Status | 版本、PoW 要求、可选的布隆过滤器（握手后最先发送） |
| 1 | Messages | 信封列表 |
| 2 | PoW Requirement | 64 位浮点数，即该节点接受的最低 PoW |
| 3 | Bloom Filter | 该节点所需主题的 512 位过滤器 |
| 126 | P2P Request（可选） | 一个信封，例如邮件客户端请求 |
| 127 | P2P Message（可选） | 一个不应再被继续转发的信封 |

**信封**为 `[Expiry, TTL, Topic, Data, Nonce]`。`Topic` 是对等节点可以用来过滤的 4 个字节。`Data` 是加密后的消息：使用 secp256k1 密钥的 ECIES，或使用共享密钥的 AES-GCM。明文内部包含标志位、载荷、随机填充（用于隐藏长度）以及可选的签名。

为了抑制垃圾信息，发送者必须搜索一个 `Nonce`，使信封的哈希具有足够多的前导零比特。消息越大或存活时间越长，需要的工作量越多：

```
PoW = 2**leading_zero_bits(hash) / (size * TTL)
```

节点可以通过数据包 2 在运行时提高或降低自己的最低要求，并可以通过数据包 3 限制自己接收的内容。

## 示例代码

按规范给出的方法，将 4 字节主题折叠进 512 位布隆过滤器（伪代码）：

```python
def topic_to_bloom(topic: bytes) -> int:      # topic is 4 bytes
    bloom = 0
    for i in range(3):
        n = topic[i]
        if topic[3] & (1 << i):
            n += 256                           # use the upper half of the 512 bits
        bloom |= 1 << n                        # sets at most 3 bits per topic
    return bloom

# A node's filter is the OR of the blooms of all its topics
my_filter = topic_to_bloom(b"\xde\xad\xbe\xef") | topic_to_bloom(b"\x01\x02\x03\x04")
```

## 需要注意

- Whisper 从未得到广泛采用，go-ethereum 后来弃用并移除了它。Status 团队的 **Waku** 协议由 Whisper v6 发展而来，在 EIP 流程之外维护。
- 规范说 Data 字段的布局是“可选的”：节点会转发自己无法解密的信封。只有接收者需要理解明文格式。
- 代码 126 和 127 的存在，是为了让“邮件服务器”可以把已过期的消息直接投递给某个对等节点，而正常的转发规则不允许这样做。
- 关于 devp2p 通用的前向兼容规则，即忽略自己不理解的内容，请参阅 [EIP-8](/eip-8/)。


---

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