diff --git a/packages/identity/src/index.ts b/packages/identity/src/index.ts index fbd628b8d..13b443ded 100644 --- a/packages/identity/src/index.ts +++ b/packages/identity/src/index.ts @@ -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) + } } diff --git a/packages/identity/tests/index.test.ts b/packages/identity/tests/index.test.ts index 3c8d5c748..3c6cdf37f 100644 --- a/packages/identity/tests/index.test.ts +++ b/packages/identity/tests/index.test.ts @@ -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) + }) + }) })