Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commands/key-generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class GenerateIdentityKeyCommand extends Command {
* @override
*/
protected async action(): Promise<void> {
const key = await JWK.generate('EC', 'secp256k1')
const key = await JWK.generate('EC', 'P-256')
const pem = key.toPEM(true)
try {
const filename = await writeFile('./identity-key.pem', pem)
Expand Down
23 changes: 22 additions & 1 deletion src/commands/payid-sign.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
convertToVerifiedAddress,
signWithKeys,
getDefaultAlgorithm,
IdentityKeySigningParams,
toKey,
} from '@payid-org/utils'
import { JWKECKey, JWKOctKey, JWKOKPKey, JWKRSAKey } from 'jose'

import Command from './Command'

Expand Down Expand Up @@ -69,3 +69,24 @@ export default class SignPayIdCommand extends Command {
)
}
}

/**
* Returns the default algorithm to use to sign with the given jwk.
*
* @param jwk - The key being used to sign.
* @returns The default algorithm.
*/
export function getDefaultAlgorithm(
jwk: JWKRSAKey | JWKECKey | JWKOctKey | JWKOKPKey,
): string {
if (jwk.kty === 'EC') {
return 'ES256'
}
if (jwk.kty === 'oct') {
return 'HS512'
}
if (jwk.kty === 'OKP') {
return 'EdDSA'
}
return 'RS512'
}
19 changes: 19 additions & 0 deletions test/unit/getDefaultAlgorithm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'mocha'
import { assert } from 'chai'
import { JWK } from 'jose'

import { getDefaultAlgorithm } from '../../src/commands/payid-sign'

describe('when getDefaultAlgorithm()', function (): void {
it('given an EC key then returns ES256', async function (): Promise<void> {
const key = await JWK.generate('EC')
const algorithm = getDefaultAlgorithm(key.toJWK())
assert.equal(algorithm, 'ES256')
})

it('given an RSA key then returns RS512', async function (): Promise<void> {
const key = await JWK.generate('RSA')
const algorithm = getDefaultAlgorithm(key.toJWK())
assert.equal(algorithm, 'RS512')
})
})