-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(open-payments): generateJwk (#788)
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,5 @@ export { | |
AuthenticatedClient, | ||
UnauthenticatedClient | ||
} from './client' | ||
|
||
export { generateJwk } from './jwk' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { generateKeyPairSync } from 'crypto' | ||
import { generateJwk } from './jwk' | ||
|
||
describe('jwk', (): void => { | ||
describe('generateJwk', (): void => { | ||
test('properly generates jwk', async (): Promise<void> => { | ||
expect(generateJwk({ keyId: 'keyid' })).toEqual({ | ||
alg: 'EdDSA', | ||
kid: 'keyid', | ||
kty: 'OKP', | ||
crv: 'Ed25519', | ||
x: expect.any(String) | ||
}) | ||
}) | ||
|
||
test('properly generates jwk with defined private key', async (): Promise<void> => { | ||
expect( | ||
generateJwk({ | ||
keyId: 'keyid', | ||
privateKey: generateKeyPairSync('ed25519').privateKey | ||
}) | ||
).toEqual({ | ||
alg: 'EdDSA', | ||
kid: 'keyid', | ||
kty: 'OKP', | ||
crv: 'Ed25519', | ||
x: expect.any(String) | ||
}) | ||
}) | ||
|
||
test('throws if empty keyId', async (): Promise<void> => { | ||
expect(() => generateJwk({ keyId: '' })).toThrow('KeyId cannot be empty') | ||
}) | ||
|
||
test('throws if provided key is not EdDSA-Ed25519', async (): Promise<void> => { | ||
expect(() => | ||
generateJwk({ | ||
keyId: 'keyid', | ||
privateKey: generateKeyPairSync('ed448').privateKey | ||
}) | ||
).toThrow('Key is not EdDSA-Ed25519') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createPublicKey, generateKeyPairSync, KeyObject } from 'crypto' | ||
import { JWK } from './types' | ||
|
||
export const generateJwk = ({ | ||
privateKey: providedPrivateKey, | ||
keyId | ||
}: { | ||
privateKey?: KeyObject | ||
keyId: string | ||
}): JWK => { | ||
if (!keyId.trim()) { | ||
throw new Error('KeyId cannot be empty') | ||
} | ||
|
||
const privateKey = providedPrivateKey | ||
? providedPrivateKey | ||
: generateKeyPairSync('ed25519').privateKey | ||
|
||
const jwk = createPublicKey(privateKey).export({ | ||
format: 'jwk' | ||
}) | ||
|
||
if (jwk.crv !== 'Ed25519' || jwk.kty !== 'OKP') { | ||
throw new Error('Key is not EdDSA-Ed25519') | ||
} | ||
|
||
return { | ||
alg: 'EdDSA', | ||
kid: keyId, | ||
kty: jwk.kty, | ||
crv: jwk.crv, | ||
x: jwk.x | ||
} | ||
} |