-
Notifications
You must be signed in to change notification settings - Fork 515
/
Copy pathpres_exch_handler.py
286 lines (267 loc) · 12.1 KB
/
pres_exch_handler.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
"""Utilities for dif presentation exchange attachment."""
import json
import logging
import time
from typing import Union, Tuple
from ....core.error import BaseError
from ....core.profile import Profile
from ....indy.holder import IndyHolder, IndyHolderError
from ....indy.models.xform import indy_proof_req2non_revoc_intervals
from ....ledger.multiple_ledger.ledger_requests_executor import (
GET_SCHEMA,
GET_REVOC_REG_DELTA,
IndyLedgerRequestsExecutor,
)
from ....multitenant.base import BaseMultitenantManager
from ....revocation.models.revocation_registry import RevocationRegistry
from ..v1_0.models.presentation_exchange import V10PresentationExchange
from ..v2_0.messages.pres_format import V20PresFormat
from ..v2_0.models.pres_exchange import V20PresExRecord
LOGGER = logging.getLogger(__name__)
class IndyPresExchHandlerError(BaseError):
"""Base class for Indy Presentation Exchange related errors."""
class IndyPresExchHandler:
"""Base Presentation Exchange Handler."""
def __init__(
self,
profile: Profile,
):
"""Initialize PresExchange Handler."""
super().__init__()
self._profile = profile
async def return_presentation(
self,
pres_ex_record: Union[V10PresentationExchange, V20PresExRecord],
requested_credentials: dict = {},
) -> dict:
"""Return Indy proof request as dict."""
# Get all credentials for this presentation
holder = self._profile.inject(IndyHolder)
credentials = {}
# extract credential ids and non_revoked
requested_referents = {}
if isinstance(pres_ex_record, V20PresExRecord):
proof_request = pres_ex_record.pres_request.attachment(
V20PresFormat.Format.INDY
)
elif isinstance(pres_ex_record, V10PresentationExchange):
proof_request = pres_ex_record._presentation_request.ser
non_revoc_intervals = indy_proof_req2non_revoc_intervals(proof_request)
attr_creds = requested_credentials.get("requested_attributes", {})
req_attrs = proof_request.get("requested_attributes", {})
for reft in attr_creds:
requested_referents[reft] = {"cred_id": attr_creds[reft]["cred_id"]}
if reft in req_attrs and reft in non_revoc_intervals:
requested_referents[reft]["non_revoked"] = non_revoc_intervals[reft]
pred_creds = requested_credentials.get("requested_predicates", {})
req_preds = proof_request.get("requested_predicates", {})
for reft in pred_creds:
requested_referents[reft] = {"cred_id": pred_creds[reft]["cred_id"]}
if reft in req_preds and reft in non_revoc_intervals:
requested_referents[reft]["non_revoked"] = non_revoc_intervals[reft]
# extract mapping of presentation referents to credential ids
for reft in requested_referents:
credential_id = requested_referents[reft]["cred_id"]
if credential_id not in credentials:
credentials[credential_id] = json.loads(
await holder.get_credential(credential_id)
)
# remove any timestamps that cannot correspond to non-revoc intervals
for r in ("requested_attributes", "requested_predicates"):
for reft, req_item in requested_credentials.get(r, {}).items():
if not credentials[req_item["cred_id"]].get(
"rev_reg_id"
) and req_item.pop("timestamp", None):
LOGGER.info(
f"Removed superfluous timestamp from requested_credentials {r} "
f"{reft} for non-revocable credential {req_item['cred_id']}"
)
# Get all schemas, credential definitions, and revocation registries in use
schemas = {}
cred_defs = {}
revocation_registries = {}
for credential in credentials.values():
schema_id = credential["schema_id"]
multitenant_mgr = self._profile.inject_or(BaseMultitenantManager)
if multitenant_mgr:
ledger_exec_inst = IndyLedgerRequestsExecutor(self._profile)
else:
ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor)
ledger = (
await ledger_exec_inst.get_ledger_for_identifier(
schema_id,
txn_record_type=GET_SCHEMA,
)
)[1]
async with ledger:
if schema_id not in schemas:
schemas[schema_id] = await ledger.get_schema(schema_id)
cred_def_id = credential["cred_def_id"]
if cred_def_id not in cred_defs:
cred_defs[cred_def_id] = await ledger.get_credential_definition(
cred_def_id
)
if credential.get("rev_reg_id"):
revocation_registry_id = credential["rev_reg_id"]
if revocation_registry_id not in revocation_registries:
revocation_registries[
revocation_registry_id
] = RevocationRegistry.from_definition(
await ledger.get_revoc_reg_def(revocation_registry_id), True
)
# Get delta with non-revocation interval defined in "non_revoked"
# of the presentation request or attributes
epoch_now = int(time.time())
revoc_reg_deltas = {}
for precis in requested_referents.values(): # cred_id, non-revoc interval
credential_id = precis["cred_id"]
if not credentials[credential_id].get("rev_reg_id"):
continue
if "timestamp" in precis:
continue
rev_reg_id = credentials[credential_id]["rev_reg_id"]
multitenant_mgr = self._profile.inject_or(BaseMultitenantManager)
if multitenant_mgr:
ledger_exec_inst = IndyLedgerRequestsExecutor(self._profile)
else:
ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor)
ledger = (
await ledger_exec_inst.get_ledger_for_identifier(
rev_reg_id,
txn_record_type=GET_REVOC_REG_DELTA,
)
)[1]
async with ledger:
reft_non_revoc_interval = precis.get("non_revoked")
if reft_non_revoc_interval:
key = (
f"{rev_reg_id}_"
f"{reft_non_revoc_interval.get('from', 0)}_"
f"{reft_non_revoc_interval.get('to', epoch_now)}"
)
if key not in revoc_reg_deltas:
(delta, delta_timestamp) = await ledger.get_revoc_reg_delta(
rev_reg_id,
reft_non_revoc_interval.get("from", 0),
reft_non_revoc_interval.get("to", epoch_now),
)
revoc_reg_deltas[key] = (
rev_reg_id,
credential_id,
delta,
delta_timestamp,
)
for stamp_me in requested_referents.values():
# often one cred satisfies many requested attrs/preds
if stamp_me["cred_id"] == credential_id:
stamp_me["timestamp"] = revoc_reg_deltas[key][3]
# Get revocation states to prove non-revoked
revocation_states = {}
for (
rev_reg_id,
credential_id,
delta,
delta_timestamp,
) in revoc_reg_deltas.values():
if rev_reg_id not in revocation_states:
revocation_states[rev_reg_id] = {}
rev_reg = revocation_registries[rev_reg_id]
tails_local_path = await rev_reg.get_or_fetch_local_tails_path()
try:
revocation_states[rev_reg_id][delta_timestamp] = json.loads(
await holder.create_revocation_state(
credentials[credential_id]["cred_rev_id"],
rev_reg.reg_def,
delta,
delta_timestamp,
tails_local_path,
)
)
except IndyHolderError as e:
LOGGER.error(
f"Failed to create revocation state: {e.error_code}, {e.message}"
)
raise e
for (referent, precis) in requested_referents.items():
if "timestamp" not in precis:
continue
if referent in requested_credentials["requested_attributes"]:
requested_credentials["requested_attributes"][referent][
"timestamp"
] = precis["timestamp"]
if referent in requested_credentials["requested_predicates"]:
requested_credentials["requested_predicates"][referent][
"timestamp"
] = precis["timestamp"]
indy_proof_json = await holder.create_presentation(
proof_request,
requested_credentials,
schemas,
cred_defs,
revocation_states,
)
indy_proof = json.loads(indy_proof_json)
return indy_proof
async def process_pres_identifiers(
self,
identifiers: list,
) -> Tuple[dict, dict, dict, dict]:
"""Return schemas, cred_defs, rev_reg_defs, rev_reg_entries."""
schema_ids = []
cred_def_ids = []
schemas = {}
cred_defs = {}
rev_reg_defs = {}
rev_reg_entries = {}
for identifier in identifiers:
schema_ids.append(identifier["schema_id"])
cred_def_ids.append(identifier["cred_def_id"])
multitenant_mgr = self._profile.inject_or(BaseMultitenantManager)
if multitenant_mgr:
ledger_exec_inst = IndyLedgerRequestsExecutor(self._profile)
else:
ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor)
ledger = (
await ledger_exec_inst.get_ledger_for_identifier(
identifier["schema_id"],
txn_record_type=GET_SCHEMA,
)
)[1]
async with ledger:
# Build schemas for anoncreds
if identifier["schema_id"] not in schemas:
schemas[identifier["schema_id"]] = await ledger.get_schema(
identifier["schema_id"]
)
if identifier["cred_def_id"] not in cred_defs:
cred_defs[
identifier["cred_def_id"]
] = await ledger.get_credential_definition(
identifier["cred_def_id"]
)
if identifier.get("rev_reg_id"):
if identifier["rev_reg_id"] not in rev_reg_defs:
rev_reg_defs[
identifier["rev_reg_id"]
] = await ledger.get_revoc_reg_def(identifier["rev_reg_id"])
if identifier.get("timestamp"):
rev_reg_entries.setdefault(identifier["rev_reg_id"], {})
if (
identifier["timestamp"]
not in rev_reg_entries[identifier["rev_reg_id"]]
):
(
found_rev_reg_entry,
_found_timestamp,
) = await ledger.get_revoc_reg_entry(
identifier["rev_reg_id"], identifier["timestamp"]
)
rev_reg_entries[identifier["rev_reg_id"]][
identifier["timestamp"]
] = found_rev_reg_entry
return (
schemas,
cred_defs,
rev_reg_defs,
rev_reg_entries,
)