Skip to content

Commit

Permalink
fix: remove recoverable parameter from ES256Signer (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshambaugh authored Sep 2, 2022
1 parent bf8c552 commit a68ac47
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/ES256Signer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Secp256r1 Signer', () => {
'vOTe64WujVUjEiQrAlwaPJtNADx4usSlCfe8OXHS6Np1BqJdqdJX912pVwVlAjmbqR_TMVE5i5TWB_GJVgrHgg'
)
})

it('refuses wrong key size (too short)', async () => {
expect.assertions(1)
const privateKey = '040f1dbf0a2ca86875447a7c010b0fc6d39d76859c458fbe8f2bf775a40ad7'
Expand Down
19 changes: 7 additions & 12 deletions src/signers/ES256Signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import elliptic from 'elliptic'
const secp256r1 = new elliptic.ec('p256')

/**
* Creates a configured signer function for signing data using the ES256K (secp256r1 + sha256) algorithm.
* Creates a configured signer function for signing data using the ES256 (secp256r1 + sha256) algorithm.
*
* The signing function itself takes the data as a `Uint8Array` or `string` and returns a `base64Url`-encoded signature
*
Expand All @@ -18,25 +18,20 @@ const secp256r1 = new elliptic.ec('p256')
* ```
*
* @param {String} privateKey a private key as `Uint8Array`
* @param {Boolean} recoverable an optional flag to add the recovery param to the generated signatures
* @return {Function} a configured signer function `(data: string | Uint8Array): Promise<string>`
*/
export function ES256Signer(privateKey: Uint8Array, recoverable = false): Signer {
export function ES256Signer(privateKey: Uint8Array): Signer {
const privateKeyBytes: Uint8Array = privateKey
if (privateKeyBytes.length !== 32) {
throw new Error(`bad_key: Invalid private key format. Expecting 32 bytes, but got ${privateKeyBytes.length}`)
}
const keyPair: elliptic.ec.KeyPair = secp256r1.keyFromPrivate(privateKeyBytes)

return async (data: string | Uint8Array): Promise<string> => {
const { r, s, recoveryParam }: elliptic.ec.Signature = keyPair.sign(sha256(data))
return toJose(
{
r: leftpad(r.toString('hex')),
s: leftpad(s.toString('hex')),
recoveryParam,
},
recoverable
)
const { r, s }: elliptic.ec.Signature = keyPair.sign(sha256(data))
return toJose({
r: leftpad(r.toString('hex')),
s: leftpad(s.toString('hex')),
})
}
}

0 comments on commit a68ac47

Please sign in to comment.