Skip to content

Commit

Permalink
feat: FS IdentityStore
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Feb 13, 2020
1 parent 1bcf4c1 commit a2521e0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/daf-core/src/identity/abstract-identity-store.ts
Expand Up @@ -8,7 +8,7 @@ export interface SerializedIdentity {

export abstract class AbstractIdentityStore {
abstract set(did: string, serializedIdentity: SerializedIdentity): Promise<boolean>
abstract get(did: string): Promise<SerializedKey>
abstract get(did: string): Promise<SerializedIdentity>
abstract delete(did: string): Promise<boolean>
abstract listDids(): Promise<string[]>
}
1 change: 1 addition & 0 deletions packages/daf-core/src/index.ts
Expand Up @@ -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'
Expand Down
54 changes: 54 additions & 0 deletions 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
}
}
}
5 changes: 1 addition & 4 deletions packages/daf-ethr-did-fs/src/key-management-system.ts
Expand Up @@ -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 {
Expand Down

0 comments on commit a2521e0

Please sign in to comment.