Skip to content

Commit

Permalink
refactor: changed the localKms to availableKMSes in the SphereonKeyMa…
Browse files Browse the repository at this point in the history
…nager
  • Loading branch information
sksadjad committed May 6, 2024
1 parent 4beabbf commit 7f54e06
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
36 changes: 20 additions & 16 deletions packages/key-manager/src/agent/SphereonKeyManager.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { KeyManager as VeramoKeyManager, AbstractKeyManagementSystem, AbstractKeyStore } from '@veramo/key-manager'

import { IKey, ManagedKeyInfo, TKeyType } from '@veramo/core'
import {IKey, ManagedKeyInfo, TKeyType} from '@veramo/core'
import { KeyType, SphereonKeyManagementSystem } from '@sphereon/ssi-sdk-ext.kms-local'
import { ISphereonKeyManager, ISphereonKeyManagerSignArgs, ISphereonKeyManagerVerifyArgs } from '../types/ISphereonKeyManager'
import {
ISphereonKeyManager,
ISphereonKeyManagerSignArgs,
ISphereonKeyManagerVerifyArgs,
} from '../types/ISphereonKeyManager'

export const sphereonKeyManagerMethods: Array<string> = ['keyManagerCreate','keyManagerImport','keyManagerSign','keyManagerVerify','keyManagerListKeys']

export class SphereonKeyManager extends VeramoKeyManager {
private localStore: AbstractKeyStore
private readonly localKms: Record<string, AbstractKeyManagementSystem>
private readonly availableKMSes: Record<string, AbstractKeyManagementSystem>
readonly localMethods: ISphereonKeyManager

constructor(options: { store: AbstractKeyStore; kms: Record<string, AbstractKeyManagementSystem> }) {
super({ store: options.store, kms: options.kms })
this.localStore = options.store
this.localKms = options.kms
this.availableKMSes = options.kms
const methods = this.methods
methods.keyManagerVerify = this.keyManagerVerify.bind(this)
this.localMethods = <ISphereonKeyManager>(<unknown>methods)
}

private getLocalKms(name: string): AbstractKeyManagementSystem {
const kms = this.localKms[name]
private getAvailableKms(name: string): AbstractKeyManagementSystem {
const kms = this.availableKMSes[name]
if (!kms) {
throw Error(`invalid_argument: This agent has no registered KeyManagementSystem with name='${name}'`)
}
Expand All @@ -29,7 +35,7 @@ export class SphereonKeyManager extends VeramoKeyManager {
//FIXME extend the IKeyManagerSignArgs.data to be a string or array of strings
async keyManagerSign(args: ISphereonKeyManagerSignArgs): Promise<string> {
const keyInfo: IKey = (await this.localStore.get({ kid: args.keyRef })) as IKey
const kms = this.getLocalKms(keyInfo.kms)
const kms = this.getAvailableKms(keyInfo.kms)
if (keyInfo.type === <TKeyType>KeyType.Bls12381G2) {
return await kms.sign({ keyRef: keyInfo, data: Uint8Array.from(Buffer.from(args.data)) })
}
Expand All @@ -38,7 +44,7 @@ export class SphereonKeyManager extends VeramoKeyManager {
}

async keyManagerVerify(args: ISphereonKeyManagerVerifyArgs): Promise<boolean> {
const kms = this.getLocalKms(args.kms)
const kms = this.getAvailableKms(args.kms)
if (('verify' in kms && typeof kms.verify === 'function') || kms instanceof SphereonKeyManagementSystem) {
// @ts-ignore
return await kms.verify(args)
Expand All @@ -47,13 +53,11 @@ export class SphereonKeyManager extends VeramoKeyManager {
}

async keyManagerListKeys(): Promise<ManagedKeyInfo[]> {
const kmsNames: string[] = await this.keyManagerGetKeyManagementSystems()
const keys: ManagedKeyInfo[] = []
for (const kmsName of kmsNames) {
const kms: AbstractKeyManagementSystem = this.getLocalKms(kmsName)
const keyList: ManagedKeyInfo[] = await kms.listKeys()
keys.push(...keyList)
}
return keys
const kmsNames: string[] = await this.keyManagerGetKeyManagementSystems();
const keysArrays = await Promise.all(kmsNames.map(async (kmsName) => {
const kms: AbstractKeyManagementSystem = this.getAvailableKms(kmsName);
return kms.listKeys()
}))
return keysArrays.flat()
}
}
2 changes: 1 addition & 1 deletion packages/key-manager/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const schema = require('../plugin.schema.json')
export { schema }
export { SphereonKeyManager } from './agent/SphereonKeyManager'
export { SphereonKeyManager, sphereonKeyManagerMethods } from './agent/SphereonKeyManager'
export * from './types/ISphereonKeyManager'
export * from '@veramo/key-manager'

0 comments on commit 7f54e06

Please sign in to comment.