Skip to content

Commit

Permalink
fix(identity): convert hex private key before signing messages
Browse files Browse the repository at this point in the history
The ZK-Kit EdDSA Poseidon package only supports the following private key types: text, buffer. The
Semaphore identity supports text and hexadecimal strings. If the identity private key is an
hexadicimal string it needs to be converted before being passed to any ZK-Kit function.

re #733
  • Loading branch information
cedoor committed Mar 28, 2024
1 parent c795ddc commit 9c2968e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/identity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ export class Identity {
* @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
*/
public signMessage(message: BigNumberish): Signature<bigint> {
return signMessage(this.privateKey, message)
const privateKey = isHexadecimal(this.privateKey, false)
? hexadecimalToBuffer(this.privateKey)
: this.privateKey

return signMessage(privateKey, message)
}

/**
Expand Down
10 changes: 9 additions & 1 deletion packages/identity/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,20 @@ describe("Identity", () => {
})

describe("# verifySignature", () => {
it("Should verify a signature", () => {
it("Should verify a signature with a text private key", () => {
const identity = new Identity(privateKeyText)

const signature = identity.signMessage("message")

expect(Identity.verifySignature("message", signature, identity.publicKey)).toBeTruthy()
})

it("Should verify a signature hexadecimal private key", () => {
const identity = new Identity(privateKeyHexadecimal)

const signature = identity.signMessage("message")

expect(Identity.verifySignature("message", signature, identity.publicKey)).toBeTruthy()
})
})
})

0 comments on commit 9c2968e

Please sign in to comment.