diff --git a/packages/did-provider-jwk/__tests__/comparison-regression.test.ts b/packages/did-provider-jwk/__tests__/comparison-regression.test.ts index 3933ac97..225dfe65 100644 --- a/packages/did-provider-jwk/__tests__/comparison-regression.test.ts +++ b/packages/did-provider-jwk/__tests__/comparison-regression.test.ts @@ -15,7 +15,9 @@ const method = require('@or13/did-jwk') const DID_METHOD = 'did:jwk' -const jwkDIDProvider = new JwkDIDProvider() +const jwkDIDProvider = new JwkDIDProvider({ + defaultKms: 'mem', +}) const agent = createAgent({ plugins: [ diff --git a/packages/did-provider-jwk/__tests__/jwk-did-provider.test.ts b/packages/did-provider-jwk/__tests__/jwk-did-provider.test.ts index c52d69cd..5cd3e071 100644 --- a/packages/did-provider-jwk/__tests__/jwk-did-provider.test.ts +++ b/packages/did-provider-jwk/__tests__/jwk-did-provider.test.ts @@ -10,7 +10,9 @@ import { JwkKeyUse } from '@sphereon/ssi-sdk-ext.key-utils' const DID_METHOD = 'did:jwk' const PRIVATE_KEY_HEX = '7dd923e40f4615ac496119f7e793cc2899e99b64b88ca8603db986700089532b' -const jwkDIDProvider = new JwkDIDProvider() +const jwkDIDProvider = new JwkDIDProvider({ + defaultKms: 'mem', +}) const agent = createAgent({ plugins: [ diff --git a/packages/did-provider-jwk/src/jwk-did-provider.ts b/packages/did-provider-jwk/src/jwk-did-provider.ts index dc8f709d..6939adec 100644 --- a/packages/did-provider-jwk/src/jwk-did-provider.ts +++ b/packages/did-provider-jwk/src/jwk-did-provider.ts @@ -12,12 +12,18 @@ const debug = Debug('sphereon:did-provider-jwk') * @public */ export class JwkDIDProvider extends AbstractIdentifierProvider { + private readonly defaultKms?: string + + constructor(options: { defaultKms?: string }) { + super() + this.defaultKms = options.defaultKms + } /** {@inheritDoc @veramo/veramo-core#IDIDManager.didManagerCreate} */ async createIdentifier(args: ICreateIdentifierArgs, context: IRequiredContext): Promise> { const key = await importProvidedOrGeneratedKey( { - kms: args.kms, + kms: args.kms ?? this.defaultKms ?? '', alias: args.alias, options: args.options, }, diff --git a/packages/did-provider-key/__tests__/key-did-provider.test.ts b/packages/did-provider-key/__tests__/key-did-provider.test.ts index f6ebe675..1ab91d73 100644 --- a/packages/did-provider-key/__tests__/key-did-provider.test.ts +++ b/packages/did-provider-key/__tests__/key-did-provider.test.ts @@ -13,7 +13,9 @@ const PRIVATE_KEY_HEX = '7dd923e40f4615ac496119f7e793cc2899e99b64b88ca8603db9867 // const jwk = toJwk(PRIVATE_KEY_HEX, 'Secp256k1', { isPrivateKey: true }) // console.log(JSON.stringify(jwk, null, 2)) -const keyDIDProvider = new SphereonKeyDidProvider() +const keyDIDProvider = new SphereonKeyDidProvider({ + defaultKms: 'mem', +}) const agent = createAgent({ plugins: [ diff --git a/packages/did-provider-key/src/SphereonKeyDidProvider.ts b/packages/did-provider-key/src/SphereonKeyDidProvider.ts index 9b5e379f..cab498f0 100644 --- a/packages/did-provider-key/src/SphereonKeyDidProvider.ts +++ b/packages/did-provider-key/src/SphereonKeyDidProvider.ts @@ -30,7 +30,13 @@ const keyCodecs = { } as const export class SphereonKeyDidProvider extends AbstractIdentifierProvider { - + private readonly kms?: string + + constructor(options: { defaultKms?: string }) { + super() + this.kms = options.defaultKms + } + async createIdentifier( { kms, @@ -61,7 +67,7 @@ export class SphereonKeyDidProvider extends AbstractIdentifierProvider { const key = await importProvidedOrGeneratedKey( { - kms: kms, + kms: kms ?? this.kms ?? '', alias: alias, options: { ...options, type: keyType }, }, diff --git a/packages/did-provider-oyd/src/oyd-did-provider.ts b/packages/did-provider-oyd/src/oyd-did-provider.ts index 685bcb97..1cf5d6b2 100644 --- a/packages/did-provider-oyd/src/oyd-did-provider.ts +++ b/packages/did-provider-oyd/src/oyd-did-provider.ts @@ -13,6 +13,12 @@ type IContext = IAgentContext * @public */ export class OydDIDProvider extends AbstractIdentifierProvider { + private defaultKms?: string + + constructor(options: { defaultKms?: string }) { + super() + this.defaultKms = options.defaultKms + } async createIdentifier( { kms, options }: { kms?: string; options: OydCreateIdentifierOptions }, @@ -42,7 +48,7 @@ export class OydDIDProvider extends AbstractIdentifierProvider { const keyType: OydDidSupportedKeyTypes = options?.keyType || 'Ed25519' const key = await this.holdKeys( { - kms: kms, + kms: kms || this.defaultKms || '', options: { keyType, kid: didDoc.did + '#key-doc', @@ -96,7 +102,7 @@ export class OydDIDProvider extends AbstractIdentifierProvider { private async holdKeys(args: OydDidHoldKeysArgs, context: IContext): Promise { if (args.options.privateKeyHex) { return context.agent.keyManagerImport({ - kms: '', + kms: args.kms || this.defaultKms || '', type: args.options.keyType, kid: args.options.kid, privateKeyHex: args.options.privateKeyHex, @@ -107,7 +113,7 @@ export class OydDIDProvider extends AbstractIdentifierProvider { } return context.agent.keyManagerCreate({ type: args.options.keyType, - kms: '', + kms: args.kms || this.defaultKms || '', meta: { algorithms: ['Ed25519'], }, diff --git a/packages/did-provider-oyd/src/types/oyd-provider-types.ts b/packages/did-provider-oyd/src/types/oyd-provider-types.ts index 999877ff..7ec4e2a8 100644 --- a/packages/did-provider-oyd/src/types/oyd-provider-types.ts +++ b/packages/did-provider-oyd/src/types/oyd-provider-types.ts @@ -5,7 +5,7 @@ export type OydCreateIdentifierOptions = { } export type OydDidHoldKeysArgs = { - kms?: string + kms: string options: HoldKeysOpts } diff --git a/packages/did-provider-web/src/web-did-provider.ts b/packages/did-provider-web/src/web-did-provider.ts index afbc9187..1c0873ed 100644 --- a/packages/did-provider-web/src/web-did-provider.ts +++ b/packages/did-provider-web/src/web-did-provider.ts @@ -15,6 +15,12 @@ type IContext = IAgentContext * @public */ export class WebDIDProvider extends AbstractIdentifierProvider { + private readonly defaultKms: string + + constructor(options: { defaultKms: string }) { + super() + this.defaultKms = options.defaultKms + } async createIdentifier(args: ICreateIdentifierArgs, context: IContext): Promise> { const { kms, alias } = args @@ -25,7 +31,7 @@ export class WebDIDProvider extends AbstractIdentifierProvider { } const keyOpts = Array.isArray(opts.keys) ? opts.keys : [opts.keys as IKeyOpts] const keys = await Promise.all( - keyOpts.map((keyOpt: IKeyOpts) => importProvidedOrGeneratedKey({ kms: kms, options: keyOpt }, context)) + keyOpts.map((keyOpt: IKeyOpts) => importProvidedOrGeneratedKey({ kms: kms ?? this.defaultKms, options: keyOpt }, context)) ) const controllerIdx = keyOpts.findIndex((opt) => opt.isController) diff --git a/packages/identifier-resolution/__tests__/localAgent.test.ts b/packages/identifier-resolution/__tests__/localAgent.test.ts index 74c094ca..7749da6b 100644 --- a/packages/identifier-resolution/__tests__/localAgent.test.ts +++ b/packages/identifier-resolution/__tests__/localAgent.test.ts @@ -22,7 +22,9 @@ let agent: TAgent const DID_METHOD = 'did:jwk' -const jwkDIDProvider = new JwkDIDProvider() +const jwkDIDProvider = new JwkDIDProvider({ + defaultKms: 'mem', +}) const setup = async (): Promise => { const db: OrPromise = new DataSource({ diff --git a/packages/identifier-resolution/__tests__/restAgent.test.ts b/packages/identifier-resolution/__tests__/restAgent.test.ts index 645efb0a..6b78a107 100644 --- a/packages/identifier-resolution/__tests__/restAgent.test.ts +++ b/packages/identifier-resolution/__tests__/restAgent.test.ts @@ -29,7 +29,9 @@ const basePath = '/agent' const DID_METHOD = 'did:jwk' -const jwkDIDProvider = new JwkDIDProvider() +const jwkDIDProvider = new JwkDIDProvider({ + defaultKms: 'mem', +}) let serverAgent: IAgent let clientAgent: TAgent diff --git a/packages/jwt-service/__tests__/localAgent.test.ts b/packages/jwt-service/__tests__/localAgent.test.ts index e2720b84..e3e0e901 100644 --- a/packages/jwt-service/__tests__/localAgent.test.ts +++ b/packages/jwt-service/__tests__/localAgent.test.ts @@ -23,7 +23,9 @@ let agent: TAgent => { const db: OrPromise = new DataSource({ diff --git a/packages/jwt-service/__tests__/restAgent.test.ts b/packages/jwt-service/__tests__/restAgent.test.ts index d7a0d9a1..7760913e 100644 --- a/packages/jwt-service/__tests__/restAgent.test.ts +++ b/packages/jwt-service/__tests__/restAgent.test.ts @@ -30,7 +30,9 @@ const basePath = '/agent' const DID_METHOD = 'did:jwk' -const jwkDIDProvider = new JwkDIDProvider() +const jwkDIDProvider = new JwkDIDProvider({ + defaultKms: 'mem', +}) let serverAgent: IAgent let clientAgent: TAgent diff --git a/packages/key-manager/src/agent/SphereonKeyManager.ts b/packages/key-manager/src/agent/SphereonKeyManager.ts index acfaf745..f6519c6d 100644 --- a/packages/key-manager/src/agent/SphereonKeyManager.ts +++ b/packages/key-manager/src/agent/SphereonKeyManager.ts @@ -51,7 +51,7 @@ export class SphereonKeyManager extends VeramoKeyManager { } override async keyManagerCreate(args: ISphereonKeyManagerCreateArgs): Promise { - const kms = this.getKmsByName(args.kms || this._defaultKms) + const kms = this.getKmsByName(args.kms ?? this._defaultKms) const meta: KeyMetadata = { ...args.meta, ...(args.opts && { opts: args.opts }) } if (hasKeyOptions(meta) && meta.opts?.ephemeral && !meta.opts.expiration?.removalDate) { // Make sure we set a delete date on an ephemeral key @@ -61,7 +61,7 @@ export class SphereonKeyManager extends VeramoKeyManager { } } const partialKey = await kms.createKey({ type: args.type, meta }) - const key: IKey = { ...partialKey, kms: args.kms || this._defaultKms } + const key: IKey = { ...partialKey, kms: args.kms ?? this._defaultKms } key.meta = { ...meta, ...key.meta } key.meta.jwkThumbprint = key.meta.jwkThumbprint ?? calculateJwkThumbprintForKey({ key }) diff --git a/packages/key-utils/src/functions.ts b/packages/key-utils/src/functions.ts index e2e3534a..5e204bf2 100644 --- a/packages/key-utils/src/functions.ts +++ b/packages/key-utils/src/functions.ts @@ -101,7 +101,7 @@ const keyMetaAlgorithmsFromKeyType = (type: string | TKeyType) => { */ export async function importProvidedOrGeneratedKey( args: IImportProvidedOrGeneratedKeyArgs & { - kms?: string + kms: string }, context: IAgentContext ): Promise { @@ -134,7 +134,7 @@ export async function importProvidedOrGeneratedKey( if (privateKeyHex) { return context.agent.keyManagerImport({ ...key, - kms: args.kms ?? '', + kms: args.kms, type, privateKeyHex: privateKeyHex!, }) @@ -142,7 +142,7 @@ export async function importProvidedOrGeneratedKey( return context.agent.keyManagerCreate({ type, - kms: args.kms ?? '', + kms: args.kms, meta: { ...key?.meta, algorithms: keyMetaAlgorithmsFromKeyType(type),