This project hasn't been updated in a long time, so it no longer appears on the homepage, categories, or search results — the data is preserved and this page still works normally.
Back to Trending
RepositoryJavaScript
miguelmota

miguelmota/merkletreejs-nft-whitelist

Solidity NFT whitelist contract example using MerkleTree.js for constructing merkle root and merkle proofs.

71stars
13forks
0issues
0views

NOTICE: The solidity code is unaudited and is for demo purposes. Use at your own risk!

MerkleTree.js Solidity NFT Whitelist example

Allow NFT minting only to whitelisted accounts by verifying merkle proof in Solidity contract. Merkle root and merkle proofs are constructed using MerkleTree.js.

Example

contracts/WhitelistSale.sol

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract WhitelistSale is ERC721 {
  bytes32 immutable public merkleRoot;
  uint256 public nextTokenId;
  mapping(address => bool) public claimed;

  constructor(bytes32 _merkleRoot) ERC721("ExampleNFT", "NFT") {
    merkleRoot = _merkleRoot;
  }

  function toBytes32(address addr) pure internal returns (bytes32) {
    return bytes32(uint256(uint160(addr)));
  }

  function mint(bytes32[] calldata merkleProof) public payable {
    require(claimed[msg.sender] == false, "already claimed");
    claimed[msg.sender] = true;
    require(MerkleProof.verify(merkleProof, merkleRoot, toBytes32(msg.sender)) == true, "invalid merkle proof");
    nextTokenId++;
    _mint(msg.sender, nextTokenId);
  }
}

test/whitelistSale.js

const { expect, use } = require('chai')
const { ethers } = require('hardhat')
const { MerkleTree } = require('merkletreejs')
const { keccak256 } = ethers.utils

use(require('chai-as-promised'))

describe('WhitelistSale', function () {
  it('allow only whitelisted accounts to mint', async () => {
    const accounts = await hre.ethers.getSigners()
    const whitelisted = accounts.slice(0, 5)
    const notWhitelisted = accounts.slice(5, 10)

    const padBuffer = (addr) => {
      return Buffer.from(addr.substr(2).padStart(32*2, 0), 'hex')
    }

    const leaves = whitelisted.map(account => padBuffer(account.address))
    const tree = new MerkleTree(leaves, keccak256, { sort: true })
    const merkleRoot = tree.getHexRoot()

    const WhitelistSale = await ethers.getContractFactory('WhitelistSale')
    const whitelistSale = await WhitelistSale.deploy(merkleRoot)
    await whitelistSale.deployed()

    const merkleProof = tree.getHexProof(padBuffer(whitelisted[0].address))
    const invalidMerkleProof = tree.getHexProof(padBuffer(notWhitelisted[0].address))

    await expect(whitelistSale.mint(merkleProof)).to.not.be.rejected
    await expect(whitelistSale.mint(merkleProof)).to.be.rejectedWith('already claimed')
    await expect(whitelistSale.connect(notWhitelisted[0]).mint(invalidMerkleProof)).to.be.rejectedWith('invalid merkle proof')
  })
})

Development

Install dependencies

npm install

Run test

npm test

License

MIT

Similar Projects

Discover other similar projects and AI models.

RepositorySolidity
AmazingAng/WTF-Solidity

WTF Solidity 极简入门教程,供小白们使用。Now supports English! 官网: https://wtf.academy

14.1k
Oct 11, 2026
Repository
ethereumbook/ethereumbook

Mastering Ethereum: 2nd Edition, by Andreas M. Antonopoulos, Gavin Wood, Carlo Parisi, Alessandro Mazza, Niccolò Pozzolini

21.5k
Oct 12, 2026
RepositoryJavaScript
OffcierCia/DeFi-Developer-Road-Map

DeFi Developer roadmap is a curated Developer handbook which includes a list of the best tools for DApps development, resources and references!

10.9k
Oct 20, 2026
RepositoryJavaScript
miguelmota/merkletreejs-solidity

Construct merkle trees with MerkleTree.js and verify merkle proofs in Solidity.

128
Mar 13, 2022
RepositoryTypeScript
NomicFoundation/hardhat

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software.

8.5k
Oct 24, 2026
RepositoryHTML
ConsenSysDiligence/smart-contract-best-practices

A guide to smart contract security best practices

7.6k
Mar 28, 2025
RepositoryGo
txaty/go-merkletree

Go Merkle Tree. High performance, Supporting parallel run, OpenZeppelin sorting pairs.

129
Nov 25, 2023
RepositoryRust
unionlabs/union

The trust-minimized, zero-knowledge bridging protocol, designed for censorship resistance, extremely high security, and usage in decentralized finance.

73.7k
Jul 25, 2026
RepositoryGo
ethereum/go-ethereum

Go implementation of the Ethereum protocol

51.4k
Oct 22, 2026