-
Notifications
You must be signed in to change notification settings - Fork 22
feat: generate merkle tree #826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1058812
fix: server resetting data
ctrlc03 3bf46b4
chore: add license
ctrlc03 e4dcafd
feat: get round data from crisp server
ctrlc03 766cd21
feat: generate merkle tree
ctrlc03 815d911
feat: generate voter's merkle tree locally and proof
ctrlc03 f053f61
chore: handle hex and add tests
ctrlc03 564bf00
chore: add erc20votes abi
ctrlc03 f4e67f2
chore: remove redundant import
ctrlc03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
| // | ||
| // This file is provided WITHOUT ANY WARRANTY; | ||
| // without even the implied warranty of MERCHANTABILITY | ||
| // or FITNESS FOR A PARTICULAR PURPOSE. | ||
|
|
||
| import { poseidon2 } from 'poseidon-lite' | ||
| import { LeanIMT } from '@zk-kit/lean-imt' | ||
|
|
||
| import type { IMerkleProof } from './types' | ||
|
|
||
| /** | ||
| * Hash a leaf node for the Merkle tree | ||
| * @param address The voter's address | ||
| * @param balance The voter's balance | ||
| * @returns The hashed leaf as a bigint | ||
| */ | ||
| export const hashLeaf = (address: string, balance: string): bigint => { | ||
| return poseidon2([address, balance]) | ||
| } | ||
|
ctrlc03 marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Generate a new LeanIMT with the leaves provided | ||
| * @param leaves The leaves of the Merkle tree | ||
| * @returns the generated Merkle tree | ||
| */ | ||
| export const generateMerkleTree = (leaves: bigint[]): LeanIMT => { | ||
| return new LeanIMT((a, b) => poseidon2([a, b]), leaves) | ||
| } | ||
|
|
||
| /** | ||
| * Generate a Merkle proof for a given address to prove inclusion in the voters' list | ||
| * @param threshold The minimum balance required to be eligible | ||
| * @param balance The voter's balance | ||
| * @param address The voter's address | ||
| * @param leaves The leaves of the Merkle tree | ||
| */ | ||
| export const generateMerkleProof = (threshold: number, balance: number, address: string, leaves: bigint[]): IMerkleProof => { | ||
| if (balance < threshold) { | ||
| throw new Error('Balance is below the threshold') | ||
| } | ||
|
|
||
| const leaf = hashLeaf(address, balance.toString()) | ||
|
|
||
| const index = leaves.findIndex((l) => l === leaf) | ||
|
|
||
| if (index === -1) { | ||
| throw new Error('Leaf not found in the tree') | ||
| } | ||
|
|
||
| const tree = generateMerkleTree(leaves) | ||
|
|
||
| const proof = tree.generateProof(index) | ||
|
|
||
| return { | ||
| leaf, | ||
| index, | ||
| proof, | ||
| } | ||
| } | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
| // | ||
| // This file is provided WITHOUT ANY WARRANTY; | ||
| // without even the implied warranty of MERCHANTABILITY | ||
| // or FITNESS FOR A PARTICULAR PURPOSE. | ||
|
|
||
| import { expect, describe, it, beforeAll } from 'vitest' | ||
| import { generateMerkleProof, generateMerkleTree, hashLeaf } from '../src/utils' | ||
| import { getTreeData } from '../src' | ||
| import { CRISP_SERVER_URL } from './constants' | ||
|
|
||
| describe('Utils', () => { | ||
| let leaves: bigint[] | ||
|
|
||
| beforeAll(async () => { | ||
| leaves = await getTreeData(CRISP_SERVER_URL, 0) | ||
| }) | ||
|
|
||
| describe('hashLeaf', () => { | ||
| it('should return a bigint hash of the two values', () => { | ||
| const leaf = hashLeaf('0x1234567890123456789012345678901234567890', '1000') | ||
| expect(typeof leaf).toBe('bigint') | ||
| }) | ||
| }) | ||
|
|
||
| describe('generateMerkleTree', () => { | ||
| it('should generate a merkle tree', () => { | ||
| const tree = generateMerkleTree(leaves) | ||
| expect(tree.root).toBeDefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('generateMerkleProof', () => { | ||
| const address = '0x1234567890123456789012345678901234567890' | ||
| const balance = 1000 | ||
| it('should generate a merkle proof for a leaf', () => { | ||
| const proof = generateMerkleProof(0, balance, address, leaves) | ||
| expect(proof.leaf).toBe(hashLeaf(address, balance.toString())) | ||
| }) | ||
| it('should throw if the leaf does not exist in the tree', () => { | ||
| expect(() => generateMerkleProof(0, balance, address, [])).toThrow('Leaf not found in the tree') | ||
| expect(() => generateMerkleProof(0, 999, address, leaves)).toThrow('Leaf not found in the tree') | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,5 +9,5 @@ | |
| "outDir": "dist", | ||
| "declaration": true | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| "include": ["src/**/*.ts", "src/**/*.json"] | ||
| } | ||
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.