@fairdatasociety/bmt-js

中文文档
2.1.0发布于 2023/07/07MITTypeScriptCommonJS零依赖近 30 天下载 2.7万

Binary Merkle Tree operations on data

原始使用文档
文档目录3 节
  1. Types
  2. Functions
  3. Other objects

BMT-JS

This JavaScript library does Binary Merkle Tree (BMT) operations on data.

The library calculates the content address of any data and also offers functions for performing cheap compact inclusion proof of any 32 bytes segments in it.

Install

npm install @fairdatasociety/bmt-js --save

How it works

First, it splits the data into chunks that have maximum 4KB payload by default, but this condition can modified as well as its span byte length (8 bytes) that indicates how long byte payload subsumed under the chunk.

If the payload byte length cannot fit exactly to this chunk division, the rightmost chunk's data will be padded with zeros in order to have fixed length data for the BMT operations.

This basic unit is also required to effectively distribute data on decentralized storage systems with regard to plausible deniability, garbage collection, load balancing and else. For more details, please visit Etherem Swarm webpage that has full implementation of this logic.

The used hashing algorithm is the keccak256 function that results in a 32 bytes long segment.

Performing BMT hashing on the chunk data will define the BMT root hash of the chunk. Then, for integrity considerations, the BMT root hash is hashed with the chunk's span from the left which takes the Chunk address.

BMT Hashing

In order to refer files also with a single 32 byte segment, the chunk addresses of the payload have to be hashed in the same way until the File address:

chunks can encapsulate 128 chunk addresses on the subsequent BMT tree level by default. These kind of chunks are called Intermediate chunks By the properties of a BMT tree, the chunks will end in a Root chunk that refers all chunks below (directly or indirectly) and its address will be the File address as well.

One can realize, the BMT levels can have an orphan chunk on the rightmost-side that cannot be hashed with a neighbour chunk, because it does not have a neighbour chunk (e.g. 129/129 chunk). When it occurs, it does not have a sense to hash this orphan chunk on every BMT level since it will be BMT hashed with zero data. That's why the the algorithm handles orphan chunk as Carrier chunk and tries to place it into that BMT tree level where the chunk address can be encapsulated with other addresses.

This BMT hashing of data allows to reference any file with unified 32 bytes unique address which is called content addressing.

File BMT calculation

Nevertheless, it also allows to perform lightweight compact inclusion proof on data. This proof requires little amount of data to be provided for proving whether any particular segment (32 bytes) of the data is present at a particular offset under the file address.

This feature allows to create a logic around data referenced by 32 bytes file addresses where the data segment values have to meet some conditions. The first/best use-case for this can happen via smart contracts that implement the validation functions that check the provided inclusion proof segments are indeed subsumed under the commited file references.

To get these inclusion segments, the library collects all required segments from the BMT tree that can be used for input of smart contract validation parameters.

Inclusion proof

API

You can import the followings directly from @fairdatasociety/bmt-js:

Types

  • Chunk # interface of the helper object for performing BMT actions on chunk data
    • payload # the passed byte payload with which the object was initialized.
    • maxPayloadLength # the maximum payload byte length in the chunk. It is 4096 by default.
    • spanLength # the reserved byte length for span serialisation. By default it is 8 bytes.
    • data() # gives back the chunk's data that is exactly maxPayloadLength long
    • span() # serialized span value of chunk
    • address() # gives back the calculated chunk address of the data
    • inclusionProof(segmentIndex) # gives back the inclusion proof segments for proofing the segment under the given index.
    • bmt() # gives back the Binary Merkle Tree of the chunk data
  • ChunkedFile # interface of the helper object for performing BMT actions on file data
    • payload # the passed byte payload with which the object was initialized.
    • leafChunks() # data chunks of the file data
    • rootChunk() # topmost chunk in the file BMT
    • address() # gives back the calculated chunk address of file data
    • span() # serialized span value of the file
    • bmt() # gives back the Binary Merkle Tree of the file data
  • ChunkInclusionProof # groups chunk inclusion proof segments and span value of a chunk
  • ChunkAddress # chunk address resulted from BMT hashing of data. It is used also fole FileAddress
  • Span # span value in byte format. Indicates how much data subsumed under the Chunk/File

Functions

  • makeChunkedFile # makes Chunk helper object for performing BMT actions on file data
  • makeChunk # makes Chunk helper object for performing BMT actions on chunk data
  • makeSpan # make serialized Span byte array that indicates how much data subsumed under the Chunk/File
  • getSpanValue # deserialised data into number carrier by Span
  • rootHashFromInclusionProof # calculate the BMT root hash from the provided inclusion proof segments and its corresponding segment index
  • getBmtIndexOfSegment # get the chunk's position of a given payload segment index in the BMT tree
  • fileInclusionProofBottomUp # gives back required sister segments of a given payload segment index for inclusion proof
  • fileAddressFromInclusionProof # gives back the file address that is calculated with only the inclusion proof segments and the corresponding proved segment and its position.

Other objects

  • Utils # mainly bytes related utility functions

Compilation

In order to compile code run

npm run compile

You can find the resulted code under the dist folder.

For types compilation, run

npm run compile:types

Testing

The testing needs running Bee client node for integration testing. You can set BEE_POSTAGE environment variable with a valid Postage batch or the test will create one for you.

The default value of the Bee Debug API endpoint is http://localhost:1635. If your address diverges from that, please, set BEE_DEBUG_API_URL system environment variable with yours.

To run test execute

npm run test

版本与发布记录

@fairdatasociety/bmt-js 共发布 4 个版本,首次发布于 2022/03/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.0latest2023/07/073 年前
2.0.12022/05/104 年前
2.0.02022/03/314 年前
1.0.02022/03/254 年前

关于 @fairdatasociety/bmt-js 的常见问题

@fairdatasociety/bmt-js 是什么?

Binary Merkle Tree operations on data

如何安装 @fairdatasociety/bmt-js?

在项目目录运行 npm install @fairdatasociety/bmt-js,或使用 pnpm add / yarn add / bun add。

@fairdatasociety/bmt-js 支持 TypeScript 吗?

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

@fairdatasociety/bmt-js 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fairdatasociety/bmt-js 可以商用吗?

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

@fairdatasociety/bmt-js 的下载量有多少?

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

@fairdatasociety/bmt-js 还在维护吗?

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