Here is my js code of generating hex proof for solidity verification
const express = require('express')
const { MerkleTree } = require('merkletreejs');
const { bufferToHex } = require('ethereumjs-util');
const keccak256 = require('keccak256');
const url = require('url');
const cors = require("cors");
const app = express();
app.use(cors())
//1_000_000_000_000e18
//10000000000000000000000000000
let whitelist = [
{addr: "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4", amount: 20000000000000000000000000}, //10_000_000_000e18
{addr: "0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2", amount: 20000000000000000000000000},
]
const allLower = whitelist.map(item => item)
const leafNodes = allLower.map(item => keccak256(item.addr, item.amount))
console.log('leafNodes: ', leafNodes)
const merkleTree = new MerkleTree(leafNodes, keccak256, { sortPairs: true })
function getProof(addr, amount) {
let index = allLower.findIndex(item => item.addr === addr && item.amount === amount);
const hexProof = merkleTree.getHexProof(leafNodes[index])
console.log('hexProof: ', hexProof, typeof hexProof)
return hexProof
}
and my solidity verification function takes 2 inputs which are amount(uint256) and merkleProof (byte32). I kept getting invalid merkleProof error. I am guessing it should be the way I am using js keccak256 is wrong as it should only take one paramemters for input but I am not sure. Can some one helps please
Here is solidity merkleproof verification code
function claimTokens(uint256 amount, bytes32[] calldata merkleProof) public {
bytes32 leaf = keccak256(abi.encodePacked(msg.sender, amount));
bool valid = MerkleProof.verify(merkleProof, merkleRoot, leaf);
require(valid, "valid proof required.");
require(!claimed[msg.sender], "Tokens already claimed.");
claimed[msg.sender] = true;
emit Claim(msg.sender, amount);
_transfer(address(this), msg.sender, amount);
}