Skip to content

Commit 4fa3ac6

Browse files
authored
✨ MerkleTreeLib (#1436)
1 parent 0c19df1 commit 4fa3ac6

File tree

5 files changed

+590
-0
lines changed

5 files changed

+590
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ utils
9191
├─ LibZip — "Library for compressing and decompressing bytes"
9292
├─ Lifebuoy — "Class that allows for rescue of ETH, ERC20, ERC721 tokens"
9393
├─ MerkleProofLib — "Library for verification of Merkle proofs"
94+
├─ MerkleTreeLib — "Library for generating Merkle trees"
9495
├─ MetadataReaderLib — "Library for reading contract metadata robustly"
9596
├─ MinHeapLib — "Library for managing a min-heap in storage or memory"
9697
├─ Multicallable — "Contract that enables a single call to call multiple methods on itself"

docs/utils/merkletreelib.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# MerkleTreeLib
2+
3+
Library for generating Merkle trees.
4+
5+
6+
7+
8+
9+
10+
<!-- customintro:start --><!-- customintro:end -->
11+
12+
## Custom Errors
13+
14+
### MerkleTreeLeafsEmpty()
15+
16+
```solidity
17+
error MerkleTreeLeafsEmpty()
18+
```
19+
20+
At least 1 leaf is required to build the tree.
21+
22+
### MerkleTreeOutOfBoundsAccess()
23+
24+
```solidity
25+
error MerkleTreeOutOfBoundsAccess()
26+
```
27+
28+
Attempt to access a node with an out-of-bounds index.
29+
Check if the tree has been built and has sufficient leafs and nodes.
30+
31+
### MerkleTreeInvalidLeafIndices()
32+
33+
```solidity
34+
error MerkleTreeInvalidLeafIndices()
35+
```
36+
37+
Leaf indices for multi proof must be strictly ascending and not empty.
38+
39+
## Merkle Tree Operations
40+
41+
### build(bytes32[])
42+
43+
```solidity
44+
function build(bytes32[] memory leafs)
45+
internal
46+
pure
47+
returns (bytes32[] memory result)
48+
```
49+
50+
Builds and return a complete Merkle tree.
51+
To make it a full Merkle tree, use `build(pad(leafs))`.
52+
53+
### root(bytes32[])
54+
55+
```solidity
56+
function root(bytes32[] memory t) internal pure returns (bytes32 result)
57+
```
58+
59+
Returns the root.
60+
61+
### numLeafs(bytes32[])
62+
63+
```solidity
64+
function numLeafs(bytes32[] memory t) internal pure returns (uint256)
65+
```
66+
67+
Returns the number of leafs.
68+
69+
### numInternalNodes(bytes32[])
70+
71+
```solidity
72+
function numInternalNodes(bytes32[] memory t)
73+
internal
74+
pure
75+
returns (uint256)
76+
```
77+
78+
Returns the number of internal nodes.
79+
80+
### leaf(bytes32[],uint256)
81+
82+
```solidity
83+
function leaf(bytes32[] memory t, uint256 leafIndex)
84+
internal
85+
pure
86+
returns (bytes32 result)
87+
```
88+
89+
Returns the leaf at `leafIndex`.
90+
91+
### leafProof(bytes32[],uint256)
92+
93+
```solidity
94+
function leafProof(bytes32[] memory t, uint256 leafIndex)
95+
internal
96+
pure
97+
returns (bytes32[] memory result)
98+
```
99+
100+
Returns the proof for the leaf at `leafIndex`.
101+
102+
### nodeProof(bytes32[],uint256)
103+
104+
```solidity
105+
function nodeProof(bytes32[] memory t, uint256 nodeIndex)
106+
internal
107+
pure
108+
returns (bytes32[] memory result)
109+
```
110+
111+
Returns the proof for the node at `nodeIndex`.
112+
This function can be used to prove the existence of internal nodes.
113+
114+
### leafsMultiProof(bytes32[],uint256[])
115+
116+
```solidity
117+
function leafsMultiProof(bytes32[] memory t, uint256[] memory leafIndices)
118+
internal
119+
pure
120+
returns (bytes32[] memory proof, bool[] memory flags)
121+
```
122+
123+
Returns proof and corresponding flags for multiple leafs.
124+
The `leafIndices` must be non-empty and sorted in strictly ascending order.
125+
126+
### pad(bytes32[],bytes32)
127+
128+
```solidity
129+
function pad(bytes32[] memory leafs, bytes32 defaultFill)
130+
internal
131+
pure
132+
returns (bytes32[] memory result)
133+
```
134+
135+
Returns a copy of leafs, with the length padded to a power of 2.

src/Milady.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import "./utils/LibString.sol";
5353
import "./utils/LibZip.sol";
5454
import "./utils/Lifebuoy.sol";
5555
import "./utils/MerkleProofLib.sol";
56+
import "./utils/MerkleTreeLib.sol";
5657
import "./utils/MetadataReaderLib.sol";
5758
import "./utils/MinHeapLib.sol";
5859
import "./utils/Multicallable.sol";

0 commit comments

Comments
 (0)