@osmura/merkletreejs 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT license
2
+
3
+ Copyright (C) 2015 Miguel Mota
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
+ of the Software, and to permit persons to whom the Software is furnished to do
10
+ so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,232 @@
1
+ <h3 align="center">
2
+ <br />
3
+ <img src="https://user-images.githubusercontent.com/4885186/193118010-2a9f5129-6232-42bd-8efe-dfb29753508e.png" alt="merkletree.js logo" width="600" />
4
+ <br />
5
+ <br />
6
+ <br />
7
+ </h3>
8
+
9
+ # MerkleTree.js
10
+
11
+ > Construct [Merkle Trees](https://en.wikipedia.org/wiki/Merkle_tree) and verify proofs in JavaScript.
12
+
13
+ [![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/miguelmota/merkletreejs/master/LICENSE)
14
+ [![Documentation](https://img.shields.io/badge/documentation-latest-blue.svg)](https://github.com/miguelmota/merkletreejs/tree/master/docs)
15
+ [![NPM version](https://badge.fury.io/js/merkletreejs.svg)](http://badge.fury.io/js/merkletreejs)
16
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](#contributing)
17
+
18
+ ## Contents
19
+
20
+ - [Install](#install)
21
+ - [Example](#example)
22
+ - [Getting started](#Getting-started)
23
+ - [Diagrams](#diagrams)
24
+ - [Documentation](#documentation)
25
+ - [Test](#test)
26
+ - [FAQ](#faq)
27
+ - [Notes](#notes)
28
+ - [Resources](#resources)
29
+ - [Contributing](#contributing)
30
+ - [License](#license)
31
+
32
+ ## Install
33
+
34
+ From [NPM](https://www.npmjs.com/package/merkletreejs):
35
+
36
+ ```bash
37
+ npm install merkletreejs
38
+ ```
39
+
40
+ Import as ES6 module
41
+
42
+ ```js
43
+ import { MerkleTree } from 'merkletreejs'
44
+ ```
45
+
46
+ Import as path import
47
+
48
+ ```js
49
+ import { MerkleTree } from 'merkletreejs/MerkleTree'
50
+ ```
51
+
52
+ Import as CommonJs
53
+
54
+ ```js
55
+ const { MerkleTree } = require('merkletreejs')
56
+ ```
57
+
58
+ ### CDN
59
+
60
+ Available on [jsDelivr](https://www.jsdelivr.com/) CDN:
61
+
62
+ ```html
63
+ <script src="https://cdn.jsdelivr.net/npm/merkletreejs@latest/merkletree.js"></script>
64
+ ```
65
+
66
+ The exported classes will be available on `window` object, e.g. `window.MerkleTree`
67
+
68
+ ## Example
69
+
70
+ [https://lab.miguelmota.com/merkletreejs](https://lab.miguelmota.com/merkletreejs)
71
+
72
+ ## Getting started
73
+
74
+ Construct tree, generate proof, and verify proof:
75
+
76
+ ```js
77
+ const { MerkleTree } = require('merkletreejs')
78
+ const SHA256 = require('crypto-js/sha256')
79
+
80
+ const leaves = ['a', 'b', 'c'].map(x => SHA256(x))
81
+ const tree = new MerkleTree(leaves, SHA256)
82
+ const root = tree.getRoot().toString('hex')
83
+ const leaf = SHA256('a')
84
+ const proof = tree.getProof(leaf)
85
+ console.log(tree.verify(proof, leaf, root)) // true
86
+
87
+
88
+ const badLeaves = ['a', 'x', 'c'].map(x => SHA256(x))
89
+ const badTree = new MerkleTree(badLeaves, SHA256)
90
+ const badLeaf = SHA256('x')
91
+ const badProof = badTree.getProof(badLeaf)
92
+ console.log(badTree.verify(badProof, badLeaf, root)) // false
93
+ ```
94
+
95
+ Print tree to console:
96
+
97
+ ```js
98
+ console.log(tree.toString())
99
+ ```
100
+
101
+ Output:
102
+
103
+ ```bash
104
+ └─ 7075152d03a5cd92104887b476862778ec0c87be5c2fa1c0a90f87c49fad6eff
105
+ ├─ e5a01fee14e0ed5c48714f22180f25ad8365b53f9779f79dc4a3d7e93963f94a
106
+ │ ├─ ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
107
+ │ └─ 3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d
108
+ └─ 2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6
109
+ └─ 2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6
110
+ ```
111
+
112
+ ## Diagrams
113
+
114
+ ▾ Visualization of Merkle Tree
115
+
116
+ <img src="https://user-images.githubusercontent.com/168240/43616375-15330c32-9671-11e8-9057-6e61c312c856.png" alt="Merkle Tree" width="500">
117
+
118
+ ▾ Visualization of Merkle Tree Proof
119
+
120
+ <img src="https://user-images.githubusercontent.com/168240/204968384-dbd16f5b-415c-4cc6-b993-5bbd7599ec8b.png" alt="Merkle Tree Proof" width="420">
121
+
122
+ ▾ Visualization of Invalid Merkle Tree Proofs
123
+
124
+ <img src="https://user-images.githubusercontent.com/168240/204968414-fefedb52-d27f-4b14-bf70-e3f96a50b6a3.png" alt="Merkle Tree Proof" width="420">
125
+
126
+ ▾ Visualization of Bitcoin Merkle Tree
127
+
128
+ <img src="https://user-images.githubusercontent.com/168240/43616417-46d3293e-9671-11e8-81c3-8cdf7f8ddd77.png" alt="Merkle Tree Proof" width="420">
129
+
130
+ ## Documentation
131
+
132
+ See [documentation](docs/classes/_src_merkletree_.merkletree.md) (under [docs/](docs/))
133
+
134
+ ## Test
135
+
136
+ ```bash
137
+ npm test
138
+ ```
139
+
140
+ ## FAQ
141
+
142
+ - Q: How do you verify merkle proofs in Solidity?
143
+ - 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.
144
+
145
+ - Q: How do you verify merkle [multiproofs](https://github.com/ethereum/eth2.0-specs/blob/dev/ssz/merkle-proofs.md#merkle-multiproofs) in Solidity?
146
+ - 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.
147
+
148
+ - Q: Is there an NFT whitelist example in Solidity?
149
+ - 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.
150
+
151
+ - Q: What other types of merkle trees are supported?
152
+
153
+ - Besides standard [`MerkleTree`](./README-MerkleTree.md), there's these implementation classes available:
154
+ - [`MerkleMountainRange`](./README-MerkleMountainRange.md)
155
+ - [`MerkleSumTree`](./README-MerkleSumTree.md)
156
+ - [`IncrementalMerkleTree`](./README-IncrementalMerkleTree.md)
157
+ - [`MerkleRadixTree`](./README-MerkleRadixTree.md)
158
+ - [`UnifiedBinaryTree`](./README-UnifiedBinaryTree.md) (EIP-7864)
159
+
160
+ Example import of other classes:
161
+
162
+ ```js
163
+ import { MerkleMountainRange } from 'merkletreejs/MerkleMountainRange'
164
+ ```
165
+
166
+ Note: For Merkle Patricia Tree, see [`@ethereumjs/trie`](https://www.npmjs.com/package/@ethereumjs/trie).
167
+
168
+ - Q: How do I hash a JSON object?
169
+
170
+ - See [`https://www.npmjs.com/package/json-stable-stringify`](https://www.npmjs.com/package/json-stable-stringify) for deterministic stringifying of JSON objects.
171
+
172
+ - Q: Is there a CLI version of this library?
173
+
174
+ - Yes, see [merkletreejs-cli](https://github.com/miguelmota/merkletreejs-cli).
175
+
176
+ - Q: Is there a way to visualize the merkle trees in the browser?
177
+
178
+ - Yes, see [example ui](https://lab.miguelmota.com/merkletreejs) and [merkletree-viz](https://github.com/miguelmota/merkletree-viz).
179
+
180
+ ## Notes
181
+
182
+ As 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)`.
183
+
184
+ Also, 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).
185
+
186
+ Please 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).
187
+
188
+ ### Disclaimer
189
+
190
+ This library was created for my own purposes and is provided as-is. Use at your own risk.
191
+
192
+ ## Resources
193
+
194
+ - [Bitcoin mining the hard way: the algorithms, protocols, and bytes](http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html)
195
+
196
+ - [Bitcoin Talk - Merkle Trees](https://bitcointalk.org/index.php?topic=403231.msg9054025#msg9054025)
197
+
198
+ - [How Log Proofs Work](https://www.certificate-transparency.org/log-proofs-work)
199
+
200
+ - [Raiden Merkle Tree Implementation](https://github.com/raiden-network/raiden/blob/f9cf12571891cdf54feb4667cd2fffcb3d5daa89/raiden/mtree.py)
201
+
202
+ - [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)
203
+
204
+ - [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)
205
+
206
+ - [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)
207
+
208
+ - [Where is Double hashing performed in Bitcoin?](https://bitcoin.stackexchange.com/questions/8443/where-is-double-hashing-performed-in-bitcoin)
209
+
210
+ - [Compact Merkle Multiproofs](https://arxiv.org/pdf/2002.07648.pdf)
211
+
212
+ - [Eth 2.0 specs - Merkle Multiproofs](https://github.com/ethereum/eth2.0-specs/blob/dev/ssz/merkle-proofs.md#merkle-multiproofs)
213
+
214
+ - [What is complete binary tree?](https://xlinux.nist.gov/dads/HTML/completeBinaryTree.html)
215
+
216
+ - [What is perfect binary tree?](https://xlinux.nist.gov/dads/HTML/perfectBinaryTree.html)
217
+
218
+ - [EIP-7864: Ethereum state using a unified binary tree](https://eips.ethereum.org/EIPS/eip-7864)
219
+
220
+ ## Contributing
221
+
222
+ Pull requests are welcome!
223
+
224
+ For contributions please create a new branch and submit a pull request for review.
225
+
226
+ _Many thanks to all the [contributors](https://github.com/miguelmota/merkletreejs/graphs/contributors) that made this library better._
227
+
228
+ ## License
229
+
230
+ Released under the [MIT](./LICENSE) license.
231
+
232
+ © [Miguel Mota](https://github.com/miguelmota)
package/dist/Base.d.ts ADDED
@@ -0,0 +1,212 @@
1
+ /// <reference types="node" />
2
+ export declare class Base {
3
+ /**
4
+ * print
5
+ * @desc Prints out a visual representation of the merkle tree.
6
+ * @example
7
+ *```js
8
+ *tree.print()
9
+ *```
10
+ */
11
+ print(): void;
12
+ /**
13
+ * bufferIndexOf
14
+ * @desc Returns the first index of which given buffer is found in array.
15
+ * @param {Buffer[]} haystack - Array of buffers.
16
+ * @param {Buffer} needle - Buffer to find.
17
+ * @return {Number} - Index number
18
+ *
19
+ * @example
20
+ * ```js
21
+ *const index = tree.bufferIndexOf(haystack, needle)
22
+ *```
23
+ */
24
+ protected bufferIndexOf(array: Buffer[], element: Buffer, isSorted?: boolean): number;
25
+ /**
26
+ * binarySearch
27
+ * @desc Returns the first index of which given item is found in array using binary search.
28
+ * @param {Buffer[]} array - Array of items.
29
+ * @param {Buffer} element - Item to find.
30
+ * @param {Function} compareFunction
31
+ * @return {Number} - Index number
32
+ *
33
+ * @example
34
+ * ```js
35
+ *const index = MerkleTree.binarySearch(array, element, Buffer.compare)
36
+ *```
37
+ */
38
+ static binarySearch(array: Buffer[], element: Buffer, compareFunction: (a: unknown, b: unknown) => number): number;
39
+ /**
40
+ * binarySearch
41
+ * @desc Returns the first index of which given item is found in array using binary search.
42
+ * @param {Buffer[]} array - Array of items.
43
+ * @param {Buffer} element - Item to find.
44
+ * @param {Function} compareFunction
45
+ * @return {Number} - Index number
46
+ *
47
+ * @example
48
+ * ```js
49
+ *const index = tree.binarySearch(array, element, Buffer.compare)
50
+ *```
51
+ */
52
+ binarySearch(array: Buffer[], element: Buffer, compareFunction: (a: unknown, b: unknown) => number): number;
53
+ /**
54
+ * linearSearch
55
+ * @desc Returns the first index of which given item is found in array using linear search.
56
+ * @param {Buffer[]} array - Array of items.
57
+ * @param {Buffer} element - Item to find.
58
+ * @param {Function} eqChecker
59
+ * @return {Number} - Index number
60
+ *
61
+ * @example
62
+ * ```js
63
+ *const index = MerkleTree.linearSearch(array, element, (a, b) => a === b)
64
+ *```
65
+ */
66
+ static linearSearch(array: Buffer[], element: Buffer, eqChecker: (a: unknown, b: unknown) => boolean): number;
67
+ /**
68
+ * linearSearch
69
+ * @desc Returns the first index of which given item is found in array using linear search.
70
+ * @param {Buffer[]} array - Array of items.
71
+ * @param {Buffer} element - Item to find.
72
+ * @param {Function} eqChecker
73
+ * @return {Number} - Index number
74
+ *
75
+ * @example
76
+ * ```js
77
+ *const index = tree.linearSearch(array, element, (a, b) => a === b)
78
+ *```
79
+ */
80
+ linearSearch(array: Buffer[], element: Buffer, eqChecker: (a: unknown, b: unknown) => boolean): number;
81
+ /**
82
+ * bufferify
83
+ * @desc Returns a buffer type for the given value.
84
+ * @param {String|Number|Object|Buffer|ArrayBuffer} value
85
+ * @return {Buffer}
86
+ *
87
+ * @example
88
+ * ```js
89
+ *const buf = MerkleTree.bufferify('0x1234')
90
+ *```
91
+ */
92
+ static bufferify(value: any): Buffer;
93
+ /**
94
+ * bufferifyFn
95
+ * @desc Returns a function that will bufferify the return value.
96
+ * @param {Function}
97
+ * @return {Function}
98
+ *
99
+ * @example
100
+ * ```js
101
+ *const fn = MerkleTree.bufferifyFn((value) => sha256(value))
102
+ *```
103
+ */
104
+ static bufferifyFn(f: any): any;
105
+ bigNumberify(value: any): BigInt;
106
+ static bigNumberify(value: any): BigInt;
107
+ /**
108
+ * isHexString
109
+ * @desc Returns true if value is a hex string.
110
+ * @param {String} value
111
+ * @return {Boolean}
112
+ *
113
+ * @example
114
+ * ```js
115
+ *console.log(MerkleTree.isHexString('0x1234'))
116
+ *```
117
+ */
118
+ static isHexString(v: string): boolean;
119
+ /**
120
+ * print
121
+ * @desc Prints out a visual representation of the given merkle tree.
122
+ * @param {Object} tree - Merkle tree instance.
123
+ * @return {String}
124
+ * @example
125
+ *```js
126
+ *MerkleTree.print(tree)
127
+ *```
128
+ */
129
+ static print(tree: any): void;
130
+ /**
131
+ * bufferToHex
132
+ * @desc Returns a hex string with 0x prefix for given buffer.
133
+ * @param {Buffer} value
134
+ * @return {String}
135
+ * @example
136
+ *```js
137
+ *const hexStr = tree.bufferToHex(Buffer.from('A'))
138
+ *```
139
+ */
140
+ bufferToHex(value: Buffer, withPrefix?: boolean): string;
141
+ /**
142
+ * bufferToHex
143
+ * @desc Returns a hex string with 0x prefix for given buffer.
144
+ * @param {Buffer} value
145
+ * @return {String}
146
+ * @example
147
+ *```js
148
+ *const hexStr = MerkleTree.bufferToHex(Buffer.from('A'))
149
+ *```
150
+ */
151
+ static bufferToHex(value: Buffer, withPrefix?: boolean): string;
152
+ /**
153
+ * bufferify
154
+ * @desc Returns a buffer type for the given value.
155
+ * @param {String|Number|Object|Buffer} value
156
+ * @return {Buffer}
157
+ *
158
+ * @example
159
+ * ```js
160
+ *const buf = tree.bufferify('0x1234')
161
+ *```
162
+ */
163
+ bufferify(value: any): Buffer;
164
+ /**
165
+ * bufferifyFn
166
+ * @desc Returns a function that will bufferify the return value.
167
+ * @param {Function}
168
+ * @return {Function}
169
+ *
170
+ * @example
171
+ * ```js
172
+ *const fn = tree.bufferifyFn((value) => sha256(value))
173
+ *```
174
+ */
175
+ bufferifyFn(f: any): any;
176
+ /**
177
+ * isHexString
178
+ * @desc Returns true if value is a hex string.
179
+ * @param {String} value
180
+ * @return {Boolean}
181
+ *
182
+ * @example
183
+ * ```js
184
+ *console.log(MerkleTree.isHexString('0x1234'))
185
+ *```
186
+ */
187
+ protected isHexString(value: string): boolean;
188
+ /**
189
+ * log2
190
+ * @desc Returns the log2 of number.
191
+ * @param {Number} value
192
+ * @return {Number}
193
+ */
194
+ protected log2(n: number): number;
195
+ /**
196
+ * zip
197
+ * @desc Returns true if value is a hex string.
198
+ * @param {String[]|Number[]|Buffer[]} a - first array
199
+ * @param {String[]|Number[]|Buffer[]} b - second array
200
+ * @return {String[][]|Number[][]|Buffer[][]}
201
+ *
202
+ * @example
203
+ * ```js
204
+ *const zipped = tree.zip(['a', 'b'],['A', 'B'])
205
+ *console.log(zipped) // [ [ 'a', 'A' ], [ 'b', 'B' ] ]
206
+ *```
207
+ */
208
+ protected zip(a: any[], b: any[]): any[][];
209
+ static hexZeroPad(hexStr: string, length: number): string;
210
+ bufferArrayIncludes(bufferArray: Buffer[], targetBuffer: Buffer): boolean;
211
+ }
212
+ export default Base;