-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the code for claims and proof generation for the merkle trees
- Loading branch information
Showing
3 changed files
with
94 additions
and
22 deletions.
There are no files selected for viewing
51 changes: 37 additions & 14 deletions
51
packages/scoutgame/src/claims/__tests__/verifyClaim.spec.ts
This file contains 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 |
---|---|---|
@@ -1,43 +1,66 @@ | ||
import { generateTree, getProofs, verifyClaim, type ProvableClaim } from '../root'; | ||
import { generateMerkleTree, getMerkleProofs, verifyMerkleClaim, type ProvableClaim } from '../merkleTree'; | ||
|
||
const claimsInput: ProvableClaim[] = [ | ||
{ | ||
address: '0x36446eF671954753801f9d73C415a80C0e550b32', | ||
amount: '100' | ||
amount: 100 | ||
}, | ||
{ | ||
address: '0xC82ee528AC8BFd7087e0DE6548955601dFcac99d', | ||
amount: '200' | ||
amount: 200 | ||
}, | ||
{ | ||
address: '0xB20C9b7e6b9cbcDed9819F88D68938D0B149887f', | ||
amount: '300' | ||
amount: 300 | ||
}, | ||
{ | ||
address: '0x36446eF671954753801f9d73C415a80C0e550b32', | ||
amount: '400' | ||
amount: 400 | ||
}, | ||
{ | ||
address: '0xD02953857250D32EC72064d9E2320B43296E52C0', | ||
amount: '500' | ||
amount: 500 | ||
} | ||
]; | ||
|
||
describe('verifyClaim', () => { | ||
describe('verifyMerkleClaim', () => { | ||
it('should return true if the claim is valid', () => { | ||
const tree = generateTree(claimsInput); | ||
const { tree } = generateMerkleTree(claimsInput); | ||
const claim = claimsInput[0]; | ||
const proofs = getProofs(tree, claim); | ||
expect(verifyClaim(tree, claim, proofs)).toBe(true); | ||
const proofs = getMerkleProofs(tree, claim); | ||
expect(verifyMerkleClaim(tree, claim, proofs)).toBe(true); | ||
}); | ||
|
||
it('should return false if the claim is invalid', () => { | ||
const tree = generateTree(claimsInput); | ||
const { tree } = generateMerkleTree(claimsInput); | ||
const claim: ProvableClaim = { | ||
address: '0x36446eF671954753801f9d73C415a80C0e550b32', | ||
amount: '200' | ||
amount: 200 | ||
}; | ||
const proof = getProofs(tree, claim); | ||
expect(verifyClaim(tree, claim, proof)).toBe(false); | ||
const proof = getMerkleProofs(tree, claim); | ||
expect(verifyMerkleClaim(tree, claim, proof)).toBe(false); | ||
}); | ||
|
||
it('should sort inputs so that it is not reliant on ordering of the claims', () => { | ||
function shuffleArray<T>(array: T[]): T[] { | ||
const newArray = [...array]; // Create a copy of the array to avoid mutating the original | ||
for (let i = newArray.length - 1; i > 0; i--) { | ||
const j = Math.floor(Math.random() * (i + 1)); | ||
[newArray[i], newArray[j]] = [newArray[j], newArray[i]]; // Swap elements | ||
} | ||
return newArray; | ||
} | ||
|
||
const shuffledOne = shuffleArray(claimsInput); | ||
|
||
const shuffledTwo = shuffleArray(claimsInput); | ||
|
||
// Make sure sorting worked | ||
expect(JSON.stringify(shuffledOne)).not.toEqual(JSON.stringify(shuffledTwo)); | ||
|
||
const { rootHash: rootHashOne } = generateMerkleTree(shuffledOne); | ||
const { rootHash: rootHashTwo } = generateMerkleTree(shuffledTwo); | ||
|
||
expect(rootHashOne).toEqual(rootHashTwo); | ||
}); | ||
}); |
This file contains 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 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