Skip to content

Commit b3f8629

Browse files
authored
feat(identity): add generate commitment function (#877)
This static method is particularly useful after signature verification, as it allows retrieval of the corresponding commitment associated with the public key. re #873
1 parent 06e11d5 commit b3f8629

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

packages/identity/src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,20 @@ export class Identity {
138138
static verifySignature(message: BigNumberish, signature: Signature, publicKey: Point): boolean {
139139
return verifySignature(message, signature, publicKey)
140140
}
141+
142+
/**
143+
* Generates the commitment from the given public key.
144+
* This static method is particularly useful after signature verification,
145+
* as it allows retrieval of the corresponding commitment associated with the public key.
146+
*
147+
* @example
148+
* const identity = new Identity()
149+
* Identity.generateCommitment(identity.publicKey)
150+
*
151+
* @param publicKey The public key to generate the commitment.
152+
* @returns The Semaphore identity commitment.
153+
*/
154+
static generateCommitment(publicKey: Point): bigint {
155+
return poseidon2(publicKey)
156+
}
141157
}

packages/identity/tests/index.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,14 @@ describe("Identity", () => {
138138
expect(Identity.verifySignature("message", signature, identity.publicKey)).toBeTruthy()
139139
})
140140
})
141+
142+
describe("# generateCommitment", () => {
143+
it("Should generate the identity commitment from the public key", () => {
144+
const identity = new Identity(privateKeyText)
145+
146+
const commitment = Identity.generateCommitment(identity.publicKey)
147+
148+
expect(identity.commitment).toBe(commitment)
149+
})
150+
})
141151
})

0 commit comments

Comments
 (0)