Skip to content

Commit

Permalink
feat: add Alg support to DID:JWK. Although optional in reality severa…
Browse files Browse the repository at this point in the history
…l external systems expect it to be present
  • Loading branch information
nklomp committed Nov 4, 2022
1 parent 0cd067d commit 12dae72
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('@sphereon/jwk-did-provider', () => {

expect(identifier).toBeDefined()
expect(identifier.did).toBe(
'did:jwk:eyJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJvankweURrQnJNTHJENFVsbVdFTjRNcnF3bUNfanRCZWY1QXVxc0Q1eU5jIiwieSI6IlRkU0VHNVRSTkNUVEt2anNEcGwyMjVxX3AtT2xuaERWWmNYVTJRRzB2bU0ifQ'
'did:jwk:eyJhbGciOiJFUzI1NksiLCJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJvankweURrQnJNTHJENFVsbVdFTjRNcnF3bUNfanRCZWY1QXVxc0Q1eU5jIiwieSI6IlRkU0VHNVRSTkNUVEt2anNEcGwyMjVxX3AtT2xuaERWWmNYVTJRRzB2bU0ifQ'
)
})

Expand Down
3 changes: 3 additions & 0 deletions packages/jwk-did-provider/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 12dae72

Please sign in to comment.