-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathecosystem_connections.py
416 lines (346 loc) · 12.4 KB
/
ecosystem_connections.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from dataclasses import dataclass
from typing import Any, Dict, TypedDict
import pytest
from aries_cloudcontroller import AcaPyClient
from app.models.tenants import CreateTenantResponse
from app.routes.connections import CreateInvitation
from app.routes.connections import router as conn_router
from app.routes.oob import router as oob_router
from app.services.trust_registry import actors
from app.services.trust_registry.actors import fetch_actor_by_id
from app.tests.util.ledger import create_public_did
from app.tests.util.webhooks import check_webhook_state
from app.util.acapy_verifier_utils import ed25519_verkey_to_did_key
from app.util.string import base64_to_json
from shared import RichAsyncClient
OOB_BASE_PATH = oob_router.prefix
CONNECTIONS_BASE_PATH = conn_router.prefix
@dataclass
class BobAliceConnect:
alice_connection_id: str
bob_connection_id: str
@pytest.fixture(scope="function")
async def bob_and_alice_connection(
bob_member_client: RichAsyncClient,
alice_member_client: RichAsyncClient,
) -> BobAliceConnect:
# create invitation on bob side
json_request = CreateInvitation(
alias="bob",
multi_use=False,
use_public_did=False,
).model_dump()
invitation = (
await bob_member_client.post(
f"{CONNECTIONS_BASE_PATH}/create-invitation", json=json_request
)
).json()
# accept invitation on alice side
invitation_response = (
await alice_member_client.post(
f"{CONNECTIONS_BASE_PATH}/accept-invitation",
json={"alias": "alice", "invitation": invitation["invitation"]},
)
).json()
bob_connection_id = invitation["connection_id"]
alice_connection_id = invitation_response["connection_id"]
# fetch and validate
# both connections should be active - we have waited long enough for events to be exchanged
assert await check_webhook_state(
alice_member_client,
topic="connections",
state="completed",
look_back=5,
)
assert await check_webhook_state(
bob_member_client,
topic="connections",
state="completed",
look_back=5,
)
return BobAliceConnect(
alice_connection_id=alice_connection_id, bob_connection_id=bob_connection_id
)
@dataclass
class AcmeAliceConnect:
alice_connection_id: str
acme_connection_id: str
@pytest.fixture(scope="function")
async def acme_and_alice_connection(
request,
alice_member_client: RichAsyncClient,
alice_tenant: CreateTenantResponse,
acme_client: RichAsyncClient,
acme_verifier: CreateTenantResponse,
) -> AcmeAliceConnect:
if hasattr(request, "param") and request.param == "trust_registry":
acme_actor = await fetch_actor_by_id(acme_verifier.wallet_id)
assert acme_actor["didcomm_invitation"]
invitation = acme_actor["didcomm_invitation"]
invitation_json = base64_to_json(invitation.split("?oob=")[1])
# accept invitation on alice side
invitation_response = (
await alice_member_client.post(
f"{OOB_BASE_PATH}/accept-invitation",
json={"invitation": invitation_json},
)
).json()
alice_label = alice_tenant.wallet_label
payload = await check_webhook_state(
client=acme_client,
topic="connections",
state="completed",
filter_map={
"their_label": alice_label,
},
)
alice_connection_id = invitation_response["connection_id"]
acme_connection_id = payload["connection_id"]
else:
# create invitation on acme side
invitation = (
await acme_client.post(f"{CONNECTIONS_BASE_PATH}/create-invitation")
).json()
# accept invitation on alice side
invitation_response = (
await alice_member_client.post(
f"{CONNECTIONS_BASE_PATH}/accept-invitation",
json={"invitation": invitation["invitation"]},
)
).json()
alice_connection_id = invitation_response["connection_id"]
acme_connection_id = invitation["connection_id"]
# fetch and validate - both connections should be active before continuing
assert await check_webhook_state(
alice_member_client,
topic="connections",
state="completed",
filter_map={
"connection_id": alice_connection_id,
},
look_back=5,
)
assert await check_webhook_state(
acme_client,
topic="connections",
state="completed",
filter_map={
"connection_id": acme_connection_id,
},
look_back=5,
)
return AcmeAliceConnect(
alice_connection_id=alice_connection_id,
acme_connection_id=acme_connection_id,
)
@dataclass
class FaberAliceConnect:
alice_connection_id: str
faber_connection_id: str
@pytest.fixture(scope="function")
async def faber_and_alice_connection(
alice_member_client: RichAsyncClient, faber_client: RichAsyncClient
) -> FaberAliceConnect:
# create invitation on faber side
invitation = (
await faber_client.post(f"{CONNECTIONS_BASE_PATH}/create-invitation")
).json()
# accept invitation on alice side
invitation_response = (
await alice_member_client.post(
f"{CONNECTIONS_BASE_PATH}/accept-invitation",
json={"invitation": invitation["invitation"]},
)
).json()
faber_connection_id = invitation["connection_id"]
alice_connection_id = invitation_response["connection_id"]
# fetch and validate
# both connections should be active - we have waited long enough for events to be exchanged
assert await check_webhook_state(
alice_member_client,
topic="connections",
state="completed",
filter_map={
"connection_id": alice_connection_id,
},
look_back=5,
)
assert await check_webhook_state(
faber_client,
topic="connections",
state="completed",
filter_map={
"connection_id": faber_connection_id,
},
look_back=5,
)
return FaberAliceConnect(
alice_connection_id=alice_connection_id, faber_connection_id=faber_connection_id
)
@dataclass
class MeldCoAliceConnect:
alice_connection_id: str
meld_co_connection_id: str
# Create fixture to handle parameters and return either meld_co-alice connection fixture
@pytest.fixture(scope="function")
async def meld_co_and_alice_connection(
request,
alice_tenant: CreateTenantResponse,
alice_member_client: RichAsyncClient,
meld_co_client: RichAsyncClient,
meld_co_issuer_verifier: CreateTenantResponse,
) -> MeldCoAliceConnect:
if hasattr(request, "param") and request.param == "trust_registry":
# get invitation as on trust registry
meld_co_label = meld_co_issuer_verifier.wallet_label
actor_record = await actors.fetch_actor_by_name(meld_co_label)
invitation = actor_record["didcomm_invitation"]
invitation_json = base64_to_json(invitation.split("?oob=")[1])
# accept invitation on alice side
invitation_response = (
await alice_member_client.post(
f"{OOB_BASE_PATH}/accept-invitation",
json={"invitation": invitation_json},
)
).json()
alice_label = alice_tenant.wallet_label
payload = await check_webhook_state(
client=meld_co_client,
topic="connections",
state="completed",
filter_map={
"their_label": alice_label,
},
)
meld_co_connection_id = payload["connection_id"]
alice_connection_id = invitation_response["connection_id"]
else:
# create invitation on meld_co side
invitation = (
await meld_co_client.post(f"{CONNECTIONS_BASE_PATH}/create-invitation")
).json()
# accept invitation on alice side
invitation_response = (
await alice_member_client.post(
f"{CONNECTIONS_BASE_PATH}/accept-invitation",
json={"invitation": invitation["invitation"]},
)
).json()
meld_co_connection_id = invitation["connection_id"]
alice_connection_id = invitation_response["connection_id"]
# fetch and validate - both connections should be active before continuing
assert await check_webhook_state(
alice_member_client,
topic="connections",
state="completed",
filter_map={
"connection_id": alice_connection_id,
},
look_back=5,
)
assert await check_webhook_state(
meld_co_client,
topic="connections",
state="completed",
filter_map={
"connection_id": meld_co_connection_id,
},
look_back=5,
)
return MeldCoAliceConnect(
alice_connection_id=alice_connection_id,
meld_co_connection_id=meld_co_connection_id,
)
@dataclass
class BobAlicePublicDid:
alice_public_did: str
bob_public_did: str
@pytest.fixture(scope="function")
async def bob_and_alice_public_did(
alice_acapy_client: AcaPyClient,
bob_acapy_client: AcaPyClient,
) -> BobAlicePublicDid:
bob_records = await bob_acapy_client.connection.get_connections()
alice_records = await alice_acapy_client.connection.get_connections()
await bob_acapy_client.connection.accept_invitation(
conn_id=bob_records.results[-1].connection_id
)
await alice_acapy_client.connection.accept_invitation(
conn_id=alice_records.results[-1].connection_id
)
bob_records = await bob_acapy_client.connection.get_connections()
alice_records = await alice_acapy_client.connection.get_connections()
bob_did = await create_public_did(bob_acapy_client)
alice_did = await create_public_did(alice_acapy_client)
if not bob_did.did or not alice_did.did:
raise Exception("Missing public did for alice or bob")
return BobAlicePublicDid(alice_public_did=alice_did, bob_public_did=bob_did)
class InvitationResultDict(TypedDict):
invitation: Dict[str, Any]
connection_id: str
class MultiInvite(TypedDict):
multi_use_invitation: InvitationResultDict
invitation_key: str
did_from_rec_key: str
async def bob_multi_use_invitation(
bob_member_client: RichAsyncClient,
) -> MultiInvite:
create_invite_json = CreateInvitation(
alias=None,
multi_use=True,
use_public_did=False,
).model_dump()
# Create a multi-use invitation
invitation = (
await bob_member_client.post(
f"{CONNECTIONS_BASE_PATH}/create-invitation",
json=create_invite_json,
)
).json()
recipient_key = invitation["invitation"]["recipientKeys"][0]
bob_multi_invite = MultiInvite(
multi_use_invitation=invitation,
invitation_key=recipient_key,
did_from_rec_key=ed25519_verkey_to_did_key(key=recipient_key),
)
return bob_multi_invite
@pytest.fixture(scope="function")
async def alice_bob_connect_multi(
bob_member_client: RichAsyncClient,
alice_member_client: RichAsyncClient,
) -> BobAliceConnect:
multi_use_invite = bob_multi_use_invitation(bob_member_client)
invitation = multi_use_invite["multi_use_invitation"]["invitation"]
# accept invitation on alice side
invitation_response = (
await alice_member_client.post(
f"{CONNECTIONS_BASE_PATH}/accept-invitation",
json={"invitation": invitation},
)
).json()
assert await check_webhook_state(
client=alice_member_client,
topic="connections",
state="request-sent",
)
bob_connection_id = multi_use_invite["multi_use_invitation"]["connection_id"]
alice_connection_id = invitation_response["connection_id"]
# fetch and validate
# both connections should be active - we have waited long enough for events to be exchanged
bob_connection_records = (await bob_member_client.get(CONNECTIONS_BASE_PATH)).json()
bob_connection_id = bob_connection_records[0]["connection_id"]
assert await check_webhook_state(
client=alice_member_client,
topic="connections",
state="completed",
look_back=5,
)
assert await check_webhook_state(
client=bob_member_client,
topic="connections",
state="completed",
look_back=5,
)
return BobAliceConnect(
alice_connection_id=alice_connection_id, bob_connection_id=bob_connection_id
)