From 12dae72860fd0dc00e96a8121b136c2195843388 Mon Sep 17 00:00:00 2001 From: nklomp Date: Fri, 4 Nov 2022 01:13:24 +0100 Subject: [PATCH] feat: add Alg support to DID:JWK. Although optional in reality several external systems expect it to be present --- .../__tests__/comparison-regression.test.ts | 10 ++++++---- .../__tests__/jwk-did-provider.test.ts | 2 +- packages/jwk-did-provider/src/functions.ts | 3 +++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/jwk-did-provider/__tests__/comparison-regression.test.ts b/packages/jwk-did-provider/__tests__/comparison-regression.test.ts index 3801f1115..61c73f2a8 100644 --- a/packages/jwk-did-provider/__tests__/comparison-regression.test.ts +++ b/packages/jwk-did-provider/__tests__/comparison-regression.test.ts @@ -77,12 +77,13 @@ describe('@sphereon/jwk-did-provider comparison ES256k', () => { const identifier: IIdentifier = await agent.didManagerCreate({ options }) const did = - 'did:jwk:eyJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJmYjY5SEE2M244ZENKd0RmaVJONGxacUtVVU1odHYyZE5BemdjUjJNY0ZBIiwieSI6Ikd3amFWNHpuSm1EZDBOdFlSWGdJeW5aOFlyWDRqN0lzLXFselFuekppclEifQ' + 'did:jwk:eyJhbGciOiJFUzI1NksiLCJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJmYjY5SEE2M244ZENKd0RmaVJONGxacUtVVU1odHYyZE5BemdjUjJNY0ZBIiwieSI6Ikd3amFWNHpuSm1EZDBOdFlSWGdJeW5aOFlyWDRqN0lzLXFselFuekppclEifQ' expect(identifier.did).toBe(did) const didResolutionResult: DIDResolutionResult = await agent.resolveDid({ didUrl: did }) const jwk = { + alg: 'ES256K', kty: 'EC', use: 'sig', crv: 'secp256k1', @@ -91,7 +92,7 @@ describe('@sphereon/jwk-did-provider comparison ES256k', () => { } const verificationMethod = { controller: - 'did:jwk:eyJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJmYjY5SEE2M244ZENKd0RmaVJONGxacUtVVU1odHYyZE5BemdjUjJNY0ZBIiwieSI6Ikd3amFWNHpuSm1EZDBOdFlSWGdJeW5aOFlyWDRqN0lzLXFselFuekppclEifQ', + 'did:jwk:eyJhbGciOiJFUzI1NksiLCJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJmYjY5SEE2M244ZENKd0RmaVJONGxacUtVVU1odHYyZE5BemdjUjJNY0ZBIiwieSI6Ikd3amFWNHpuSm1EZDBOdFlSWGdJeW5aOFlyWDRqN0lzLXFselFuekppclEifQ', id: '#0', publicKeyJwk: jwk, type: 'JsonWebKey2020', @@ -150,11 +151,12 @@ describe('@sphereon/jwk-did-provider comparison ES256', () => { const identifier: IIdentifier = await agent.didManagerCreate({ options }) const did = - 'did:jwk:eyJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6IlAtMjU2IiwieCI6IktQalQxY0IwYU1XclBzVGp3cmdtMEhwSVNwUHZ6aGpyVGxfakVLQVhrUSIsInkiOiJpeVlGZnRwZXl5dk9FTUtjR01pOFpvT3BjVy1ULU4yc2szUl9FaVZYQmdzIn0' + 'did:jwk:eyJhbGciOiJFUzI1NiIsInVzZSI6InNpZyIsImt0eSI6IkVDIiwiY3J2IjoiUC0yNTYiLCJ4IjoiS1BqVDFjQjBhTVdyUHNUandyZ20wSHBJU3BQdnpoanJUbF9qRUtBWGtRIiwieSI6Iml5WUZmdHBleXl2T0VNS2NHTWk4Wm9PcGNXLVQtTjJzazNSX0VpVlhCZ3MifQ' expect(identifier.did).toBe(did) const didResolutionResult: DIDResolutionResult = await agent.resolveDid({ didUrl: did }) const jwk = { + alg: 'ES256', kty: 'EC', use: 'sig', crv: 'P-256', @@ -163,7 +165,7 @@ describe('@sphereon/jwk-did-provider comparison ES256', () => { } const verificationMethod = { controller: - 'did:jwk:eyJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6IlAtMjU2IiwieCI6IktQalQxY0IwYU1XclBzVGp3cmdtMEhwSVNwUHZ6aGpyVGxfakVLQVhrUSIsInkiOiJpeVlGZnRwZXl5dk9FTUtjR01pOFpvT3BjVy1ULU4yc2szUl9FaVZYQmdzIn0', + 'did:jwk:eyJhbGciOiJFUzI1NiIsInVzZSI6InNpZyIsImt0eSI6IkVDIiwiY3J2IjoiUC0yNTYiLCJ4IjoiS1BqVDFjQjBhTVdyUHNUandyZ20wSHBJU3BQdnpoanJUbF9qRUtBWGtRIiwieSI6Iml5WUZmdHBleXl2T0VNS2NHTWk4Wm9PcGNXLVQtTjJzazNSX0VpVlhCZ3MifQ', id: '#0', publicKeyJwk: jwk, type: 'JsonWebKey2020', diff --git a/packages/jwk-did-provider/__tests__/jwk-did-provider.test.ts b/packages/jwk-did-provider/__tests__/jwk-did-provider.test.ts index 826c46287..b872ea727 100644 --- a/packages/jwk-did-provider/__tests__/jwk-did-provider.test.ts +++ b/packages/jwk-did-provider/__tests__/jwk-did-provider.test.ts @@ -48,7 +48,7 @@ describe('@sphereon/jwk-did-provider', () => { expect(identifier).toBeDefined() expect(identifier.did).toBe( - 'did:jwk:eyJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJvankweURrQnJNTHJENFVsbVdFTjRNcnF3bUNfanRCZWY1QXVxc0Q1eU5jIiwieSI6IlRkU0VHNVRSTkNUVEt2anNEcGwyMjVxX3AtT2xuaERWWmNYVTJRRzB2bU0ifQ' + 'did:jwk:eyJhbGciOiJFUzI1NksiLCJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJvankweURrQnJNTHJENFVsbVdFTjRNcnF3bUNfanRCZWY1QXVxc0Q1eU5jIiwieSI6IlRkU0VHNVRSTkNUVEt2anNEcGwyMjVxX3AtT2xuaERWWmNYVTJRRzB2bU0ifQ' ) }) diff --git a/packages/jwk-did-provider/src/functions.ts b/packages/jwk-did-provider/src/functions.ts index b0774ef30..d1ef95cde 100644 --- a/packages/jwk-did-provider/src/functions.ts +++ b/packages/jwk-did-provider/src/functions.ts @@ -92,6 +92,7 @@ const assertProperKeyLength = (keyHex: string, expectedKeyLength: number) => { const toSecp256k1Jwk = (publicKeyHex: string, use?: KeyUse): JsonWebKey => { assertProperKeyLength(publicKeyHex, 130) return { + alg: 'ES256K', ...(use !== undefined && { use }), kty: KeyType.EC, crv: KeyCurve.Secp256k1, @@ -113,6 +114,7 @@ const toSecp256r1Jwk = (publicKeyHex: string, use?: KeyUse): JsonWebKey => { const key = secp256r1.keyFromPublic(publicKey, 'hex') const pubPoint = key.getPublic() return { + alg: 'ES256', ...(use !== undefined && { use }), kty: KeyType.EC, crv: KeyCurve.P_256, @@ -130,6 +132,7 @@ const toSecp256r1Jwk = (publicKeyHex: string, use?: KeyUse): JsonWebKey => { const toEd25519Jwk = (publicKeyHex: string, use?: KeyUse): JsonWebKey => { assertProperKeyLength(publicKeyHex, 64) return { + alg: 'EdDSA', ...(use !== undefined && { use }), kty: KeyType.OKP, crv: KeyCurve.Ed25519,