This repository was archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
854 lines (792 loc) · 35.7 KB
/
server.py
File metadata and controls
854 lines (792 loc) · 35.7 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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
from flask import Flask, request
from flask_cors import CORS
from flask_restx import Api, Resource, fields, reqparse, inputs
from flask_login import LoginManager, current_user, login_required
from redis_conn import provide_redis_conn
import time
import requests
import random
from concurrent.futures import ThreadPoolExecutor, as_completed
from functools import partial, wraps
import json
from email_helper import send_ses_email
import eth_utils
from eth_account.messages import defunct_hash_message
from eth_account.account import Account
import stripe
from ethvigil.EVCore import EVCore
from constants import *
evc = EVCore(verbose=False)
with open('settings.conf.json', 'r') as f:
settings = json.load(f)
main_contract_instance = evc.generate_contract_sdk(contract_address=settings['MAIN_CONTRACT'], app_name='AdabdhaMain')
app = Flask(__name__)
if settings['CORS_ENABLED']:
CORS(app)
login_manager = LoginManager()
login_manager.init_app(app)
def recover_address_from_sig():
signed_timestamp_token = request.headers.get('X-Wallet-Signature')
if signed_timestamp_token:
# 'adabdha_'+timestamp+':'+walletSign('adabdha_'+timestamp)
string_data, signed_data = signed_timestamp_token.split(':')
string_data_hashed = defunct_hash_message(string_data.encode('utf-8'))
recovered_eth_address = Account.recoverHash(message_hash=string_data_hashed, signature=signed_data)
recovered_eth_address = eth_utils.to_normalized_address(recovered_eth_address)
return recovered_eth_address
else:
return None
class AdabdhaUserPrototype():
def __init__(self, json_details):
self.user_details = json_details
for each in json_details.keys():
self.__setattr__(each, json_details[each])
def __repr__(self):
return json.dumps(self.user_details)
def is_authenticated(self):
return True
@login_manager.request_loader
@provide_redis_conn
def load_user_from_timestamp_token(request, redis_conn=None):
signed_timestamp_token = request.headers.get('X-Wallet-Signature')
ret = None
if signed_timestamp_token:
# 'adabdha_'+timestamp+':'+walletSign('adabdha_'+timestamp)
recovered_eth_address = recover_address_from_sig()
u = redis_conn.hget(REDIS_ADABDHA_USERS, recovered_eth_address)
if not u:
ret = None
else:
try:
user_details = json.loads(u)
except json.JSONDecodeError:
ret = None
else:
ret = AdabdhaUserPrototype(user_details)
else:
ret = None
return ret
@login_manager.unauthorized_handler
def unauthorized():
return {'success': False, 'error': 'Unauthorized'}, 401
def only_ethaddress_allowed(f):
"""
check identified user from header X-Wallet-Signature.
If API paylaod contains argument for ethAddress allow only if the identifying user is the same as ethAddress
:param f:
:return:
"""
@wraps(f)
def wrapper(*args, **kwargs):
try:
supplied_eth_addr = api.payload['ethAddress']
except:
supplied_eth_addr = None
if not supplied_eth_addr:
return {'success': False, 'error': 'BadPayload'}, 400
header_sig_eth_address = recover_address_from_sig()
if not header_sig_eth_address:
return {'success': False, 'error': 'Unauthorized'}, 401
if header_sig_eth_address == eth_utils.to_normalized_address(api.payload['ethAddress']):
return f(*args, **kwargs)
else:
return {'success': False, 'error': 'Unauthorized'}, 401
return wrapper
def get_check_usermode_wrapper(god_mode=False):
def deco(func):
def mod_func(*args, **kwargs):
try:
is_admin = current_user.isAdmin
except AttributeError:
is_admin = False
path_eth_addr = kwargs.get('ethAddress')
if is_admin or (path_eth_addr and current_user.ethAddress == eth_utils.to_normalized_address(path_eth_addr)) or god_mode:
return func(*args, **kwargs)
else:
return {'success': False, 'error': 'Unauthorized'}, 401
return mod_func
return deco
only_allowed_user_or_admin = get_check_usermode_wrapper()
only_form_approval_god_mode = get_check_usermode_wrapper(god_mode=settings['GOD_MODE_APPROVAL'])
api = Api(app, version='0.1', title='Adabdha Identity Oracle MVP API',
description='A simple API for the Identity Oracle for Adabdha™️')
ns_toplevel_users = api.namespace('users', description='Adabdha user operations')
user_model = api.model('User', {
'email': fields.String(required=True, description='User email'),
'ethAddress': fields.String(required=True, description='User ethereum address'),
'otpVerified': fields.Boolean(readonly=True, description='Is user OTP verified'),
'kycVerified': fields.String(readonly=True, description='user KYC verification state'),
'otp': fields.Integer(readonly=True, description='Assigned OTP')
})
form_data_model = api.model('FormData', {
'ethAddress': fields.String(required=True, description='Ethereum address of user'),
'uuid': fields.String(required=True, description='UUID of the application'),
'formData': fields.Arbitrary(description='JSON data')
})
form_op_model = api.model('FormOperation', {
'action': fields.String(required=True, description="Action on form: approve or reject")
})
user_put_parser = reqparse.RequestParser()
user_put_parser.add_argument('otp', help='Confirm the OTP', location='json')
updateops_return = api.model('UserUpdateOpsReturn', {
'txHash': fields.String(required=True, description='transaction hash sent out'),
'contractAddress': fields.String(required=True, description='contract address to which tx is sent'),
'methodName': fields.String(required=True, description='method called in transaction'),
})
user_post_parser = reqparse.RequestParser()
user_post_parser.add_argument('action', help='initiateVerify | initiateMockVerify', location='json', default='initiateVerify | initiateMockVerify')
user_post_parser.add_argument(
'return_url',
help='URL to redirect to once verification process is complete',
location='json'
)
pass_modify_parser = reqparse.RequestParser()
pass_modify_parser.add_argument('action', help='revoke | [....]', location='json')
user_post_return = api.model('UserPostReturn', {
'id': fields.String(required=True, description='Stripe verification intent ID'),
'redirect': fields.String(required=False, description='Redirect URL for ID verification')
})
user_passes_return = api.model('UserPassesReturn', {
'passDataHash': fields.String(required=True, description='Hash of JSON serialized pass data committed on contract'),
# 'passData': fields.Nested(required=True, description='JSON serialized pass data'),
'formUUIDHash': fields.String(required=True, description='UUID Hash of JSON serialized form data against which '
'pass was generated'),
'timestamp': fields.Integer(required=True, description='Timestamp of PassGenerated event')
})
user_get_return = api.model('UserGetReturn', {
'email': fields.String,
'ethAddress': fields.String,
'otpVerified': fields.Boolean,
'kycVerified': fields.String,
'isAdmin': fields.Boolean
})
ns_indiv_user = api.namespace('user', description='User specific operations')
ns_forms = api.namespace('forms', description='user data form submission and retrieval')
ns_form_operations = api.namespace('form', description='user data form submission and retrieval')
ns_user_pass = api.namespace('pass', description='Passes generated for users')
ns_passes = api.namespace('passes', description='All passes')
@ns_toplevel_users.route('/')
class AdabdhaUsers(Resource):
@only_ethaddress_allowed
@ns_toplevel_users.doc('create_user')
@ns_toplevel_users.expect(user_model)
@provide_redis_conn
def post(self, redis_conn=None):
"""Initiate registration of a new user"""
# add entry into redis, return OTP
user_obj = api.payload
eth_address = eth_utils.to_normalized_address(api.payload['ethAddress'])
otp = random.choice(range(100000, 999999))
if settings['EMAIL_SEND']:
email_status = send_ses_email(
email_addr=api.payload['email'],
subject='Your Adabdha OTP',
text='Confirm your OTP on Adabdha: ' + str(otp)
)
if not email_status:
return user_obj, 500
redis_conn.set( eth_address + '_otp', otp, ex=900)
user_obj = {
'email': api.payload['email'],
'ethAddress': eth_address,
'otpVerified': False,
'kycVerified': 'unverified'
}
redis_conn.hset(
REDIS_ADABDHA_USERS,
eth_address,
json.dumps(user_obj).encode('utf-8')
)
return user_obj, 201
@login_required
@only_allowed_user_or_admin
@ns_toplevel_users.doc('list_users')
@ns_toplevel_users.marshal_list_with(user_get_return)
@provide_redis_conn
def get(self, redis_conn=None):
"""Returns all users"""
u_l = list()
try:
users_mapping = redis_conn.hgetall(REDIS_ADABDHA_USERS)
except json.JSONDecodeError:
return [], 404
else:
for k, v in users_mapping.items():
v = json.loads(v)
if 'isAdmin' not in v:
v['isAdmin'] = False
_u_v = redis_conn.get(REDIS_ADABDHA_USER_VI_TOKEN.format(k.decode('utf-8')))
if not _u_v:
# no waiting verificaion intents found
pass
else:
# check VI status
v_s = redis_conn.get(REDIS_ADABDHA_VI_STATUS.format(_u_v.decode('utf-8')))
if v_s and int(v_s) == -1:
# set kyc verified status to requested
v['kycVerified'] = 'requested'
u_l.append(v)
return u_l
@ns_indiv_user.route('/<string:ethAddress>')
@ns_indiv_user.param('ethAddress', 'The Ethereum address of the user')
class IndividualAdabdhaUserByEthAddr(Resource):
"""Operations on individual Adabdha users"""
@login_required
@only_allowed_user_or_admin
@ns_indiv_user.expect(user_put_parser)
@ns_indiv_user.marshal_with(updateops_return, 201)
@provide_redis_conn
def put(self, ethAddress, redis_conn=None):
eth_address = eth_utils.to_normalized_address(ethAddress)
if current_user.ethAddress != eth_address:
return {'success': False, 'error': 'Unauthorized'}, 400
parsed_args = user_put_parser.parse_args()
user_details = json.loads(redis_conn.hget('adabdha:users', eth_address))
if parsed_args.get('otp'):
try:
parsed_otp = int(parsed_args.get('otp'))
except:
return {'success': False, 'error': 'BadOTP'}, 400
stored_otp = redis_conn.get(eth_address + '_otp')
if not stored_otp:
return parsed_args, 404
else:
stored_otp = int(stored_otp.decode('utf-8'))
if stored_otp != parsed_otp:
return parsed_args, 400
else:
# Set OTP verified on user details only after transaction is confirmed on contract
# user_details.update({'otpVerified': True})
#
# redis_conn.hset(
# 'adabdha:users',
# eth_address,
# json.dumps(user_details).encode('utf-8')
# )
# Add to main contract
email_hash = eth_utils.keccak(text=user_details['email']).hex()
email_hash = eth_utils.add_0x_prefix(email_hash)
add_user_params = dict(ethAddress=eth_address, emailHash=email_hash)
try:
tx = main_contract_instance.addUserByEmail(**add_user_params)
print('Confirmed user on contract with tx: ', tx[0]['txHash'])
except Exception as e:
print('Exception trying to add user details on Contract ', add_user_params, e)
return user_details, 400
else:
return_struct = {
'txHash': tx[0]['txHash'],
'contractAddress': settings['MAIN_CONTRACT'],
'methodName': 'addUserByEmail'
}
return return_struct, 201
@ns_indiv_user.expect(user_post_parser)
@ns_indiv_user.marshal_with(user_post_return, 201)
@provide_redis_conn
def post(self, ethAddress, redis_conn=None):
parsed_args = user_post_parser.parse_args()
eth_address = eth_utils.to_normalized_address(ethAddress)
return_url = api.payload['return_url']
user_details = json.loads(redis_conn.hget('adabdha:users', eth_address))
action = parsed_args.get('action')
if action == 'initiateVerify' or action == 'initiateMockVerify' \
and user_details.get('kycVerified') not in ['verified', 'pending']:
# check if vi token already exists and has been initiated for processing
existing_vi = redis_conn.get(REDIS_ADABDHA_USER_VI_TOKEN.format(eth_address))
if existing_vi \
and int(redis_conn.get(REDIS_ADABDHA_VI_STATUS.format(existing_vi.decode('utf-8')))) == -1:
return {'success': False, 'error': 'VerificationInitiated'}, 400
if action == 'initiateVerify':
stripe.api_key = settings['STRIPE_KEY']['LIVE']
vi_live_mode = True
else:
stripe.api_key = settings['STRIPE_KEY']['DEMO']
vi_live_mode = False
stripe.api_version = '2018-11-08; identity_beta=v3'
r = stripe.stripe_object.StripeObject().request(
'post',
'/v1/identity/verification_intents',
{
'requested_verifications': [
'identity_document',
'selfie'
],
'metadata': {
'ethAddress': eth_address
},
'return_url': return_url
}
)
verification_intent_id = r['id']
print('Got stripe verification intent ID : ', verification_intent_id)
# print(r)
# associate intent ID with user
redis_conn.set(
REDIS_ADABDHA_USER_VI_TOKEN.format(eth_address),
verification_intent_id,
ex=600
)
redis_conn.set(
REDIS_ADABDHA_VI_STATUS.format(verification_intent_id),
str(-1)
)
redis_conn.set(
REDIS_ADABDHA_VI_LIVE_MODE.format(verification_intent_id),
str(int(vi_live_mode))
)
return {'id': verification_intent_id, 'redirect': r['next_action']['redirect_to_url']}, 201
@login_required
@only_allowed_user_or_admin
@ns_indiv_user.marshal_with(user_get_return, 200)
@provide_redis_conn
def get(self, ethAddress, redis_conn=None):
ethAddress = eth_utils.to_normalized_address(ethAddress)
u = redis_conn.hget(REDIS_ADABDHA_USERS, ethAddress)
if not u:
return {}, 404
details = json.loads(u)
if 'isAdmin' not in details:
details['isAdmin'] = False
_u_v = redis_conn.get(REDIS_ADABDHA_USER_VI_TOKEN.format(ethAddress))
if not _u_v:
# no waiting verificaion intents found
pass
else:
# check VI status
v_s = redis_conn.get(REDIS_ADABDHA_VI_STATUS.format(_u_v.decode('utf-8')))
if v_s and int(v_s) == -1:
# set kyc verified status to requested
details['kycVerified'] = 'requested'
return details, 200
def get_NewForm_eventlogs(event_param_name, event_param_value, evcore_obj: EVCore):
"""
:return Return event logs for `NewForm` events indexed by `event_param_name` and `event_param_value`
"""
api_key = evcore_obj._api_write_key
contract_address = settings['MAIN_CONTRACT']
headers = {'accept': 'application/json', 'Content-Type': 'application/json', 'X-API-KEY': api_key}
method_api_endpoint = f'{evc._settings["REST_API_ENDPOINT"]}/cachedeventdata'
method_args = {
'contract': contract_address,
'event_name': 'NewForm',
'indexed_param_name': event_param_name,
'indexed_param_value': event_param_value
}
r = requests.post(url=method_api_endpoint, json=method_args, headers=headers)
# print(r.text)
r = r.json()
data = r['data']
form_uuidhashes = dict()
for cache_entry_with_ts in data:
event_data = json.loads(cache_entry_with_ts[0])
form_uuidhashes[event_data['uuidHash']] = {
'uuidHash': event_data['uuidHash'],
'formDataHash': event_data['formdataHash'],
'timestamp': cache_entry_with_ts[1]
}
return form_uuidhashes
@ns_indiv_user.route('/<string:ethAddress>/forms')
@ns_indiv_user.param('ethAddress', 'Ethereum address associated with user')
class IndividualAdabdhaUserFormByEthAddr(Resource):
@login_required
@only_allowed_user_or_admin
@provide_redis_conn
def get(self, ethAddress, redis_conn=None):
forms = list()
ethAddress = eth_utils.to_normalized_address(ethAddress)
# fetch from event logs cache on EthVigil
submitted_form_uuidhashes = get_NewForm_eventlogs('ethAddress', ethAddress, evc)
# merge with locally stored applications
pending_form_uuidhashes = redis_conn.zrange(
'adabdha:pendingUserForms:{}'.format(eth_utils.to_normalized_address(ethAddress)),
0,
-1,
withscores=True
)
for _ in pending_form_uuidhashes:
form_uuid_hash = _[0].decode('utf-8')
timestamp = int(_[1])
if form_uuid_hash not in submitted_form_uuidhashes:
redis_form_data = redis_conn.hget(
'adabdha:forms',
form_uuid_hash
)
if redis_form_data:
redis_form_data = redis_form_data.decode('utf-8')
hashed_formdata = eth_utils.keccak(text=redis_form_data).hex()
hashed_formdata = eth_utils.add_0x_prefix(hashed_formdata)
submitted_form_uuidhashes.update({
form_uuid_hash: {
'uuidHash': form_uuid_hash,
'formDataHash': hashed_formdata,
'timestamp': timestamp
}
})
else:
# update time stamp to hold submitted time instead of event time
submitted_form_uuidhashes[form_uuid_hash]['timestamp'] = timestamp
for each in submitted_form_uuidhashes:
form_info_map = {}
form_dets = submitted_form_uuidhashes[each]
# further fetch form data JSON information from redis
# and status information of form from contract
redis_form_data = redis_conn.hget('adabdha:forms', form_dets['uuidHash'])
if not redis_form_data:
return [], 404
redis_form_data = json.loads(redis_form_data)
form_uuid = redis_form_data.pop('uuid', None)
form_status = redis_form_data.pop('status', None)
form_user_eth_address = redis_form_data.pop('userEthAddress', None)
form_info_map['formData'] = redis_form_data
form_info_map['formDataHash'] = form_dets['formDataHash']
form_info_map['uuid'] = form_uuid
form_info_map['uuidHash'] = form_dets['uuidHash']
form_info_map['timestamp'] = form_dets['timestamp']
# form_contract_info = main_contract_instance.submittedForms(each['uuidHash'])
# form_status= form_contract_info['status']
form_info_map['status'] = form_status
forms.append(form_info_map)
return forms
@ns_forms.route('/')
class FormsResource(Resource):
@login_required
@only_allowed_user_or_admin
@provide_redis_conn
def get(self, redis_conn=None):
"""
Get all forms
"""
all_formdata = redis_conn.hgetall('adabdha:forms')
form_uuid_hashes = list(map(lambda x: x.decode('utf-8'), all_formdata.keys()))
new_forms_events = dict()
with ThreadPoolExecutor(max_workers=20) as executor:
future_to_rpcresult = {
executor.submit(
get_NewForm_eventlogs,
event_param_name='uuidHash',
event_param_value=form_uuid_hash,
evcore_obj=main_contract_instance
): form_uuid_hash
for form_uuid_hash in form_uuid_hashes
}
for future in as_completed(future_to_rpcresult):
# collect form uuid hashes
form_uuid_hash = future_to_rpcresult[future]
try:
form_logs = future.result()
except Exception as exc:
# do something in future
print("Error fetching NewForm event logs for form UUID hash{}: {}".format(form_uuid_hash, exc))
continue
else:
for x in form_logs.keys():
new_forms_events[x] = form_logs[x]
print(all_formdata)
transformed_all_formdata = list()
for k, v in all_formdata.items():
k_decoded = k.decode('utf-8')
form_data = json.loads(v)
form_uuid = form_data.pop('uuid', None)
form_status = form_data.pop('status', None)
form_user_eth_addr = form_data.pop('userEthAddress', None)
return_mapping = {
'formData': form_data,
'uuid': form_uuid,
'uuidHash': k_decoded,
'status': form_status
}
return_mapping.update({'uuidHash': k_decoded})
if form_user_eth_addr:
print(f'Looking for form uuid hash {k_decoded} in {new_forms_events}')
if k_decoded in new_forms_events:
return_mapping.update({'timestamp': new_forms_events[k_decoded]['timestamp'], 'ethAddress': form_user_eth_addr})
transformed_all_formdata.append(return_mapping)
del all_formdata
return transformed_all_formdata, 201
@login_required
@ns_forms.expect(form_data_model)
@ns_forms.marshal_with(updateops_return, 201)
@provide_redis_conn
def post(self, redis_conn=None):
"""
Creates a new form application
"""
eth_address = api.payload['ethAddress']
eth_address = eth_utils.to_normalized_address(eth_address)
if not eth_address:
return {}, 400
try:
user_details = json.loads(redis_conn.hget('adabdha:users', eth_address))
except json.JSONDecodeError:
return {}, 404
if not user_details:
return user_details, 404
if not api.payload['formData']:
return {}, 400
_form_data = json.loads(api.payload['formData'])
serialized_form_data = json.dumps(_form_data, separators=(',', ':'))
_form_data.update({'uuid': api.payload['uuid']}) # because we want to store the UUID information in local cache
_form_data.update({'status': -1}) # because we want to store the UUID information in local cache
_form_data.update({'userEthAddress': eth_address})
hashed_formdata = eth_utils.keccak(text=serialized_form_data).hex()
hashed_formdata = eth_utils.add_0x_prefix(hashed_formdata)
hashed_uuid = eth_utils.add_0x_prefix(eth_utils.keccak(text=api.payload['uuid']).hex())
redis_conn.hset(
'adabdha:forms',
hashed_uuid,
json.dumps(_form_data, separators=(',', ':')).encode('utf-8')
)
# Add to main contract
update_formdata_params = dict(
uuidHash=hashed_uuid,
ethAddress=eth_address,
formdataHash=hashed_formdata
)
try:
tx = main_contract_instance.submitFormData(**update_formdata_params)
print('Updated user form data on contract with tx: ', tx[0]['txHash'])
except Exception as e:
print('Exception trying to update user form data on Contract ', update_formdata_params, e)
return {}, 400
else:
redis_conn.zadd(
f'adabdha:pendingUserForms:{eth_address}',
{hashed_uuid: int(time.time())}
)
return_struct = {
'txHash': tx[0]['txHash'],
'contractAddress': settings['MAIN_CONTRACT'],
'methodName': 'submitFormData'
}
return return_struct, 201
@ns_form_operations.route('/<string:uuid>')
class IndividualFormResource(Resource):
@login_required
@only_form_approval_god_mode
@ns_form_operations.expect(form_op_model)
@ns_form_operations.marshal_with(updateops_return, 201)
@provide_redis_conn
def post(self, uuid, redis_conn=None):
if not (api.payload['action'] == 'approve' or api.payload['action'] == 'reject'):
return {}, 400
action = api.payload['action']
hashed_uuid = eth_utils.add_0x_prefix(eth_utils.keccak(text=uuid).hex())
form_action_params = dict(uuidHash=hashed_uuid)
_fd = redis_conn.hget(
REDIS_ADABDHA_FORMS,
hashed_uuid
)
if not _fd:
return {}, 404
redis_form_data = json.loads(_fd)
try:
if action == 'approve':
# set form status to pending approval
tx = main_contract_instance.approveForm(**form_action_params)
redis_form_data.update({'status': 'pendingApproval'})
else:
tx = main_contract_instance.rejectForm(**form_action_params)
redis_form_data.update({'status': 'pendingRejection'})
redis_conn.hset(
REDIS_ADABDHA_FORMS,
hashed_uuid,
json.dumps(redis_form_data).encode('utf-8')
)
print('Updated form data on contract with tx: ', tx[0]['txHash'])
except Exception as e:
print('Exception trying to update form status on Contract ', form_action_params, e)
return {}, 400
else:
return_struct = {
'txHash': tx[0]['txHash'],
'contractAddress': settings['MAIN_CONTRACT'],
'methodName': 'approveForm' if api.payload['action'] == 'approve' else 'rejectForm'
}
return return_struct, 201
@ns_form_operations.route('/<string:uuid>/pass')
class FormToPassResource(Resource):
@login_required
@provide_redis_conn
def get(self, uuid, redis_conn=None):
passes = list()
hashed_uuid = eth_utils.add_0x_prefix(eth_utils.keccak(text=uuid).hex())
generated_passes = get_generated_passes_eventlogs_by_form(hashed_uuid, main_contract_instance)
p_hashes = list(map(lambda x: x[0]['passDataHash'], generated_passes))
status_map = dict()
# asynchronously also fetch pass information
with ThreadPoolExecutor(max_workers=20) as executor:
future_to_rpcresult = dict()
for p_data_hash in p_hashes:
curried_fn = partial(main_contract_instance.passInformation, p_data_hash)
fut = executor.submit(curried_fn)
future_to_rpcresult[fut] = p_data_hash
for future in as_completed(future_to_rpcresult):
p_data_hash = future_to_rpcresult[future]
try:
pass_data_contract = future.result()
except Exception as exc:
# do something in future
print("Error fetching pass Hash {}: {}".format(p_data_hash, exc))
status_map[p_data_hash] = False
else:
status_map[p_data_hash] = pass_data_contract['status']
for e in generated_passes:
each_map = dict()
pass_hash = e[0]['passDataHash']
form_uuid_hash = e[0]['formUUIDHash']
ethAddress = e[0]['ethAddress']
# pull pass data
_pass_data = redis_conn.get(REDIS_ADABDHA_USER_FORM_PASS_DATA.format(form_uuid_hash, ethAddress))
if not _pass_data:
print(f'Could not find pass data in local cache for form UUID hash {form_uuid_hash}')
continue
redis_pass_data = json.loads(_pass_data)
each_map['passDataHash'] = pass_hash
each_map['passData'] = redis_pass_data
each_map['formUUIDHash'] = form_uuid_hash
each_map['timestamp'] = e[1]
each_map['status'] = status_map[pass_hash]
passes.append(each_map)
return passes, 200
def get_generated_passes_eventlogs_by_ethAddress(track_address, evcore_obj: EVCore):
api_key = evcore_obj._api_write_key
contract_address = settings['MAIN_CONTRACT']
headers = {'accept': 'application/json', 'Content-Type': 'application/json', 'X-API-KEY': api_key}
method_api_endpoint = f'{evc._settings["REST_API_ENDPOINT"]}/cachedeventdata'
method_args = {
'contract': contract_address,
'event_name': 'PassGenerated',
'indexed_param_name': 'ethAddress',
'indexed_param_value': track_address
}
r = requests.post(url=method_api_endpoint, json=method_args, headers=headers)
print(r.text)
r = r.json()
data = r['data']
passes = list()
for cache_entry_with_ts in data:
event_data = json.loads(cache_entry_with_ts[0])
passes.append((event_data, int(cache_entry_with_ts[1])))
return passes
def get_generated_passes_eventlogs_by_form(track_formUUIDHash, evcore_obj: EVCore):
api_key = evcore_obj._api_write_key
contract_address = settings['MAIN_CONTRACT']
headers = {'accept': 'application/json', 'Content-Type': 'application/json', 'X-API-KEY': api_key}
method_api_endpoint = f'{evc._settings["REST_API_ENDPOINT"]}/cachedeventdata'
method_args = {
'contract': contract_address,
'event_name': 'PassGenerated',
'indexed_param_name': 'formUUIDHash',
'indexed_param_value': track_formUUIDHash
}
r = requests.post(url=method_api_endpoint, json=method_args, headers=headers)
print(r.text)
r = r.json()
data = r['data']
passes = list()
for cache_entry_with_ts in data:
event_data = json.loads(cache_entry_with_ts[0])
passes.append((event_data, int(cache_entry_with_ts[1])))
return passes
@ns_indiv_user.route('/<string:ethAddress>/passes')
class UserPassesResource(Resource):
# @ns_indiv_user.marshal_list_with(user_passes_return, code=200)
@login_required
@only_allowed_user_or_admin
@provide_redis_conn
def get(self, ethAddress, redis_conn=None):
ethAddress = eth_utils.to_normalized_address(ethAddress)
passes_event_data = get_generated_passes_eventlogs_by_ethAddress(ethAddress, main_contract_instance)
return_list = list()
p_hashes = list(map(lambda x: x[0]['passDataHash'], passes_event_data))
status_map = dict()
# asynchronously also fetch pass information
with ThreadPoolExecutor(max_workers=20) as executor:
future_to_rpcresult = dict()
for p_data_hash in p_hashes:
curried_fn = partial(main_contract_instance.passInformation, p_data_hash)
fut = executor.submit(curried_fn)
future_to_rpcresult[fut] = p_data_hash
for future in as_completed(future_to_rpcresult):
p_data_hash = future_to_rpcresult[future]
try:
pass_data_contract = future.result()
except Exception as exc:
# do something in future
print("Error fetching pass Hash {}: {}".format(p_data_hash, exc))
status_map[p_data_hash] = False
else:
status_map[p_data_hash] = pass_data_contract['status']
for e in passes_event_data:
each_map = dict()
pass_hash = e[0]['passDataHash']
form_uuid_hash = e[0]['formUUIDHash']
# pull pass data
_pass_data = redis_conn.get(REDIS_ADABDHA_USER_FORM_PASS_DATA.format(form_uuid_hash, ethAddress))
if not _pass_data:
print(f'Could not find pass data in local cache for form UUID hash {form_uuid_hash}')
continue
redis_pass_data = json.loads(_pass_data)
each_map['passDataHash'] = pass_hash
each_map['passData'] = redis_pass_data
each_map['formUUIDHash'] = form_uuid_hash
each_map['timestamp'] = e[1]
each_map['status'] = status_map[pass_hash]
return_list.append(each_map)
return return_list, 200
@ns_user_pass.route('/<string:passDataHash>')
class IndividualPassResource(Resource):
@login_required
@only_allowed_user_or_admin
@ns_user_pass.expect(pass_modify_parser)
@ns_user_pass.marshal_with(updateops_return, code=201)
def post(self, passDataHash):
parsed_args = pass_modify_parser.parse_args()
if parsed_args['action'] == 'revoke':
tx = main_contract_instance.revokePass(passDataHash=passDataHash)
print(f'Sent out tx {tx[0]["txHash"]} to revoke pass {passDataHash}')
return {
'txHash': tx[0]["txHash"],
'contractAddress': settings['MAIN_CONTRACT'],
'methodName': 'revokePass'
}, 201
return {}, 400
@ns_passes.route('/')
class PassesResource(Resource):
@login_required
@only_allowed_user_or_admin
@provide_redis_conn
def get(self, redis_conn=None):
passes = list()
p = redis_conn.hgetall(REDIS_ADABDHA_ALL_PASSES)
p_hashes = dict()
for k, v in p.items():
pass_data_hash = k.decode('utf-8')
pass_data_json = json.loads(v)
p_hashes[pass_data_hash] = pass_data_json
with ThreadPoolExecutor(max_workers=20) as executor:
future_to_rpcresult = dict()
for p_data_hash in p_hashes:
curried_fn = partial(main_contract_instance.passInformation, p_data_hash)
fut = executor.submit(curried_fn)
future_to_rpcresult[fut] = p_data_hash
for future in as_completed(future_to_rpcresult):
p_data_hash = future_to_rpcresult[future]
try:
pass_data_contract = future.result()
except Exception as exc:
# do something in future
print("Error fetching pass Hash {}: {}".format(p_data_hash, exc))
continue
else:
form_uuid_hash = pass_data_contract['formUUIDHash']
print(f'Extracted form UUID hash {form_uuid_hash} for pass hash {p_data_hash}')
single_map_obj = {
'passData': p_hashes[p_data_hash],
'passDataHash': p_data_hash,
'formUUIDHash': form_uuid_hash,
'timestamp': pass_data_contract['createdTimestamp'],
'status': pass_data_contract['status']
}
passes.append(single_map_obj)
return passes, 200