You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The library also supports "simple" merkle trees, which are a simplified version of the standard ones. They are designed to be more flexible and accept arbitrary `bytes32` data as leaves. It keeps the same tree shape and internal pair hashing algorithm.
125
+
126
+
As opposed to standard trees, leaves are not double-hashed. Instead they are ABI encoded to `bytes32` and hashed in pairs inside the tree. This is useful to override the leaf hashing algorithm and use a different one prior to building the tree.
127
+
128
+
Users of tooling that produced trees without double leaf hashing can use this feature to build a representation of the tree in JavaScript. We recommend this approach exclusively for trees that are already built on-chain. Otherwise the standard tree may be a better fit.
const tree =SimpleMerkleTree.of([keccak256('Value 1'), keccak256('Value 2')]);
136
+
137
+
// (2)
138
+
// ...
139
+
```
140
+
141
+
1. Use a custom leaf hashing algorithm to produce `bytes32` values for the tree.
142
+
2. The Simple Merkle Tree share the same API as Standard Merkle Tree.
143
+
144
+
## Advanced usage
145
+
123
146
### Leaf Hash
124
147
125
-
From the last three points we get that the hash of a leaf in the tree with value `[addr, amount]` can be computed in Solidity as follows:
148
+
The Standard Merkle Tree uses an opinionated double leaf hashing algorithm. For example, a leaf in the tree with value `[addr, amount]` can be computed in Solidity as follows:
This is an opinionated design that we believe will offer the best out of the box experience for most users. We may introduce options for customization in the future based on user requests.
154
+
This is an opinionated design that we believe will offer the best out of the box experience for most users. However, there are advanced use case where a different leaf hashing algorithm may be needed. For those, the `SimpleMerkleTree` can be used to build a tree with custom leaf hashing.
132
155
133
156
### Leaf ordering
134
157
@@ -144,73 +167,117 @@ However, some trees are constructed iteratively from unsorted data, causing the
144
167
145
168
## API & Examples
146
169
170
+
> **Note**
171
+
> Consider reading the array of elements from a CSV file for easy interoperability with spreadsheets or other data processing pipelines.
172
+
173
+
> **Note**
174
+
> By default, leaves are sorted according to their hash. This is done so that multiproof generated by the library can more easily be verified onchain. This can be disabled using the optional third argument. See the [Leaf ordering](#leaf-ordering) section for more details.
const tree =StandardMerkleTree.of([[alice, '100'], [bob, '200']], ['address', 'uint'], options);
157
186
```
158
187
159
188
Creates a standard merkle tree out of an array of the elements in the tree, along with their types for ABI encoding. For documentation on the syntax of the types, including how to encode structs, refer to the documentation for Ethers.js's [`AbiCoder`](https://docs.ethers.org/v5/api/utils/abi/coder/#AbiCoder-encode).
160
189
161
-
> **Note**
162
-
> Consider reading the array of elements from a CSV file for easy interoperability with spreadsheets or other data processing pipelines.
163
-
164
-
> **Note**
165
-
> By default, leaves are sorted according to their hash. This is done so that multiproof generated by the library can more easily be verified onchain. This can be disabled using the optional third argument. See the [Leaf ordering](#leaf-ordering) section for more details.
Returns a boolean that is `true` when the multiproof verifies that all the values are contained in the tree given only the multiproof, merkle root, and leaf encoding.
Loads the tree from a description previously returned by `tree.dump`.
220
+
#### `SimpleMerkleTree.of`
221
+
222
+
```typescript
223
+
const tree =SimpleMerkleTree.of([hashFn('Value 1'), hashFn('Value 2')]);
224
+
```
225
+
226
+
The `hashFn` is a custom cryptographic leaf hashing algorithm that returns `bytes32` values. The tree will be built using these values as leaves. The function should be different to the internal hashing pair algorithm used by the tree.
|`sortLeaves`| Enable or disable sorted leaves. Sorting is strongly recommended for multiproofs. |`true`|
196
263
197
-
### `tree.root`
264
+
####`tree.root`
198
265
199
266
```typescript
200
267
console.log(tree.root);
201
268
```
202
269
203
270
The root of the tree is a commitment on the values of the tree. It can be published (e.g., in a smart contract) to later prove that its values are part of the tree.
Returns a description of the merkle tree for distribution. It contains all the necessary information to reproduce the tree, find the relevant leaves, and generate proofs. You should distribute this to users in a web application or command line interface so they can generate proofs for their leaves of interest.
212
279
213
-
### `tree.getProof`
280
+
####`tree.getProof`
214
281
215
282
```typescript
216
283
const proof =tree.getProof(i);
@@ -221,10 +288,10 @@ Returns a proof for the `i`th value in the tree. Indices refer to the position o
221
288
Also accepts a value instead of an index, but this will be less efficient. It will fail if the value is not found in the tree.
222
289
223
290
```typescript
224
-
const proof =tree.getProof([alice, '100']);
291
+
const proof =tree.getProof(value); // e.g. [alice, '100']
0 commit comments