Skip to content

Commit

Permalink
Refactor DID Manager
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed Apr 21, 2023
1 parent 6d63a0b commit 3f4b8d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
44 changes: 25 additions & 19 deletions src/did/manager.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
export async function clear(store) {
if (store){
store.clear();
}
}

export async function exists(id, store) {
const value = await store.get(id);
return value !== undefined;
}
export class DidManager {
#store;

export async function get(id, store) {
return store.get(id);
}
constructor(options) {
this.#store = options.store;
}

export async function remove(id, store) {
store.remove(id);
async clear() {
this.#store.clear();
}

async exists(id) {
const value = await this.#store.get(id);
return value !== undefined;
}

async get(id) {
return this.#store.get(id);
}

async remove(id) {
this.#store.remove(id);
}

async set(id, value) {
this.#store.set(id, value);
}
}

export async function set(id, value, store) {
store.set(id, value);
}
16 changes: 9 additions & 7 deletions src/did/web5-did.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Encoder } from '@tbd54566975/dwn-sdk-js';

import { DidConnect } from './connect/connect.js';
import * as CryptoCiphers from './crypto/ciphers.js';
import * as DidManager from './manager.js';
import { DidManager } from './manager.js';
import * as Methods from './methods/methods.js';
import * as DidUtils from './utils.js';
import { MemoryStorage } from '../storage/memory-storage.js';
Expand All @@ -13,7 +13,7 @@ export class Web5Did {
#didConnect;
#web5;

#didStore = new MemoryStorage();
#didManager;
#resolvedDids = new MemoryStorage();

constructor(web5) {
Expand All @@ -28,6 +28,8 @@ export class Web5Did {
// Bind functions to the instance of DidConnect
this.#didConnect.connect = this.#didConnect.connect.bind(this.#didConnect);
this.#didConnect.permissionsRequest = this.#didConnect.permissionsRequest.bind(this.#didConnect);

this.#didManager = new DidManager({ store: new MemoryStorage() });
}

get connect() {
Expand All @@ -40,11 +42,11 @@ export class Web5Did {

get manager() {
return {
clear: (...args) => DidManager.clear(...args, this.#didStore),
exists: (...args) => DidManager.exists(...args, this.#didStore),
get: (...args) => DidManager.get(...args, this.#didStore),
remove: (...args) => DidManager.remove(...args, this.#didStore),
set: (...args) => DidManager.set(...args, this.#didStore),
clear: () => this.#didManager.clear(),
exists: (...args) => this.#didManager.exists(...args),
get: (...args) => this.#didManager.get(...args),
remove: (...args) => this.#didManager.remove(...args),
set: (...args) => this.#didManager.set(...args),
};
}

Expand Down

0 comments on commit 3f4b8d4

Please sign in to comment.