-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1.createTree.ts
44 lines (41 loc) · 2.1 KB
/
1.createTree.ts
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
// Create & test a Merkle tree hashed with Poseidon
// Coded with Starknet.js v6.0.0-beta.11
// launch with npx ts-node src/scripts/merkleTree/airdropSJS6Sepolia/1.createTree.ts
import { Account, ec, hash, Provider, num, json, Contract, encode, Signature, typedData, uint256, RpcProvider, RPC } from "starknet";
import * as Merkle from "starknet-merkle-tree";
import * as dotenv from "dotenv";
import fs from "fs";
dotenv.config();
async function main() {
const list = json.parse(fs.readFileSync("./src/scripts/merkleTree/airdropSJS6Sepolia/listAddressesSepolia.json").toString("ascii"));
// each leaf is a string array : address, number of token
const airdrop: Merkle.InputForMerkle[] = list.list;
const tree1 = Merkle.StarknetMerkleTree.create(airdrop, Merkle.HashType.Poseidon);
console.log("root =", tree1.root); // for smartcontract constructor
fs.writeFileSync('./src/scripts/merkleTree/airdropSJS6Sepolia/treeListAddressSepolia.json', JSON.stringify(tree1.dump(),undefined,2));
const tree = Merkle.StarknetMerkleTree.load(
JSON.parse(fs.readFileSync('./src/scripts/merkleTree/airdropSJS6Sepolia/treeListAddressSepolia.json', 'ascii'))
);
// process.exit(5);
const accountAddress = '0x0324f3401Bb5CB9B7b396BC6065faD94D32cd7628a39C89EeaD456f077f27c0e';
const indexAddress = tree.dump().values.findIndex((leaf) => leaf.value[0] == accountAddress);
if (indexAddress === -1) {
throw new Error("address not found in the list.");
}
const inpData = tree.getInputData(indexAddress);
console.log("Leaf #", indexAddress, "contains =", inpData);
const leafHash = Merkle.StarknetMerkleTree.leafHash(inpData, Merkle.HashType.Poseidon);
console.log("leafHash =", leafHash);
const proof = tree.getProof(indexAddress);
console.log("corresponding proof =", proof);
const isValid = tree.verify(inpData, proof);
console.log("This proof is", isValid);
// console.log(tree.render());
console.log("✅ test completed.");
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});