diff --git a/packages/protocols/src/utils.ts b/packages/protocols/src/utils.ts index d7fcbec..89f8394 100644 --- a/packages/protocols/src/utils.ts +++ b/packages/protocols/src/utils.ts @@ -62,9 +62,15 @@ export function generateMerkleProof( zeroValue: StrBigInt, arity: number, leaves: StrBigInt[], - leafIndex: number + leaf: StrBigInt ): MerkleProof { const tree = generateMerkleTree(depth, zeroValue, arity, leaves) + const leafIndex = leaves.indexOf(BigInt(leaf)) + + if (leafIndex === -1) { + throw new Error("The leaf does not exists") + } + return tree.createProof(leafIndex) } diff --git a/packages/protocols/tests/rln.test.ts b/packages/protocols/tests/rln.test.ts index d9eb15a..637df14 100644 --- a/packages/protocols/tests/rln.test.ts +++ b/packages/protocols/tests/rln.test.ts @@ -41,7 +41,7 @@ describe("RLN", () => { const epoch: string = genExternalNullifier("test-epoch") const rlnIdentifier = RLN.genIdentifier() - const merkleProof = generateMerkleProof(15, BigInt(0), 2, leaves, leaves.length - 1) + const merkleProof = generateMerkleProof(15, BigInt(0), 2, leaves, identityCommitment) const witness = RLN.genWitness(secretHash, merkleProof, epoch, signal, rlnIdentifier) expect(typeof witness).toBe("object") @@ -61,7 +61,7 @@ describe("RLN", () => { const epoch = genExternalNullifier("test-epoch") const rlnIdentifier = RLN.genIdentifier() - const merkleProof = generateMerkleProof(15, BigInt(0), 2, leaves, leaves.length - 1) + const merkleProof = generateMerkleProof(15, BigInt(0), 2, leaves, identityCommitment) const witness = RLN.genWitness(secretHash, merkleProof, epoch, signal, rlnIdentifier) const [y, nullifier] = RLN.calculateOutput(secretHash, BigInt(epoch), rlnIdentifier, signalHash) diff --git a/packages/protocols/tests/semaphore.test.ts b/packages/protocols/tests/semaphore.test.ts index e96175a..373a6dc 100644 --- a/packages/protocols/tests/semaphore.test.ts +++ b/packages/protocols/tests/semaphore.test.ts @@ -26,7 +26,7 @@ describe("Semaphore", () => { const leaves = [BigInt(3), BigInt(2), identityCommitment, BigInt(4)] const signal = "0x111" - const merkleProof = generateMerkleProof(20, BigInt(0), 5, leaves, 2) + const merkleProof = generateMerkleProof(20, BigInt(0), 5, leaves, identityCommitment) const witness = Semaphore.genWitness( identity.getTrapdoor(), @@ -47,7 +47,7 @@ describe("Semaphore", () => { const leaves = [BigInt(3), BigInt(2), identityCommitment, BigInt(4)] const signal = "0x111" - const merkleProof = generateMerkleProof(20, BigInt(0), 5, leaves, 2) + const merkleProof = generateMerkleProof(20, BigInt(0), 5, leaves, identityCommitment) const witness = Semaphore.genWitness( identity.getTrapdoor(),