{"id":13632366,"url":"https://github.com/merkletreejs/merkletreejs","last_synced_at":"2025-05-14T15:07:18.710Z","repository":{"id":37840034,"uuid":"98014822","full_name":"merkletreejs/merkletreejs","owner":"merkletreejs","description":"🌱 Construct Merkle Trees and verify proofs in JavaScript. By @miguelmota","archived":false,"fork":false,"pushed_at":"2025-04-19T01:47:59.000Z","size":1220,"stargazers_count":1206,"open_issues_count":25,"forks_count":219,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-05-14T10:56:39.750Z","etag":null,"topics":["bitcoin","blockchain","ethereum","javascript","merkle","merkle-mountain-range","merkle-multiproof","merkle-proof","merkle-root","merkle-tree","merkle-tree-diagram","merkletreejs","mmr","multiproof","nodejs","protocol","smart-contracts","soli","solidity","verify-proofs"],"latest_commit_sha":null,"homepage":"https://merkletree.js.org","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/merkletreejs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["miguelmota"],"patreon":"miguelmota"}},"created_at":"2017-07-22T07:25:26.000Z","updated_at":"2025-05-06T10:42:22.000Z","dependencies_parsed_at":"2023-01-23T12:01:18.516Z","dependency_job_id":"b7522e1e-4c85-469a-827e-7f745b09cb6f","html_url":"https://github.com/merkletreejs/merkletreejs","commit_stats":{"total_commits":230,"total_committers":24,"mean_commits":9.583333333333334,"dds":"0.19999999999999996","last_synced_commit":"dd10cd0205040b338a6a44101cf091ed08628028"},"previous_names":["miguelmota/merkletreejs","miguelmota/merkle-tree"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/merkletreejs%2Fmerkletreejs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/merkletreejs%2Fmerkletreejs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/merkletreejs%2Fmerkletreejs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/merkletreejs%2Fmerkletreejs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/merkletreejs","download_url":"https://codeload.github.com/merkletreejs/merkletreejs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254169599,"owners_count":22026213,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bitcoin","blockchain","ethereum","javascript","merkle","merkle-mountain-range","merkle-multiproof","merkle-proof","merkle-root","merkle-tree","merkle-tree-diagram","merkletreejs","mmr","multiproof","nodejs","protocol","smart-contracts","soli","solidity","verify-proofs"],"created_at":"2024-08-01T22:03:01.558Z","updated_at":"2025-05-14T15:07:18.691Z","avatar_url":"https://github.com/merkletreejs.png","language":"TypeScript","funding_links":["https://github.com/sponsors/miguelmota","https://patreon.com/miguelmota"],"categories":["TypeScript"],"sub_categories":[],"readme":"\u003ch3 align=\"center\"\u003e\n  \u003cbr /\u003e\n  \u003cimg src=\"https://user-images.githubusercontent.com/4885186/193118010-2a9f5129-6232-42bd-8efe-dfb29753508e.png\" alt=\"merkletree.js logo\" width=\"600\" /\u003e\n  \u003cbr /\u003e\n  \u003cbr /\u003e\n  \u003cbr /\u003e\n\u003c/h3\u003e\n\n# MerkleTree.js\n\n\u003e Construct [Merkle Trees](https://en.wikipedia.org/wiki/Merkle_tree) and verify proofs in JavaScript.\n\n[![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/miguelmota/merkletreejs/master/LICENSE)\n[![Documentation](https://img.shields.io/badge/documentation-latest-blue.svg)](https://github.com/miguelmota/merkletreejs/tree/master/docs)\n[![NPM version](https://badge.fury.io/js/merkletreejs.svg)](http://badge.fury.io/js/merkletreejs)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](#contributing)\n\n## Contents\n\n- [Install](#install)\n- [Example](#example)\n- [Getting started](#Getting-started)\n- [Diagrams](#diagrams)\n- [Documentation](#documentation)\n- [Test](#test)\n- [FAQ](#faq)\n- [Notes](#notes)\n- [Resources](#resources)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Install\n\nFrom [NPM](https://www.npmjs.com/package/merkletreejs):\n\n```bash\nnpm install merkletreejs\n```\n\nImport as ES6 module\n\n```js\nimport { MerkleTree } from 'merkletreejs'\n```\n\nImport as path import\n\n```js\nimport { MerkleTree } from 'merkletreejs/MerkleTree'\n```\n\nImport as CommonJs\n\n```js\nconst { MerkleTree } = require('merkletreejs')\n```\n\n### CDN\n\nAvailable on [jsDelivr](https://www.jsdelivr.com/) CDN:\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/merkletreejs@latest/merkletree.js\"\u003e\u003c/script\u003e\n```\n\nThe exported classes will be available on `window` object, e.g. `window.MerkleTree`\n\n## Example\n\n[https://lab.miguelmota.com/merkletreejs](https://lab.miguelmota.com/merkletreejs)\n\n## Getting started\n\nConstruct tree, generate proof, and verify proof:\n\n```js\nconst { MerkleTree } = require('merkletreejs')\nconst SHA256 = require('crypto-js/sha256')\n\nconst leaves = ['a', 'b', 'c'].map(x =\u003e SHA256(x))\nconst tree = new MerkleTree(leaves, SHA256)\nconst root = tree.getRoot().toString('hex')\nconst leaf = SHA256('a')\nconst proof = tree.getProof(leaf)\nconsole.log(tree.verify(proof, leaf, root)) // true\n\n\nconst badLeaves = ['a', 'x', 'c'].map(x =\u003e SHA256(x))\nconst badTree = new MerkleTree(badLeaves, SHA256)\nconst badLeaf = SHA256('x')\nconst badProof = badTree.getProof(badLeaf)\nconsole.log(badTree.verify(badProof, badLeaf, root)) // false\n```\n\nPrint tree to console:\n\n```js\nconsole.log(tree.toString())\n```\n\nOutput:\n\n```bash\n└─ 7075152d03a5cd92104887b476862778ec0c87be5c2fa1c0a90f87c49fad6eff\n   ├─ e5a01fee14e0ed5c48714f22180f25ad8365b53f9779f79dc4a3d7e93963f94a\n   │  ├─ ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb\n   │  └─ 3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d\n   └─ 2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6\n      └─ 2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6\n```\n\n## Diagrams\n\n▾ Visualization of Merkle Tree\n\n\u003cimg src=\"https://user-images.githubusercontent.com/168240/43616375-15330c32-9671-11e8-9057-6e61c312c856.png\" alt=\"Merkle Tree\" width=\"500\"\u003e\n\n▾ Visualization of Merkle Tree Proof\n\n\u003cimg src=\"https://user-images.githubusercontent.com/168240/204968384-dbd16f5b-415c-4cc6-b993-5bbd7599ec8b.png\" alt=\"Merkle Tree Proof\" width=\"420\"\u003e\n\n▾ Visualization of Invalid Merkle Tree Proofs\n\n\u003cimg src=\"https://user-images.githubusercontent.com/168240/204968414-fefedb52-d27f-4b14-bf70-e3f96a50b6a3.png\" alt=\"Merkle Tree Proof\" width=\"420\"\u003e\n\n▾ Visualization of Bitcoin Merkle Tree\n\n\u003cimg src=\"https://user-images.githubusercontent.com/168240/43616417-46d3293e-9671-11e8-81c3-8cdf7f8ddd77.png\" alt=\"Merkle Tree Proof\" width=\"420\"\u003e\n\n## Documentation\n\nSee [documentation](docs/classes/_src_merkletree_.merkletree.md) (under [docs/](docs/))\n\n## Test\n\n```bash\nnpm test\n```\n\n## FAQ\n\n- Q: How do you verify merkle proofs in Solidity?\n  - A: Check out the example repo [merkletreejs-solidity](https://github.com/miguelmota/merkletreejs-solidity) on how to generate merkle proofs with this library and verify them in Solidity.\n\n- Q: How do you verify merkle [multiproofs](https://github.com/ethereum/eth2.0-specs/blob/dev/ssz/merkle-proofs.md#merkle-multiproofs) in Solidity?\n  - A: Check out the example repo [merkletreejs-multiproof-solidity](https://github.com/miguelmota/merkletreejs-multiproof-solidity) on how to generate merkle multiproofs with this library and verify them in Solidity.\n\n- Q: Is there an NFT whitelist example in Solidity?\n  - A: Check out the example repo [merkletreejs-nft-whitelist](https://github.com/miguelmota/merkletreejs-nft-whitelist) on how to generate merkle root of whitelisted accounts and merkle proofs with this library and verify them in Solidity.\n\n- Q: What other types of merkle trees are supported?\n\n  - Besides standard `MerkleTree`, there's these implementation classes available:\n    - `MerkleMountainRange`\n    - `MerkleSumTree`\n    - `IncrementalMerkleTree`\n    - `MerkleRadixTree`\n    - `UnifiedBinaryTree` (EIP-7864)\n\n    Example import of other classes:\n\n    ```js\n    import { MerkleMountainRange } from 'merkletreejs/MerkleMountainRange'\n    ```\n\n    Note: For Merkle Patricia Tree, see [`@ethereumjs/trie`](https://www.npmjs.com/package/@ethereumjs/trie).\n\n- Q: How do I hash a JSON object?\n\n- See [`https://www.npmjs.com/package/json-stable-stringify`](https://www.npmjs.com/package/json-stable-stringify) for deterministic stringifying of JSON objects.\n\n- Q: Is there a CLI version of this library?\n\n  - Yes, see [merkletreejs-cli](https://github.com/miguelmota/merkletreejs-cli).\n\n- Q: Is there a way to visualize the merkle trees in the browser?\n\n  - Yes, see [example ui](https://lab.miguelmota.com/merkletreejs) and [merkletree-viz](https://github.com/miguelmota/merkletree-viz).\n\n## Notes\n\nAs is, this implementation is vulnerable to a [second pre-image attack](https://en.wikipedia.org/wiki/Merkle_tree#Second_preimage_attack). Use a difference hashing function for leaves and nodes, so that `H(x) != H'(x)`.\n\nAlso, as is, this implementation is vulnerable to a forgery attack for an unbalanced tree, where the last leaf node can be duplicated to create an artificial balanced tree, resulting in the same Merkle root hash. Do not accept unbalanced tree to prevent this. More info [here](https://bitcointalk.org/?topic=102395).\n\nPlease use the library [`@openzeppelin/merkle-tree`](https://github.com/OpenZeppelin/merkle-tree) if you're integrating with OpenZeppelin contracts or using multiproofs. There are known issues with the current multiproof implementation as pointed out in [issues](https://github.com/merkletreejs/merkletreejs/issues/63).\n\n### Disclaimer\n\nThis library was created for my own purposes and is provided as-is. Use at your own risk.\n\n## Resources\n\n- [Bitcoin mining the hard way: the algorithms, protocols, and bytes](http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html)\n\n- [Bitcoin Talk - Merkle Trees](https://bitcointalk.org/index.php?topic=403231.msg9054025#msg9054025)\n\n- [How Log Proofs Work](https://www.certificate-transparency.org/log-proofs-work)\n\n- [Raiden Merkle Tree Implementation](https://github.com/raiden-network/raiden/blob/f9cf12571891cdf54feb4667cd2fffcb3d5daa89/raiden/mtree.py)\n\n- [Why aren't Solidity sha3 hashes not matching what other sha3 libraries produce?](https://ethereum.stackexchange.com/questions/559/why-arent-solidity-sha3-hashes-not-matching-what-other-sha3-libraries-produce)\n\n- [What is the purpose of using different hash functions for the leaves and internals of a hash tree?](https://crypto.stackexchange.com/questions/2106/what-is-the-purpose-of-using-different-hash-functions-for-the-leaves-and-interna)\n\n- [Why is the full Merkle path needed to verify a transaction?](https://bitcoin.stackexchange.com/questions/50674/why-is-the-full-merkle-path-needed-to-verify-a-transaction)\n\n- [Where is Double hashing performed in Bitcoin?](https://bitcoin.stackexchange.com/questions/8443/where-is-double-hashing-performed-in-bitcoin)\n\n- [Compact Merkle Multiproofs](https://arxiv.org/pdf/2002.07648.pdf)\n\n- [Eth 2.0 specs - Merkle Multiproofs](https://github.com/ethereum/eth2.0-specs/blob/dev/ssz/merkle-proofs.md#merkle-multiproofs)\n\n- [What is complete binary tree?](https://xlinux.nist.gov/dads/HTML/completeBinaryTree.html)\n\n- [What is perfect binary tree?](https://xlinux.nist.gov/dads/HTML/perfectBinaryTree.html)\n\n- [EIP-7864: Ethereum state using a unified binary tree](https://eips.ethereum.org/EIPS/eip-7864)\n\n## Contributing\n\nPull requests are welcome!\n\nFor contributions please create a new branch and submit a pull request for review.\n\n_Many thanks to all the [contributors](https://github.com/miguelmota/merkletreejs/graphs/contributors) that made this library better._\n\n## License\n\nReleased under the [MIT](./LICENSE) license.\n\n© [Miguel Mota](https://github.com/miguelmota)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmerkletreejs%2Fmerkletreejs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmerkletreejs%2Fmerkletreejs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmerkletreejs%2Fmerkletreejs/lists"}