import { DidDocument } from '../../src/types' interface DidStore { put(key: string, val: string): Promise del(key: string): Promise get(key: string): Promise } class MemoryStore implements DidStore { private store: Record = {} async put(key: string, val: string): Promise { this.store[key] = val } async del(key: string): Promise { this.assertHas(key) delete this.store[key] } async get(key: string): Promise { this.assertHas(key) return this.store[key] } assertHas(key: string): void { if (!this.store[key]) { throw new Error(`No object with key: ${key}`) } } } export class DidWebDb { constructor(private store: DidStore) {} static memory(): DidWebDb { const store = new MemoryStore() return new DidWebDb(store) } async put(didPath: string, didDoc: DidDocument): Promise { await this.store.put(didPath, JSON.stringify(didDoc)) } async get(didPath: string): Promise { try { const got = await this.store.get(didPath) return JSON.parse(got) } catch (err) { console.log(`Could not get did with path ${didPath}: ${err}`) return null } } async has(didPath: string): Promise { const got = await this.get(didPath) return got !== null } async del(didPath: string): Promise { await this.store.del(didPath) } } export default DidWebDb