Skip to content
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

feat(identity): add generate commitment function #877

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/identity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,20 @@ export class Identity {
static verifySignature(message: BigNumberish, signature: Signature, publicKey: Point): boolean {
return verifySignature(message, signature, publicKey)
}

/**
* Generates the commitment from the given public key.
* This static method is particularly useful after signature verification,
* as it allows retrieval of the corresponding commitment associated with the public key.
*
* @example
* const identity = new Identity()
* Identity.generateCommitment(identity.publicKey)
*
* @param publicKey The public key to generate the commitment.
* @returns The Semaphore identity commitment.
*/
static generateCommitment(publicKey: Point): bigint {
return poseidon2(publicKey)
}
}
10 changes: 10 additions & 0 deletions packages/identity/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,14 @@ describe("Identity", () => {
expect(Identity.verifySignature("message", signature, identity.publicKey)).toBeTruthy()
})
})

describe("# generateCommitment", () => {
it("Should generate the identity commitment from the public key", () => {
const identity = new Identity(privateKeyText)

const commitment = Identity.generateCommitment(identity.publicKey)

expect(identity.commitment).toBe(commitment)
})
})
})