forked from sirilux191/Lyfelynk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.mo
767 lines (683 loc) · 27.9 KB
/
main.mo
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
import Array "mo:base/Array";
import Blob "mo:base/Blob";
import Buffer "mo:base/Buffer";
import HashMap "mo:base/HashMap";
import Int "mo:base/Int";
import Iter "mo:base/Iter";
import Nat "mo:base/Nat";
import Nat8 "mo:base/Nat8";
import Principal "mo:base/Principal";
import Result "mo:base/Result";
import Text "mo:base/Text";
import Time "mo:base/Time";
import Map "mo:map/Map";
import { phash; thash } "mo:map/Map";
import Source "mo:uuid/async/SourceV4";
import UUID "mo:uuid/UUID";
import Types "Types";
import Hex "utils/Hex";
actor LyfeLynk {
let healthUserIDMap = Map.new<Principal, Types.HealthIDUser>();
let healthProfessionalIDMap = Map.new<Principal, Types.HealthIDProfessional>();
let healthFacilityIDMap = Map.new<Principal, Types.HealthIDFacility>();
let principalIDMap = Map.new<Principal, Text>(); //Map of Principal of User with Health ID
let idPrincipalMap = Map.new<Text, Principal>(); //Map of Health ID of User with Principal
let dataAssetStorage = Map.new<Text, HashMap.HashMap<Text, Types.DataAsset>>(); //UserID <---> Timestamp <---> DataAsset
let dataAccessTP = Map.new<Text, [Principal]>(); //AssetIDUnique <---> user
let dataAccessPT = Map.new<Principal, [Text]>(); //User <---> AssetIDUnique
let sharedFileList = Map.new<Principal, [Types.sharedActivityInfo]>();
stable var userRegistrationNumberCount : Nat = 10000000000000;
stable var profRegistrationNumberCount : Nat = 100000000000;
stable var facilityRegistrationNumberCount : Nat = 1000000000;
let userListings = Map.new<Principal, [Types.Listing]>();
//AUTHENTICATION FUNCTIONS
//AUTHENTICATION FUNCTIONS
// CRUD OPERATIONS USER ID
public shared ({ caller }) func createUser(demoInfo : Blob, basicHealthPara : Blob, bioMData : ?Blob, familyData : ?Blob) : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't register, please login with wallet or internet identity");
};
let g = Source.Source();
let uuid = UUID.toText(await g.new());
userRegistrationNumberCount := userRegistrationNumberCount +1;
let tempIDData : Types.HealthIDUserData = {
DemographicInformation = demoInfo;
BasicHealthParameters = basicHealthPara;
BiometricData = bioMData;
FamilyInformation = familyData;
};
let tempID : Types.HealthIDUser = {
IDNum = Nat.toText(userRegistrationNumberCount);
UUID = uuid;
MetaData = tempIDData;
};
Map.set(healthUserIDMap, phash, caller, tempID);
Map.set(principalIDMap, phash, caller, Nat.toText(userRegistrationNumberCount));
Map.set(idPrincipalMap, thash, Nat.toText(userRegistrationNumberCount), caller);
#ok("Your unique Health ID is:" # Nat.toText(userRegistrationNumberCount));
};
public shared ({ caller }) func readUser() : async Result.Result<Types.HealthIDUser, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't register, please login with wallet or internet identity");
};
let readResult = Map.get(healthUserIDMap, phash, caller);
switch (readResult) {
case (?value) { #ok(value) };
case (null) { #err("Your not registered as Health User") };
};
};
public shared ({ caller }) func updateUser(demoInfo : Blob, basicHealthPara : Blob, bioMData : ?Blob, familyData : ?Blob) : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't update, please login with wallet or internet identity");
};
let readResult = Map.get(healthUserIDMap, phash, caller);
switch (readResult) {
case (?value) {
let tempIDData : Types.HealthIDUserData = {
DemographicInformation = demoInfo;
BasicHealthParameters = basicHealthPara;
BiometricData = bioMData;
FamilyInformation = familyData;
};
let updatedID : Types.HealthIDUser = {
IDNum = value.IDNum;
UUID = value.UUID;
MetaData = tempIDData;
};
Map.set(healthUserIDMap, phash, caller, updatedID);
#ok("User health ID updated successfully");
};
case (null) {
#err("You're not registered as a Health User");
};
};
};
public shared ({ caller }) func deleteUser() : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't delete, please login with wallet or internet identity");
};
let readResult = Map.get(healthUserIDMap, phash, caller);
switch (readResult) {
case (?value) {
Map.delete(healthUserIDMap, phash, caller);
Map.delete(principalIDMap, phash, caller);
Map.delete(idPrincipalMap, thash, value.IDNum);
#ok("User health ID deleted successfully");
};
case (null) {
#err("You're not registered as a Health User");
};
};
};
// CRUD OPERATIONS USER ID
// CRUD OPERATIONS PROFESSIONAL ID
public shared ({ caller }) func createProfessional(demoInfo : Blob, occupationInfo : Blob, certificationInfo : Blob) : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't register, please login with wallet or internet identity");
};
let g = Source.Source();
let uuid = UUID.toText(await g.new());
profRegistrationNumberCount := profRegistrationNumberCount + 1;
let tempIDData : Types.HealthIDProfessionalData = {
DemographicInformation = demoInfo;
OccupationInformation = occupationInfo;
CertificationInformation = certificationInfo;
};
let tempID : Types.HealthIDProfessional = {
IDNum = Nat.toText(profRegistrationNumberCount);
UUID = uuid;
MetaData = tempIDData;
};
Map.set(healthProfessionalIDMap, phash, caller, tempID);
Map.set(principalIDMap, phash, caller, Nat.toText(profRegistrationNumberCount));
Map.set(idPrincipalMap, thash, Nat.toText(profRegistrationNumberCount), caller);
#ok("Your unique Health Professional ID is:" # Nat.toText(profRegistrationNumberCount));
};
public shared ({ caller }) func readProfessional() : async Result.Result<Types.HealthIDProfessional, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't register, please login with wallet or internet identity");
};
let readResult = Map.get(healthProfessionalIDMap, phash, caller);
switch (readResult) {
case (?value) { #ok(value) };
case (null) {
#err("You're not registered as a Health Professional");
};
};
};
public shared ({ caller }) func updateProfessional(demoInfo : Blob, occupationInfo : Blob, certificationInfo : Blob) : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't update, please login with wallet or internet identity");
};
let readResult = Map.get(healthProfessionalIDMap, phash, caller);
switch (readResult) {
case (?value) {
let tempIDData : Types.HealthIDProfessionalData = {
DemographicInformation = demoInfo;
OccupationInformation = occupationInfo;
CertificationInformation = certificationInfo;
};
let updatedID : Types.HealthIDProfessional = {
IDNum = value.IDNum;
UUID = value.UUID;
MetaData = tempIDData;
};
Map.set(healthProfessionalIDMap, phash, caller, updatedID);
#ok("Professional health ID updated successfully");
};
case (null) {
#err("You're not registered as a Health Professional");
};
};
};
public shared ({ caller }) func deleteProfessional() : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't delete, please login with wallet or internet identity");
};
let readResult = Map.get(healthProfessionalIDMap, phash, caller);
switch (readResult) {
case (?value) {
Map.delete(healthProfessionalIDMap, phash, caller);
Map.delete(principalIDMap, phash, caller);
Map.delete(idPrincipalMap, thash, value.IDNum);
#ok("Professional health ID deleted successfully");
};
case (null) {
#err("You're not registered as a Health Professional");
};
};
};
// CRUD OPERATIONS FACILITY ID
public shared ({ caller }) func createFacility(demoInfo : Blob, servicesOfferedInfo : Blob, licenseInfo : Blob) : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't register, please login with wallet or internet identity");
};
let g = Source.Source();
let uuid = UUID.toText(await g.new());
facilityRegistrationNumberCount := facilityRegistrationNumberCount + 1;
let tempIDData : Types.HealthIDFacilityData = {
DemographicInformation = demoInfo;
ServicesOfferedInformation = servicesOfferedInfo;
LicenseInformation = licenseInfo;
};
let tempID : Types.HealthIDFacility = {
IDNum = Nat.toText(facilityRegistrationNumberCount);
UUID = uuid;
MetaData = tempIDData;
};
Map.set(healthFacilityIDMap, phash, caller, tempID);
Map.set(principalIDMap, phash, caller, Nat.toText(facilityRegistrationNumberCount));
Map.set(idPrincipalMap, thash, Nat.toText(facilityRegistrationNumberCount), caller);
#ok("Your unique Health Facility ID is:" # Nat.toText(facilityRegistrationNumberCount));
};
public shared ({ caller }) func readFacility() : async Result.Result<Types.HealthIDFacility, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't register, please login with wallet or internet identity");
};
let readResult = Map.get(healthFacilityIDMap, phash, caller);
switch (readResult) {
case (?value) { #ok(value) };
case (null) { #err("You're not registered as a Health Facility") };
};
};
public shared ({ caller }) func updateFacility(demoInfo : Blob, servicesOfferedInfo : Blob, licenseInfo : Blob) : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't update, please login with wallet or internet identity");
};
let readResult = Map.get(healthFacilityIDMap, phash, caller);
switch (readResult) {
case (?value) {
let tempIDData : Types.HealthIDFacilityData = {
DemographicInformation = demoInfo;
ServicesOfferedInformation = servicesOfferedInfo;
LicenseInformation = licenseInfo;
};
let updatedID : Types.HealthIDFacility = {
IDNum = value.IDNum;
UUID = value.UUID;
MetaData = tempIDData;
};
Map.set(healthFacilityIDMap, phash, caller, updatedID);
#ok("Facility health ID updated successfully");
};
case (null) {
#err("You're not registered as a Health Facility");
};
};
};
public shared ({ caller }) func deleteFacility() : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't delete, please login with wallet or internet identity");
};
let readResult = Map.get(healthFacilityIDMap, phash, caller);
switch (readResult) {
case (?value) {
Map.delete(healthFacilityIDMap, phash, caller);
Map.delete(principalIDMap, phash, caller);
Map.delete(idPrincipalMap, thash, value.IDNum);
#ok("Facility health ID deleted successfully");
};
case (null) {
#err("You're not registered as a Health Facility");
};
};
};
// Link health data with user's health ID
public shared ({ caller }) func linkHealthData(healthData : Types.DataAsset) : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't link health data, please login with wallet or internet identity");
};
let userIDResult = Map.get(principalIDMap, phash, caller);
switch (userIDResult) {
case (?userID) {
let timestamp = Int.toText(Time.now());
let uniqueID = userID # timestamp;
let healthDataMap = Map.get(dataAssetStorage, thash, userID);
switch (healthDataMap) {
case (?existingDataMap) {
existingDataMap.put(timestamp, healthData);
Map.set(dataAssetStorage, thash, userID, existingDataMap);
Map.set(dataAccessPT, phash, caller, [uniqueID]);
Map.set(dataAccessTP, thash, uniqueID, [caller]);
#ok(uniqueID);
};
case (null) {
let newDataMap = HashMap.HashMap<Text, Types.DataAsset>(0, Text.equal, Text.hash);
newDataMap.put(timestamp, healthData);
Map.set(dataAssetStorage, thash, userID, newDataMap);
Map.set(dataAccessPT, phash, caller, [uniqueID]);
Map.set(dataAccessTP, thash, uniqueID, [caller]);
#ok(uniqueID);
};
};
};
case (null) {
#err("You're not registered.");
};
};
};
// Grant access to a health data asset
public shared ({ caller }) func grantDataAccess(userID : Text, timestamp : Text) : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't grant access, please login with wallet or internet identity");
};
let userIDResult = Map.get(idPrincipalMap, thash, userID);
switch (userIDResult) {
case (?userIDResult) {
let ownerID = Map.get(principalIDMap, phash, caller);
switch (ownerID) {
case (?ownerID) {
let dataAssetMap = Map.get(dataAssetStorage, thash, ownerID);
switch (dataAssetMap) {
case (?assetMap) {
let tempArray1 = Map.get(dataAccessPT, phash, userIDResult);
switch (tempArray1) {
case (?array) {
let buff = Buffer.fromArray<Text>(array);
buff.add(userID #timestamp);
Map.set(dataAccessPT, phash, userIDResult, Buffer.toArray(buff));
};
case (null) {
Map.set(dataAccessPT, phash, userIDResult, [userID #timestamp]);
};
};
let tempArray2 = Map.get(dataAccessTP, thash, userID #timestamp);
switch (tempArray2) {
case (?array) {
let buff = Buffer.fromArray<Principal>(array);
buff.add(userIDResult);
Map.set(dataAccessTP, thash, userID #timestamp, Buffer.toArray(buff));
};
case (null) {
Map.set(dataAccessTP, thash, userID #timestamp, [userIDResult]);
};
};
// Add the shared activity information to the sharedFileList map
let sharedInfo : Types.sharedActivityInfo = {
assetID = userID # timestamp;
usedSharedTo = userID;
time = Int.abs(Time.now());
};
let ownerSharedFiles = Map.get(sharedFileList, phash, caller);
switch (ownerSharedFiles) {
case (?files) {
let buff = Buffer.fromArray<Types.sharedActivityInfo>(files);
buff.add(sharedInfo);
Map.set(sharedFileList, phash, caller, Buffer.toArray(buff));
};
case (null) {
Map.set(sharedFileList, phash, caller, [sharedInfo]);
};
};
#ok("Sucessfully access given to user with ID " #userID);
};
case (null) {
#err("No data asset available");
};
};
};
case (null) {
#err("Invalid user");
};
};
};
case (null) {
#err("Invalid user ID.");
};
};
};
// Revoke access to a health data asset
public shared ({ caller }) func revokeDataAccess(userID : Text, timestamp : Text) : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't revoke access, please login with wallet or internet identity");
};
let userIDResult = Map.get(idPrincipalMap, thash, userID);
switch (userIDResult) {
case (?userIDResult) {
let ownerID = Map.get(principalIDMap, phash, caller);
switch (ownerID) {
case (?ownerID) {
let dataAssetMap = Map.get(dataAssetStorage, thash, ownerID);
switch (dataAssetMap) {
case (?assetMap) {
let tempArray1 = Map.get(dataAccessPT, phash, userIDResult);
switch (tempArray1) {
case (?array) {
let buff = Buffer.Buffer<Text>(array.size());
for (val in array.vals()) {
if (val != userID #timestamp) {
buff.add(val);
};
};
Map.set(dataAccessPT, phash, userIDResult, Buffer.toArray(buff));
};
case (null) {};
};
let tempArray2 = Map.get(dataAccessTP, thash, userID #timestamp);
switch (tempArray2) {
case (?array) {
let buff = Buffer.Buffer<Principal>(array.size());
for (val in array.vals()) {
if (val != userIDResult) {
buff.add(val);
};
};
Map.set(dataAccessTP, thash, userID #timestamp, Buffer.toArray(buff));
};
case (null) {};
};
// Remove the shared activity information from the sharedFileList map
let ownerSharedFiles = Map.get(sharedFileList, phash, caller);
switch (ownerSharedFiles) {
case (?files) {
let updatedFiles = Array.filter(
files,
func(item : Types.sharedActivityInfo) : Bool {
item.assetID != userID # timestamp;
},
);
Map.set(sharedFileList, phash, caller, updatedFiles);
};
case (null) {};
};
#ok("Access revoked for user with ID " #userID);
};
case (null) {
#err("No data asset available");
};
};
};
case (null) {
#err("Invalid user");
};
};
};
case (null) {
#err("Invalid user ID.");
};
};
};
public shared query ({ caller }) func getSharedFileList() : async Result.Result<[Types.sharedActivityInfo], Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous persons can't revoke access, please login with wallet or internet identity");
};
let list = Map.get(sharedFileList, phash, caller);
switch (list) {
case (?value) {
#ok(value);
};
case (null) {
#err("No activity of user");
};
};
};
public shared query ({ caller }) func getUserDataAssets() : async Result.Result<[(Text, Types.DataAsset)], Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous principals cannot access data assets. Please log in with a wallet or internet identity.");
};
let userID = Map.get(principalIDMap, phash, caller);
switch (userID) {
case (?id) {
let dataAssetMap = Map.get(dataAssetStorage, thash, id);
switch (dataAssetMap) {
case (?assetMap) {
let assetList = Iter.toArray<(Text, Types.DataAsset)>(assetMap.entries());
return #ok(assetList);
};
case (null) {
return #err("No data assets found for the user.");
};
};
};
case (null) {
return #err("Caller is not a registered user.");
};
};
};
public shared ({ caller }) func addListing(
title : Text,
description : Text,
price : Nat,
category : Text,
assetID : Text,
) : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous principals cannot add listings. Please log in with a wallet or internet identity.");
};
let userID = Map.get(principalIDMap, phash, caller);
switch (userID) {
case (?id) {
let dataAssetMap = Map.get(dataAssetStorage, thash, id);
switch (dataAssetMap) {
case (?assetMap) {
let assetData = assetMap.get(assetID);
switch (assetData) {
case (?asset) {
let newListing : Types.Listing = {
title;
description;
price;
category;
seller = caller;
assetID = assetID;
};
let existingListings = Map.get(userListings, phash, caller);
switch (existingListings) {
case (?listings) {
let buff = Buffer.fromArray<Types.Listing>(listings);
buff.add(newListing);
Map.set(userListings, phash, caller, Buffer.toArray(buff));
};
case (null) {
Map.set(userListings, phash, caller, [newListing]);
};
};
return #ok("Listing added successfully");
};
case (null) {
return #err("No data asset found for the provided timestamp.");
};
};
};
case (null) {
return #err("User has no data assets.");
};
};
};
case (null) {
return #err("Caller is not a registered user.");
};
};
};
public shared ({ caller }) func deleteListingByAssetID(assetID : Text) : async Result.Result<Text, Text> {
if (Principal.isAnonymous(caller)) {
return #err("Anonymous principals cannot delete listings. Please log in with a wallet or internet identity.");
};
let userID = Map.get(principalIDMap, phash, caller);
switch (userID) {
case (?id) {
let listings = Map.get(userListings, phash, caller);
switch (listings) {
case (?listings) {
let filteredListings = Array.filter<Types.Listing>(
listings,
func(listing : Types.Listing) : Bool {
listing.assetID != assetID;
},
);
if (filteredListings.size() == listings.size()) {
return #err("Listing with the provided asset ID not found.");
};
Map.set(userListings, phash, caller, filteredListings);
return #ok("Listing deleted successfully.");
};
case (null) {
return #err("You have no listings.");
};
};
};
case (null) {
return #err("Caller is not a registered user.");
};
};
};
public shared query func getAllListings() : async Result.Result<[Types.Listing], Text> {
if (Map.size(userListings) == 0) {
return #err("No listings found.");
};
let allListings = Buffer.Buffer<Types.Listing>(0);
for ((user, listings) in Map.entries(userListings)) {
for (listing in listings.vals()) {
allListings.add(listing);
};
};
if (allListings.size() == 0) {
return #err("No listings found.");
};
return #ok(Buffer.toArray(allListings));
};
//VetKey Section
type VETKD_SYSTEM_API = actor {
vetkd_public_key : ({
canister_id : ?Principal;
derivation_path : [Blob];
key_id : { curve : { #bls12_381 }; name : Text };
}) -> async ({ public_key : Blob });
vetkd_encrypted_key : ({
public_key_derivation_path : [Blob];
derivation_id : Blob;
key_id : { curve : { #bls12_381 }; name : Text };
encryption_public_key : Blob;
}) -> async ({ encrypted_key : Blob });
};
let vetkd_system_api : VETKD_SYSTEM_API = actor ("s55qq-oqaaa-aaaaa-aaakq-cai");
public shared ({ caller }) func symmetric_key_verification_key_for_profile() : async Text {
let { public_key } = await vetkd_system_api.vetkd_public_key({
canister_id = null;
derivation_path = Array.make(Text.encodeUtf8("profile_symmetric_key"));
key_id = { curve = #bls12_381; name = "test_key_1" };
});
Hex.encode(Blob.toArray(public_key));
};
public shared ({ caller }) func encrypted_symmetric_key_for_id(profile_id : Text, encryption_public_key : Blob) : async Result.Result<Text, Text> {
let principal = Map.get(idPrincipalMap, thash, profile_id);
switch (principal) {
case (?principal) {
if (principal != caller) {
return #err("Unauthorized access. The provided profile ID does not belong to the caller.");
};
let profile_data = if (profile_id.size() == 14) {
Map.get(healthUserIDMap, phash, caller);
} else if (profile_id.size() == 12) {
Map.get(healthProfessionalIDMap, phash, caller);
} else if (profile_id.size() == 10) {
Map.get(healthFacilityIDMap, phash, caller);
} else {
return #err("Invalid profile ID length.");
};
let idToDerive = Nat.fromText(profile_id);
switch (idToDerive) {
case (?idToDerive) {
switch (profile_data) {
case (?profile_data) {
let buf = Buffer.Buffer<Nat8>(32);
buf.append(Buffer.fromArray(natToBigEndianByteArray(16, idToDerive))); // fixed-size encoding
buf.append(Buffer.fromArray(Blob.toArray(Text.encodeUtf8(profile_data.UUID))));
let derivation_id = Blob.fromArray(Buffer.toArray(buf));
let { encrypted_key } = await vetkd_system_api.vetkd_encrypted_key({
derivation_id;
public_key_derivation_path = Array.make(Text.encodeUtf8("profile_symmetric_key"));
key_id = { curve = #bls12_381; name = "test_key_1" };
encryption_public_key;
});
#ok(Hex.encode(Blob.toArray(encrypted_key)));
};
case (null) {
#err("Profile data not found for the provided profile ID.");
};
};
};
case (null) {
#err("Sorry you have to given proper profile ID");
};
};
};
case (null) {
#err("Invalid profile ID. No principal associated with the provided ID.");
};
};
};
public shared ({ caller }) func encrypted_symmetric_key_for_data(assetID : Text, encryption_public_key : Blob) : async Result.Result<Text, Text> {
let accessList = Map.get(dataAccessTP, thash, assetID);
switch (accessList) {
case (?accessList) {
if (not Array.contains<Principal>(accessList, caller, Principal.equal)) {
return #err("Unauthorized access. The caller does not have access to the data asset.");
};
let buf = Buffer.Buffer<Nat8>(32);
buf.append(Buffer.fromArray(Blob.toArray(Text.encodeUtf8(assetID))));
let derivation_id = Blob.fromArray(Buffer.toArray(buf));
let { encrypted_key } = await vetkd_system_api.vetkd_encrypted_key({
derivation_id;
public_key_derivation_path = Array.make(Text.encodeUtf8("data_symmetric_key"));
key_id = { curve = #bls12_381; name = "test_key_1" };
encryption_public_key;
});
#ok(Hex.encode(Blob.toArray(encrypted_key)));
};
case (null) {
#err("Data asset not found or access not granted.");
};
};
};
//VetKey Section
// Converts a nat to a fixed-size big-endian byte (Nat8) array
private func natToBigEndianByteArray(len : Nat, n : Nat) : [Nat8] {
let ith_byte = func(i : Nat) : Nat8 {
assert (i < len);
let shift : Nat = 8 * (len - 1 - i);
Nat8.fromIntWrap(n / 2 ** shift);
};
Array.tabulate<Nat8>(len, ith_byte);
};
};