|
| 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. |
0 commit comments