-
Notifications
You must be signed in to change notification settings - Fork 516
/
base.py
190 lines (149 loc) · 5.21 KB
/
base.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""Base Registry."""
from abc import ABC, abstractmethod
from typing import Generic, Optional, Pattern, Sequence, TypeVar
from ..config.injection_context import InjectionContext
from ..core.error import BaseError
from ..core.profile import Profile
from .models.anoncreds_cred_def import (
CredDef,
CredDefResult,
GetCredDefResult,
)
from .models.anoncreds_revocation import (
GetRevListResult,
GetRevRegDefResult,
RevList,
RevListResult,
RevRegDef,
RevRegDefResult,
)
from .models.anoncreds_schema import AnonCredsSchema, GetSchemaResult, SchemaResult
T = TypeVar("T")
class BaseAnonCredsError(BaseError):
"""Base error class for AnonCreds."""
class AnonCredsObjectNotFound(BaseAnonCredsError):
"""Raised when object is not found in resolver."""
def __init__(
self, message: Optional[str] = None, resolution_metadata: Optional[dict] = None
):
"""Constructor."""
super().__init__(message, resolution_metadata)
self.resolution_metadata = resolution_metadata or {}
class AnonCredsRegistrationError(BaseAnonCredsError):
"""Raised when registering an AnonCreds object fails."""
class AnonCredsObjectAlreadyExists(AnonCredsRegistrationError, Generic[T]):
"""Raised when an AnonCreds object already exists."""
def __init__(
self,
message: str,
obj_id: str,
obj: T = None,
*args,
**kwargs,
):
"""Initialize an instance.
Args:
message: Message
obj_id: Object ID
obj: Object
TODO: update this docstring - Anoncreds-break.
"""
super().__init__(message, obj_id, obj, *args, **kwargs)
self._message = message
self.obj_id = obj_id
self.obj = obj
@property
def message(self):
"""Message."""
return f"{self._message}: {self.obj_id}, {self.obj}"
class AnonCredsSchemaAlreadyExists(AnonCredsObjectAlreadyExists[AnonCredsSchema]):
"""Raised when a schema already exists."""
@property
def schema_id(self):
"""Get Schema Id."""
return self.obj_id
@property
def schema(self):
"""Get Schema."""
return self.obj
class AnonCredsResolutionError(BaseAnonCredsError):
"""Raised when resolving an AnonCreds object fails."""
class BaseAnonCredsHandler(ABC):
"""Base Anon Creds Handler."""
@property
@abstractmethod
def supported_identifiers_regex(self) -> Pattern:
"""Regex to match supported identifiers."""
async def supports(self, identifier: str) -> bool:
"""Determine whether this registry supports the given identifier."""
return bool(self.supported_identifiers_regex.match(identifier))
@abstractmethod
async def setup(self, context: InjectionContext):
"""Class Setup method."""
class BaseAnonCredsResolver(BaseAnonCredsHandler):
"""Base Anon Creds Resolver."""
@abstractmethod
async def get_schema(self, profile: Profile, schema_id: str) -> GetSchemaResult:
"""Get a schema from the registry."""
@abstractmethod
async def get_credential_definition(
self, profile: Profile, credential_definition_id: str
) -> GetCredDefResult:
"""Get a credential definition from the registry."""
@abstractmethod
async def get_revocation_registry_definition(
self, profile: Profile, revocation_registry_id: str
) -> GetRevRegDefResult:
"""Get a revocation registry definition from the registry."""
@abstractmethod
async def get_revocation_list(
self, profile: Profile, revocation_registry_id: str, timestamp: int
) -> GetRevListResult:
"""Get a revocation list from the registry."""
class BaseAnonCredsRegistrar(BaseAnonCredsHandler):
"""Base Anon Creds Registrar."""
@abstractmethod
async def register_schema(
self,
profile: Profile,
schema: AnonCredsSchema,
options: Optional[dict] = None,
) -> SchemaResult:
"""Register a schema on the registry."""
@abstractmethod
async def register_credential_definition(
self,
profile: Profile,
schema: GetSchemaResult,
credential_definition: CredDef,
options: Optional[dict] = None,
) -> CredDefResult:
"""Register a credential definition on the registry."""
@abstractmethod
async def register_revocation_registry_definition(
self,
profile: Profile,
revocation_registry_definition: RevRegDef,
options: Optional[dict] = None,
) -> RevRegDefResult:
"""Register a revocation registry definition on the registry."""
@abstractmethod
async def register_revocation_list(
self,
profile: Profile,
rev_reg_def: RevRegDef,
rev_list: RevList,
options: Optional[dict] = None,
) -> RevListResult:
"""Register a revocation list on the registry."""
@abstractmethod
async def update_revocation_list(
self,
profile: Profile,
rev_reg_def: RevRegDef,
prev_list: RevList,
curr_list: RevList,
revoked: Sequence[int],
options: Optional[dict] = None,
) -> RevListResult:
"""Update a revocation list on the registry."""