-
Notifications
You must be signed in to change notification settings - Fork 0
/
merklegen.js
60 lines (47 loc) · 2.1 KB
/
merklegen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
console.log('merklegen.js loaded');
loginWithEth()
async function loginWithEth(){
if(window.ethereum){
await ethereum.request({ method: 'eth_requestAccounts' });
window.web3 = await new Web3(ethereum);
accountarray = await web3.eth.getAccounts();
account = accountarray[0];
console.log('Logged In')
GenerateStuff();
return(true)
}
else {
alert("No ETHER Wallet available")
}
}
let WhitelistMerkleTree;
let leafNodesWhitelist;
let rootHashWhitelist;
function GenerateStuff(){
//convert the whitelistAddresses to an array of addresses with the points number appended to the address end
let WhitelistAddresses = data2.map(address => web3.utils.encodePacked({value: address.address, type: 'address'}));
let ClaimantAddresses = data1.map(address => web3.utils.encodePacked({value: address.address + address.points, type: 'string'}));
WhitelistAddresses = WhitelistAddresses.concat(ClaimantAddresses);
console.log(WhitelistAddresses);
const leafNodesWhitelist = WhitelistAddresses.map(addr => web3.utils.keccak256(addr));
WhitelistMerkleTree = new MerkleTree(leafNodesWhitelist, keccak256, { sortPairs: true });
// Get the Merkle Root of the Merkle Tree
rootHashWhitelist = WhitelistMerkleTree.getRoot();
let rootHashWhitelistBytes32 = '0x' + WhitelistMerkleTree.getRoot().toString('hex');
console.log("Root Hash Whitelist: ", rootHashWhitelistBytes32);
}
// Function to generate hex proof for a claiming address
function GenerateHexProofWhitelist(claimingAddress, points) {
if(points > 0){
claimingAddress = web3.utils.encodePacked({value: claimingAddress + points, type: 'string'});
console.log(web3.utils.keccak256(claimingAddress));
}
claimingAddress = keccak256(web3.utils.encodePacked(claimingAddress));
const hexProof = WhitelistMerkleTree.getHexProof(claimingAddress);
const isAddressInTree = WhitelistMerkleTree.verify(hexProof, claimingAddress, rootHashWhitelist);
//get the lea
return {
hexProof: hexProof,
isAddressInTree: isAddressInTree
};
}