Created
May 9, 2022 23:19
-
-
Save ilamanov/5d75336431e5bce762bd51a55fd7c694 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contract YourNFTContract is ERC721 { | |
bytes32 private _allowlistMerkleRoot; | |
function setMerkleRoot(bytes32 newRoot) external onlyOwner { | |
_allowlistMerkleRoot = newRoot; | |
} | |
modifier onlyIfValidMerkleProof(bytes32[] calldata proof) { | |
// leaf is just the address of the sender | |
bytes32 leaf = keccak256(abi.encodePacked(msg.sender)); | |
// compute hash | |
bytes32 computedHash = leaf; | |
for (uint256 i = 0; i < proof.length; i++) { | |
bytes32 proofElement = proof[i]; | |
if (computedHash <= proofElement) { | |
// Hash(current computed hash + current element of the proof) | |
computedHash = _efficientHash(computedHash, proofElement); | |
} else { | |
// Hash(current element of the proof + current computed hash) | |
computedHash = _efficientHash(proofElement, computedHash); | |
} | |
} | |
// compare the resulting hash to root | |
if (computedHash != _allowlistMerkleRoot) revert MerkleProofInvalid(); | |
_; | |
} | |
function allowlistedMint(bytes32[] calldata merkleProof) | |
external | |
payable | |
onlyIfValidMerkleProof(merkleProof) | |
{ | |
// your mint logic | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment