lite-merkle-patricia-tree
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

lite-merkle-patricia-tree

lite-merkle-patricia-tree Version

A simplified and synchronous implementation of the Ethereum modified Merkle Patricia Tree from ethereumjs-monorepo.

This browser friendly implementation is for one-time use cases of data verification.

For example: put serialized BlockReceipts in the Trie and build an inclusion proof with findPath, and that's it, data is not persisted.

It replaces the database and async logic with a simple Map to temporarily store trie nodes.

Installation

yarn add lite-merkle-patricia-tree
"dependencies": {
  "lite-merkle-patricia-tree": "^1.0.0",
}

Usage

import { Trie } from 'lite-merkle-patricia-tree'
import { Proof } from 'eth-object'

const block = await web3.eth.getBlock()
const blockReceipts = await Promise.all(
  block.transactions.map(t => web3.eth.getTransactionReceipt(t))
)

// Build the receipts Trie
const trie = new Trie()
blockReceipts.forEach(receipt => {
  const path = rlp.encode(receipt.transactionIndex)
  const serializedReceipt = Receipt.fromWeb3(receipt).serialize()
  trie.put(path, serializedReceipt)
})
if (trie.root.toString('hex') !== block.receiptsRoot.slice(2)) {
  throw new Error('Failed to build receipts trie root.')
}

// Prove a receipt inclusion inside the Trie
const stack = trie.findPath(rlp.encode(transactionIndex)).stack.map(
  node => { return { raw: node.raw() } }
)
const receiptProof = Proof.fromStack(stack),

Readme

Keywords

none

Package Sidebar

Install

npm i lite-merkle-patricia-tree

Weekly Downloads

87

Version

1.0.1

License

(MIT AND Apache-2.0)

Unpacked Size

62 kB

Total Files

23

Last publish

Collaborators

  • paouvrard
  • aurora-is-near-org