bitcoin merkle tree diagram

This commit is contained in:
Miguel Mota 2017-08-07 20:54:56 -07:00
parent 3cbbf31fa6
commit b94b2625ba
6 changed files with 13 additions and 4 deletions

View File

@ -14,6 +14,10 @@ Diagram of Invalid Merkle Tree Proofs
<img src="https://github.com/miguelmota/merkle-tree/blob/master/diagrams/merkle-tree-proof-fail.png?raw=true" alt="Merkle Tree Proof" width="420">
Diagram of Bitcoin Merkle Tree
<img src="https://github.com/miguelmota/merkle-tree/blob/master/diagrams/merkle-tree-proof-bitcoin.png?raw=true" alt="Merkle Tree Proof" width="420">
# Install
```bash
@ -64,7 +68,7 @@ Lonely leaf nodes are promoted to the next level up without being hashed again.
| leaves | <code>Array.&lt;Buffer&gt;</code> | Array of hashed leaves. Each leaf must be a Buffer. |
| hashAlgorithm | <code>function</code> | Algorithm used for hashing leaves and nodes |
| options | <code>Object</code> | Additional options |
| options.isBitcoinTree | <code>Boolean</code> | If set to `true`, constructs the Merkle Tree using the [Bitcoin Merkle Tree implementation](http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html). Enable it when you need to replicate Bitcoin constructed Merkle Trees. |
| options.isBitcoinTree | <code>Boolean</code> | If set to `true`, constructs the Merkle Tree using the [Bitcoin Merkle Tree implementation](http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html). Enable it when you need to replicate Bitcoin constructed Merkle Trees. In Bitcoin Merkle Trees, single nodes are combined with themselves, and each output hash is hashed again. |
**Example**
```js
@ -202,7 +206,7 @@ Lonely leaf nodes are promoted to the next level up without being hashed again.
| leaves | <code>Array.&lt;Buffer&gt;</code> | Array of hashed leaves. Each leaf must be a Buffer. |
| hashAlgorithm | <code>function</code> | Algorithm used for hashing leaves and nodes |
| options | <code>Object</code> | Additional options |
| options.isBitcoinTree | <code>Boolean</code> | If set to `true`, constructs the Merkle Tree using the [Bitcoin Merkle Tree implementation](http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html). Enable it when you need to replicate Bitcoin constructed Merkle Trees. |
| options.isBitcoinTree | <code>Boolean</code> | If set to `true`, constructs the Merkle Tree using the [Bitcoin Merkle Tree implementation](http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html). Enable it when you need to replicate Bitcoin constructed Merkle Trees. In Bitcoin Merkle Trees, single nodes are combined with themselves, and each output hash is hashed again. |
**Example**
```js
@ -335,6 +339,10 @@ Also, as is, this implementation is vulnerable to a forgery attack for an unbala
- [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)
- [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)
- [Where is Double hashing performed in Bitcoin?](https://bitcoin.stackexchange.com/questions/8443/where-is-double-hashing-performed-in-bitcoin)
# License
MIT

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

View File

@ -0,0 +1 @@
<mxfile userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" version="7.0.3" editor="www.draw.io" type="device"><diagram id="0ffe4643-5db2-c819-e50e-e9015c718b6a" name="Page-1">7Vxbc5s4FP41ntk+7A6SuD7GSVo/tDuZtjO7fZRBsdkQ8GDl1l+/EkgYJOxgjFJK3M609pEQ5tOnc9MRM3R5//wpx5v1lywiyQxa0fMMXc0gdJDN/uWCl1Jge1YpWOVxVIrATvAt/kmEUHZ7iCOybXSkWZbQeNMUhlmakpA2ZDjPs6dmt9ssad51g1dEE3wLcaJL/4kjui6lvmPt5AsSr9byzsASLUsc3q3y7CEV95tBdFv8KZvvsRxL9N+ucZQ91UToeoYu8yyj5af750uScGglbOV1H/e0Vr87JyntdAEsr3jEyYN49oXL/l/8wf6y0eYL+OGD+Kn0RcJDyTMbfb6m9wkTAPZxS/PsjlxmSZYXXZAb+mR5y1pu4ySpySNM/NuQyX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Yzl3ZLKpnNUJHAuOiXRYq8YFkQRFu/xB3JlkkT3sDWlq+MegZfvnp+YgEF3POpwl0l5A7gbUpd6qmFBRJ4X04Wbx+8hrdraNvV1oPvhS3wvpoq3tB5nd+2Ibz1qHnhCLyvp4o3kqWIB/B2zeAtPYXzDuB0dwCBjIxP3QLUBhrObXN+ozDgUJZj76TXXX9HPFvD9fc7kuONsheuEqxbPTmkZQnUgQbkENQ49Jngx3OmvEcKoMUEmUqUO3rEdtzSN5AF12xUu2WzVGsUJni7jUPFIIGuikEGr3XF4I5KL3hqLrJvdYmiFsxVlzh6hPpu6eXo9BrXBk2gbKv0tTrKOIExdrm6zXm37HJ1do3Lqam2f0+llzqQQX61lWi9U355Or+CUfGrqqc9lV/qQAb5dXa+Kn75Or/kluVYCBYo1aK9CaYMZJBgp5bBTIdgoG3Hb1zuvd/0yntXxwFlR0oLFAck2Kn1MRMn2Lgc/GqcUwNIqFSTIMsYw+QiODOsqrEac+ZSOWuM+jIMKVQ1mKPwzl6+ZJjXwjD5AoiRMAy4ylFJeXb91CRYVf9jgGFtfr5+1GcRzMRZH686djXYUZ/xn8YCUEkQtJ7G8nVmDfI2iHOi8qASQKNSAkpFW3WM92gdoNZemqsk9/RalvdKsN+hflwhBlKNQ+/iS3PBvn/2lA+V944sXakebEWqFesci6k6zNxhOFkJ2JFhu0k0QbKjq51EZdIQ5hCNyxyqCR8tHO/MJTXqMhfX+1Dj0jymIRuZCb+Q/I4RClrfc0I0iv26ApK9hDnmTVFKWb9QS3UnF7ZQZZC3yp1a5zUdCyGj2kYuZVyHpYGkhnJk6+hVrbybDEBjRWDu2QU5SLCRObmvKPuu/FL2LYAzlNFgX3cvOi27714mi67/Bw==</diagram></mxfile>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 52 KiB

View File

@ -1 +1 @@
<mxfile userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" version="7.0.3" editor="www.draw.io" type="device"><diagram id="3ca9cd9c-0c8a-eb2a-9f4f-0b165685525d" name="Page-1">7Vtdc5s4FP01zGwftoMkvvxYp0m9M+1OpsnM7j7KIGMajDwg56O/fiWQMEjEdgk0TJw8xNaVEOjco3ulg2yhi83jlxxv199oRFIL2tGjhT5bELrI4f+F4akyOL5dGeI8iSoT2Btukp9EGlWzXRKRotWQUZqyZNs2hjTLSMhaNpzn9KHdbEXT9l23OCaG4SbEqWn9J4nYurIGrr23L0gSr9WdgS1rlji8i3O6y+T9LIhW5V9VvcGqL9m+WOOIPjRM6NJCFzmlrPq2ebwgqYBWwVZdd/VMbf3cOcnYKRfA6oJ7nO6IeuLyudiTwqIcDRHtbQvNH9YJIzdbHIraB+58bluzTcpLgH8tWE7vyAVNaV5ejezyr65RaIq2qyRNGy0lTmiO0yTOuC3kYyC8cm4OSo7znuSMPDZMcpBfCN0Qlj/xJqrWl4yUhAxmVfGh4V3pknXDsUjasORTXHe8x5R/kbB2Q4wMiBce/1z8seCPPF/ADwbifEzsVFhPBlFglXCGf5IVmySKxA07PToA4h5oAQ4QNBGfmYjDARB3TMT9CnEkEHd6IR55S8/1OhGHYTgFxKHtH4c8GAdy14Tc7YOyFwZkuTJRjjAJVpNAGaFAQxkYKLsdoWQIlL0zQlnjsheYKMNxUPY7cqKXCkyLLc7491h8/15FlEBEFPeDasH7bjbq45xl4DpuV2gPQjKRQAPasR3a5hToyqZDOCcwpwB4mwlUrT0kyDPHwNgH42A8MzE2l4VvAmMAjoLsjrRIUXduoox6LUz82bIbZeJNI16A4CjKnjMSysBE2bHQ1dvMmtA7DjQaCWhzJ9lvbTL9oIGOR2bPHQllM0KQKCY3skhztqYxzXB6ubfO2xv4BvbkMWH/CvNHV5b+UzUZf7BGlSiqulqyEL1FuFiXXQNZuMaMeyUrLdB2Sg/jnH0Sogw3ZjQjynaViHGW3ZAs0lpwS6P+B2HsScpEeMcoN+3H+pXS7VE2mfKD4lFKVuwQMQTAB2mRkxSz5L4tHXV5WV56TRN+i0Z01Ja6wGl3UdBdHhJ5VVPL+dWOOOgxYUZHJenq8ZzGQ3OLbRLT8Glz2jdJ9AKvdVPY1mkXprgoklBjHjjk9gp1qykoHGXCkS2gsr2QMFo2hb7djy91NFF80SPUgHwx9YFX4suBwPZbuVRh216kTIRfdT+1wtGbYO2OkD0ewUxp5FwJ5nYQDE6KYDUPFC/6EgxpTK1D4wgE61KFzpNgIDAZhiZFMOB57Qjmw34Eq1OiIpgzHsFMZatLdlwEVqk7+kJ3DIbUHaf/gqN+oXFAFB7rnRIwRbH3+b+f/9NaIc+0/AJ65hfgaNPfG236K46+80u95WrSy50UvZBGC6RnhZPXLxpRa+KOwC9TCD1Xfvkmv7xJ8Qvo8UtPYCdvwPT4pUsFA/IL/hK/9i78baLQIV3TbimOL8qE/rSopGnVxhb8ZCrpW63x9vIdJ6b+yniB10P7G8nvOJ+gfZsT8XGdU7oqDKqZ692cFMlPvCwbCHdvxZOWz+7OLfezYNGO0aLSmDtIpS92mRCdxdI8TLL4tlSg/3QGWudqh9WUDxuUAV4HZ4ZY53YcnjrXPIHMyT2b1uRWz6P2Q47WxamT29e2y4oEI8ztdyH4AL2CSdELHYn4p7JLe18B3PEyh6kC31bwQvtvGpET0kSbUpJ/TUVEml5fDPHab/tUsamF+CPlCFMLXVTvgfmYRD7mnxkVB/ihHdGyIDywwSxcN1P4d3HMfXiXPJOwx3aI0/YHdF3DIfVCfnCPdImHGrAvO8dfn0o8fo7/2XOKA4CsxySvY2nUgbEuAPXC2FQAx2byK57wdNs4q2zVPH04EpeVyNta/oc0z8UPfaD9leDVxKA2cO1A/3mo1THm0eM4L+5/WFRl1/2Pt9Dl/w==</diagram></mxfile>
<mxfile userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" version="7.0.3" editor="www.draw.io" type="device"><diagram id="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OoCr9tFSXcsJuqqtpbzsx0J0FPCrY4q0jXjOY2H9hbbJqbl0/a0b5PoGV7rp7Br0i7OcVlmscE8cMjtNepOW1A4yoQjW0BteyZhjGwKQ/c8vjTRRPPFjFAD8sXWB16ILwcC2y/lUo1td5EyEX41/TQKx9kE63aE3PEIZksjb5Vgfg/B4KQI1vBA8+JcgiGDqU1oHIFgfarQ2yQYiGyGoUkRDARBN4KF8DyCNSlRE8wbj2C2stUnOy4jp9IdQ6k7RkPqjtN/wdG80DggCo/1TgnYotj7/N/P/2mtkGdGfgFn5hfgGdM/GG36a46+80u/5WrTy58UvZBBC2RmhZPXLwZRG+KOwC9bCH2r/AptfgWT4hcw45eZwE7egJnxy5QKBuQX/Cl+7V34y0ShQ7qm21Ecn5UJw2lRydCqrS34yVQyt1rj7eV7Tkz9WYiCqIfuV8JuBZ+g+50R+XHFKF2VFtXs9S4jZfYD31QNpLu38kmrZ/fnjv9JsmjHaVlrzD2kMhe7XIrOcmkeZ0X6vVKgf/cGWucah9W0D1uUAUEPZ4ZY5/YcnnqreQLZk3s2rcmtn0fvhzyji1Mnd2hslzUJRpjb70LwAXpFk6IXOhLxT2WX8b4C+ONlDlsF/l7DC92/aEJOSBNdSin+tRURZXp5MSTovu3TxbYWEo6UI2wtdFm/BxZjkvlYfBZUHuCHbkKrgvTABvN43U7h3+Qx9+Fd8kTCHtshXtcf0PcthzQL+cE90iceGsA+7xx/cyrx+Dn+J88pDgCyGZOCnqVRD8amAHQWxrYCODaTX/CEp9/FWWer9unDkbisRd7O8j+mjMkf+kD3C8GriUFt4dqD/tNQ62PMo8dxUdz/sKjOrvsfb6HL/wE=</diagram></mxfile>

View File

@ -14,7 +14,7 @@ class MerkleTree {
* @param {Object} options - Additional options
* @param {Boolean} options.isBitcoinTree - If set to `true`, constructs the Merkle
* Tree using the [Bitcoin Merkle Tree implementation](http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html). Enable it when you need
* to replicate Bitcoin constructed Merkle Trees.
* to replicate Bitcoin constructed Merkle Trees. In Bitcoin Merkle Trees, single nodes are combined with themselves, and each output hash is hashed again.
* @example
* const MerkleTree = require('m-tree')
* const crypto = require('crypto')