Skip to content

Commit 8915627

Browse files
committed
full aries-fl flow implemented
1 parent cfe25c3 commit 8915627

13 files changed

+1386
-721
lines changed

projects/aries-fl/hospital/hospital.py

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import matplotlib.pyplot as plt
1717
import seaborn as sns
1818
import torch
19-
19+
import time
2020
# prep
2121
from sklearn.model_selection import train_test_split
2222
from sklearn import preprocessing
@@ -131,35 +131,95 @@ def _connection_handler(self, payload):
131131
# No Auth Policy set
132132
print("No Auth Policy set")
133133
connection["is_trusted"].set_result(True)
134-
break
134+
break
135135

136136

137137
def _cred_handler(self, payload):
138+
loop = asyncio.get_event_loop()
139+
138140
print("Handle Credentials")
139141
exchange_id = payload['credential_exchange_id']
140142
state = payload['state']
141143
role = payload['role']
142144
attributes = payload['credential_proposal_dict']['credential_proposal']['attributes']
143145
print(f"Credential exchange {exchange_id}, role: {role}, state: {state}")
144-
print(f"Offering: {attributes}")
146+
147+
if state == "offer_received":
148+
print(f"Offering: {attributes}")
149+
print("Requesting credential")
150+
loop.run_until_complete(self.agent_controller.issuer.send_request_for_record(exchange_id))
151+
elif state == "credential_received":
152+
print("Storing Credential")
153+
credential_id = "NHS Hosptial " + exchange_id
154+
loop.run_until_complete(self.agent_controller.issuer.store_credential(exchange_id, credential_id))
155+
145156

146157
def _proof_handler(self, payload):
147158
print("Handle present proof")
148159
role = payload["role"]
149160
connection_id = payload["connection_id"]
150161
pres_ex_id = payload["presentation_exchange_id"]
151162
state = payload["state"]
163+
loop = asyncio.get_event_loop()
164+
152165
print(f"Role {role}, Exchange {pres_ex_id} in state {state}")
166+
for connection in self.pending_connections:
167+
if connection["connection_id"] == connection_id:
168+
if state == "request_received":
169+
print("Received Authentication Challenge")
170+
171+
credentials_by_reft = {}
172+
revealed = {}
173+
self_attested = {}
174+
predicates = {}
175+
176+
# select credentials to provide for the proof
177+
credentials = loop.run_until_complete(self.agent_controller.proofs.get_presentation_credentials(pres_ex_id))
178+
print("Credentials", credentials)
179+
180+
reveal_cred = credentials[0]
181+
print("Revealed Credential", reveal_cred)
182+
# {'cred_info': {'referent': 'NHS Hosptial 1e5044b7-e6c7-4ec6-88ea-49df91e19dd5',
183+
# 'attrs': {'name': 'Royal Infirmary of Edinburgh'}, 'schema_id': 'BvRKFu1fdzGdJzQcKb8e8p:2:NHS Hosptial:0.0.1', 'cred_def_id': 'RiUq77qGfmFmAHxj1NQcSY:3:CL:156569:default', 'rev_reg_id': None, 'cred_rev_id': None}, 'interval': None, 'presentation_referents': ['0_name_uuid']}
184+
if credentials:
185+
for row in credentials:
186+
187+
for referent in row["presentation_referents"]:
188+
if referent not in credentials_by_reft:
189+
credentials_by_reft[referent] = row
190+
191+
for referent in payload["presentation_request"]["requested_attributes"]:
192+
if referent in credentials_by_reft:
193+
revealed[referent] = {
194+
"cred_id": credentials_by_reft[referent]["cred_info"][
195+
"referent"
196+
],
197+
"revealed": True,
198+
}
199+
200+
201+
print("\nGenerate the proof")
202+
proof = {
203+
"requested_predicates": predicates,
204+
"requested_attributes": revealed,
205+
"self_attested_attributes": self_attested,
206+
}
207+
print(proof)
208+
print("\nXXX")
209+
print(predicates)
210+
print(revealed)
211+
print(self_attested)
212+
213+
loop.run_until_complete(self.agent_controller.proofs.send_presentation(pres_ex_id, proof))
214+
215+
elif state == "presentation_received":
153216

