JSPM

  • Created
  • Published
  • Downloads 309
  • Score
    100M100P100Q104417F
  • License Apache 2.0

Lib to generate and validate a merkle tree of content hashes

Package Exports

  • @dcl/content-hash-tree
  • @dcl/content-hash-tree/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@dcl/content-hash-tree) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@dcl/content-hash-tree

NPM version Install Size

Idea from @uniswap/merkle-distributor

Local Development

The following assumes the use of node@>=10.

Install Dependencies

npm ci

Lib

Generate a tree

import { generateTree } from '@dcl/content-hash-tree'

const items = [
  { urn: 'urn:item:1', contentHash: 'hash1' },
  { urn: 'urn:item:2', contentHash: 'hash2' },
  { urn: 'urn:item:3', contentHash: 'hash3' }
]

const tree = generateTree(items)

Verify whether a contnet hash is part of the tree or not

import { verifyProof } from '@dcl/content-hash-tree'

const items = [
  { urn: 'urn:item:1', contentHash: 'hash1' },
  { urn: 'urn:item:2', contentHash: 'hash2' },
  { urn: 'urn:item:3', contentHash: 'hash3' }
]
const proof = tree.getProof(0, items[0].urn, items[0].contentHash)
const root = tree.getHexRoot()

const isPartOfTheTree = verifyProof(
  0,
  items[0].urn,
  items[0].contentHash,
  proof,
  root
)

CLI (Benchmark)

Generate a tree

npm run generate-merkle-root:data

~10k items: time to completion 1.049s. Json size: 18mb ~100k items: time to completion 10.049s. Json size: 155mb

This will generate a proofs.json file in the root of the project.

Verify a single proof of a tree with ~100k leafs

npm run verify-merkle-root:proof

Time to completion 9.294ms. Json size: 4kb