Skip to content

Commit

Permalink
feat: support Ed25519 publicKeyJwk (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
yhuard authored May 17, 2022
1 parent 741f79b commit fd81edb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/VerifierAlgorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ function extractPublicKeyBytes(pk: VerificationMethod): Uint8Array {
})
.getPublic('hex')
)
} else if (
pk.publicKeyJwk &&
pk.publicKeyJwk.kty === 'OKP' &&
pk.publicKeyJwk.crv === 'Ed25519' &&
pk.publicKeyJwk.x
) {
return base64ToBytes(pk.publicKeyJwk.x)
} else if (pk.publicKeyMultibase) {
const { base16, base58btc, base64, base64url } = bases
const baseDecoder = base16.decoder.or(base58btc.decoder.or(base64.decoder.or(base64url.decoder)))
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/VerifierAlgorithm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,20 @@ describe('Ed25519', () => {
return expect(verifier(parts[1], parts[2], [pubkey])).toEqual(pubkey)
})

it('validates with publicKeyJwk', async () => {
expect.assertions(1)
const jwt = await createJWT({ bla: 'bla' }, { alg: 'Ed25519', issuer: did, signer: edSigner })
const parts = jwt.match(/^([a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)$/)
const publicKeyJwk = {
crv: 'Ed25519',
kty: 'OKP',
x: u8a.toString(u8a.fromString(edKey.publicKeyBase64, 'base64pad'), 'base64url'),
}
const pubkey = Object.assign({ publicKeyJwk }, edKey)
delete pubkey.publicKeyBase64
return expect(verifier(parts[1], parts[2], [pubkey])).toEqual(pubkey)
})

it('throws error if invalid signature', async () => {
expect.assertions(1)
const jwt = await createJWT({ bla: 'bla' }, { alg: 'Ed25519', issuer: did, signer: edSigner })
Expand Down

0 comments on commit fd81edb

Please sign in to comment.