154-
if state == "presentation_received":
155-
for connection in self.pending_dataowner_connections:
156-
if connection["connection_id"] == connection_id:
157-
loop = asyncio.get_event_loop()
158217

159218
print("Verifying DataOwner Presentation")
160219
verify = loop.run_until_complete(self.agent_controller.proofs.verify_presentation(pres_ex_id))
161220
connection["is_trusted"].set_result(verify['state'] == "verified")
162-
break
221+
break
222+
163223

164224
def _process_data(self, file_path):
165225
# Read in Data

projects/aries-fl/manage

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ EOF
6262
# -----------------------------------------------------------------------------------------------------------------
6363
# Default Settings:
6464
# -----------------------------------------------------------------------------------------------------------------
65-
# DEFAULT_CONTAINERS="nhstrust-agent nhstrust-notebook regulator-agent regulator-notebook researcher-agent researcher-notebook hospital1-agent hospital1-notebook hospital2-agent hospital2-notebook hospital3-agent hospital3-notebook"
66-
DEFAULT_CONTAINERS="researcher-agent researcher-notebook hospital1-agent hospital1-notebook hospital2-agent hospital2-notebook hospital3-agent hospital3-notebook"
65+
DEFAULT_CONTAINERS="nhstrust-agent nhstrust-notebook regulator-agent regulator-notebook researcher-agent researcher-notebook hospital1-agent hospital1-notebook hospital2-agent hospital2-notebook hospital3-agent hospital3-notebook"
66+
# DEFAULT_CONTAINERS="researcher-agent researcher-notebook hospital1-agent hospital1-notebook hospital2-agent hospital2-notebook hospital3-agent hospital3-notebook"
6767
# -----------------------------------------------------------------------------------------------------------------
6868
# Functions:
6969
# -----------------------------------------------------------------------------------------------------------------

projects/aries-fl/notebooks/hospital1/Hospital1.ipynb

