Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
572 lines (437 loc) · 20.6 KB
/
Copy pathapp.py
File metadata and controls
572 lines (437 loc) · 20.6 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
__copyright__ = 'COPYRIGHT 2013-2019, ALL RIGHTS RESERVED, EVERNYM INC.'
import logging
import os
import pyqrcode
import requests
import traceback
import asyncio
from asyncio.base_events import Server
from aiohttp import web
from aiohttp.web_routedef import RouteTableDef
from urllib.parse import quote_plus
from verity_sdk.handlers import Handlers
from verity_sdk.protocols.v0_6.IssuerSetup import IssuerSetup
from verity_sdk.protocols.v0_6.UpdateConfigs import UpdateConfigs
from verity_sdk.protocols.v0_6.UpdateEndpoint import UpdateEndpoint
from verity_sdk.protocols.v0_6.WriteCredentialDefinition import WriteCredentialDefinition
from verity_sdk.protocols.v0_6.WriteSchema import WriteSchema
from verity_sdk.protocols.v0_7.Provision import Provision
from verity_sdk.protocols.v1_0.Connecting import Connecting
from verity_sdk.protocols.v1_0.IssueCredential import IssueCredential
from verity_sdk.protocols.v1_0.PresentProof import PresentProof
from verity_sdk.protocols.v1_0.Relationship import Relationship
from verity_sdk.protocols.v1_0.CommittedAnswer import CommittedAnswer
from verity_sdk.utils.Context import Context
from vdrtools.wallet import delete_wallet
from vdrtools.error import WalletNotFoundError
from helper import *
INSTITUTION_NAME: str = 'Faber College'
LOGO_URL: str = 'https://freeiconshop.com/wp-content/uploads/edd/bank-flat.png'
CONFIG_PATH: str = 'verity-context.json'
WALLET_NAME: str = 'examplewallet1'
WALLET_KEY: str = 'examplewallet1'
context: Context
issuer_did: str = ''
issuer_verkey: str = ''
server: Server
port: int = 4000
handlers: Handlers = Handlers()
handlers.set_default_handler(default_handler)
handlers.add_handler('trust_ping', '1.0', noop)
routes: RouteTableDef = web.RouteTableDef()
async def example(loop):
logging.info('Starting setup')
await setup(loop)
schema_id = await write_ledger_schema(loop)
cred_def_id = await write_ledger_cred_def(loop, schema_id)
rel_did = await create_relationship(loop)
await create_connection(loop)
await ask_question(loop, rel_did)
await issue_credential(loop, rel_did, cred_def_id)
await request_proof(loop, rel_did)
async def create_relationship(loop) -> str:
global context
global handlers
# Relationship protocol has two steps
# 1. create relationship key
# 2. create invitation
# Constructor for the Relationship API
relationship: Relationship = Relationship()
rel_did = loop.create_future()
thread_id = loop.create_future()
spinner = make_spinner('Waiting to create relationship') # Console spinner
# handler for the response to the request to start the Connecting protocol.
async def created_handler(msg_name, message):
spinner.stop_and_persist('Done')
print_message(msg_name, message)
if msg_name == Relationship.CREATED:
thread_id.set_result(message['~thread']['thid'])
rel_did.set_result(message['did'])
else:
non_handled(f'Message name is not handled - {msg_name}', message)
# adds handler to the set of handlers
handlers.add_handler(Relationship.MSG_FAMILY, Relationship.MSG_FAMILY_VERSION, created_handler)
spinner.start()
# starts the relationship protocol
await relationship.create(context)
thread_id = await thread_id
rel_did = await rel_did
# Step 2
invitation = loop.create_future()
spinner = make_spinner('Waiting to create invitation') # Console spinner
# handler for the accept message sent when invitation is created
async def invitation_handler(msg_name, message):
spinner.stop_and_persist('Done')
print_message(msg_name, message)
if msg_name == Relationship.INVITATION:
invite_url = message['inviteURL']
# write QRCode to disk
qr = pyqrcode.create(invite_url)
qr.png('qrcode.png')
if os.environ.get('HTTP_SERVER_URL'):
print('Open the following URL in your browser and scan presented QR code')
print(f'{ANSII_GREEN}https://chart.googleapis.com/chart?cht=qr&chs=512x512&chl={quote_plus(invite_url)}{ANSII_RESET}')
else:
print('QR code generated at: qrcode.png')
print('Open this file and scan QR code to establish a connection')
invitation.set_result(None)
else:
non_handled(f'Message name is not handled - {msg_name}', message)
spinner.start()
# note this overrides the handler for this message family! This is for demonstration purposes only.
handlers.add_handler(Relationship.MSG_FAMILY, Relationship.MSG_FAMILY_VERSION, invitation_handler)
relationship: Relationship = Relationship(rel_did, thread_id)
await relationship.out_of_band_invitation(context)
await invitation
return rel_did # return owning DID for the connection
async def create_connection(loop):
global context
global handlers
# Connecting protocol is started from the Holder's side (ConnectMe)
# by scanning the QR code containing connection invitation
# Connection is established when the Holder accepts the connection on the device
# i.e. when the RESPONSE_SENT control message is received
connection = loop.create_future()
spinner = make_spinner('Waiting to respond to connection') # Console spinner
# handler for messages in Connecting protocol
async def connection_handler(msg_name, message):
if msg_name == Connecting.REQUEST_RECEIVED:
print()
print_message(msg_name, message)
elif msg_name == Connecting.RESPONSE_SENT:
spinner.stop_and_persist('Done')
print_message(msg_name, message)
connection.set_result(None)
else:
non_handled(f'Message name is not handled - {msg_name}', message)
# handler for relationship-reused message which is sent by the ConnectMe app if it is already connected with this example app agent
async def releationship_reused_handler(msg_name, message):
spinner.stop_and_persist('Done')
print_message(msg_name, message)
if msg_name == 'relationship-reused':
print('The mobile wallet app signalled that it already has the connection with this example app agent')
print('This example app does not support relationship-reuse since it does not store the data about previous relationships')
print('Please delete existing connection in your mobile wallet and re-run this application')
print('To learn how relationship-reuse can be used check out "ssi-auth" or "out-of-band" sample apps')
sys.exit(1)
else:
non_handled(f'Message name is not handled - {msg_name}', message)
# adds handler to the set of handlers
handlers.add_handler(Connecting.MSG_FAMILY, Connecting.MSG_FAMILY_VERSION, connection_handler)
handlers.add_handler('out-of-band', '1.0', releationship_reused_handler)
spinner.start()
# waits for request
await connection # wait for response from verity application
async def write_ledger_schema(loop) -> str:
# input parameters for schema
schema_name = 'Diploma'
schema_version = get_random_version()
schema_attrs = ['name', 'degree']
# constructor for the Write Schema protocol
schema = WriteSchema(schema_name, schema_version, schema_attrs)
first_step = loop.create_future()
spinner = make_spinner('Waiting to write schema to ledger') # Console spinner
# handler for message received when schema is written
async def schema_written_handler(msg_name, message):
spinner.stop_and_persist('Done')
print_message(msg_name, message)
if msg_name == 'needs-endorsement':
print('Please manually endorse this schema json. If you do not have an endorser DID on the ledger send it to Evernym to be endorsed: ')
print(message['schemaJson'])
console_input('Press enter when the schema has been endorsed', None)
first_step.set_result(message['schemaId'])
elif msg_name == 'status-report':
first_step.set_result(message['schemaId'])
else:
non_handled(f'Message name is not handled - {msg_name}', message)
# adds handler to the set of handlers
handlers.add_handler(WriteSchema.MSG_FAMILY, WriteSchema.MSG_FAMILY_VERSION, schema_written_handler)
spinner.start()
# request schema be written to ledger
await schema.write(context)
schema_id = await first_step # wait for operation to be complete
return schema_id # returns ledger schema identifier
async def write_ledger_cred_def(loop, schema_id: str) -> str:
# input parameters for cred definition
cred_def_name = 'Trinity College Diplomas'
cred_def_tag = 'latest'
# constructor for the Write Credential Definition protocol
cred_def = WriteCredentialDefinition(cred_def_name, schema_id, cred_def_tag)
first_step = loop.create_future()
spinner = make_spinner('Waiting to write cred def to ledger') # Console spinner
# handler for message received when schema is written
async def cred_def_written_handler(msg_name, message):
spinner.stop_and_persist('Done')
print_message(msg_name, message)
if msg_name == 'needs-endorsement':
print('Please manually endorse this cred def json. If you do not have an endorser DID on the ledger send it to Evernym to be endorsed: ')
print(message['credDefJson'])
first_step.set_result(message['credDefId'])
console_input('Press enter when the cred def has been endorsed', None)
elif msg_name == 'status-report':
first_step.set_result(message['credDefId'])
else:
non_handled(f'Message name is not handled - {msg_name}', message)
# adds handler to the set of handlers
handlers.add_handler(
WriteCredentialDefinition.MSG_FAMILY,
WriteCredentialDefinition.MSG_FAMILY_VERSION,
cred_def_written_handler
)
spinner.start()
# request the cred def be writen to ledger
await cred_def.write(context)
cred_def_id = await first_step # wait for operation to be complete
return cred_def_id # returns ledger cred def identifier
async def ask_question(loop, for_did):
question_text = 'Hi Alice, how are you today?'
question_detail = 'Checking up on you today.'
valid_responses = ['Great!', 'Not so good.']
question = CommittedAnswer(for_did, None, question_text, question_detail, valid_responses, True)
first_step = loop.create_future()
spinner = make_spinner('Waiting for Connect.Me to answer the question') # Console spinner
async def receive_answer(msg_name, message):
spinner.stop_and_persist('Done')
print_message(msg_name, message)
if msg_name == CommittedAnswer.ANSWER_GIVEN:
first_step.set_result(None)
else:
non_handled(f'Message name is not handled - {msg_name}', message)
handlers.add_handler(CommittedAnswer.MSG_FAMILY, CommittedAnswer.MSG_FAMILY_VERSION, receive_answer)
spinner.start()
await question.ask(context)
await first_step
async def issue_credential(loop, rel_did, cred_def_id):
# input parameters for issue credential
credential_name = 'Degree'
credential_data = {
'name': 'Alice Smith',
'degree': 'Bachelors'
}
# constructor for the Issue Credential protocol
issue = IssueCredential(rel_did, None, cred_def_id, credential_data, credential_name, 0, True)
offer_sent = loop.create_future()
cred_sent = loop.create_future()
spinner = make_spinner('Wait for Connect.me to accept the Credential Offer') # Console spinner
# handler for 'sent` message when the offer for credential is sent
async def send_offer_handler(msg_name, message):
spinner.stop_and_persist('Done')
print_message(msg_name, message)
if msg_name == IssueCredential.SENT:
offer_sent.set_result(None)
else:
non_handled(f'Message name is not handled - {msg_name}', message)
# adds handler to the set of handlers
handlers.add_handler(IssueCredential.MSG_FAMILY, IssueCredential.MSG_FAMILY_VERSION, send_offer_handler)
spinner.start()
# request that credential is offered
await issue.offer_credential(context)
await offer_sent # wait for sending of offer to connect.me user
# handler for 'sent` message when the credential is sent
async def send_cred_handler(msg_name, message):
spinner.stop_and_persist('Done')
print_message(msg_name, message)
if msg_name == IssueCredential.SENT:
cred_sent.set_result(None)
else:
non_handled(f'Message name is not handled - {msg_name}', message)
# adds handler to the set of handlers
handlers.add_handler(IssueCredential.MSG_FAMILY, IssueCredential.MSG_FAMILY_VERSION, send_cred_handler)
spinner = make_spinner('waiting to send credential') # Console spinner
spinner.start()
handlers.add_handler(IssueCredential.MSG_FAMILY, IssueCredential.MSG_FAMILY_VERSION, send_cred_handler)
await cred_sent
await asyncio.sleep(3) # Wait a few seconds for the credential to arrive before sending the proof
async def request_proof(loop, for_did):
global issuer_did
# input parameters for request proof
proof_name = 'Proof of Degree'
proof_attrs = [
{
'name': 'name',
'restrictions': [{'issuer_did': issuer_did}]
},
{
'name': 'degree',
'restrictions': [{'issuer_did': issuer_did}]
}
]
# constructor for the Present Proof protocol
proof = PresentProof(for_did, None, proof_name, proof_attrs)
spinner = make_spinner('Waiting for proof presentation from Connect.me') # Console spinner
first_step = loop.create_future()
# handler for the result of the proof presentation
async def proof_handler(msg_name, message):
spinner.stop_and_persist('Done')
print_message(msg_name, message)
if msg_name == PresentProof.PRESENTATION_RESULT:
first_step.set_result(None) # proof data contained inside `message`
else:
non_handled(f'Message name is not handled - {msg_name}', message)
# adds handler to the set of handlers
handlers.add_handler(PresentProof.MSG_FAMILY, PresentProof.MSG_FAMILY_VERSION, proof_handler)
spinner.start()
# request proof
await proof.request(context)
await first_step # wait for connect.me user to present the requested proof
async def setup(loop):
global context
global issuer_did
config = ''
# look for context on disk
try:
with open(CONFIG_PATH, 'r') as f:
if console_yes_no(f'Reuse Verity Context (in {CONFIG_PATH})', True):
config = f.read()
else:
await delete_wallet(json.dumps({'id': WALLET_NAME}), json.dumps({'key': WALLET_KEY}))
except (FileNotFoundError, WalletNotFoundError):
pass
if config:
context = await Context.create_with_config(config)
else:
context = await provision_agent()
with open('verity-context.json', 'w') as f:
f.write(context.to_json())
await update_webhook_endpoint()
print_object(context.to_json(indent=2), '>>>', 'Context Used:')
with open('verity-context.json', 'w') as f:
f.write(context.to_json())
await update_configs()
await issuer_identifier(loop)
if not issuer_did:
print('\nIssuer DID is not created. Performing Issuer setup now...')
await setup_issuer(loop)
else:
print(f'Issuer DID: {ANSII_GREEN}{issuer_did}{ANSII_RESET}')
print(f'Issuer Verkey: {ANSII_GREEN}{issuer_verkey}{ANSII_RESET}')
async def provision_agent() -> str:
global context
token = None
if console_yes_no('Provide Provision Token', True):
token = console_input('Token', os.environ.get('TOKEN'))
print(f'Using provision token: {ANSII_GREEN}{token}{ANSII_RESET}')
verity_url = console_input(f'Verity Application Endpoint', os.environ.get('VERITY_SERVER'))
print(f'Using Verity Application Endpoint Url: {ANSII_GREEN}{verity_url}{ANSII_RESET}')
# create initial Context
context = await Context.create(WALLET_NAME, WALLET_KEY, verity_url)
# ask that an agent by provision (setup) and associated with created key pair
try:
response = await Provision(token).provision(context)
return response
except Exception as e:
print(e)
print('Provisioning failed! Likely causes:')
print('- token not provided but Verity Endpoint requires it')
print('- token provided but is invalid or expired')
sys.exit(1)
async def update_webhook_endpoint():
global context, port
webhook_from_ctx: str = context.endpoint_url
if not webhook_from_ctx:
# Default to localhost on the default port
webhook_from_ctx = f'http://localhost:{port}'
webhook: str = console_input(f'Ngrok endpoint [{webhook_from_ctx}]', os.environ.get('WEBHOOK_URL'))
if not webhook:
webhook = webhook_from_ctx
print(f'Using Webhook: {ANSII_GREEN}{webhook}{ANSII_RESET}')
print()
context.endpoint_url = webhook
# request that verity application use specified webhook endpoint
await UpdateEndpoint().update(context)
async def update_configs():
handlers.add_handler('update-configs', '0.6', noop)
configs = UpdateConfigs(INSTITUTION_NAME, LOGO_URL)
await configs.update(context)
async def issuer_identifier(loop):
# constructor for the Issuer Setup protocol
issuer_setup = IssuerSetup()
first_step = loop.create_future()
spinner = make_spinner('Waiting for current issuer DID') # Console spinner
# handler for current issuer identifier message
async def current_identifier(msg_name, message):
global issuer_did
global issuer_verkey
spinner.stop_and_persist('Done')
if msg_name == IssuerSetup.PUBLIC_IDENTIFIER:
issuer_did = message['did']
issuer_verkey = message['verKey']
first_step.set_result(None)
elif msg_name == IssuerSetup.PROBLEM_REPORT:
# Do nothing. Just means we need to write the keys to the ledger. Checked for in setup()
first_step.set_result(None)
else:
non_handled(f'Message name is not handled - {msg_name}', message)
# adds handler to the set of handlers
handlers.add_handler(IssuerSetup.MSG_FAMILY, IssuerSetup.MSG_FAMILY_VERSION, current_identifier)
spinner.start()
# query the current identifier
await issuer_setup.current_public_identifier(context)
await first_step # wait for response from verity application
async def setup_issuer(loop):
# constructor for the Issuer Setup protocol
issuer_setup = IssuerSetup()
first_step = loop.create_future()
spinner = make_spinner('Waiting for setup to complete') # Console spinner
# handler for created issuer identifier message
async def public_identifier_handler(msg_name, message):
global issuer_did
global issuer_verkey
spinner.stop_and_persist('Done')
if msg_name == IssuerSetup.PUBLIC_IDENTIFIER_CREATED:
issuer_did = message['identifier']['did']
issuer_verkey = message['identifier']['verKey']
print(f'Issuer DID: {ANSII_GREEN}{issuer_did}{ANSII_RESET}')
print(f'Issuer Verkey: {ANSII_GREEN}{issuer_verkey}{ANSII_RESET}')
print('The issuer DID and Verkey must be registered on the ledger.')
print('Please send your issuer DID and Verkey to support@evernym.com to add them to the ledger.')
console_input('Press ENTER when DID is on ledger')
first_step.set_result(None)
else:
non_handled(f'Message name is not handled - {msg_name}')
# adds handler to the set of handlers
handlers.add_handler(IssuerSetup.MSG_FAMILY, IssuerSetup.MSG_FAMILY_VERSION, public_identifier_handler)
spinner.start()
# request that issuer identifier be created
await issuer_setup.create(context)
await first_step # wait for request to complete
@routes.post('/')
async def endpoint_handler(request):
try:
await handlers.handle_message(context, await request.read())
return web.Response(text='Success')
except Exception as e:
traceback.print_exc()
return web.Response(text=str(e))
async def main(loop):
global port
global server
app = web.Application(loop=loop)
app.add_routes(routes)
# noinspection PyDeprecation
server = await loop.create_server(app.make_handler(), '0.0.0.0', port)
print('Listening on port {}'.format(port))
await loop.create_task(example(loop))
if __name__ == '__main__':
mainloop = asyncio.get_event_loop()
mainloop.run_until_complete(main(mainloop))