Skip to content

Commit

Permalink
Fix generate tree script
Browse files Browse the repository at this point in the history
  • Loading branch information
ScreamingHawk committed May 14, 2024
1 parent 604cd7f commit c57ee9f
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions scripts/generateMerkleTree.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { MerkleTree } from 'merkletreejs'
import { utils } from 'ethers'
import { BigNumberish, utils } from 'ethers'
import keccak256 from 'keccak256'

const generateTree = (elements: string[]) => {
const hashed = elements.map(e => utils.solidityKeccak256(['uint256'], [e]))
export type TreeElement = {
address: string
tokenId: BigNumberish
}

const generateTree = (elements: TreeElement[]) => {
const hashed = elements.map(e => getLeaf(e))

const merkleTree = new MerkleTree(hashed, keccak256, {
sort: true,
Expand All @@ -17,7 +22,13 @@ const generateTree = (elements: string[]) => {
}
}

const generateProof = (tree: MerkleTree, element: string) =>
tree.getHexProof(utils.solidityKeccak256(['uint256'], [element]))
const getLeaf = (element: TreeElement) =>
utils.solidityKeccak256(
['address', 'uint256'],
[element.address.toLowerCase(), element.tokenId],
)

const generateProof = (tree: MerkleTree, element: TreeElement) =>
tree.getHexProof(getLeaf(element))

export { generateTree, generateProof }
export { generateTree, generateProof, getLeaf }

0 comments on commit c57ee9f

Please sign in to comment.