Created
August 14, 2022 05:01
-
-
Save geeknees/cd819e1008aa339b863c53ff7b2e1218 to your computer and use it in GitHub Desktop.
whitelistMarkleTree
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
const { MerkleTree } = require("merkletreejs"); | |
const keccak256 = require("keccak256"); | |
// List of 7 public Ethereum addresses | |
let addresses = [ | |
"0x...", | |
"0x1..", | |
"0x2..", | |
"0x3..", | |
"0x4..", | |
"0x5..", | |
"0x6..", | |
// ... | |
]; | |
// Hash addresses to get the leaves | |
let leaves = addresses.map((addr) => keccak256(addr)); | |
// Create tree | |
let merkleTree = new MerkleTree(leaves, keccak256, { sortPairs: true }); | |
// Get root | |
let rootHash = merkleTree.getRoot().toString("hex"); | |
console.log(rootHash); | |
console.log("---------"); | |
// Pretty-print tree | |
console.log(merkleTree.toString()); | |
// 'Serverside' code | |
let address = addresses[0]; | |
let hashedAddress = keccak256(address); | |
let proof = merkleTree.getHexProof(hashedAddress); | |
console.log(proof); | |
// Check proof | |
let v = merkleTree.verify(proof, hashedAddress, rootHash); | |
console.log(v); // returns true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment