@ethereumjs/binarytree

中文文档
10.1.3发布于 2026/08/21MITTypeScriptESM + CJS近 30 天下载 18.7万

Implementation of binary trees as used in Ethereum.

原始使用文档
文档目录8 节
  1. Table of Contents
  2. Installation
  3. Getting Started
  4. Proofs
  5. Browser
  6. API
  7. EthereumJS
  8. License

@ethereumjs/binarytree v10 (EXPERIMENTAL)

NPM Package GitHub Issues Actions Status Code Coverage Discord

Implementation of Binary Trees as specified in EIP-7864

Binary Trees are a novel cryptographic data structure proposed for Ethereum state storage with smaller proof sizes than Merkle Patricia Tries. Keys map to a stem (first 31 bytes) and index (last byte) within a stem node.

This library is experimental — APIs are not stable and it must not be used in production.

Runnable examples live in examples/.

Table of Contents

Installation

npm install @ethereumjs/binarytree

Getting Started

Use createBinaryTree() to instantiate a tree. By default an in-memory MapDB is used; pass db for persistence.

// ./examples/basicUsage.ts

import { createBinaryTree } from '@ethereumjs/binarytree'
import { bytesToHex, hexToBytes } from '@ethereumjs/util'

const main = async () => {
  const tree = await createBinaryTree()

  const key = hexToBytes(`0x${'00'.repeat(32)}`)
  const value = hexToBytes(`0x${'01'.repeat(32)}`)
  const stem = key.slice(0, 31)
  const index = key[31]

  await tree.put(stem, [index], [value])
  const [retrieved] = await tree.get(stem, [index])

  console.log(`Root: ${bytesToHex(tree.root())}`)
  console.log(`Value match: ${bytesToHex(retrieved!)}`)
}

void main()

put(stem, indices, values) accepts parallel arrays of slot indices and 32-byte values within one stem. get(stem, indices) returns values in the same order.

Options (BinaryTreeOpts): db, useRootPersistence, cacheSize, hashFunction (defaults to BLAKE3).

Proofs

Create inclusion proofs with tree.createBinaryProof(key), verify with verifyBinaryProof(), and rebuild a sparse tree from proof nodes with binaryTreeFromProof():

// ./examples/binaryProof.ts

import { binaryTreeFromProof, createBinaryTree, verifyBinaryProof } from '@ethereumjs/binarytree'
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
import { blake3 } from '@noble/hashes/blake3.js'

const main = async () => {
  const tree = await createBinaryTree()

  const key = hexToBytes(`0x${'00'.repeat(31)}01`)
  const hashedKey = blake3(key)
  const value = hexToBytes(`0x${'02'.repeat(32)}`)
  const stem = hashedKey.slice(0, 31)
  const index = hashedKey[31]

  await tree.put(stem, [index], [value])

  const proof = await tree.createBinaryProof(hashedKey)
  const verified = await verifyBinaryProof(tree.root(), hashedKey, proof)
  const sparse = await binaryTreeFromProof(proof)

  console.log(`Proof length: ${proof.length} nodes`)
  console.log(`Verified value: ${bytesToHex(verified!)}`)
  console.log(`Sparse tree root match: ${bytesToHex(sparse.root()) === bytesToHex(tree.root())}`)
}

void main()

Proof keys are typically blake3(address || treeIndex || subIndex) as used by the Verkle/binary-tree state layer. A valid proof of non-existence returns null / undefined from verifyBinaryProof().

Browser

Hybrid ESM/CJS builds are provided. For a browser setup see ./examples/browser.html (build the package, then npx vite from the package root).

API

Generated TypeDoc documentation.

Main exports: BinaryTree, createBinaryTree, binaryTreeFromProof, verifyBinaryProof, CheckpointDB, node types under ./node.

Used by StatefulBinaryTreeStateManager in @ethereumjs/statemanager when EIP-7864 is activated on Common.

EthereumJS

The EthereumJS GitHub organization and its repositories are managed by members of the former Ethereum Foundation JavaScript team and the broader Ethereum community. If you want to join for work or carry out improvements on the libraries see the developer docs for an overview of current standards and tools and review our code of conduct.

License

MIT

版本与发布记录

@ethereumjs/binarytree 共发布 8 个版本,首次发布于 2025/03/25;过去 12 个月发布了 5 个版本。

版本发布日期距今
10.1.3latest2026/08/212 个月前
10.1.22026/05/295 个月前
10.1.12026/01/289 个月前
10.1.1-nightly.2nightly2026/01/199 个月前
10.1.1-nightly.12025/12/0411 个月前
10.1.02025/11/071 年前

其他发布通道:rc → 10.0.0-dev-rc.1,nightly → 10.1.1-nightly.2。

关于 @ethereumjs/binarytree 的常见问题

@ethereumjs/binarytree 是什么?

Implementation of binary trees as used in Ethereum.

如何安装 @ethereumjs/binarytree?

在项目目录运行 npm install @ethereumjs/binarytree,或使用 pnpm add / yarn add / bun add。

@ethereumjs/binarytree 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ethereumjs/binarytree 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ethereumjs/binarytree 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ethereumjs/binarytree 可以商用吗?

@ethereumjs/binarytree 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ethereumjs/binarytree 的下载量有多少?

根据 npm 官方下载统计,@ethereumjs/binarytree 在 2026/10/14 – 2026/11/12 这 30 天内被下载 186,896 次,日均约 6,230 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ethereumjs/binarytree 还在维护吗?

最新版本 10.1.3 发布于 2026/08/21(2 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 5 个;首次发布于 2025/03/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。