npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

tiny-merkletree

v1.0.0

Published

Merkle tree implementation in TypeScript

Downloads

3

Readme

merkel-tree

A Merkle Tree implementation written in Typescript.

What are Merkle Trees?

Merkle trees are a hash-based data structure used to efficiently summarize and verify large sets of data. In Bitcoin, each block's transactions are organized into a Merkle tree, where individual transaction hashes are repeatedly paired and hashed until a single "Merkle root" remains, ensuring that any alteration to a transaction invalidates the entire tree. This allows Bitcoin nodes to verify transactions without storing the entire blockchain, improving security and performance.

In Bitcoin, it's used to summarize all transactions in a block with a single hash: the Merkle Root.

Example

Let’s say we have 4 transactions in a block:

    Tx0: "Alice → Bob : 1 BTC"
    Tx1: "Charlie → Dave : 2 BTC"
    Tx2: "Eve → Frank : 1.5 BTC"
    Tx3: "George → Hannah : 3 BTC"

Steps

  1. Hash each transaction
    H0 = hash(Tx0) => "a1"
    H1 = hash(Tx1) => "b2"
    H2 = hash(Tx2) => "c3"
    H3 = hash(Tx3) => "d4"
  1. Pair and hash the pairs
    H01 = hash(H0 + H1) = hash("a1b2") = "e5"
    H23 = hash(H2 + H3) = hash("c3d4") = "f6"
  1. Hash again to get the Merkle Root Merkle Root = hash(H01 + H23) = hash("e5f6"rzez) = "r7"

This root hash, r7, uniquely summarizes the entire set of transactions.

The last hash was copied and added to the end of the list. This is needed to be able to concatenate it with itself and hash it, since we hash in pair and if the hash list length is odd, then we copy it and add it to the end of the list to make the hash list even. Each of the tree levels is called a hash list.

Why is it secure?

If any transaction changes, its hash changes, which affects all parent nodes up to the root.

Representation

merkel path

If we are interested in verifying if the hash 41b6 is actually included in the Merkle root (da07) then we would only need to have the hashes in color:

[41b6, a8c0, 1013, b5fb, f94a]

With 41b6, we need a8c0 to reconstruct 8fca. With 8fca, we need 1013 to reconstruct 87fd. With 87fd, we need b5fb to reconstruct 7460. With 7460 we need f94a to reconstruct da07, the Merkle root.

With this information, we notice a pattern and we can determine that we only need about log(n) number of hashes to be able to check if a hash belongs to a certain Merkle root, which is really efficient, instead of having to hash them all or to have all the hashes to verify that.

Real world example

Blockchains like Bitcoin use Merkle trees to check if a transaction belongs to the Merkle root in a block header. Bitcoin, for example, converts the concatenated hash to binary first, before hashing it. Bitcoin also uses a double sha256 hash, something like:

const result = sha256(sha256(binaryOfTheConcatenatedPairOfHashes))

Code

We are going to use sha256 to hash the concatenation of our hashes. So, we will have 64 character hashes.

const merkleRoot = generateMerkleRoot(HASH_VALUE);
console.log('\x1b[36m%s\x1b[0m', 'merkleRoot:', merkleRoot);

Resources

  • https://github.com/akbng/merkle-tree
  • https://github.com/bitcoin/bitcoin/blob/master/src/consensus/merkle.cpp