Lines changed: 158 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "genetic-shanghai",
5+
"id": "prostate-merit",
66
"metadata": {},
77
"source": [
88
"# Hospital 1\n",
@@ -13,7 +13,7 @@
1313
{
1414
"cell_type": "code",
1515
"execution_count": 1,
16-
"id": "democratic-worthy",
16+
"id": "sonic-protocol",
1717
"metadata": {},
1818
"outputs": [],
1919
"source": [
@@ -26,7 +26,7 @@
2626
{
2727
"cell_type": "code",
2828
"execution_count": 2,
29-
"id": "heard-coach",
29+
"id": "streaming-digest",
3030
"metadata": {},
3131
"outputs": [],
3232
"source": [
@@ -36,7 +36,7 @@
3636
{
3737
"cell_type": "code",
3838
"execution_count": 3,
39-
"id": "alien-checklist",
39+
"id": "historical-stationery",
4040
"metadata": {},
4141
"outputs": [],
4242
"source": [
@@ -56,7 +56,7 @@
5656
{
5757
"cell_type": "code",
5858
"execution_count": 4,
59-
"id": "psychological-wrapping",
59+
"id": "french-duncan",
6060
"metadata": {},
6161
"outputs": [
6262
{
@@ -73,51 +73,161 @@
7373
},
7474
{
7575
"cell_type": "markdown",
76-
"id": "oriented-heart",
76+
"id": "micro-argument",
7777
"metadata": {},
7878
"source": [
79-
"## Copy from Researcher"
79+
"## Get Hospital Credential\n",
80+
"\n",
81+
"Copy connection invite from the NHS trust to receive the hospital credential. [NHS Trust Notebooks are here](http://localhost:8888/tree)"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": null,
87+
"id": "persistent-listing",
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"nhs_invite = {'@type': 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0/invitation', '@id': '21aaaf46-7526-448e-a692-1becf370e820', 'label': 'NHS Lothian', 'serviceEndpoint': 'http://172.17.0.1:8020', 'recipientKeys': ['E6rysFzG3TNo3Hz2ZdLqswA92TPgbdm7DUSuYyMzEuPh']}"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": null,
97+
"id": "round-folder",
98+
"metadata": {},
99+
"outputs": [],
100+
"source": [
101+
"response =await hospital1.agent_controller.connections.accept_connection(nhs_invite)\n",
102+
"print(response)"
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"id": "fourth-audit",
108+
"metadata": {},
109+
"source": [
110+
"## Optional: Configure Research Authentication Policy\n",
111+
"\n",
112+
"This is a proof request that the hospital will automatically challenge the researcher for"
80113
]
81114
},
82115
{
83116
"cell_type": "code",
84117
"execution_count": 5,
85-
"id": "another-cosmetic",
118+
"id": "bulgarian-reasoning",
86119
"metadata": {},
87120
"outputs": [],
88121
"source": [
89-
"research_invite = {\"@type\": \"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0/invitation\", \"@id\": \"200407a5-068c-4064-afe2-ef43d31dcd25\", \"serviceEndpoint\": \"http://172.17.0.1:8040\", \"recipientKeys\": [\"7vtM4totFusGjo2H7AYE3dZkxcw2tMWCtHSnwFXqGKmQ\"], \"label\": \"Health Researcher\"}"
122+
"schema_id = \"FWyJ74b38bCF1QbYF7tWMM:2:Authorised Research:0.0.1\"\n",
123+
"# cred_def = \"WfntKNFwXMQfgmU9ofbxPM:3:CL:156569:default\"\n",
124+
"\n",
125+
"revocation = False\n",
126+
"exchange_tracing = False\n",
127+
"\n",
128+
"#Enable this to ask for attributes to identity a user\n",
129+
"#TODO - change restriction to schemaId or credentialId\n",
130+
"\n",
131+
"req_attrs = [\n",
132+
" {\"name\": \"scope\", \"restrictions\": [{\"schema_id\": schema_id}]},#, \"cred_def_id\": cred_def}]},\n",
133+
"]\n",
134+
"\n",
135+
"global indy_proof_request\n",
136+
"indy_proof_request = {\n",
137+
" \"name\": \"Proof of Hospital\",\n",
138+
" \"version\": \"1.0\",\n",
139+
" \"requested_attributes\": {\n",
140+
" f\"0_{req_attr['name']}_uuid\":\n",
141+
" req_attr for req_attr in req_attrs\n",
142+
" },\n",
143+
" \"requested_predicates\": {\n",
144+
"# f\"0_{req_pred['name']}_GE_uuid\":\n",
145+
"# req_pred for req_pred in req_preds\n",
146+
" },\n",
147+
"}\n",
148+
"\n"
90149
]
91150
},
92151
{
93152
"cell_type": "code",
94153
"execution_count": 6,
95-
"id": "consolidated-broadcasting",
154+
"id": "statistical-electricity",
155+
"metadata": {},
156+
"outputs": [],
157+
"source": [
158+
"hospital1.set_authentication_policy(indy_proof_request)"
159+
]
160+
},
161+
{
162+
"cell_type": "markdown",
163+
"id": "viral-diana",
164+
"metadata": {},
165+
"source": [
166+
"## Copy from Researcher"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": 7,
172+
"id": "prerequisite-tablet",
173+
"metadata": {},
174+
"outputs": [],
175+
"source": [
176+
"research_invite ={\"@type\": \"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0/invitation\", \"@id\": \"df76459d-7a65-462e-ae5d-bc1f96a7674f\", \"serviceEndpoint\": \"http://172.17.0.1:8040\", \"label\": \"Health Researcher\", \"recipientKeys\": [\"9QkEnGGQHUNp95rRdsf2GxyGzGniuApqGmoQuXT5UvCC\"]}"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": 8,
182+
"id": "accurate-education",
96183
"metadata": {},
97184
"outputs": [
98185
{
99186
"name": "stdout",
100187
"output_type": "stream",
101188
"text": [
102189
"Connection Handler Called\n",
103-
"Connection 5b995cd7-cdbf-44a5-951d-3ec538feeec9 in State invitation\n",
190+
"Connection c49d22a1-d306-4278-b955-63c098ffc529 in State invitation\n",
104191
"Connection Handler Called\n",
105-
"Connection 5b995cd7-cdbf-44a5-951d-3ec538feeec9 in State request\n",
192+
"Connection c49d22a1-d306-4278-b955-63c098ffc529 in State request\n",
106193
"Establishing connection\n",
107194
"Connection Handler Called\n",
108-
"Connection 5b995cd7-cdbf-44a5-951d-3ec538feeec9 in State response\n",
195+
"Connection c49d22a1-d306-4278-b955-63c098ffc529 in State response\n",
109196
"Connection Handler Called\n",
110-
"Connection 5b995cd7-cdbf-44a5-951d-3ec538feeec9 in State active\n",
197+
"Connection c49d22a1-d306-4278-b955-63c098ffc529 in State active\n",
111198
"Pending connection moved to active. Challenging with auth policy if set\n",
112-
"No Auth Policy set\n",
113-
"Trusted Research Connection Established - 5b995cd7-cdbf-44a5-951d-3ec538feeec9\n",
114-
"ML Message from 5b995cd7-cdbf-44a5-951d-3ec538feeec9\n",
199+
"Handle present proof\n",
200+
"Role verifier, Exchange babbffa0-f5fc-49e3-ac20-c69eee1480ac in state request_sent\n",
201+
"Handle present proof\n",
202+
"Role prover, Exchange 62bb0164-6076-40ad-b269-87ec43ccdf85 in state request_received\n",
203+
"Received Authentication Challenge\n",
204+
"Credentials [{'cred_info': {'referent': 'NHS Hosptial 1e5044b7-e6c7-4ec6-88ea-49df91e19dd5', 'attrs': {'name': 'Royal Infirmary of Edinburgh'}, 'schema_id': 'BvRKFu1fdzGdJzQcKb8e8p:2:NHS Hosptial:0.0.1', 'cred_def_id': 'RiUq77qGfmFmAHxj1NQcSY:3:CL:156569:default', 'rev_reg_id': None, 'cred_rev_id': None}, 'interval': None, 'presentation_referents': ['0_name_uuid']}, {'cred_info': {'referent': 'NHS Hosptial 434e2d8f-89ad-4a4a-a77e-e6a3b33fac0f', 'attrs': {'name': 'Royal Infirmary of Edinburgh'}, 'schema_id': 'BvRKFu1fdzGdJzQcKb8e8p:2:NHS Hosptial:0.0.1', 'cred_def_id': 'RiUq77qGfmFmAHxj1NQcSY:3:CL:156569:default', 'rev_reg_id': None, 'cred_rev_id': None}, 'interval': None, 'presentation_referents': ['0_name_uuid']}, {'cred_info': {'referent': 'NHS Hosptial 9a85ecc2-936a-451e-bad1-995b7b23b21b', 'attrs': {'name': 'Royal Infirmary of Edinburgh'}, 'schema_id': 'BvRKFu1fdzGdJzQcKb8e8p:2:NHS Hosptial:0.0.1', 'cred_def_id': 'RiUq77qGfmFmAHxj1NQcSY:3:CL:156569:default', 'rev_reg_id': None, 'cred_rev_id': None}, 'interval': None, 'presentation_referents': ['0_name_uuid']}]\n",
205+
"Revealed Credential {'cred_info': {'referent': 'NHS Hosptial 1e5044b7-e6c7-4ec6-88ea-49df91e19dd5', 'attrs': {'name': 'Royal Infirmary of Edinburgh'}, 'schema_id': 'BvRKFu1fdzGdJzQcKb8e8p:2:NHS Hosptial:0.0.1', 'cred_def_id': 'RiUq77qGfmFmAHxj1NQcSY:3:CL:156569:default', 'rev_reg_id': None, 'cred_rev_id': None}, 'interval': None, 'presentation_referents': ['0_name_uuid']}\n",
206+
"\n",
207+
"Generate the proof\n",
208+
"{'requested_predicates': {}, 'requested_attributes': {'0_name_uuid': {'cred_id': 'NHS Hosptial 1e5044b7-e6c7-4ec6-88ea-49df91e19dd5', 'revealed': True}}, 'self_attested_attributes': {}}\n",
209+
"\n",
210+
"XXX\n",
211+
"{}\n",
212+
"{'0_name_uuid': {'cred_id': 'NHS Hosptial 1e5044b7-e6c7-4ec6-88ea-49df91e19dd5', 'revealed': True}}\n",
213+
"{}\n",
214+
"Handle present proof\n",
215+
"Role verifier, Exchange babbffa0-f5fc-49e3-ac20-c69eee1480ac in state presentation_received\n",
216+
"Verifying DataOwner Presentation\n",
217+
"Handle present proof\n",
218+
"Role prover, Exchange 62bb0164-6076-40ad-b269-87ec43ccdf85 in state presentation_sent\n",
219+
"Handle present proof\n",
220+
"Role prover, Exchange 62bb0164-6076-40ad-b269-87ec43ccdf85 in state presentation_acked\n",
221+
"Handle present proof\n",
222+
"Role verifier, Exchange babbffa0-f5fc-49e3-ac20-c69eee1480ac in state verified\n",
223+
"Trusted Research Connection Established - c49d22a1-d306-4278-b955-63c098ffc529\n",
224+
"ML Message from c49d22a1-d306-4278-b955-63c098ffc529\n",
115225
"Open file\n",
116226
"learning\n",
117227
"untrained_model.pt\n",
118228
"HOSPITAL MODEL LOADED\n",
119229
"HOSPITAL IS TRAINING\n",
120-
"loss at epoch 0 : tensor(0.2495)\n"
230+
"loss at epoch 0 : tensor(0.2652)\n"
121231
]
122232
},
123233
{
@@ -132,18 +242,37 @@
132242
"name": "stdout",
133243
"output_type": "stream",
134244
"text": [
135-
"loss at epoch 5000 : tensor(0.1277)\n",
136-
"loss at epoch 10000 : tensor(0.1135)\n",
137-
"loss at epoch 15000 : tensor(0.1081)\n",
138-
"loss at epoch 20000 : tensor(0.1011)\n",
139-
"loss at epoch 25000 : tensor(0.0980)\n",
140-
"loss at epoch 30000 : tensor(0.0955)\n",
141-
"loss at epoch 35000 : tensor(0.0931)\n",
142-
"loss at epoch 40000 : tensor(0.0911)\n",
143-
"loss at epoch 45000 : tensor(0.0894)\n",
245+
"loss at epoch 5000 : tensor(0.1328)\n",
246+
"loss at epoch 10000 : tensor(0.1152)\n",
247+
"loss at epoch 15000 : tensor(0.1115)\n",
248+
"loss at epoch 20000 : tensor(0.1068)\n",
249+
"loss at epoch 25000 : tensor(0.1006)\n",
250+
"loss at epoch 30000 : tensor(0.0973)\n",
251+
"loss at epoch 35000 : tensor(0.0955)\n",
252+
"loss at epoch 40000 : tensor(0.0943)\n",
253+
"loss at epoch 45000 : tensor(0.0934)\n",
254+
"Learnt \n",
255+
"Trained file open\n",
256+
"connection ID c49d22a1-d306-4278-b955-63c098ffc529\n",
257+
"ML Message from c49d22a1-d306-4278-b955-63c098ffc529\n",
258+
"Open file\n",
259+
"learning\n",
260+
"untrained_model.pt\n",
261+
"HOSPITAL MODEL LOADED\n",
262+
"HOSPITAL IS TRAINING\n",
263+
"loss at epoch 0 : tensor(0.2652)\n",
264+
"loss at epoch 5000 : tensor(0.1328)\n",
265+
"loss at epoch 10000 : tensor(0.1152)\n",
266+
"loss at epoch 15000 : tensor(0.1115)\n",
267+
"loss at epoch 20000 : tensor(0.1068)\n",
268+
"loss at epoch 25000 : tensor(0.1006)\n",
269+
"loss at epoch 30000 : tensor(0.0973)\n",
270+
"loss at epoch 35000 : tensor(0.0955)\n",
271+
"loss at epoch 40000 : tensor(0.0943)\n",
272+
"loss at epoch 45000 : tensor(0.0934)\n",
144273
"Learnt \n",
145274
"Trained file open\n",
146-
"connection ID 5b995cd7-cdbf-44a5-951d-3ec538feeec9\n"
275+
"connection ID c49d22a1-d306-4278-b955-63c098ffc529\n"
147276
]
148277
}
149278
],
@@ -154,7 +283,7 @@
154283
{
155284
"cell_type": "code",
156285
"execution_count": null,
157-
"id": "printable-rally",
286+
"id": "magnetic-firmware",
158287
"metadata": {},
159288
"outputs": [],
160289
"source": []

0 commit comments

Comments
 (0)