Skip to content

Commit

Permalink
chore: removed defaultKms from DID providers
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderPostma committed Dec 3, 2024
1 parent dccb084 commit 10d34a9
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<IKeyManager & DIDManager & IResolver>({
plugins: [
Expand Down
4 changes: 3 additions & 1 deletion packages/did-provider-jwk/__tests__/jwk-did-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IKeyManager, DIDManager>({
plugins: [
Expand Down
8 changes: 7 additions & 1 deletion packages/did-provider-jwk/src/jwk-did-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Omit<IIdentifier, 'provider'>> {
const key = await importProvidedOrGeneratedKey(
{
kms: args.kms,
kms: args.kms ?? this.defaultKms ?? '',
alias: args.alias,
options: args.options,
},
Expand Down
4 changes: 3 additions & 1 deletion packages/did-provider-key/__tests__/key-did-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IKeyManager, DIDManager>({
plugins: [
Expand Down
10 changes: 8 additions & 2 deletions packages/did-provider-key/src/SphereonKeyDidProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 },
},
Expand Down
12 changes: 9 additions & 3 deletions packages/did-provider-oyd/src/oyd-did-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ type IContext = IAgentContext<IKeyManager>
* @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 },
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -96,7 +102,7 @@ export class OydDIDProvider extends AbstractIdentifierProvider {
private async holdKeys(args: OydDidHoldKeysArgs, context: IContext): Promise<IKey> {
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,
Expand All @@ -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'],
},
Expand Down
2 changes: 1 addition & 1 deletion packages/did-provider-oyd/src/types/oyd-provider-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type OydCreateIdentifierOptions = {
}

export type OydDidHoldKeysArgs = {
kms?: string
kms: string
options: HoldKeysOpts
}

Expand Down
8 changes: 7 additions & 1 deletion packages/did-provider-web/src/web-did-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ type IContext = IAgentContext<IKeyManager>
* @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<Omit<IIdentifier, 'provider'>> {
const { kms, alias } = args
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion packages/identifier-resolution/__tests__/localAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ let agent: TAgent<IKeyManager & IDIDManager & IIdentifierResolution>

const DID_METHOD = 'did:jwk'

const jwkDIDProvider = new JwkDIDProvider()
const jwkDIDProvider = new JwkDIDProvider({
defaultKms: 'mem',
})

const setup = async (): Promise<boolean> => {
const db: OrPromise<DataSource> = new DataSource({
Expand Down
4 changes: 3 additions & 1 deletion packages/identifier-resolution/__tests__/restAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IKeyManager & IDIDManager & IIdentifierResolution>
Expand Down
4 changes: 3 additions & 1 deletion packages/jwt-service/__tests__/localAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ let agent: TAgent<IKeyManager & IDIDManager & IIdentifierResolution & IJwtServic

const DID_METHOD = 'did:jwk'

const jwkDIDProvider = new JwkDIDProvider()
const jwkDIDProvider = new JwkDIDProvider({
defaultKms: 'mem',
})

const setup = async (): Promise<boolean> => {
const db: OrPromise<DataSource> = new DataSource({
Expand Down
4 changes: 3 additions & 1 deletion packages/jwt-service/__tests__/restAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IKeyManager & IDIDManager & IIdentifierResolution & IJwtService>
Expand Down
4 changes: 2 additions & 2 deletions packages/key-manager/src/agent/SphereonKeyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class SphereonKeyManager extends VeramoKeyManager {
}

override async keyManagerCreate(args: ISphereonKeyManagerCreateArgs): Promise<ManagedKeyInfo> {
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
Expand All @@ -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 })

Expand Down
6 changes: 3 additions & 3 deletions packages/key-utils/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const keyMetaAlgorithmsFromKeyType = (type: string | TKeyType) => {
*/
export async function importProvidedOrGeneratedKey(
args: IImportProvidedOrGeneratedKeyArgs & {
kms?: string
kms: string
},
context: IAgentContext<IKeyManager>
): Promise<IKey> {
Expand Down Expand Up @@ -134,15 +134,15 @@ export async function importProvidedOrGeneratedKey(
if (privateKeyHex) {
return context.agent.keyManagerImport({
...key,
kms: args.kms ?? '',
kms: args.kms,
type,
privateKeyHex: privateKeyHex!,
})
}

return context.agent.keyManagerCreate({
type,
kms: args.kms ?? '',
kms: args.kms,
meta: {
...key?.meta,
algorithms: keyMetaAlgorithmsFromKeyType(type),
Expand Down

0 comments on commit 10d34a9

Please sign in to comment.