Skip to content

Commit b73bcb2

Browse files
Amxxernestognw
andauthored
Process and verify merkle proofs (and multiproof) with custom hash function (#4887)
Co-authored-by: ernestognw <[email protected]>
1 parent 4b33d32 commit b73bcb2

File tree

10 files changed

+772
-262
lines changed

10 files changed

+772
-262
lines changed

.changeset/spotty-queens-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': minor
3+
---
4+
5+
`MerkleProof`: Add variations of `verify`, `processProof`, `multiProofVerify` and `processMultiProof` (and equivalent calldata version) with support for custom hashing functions.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.20;
4+
5+
import {MerkleProof} from "../utils/cryptography/MerkleProof.sol";
6+
7+
// This could be a library, but then we would have to add it to the Stateless.sol mock for upgradeable tests
8+
abstract contract MerkleProofCustomHashMock {
9+
function customHash(bytes32 a, bytes32 b) internal pure returns (bytes32) {
10+
return a < b ? sha256(abi.encode(a, b)) : sha256(abi.encode(b, a));
11+
}
12+
13+
function verify(bytes32[] calldata proof, bytes32 root, bytes32 leaf) internal view returns (bool) {
14+
return MerkleProof.verify(proof, root, leaf, customHash);
15+
}
16+
17+
function processProof(bytes32[] calldata proof, bytes32 leaf) internal view returns (bytes32) {
18+
return MerkleProof.processProof(proof, leaf, customHash);
19+
}
20+
21+
function verifyCalldata(bytes32[] calldata proof, bytes32 root, bytes32 leaf) internal view returns (bool) {
22+
return MerkleProof.verifyCalldata(proof, root, leaf, customHash);
23+
}
24+
25+
function processProofCalldata(bytes32[] calldata proof, bytes32 leaf) internal view returns (bytes32) {
26+
return MerkleProof.processProofCalldata(proof, leaf, customHash);
27+
}
28+
29+
function multiProofVerify(
30+
bytes32[] calldata proof,
31+
bool[] calldata proofFlags,
32+
bytes32 root,
33+
bytes32[] calldata leaves
34+
) internal view returns (bool) {
35+
return MerkleProof.multiProofVerify(proof, proofFlags, root, leaves, customHash);
36+
}
37+
38+
function processMultiProof(
39+
bytes32[] calldata proof,
40+
bool[] calldata proofFlags,
41+
bytes32[] calldata leaves
42+
) internal view returns (bytes32) {
43+
return MerkleProof.processMultiProof(proof, proofFlags, leaves, customHash);
44+
}
45+
46+
function multiProofVerifyCalldata(
47+
bytes32[] calldata proof,
48+
bool[] calldata proofFlags,
49+
bytes32 root,
50+
bytes32[] calldata leaves
51+
) internal view returns (bool) {
52+
return MerkleProof.multiProofVerifyCalldata(proof, proofFlags, root, leaves, customHash);
53+
}
54+
55+
function processMultiProofCalldata(
56+
bytes32[] calldata proof,
57+
bool[] calldata proofFlags,
58+
bytes32[] calldata leaves
59+
) internal view returns (bytes32) {
60+
return MerkleProof.processMultiProofCalldata(proof, proofFlags, leaves, customHash);
61+
}
62+
}

0 commit comments

Comments
 (0)