Skip to content

Instantly share code, notes, and snippets.

@kiknaio
Last active June 17, 2024 09:05
Show Gist options
  • Save kiknaio/46315a5c232176fd9325c1d95c8a1267 to your computer and use it in GitHub Desktop.
Save kiknaio/46315a5c232176fd9325c1d95c8a1267 to your computer and use it in GitHub Desktop.
import { MerkleTree } from 'merkletreejs';
import keccak256 from 'keccak256';
let whiltelistAddresses = [
'0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5',
'0x814fae9f487206471b6b0d713cd51a2d35980000',
'0x82e7d9afccb8f6e06812c4f2c3e1852e655b8244',
'0xf5784072a102089505eae832c85b58abe32c5e22',
];
const leafNodes = whiltelistAddresses.map(x => keccak256(x));
const tree = new MerkleTree(leafNodes, keccak256, { sortPairs: true });
const root = tree.getRoot().toString('hex');
/*
This is an example how one can verify if the piece of data is part of a Merkle tree
*/
const leafToVerify = keccak256('0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5');
const proof = tree.getProof(leafToVerify);
console.log(tree.verify(proof, leafToVerify, root)); // true
const result = tree.getRoot();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment