forked from hyperledger/indy-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.py
362 lines (298 loc) · 13.2 KB
/
types.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import json
from copy import deepcopy
from hashlib import sha256
from indy_common.constants import TXN_TYPE, ATTRIB, GET_ATTR, \
DATA, GET_NYM, GET_SCHEMA, GET_CLAIM_DEF, ACTION, \
POOL_UPGRADE, POOL_CONFIG, \
DISCLO, SCHEMA, ENDPOINT, CLAIM_DEF, SCHEDULE, SHA256, \
TIMEOUT, JUSTIFICATION, JUSTIFICATION_MAX_SIZE, REINSTALL, WRITES, START, CANCEL, \
REVOC_REG_DEF, ISSUANCE_TYPE, MAX_CRED_NUM, PUBLIC_KEYS, \
TAILS_HASH, TAILS_LOCATION, ID, REVOC_TYPE, TAG, CRED_DEF_ID, VALUE, \
REVOC_REG_ENTRY, ISSUED, REVOC_REG_DEF_ID, REVOKED, ACCUM, PREV_ACCUM, \
GET_REVOC_REG_DEF, GET_REVOC_REG, TIMESTAMP, \
GET_REVOC_REG_DELTA, FROM, TO, POOL_RESTART, DATETIME, VALIDATOR_INFO, SCHEMA_FROM, SCHEMA_NAME, SCHEMA_VERSION, \
SCHEMA_ATTR_NAMES, CLAIM_DEF_SIGNATURE_TYPE, CLAIM_DEF_PUBLIC_KEYS, CLAIM_DEF_TAG, CLAIM_DEF_SCHEMA_REF, \
CLAIM_DEF_PRIMARY, CLAIM_DEF_REVOCATION, CLAIM_DEF_FROM
from plenum.common.constants import TARGET_NYM, NONCE, RAW, ENC, HASH, NAME, \
VERSION, FORCE, ORIGIN, OPERATION_SCHEMA_IS_STRICT
from plenum.common.messages.client_request import ClientMessageValidator as PClientMessageValidator
from plenum.common.messages.client_request import ClientOperationField as PClientOperationField
from plenum.common.messages.fields import ConstantField, IdentifierField, \
LimitedLengthStringField, TxnSeqNoField, \
Sha256HexField, JsonField, MapField, BooleanField, VersionField, \
ChooseField, IntegerField, IterableField, \
AnyMapField, NonEmptyStringField, DatetimeStringField
from plenum.common.messages.message_base import MessageValidator
from plenum.common.messages.node_messages import NonNegativeNumberField
from plenum.common.request import Request as PRequest
from plenum.common.types import OPERATION
from plenum.common.util import is_network_ip_address_valid, is_network_port_valid
from plenum.config import JSON_FIELD_LIMIT, NAME_FIELD_LIMIT, DATA_FIELD_LIMIT, \
NONCE_FIELD_LIMIT, \
ENC_FIELD_LIMIT, RAW_FIELD_LIMIT, SIGNATURE_TYPE_FIELD_LIMIT, \
VERSION_FIELD_LIMIT
class Request(PRequest):
def signingState(self, identifier=None):
"""
Special signing state where the the data for an attribute is hashed
before signing
:return: state to be used when signing
"""
if self.operation.get(TXN_TYPE) == ATTRIB:
d = deepcopy(super().signingState(identifier=identifier))
op = d[OPERATION]
keyName = {RAW, ENC, HASH}.intersection(set(op.keys())).pop()
op[keyName] = sha256(op[keyName].encode()).hexdigest()
return d
return super().signingState(identifier=identifier)
class ClientGetNymOperation(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(GET_NYM)),
(TARGET_NYM, IdentifierField()),
)
class ClientDiscloOperation(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(DISCLO)),
(DATA, LimitedLengthStringField(max_length=DATA_FIELD_LIMIT)),
(NONCE, LimitedLengthStringField(max_length=NONCE_FIELD_LIMIT)),
(TARGET_NYM, IdentifierField(optional=True)),
)
class GetSchemaField(MessageValidator):
schema = (
(SCHEMA_NAME, LimitedLengthStringField(max_length=NAME_FIELD_LIMIT)),
(SCHEMA_VERSION, VersionField(components_number=(2, 3,), max_length=VERSION_FIELD_LIMIT)),
(ORIGIN, IdentifierField(optional=True))
)
class SchemaField(MessageValidator):
schema = (
(SCHEMA_NAME, LimitedLengthStringField(max_length=NAME_FIELD_LIMIT)),
(SCHEMA_VERSION, VersionField(components_number=(2, 3,), max_length=VERSION_FIELD_LIMIT)),
(SCHEMA_ATTR_NAMES, IterableField(
LimitedLengthStringField(max_length=NAME_FIELD_LIMIT),
min_length=1)),
)
class ClaimDefField(MessageValidator):
schema = (
(CLAIM_DEF_PRIMARY, AnyMapField()),
(CLAIM_DEF_REVOCATION, AnyMapField(optional=True)),
)
class RevocDefValueField(MessageValidator):
schema = (
(ISSUANCE_TYPE, NonEmptyStringField()),
(MAX_CRED_NUM, IntegerField()),
(PUBLIC_KEYS, AnyMapField()),
(TAILS_HASH, NonEmptyStringField()),
(TAILS_LOCATION, NonEmptyStringField()),
)
class ClientRevocDefSubmitField(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(REVOC_REG_DEF)),
(ID, NonEmptyStringField()),
(REVOC_TYPE, NonEmptyStringField()),
(TAG, NonEmptyStringField()),
(CRED_DEF_ID, NonEmptyStringField()),
(VALUE, RevocDefValueField())
)
class RevocRegEntryValueField(MessageValidator):
schema = (
(PREV_ACCUM, NonEmptyStringField(optional=True)),
(ACCUM, NonEmptyStringField()),
(ISSUED, IterableField(inner_field_type=IntegerField(), optional=True)),
(REVOKED, IterableField(inner_field_type=IntegerField(), optional=True))
)
class ClientRevocRegEntrySubmitField(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(REVOC_REG_ENTRY)),
(REVOC_REG_DEF_ID, NonEmptyStringField()),
(REVOC_TYPE, NonEmptyStringField()),
(VALUE, RevocRegEntryValueField())
)
class ClientSchemaOperation(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(SCHEMA)),
(DATA, SchemaField()),
)
class ClientGetSchemaOperation(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(GET_SCHEMA)),
(SCHEMA_FROM, IdentifierField()),
(DATA, GetSchemaField()),
)
class ClientAttribOperation(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(ATTRIB)),
(TARGET_NYM, IdentifierField(optional=True)),
(RAW, JsonField(max_length=JSON_FIELD_LIMIT, optional=True)),
(ENC, LimitedLengthStringField(max_length=ENC_FIELD_LIMIT, optional=True)),
(HASH, Sha256HexField(optional=True)),
)
def _validate_message(self, msg):
self._validate_field_set(msg)
if RAW in msg:
self.__validate_raw_field(msg[RAW])
def _validate_field_set(self, msg):
fields_n = sum(1 for f in (RAW, ENC, HASH) if f in msg)
if fields_n == 0:
self._raise_missed_fields(RAW, ENC, HASH)
if fields_n > 1:
self._raise_invalid_message(
"only one field from {}, {}, {} is expected".format(
RAW, ENC, HASH)
)
def __validate_raw_field(self, raw_field):
raw = self.__decode_raw_field(raw_field)
if not isinstance(raw, dict):
self._raise_invalid_fields(RAW, type(raw),
'should be a dict')
if len(raw) != 1:
self._raise_invalid_fields(RAW, raw,
'should contain one attribute')
if ENDPOINT in raw:
self.__validate_endpoint_ha_field(raw[ENDPOINT])
def __decode_raw_field(self, raw_field):
return json.loads(raw_field)
def __validate_endpoint_ha_field(self, endpoint):
if endpoint is None:
return # remove the attribute, valid case
HA_NAME = 'ha'
ha = endpoint.get(HA_NAME)
if ha is None:
return # remove ha attribute, valid case
if ':' not in ha:
self._raise_invalid_fields(ENDPOINT, endpoint,
"invalid endpoint format "
"ip_address:port")
ip, port = ha.split(':')
if not is_network_ip_address_valid(ip):
self._raise_invalid_fields('ha', ha,
'invalid endpoint address')
if not is_network_port_valid(port):
self._raise_invalid_fields('ha', ha,
'invalid endpoint port')
class ClientGetAttribOperation(ClientAttribOperation):
schema = (
(TXN_TYPE, ConstantField(GET_ATTR)),
(TARGET_NYM, IdentifierField(optional=True)),
(RAW, LimitedLengthStringField(max_length=RAW_FIELD_LIMIT, optional=True)),
(ENC, LimitedLengthStringField(max_length=ENC_FIELD_LIMIT, optional=True)),
(HASH, Sha256HexField(optional=True)),
)
def _validate_message(self, msg):
self._validate_field_set(msg)
class ClientClaimDefSubmitOperation(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(CLAIM_DEF)),
(CLAIM_DEF_SCHEMA_REF, TxnSeqNoField()),
(CLAIM_DEF_PUBLIC_KEYS, ClaimDefField()),
(CLAIM_DEF_SIGNATURE_TYPE, LimitedLengthStringField(max_length=SIGNATURE_TYPE_FIELD_LIMIT)),
(CLAIM_DEF_TAG, LimitedLengthStringField(max_length=256, optional=True)),
)
class ClientClaimDefGetOperation(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(GET_CLAIM_DEF)),
(CLAIM_DEF_SCHEMA_REF, TxnSeqNoField()),
(CLAIM_DEF_FROM, IdentifierField()),
(CLAIM_DEF_SIGNATURE_TYPE, LimitedLengthStringField(max_length=SIGNATURE_TYPE_FIELD_LIMIT)),
(CLAIM_DEF_TAG, LimitedLengthStringField(max_length=256, optional=True)),
)
class ClientGetRevocRegDefField(MessageValidator):
schema = (
(ID, NonEmptyStringField()),
(TXN_TYPE, ConstantField(GET_REVOC_REG_DEF)),
)
class ClientGetRevocRegField(MessageValidator):
schema = (
(REVOC_REG_DEF_ID, NonEmptyStringField()),
(TIMESTAMP, IntegerField()),
(TXN_TYPE, ConstantField(GET_REVOC_REG)),
)
class ClientGetRevocRegDeltaField(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(GET_REVOC_REG_DELTA)),
(REVOC_REG_DEF_ID, NonEmptyStringField()),
(FROM, IntegerField(optional=True)),
(TO, IntegerField()),
)
class ClientPoolUpgradeOperation(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(POOL_UPGRADE)),
(ACTION, ChooseField(values=(START, CANCEL,))),
(VERSION, VersionField(components_number=(2, 3,), max_length=VERSION_FIELD_LIMIT)),
# TODO replace actual checks (idr, datetime)
(SCHEDULE, MapField(IdentifierField(),
NonEmptyStringField(), optional=True)),
(SHA256, Sha256HexField()),
(TIMEOUT, NonNegativeNumberField(optional=True)),
(JUSTIFICATION, LimitedLengthStringField(max_length=JUSTIFICATION_MAX_SIZE, optional=True, nullable=True)),
(NAME, LimitedLengthStringField(max_length=NAME_FIELD_LIMIT)),
(FORCE, BooleanField(optional=True)),
(REINSTALL, BooleanField(optional=True)),
)
class ClientPoolRestartOperation(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(POOL_RESTART)),
(ACTION, ChooseField(values=(START, CANCEL,))),
(DATETIME, DatetimeStringField(exceptional_values=["0", ""],
optional=True)),
)
class ClientValidatorInfoOperation(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(VALIDATOR_INFO)),
)
class ClientPoolConfigOperation(MessageValidator):
schema = (
(TXN_TYPE, ConstantField(POOL_CONFIG)),
(WRITES, BooleanField()),
(FORCE, BooleanField(optional=True)),
)
class ClientOperationField(PClientOperationField):
_specific_operations = {
SCHEMA: ClientSchemaOperation(),
ATTRIB: ClientAttribOperation(),
GET_ATTR: ClientGetAttribOperation(),
CLAIM_DEF: ClientClaimDefSubmitOperation(),
GET_CLAIM_DEF: ClientClaimDefGetOperation(),
DISCLO: ClientDiscloOperation(),
GET_NYM: ClientGetNymOperation(),
GET_SCHEMA: ClientGetSchemaOperation(),
POOL_UPGRADE: ClientPoolUpgradeOperation(),
POOL_CONFIG: ClientPoolConfigOperation(),
POOL_RESTART: ClientPoolRestartOperation(),
VALIDATOR_INFO: ClientValidatorInfoOperation(),
REVOC_REG_DEF: ClientRevocDefSubmitField(),
REVOC_REG_ENTRY: ClientRevocRegEntrySubmitField(),
GET_REVOC_REG_DEF: ClientGetRevocRegDefField(),
GET_REVOC_REG: ClientGetRevocRegField(),
GET_REVOC_REG_DELTA: ClientGetRevocRegDeltaField(),
}
# TODO: it is a workaround because INDY-338, `operations` must be a class
# constant
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.operations.update(self._specific_operations)
class ClientMessageValidator(PClientMessageValidator):
# extend operation field
schema = tuple(
map(lambda x: (x[0], ClientOperationField()) if x[0] == OPERATION else x,
PClientMessageValidator.schema)
)
# THE CODE BELOW MIGHT BE NEEDED IN THE FUTURE, THEREFORE KEEPING IT
# class LedgerIdField(PLedgerIdField):
# ledger_ids = PLedgerIdField.ledger_ids + (CONFIG_LEDGER_ID,)
#
#
# class LedgerInfoField(PLedgerInfoField):
# _ledger_id_class = LedgerIdField
# TODO: it is a workaround which helps extend some fields from
# downstream projects, should be removed after we find a better way
# to do this
# node_message_factory.update_schemas_by_field_type(
# PLedgerIdField, LedgerIdField)
# node_message_factory.update_schemas_by_field_type(
# PLedgerInfoField, LedgerInfoField)
class SafeRequest(Request, ClientMessageValidator):
def __init__(self, **kwargs):
ClientMessageValidator.__init__(self,
operation_schema_is_strict=OPERATION_SCHEMA_IS_STRICT)
self.validate(kwargs)
Request.__init__(self, **kwargs)