-
Notifications
You must be signed in to change notification settings - Fork 9
/
didMethodKeeper.ts
42 lines (34 loc) · 1.08 KB
/
didMethodKeeper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { didMethods } from 'jolocom-lib/js/didMethods'
import { IDidMethod } from 'jolocom-lib/js/didMethods/types'
export class DidMethodKeeper {
methods: { [k: string]: IDidMethod } = {}
private _defaultMethod: IDidMethod
constructor(defaultMethod = didMethods.jolo) {
this._defaultMethod = defaultMethod
this.methods[defaultMethod.prefix] = defaultMethod
}
register(methodName: string, implementation: IDidMethod) {
if (this.methods[methodName]) {
throw new Error('DID method "' + methodName + '" already registered')
}
this.methods[methodName] = implementation
}
get(methodName: string) {
const method = this.methods[methodName]
if (!method) {
throw new Error('no did method "' + methodName + '" registered!')
}
return method
}
getForDid(did: string) {
const methodName = did.split(':')[1]
if (!methodName) throw new Error('could not parse DID: ' + did)
return this.get(methodName)
}
setDefault(method: IDidMethod) {
this._defaultMethod = method
}
getDefault() {
return this._defaultMethod
}
}