Skip to content

Commit

Permalink
fix(key-manager): rename Abstract[Private]KeyStore methods for SES co…
Browse files Browse the repository at this point in the history
…mpatibility

The only problem method name was `import`, but all methods were renamed to maintain consistency in the API.

fixes #1090

BREAKING CHANGE: implementations of AbstractKeyStore and AbstractPrivateKeyStore need to rename their methods to conform to the new API. Functionality remains the same.
  • Loading branch information
mirceanis committed Jan 30, 2023
1 parent b5cea3c commit 91631b6
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 49 deletions.
8 changes: 4 additions & 4 deletions packages/data-store-json/src/identifier/key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export class KeyStoreJson extends AbstractKeyStore {
}
}

async get({ kid }: { kid: string }): Promise<IKey> {
async getKey({ kid }: { kid: string }): Promise<IKey> {
if (this.cacheTree.keys[kid]) {
return deserialize(serialize(this.cacheTree.keys[kid]))
} else {
throw Error('not_found: Key not found')
}
}

async delete({ kid }: { kid: string }) {
async deleteKey({ kid }: { kid: string }) {
if (this.cacheTree.keys[kid]) {
const oldTree = deserialize(serialize(this.cacheTree, { lossy: true }))
delete this.cacheTree.keys[kid]
Expand All @@ -59,14 +59,14 @@ export class KeyStoreJson extends AbstractKeyStore {
}
}

async import(args: IKey) {
async importKey(args: IKey) {
const oldTree = deserialize(serialize(this.cacheTree, { lossy: true }))
this.cacheTree.keys[args.kid] = args
await this.notifyUpdate(oldTree, this.cacheTree)
return true
}

async list(args: {} = {}): Promise<ManagedKeyInfo[]> {
async listKeys(args: {} = {}): Promise<ManagedKeyInfo[]> {
const keys = Object.values(this.cacheTree.keys).map((key: IKey) => {
const { kid, publicKeyHex, type, meta, kms } = key
return { kid, publicKeyHex, type, meta: deserialize(serialize(meta)), kms } as ManagedKeyInfo
Expand Down
8 changes: 4 additions & 4 deletions packages/data-store-json/src/identifier/private-key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class PrivateKeyStoreJson extends AbstractPrivateKeyStore {
}
}

async get({ alias }: { alias: string }): Promise<ManagedPrivateKey> {
async getKey({ alias }: { alias: string }): Promise<ManagedPrivateKey> {
const key = deserialize(serialize(this.cacheTree.privateKeys[alias]))
if (!key) throw Error('not_found: PrivateKey not found')
if (this.secretBox && key.privateKeyHex) {
Expand All @@ -52,7 +52,7 @@ export class PrivateKeyStoreJson extends AbstractPrivateKeyStore {
return key
}

async delete({ alias }: { alias: string }) {
async deleteKey({ alias }: { alias: string }) {
debug(`Deleting private key data for alias=${alias}`)
const privateKeyEntry = this.cacheTree.privateKeys[alias]
if (privateKeyEntry) {
Expand All @@ -63,7 +63,7 @@ export class PrivateKeyStoreJson extends AbstractPrivateKeyStore {
return true
}

async import(args: ImportablePrivateKey): Promise<ManagedPrivateKey> {
async importKey(args: ImportablePrivateKey): Promise<ManagedPrivateKey> {
debug('Saving private key data', args.alias)
const alias = args.alias || uuid4()
const key: ManagedPrivateKey = deserialize(serialize({
Expand All @@ -88,7 +88,7 @@ export class PrivateKeyStoreJson extends AbstractPrivateKeyStore {
return key
}

async list(): Promise<Array<ManagedPrivateKey>> {
async listKeys(): Promise<Array<ManagedPrivateKey>> {
return deserialize(serialize(Object.values(this.cacheTree.privateKeys)))
}
}
8 changes: 4 additions & 4 deletions packages/data-store/src/identifier/key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ export class KeyStore extends AbstractKeyStore {
super()
}

async get({ kid }: { kid: string }): Promise<IKey> {
async getKey({ kid }: { kid: string }): Promise<IKey> {
const key = await (await getConnectedDb(this.dbConnection)).getRepository(Key).findOneBy({ kid })
if (!key) throw Error('Key not found')
return key as IKey
}

async delete({ kid }: { kid: string }) {
async deleteKey({ kid }: { kid: string }) {
const key = await (await getConnectedDb(this.dbConnection)).getRepository(Key).findOneBy({ kid })
if (!key) throw Error('Key not found')
debug('Deleting key', kid)
await (await getConnectedDb(this.dbConnection)).getRepository(Key).remove(key)
return true
}

async import(args: IKey) {
async importKey(args: IKey) {
const key = new Key()
key.kid = args.kid
key.publicKeyHex = args.publicKeyHex
Expand All @@ -53,7 +53,7 @@ export class KeyStore extends AbstractKeyStore {
return true
}

async list(args: {} = {}): Promise<ManagedKeyInfo[]> {
async listKeys(args: {} = {}): Promise<ManagedKeyInfo[]> {
const keys = await (await getConnectedDb(this.dbConnection)).getRepository(Key).find()
const managedKeys: ManagedKeyInfo[] = keys.map((key) => {
const { kid, publicKeyHex, type, meta, kms } = key
Expand Down
8 changes: 4 additions & 4 deletions packages/data-store/src/identifier/private-key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class PrivateKeyStore extends AbstractPrivateKeyStore {
}
}

async get({ alias }: { alias: string }): Promise<ManagedPrivateKey> {
async getKey({ alias }: { alias: string }): Promise<ManagedPrivateKey> {
const key = await (await getConnectedDb(this.dbConnection)).getRepository(PrivateKey).findOneBy({ alias })
if (!key) throw Error('Key not found')
if (this.secretBox && key.privateKeyHex) {
Expand All @@ -35,15 +35,15 @@ export class PrivateKeyStore extends AbstractPrivateKeyStore {
return key as ManagedPrivateKey
}

async delete({ alias }: { alias: string }) {
async deleteKey({ alias }: { alias: string }) {
const key = await (await getConnectedDb(this.dbConnection)).getRepository(PrivateKey).findOneBy({ alias })
if (!key) throw Error(`not_found: Private Key data not found for alias=${alias}`)
debug('Deleting private key data', alias)
await (await getConnectedDb(this.dbConnection)).getRepository(PrivateKey).remove(key)
return true
}

async import(args: ImportablePrivateKey): Promise<ManagedPrivateKey> {
async importKey(args: ImportablePrivateKey): Promise<ManagedPrivateKey> {
const key = new PrivateKey()
key.alias = args.alias || uuid4()
key.privateKeyHex = args.privateKeyHex
Expand All @@ -63,7 +63,7 @@ export class PrivateKeyStore extends AbstractPrivateKeyStore {
return key
}

async list(): Promise<Array<ManagedPrivateKey>> {
async listKeys(): Promise<Array<ManagedPrivateKey>> {
const keys = await (await getConnectedDb(this.dbConnection)).getRepository(PrivateKey).find()
return keys
}
Expand Down
8 changes: 4 additions & 4 deletions packages/key-manager/src/__tests__/abstract-key-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AbstractKeyStore } from '../abstract-key-store'
import { IKey, ManagedKeyInfo } from '@veramo/core'

class MockKeyStore extends AbstractKeyStore {
async list(args: {}): Promise<ManagedKeyInfo[]> {
async listKeys(args: {}): Promise<ManagedKeyInfo[]> {
return [
{
kid: '',
Expand All @@ -12,7 +12,7 @@ class MockKeyStore extends AbstractKeyStore {
},
]
}
async get({ kid }: { kid: string }): Promise<IKey> {
async getKey({ kid }: { kid: string }): Promise<IKey> {
return {
kid: '',
kms: '',
Expand All @@ -21,11 +21,11 @@ class MockKeyStore extends AbstractKeyStore {
}
}

async delete({ kid }: { kid: string }) {
async deleteKey({ kid }: { kid: string }) {
return true
}

async import(args: IKey): Promise<boolean> {
async importKey(args: IKey): Promise<boolean> {
return true
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/key-manager/src/abstract-key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { IKey, ManagedKeyInfo } from '@veramo/core'
* @public
*/
export abstract class AbstractKeyStore {
abstract import(args: Partial<IKey>): Promise<boolean>
abstract importKey(args: Partial<IKey>): Promise<boolean>

abstract get(args: { kid: string }): Promise<IKey>
abstract getKey(args: { kid: string }): Promise<IKey>

abstract delete(args: { kid: string }): Promise<boolean>
abstract deleteKey(args: { kid: string }): Promise<boolean>

abstract list(args: {}): Promise<Array<ManagedKeyInfo>>
abstract listKeys(args: {}): Promise<Array<ManagedKeyInfo>>
}
8 changes: 4 additions & 4 deletions packages/key-manager/src/abstract-private-key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export type ImportablePrivateKey = RequireOnly<ManagedPrivateKey, 'privateKeyHex
* @public
*/
export abstract class AbstractPrivateKeyStore {
abstract import(args: ImportablePrivateKey): Promise<ManagedPrivateKey>
abstract importKey(args: ImportablePrivateKey): Promise<ManagedPrivateKey>

abstract get(args: { alias: string }): Promise<ManagedPrivateKey>
abstract getKey(args: { alias: string }): Promise<ManagedPrivateKey>

abstract delete(args: { alias: string }): Promise<boolean>
abstract deleteKey(args: { alias: string }): Promise<boolean>

abstract list(args: {}): Promise<Array<ManagedPrivateKey>>
abstract listKeys(args: {}): Promise<Array<ManagedPrivateKey>>
}
16 changes: 8 additions & 8 deletions packages/key-manager/src/key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class KeyManager implements IAgentPlugin {
if (args.meta || key.meta) {
key.meta = { ...args.meta, ...key.meta }
}
await this.store.import(key)
await this.store.importKey(key)
if (key.privateKeyHex) {
delete key.privateKeyHex
}
Expand All @@ -102,15 +102,15 @@ export class KeyManager implements IAgentPlugin {

/** {@inheritDoc @veramo/core#IKeyManager.keyManagerGet} */
async keyManagerGet({ kid }: IKeyManagerGetArgs): Promise<IKey> {
return this.store.get({ kid })
return this.store.getKey({ kid })
}

/** {@inheritDoc @veramo/core#IKeyManager.keyManagerDelete} */
async keyManagerDelete({ kid }: IKeyManagerDeleteArgs): Promise<boolean> {
const key = await this.store.get({ kid })
const key = await this.store.getKey({ kid })
const kms = this.getKms(key.kms)
await kms.deleteKey({ kid })
return this.store.delete({ kid })
return this.store.deleteKey({ kid })
}

/** {@inheritDoc @veramo/core#IKeyManager.keyManagerImport} */
Expand All @@ -119,7 +119,7 @@ export class KeyManager implements IAgentPlugin {
const managedKey = await kms.importKey(key)
const { meta } = key
const importedKey = { ...managedKey, meta: { ...meta, ...managedKey.meta }, kms: key.kms }
await this.store.import(importedKey)
await this.store.importKey(importedKey)
return importedKey
}

Expand Down Expand Up @@ -171,7 +171,7 @@ export class KeyManager implements IAgentPlugin {
/** {@inheritDoc @veramo/core#IKeyManager.keyManagerSign} */
async keyManagerSign(args: IKeyManagerSignArgs): Promise<string> {
const { keyRef, data, algorithm, encoding, ...extras } = { encoding: 'utf-8', ...args }
const keyInfo: ManagedKeyInfo = await this.store.get({ kid: keyRef })
const keyInfo: ManagedKeyInfo = await this.store.getKey({ kid: keyRef })
let dataBytes
if (typeof data === 'string') {
if (encoding === 'base16' || encoding === 'hex') {
Expand All @@ -192,7 +192,7 @@ export class KeyManager implements IAgentPlugin {
const { v, r, s, from, ...tx } = <any>transaction
if (typeof from === 'string') {
debug('WARNING: executing a transaction signing request with a `from` field.')
const key = await this.store.get({ kid })
const key = await this.store.getKey({ kid })
if (key.publicKeyHex) {
const address = computeAddress('0x' + key.publicKeyHex)
if (address.toLowerCase() !== from.toLowerCase()) {
Expand All @@ -211,7 +211,7 @@ export class KeyManager implements IAgentPlugin {
/** {@inheritDoc @veramo/core#IKeyManager.keyManagerSharedSecret} */
async keyManagerSharedSecret(args: IKeyManagerSharedSecretArgs): Promise<string> {
const { secretKeyRef, publicKey } = args
const myKeyRef = await this.store.get({ kid: secretKeyRef })
const myKeyRef = await this.store.getKey({ kid: secretKeyRef })
const theirKey = publicKey
if (
myKeyRef.type === theirKey.type ||
Expand Down
16 changes: 8 additions & 8 deletions packages/key-manager/src/memory-key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ import { v4 as uuidv4 } from 'uuid'
export class MemoryKeyStore extends AbstractKeyStore {
private keys: Record<string, IKey> = {}

async get({ kid }: { kid: string }): Promise<IKey> {
async getKey({ kid }: { kid: string }): Promise<IKey> {
const key = this.keys[kid]
if (!key) throw Error('Key not found')
return key
}

async delete({ kid }: { kid: string }) {
async deleteKey({ kid }: { kid: string }) {
delete this.keys[kid]
return true
}

async import(args: IKey) {
async importKey(args: IKey) {
this.keys[args.kid] = { ...args }
return true
}

async list(args: {}): Promise<Exclude<IKey, 'privateKeyHex'>[]> {
async listKeys(args: {}): Promise<Exclude<IKey, 'privateKeyHex'>[]> {
const safeKeys = Object.values(this.keys).map((key) => {
const { privateKeyHex, ...safeKey } = key
return safeKey
Expand All @@ -53,18 +53,18 @@ export class MemoryKeyStore extends AbstractKeyStore {
export class MemoryPrivateKeyStore extends AbstractPrivateKeyStore {
private privateKeys: Record<string, ManagedPrivateKey> = {}

async get({ alias }: { alias: string }): Promise<ManagedPrivateKey> {
async getKey({ alias }: { alias: string }): Promise<ManagedPrivateKey> {
const key = this.privateKeys[alias]
if (!key) throw Error(`not_found: PrivateKey not found for alias=${alias}`)
return key
}

async delete({ alias }: { alias: string }) {
async deleteKey({ alias }: { alias: string }) {
delete this.privateKeys[alias]
return true
}

async import(args: ImportablePrivateKey) {
async importKey(args: ImportablePrivateKey) {
const alias = args.alias || uuidv4()
const existingEntry = this.privateKeys[alias]
if (existingEntry && existingEntry.privateKeyHex !== args.privateKeyHex) {
Expand All @@ -74,7 +74,7 @@ export class MemoryPrivateKeyStore extends AbstractPrivateKeyStore {
return this.privateKeys[alias]
}

async list(): Promise<Array<ManagedPrivateKey>> {
async listKeys(): Promise<Array<ManagedPrivateKey>> {
return [...Object.values(this.privateKeys)]
}
}
10 changes: 5 additions & 5 deletions packages/kms-local/src/key-management-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ export class KeyManagementSystem extends AbstractKeyManagementSystem {
throw new Error('invalid_argument: type and privateKeyHex are required to import a key')
}
const managedKey = this.asManagedKeyInfo({ alias: args.kid, ...args })
await this.keyStore.import({ alias: managedKey.kid, ...args })
await this.keyStore.importKey({ alias: managedKey.kid, ...args })
debug('imported key', managedKey.type, managedKey.publicKeyHex)
return managedKey
}

async listKeys(): Promise<ManagedKeyInfo[]> {
const privateKeys = await this.keyStore.list({})
const privateKeys = await this.keyStore.listKeys({})
const managedKeys = privateKeys.map((key) => this.asManagedKeyInfo(key))
return managedKeys
}
Expand Down Expand Up @@ -99,7 +99,7 @@ export class KeyManagementSystem extends AbstractKeyManagementSystem {
}

async deleteKey(args: { kid: string }) {
return await this.keyStore.delete({ alias: args.kid })
return await this.keyStore.deleteKey({ alias: args.kid })
}

async sign({
Expand All @@ -113,7 +113,7 @@ export class KeyManagementSystem extends AbstractKeyManagementSystem {
}): Promise<string> {
let managedKey: ManagedPrivateKey
try {
managedKey = await this.keyStore.get({ alias: keyRef.kid })
managedKey = await this.keyStore.getKey({ alias: keyRef.kid })
} catch (e) {
throw new Error(`key_not_found: No key entry found for kid=${keyRef.kid}`)
}
Expand Down Expand Up @@ -150,7 +150,7 @@ export class KeyManagementSystem extends AbstractKeyManagementSystem {
}): Promise<string> {
let myKey: ManagedPrivateKey
try {
myKey = await this.keyStore.get({ alias: args.myKeyRef.kid })
myKey = await this.keyStore.getKey({ alias: args.myKeyRef.kid })
} catch (e) {
throw new Error(`key_not_found: No key entry found for kid=${args.myKeyRef.kid}`)
}
Expand Down

0 comments on commit 91631b6

Please sign in to comment.