Skip to content

Commit fd81edb

Browse files
authored
feat: support Ed25519 publicKeyJwk (#227)
1 parent 741f79b commit fd81edb

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/VerifierAlgorithm.ts

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ function extractPublicKeyBytes(pk: VerificationMethod): Uint8Array {
4444
})
4545
.getPublic('hex')
4646
)
47+
} else if (
48+
pk.publicKeyJwk &&
49+
pk.publicKeyJwk.kty === 'OKP' &&
50+
pk.publicKeyJwk.crv === 'Ed25519' &&
51+
pk.publicKeyJwk.x
52+
) {
53+
return base64ToBytes(pk.publicKeyJwk.x)
4754
} else if (pk.publicKeyMultibase) {
4855
const { base16, base58btc, base64, base64url } = bases
4956
const baseDecoder = base16.decoder.or(base58btc.decoder.or(base64.decoder.or(base64url.decoder)))

src/__tests__/VerifierAlgorithm.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,20 @@ describe('Ed25519', () => {
403403
return expect(verifier(parts[1], parts[2], [pubkey])).toEqual(pubkey)
404404
})
405405

406+
it('validates with publicKeyJwk', async () => {
407+
expect.assertions(1)
408+
const jwt = await createJWT({ bla: 'bla' }, { alg: 'Ed25519', issuer: did, signer: edSigner })
409+
const parts = jwt.match(/^([a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)$/)
410+
const publicKeyJwk = {
411+
crv: 'Ed25519',
412+
kty: 'OKP',
413+
x: u8a.toString(u8a.fromString(edKey.publicKeyBase64, 'base64pad'), 'base64url'),
414+
}
415+
const pubkey = Object.assign({ publicKeyJwk }, edKey)
416+
delete pubkey.publicKeyBase64
417+
return expect(verifier(parts[1], parts[2], [pubkey])).toEqual(pubkey)
418+
})
419+
406420
it('throws error if invalid signature', async () => {
407421
expect.assertions(1)
408422
const jwt = await createJWT({ bla: 'bla' }, { alg: 'Ed25519', issuer: did, signer: edSigner })

0 commit comments

Comments
 (0)