This Hardhat project implements a Merkle tree to verify the presence of a value in the whitelist. The contract stores only the root of the Merkle tree, and users can prove their inclusion by providing a Merkle proof and a token amount.
The smart contract WhitelistVerifier
implements the following functions:
constructor(bytes32 _root)
: initializes the Merkle tree with the given root hash.verifyInWhitelist(bytes32[] memory _proof, address _address, uint256 _tokenAmount)
: verifies that_address
with_tokenAmount
is included in the Merkle tree using the provided_proof
.
Note that the contract only stores the root of the Merkle tree, and the actual values are computed using the hash of the leaf node.
The test file check.js
tests the functionality of the contract. It performs the following steps:
- Fetch a list of signers from Hardhat.
- Create a list of nodes and encode them as byte strings using
ethers.utils.defaultAbiCoder.encode()
. - Use the
MerkleTree
class frommerkletreejs
to create a Merkle tree with the given nodes and hash function (keccak256
). - Get the root of the Merkle tree using
getHexRoot()
function and deploy theWhitelistVerifier
contract with it. - Call
verifyInWhitelist()
function for each address in the Merkle tree to ensure that the contract is able to verify the presence of the value using the provided Merkle proof. - If all the tests pass, the Merkle tree is working as expected.
To use this project, first clone the repository:
git clone https://github.com/Akkii4/Merkle_Whitelist-Verify.git
Then install the required dependencies:
npm install
To run the tests, use the following command:
npx hardhat test