-
Notifications
You must be signed in to change notification settings - Fork 1
/
if_lib.py
1343 lines (1069 loc) · 44.5 KB
/
if_lib.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
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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2022-2023 Dyne.org foundation <[email protected]>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Let's start with some lovely imports that should have been installed if not available by default
import json
import inspect
import requests
import os
import contextlib
from zenroom import zenroom
import base64
from datetime import datetime, timezone
import random
from pdb import set_trace
from if_consts import SUPPORTED_ACTIONS, IN_PR_ACTIONS, OUT_PR_ACTIONS, IN_OUT_PR_ACTIONS
from if_consts import EVENT_FRAG, AGENT_FRAG, QUANTITY_FRAG, RESOURCE_FRAG, PROPOSAL_FRAG, \
INTENT_FRAG, PROPINT_FRAG, LOCATION_FRAG, ACTION_FRAG, PROCESS_FRAG, PROCESSGRP_FRAG, PROCESSSPEC_FRAG, \
UNIT_FRAG, RESSPEC_FRAG
from if_utils import stringify
def zenroom_wrapper(contract, keys=None, data=None):
with open(os.devnull, 'w') as f:
with contextlib.redirect_stderr(f):
# set_trace()
return zenroom.zencode_exec(contract, keys=keys, data=data)
# Test zenroom is correctly installed and running
def generate_random_challenge():
"""
This function calls zenroom to generate
a random string to be used as challenge
"""
contract = """
rule check version 1.0.0
Given nothing
When I create the random object of '512' bits
and I rename the 'random_object' to 'challenge'
Then print 'challenge'
"""
try:
resz = zenroom.zencode_exec(contract)
except Exception as e:
print(f'Exception in zenroom call: {e}')
return None
resz_json = json.loads(resz.output)
print(f"Generated challenge: {resz_json['challenge']}")
return
# we will save endpoint specific files since the data is saved on a particular endpoint
# Get the seed from the server to later generate the keypair
DEBUG_get_HMAC = False
def get_HMAC(email, endpoint, newUser=True):
variables = {
"firstRegistration": newUser,
"userData": "{\"email\": \"" + email + "\"}"
};
payload = {
"query": """mutation ($firstRegistration: Boolean!, $userData: JSONObject!){
keypairoomServer(firstRegistration: $firstRegistration, userData: $userData)
}""",
"variables": variables
}
res = requests.post(endpoint, json=payload)
if DEBUG_get_HMAC:
print("Payload")
print(payload)
print("Variables")
print(variables)
print("Result")
print(res)
try:
res_json = res.json()
except Exception as e:
print("Payload")
print(payload)
print("Variables")
print(variables)
print("Result")
print(res)
raise Exception(f"Exception {e} in function {inspect.stack()[0][3]}")
if DEBUG_get_HMAC:
print("JSON")
print(json.dumps(res_json, indent=2))
has_errors = False
if "errors" in res_json and len(res_json['errors']) > 0:
for err in res_json['errors']:
if err['message'] == "email exists":
return get_HMAC(email, endpoint, newUser=False)
else:
has_errors = True
if has_errors:
print("Payload")
print(payload)
print("Variables")
print(variables)
print("Result")
print(json.dumps(res_json, indent=2))
raise Exception(f"Error in function {inspect.stack()[0][3]}")
return res_json
# if the HMAC is not in the conf files call the function to get it
def read_HMAC(file, users_data, user, endpoint):
# this should not be possible since we initialize the data, but anyway
if not f'{user}' in users_data:
users_data[f'{user}'] = {}
print("Warning this should not happen")
user_data = users_data[f'{user}']
# check we already have a credentials file with a HMAC
if 'seedServerSideShard.HMAC' in user_data:
print(f"Server HMAC available for {user_data['name']}")
return
if os.path.isfile(file):
with open(file,'r') as f:
tmp_users_data = json.loads(f.read())
tmp_user_data = tmp_users_data[f'{user}']
else:
tmp_user_data = {}
if 'seedServerSideShard.HMAC' not in tmp_user_data:
res_json = get_HMAC(user_data['email'], endpoint)
# save the HMAC in the user data
user_data['seedServerSideShard.HMAC'] = res_json['data']['keypairoomServer']
# save data with HMAC
with open(file,'w') as f:
# Save the entire data structure
json.dump(users_data, f)
else:
user_data['seedServerSideShard.HMAC'] = tmp_user_data['seedServerSideShard.HMAC']
# no need to save since we read it from file
DEBUG_generate_keypair = False
# Generate the user keypair (and the mnemonic seed)
def generate_keypair(userdata: dict) -> dict:
"""
This function calls zenroom to generate
a keypair using the server-provided HMAC
"""
contract = """
Scenario 'ecdh': Create the key
Scenario 'ethereum': Create key
Scenario 'reflow': Create the key
Scenario 'schnorr': Create the key
Scenario 'eddsa': Create the key
Scenario 'qp': Create the key
# Loading the user name from data
Given my name is in a 'string' named 'username'
# Loading the answers from 3 secret questions. The user will have to pick the 3 challenges from a list
# and have to remember the questions - the order is not important cause Zenroom will sort alphabetically
# the data in input
#
# NOTE: the challenges will never be communicated to the server or to anybody else!
Given I have a 'string dictionary' named 'userChallenges'
# Loading the individual challenges, in order to have them hashed
# and the hashes OPTIONALLY stored by the server, to improve regeneration of the keypair
Given I have a 'string' named 'whereParentsMet' in 'userChallenges'
Given I have a 'string' named 'nameFirstPet' in 'userChallenges'
Given I have a 'string' named 'whereHomeTown' in 'userChallenges'
Given I have a 'string' named 'nameFirstTeacher' in 'userChallenges'
Given I have a 'string' named 'nameMotherMaid' in 'userChallenges'
# Loading the pbkdf received from the server, containing a signed hash of known data
Given that I have a 'base64' named 'seedServerSideShard.HMAC'
# Save the backup for mnemonic dump, before factoring with the salt
# it is shortened to 16 bytes by hashing sha512 the KDF and taking the first 16 bytes
When I create the key derivation of 'userChallenges'
and I create the hash of 'key derivation' using 'sha512'
and I split the leftmost '16' bytes of 'hash'
and I delete the 'key derivation'
and I delete the 'hash'
and I rename the 'leftmost' to 'seed'
# Hash again the user's challenges with salt for the seed root
When I rename 'seedServerSideShard.HMAC' to 'salt'
and I create the key derivation of 'seed' with password 'salt'
and I rename the 'key derivation' to 'seed.root'
# In the following flow the order should NOT be changed
When I create the hash of 'seed.root'
When I rename the 'hash' to 'seed.ecdh'
When I create the hash of 'seed.ecdh'
When I rename the 'hash' to 'seed.eddsa'
When I create the hash of 'seed.eddsa'
When I rename the 'hash' to 'seed.ethereum'
When I create the hash of 'seed.ethereum'
When I rename the 'hash' to 'seed.reflow'
When I create the hash of 'seed.reflow'
When I rename the 'hash' to 'seed.schnorr'
# end of the sorted creation flow
When I create the ecdh key with secret key 'seed.ecdh'
When I create the eddsa key with secret key 'seed.eddsa'
When I create the ethereum key with secret key 'seed.ethereum'
When I create the reflow key with secret key 'seed.reflow'
When I create the schnorr key with secret key 'seed.schnorr'
When I create the ecdh public key
When I create the eddsa public key
When I create the ethereum address
When I create the reflow public key
When I create the schnorr public key
# Creating the hashes of the single challenges, to OPTIONALLY help
# regeneration of the keypair
When I create the 'base64 dictionary'
and I rename the 'base64 dictionary' to 'hashedAnswers'
When I create the key derivation of 'whereParentsMet'
and I rename the 'key derivation' to 'whereParentsMet.kdf'
When I insert 'whereParentsMet.kdf' in 'hashedAnswers'
When I create the key derivation of 'nameFirstPet'
and I rename the 'key derivation' to 'nameFirstPet.kdf'
When I insert 'nameFirstPet.kdf' in 'hashedAnswers'
When I create the key derivation of 'whereHomeTown'
and I rename the 'key derivation' to 'whereHomeTown.kdf'
When I insert 'whereHomeTown.kdf' in 'hashedAnswers'
When I create the key derivation of 'nameFirstTeacher'
and I rename the 'key derivation' to 'nameFirstTeacher.kdf'
When I insert 'nameFirstTeacher.kdf' in 'hashedAnswers'
When I create the key derivation of 'nameMotherMaid'
and I rename the 'key derivation' to 'nameMotherMaid.kdf'
When I insert 'nameMotherMaid.kdf' in 'hashedAnswers'
# This prints the keyring
Then print the 'keyring'
# this prints the hashes of the challenges
# Then print the 'hashedAnswers'
# This prints the seed for the private keys as mnemonic
Then print the 'seed' as 'mnemonic'
Then print the 'ecdh public key'
Then print the 'eddsa public key'
Then print the 'ethereum address'
Then print the 'reflow public key'
Then print the 'schnorr public key'
"""
data = json.dumps(userdata)
try:
resz = zenroom.zencode_exec(contract, data=data)
except Exception as e:
print(f'Exception in zenroom call: {e}')
raise Exception(f"Error in function {inspect.stack()[0][3]}")
return {}
if DEBUG_generate_keypair:
print(f'result: {resz}')
resz_json = json.loads(resz.output)
if DEBUG_generate_keypair:
print(f"Generated keypair data: {json.dumps(resz_json, indent=2)}")
return resz_json
# Read the keypair from conf files or call the function to generate it
def read_keypair(file, users_data, user):
# this should not be possible since we initialize the data, but anyway
if not f'{user}' in users_data:
users_data[f'{user}'] = {}
print("Warning this should not happen")
user_data = users_data[f'{user}']
if ('seed' in user_data and 'eddsa_public_key' in user_data and \
'keyring' in user_data and 'eddsa' in user_data['keyring']):
print(f"Keypair available for {user_data['name']}")
return
if os.path.isfile(file):
with open(file,'r') as f:
tmp_users_data = json.loads(f.read())
tmp_user_data = tmp_users_data[f'{user}']
else:
tmp_user_data = {}
if not ('seed' in tmp_user_data and 'eddsa_public_key' in tmp_user_data and \
'keyring' in tmp_user_data and 'eddsa' in tmp_user_data['keyring']):
res_json = generate_keypair(user_data)
# Update data in user data
user_data['seed'] = res_json['seed']
user_data['eddsa_public_key'] = res_json['eddsa_public_key']
user_data['keyring'] = {}
user_data['keyring']['eddsa'] = res_json['keyring']['eddsa']
with open(file,'w') as f:
# save the entire data structure, not just one user
json.dump(users_data, f)
else:
print(f"Keypair available from file for {user_data['name']}")
user_data['seed'] = tmp_user_data['seed']
user_data['eddsa_public_key'] = tmp_user_data['eddsa_public_key']
user_data['keyring'] = {}
user_data['keyring']['eddsa'] = tmp_user_data['keyring']['eddsa']
# Create the person using their public key
DEBUG_create_Person = False
def create_Person(name, username, email, eddsaPublicKey, endpoint, newPerson=True):
if newPerson:
variables = {
"person": {
"name": name,
"user": username,
"email": email,
"eddsaPublicKey": eddsaPublicKey
}
}
payload = {
"query": """mutation ($person: PersonCreateParams!){
createPerson(person: $person)
{
agent{
...agent
}
}
}""" + AGENT_FRAG + LOCATION_FRAG,
"variables": json.dumps(variables)
}
else:
variables = {
"email": email,
"eddsaPublicKey": eddsaPublicKey
}
payload = {
"query": """query ($email:String!, $eddsaPublicKey: String!){
personCheck(email:$email, eddsaPublicKey:$eddsaPublicKey){
id
}
}""",
"variables": json.dumps(variables)
}
# print(json.dumps(payload, indent=2))
# Temporarily: we need a key to create a person, before email authentication is implemented
if 'IF_KEY' in os.environ:
SECRET_KEY = os.environ['IF_KEY']
else:
file = '.credentials.json'
assert os.path.isfile(file)
with open(file) as f:
data = json.load(f)
SECRET_KEY = data['key']
headers={'zenflows-admin': SECRET_KEY}
res = requests.post(endpoint, json=payload, headers=headers)
if DEBUG_create_Person:
print("Payload")
print(payload)
print("Headers")
print(headers)
print("Response")
print(res)
try:
res_json = res.json()
except Exception as e:
print("Payload")
print(payload)
print("Headers")
print(headers)
print("Result")
print(res)
raise Exception(f"Exception {e} in function {inspect.stack()[0][3]}")
if DEBUG_create_Person:
print("Result")
print(json.dumps(res_json, indent=2))
if "errors" in res_json and len(res_json['errors']) > 0:
for err in res_json['errors']:
if err['message'] == "user: [\"has already been taken\"]":
return create_Person(name, username, email, eddsaPublicKey, endpoint, newPerson=False)
if newPerson:
user_id = res_json['data']['createPerson']['agent']['id']
else:
user_id = res_json['data']['personCheck']['id']
return user_id
# Read the ID of the person from file or create a new person
def get_id_person(file, users_data, user, endpoint):
user_data = users_data[f'{user}']
if 'id' in user_data:
print(f"Id available for {user_data['name']}")
return
if os.path.isfile(file):
with open(file,'r') as f:
tmp_users_data = json.loads(f.read())
tmp_user_data = tmp_users_data[f'{user}']
else:
tmp_user_data = {}
if not 'id' in tmp_user_data:
user_data['id'] = create_Person(user_data['name'], user_data['username'], user_data['email'], user_data['eddsa_public_key'], endpoint)
# print(json.dumps(user_data, indent=2))
with open(file,'w') as f:
json.dump(users_data, f)
else:
print(f"Id available from file for {user_data['name']}")
user_data['id'] = tmp_user_data['id']
# sign and send each request now that we have a registered public key
DEBUG_send_signed = False
def send_signed(query: str, variables: dict, username: str, eddsa: str, endpoint:str) -> dict:
sign_script = """
Scenario eddsa: sign a graph query
Given I have a 'base64' named 'gql'
Given I have a 'keyring'
# Fix Apollo's mingling with query string
When I remove spaces in 'gql'
and I compact ascii strings in 'gql'
When I create the eddsa signature of 'gql'
And I create the hash of 'gql'
Then print 'eddsa signature' as 'base64'
Then print 'gql' as 'base64'
Then print 'hash' as 'hex'
"""
zenKeys = stringify({
"keyring": {
"eddsa": eddsa
}
})
payload = {"query": query, "variables": variables}
zenData = {
"gql": base64.b64encode(bytes(json.dumps(payload), 'utf-8')).decode('utf-8')
}
zenData_str = stringify(zenData)
try:
resz = zenroom.zencode_exec(sign_script, keys=zenKeys, data=zenData_str)
except Exception as e:
print(f'Exception in zenroom call: {e}')
return {}
if DEBUG_send_signed:
print("zenData")
print(zenData)
print("Zenroom result")
print(resz)
resz_json = json.loads(resz.output)
if DEBUG_send_signed:
print("Generated signature")
print(json.dumps(resz_json, indent=2))
# Reset the headears
headers = {}
headers['content-type'] = 'application/json'
headers['zenflows-sign'] = resz_json['eddsa_signature']
headers['zenflows-user'] = username
headers['zenflows-hash'] = resz_json['hash']
res = requests.post(endpoint, json=payload, headers=headers)
try:
res_json = res.json()
except Exception as e:
print("Payload")
print(payload)
print("Headers")
print(headers)
print("Result")
print(res)
raise Exception(f"Exception {e} in function {inspect.stack()[0][3]}")
if DEBUG_send_signed:
print("Payload")
print(payload)
print("Headers")
print(headers)
print("Response")
print(json.dumps(res_json, indent=2))
return res_json
# Read the location id from file or generate it by calling the back-end
DEBUG_get_location_id = False
def get_location_id(file, user_data, locs_data, user, endpoint):
# this should not be possible since we initialize the data, but anyway
if not f'{user}' in locs_data:
locs_data[f'{user}'] = {}
print("Warning this should not happen")
loc_data = locs_data[f'{user}']
if 'id' in loc_data:
print(f"Location id available for {loc_data['name']}")
return
# check we already have a location file with an id
if os.path.isfile(file):
with open(file,'r') as f:
temp_locs_data = json.loads(f.read())
temp_loc_data = temp_locs_data[f'{user}']
else:
temp_loc_data = {}
if 'id' not in temp_loc_data:
# Register location
# Produce the query and variables vars to be signed
variables = {
"location": {
"name": loc_data['name'],
"alt": 0,
"lat": loc_data['lat'],
"long": loc_data['long'],
"mappableAddress": loc_data['addr'],
"note": loc_data['note']
}
}
query = """mutation($location: SpatialThingCreateParams!) {
createSpatialThing(spatialThing: $location) {
spatialThing {
id
}
}
}"""
res_json = send_signed(query, variables, user_data['username'], user_data['keyring']['eddsa'], endpoint)
if 'errors' in res_json:
print("Error message")
print(json.dumps(res_json['errors'], indent=2))
print("Query")
print(query)
print("Variables")
print(variables)
raise Exception(f"Error in function {inspect.stack()[0][3]}")
if DEBUG_get_location_id:
print("Query")
print(query)
print("Variables")
print(variables)
print("Response")
print(json.dumps(res_json, indent=2))
# save the id in the location data
loc_data['id'] = res_json['data']['createSpatialThing']['spatialThing']['id']
# reference the location in the user data
loc_data['user_id'] = user_data['id']
# save data with id
with open(file,'w') as f:
# Save the entire location data, not just the user one
json.dump(locs_data, f)
else:
print(f"Location id available in file for {loc_data['name']}")
loc_data['id'] = temp_loc_data['id']
# Read the location id from file or generate it by calling the back-end
DEBUG_set_user_location = False
def set_user_location(file, users_data, locs_data, user, endpoint):
user_data = users_data[user]
if 'location_id' in user_data:
print(f"Location id available for {user_data['name']}")
return
loc_data = locs_data[f'{user}']
variables = {
"person": {
"id": user_data['id'],
"primaryLocation": loc_data['id']
}
}
query = """mutation($person: PersonUpdateParams!) {
updatePerson(person: $person) {
agent {
...agent
primaryLocation {
id
}
}
}
}""" + AGENT_FRAG + LOCATION_FRAG
res_json = send_signed(query, variables, user_data['username'], user_data['keyring']['eddsa'], endpoint)
if 'errors' in res_json:
print("Error message")
print(json.dumps(res_json['errors'], indent=2))
print("Query")
print(query)
print("Variables")
print(variables)
raise Exception(f"Error in function {inspect.stack()[0][3]}")
if DEBUG_set_user_location:
print("Query")
print(query)
print("Variables")
print(variables)
print("Response")
print(json.dumps(res_json, indent=2))
# save the id in the location data
user_data['location_id'] = res_json['data']['updatePerson']['agent']['primaryLocation']['id']
# save data with location id (for each user, redudant)
with open(file,'w') as f:
# Save the entire location data, not just the user one
json.dump(users_data, f)
# Read the unit id from file or generate it by calling the back-end
def get_unit_id(file, user_data, units_data, name, label, symbol, endpoint):
if name in units_data and 'id' in units_data[f'{name}']:
print(f"Unit {name} available")
return
# check we already have a unit file with an id
if os.path.isfile(file):
with open(file,'r') as f:
temp_units_data = json.loads(f.read())
else:
temp_units_data = {}
if not (name in temp_units_data and 'id' in temp_units_data[f'{name}']):
# Produce the query and variables vars to be signed
variables = {
"unit": {
"label": label,
"symbol": symbol
}
}
query = """mutation($unit:UnitCreateParams!) {
createUnit(unit: $unit) {
unit {
id
}
}
}"""
res_json = send_signed(query, variables, user_data['username'], user_data['keyring']['eddsa'], endpoint)
if 'errors' in res_json:
print("Error message")
print(json.dumps(res_json['errors'], indent=2))
print("Query")
print(query)
print("Variables")
print(variables)
raise Exception(f"Error in function {inspect.stack()[0][3]}")
# save the unit info
units_data[f'{name}'] = {}
units_data[f'{name}']['label'] = label
units_data[f'{name}']['symbol'] = symbol
units_data[f'{name}']['id'] = res_json['data']['createUnit']['unit']['id']
# save data with id
with open(file,'w') as f:
json.dump(units_data, f)
else:
print(f"Unit available in file for {temp_units_data[f'{name}']}")
units_data[f'{name}'] = {}
units_data[f'{name}']['label'] = temp_units_data[f'{name}']['label']
units_data[f'{name}']['symbol'] = temp_units_data[f'{name}']['symbol']
units_data[f'{name}']['id'] = temp_units_data[f'{name}']['id']
# Read the resource specification id or create a new one if not available
def get_resource_spec_id(file, user_data, res_spec_data, name, note, classification, default_unit_id, endpoint):
if name in res_spec_data and 'id' in res_spec_data[f'{name}']:
print(f"Specification {name} available")
return
# check we already have a unit file with an id
if os.path.isfile(file):
with open(file,'r') as f:
temp_res_spec_data = json.loads(f.read())
else:
temp_res_spec_data = {}
if not (name in temp_res_spec_data and 'id' in temp_res_spec_data[f'{name}']):
# Produce the query and variables vars to be signed
variables = {
"resourceSpecification": {
"defaultUnitOfResource": default_unit_id,
"name": name,
"note": note,
"resourceClassifiedAs": classification
}
}
query = """mutation ($resourceSpecification:ResourceSpecificationCreateParams!){
createResourceSpecification(resourceSpecification:$resourceSpecification){
resourceSpecification{
id,
name
}
}
}"""
res_json = send_signed(query, variables, user_data['username'], user_data['keyring']['eddsa'], endpoint)
if 'errors' in res_json:
print("Error message")
print(json.dumps(res_json['errors'], indent=2))
print("Query")
print(query)
print("Variables")
print(variables)
raise Exception(f"Error in function {inspect.stack()[0][3]}")
# save the unit info
res_spec_data[f'{name}'] = {}
res_spec_data[f'{name}']['note'] = note
res_spec_data[f'{name}']['classification'] = classification
res_spec_data[f'{name}']['defaultUnit'] = default_unit_id
res_spec_data[f'{name}']['id'] = res_json['data']['createResourceSpecification']['resourceSpecification']['id']
# save data with id
with open(file,'w') as f:
json.dump(res_spec_data, f)
else:
print(f"Specification available in file for {temp_res_spec_data[f'{name}']}")
res_spec_data[f'{name}'] = {}
res_spec_data[f'{name}']['note'] = temp_res_spec_data[f'{name}']['note']
res_spec_data[f'{name}']['classification'] = temp_res_spec_data[f'{name}']['classification']
res_spec_data[f'{name}']['defaultUnit'] = temp_res_spec_data[f'{name}']['defaultUnit']
res_spec_data[f'{name}']['id'] = temp_res_spec_data[f'{name}']['id']
# Create the resource by calling the back-end
DEBUG_create_resource = False
def create_resource(user_data, res_data, res_spec_data, amount, endpoint):
provider = user_data['id']
receiver = user_data['id']
# Get the unit from the spec, no need to pass it
unit_id = [specs['defaultUnit'] for name, specs in res_spec_data.items() \
if specs['id'] == res_data['spec_id']][0]
# Produce the query and variables vars to be signed
# Getting the current date and time
ts = datetime.now(timezone.utc).isoformat()
variables = {
"event": {
"note": "create resource",
"action": "raise",
"provider": provider,
"receiver": receiver,
"hasPointInTime" : ts,
"resourceQuantity": {
"hasUnit": unit_id,
"hasNumericalValue": amount
},
"resourceConformsTo": res_data['spec_id'],
"toLocation" : user_data['location_id']
},
"newInventoriedResource": {
"name": res_data['name'],
"trackingIdentifier": res_data['res_ref_id']
}
}
query = """mutation($event:EconomicEventCreateParams!, $newInventoriedResource:EconomicResourceCreateParams) {
createEconomicEvent(event:$event, newInventoriedResource:$newInventoriedResource) {
economicEvent {
id
provider {
...agent
}
resourceQuantity {
...quantity
}
resourceInventoriedAs {
...resource
}
}
}
}""" + AGENT_FRAG + LOCATION_FRAG + QUANTITY_FRAG + RESOURCE_FRAG
res_json = send_signed(query, variables, user_data['username'], user_data['keyring']['eddsa'], endpoint)
if 'errors' in res_json:
print("Error message")
print(json.dumps(res_json['errors'], indent=2))
print("Query")
print(query)
print("Variables")
print(variables)
raise Exception(f"Error in function {inspect.stack()[0][3]}")
if DEBUG_create_resource:
print("Query")
print(query)
print("Variables")
print(variables)
print("Result")
print(json.dumps(res_json, indent=2))
# save the unit info
res_data['id'] = res_json['data']['createEconomicEvent']['economicEvent']['resourceInventoriedAs']['id']
return res_json['data']['createEconomicEvent']['economicEvent']['id'], ts
# Create the resource by calling the back-end
DEBUG_reduce_resource = False
def reduce_resource(user_data, res_data, res_spec_data, amount, endpoint):
provider = user_data['id']
receiver = user_data['id']
# Get the unit from the spec, no need to pass it
unit_id = [specs['defaultUnit'] for name, specs in res_spec_data.items() \
if specs['id'] == res_data['spec_id']][0]
# Produce the query and variables vars to be signed
# Getting the current date and time
ts = datetime.now(timezone.utc).isoformat()
variables = {
"event": {
"note": "update event",
"action": "lower",
"provider": provider,
"receiver": receiver,
"hasPointInTime" : ts,
"resourceInventoriedAs" : res_data['id'],
"resourceQuantity": {
"hasUnit": unit_id,
"hasNumericalValue": amount
},
"resourceConformsTo": res_data['spec_id']
}
}
query = """mutation($event:EconomicEventCreateParams!, $newInventoriedResource:EconomicResourceCreateParams) {
createEconomicEvent(event:$event, newInventoriedResource:$newInventoriedResource) {
economicEvent {
id
provider {
...agent
}
resourceQuantity {
...quantity
}
resourceInventoriedAs {
...resource
}
}
}
}""" + AGENT_FRAG + LOCATION_FRAG + QUANTITY_FRAG + RESOURCE_FRAG
res_json = send_signed(query, variables, user_data['username'], user_data['keyring']['eddsa'], endpoint)
if 'errors' in res_json:
print("Error message")
print(json.dumps(res_json['errors'], indent=2))
print("Query")
print(query)
print("Variables")
print(variables)
raise Exception(f"Error in function {inspect.stack()[0][3]}")
if DEBUG_reduce_resource:
print("Query")
print(query)
print("Variables")
print(variables)
print("Result")
print(json.dumps(res_json, indent=2))
return res_json['data']['createEconomicEvent']['economicEvent']['id'], ts
# Wrapper for the resource creation
def get_resource(res_data, res_spec_data, res_name, user_data, event_seq, amount, endpoint):
# breakpoint()
res_data[f'{res_name}_res'] = {}
cur_res = res_data[f'{res_name}_res']
rnd = random.randint(0, 10000)
cur_res['res_ref_id'] = f'{res_name}-{rnd}'
cur_res['name'] = res_name
cur_res['spec_id'] = res_spec_data[f'{res_name}']['id']
event_id, ts = create_resource(user_data, cur_res, res_spec_data, amount, endpoint)