Skip to content

Commit

Permalink
Merge pull request #734 from semaphore-protocol/fix/convert-hex-pk
Browse files Browse the repository at this point in the history
Convert hexadecimal private key before signing messages
  • Loading branch information
cedoor authored Mar 28, 2024
2 parents c795ddc + 88df87e commit d9d97de
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 with 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 d9d97de

Please sign in to comment.