-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerkleVerification.sol
36 lines (27 loc) · 1.11 KB
/
MerkleVerification.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import {MerkleProofLib} from "solmate/utils/MerkleProofLib.sol";
contract MerkleVerification {
error InvalidProof();
bytes32 public immutable merkleRoot;
constructor(bytes32 merkleRoot_) {
merkleRoot = merkleRoot_;
}
// Does nothing if caller provides valid merkle proof, otherwise reverts.
function airdrop(uint256 amount, bytes32[] calldata proof) external {
// See https://github.com/OpenZeppelin/merkle-tree#treeleafhash.
bytes32 leaf = keccak256(bytes.concat(keccak256(abi.encode(msg.sender, amount))));
bool ok = MerkleProofLib.verify({proof: proof, root: merkleRoot, leaf: leaf});
if (!ok) {
revert InvalidProof();
}
}
// Note that it's required to compute the leaf outside the verification
// contract for this attack to work.
function secondPreimageAttack(bytes32 leaf, bytes32[] calldata proof) external {
bool ok = MerkleProofLib.verify({proof: proof, root: merkleRoot, leaf: leaf});
if (!ok) {
revert InvalidProof();
}
}
}