Skip to content

Commit

Permalink
Merge pull request #15 from Sphereon-Opensource/develop
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
nklomp authored Jul 30, 2023
2 parents c169110 + 89b4916 commit 8df3ab6
Show file tree
Hide file tree
Showing 47 changed files with 1,029 additions and 275 deletions.
7 changes: 1 addition & 6 deletions packages/did-provider-ebsi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

## [0.12.1](https://github.com/Sphereon-Opensource/SSI-SDK/compare/v0.12.0...v0.12.1) (2023-06-24)


### Bug Fixes

* Make sure we set the saltLength for RSA PSS ([e19ed6c](https://github.com/Sphereon-Opensource/SSI-SDK/commit/e19ed6c3a7b8454e8074111d33fc59a9c6bcc611))




- Make sure we set the saltLength for RSA PSS ([e19ed6c](https://github.com/Sphereon-Opensource/SSI-SDK/commit/e19ed6c3a7b8454e8074111d33fc59a9c6bcc611))

# [0.12.0](https://github.com/Sphereon-Opensource/SSI-SDK/compare/v0.11.0...v0.12.0) (2023-05-07)

Expand Down
10 changes: 5 additions & 5 deletions packages/did-provider-ebsi/__tests__/functions.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { generatePrivateKeyHex } from '../src/functions'
import { generateEbsiPrivateKeyHex } from '../src/functions'

describe('functions: key generator', () => {
it('Secp256k1 should generate random keys', () => {
const key1 = generatePrivateKeyHex()
const key2 = generatePrivateKeyHex()
const key3 = generatePrivateKeyHex()
const key1 = generateEbsiPrivateKeyHex()
const key2 = generateEbsiPrivateKeyHex()
const key3 = generateEbsiPrivateKeyHex()
expect(key1).toBeDefined()
expect(key2).toBeDefined()
expect(key3).toBeDefined()
expect(key1).not.toBe(key2)
expect(key2).not.toBe(key3)
})
it('Secp256k1 should result in hex length 64', () => {
expect(generatePrivateKeyHex().length).toBe(64)
expect(generateEbsiPrivateKeyHex().length).toBe(64)
})
})
4 changes: 2 additions & 2 deletions packages/did-provider-ebsi/src/EbsiDidProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DIDDocument } from 'did-resolver'
import { IKey, IService } from '@veramo/core/build/types/IIdentifier'
import * as u8a from 'uint8arrays'
import { ebsiDIDSpecInfo, IContext, ICreateIdentifierArgs } from './types'
import { generatePrivateKeyHex, toMethodSpecificId } from './functions'
import { generateEbsiPrivateKeyHex, toMethodSpecificId } from './functions'

const debug = Debug('sphereon:did-provider-ebsi')

Expand All @@ -28,7 +28,7 @@ export class EbsiDidProvider extends AbstractIdentifierProvider {
context: IContext
): Promise<Omit<IIdentifier, 'provider'>> {
if (!options?.type || options.type === ebsiDIDSpecInfo.V1) {
const privateKeyHex = generatePrivateKeyHex(
const privateKeyHex = await generateEbsiPrivateKeyHex(
ebsiDIDSpecInfo.V1,
options?.options?.key?.privateKeyHex ? u8a.fromString(options.options.key.privateKeyHex, 'base16') : undefined
)
Expand Down
2 changes: 1 addition & 1 deletion packages/did-provider-ebsi/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function toMethodSpecificId(specInfo?: EbsiDidSpecInfo, methodSpecificId?
return base58btc.encode(result)
}

export function generatePrivateKeyHex(specInfo?: EbsiDidSpecInfo, privateKeyBytes?: Uint8Array): string {
export function generateEbsiPrivateKeyHex(specInfo?: EbsiDidSpecInfo, privateKeyBytes?: Uint8Array): string {
const spec = specInfo ?? ebsiDIDSpecInfo.V1
const length = spec.didLength ? 2 * spec.didLength : 32

Expand Down
13 changes: 4 additions & 9 deletions packages/did-provider-jwk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

## [0.12.1](https://github.com/Sphereon-OpenSource/ssi-sdk/compare/v0.12.0...v0.12.1) (2023-06-24)


### Bug Fixes

* Fix EC handling for JWKs ([7be20f5](https://github.com/Sphereon-OpenSource/ssi-sdk/commit/7be20f57d6b7d4b7ebf5a2e9b432da34f8f98436))
* Fixes in JWK handling ([f5cd4dd](https://github.com/Sphereon-OpenSource/ssi-sdk/commit/f5cd4ddd4f0cd0f155dcbf3a7e8b43c89b97cacb))
* Make sure we set the saltLength for RSA PSS ([51ae676](https://github.com/Sphereon-OpenSource/ssi-sdk/commit/51ae6769386866771c68c7b7806a75b62a9d5ec1))
* Make sure we set the saltLength for RSA PSS ([e19ed6c](https://github.com/Sphereon-OpenSource/ssi-sdk/commit/e19ed6c3a7b8454e8074111d33fc59a9c6bcc611))




- Fix EC handling for JWKs ([7be20f5](https://github.com/Sphereon-OpenSource/ssi-sdk/commit/7be20f57d6b7d4b7ebf5a2e9b432da34f8f98436))
- Fixes in JWK handling ([f5cd4dd](https://github.com/Sphereon-OpenSource/ssi-sdk/commit/f5cd4ddd4f0cd0f155dcbf3a7e8b43c89b97cacb))
- Make sure we set the saltLength for RSA PSS ([51ae676](https://github.com/Sphereon-OpenSource/ssi-sdk/commit/51ae6769386866771c68c7b7806a75b62a9d5ec1))
- Make sure we set the saltLength for RSA PSS ([e19ed6c](https://github.com/Sphereon-OpenSource/ssi-sdk/commit/e19ed6c3a7b8454e8074111d33fc59a9c6bcc611))

# [0.12.0](https://github.com/Sphereon-OpenSource/ssi-sdk/compare/v0.11.0...v0.12.0) (2023-05-07)

Expand Down
67 changes: 47 additions & 20 deletions packages/did-provider-jwk/__tests__/comparison-regression.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dereferenceDidKeysWithJwkSupport } from '@sphereon/ssi-sdk-ext.did-utils'
import { SphereonKeyManager } from '@sphereon/ssi-sdk-ext.key-manager'
import { JwkKeyUse, toJwk } from '@sphereon/ssi-sdk-ext.key-utils'
import { IKeyOpts, JwkKeyUse, toJwk } from '@sphereon/ssi-sdk-ext.key-utils'
import { SphereonKeyManagementSystem } from '@sphereon/ssi-sdk-ext.kms-local'
import { createAgent, DIDDocument, DIDResolutionResult, IAgentContext, IIdentifier, IKeyManager, IResolver } from '@veramo/core'
import { DIDManager, MemoryDIDStore } from '@veramo/did-manager'
Expand All @@ -9,7 +9,7 @@ import { MemoryKeyStore, MemoryPrivateKeyStore } from '@veramo/key-manager'
import base64url from 'base64url'
import { Resolver } from 'did-resolver'
import { getDidJwkResolver, Key } from '../../did-resolver-jwk/src'
import { IKeyOpts, JwkDIDProvider } from '../src'
import { JwkDIDProvider } from '../src'

const method = require('@or13/did-jwk')

Expand Down Expand Up @@ -40,6 +40,9 @@ const agent = createAgent<IKeyManager & DIDManager & IResolver>({
],
})

function toAbsolute(didDoc: any, did: string) {
return JSON.parse(JSON.stringify(didDoc).replace(/#0/g, `${did}#0`))
}
describe('@sphereon/did-provider-jwk comparison ES256k', () => {
it('external JWK should result in equal DID Document', async () => {
const { publicKeyJwk } = await method.generateKeyPair('ES256K')
Expand All @@ -48,7 +51,8 @@ describe('@sphereon/did-provider-jwk comparison ES256k', () => {
const didResolutionResult: DIDResolutionResult = await agent.resolveDid({ didUrl: did })

const comparisonDidDoc = await method.toDidDocument(publicKeyJwk)
expect(didResolutionResult.didDocument).toEqual(comparisonDidDoc)

expect(didResolutionResult.didDocument).toEqual(toAbsolute(comparisonDidDoc, did))
})

it('test resolution', async () => {
Expand All @@ -67,14 +71,15 @@ describe('@sphereon/did-provider-jwk comparison ES256k', () => {
// Resolution
const comparisonDidDoc = await method.toDidDocument(jwk)
const didResolutionResult: DIDResolutionResult = await agent.resolveDid({ didUrl: did })
expect(didResolutionResult.didDocument).toEqual(comparisonDidDoc)
expect(didResolutionResult.didDocument).toEqual(toAbsolute(comparisonDidDoc, did))
})

it('Creation from privateKeyHex', async () => {
const privateKeyHex = 'e8fa0da4d6e7dcdf77b70e4fb0e304bb7cbcb3aeddf33257f0e007a602a46d42'
const options: IKeyOpts = {
key: {
privateKeyHex,
type: 'Secp256k1',
},
use: JwkKeyUse.Signature,
}
Expand All @@ -97,19 +102,30 @@ describe('@sphereon/did-provider-jwk comparison ES256k', () => {
const verificationMethod = {
controller:
'did:jwk:eyJhbGciOiJFUzI1NksiLCJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJmYjY5SEE2M244ZENKd0RmaVJONGxacUtVVU1odHYyZE5BemdjUjJNY0ZBIiwieSI6Ikd3amFWNHpuSm1EZDBOdFlSWGdJeW5aOFlyWDRqN0lzLXFselFuekppclEifQ',
id: '#0',
id: 'did:jwk:eyJhbGciOiJFUzI1NksiLCJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJmYjY5SEE2M244ZENKd0RmaVJONGxacUtVVU1odHYyZE5BemdjUjJNY0ZBIiwieSI6Ikd3amFWNHpuSm1EZDBOdFlSWGdJeW5aOFlyWDRqN0lzLXFselFuekppclEifQ#0',
publicKeyJwk: jwk,
type: 'JsonWebKey2020',
}

expect(didResolutionResult!.didDocument!.verificationMethod).toEqual([verificationMethod])
// We correctly resolve the use property. The other lib does not, so let's add it to their response
expect(didResolutionResult!.didDocument).toEqual({
assertionMethod: ['#0'],
authentication: ['#0'],
capabilityDelegation: ['#0'],
capabilityInvocation: ['#0'],
...(await method.resolve(did)),
assertionMethod: [
'did:jwk:eyJhbGciOiJFUzI1NksiLCJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJmYjY5SEE2M244ZENKd0RmaVJONGxacUtVVU1odHYyZE5BemdjUjJNY0ZBIiwieSI6Ikd3amFWNHpuSm1EZDBOdFlSWGdJeW5aOFlyWDRqN0lzLXFselFuekppclEifQ#0',
],
authentication: [
'did:jwk:eyJhbGciOiJFUzI1NksiLCJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJmYjY5SEE2M244ZENKd0RmaVJONGxacUtVVU1odHYyZE5BemdjUjJNY0ZBIiwieSI6Ikd3amFWNHpuSm1EZDBOdFlSWGdJeW5aOFlyWDRqN0lzLXFselFuekppclEifQ#0',
],
capabilityDelegation: [
'did:jwk:eyJhbGciOiJFUzI1NksiLCJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJmYjY5SEE2M244ZENKd0RmaVJONGxacUtVVU1odHYyZE5BemdjUjJNY0ZBIiwieSI6Ikd3amFWNHpuSm1EZDBOdFlSWGdJeW5aOFlyWDRqN0lzLXFselFuekppclEifQ#0',
],
capabilityInvocation: [
'did:jwk:eyJhbGciOiJFUzI1NksiLCJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJmYjY5SEE2M244ZENKd0RmaVJONGxacUtVVU1odHYyZE5BemdjUjJNY0ZBIiwieSI6Ikd3amFWNHpuSm1EZDBOdFlSWGdJeW5aOFlyWDRqN0lzLXFselFuekppclEifQ#0',
],
...toAbsolute(
await method.resolve(did),
'did:jwk:eyJhbGciOiJFUzI1NksiLCJ1c2UiOiJzaWciLCJrdHkiOiJFQyIsImNydiI6InNlY3AyNTZrMSIsIngiOiJmYjY5SEE2M244ZENKd0RmaVJONGxacUtVVU1odHYyZE5BemdjUjJNY0ZBIiwieSI6Ikd3amFWNHpuSm1EZDBOdFlSWGdJeW5aOFlyWDRqN0lzLXFselFuekppclEifQ'
),
})
})
})
Expand All @@ -121,7 +137,7 @@ describe('@sphereon/did-provider-jwk comparison ES256', () => {

const didResolutionResult: DIDResolutionResult = await agent.resolveDid({ didUrl: did })
const comparisonDidDoc = await method.toDidDocument(publicKeyJwk)
expect(didResolutionResult.didDocument).toEqual(comparisonDidDoc)
expect(didResolutionResult.didDocument).toEqual(toAbsolute(comparisonDidDoc, did))
})

it('test resolution', async () => {
Expand All @@ -140,7 +156,7 @@ describe('@sphereon/did-provider-jwk comparison ES256', () => {
// Resolution
const comparisonDidDoc = await method.toDidDocument(jwk)
const didResolutionResult: DIDResolutionResult = await agent.resolveDid({ didUrl: did })
expect(didResolutionResult.didDocument).toEqual(comparisonDidDoc)
expect(didResolutionResult.didDocument).toEqual(toAbsolute(comparisonDidDoc, did))
})

it('Should decode test vector from spec', async () => {
Expand Down Expand Up @@ -211,7 +227,7 @@ describe('@sphereon/did-provider-jwk comparison ES256', () => {
id: 'did:jwk:eyJraWQiOiJ1cm46aWV0ZjpwYXJhbXM6b2F1dGg6andrLXRodW1icHJpbnQ6c2hhLTI1NjpUOVh4eFZVUHR2TDd0Z0dMOVk4alR4WENPVDFMRjduU2VzWnl0d3FpNVM4Iiwia3R5IjoiRUMiLCJjcnYiOiJQLTI1NiIsImFsZyI6IkVTMjU2IiwieCI6InAwMUFBQ2FkNWFXYVpmVzAwbXhqU0dIVG41R3VpN3Z6cGZqQm1DX2ZhR0EiLCJ5IjoiczR4Y0FYUnVoQ1Z0YTZiaF9Vc3M3eE52NGd5UkRVQW5SS2NzRlJCMzJvWSJ9',
verificationMethod: [
{
id: '#0',
id: 'did:jwk:eyJraWQiOiJ1cm46aWV0ZjpwYXJhbXM6b2F1dGg6andrLXRodW1icHJpbnQ6c2hhLTI1NjpUOVh4eFZVUHR2TDd0Z0dMOVk4alR4WENPVDFMRjduU2VzWnl0d3FpNVM4Iiwia3R5IjoiRUMiLCJjcnYiOiJQLTI1NiIsImFsZyI6IkVTMjU2IiwieCI6InAwMUFBQ2FkNWFXYVpmVzAwbXhqU0dIVG41R3VpN3Z6cGZqQm1DX2ZhR0EiLCJ5IjoiczR4Y0FYUnVoQ1Z0YTZiaF9Vc3M3eE52NGd5UkRVQW5SS2NzRlJCMzJvWSJ9#0',
type: 'JsonWebKey2020',
controller:
'did:jwk:eyJraWQiOiJ1cm46aWV0ZjpwYXJhbXM6b2F1dGg6andrLXRodW1icHJpbnQ6c2hhLTI1NjpUOVh4eFZVUHR2TDd0Z0dMOVk4alR4WENPVDFMRjduU2VzWnl0d3FpNVM4Iiwia3R5IjoiRUMiLCJjcnYiOiJQLTI1NiIsImFsZyI6IkVTMjU2IiwieCI6InAwMUFBQ2FkNWFXYVpmVzAwbXhqU0dIVG41R3VpN3Z6cGZqQm1DX2ZhR0EiLCJ5IjoiczR4Y0FYUnVoQ1Z0YTZiaF9Vc3M3eE52NGd5UkRVQW5SS2NzRlJCMzJvWSJ9',
Expand Down Expand Up @@ -262,7 +278,7 @@ describe('@sphereon/did-provider-jwk comparison ES256', () => {
})

it('Creation from privateKeyHex', async () => {
/*const privateKeyHex = generatePrivateKeyHex('Secp256r1')
/*const privateKeyHex = await generatePrivateKeyHex('Secp256r1')
console.log(privateKeyHex)*/
const privateKeyHex = '47dc6ae067aa011f8574d2da7cf8c326538af08b85e6779d192a9893291c9a0a'
const options: IKeyOpts = {
Expand Down Expand Up @@ -295,19 +311,30 @@ describe('@sphereon/did-provider-jwk comparison ES256', () => {
const verificationMethod = {
controller:
'did:jwk:eyJhbGciOiJFUzI1NiIsInVzZSI6InNpZyIsImt0eSI6IkVDIiwiY3J2IjoiUC0yNTYiLCJ4IjoiOWdnczRDbTRWWGNLT2VQcGprTDlpU3lNQ2EyMnlPamJvLW9VWHB5LWF3MCIsInkiOiJsRVhXN2JfSjdsY2VpVkV0cmZwdHZ1UGVFTnNPSmwtZmh6bXU2NTRHUFI4In0',
id: '#0',
id: 'did:jwk:eyJhbGciOiJFUzI1NiIsInVzZSI6InNpZyIsImt0eSI6IkVDIiwiY3J2IjoiUC0yNTYiLCJ4IjoiOWdnczRDbTRWWGNLT2VQcGprTDlpU3lNQ2EyMnlPamJvLW9VWHB5LWF3MCIsInkiOiJsRVhXN2JfSjdsY2VpVkV0cmZwdHZ1UGVFTnNPSmwtZmh6bXU2NTRHUFI4In0#0',
publicKeyJwk: jwk,
type: 'JsonWebKey2020',
}

expect(didResolutionResult!.didDocument!.verificationMethod).toEqual([verificationMethod])
// We correctly resolve the use property. The other lib does not, so let's add it to their response
expect(didResolutionResult!.didDocument).toEqual({
assertionMethod: ['#0'],
authentication: ['#0'],
capabilityDelegation: ['#0'],
capabilityInvocation: ['#0'],
...(await method.resolve(did)),
assertionMethod: [
'did:jwk:eyJhbGciOiJFUzI1NiIsInVzZSI6InNpZyIsImt0eSI6IkVDIiwiY3J2IjoiUC0yNTYiLCJ4IjoiOWdnczRDbTRWWGNLT2VQcGprTDlpU3lNQ2EyMnlPamJvLW9VWHB5LWF3MCIsInkiOiJsRVhXN2JfSjdsY2VpVkV0cmZwdHZ1UGVFTnNPSmwtZmh6bXU2NTRHUFI4In0#0',
],
authentication: [
'did:jwk:eyJhbGciOiJFUzI1NiIsInVzZSI6InNpZyIsImt0eSI6IkVDIiwiY3J2IjoiUC0yNTYiLCJ4IjoiOWdnczRDbTRWWGNLT2VQcGprTDlpU3lNQ2EyMnlPamJvLW9VWHB5LWF3MCIsInkiOiJsRVhXN2JfSjdsY2VpVkV0cmZwdHZ1UGVFTnNPSmwtZmh6bXU2NTRHUFI4In0#0',
],
capabilityDelegation: [
'did:jwk:eyJhbGciOiJFUzI1NiIsInVzZSI6InNpZyIsImt0eSI6IkVDIiwiY3J2IjoiUC0yNTYiLCJ4IjoiOWdnczRDbTRWWGNLT2VQcGprTDlpU3lNQ2EyMnlPamJvLW9VWHB5LWF3MCIsInkiOiJsRVhXN2JfSjdsY2VpVkV0cmZwdHZ1UGVFTnNPSmwtZmh6bXU2NTRHUFI4In0#0',
],
capabilityInvocation: [
'did:jwk:eyJhbGciOiJFUzI1NiIsInVzZSI6InNpZyIsImt0eSI6IkVDIiwiY3J2IjoiUC0yNTYiLCJ4IjoiOWdnczRDbTRWWGNLT2VQcGprTDlpU3lNQ2EyMnlPamJvLW9VWHB5LWF3MCIsInkiOiJsRVhXN2JfSjdsY2VpVkV0cmZwdHZ1UGVFTnNPSmwtZmh6bXU2NTRHUFI4In0#0',
],
...toAbsolute(
await method.resolve(did),
'did:jwk:eyJhbGciOiJFUzI1NiIsInVzZSI6InNpZyIsImt0eSI6IkVDIiwiY3J2IjoiUC0yNTYiLCJ4IjoiOWdnczRDbTRWWGNLT2VQcGprTDlpU3lNQ2EyMnlPamJvLW9VWHB5LWF3MCIsInkiOiJsRVhXN2JfSjdsY2VpVkV0cmZwdHZ1UGVFTnNPSmwtZmh6bXU2NTRHUFI4In0'
),
})
})
})
6 changes: 3 additions & 3 deletions packages/did-provider-jwk/__tests__/jwk-did-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('@sphereon/did-provider-jwk', () => {

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

Expand Down Expand Up @@ -94,14 +94,14 @@ describe('@sphereon/did-provider-jwk', () => {
const options = {
key: {},
}
await expect(agent.didManagerCreate({ options })).rejects.toThrow('We need to have a private key when importing a key')
await expect(agent.didManagerCreate({ options })).rejects.toThrow('We need to have a private key in Hex or PEM when importing a key')
})

it('should throw error for keys Ed25519 with key usage encryption', async () => {
const options = {
type: Key.Ed25519,
use: JwkKeyUse.Encryption,
}
await expect(agent.didManagerCreate({ options })).rejects.toThrow('Ed25519 keys are only valid for signatures')
await expect(agent.didManagerCreate({ options })).rejects.toThrow('Ed25519 keys are not valid for encryption')
})
})
62 changes: 9 additions & 53 deletions packages/did-provider-jwk/src/jwk-did-provider.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { generatePrivateKeyHex, jwkDetermineUse, JwkKeyUse, toJwk } from '@sphereon/ssi-sdk-ext.key-utils'
import { DIDDocument, IAgentContext, IIdentifier, IKey, IKeyManager } from '@veramo/core'
import { importProvidedOrGeneratedKey, jwkDetermineUse, toJwk } from '@sphereon/ssi-sdk-ext.key-utils'
import { DIDDocument, IAgentContext, IIdentifier, IKeyManager } from '@veramo/core'
import { AbstractIdentifierProvider } from '@veramo/did-manager'
import base64url from 'base64url'
import Debug from 'debug'
import {
IAddKeyArgs,
IAddServiceArgs,
ICreateIdentifierArgs,
IImportProvidedOrGeneratedKeyArgs,
IRemoveKeyArgs,
IRequiredContext,
Key,
} from './types/jwk-provider-types'
// import * as u8a from 'uint8arrays'
import { IAddKeyArgs, IAddServiceArgs, ICreateIdentifierArgs, IRemoveKeyArgs, IRequiredContext } from './types/jwk-provider-types'

const debug = Debug('sphereon:did-provider-jwk')

Expand All @@ -30,21 +21,21 @@ export class JwkDIDProvider extends AbstractIdentifierProvider {

/** {@inheritDoc @veramo/veramo-core#IDIDManager.didManagerCreate} */
async createIdentifier(args: ICreateIdentifierArgs, context: IRequiredContext): Promise<Omit<IIdentifier, 'provider'>> {
const key = await this.importProvidedOrGeneratedKey(
const key = await importProvidedOrGeneratedKey(
{
kms: args.kms,
kms: args.kms ?? this.defaultKms,
options: args.options,
},
context
)

const use = jwkDetermineUse(key.type, args?.options?.use)
const jwk: JsonWebKey = toJwk(key.publicKeyHex, key.type, use)

const jwk: JsonWebKey = toJwk(key.publicKeyHex, key.type, { use, key })
debug(JSON.stringify(jwk, null, 2))
const did = `did:jwk:${base64url(JSON.stringify(jwk))}`
const identifier: Omit<IIdentifier, 'provider'> = {
did: `did:jwk:${base64url(JSON.stringify(jwk))}`,
controllerKeyId: '#0',
did,
controllerKeyId: `${did}#0`,
keys: [key],
services: [],
}
Expand Down Expand Up @@ -86,39 +77,4 @@ export class JwkDIDProvider extends AbstractIdentifierProvider {
async removeService(args: IRemoveKeyArgs, context: IRequiredContext): Promise<any> {
return Promise.reject(Error('Not supported for DID JWKs'))
}

/**
* We optionally generate and then import our own keys.
*
* @param args The key arguments
* @param context The Veramo agent context
* @private
*/
private async importProvidedOrGeneratedKey(args: IImportProvidedOrGeneratedKeyArgs, context: IRequiredContext): Promise<IKey> {
// @ts-ignore
const type = args.options?.type ?? args.options?.key?.type ?? args.options?.keyType ?? Key.Secp256k1

if (args.options && args.options?.use === JwkKeyUse.Encryption && type === Key.Ed25519) {
throw new Error('Ed25519 keys are only valid for signatures')
}

let privateKeyHex: string
if (args.options?.key) {
if (!args.options.key.privateKeyHex) {
throw new Error(`We need to have a private key when importing a key`)
}
privateKeyHex = args.options.key.privateKeyHex
/*if (type === Key.Secp256r1 && privateKeyHex.length === 64) {
privateKeyHex = `04${privateKeyHex}`
}*/
} else {
privateKeyHex = generatePrivateKeyHex(type)
}

return context.agent.keyManagerImport({
kms: args.kms || this.defaultKms,
type,
privateKeyHex,
})
}
}
Loading

0 comments on commit 8df3ab6

Please sign in to comment.