forked from hyperledger/indy-plenum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdid_method.py
46 lines (34 loc) · 1.33 KB
/
did_method.py
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
43
44
45
46
from typing import Callable, Sequence
from plenum.common.exceptions import DidMethodNotFound
from plenum.common.signer_did import DidSigner
from stp_core.types import Identifier
Seed = str
SignerConstructor = Callable[[Identifier], Seed]
class DidMethod:
def __init__(self,
name,
pattern,
signerConstructor: SignerConstructor=None):
self.name = name
self.pattern = pattern
self.signerConstructor = signerConstructor or DidSigner
def newSigner(self, identifier, seed):
return self.signerConstructor(identifier=identifier, seed=seed)
PlenumDidMethod = DidMethod('plenum', 'did:plenum:')
class DidMethods:
def __init__(self, *didMethods: Sequence[DidMethod]):
self.d = {}
for dm in didMethods:
self.d[dm.name] = dm
self.default = didMethods[0] if didMethods else None
def get(self, didMethodName, required=True) -> DidMethod:
"""
:param didMethodName: name of DID Method
:param required: if not found and True, throws an exception, else None
:return: DID Method
"""
dm = self.d.get(didMethodName) if didMethodName else self.default
if not dm and required:
raise DidMethodNotFound
return dm
DefaultDidMethods = DidMethods(PlenumDidMethod)