
{"id":164776,"date":"2026-05-13T05:07:48","date_gmt":"2026-05-13T05:07:48","guid":{"rendered":"https:\/\/mycryptomania.com\/?p=164776"},"modified":"2026-05-13T05:07:48","modified_gmt":"2026-05-13T05:07:48","slug":"implementing-merkle-trees-in-typescript-part-ii","status":"publish","type":"post","link":"https:\/\/mycryptomania.com\/?p=164776","title":{"rendered":"Implementing Merkle Trees in TypeScript \u2014 Part II"},"content":{"rendered":"<p>This is part II of the multi part series on Merkle trees. To get an understanding of what merkle trees are and why they\u2019re used, you can read the previous\u00a0<a href=\"https:\/\/medium.com\/@chaitanyarout6\/implementing-merkle-trees-a-comparative-guide-in-typescript-vs-rust-part-i-5b09bb8c34e8\">article<\/a>.<\/p>\n<p>Gemini Generated<\/p>\n<h3>Implementing a Merkle\u00a0Tree<\/h3>\n<p>Merkle Tree implementation basically consists of 3 main parts\u200a\u2014\u200acalculating root, making a proof for an element and finally verifying the presence of that element in that list. We will implement these parts in both Typescript and\u00a0Rust.<\/p>\n<h3>1. Calculating the Merkle\u00a0Root<\/h3>\n<p>The root is a single hash that represents the entire dataset. To calculate it, you recursively hash pairs of nodes until only one hash remains. For simplicity we will use arrays or vectors for representing the dataset. It follows the following pseudocode.<\/p>\n<p>WHILE length of leaves &gt; 1:<br \/> \/\/ Ensure even number for pairing<br \/> IF length of leaves is ODD:<br \/> DUPLICATE the last element and APPEND it to leaves<\/p>\n<p> SET next_level to an empty list<\/p>\n<p> FOR index FROM 0 TO length of leaves &#8211; 1 STEP 2:<br \/> CONCATENATE leaves[index] and leaves[index + 1]<br \/> HASH the concatenated string<br \/> ADD result to next_level<\/p>\n<p> SET leaves to next_level<\/p>\n<p>RETURN leaves[0] (The Root)<\/p>\n<h4>Typescript<\/h4>\n<p><a href=\"https:\/\/medium.com\/media\/2f4cb001f5360f449abd983528e02dbe\/href\">https:\/\/medium.com\/media\/2f4cb001f5360f449abd983528e02dbe\/href<\/a><\/p>\n<h3>2. Building a Merkle\u00a0Proof<\/h3>\n<p>A proof (or \u201cMerkle path\u201d) is the set of \u201csibling\u201d hashes required to reconstruct the path from a specific leaf to the root. You don\u2019t need the whole tree; you just need the hashes of the nodes you <em>didn\u2019t<\/em>\u00a0have.<\/p>\n<h4>PseudoCode<\/h4>\n<p>FUNCTION build_proof(leaves, target_element):<br \/> FIND target_index of target_element in leaves<br \/> IF target_index not found, THROW error<\/p>\n<p> MAP each leaf through hash_function()<br \/> SET proof to an empty list<br \/>WHILE length of leaves &gt; 1:<br \/> IF length of leaves is ODD:<br \/> DUPLICATE the last element and APPEND it to leaves<\/p>\n<p> \/\/ Identify sibling and its relative position<br \/> IF target_index is ODD:<br \/> ADD {sibling: leaves[target_index &#8211; 1], position: &#8220;left&#8221;} to proof<br \/> ELSE:<br \/> ADD {sibling: leaves[target_index + 1], position: &#8220;right&#8221;} to proof<\/p>\n<p> \/\/ Move to next level<br \/> SET next_level to empty list<br \/> FOR index FROM 0 TO length of leaves &#8211; 1 STEP 2:<br \/> HASH(leaves[index] + leaves[index + 1]) and ADD to next_level<\/p>\n<p> SET leaves to next_level<br \/> UPDATE target_index to (target_index \/ 2, rounded down)<\/p>\n<p> RETURN proof<\/p>\n<h4>Typescript<\/h4>\n<p><a href=\"https:\/\/medium.com\/media\/60721867a352cb44f0455b8d3378e3ac\/href\">https:\/\/medium.com\/media\/60721867a352cb44f0455b8d3378e3ac\/href<\/a><\/p>\n<h3>3. Verifying a Merkle\u00a0Proof<\/h3>\n<p>Verification is where the magic happens. A user provides a leaf and a proof. You hash them together repeatedly; if the final result matches the known <strong>Merkle Root<\/strong>, the leaf is\u00a0valid.<\/p>\n<h4>PseudoCode<\/h4>\n<p>FUNCTION verify_merkle_proof(merkle_root, leaf_value, proof):<br \/> SET current_hash to HASH(leaf_value)<\/p>\n<p> FOR EACH element IN proof:<br \/> IF element.position is \u201cleft\u201d:<br \/> SET current_hash to HASH(element.sibling + current_hash)<br \/> ELSE:<br \/> SET current_hash to HASH(current_hash + element.sibling)<\/p>\n<p> IF current_hash EQUALS merkle_root:<br \/> RETURN true<br \/> ELSE:<br \/> RETURN false<\/p>\n<h4>Typescript<\/h4>\n<p><a href=\"https:\/\/medium.com\/media\/01222541770b001633858d39a7b9402d\/href\">https:\/\/medium.com\/media\/01222541770b001633858d39a7b9402d\/href<\/a><\/p>\n<h3>Conclusion<\/h3>\n<p>By implementing a Merkle Tree in code, we\u2019ve shifted from looking at data as a simple list to seeing it as a verifiable, cryptographic structure. We\u2019ve explored how to condense large datasets into a single <strong>Merkle Root<\/strong>, generate <strong>Proofs<\/strong> that allow for efficient verification, and ensure data integrity without needing to share the entire\u00a0tree.<\/p>\n<p>Whether you are building a whitelist for an NFT drop, optimizing state sync in a blockchain client, or just curious about how Git handles file changes, the Merkle Tree is an indispensable tool in your engineering toolkit. Now that you have the core logic down, try experimenting with different hashing algorithms or scaling the tree to handle thousands of leaves to see how the performance holds\u00a0up!<\/p>\n<p><a href=\"https:\/\/medium.com\/coinmonks\/implementing-merkle-trees-in-typescript-part-ii-71d1b88f9627\">Implementing Merkle Trees in TypeScript \u2014 Part II<\/a> was originally published in <a href=\"https:\/\/medium.com\/coinmonks\">Coinmonks<\/a> on Medium, where people are continuing the conversation by highlighting and responding to this story.<\/p>","protected":false},"excerpt":{"rendered":"<p>This is part II of the multi part series on Merkle trees. To get an understanding of what merkle trees are and why they\u2019re used, you can read the previous\u00a0article. Gemini Generated Implementing a Merkle\u00a0Tree Merkle Tree implementation basically consists of 3 main parts\u200a\u2014\u200acalculating root, making a proof for an element and finally verifying the [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":164777,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-164776","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-interesting"],"_links":{"self":[{"href":"https:\/\/mycryptomania.com\/index.php?rest_route=\/wp\/v2\/posts\/164776"}],"collection":[{"href":"https:\/\/mycryptomania.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mycryptomania.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/mycryptomania.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=164776"}],"version-history":[{"count":0,"href":"https:\/\/mycryptomania.com\/index.php?rest_route=\/wp\/v2\/posts\/164776\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mycryptomania.com\/index.php?rest_route=\/wp\/v2\/media\/164777"}],"wp:attachment":[{"href":"https:\/\/mycryptomania.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=164776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mycryptomania.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=164776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mycryptomania.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=164776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}