From a2521e0e36a9531478910815d17409bc749658c2 Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Thu, 13 Feb 2020 16:58:24 +0200 Subject: [PATCH] feat: FS IdentityStore --- .../src/identity/abstract-identity-store.ts | 2 +- packages/daf-core/src/index.ts | 1 + .../daf-ethr-did-fs/src/identity-store.ts | 54 +++++++++++++++++++ .../src/key-management-system.ts | 5 +- 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 packages/daf-ethr-did-fs/src/identity-store.ts diff --git a/packages/daf-core/src/identity/abstract-identity-store.ts b/packages/daf-core/src/identity/abstract-identity-store.ts index 90f013003..ff619da49 100644 --- a/packages/daf-core/src/identity/abstract-identity-store.ts +++ b/packages/daf-core/src/identity/abstract-identity-store.ts @@ -8,7 +8,7 @@ export interface SerializedIdentity { export abstract class AbstractIdentityStore { abstract set(did: string, serializedIdentity: SerializedIdentity): Promise - abstract get(did: string): Promise + abstract get(did: string): Promise abstract delete(did: string): Promise abstract listDids(): Promise } diff --git a/packages/daf-core/src/index.ts b/packages/daf-core/src/index.ts index 70008fbc3..6936aedb4 100644 --- a/packages/daf-core/src/index.ts +++ b/packages/daf-core/src/index.ts @@ -9,6 +9,7 @@ export { KeyType, SerializedKey, } from './identity/abstract-key-management-system' +export { AbstractIdentityStore, SerializedIdentity } from './identity/abstract-identity-store' export { AbstractMessageValidator } from './message/abstract-message-validator' export { Message } from './message/message' export { ServiceManager, LastMessageTimestampForInstance, ServiceEventTypes } from './service/service-manager' diff --git a/packages/daf-ethr-did-fs/src/identity-store.ts b/packages/daf-ethr-did-fs/src/identity-store.ts new file mode 100644 index 000000000..9989f1c9d --- /dev/null +++ b/packages/daf-ethr-did-fs/src/identity-store.ts @@ -0,0 +1,54 @@ +import { AbstractIdentityStore, SerializedIdentity } from 'daf-core' +const fs = require('fs') + +interface FileContents { + [did: string]: SerializedIdentity +} + +export class IdentityStore extends AbstractIdentityStore { + constructor(private fileName: string) { + super() + } + + async get(did: string) { + const fileContents = this.readFromFile() + if (!fileContents[did]) throw Error('Identity does not found') + return fileContents[did] + } + + async delete(did: string) { + const fileContents = this.readFromFile() + if (!fileContents[did]) throw Error('Identity does not found') + delete fileContents[did] + return this.writeToFile(fileContents) + } + + async set(did: string, serializedIdentity: SerializedIdentity) { + const fileContents = this.readFromFile() + fileContents[did] = serializedIdentity + return this.writeToFile(fileContents) + } + + async listDids() { + const fileContents = this.readFromFile() + return Object.keys(fileContents) + } + + private readFromFile(): FileContents { + try { + const raw = fs.readFileSync(this.fileName) + return JSON.parse(raw) as FileContents + } catch (e) { + return {} + } + } + + private writeToFile(json: FileContents): boolean { + try { + fs.writeFileSync(this.fileName, JSON.stringify(json)) + return true + } catch (e) { + return false + } + } +} diff --git a/packages/daf-ethr-did-fs/src/key-management-system.ts b/packages/daf-ethr-did-fs/src/key-management-system.ts index c8b80aec0..38039dbf4 100644 --- a/packages/daf-ethr-did-fs/src/key-management-system.ts +++ b/packages/daf-ethr-did-fs/src/key-management-system.ts @@ -47,11 +47,8 @@ interface FileContents { } export class KeyManagementSystem extends AbstractKeyManagementSystem { - private fileName: string - - constructor(options: { fileName: string }) { + constructor(private fileName: string) { super() - this.fileName = options.fileName } private readFromFile(): FileContents {