-
Notifications
You must be signed in to change notification settings - Fork 0
/
merkle.mjs
56 lines (51 loc) · 1.56 KB
/
merkle.mjs
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
import { StandardMerkleTree } from "@openzeppelin/merkle-tree";
import fs from "fs";
import csv from "csv-parser";
const csvFilePath = "./airdrop.csv"; // Path to your CSV file
const searchAddress = "0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB"; // Address to search
// Function to read the CSV file
async function readCSV(filePath) {
return new Promise((resolve, reject) => {
const values = [];
fs.createReadStream(filePath)
.pipe(csv())
.on("data", (row) => {
values.push([row.address, row.amount]);
})
.on("end", () => {
resolve(values);
})
.on("error", reject);
});
}
// Function to generate the Merkle Tree
async function generateMerkleTree() {
try {
const values = await readCSV(csvFilePath);
const tree = StandardMerkleTree.of(values, ["address", "uint256"]);
console.log("Merkle Root:", tree.root);
fs.writeFileSync("tree.json", JSON.stringify(tree.dump()));
} catch (error) {
console.error("Error generating Merkle tree:", error);
}
}
// Function to generate proof for a given address
async function generateProof() {
try {
const tree = StandardMerkleTree.load(
JSON.parse(fs.readFileSync("tree.json", "utf8"))
);
for (const [i, v] of tree.entries()) {
if (v[0] === searchAddress) {
const proof = tree.getProof(i);
console.log("Value:", v);
console.log("Proof:", proof);
}
}
} catch (error) {
console.error("Error generating proof:", error);
}
}
// Execute the functions
await generateMerkleTree();
await generateProof();