This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 736
/
interaction.rs
1081 lines (786 loc) · 43 KB
/
interaction.rs
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
#[macro_use]
mod utils;
inject_indy_dependencies!();
extern crate indyrs as indy;
extern crate indyrs as api;
extern crate core;
extern crate indy_sys;
use crate::utils::{wallet, anoncreds, blob_storage, pool, ledger, did};
use crate::utils::anoncreds::{COMMON_MASTER_SECRET, CREDENTIAL1_ID};
#[cfg(any(feature = "force_full_interaction_tests", not(target_os = "android")))]
#[cfg(not(feature = "only_high_cases"))]
use crate::utils::anoncreds::{CREDENTIAL2_ID, CREDENTIAL3_ID};
use crate::utils::constants::*;
use crate::utils::Setup;
use crate::utils::domain::anoncreds::schema::Schema;
use crate::utils::domain::anoncreds::credential_definition::CredentialDefinition;
use crate::utils::domain::anoncreds::credential_offer::CredentialOffer;
use crate::utils::domain::anoncreds::credential::Credential;
use crate::utils::domain::anoncreds::credential::CredentialInfo;
use crate::utils::domain::anoncreds::revocation_registry_definition::RevocationRegistryDefinition;
use crate::utils::domain::anoncreds::proof::Proof;
use crate::utils::domain::anoncreds::revocation_state::RevocationState;
use crate::utils::domain::anoncreds::revocation_registry::RevocationRegistry;
use std::thread;
use serde_json::Value;
use core::borrow::Borrow;
use indy::{PoolHandle, WalletHandle};
struct Pool {
pool_handle: PoolHandle
}
struct Issuer {
issuer_wallet_handle: WalletHandle,
issuer_wallet_config: String,
issuer_did: String,
schema_id: String,
cred_def_id: String,
rev_reg_id: String,
issuance_type: String,
tails_writer_config: String
}
struct Prover {
wallet_handle: WalletHandle,
wallet_config: String,
did: String,
verkey: String,
master_secret_id: String,
cred_def_id: Option<String>,
cred_req_metadata_json: Option<String>
}
struct Verifier {
proof_request: String
}
impl Pool {
pub fn new(pool_name: &str) -> Pool {
Pool { pool_handle: pool::create_and_open_pool_ledger(pool_name).unwrap() }
}
pub fn close(self) {
let _ = pool::close(self.pool_handle);
}
pub fn submit_nym(&self, issuer_did: &str, issuer_wallet_handle: WalletHandle, prover_did: &str, prover_verkey: Option<&str>)
{
let nym_request = ledger::build_nym_request(issuer_did, prover_did, prover_verkey, None, None).unwrap();
ledger::sign_and_submit_request(self.pool_handle, issuer_wallet_handle, &issuer_did, &nym_request).unwrap();
}
pub fn submit_schema(&self, issuer_did: &str, issuer_wallet_handle: WalletHandle, schema_json: &str) -> String {
let schema_request = ledger::build_schema_request(issuer_did, schema_json).unwrap();
ledger::sign_and_submit_request(self.pool_handle, issuer_wallet_handle, issuer_did, &schema_request).unwrap()
}
pub fn get_schema(&self, did: Option<&str>, schema_id: &str) -> (String, String) {
let get_schema_request = ledger::build_get_schema_request(did, schema_id).unwrap();
let get_schema_response = ledger::submit_request(self.pool_handle, &get_schema_request).unwrap();
ledger::parse_get_schema_response(&get_schema_response).unwrap()
}
pub fn submit_cred_def(&self, issuer_did: &str, issuer_wallet_handle: WalletHandle, cred_def_json: &str) -> String {
let cred_def_request = ledger::build_cred_def_txn(issuer_did, cred_def_json).unwrap();
ledger::sign_and_submit_request(self.pool_handle, issuer_wallet_handle, issuer_did, &cred_def_request).unwrap()
}
pub fn get_cred_def(&self, did: Option<&str>, cred_def_id: &str) -> (String, String) /* (cred_def_id, cred_def_json) */ {
let get_cred_def_request = ledger::build_get_cred_def_request(did, cred_def_id).unwrap();
let get_cred_def_response = ledger::submit_request(self.pool_handle, &get_cred_def_request).unwrap();
ledger::parse_get_cred_def_response(&get_cred_def_response).unwrap()
}
pub fn submit_revoc_reg_def(&self, issuer_did: &str, issuer_wallet_handle: WalletHandle, rev_reg_def_json: &str) -> String {
let rev_reg_def_request = ledger::build_revoc_reg_def_request(issuer_did, rev_reg_def_json).unwrap();
ledger::sign_and_submit_request(self.pool_handle, issuer_wallet_handle, issuer_did, &rev_reg_def_request).unwrap()
}
pub fn get_revoc_reg_def(&self, did: Option<&str>, revoc_reg_def_id: &str) -> (String, String) /* revoc_reg_def_id, revo_reg_def_json */ {
let get_rev_reg_def_request = ledger::build_get_revoc_reg_def_request(did, &revoc_reg_def_id).unwrap();
let get_rev_reg_def_response = ledger::submit_request(self.pool_handle, &get_rev_reg_def_request).unwrap();
ledger::parse_get_revoc_reg_def_response(&get_rev_reg_def_response).unwrap()
}
pub fn submit_revoc_reg_entry(&self, issuer_did: &str, issuer_wallet_handle: WalletHandle, rev_reg_id: &str, rev_reg_entry_json: &str) -> String {
let rev_reg_entry_request =
ledger::build_revoc_reg_entry_request(issuer_did, rev_reg_id, REVOC_REG_TYPE, rev_reg_entry_json).unwrap();
ledger::sign_and_submit_request(self.pool_handle, issuer_wallet_handle, issuer_did, &rev_reg_entry_request).unwrap()
}
pub fn get_revoc_reg_delta(&self, did: Option<&str>, revoc_reg_def_id: &str, from: Option<u64>, to: u64) -> (String, String, u64) /* rev_reg_id, revoc_reg_delta_json, timestamp */ {
let get_rev_reg_delta_request = ledger::build_get_revoc_reg_delta_request(did, revoc_reg_def_id, from, to).unwrap();
let get_rev_reg_delta_response = ledger::submit_request(self.pool_handle, &get_rev_reg_delta_request).unwrap();
ledger::parse_get_revoc_reg_delta_response(&get_rev_reg_delta_response).unwrap()
}
}
impl Issuer {
pub fn new(pool: &Pool) -> Issuer {
let (wallet_handle, wallet_config) = wallet::create_and_open_default_wallet(format!("wallet_for_pool_{}", pool.pool_handle).borrow()).unwrap();
Issuer {
// Issuer creates wallet, gets wallet handle
issuer_wallet_handle: wallet_handle,
issuer_wallet_config: wallet_config,
// Issuer create DID
issuer_did: did::create_store_and_publish_my_did_from_trustee(wallet_handle, pool.pool_handle).unwrap().0,
schema_id: String::new(),
rev_reg_id: String::new(),
cred_def_id: String::new(),
issuance_type: String::new(),
tails_writer_config: anoncreds::tails_writer_config()
}
}
// creates schema , credential definition and revocation registry
pub fn create_initial_ledger_state(&mut self, pool: &Pool, revoc_registry_config: &str)
{
let revoc_reg_config_value: Value = serde_json::from_str(revoc_registry_config).unwrap();
self.issuance_type = String::from(revoc_reg_config_value.as_object().unwrap().get("issuance_type").unwrap().as_str().unwrap());
// Issuer creates Schema
let (schema_id, schema_json) = anoncreds::issuer_create_schema(&self.issuer_did,
GVT_SCHEMA_NAME,
SCHEMA_VERSION,
GVT_SCHEMA_ATTRIBUTES).unwrap();
// !!IMPORTANT!!
// It is important Post and Get Schema from Ledger and parse it to get the correct Schema JSON and correspondent it seq_no in Ledger
// After that we can create CredentialDefinition for received Schema(not for result of indy_issuer_create_schema)
let _schema_response = pool.submit_schema(&self.issuer_did, self.issuer_wallet_handle, &schema_json);
::std::thread::sleep(::std::time::Duration::from_secs(1));
// Issuer gets Schema from Ledger
let (_, schema_json) = pool.get_schema(Some(&self.issuer_did), &schema_id);
self.schema_id = schema_id;
// Issuer creates CredentialDefinition
let (cred_def_id, cred_def_json) = anoncreds::issuer_create_credential_definition(self.issuer_wallet_handle,
&self.issuer_did,
&schema_json,
TAG_1,
None,
Some(&anoncreds::revocation_cred_def_config())).unwrap();
// Issuer post CredentialDefinition to Ledger
pool.submit_cred_def(&self.issuer_did, self.issuer_wallet_handle, &cred_def_json);
self.cred_def_id = cred_def_id;
// Issuer creates RevocationRegistry
let tails_writer_handle = blob_storage::open_writer("default", &self.tails_writer_config).unwrap();
let (rev_reg_id, rev_reg_def_json, rev_reg_entry_json) =
anoncreds::issuer_create_and_store_revoc_reg(self.issuer_wallet_handle,
&self.issuer_did,
None,
TAG_1,
&self.cred_def_id,
revoc_registry_config,
tails_writer_handle).unwrap();
// Issuer posts RevocationRegistryDefinition to Ledger
pool.submit_revoc_reg_def(&self.issuer_did, self.issuer_wallet_handle, &rev_reg_def_json);
self.rev_reg_id = rev_reg_id;
// Issuer posts RevocationRegistryEntry to Ledger
pool.submit_revoc_reg_entry(&self.issuer_did, self.issuer_wallet_handle, &self.rev_reg_id, &rev_reg_entry_json);
}
pub fn make_credential_offer(&self) -> String
{
let cred_offer_json = anoncreds::issuer_create_credential_offer(self.issuer_wallet_handle, &self.cred_def_id).unwrap();
cred_offer_json
}
pub fn issue_credential(&self, pool: &Pool, cred_offer_json: &str, cred_req_json: &str, cred_values_json: &str) -> (String, String, Option<String>)
{
// Issuer creates TailsReader
let blob_storage_reader_handle = blob_storage::open_reader(TYPE, &self.tails_writer_config).unwrap();
// Issuer creates Credential
// NOte that the function returns revoc_reg_delta_json as None in case
// the revocation registry was created with the strategy ISSUANCE_BY_DEFAULT
let (cred_json, cred_rev_id, revoc_reg_delta_json) = anoncreds::issuer_create_credential(self.issuer_wallet_handle,
&cred_offer_json,
&cred_req_json,
cred_values_json,
Some(&self.rev_reg_id),
Some(blob_storage_reader_handle)).unwrap();
// Issuer does not have to post rev_reg_delta to ledger in case of the strategy ISSUANCE_BY_DEFAULT
if &self.issuance_type == "ISSUANCE_ON_DEMAND" {
pool.submit_revoc_reg_entry(&self.issuer_did, self.issuer_wallet_handle, &self.rev_reg_id, &revoc_reg_delta_json.clone().unwrap());
}
(cred_json, cred_rev_id.unwrap(), revoc_reg_delta_json)
}
pub fn revoke_credential(&self, pool: &Pool, cred_rev_id: &str) -> String
{
// Issuer creates TailsReader
let blob_storage_reader_handle = blob_storage::open_reader(TYPE, &self.tails_writer_config).unwrap();
// Issuer revokes cred_info
let rev_reg_delta_json = anoncreds::issuer_revoke_credential(self.issuer_wallet_handle, blob_storage_reader_handle, &self.rev_reg_id, &cred_rev_id).unwrap();
// Issuer post RevocationRegistryDelta to Ledger
pool.submit_revoc_reg_entry(&self.issuer_did, self.issuer_wallet_handle, &self.rev_reg_id, &rev_reg_delta_json);
rev_reg_delta_json
}
pub fn close(&self)
{
wallet::close_and_delete_wallet(self.issuer_wallet_handle, &self.issuer_wallet_config).unwrap();
}
}
impl Prover
{
pub fn new(master_secret_id: Option<&str>) -> Prover
{
// Prover creates wallet, gets wallet handle
let (prover_wallet_handle, prover_wallet_config) = wallet::create_and_open_default_wallet("interactions_prover").unwrap();
// Prover create DID
let (prover_did, prover_verkey) = did::create_my_did(prover_wallet_handle, "{}").unwrap();
// Prover creates Master Secret
let master_secret_id = master_secret_id.unwrap_or(COMMON_MASTER_SECRET);
anoncreds::prover_create_master_secret(prover_wallet_handle, master_secret_id).unwrap();
Prover {
wallet_handle: prover_wallet_handle,
wallet_config: prover_wallet_config,
did: prover_did.clone(),
verkey: prover_verkey.clone(),
master_secret_id: String::from(master_secret_id),
cred_def_id: None,
cred_req_metadata_json: None
}
}
pub fn make_credential_request(&mut self, pool: &Pool, cred_offer_json: &str) -> String
{
// Prover gets CredentialDefinition from Ledger
let cred_offer: CredentialOffer = serde_json::from_str(&cred_offer_json).unwrap();
let (cred_def_id, cred_def_json) = pool.get_cred_def(Some(&self.did), &cred_offer.cred_def_id.0);
self.cred_def_id = Some(cred_def_id);
// Prover creates Credential Request
let (cred_req_json, cred_req_metadata_json) = anoncreds::prover_create_credential_req(self.wallet_handle,
&self.did,
&cred_offer_json,
&cred_def_json,
&self.master_secret_id).unwrap();
self.cred_req_metadata_json = Some(cred_req_metadata_json);
cred_req_json
}
pub fn store_credentials(&self, pool: &Pool, cred_json: &str, cred_id: &str)
{
let credential: Credential = serde_json::from_str(&cred_json).unwrap();
// Prover gets CredentialDefinition from Ledger
let (_, cred_def_json) = pool.get_cred_def(Some(&self.did), &self.cred_def_id.clone().unwrap());
// Prover gets RevocationRegistryDefinition
let (_, revoc_reg_def_json) = pool.get_revoc_reg_def(None, &credential.rev_reg_id.unwrap().0);
// Prover stores received Credential
anoncreds::prover_store_credential(self.wallet_handle,
cred_id,
&self.cred_req_metadata_json.clone().unwrap(),
&cred_json,
&cred_def_json,
Some(&revoc_reg_def_json)).unwrap();
}
pub fn make_proof(&self, pool: &Pool, proof_request: &str, attr1_referent: &str, from: Option<u64>, to: u64) -> String
{
// Prover searches Credentials for Proof Request
let search_handle = anoncreds::prover_search_credentials_for_proof_req(self.wallet_handle, &proof_request, None).unwrap();
let credentials_list = anoncreds::prover_fetch_next_credentials_for_proof_req(search_handle, attr1_referent, 1).unwrap();
let credentials_list_value: Value = serde_json::from_str(&credentials_list).unwrap();
// extract first result of the search as Value
let credentials_first = &credentials_list_value.as_array().unwrap()[0];
// extract cred_info as Value from the result
let cred_info_value = credentials_first.as_object().unwrap().get("cred_info").unwrap();
let cred_info: CredentialInfo = serde_json::from_value(cred_info_value.clone()).unwrap();
let _ = anoncreds::prover_close_credentials_search_for_proof_req(search_handle).unwrap();
let schema_id = cred_info.schema_id;
let cred_def_id = cred_info.cred_def_id;
let cred_rev_id = cred_info.cred_rev_id.clone().unwrap();
let rev_reg_id = cred_info.rev_reg_id.clone().unwrap();
// Prover gets Schema from Ledger
let (_, schema_json) = pool.get_schema(None, &schema_id.0);
// Prover gets CredentialDefinition from Ledger
let (_, cred_def_json) = pool.get_cred_def(Some(&self.did), &cred_def_id.0);
// Prover gets RevocationRegistryDefinition
let (_, revoc_reg_def_json) = pool.get_revoc_reg_def(None, &rev_reg_id.0);
// Prover gets RevocationRegistryDelta from Ledger
let (_, revoc_reg_delta_json, timestamp) = pool.get_revoc_reg_delta(None, &rev_reg_id.0, from, to);
// Prover creates RevocationState
let prover_blob_storage_reader_handle = blob_storage::open_reader(TYPE, &anoncreds::tails_writer_config()).unwrap();
let rev_state_json = anoncreds::create_revocation_state(prover_blob_storage_reader_handle,
&revoc_reg_def_json,
&revoc_reg_delta_json,
timestamp,
&cred_rev_id).unwrap();
let proof_request_value: Value = serde_json::from_str(proof_request).unwrap();
let requested_predicates = !proof_request_value.as_object().unwrap().get("requested_predicates").unwrap().as_object().unwrap().is_empty();
// Prover creates Proof
let requested_credentials_json = if requested_predicates
{
json!({
"self_attested_attributes": json!({}),
"requested_attributes": json!({
attr1_referent.clone(): json!({ "cred_id": cred_info.referent, "timestamp": timestamp, "revealed":true })
}),
"requested_predicates": json!({
"predicate1_referent": json!({ "cred_id": cred_info.referent, "timestamp": timestamp })
})
}).to_string()
} else {
json!({
"self_attested_attributes": json!({}),
"requested_attributes": json!({
"attr1_referent": json!({ "cred_id": cred_info.referent, "timestamp": timestamp, "revealed":true })
}),
"requested_predicates": json!({})
}).to_string()
};
let schemas_json = json!({
schema_id.0: serde_json::from_str::<Schema>(&schema_json).unwrap()
}).to_string();
let cred_defs_json = json!({
cred_def_id.0: serde_json::from_str::<CredentialDefinition>(&cred_def_json).unwrap()
}).to_string();
let rev_states_json = json!({
rev_reg_id.0: json!({
timestamp.to_string(): serde_json::from_str::<RevocationState>(&rev_state_json).unwrap()
})
}).to_string();
let proof_json = anoncreds::prover_create_proof(self.wallet_handle,
&proof_request,
&requested_credentials_json,
&self.master_secret_id,
&schemas_json,
&cred_defs_json,
&rev_states_json).unwrap();
proof_json
}
pub fn close(&self)
{
wallet::close_and_delete_wallet(self.wallet_handle, &self.wallet_config).unwrap();
}
}
impl Verifier {
pub fn new(proof_request: &String) -> Verifier {
Verifier {
proof_request: proof_request.clone()
}
}
pub fn verify_revealed(&self, proof_json: &str, attr_name: &str, attr_value: &str)
{
let proof: Proof = serde_json::from_str(&proof_json).unwrap();
assert_eq!(attr_value, proof.requested_proof.revealed_attrs.get(attr_name).unwrap().raw)
}
pub fn verify(&self, pool: &Pool, proof_json: &str) -> bool
{
let proof: Proof = serde_json::from_str(&proof_json).unwrap();
assert_eq!(1, proof.identifiers.len());
let identifier = proof.identifiers[0].clone();
// Verifier gets Schema from Ledger
let (_, schema_json) = pool.get_schema(Some(DID_MY1), &identifier.schema_id.0);
// Verifier gets CredentialDefinition from Ledger
let (_, cred_def_json) = pool.get_cred_def(Some(DID_MY1), &identifier.cred_def_id.0);
// Verifier gets RevocationRegistryDefinition from Ledger
let (_, revoc_reg_def_json) = pool.get_revoc_reg_def(Some(DID_MY1), &identifier.rev_reg_id.clone().unwrap().0);
// Verifier gets RevocationRegistry from Ledger
let (_, rev_reg_json, timestamp) =
pool.get_revoc_reg_delta(Some(DID_MY1), &identifier.rev_reg_id.clone().unwrap().0, None, identifier.timestamp.unwrap());
let schemas_json = json!({
identifier.schema_id.0.clone(): serde_json::from_str::<Schema>(&schema_json).unwrap()
}).to_string();
let cred_defs_json = json!({
identifier.cred_def_id.0.clone(): serde_json::from_str::<CredentialDefinition>(&cred_def_json).unwrap()
}).to_string();
let rev_reg_defs_json = json!({
identifier.rev_reg_id.clone().unwrap().0.clone(): serde_json::from_str::<RevocationRegistryDefinition>(&revoc_reg_def_json).unwrap()
}).to_string();
let rev_regs_json = json!({
identifier.rev_reg_id.clone().unwrap().0.clone(): json!({
timestamp.to_string(): serde_json::from_str::<RevocationRegistry>(&rev_reg_json).unwrap()
})
}).to_string();
let valid = anoncreds::verifier_verify_proof(&self.proof_request,
proof_json,
&schemas_json,
&cred_defs_json,
&rev_reg_defs_json,
&rev_regs_json).unwrap();
valid
}
}
#[cfg(feature = "revocation_tests")]
#[ignore]
#[test]
fn anoncreds_revocation_interaction_test_issuance_by_demand() {
anoncreds_revocation_interaction_test_one_prover(r#"{"max_cred_num":5, "issuance_type":"ISSUANCE_ON_DEMAND"}"#);
}
#[cfg(feature = "revocation_tests")]
#[cfg(any(feature = "force_full_interaction_tests", not(target_os = "android")))]
#[cfg(not(feature = "only_high_cases"))]
#[test]
fn anoncreds_revocation_interaction_test_issuance_by_default()
{
anoncreds_revocation_interaction_test_one_prover(r#"{"max_cred_num":5, "issuance_type":"ISSUANCE_BY_DEFAULT"}"#);
}
// the common function for two previous tests
fn anoncreds_revocation_interaction_test_one_prover(revocation_registry_config: &str)
{
let setup = Setup::empty();
let pool = Pool::new(&setup.name);
let mut issuer = Issuer::new(&pool);
let mut prover = Prover::new(None);
// Issuer publish Prover DID
pool.submit_nym(&issuer.issuer_did, issuer.issuer_wallet_handle, &prover.did, Some(&prover.verkey));
// ISSUER post to Ledger Schema, CredentialDefinition, RevocationRegistry
issuer.create_initial_ledger_state(&pool, revocation_registry_config);
///////////////////////////////////////////////////////////////////////////////////////////////////////
// Issuance Credential for Prover
// Issuer creates Credential Offer
let cred_offer_json = issuer.make_credential_offer();
// Prover makes credential request
let cred_req_json = prover.make_credential_request(&pool, &cred_offer_json);
// Issuer issues credential
let (cred_json, cred_rev_id, _revoc_reg_delta_json) = issuer.issue_credential(&pool, &cred_offer_json, &cred_req_json, &anoncreds::gvt_credential_values_json());
// Prover stores credentials
prover.store_credentials(&pool, &cred_json, CREDENTIAL1_ID);
// Basic check
let credentials = anoncreds::prover_get_credentials(prover.wallet_handle, &json!({"schema_name": GVT_SCHEMA_NAME}).to_string()).unwrap();
let credentials: Vec<serde_json::Value> = serde_json::from_str(&credentials).unwrap();
assert_eq!(credentials.len(), 1);
/////////////////////////////////////////////////////////////////////////////////////////////////
// Verifying Prover's Credential
thread::sleep(std::time::Duration::from_secs(1));
let to = time::get_time().sec as u64;
let proof_request = json!({
"nonce":"123432421212",
"name":"proof_req_1",
"version":"0.1",
"requested_attributes": json!({
"attr1_referent": json!({
"name":"name"
})
}),
"requested_predicates": json!({
"predicate1_referent": json!({ "name":"age", "p_type":">=", "p_value":18 })
}),
"non_revoked": json!({ "to": to.clone() })
}).to_string();
let verifier = Verifier::new(&proof_request);
let proof_json = prover.make_proof(&pool, &proof_request, "attr1_referent", None, to);
// Verifier verifies revealed attribute
verifier.verify_revealed(&proof_json, "attr1_referent", "Alex");
let valid = verifier.verify(&pool, &proof_json);
assert!(valid);
/////////////////////////////////////////////////////////////////////////////////////////
// Issuer revokes cred_rev_id
let _rev_reg_delta_json = issuer.revoke_credential(&pool, &cred_rev_id);
// Verifying Prover Credential after Revocation
thread::sleep(std::time::Duration::from_secs(1));
let from = to;
let to = time::get_time().sec as u64;
let proof_json = prover.make_proof(&pool, &proof_request, "attr1_referent", Some(from), to);
let valid = verifier.verify(&pool, &proof_json);
assert!(!valid);
issuer.close();
prover.close();
pool.close();
}
#[cfg(not(feature = "only_high_cases"))]
fn multi_steps_create_revocation_credential(pool: &Pool, issuer: &Issuer, prover: &mut Prover, cred_values_json: &str, cred_id: &str) -> (String, Option<String>)
{
// Issuer creates Credential Offer
let cred_offer_json = issuer.make_credential_offer();
// Prover makes credential request
let cred_req_json = prover.make_credential_request(&pool, &cred_offer_json);
// Issuer issues credential
let (cred_json, cred_rev_id, revoc_reg_delta_json) = issuer.issue_credential(&pool, &cred_offer_json, &cred_req_json, cred_values_json);
// Prover stores credentials
prover.store_credentials(&pool, &cred_json, cred_id);
(cred_rev_id, revoc_reg_delta_json)
}
#[cfg(feature = "revocation_tests")]
#[cfg(any(feature = "force_full_interaction_tests", not(target_os = "android")))]
#[cfg(not(feature = "only_high_cases"))]
#[test]
fn anoncreds_revocation_interaction_test_issuance_by_demand_three_credentials_post_entry_three_times_proving_first() {
let setup = Setup::empty();
let pool = Pool::new(&setup.name);
let mut issuer = Issuer::new(&pool);
let mut prover1 = Prover::new(Some("prover1_master_secret"));
let mut prover2 = Prover::new(Some("prover2_master_secret"));
let mut prover3 = Prover::new(Some("prover3_master_secret"));
// ISSUER post to Ledger Schema, CredentialDefinition, RevocationRegistry
issuer.create_initial_ledger_state(&pool, r#"{"max_cred_num":5, "issuance_type":"ISSUANCE_ON_DEMAND"}"#);
/*ISSUANCE CREDENTIAL FOR PROVER1*/
let (_prover1_cred_rev_id, _prover1_revoc_reg_delta1_json) =
multi_steps_create_revocation_credential(&pool, &issuer, &mut prover1, &anoncreds::gvt_credential_values_json(), CREDENTIAL1_ID);
/*ISSUANCE CREDENTIAL FOR PROVER2*/
let (_prover2_cred_rev_id, _prover2_revoc_reg_delta1_json) =
multi_steps_create_revocation_credential(&pool, &issuer, &mut prover2, &anoncreds::gvt2_credential_values_json(), CREDENTIAL2_ID);
/*ISSUANCE CREDENTIAL FOR PROVER3*/
let (_prover3_cred_rev_id, _prover3_revoc_reg_delta1_json) =
multi_steps_create_revocation_credential(&pool, &issuer, &mut prover3, &anoncreds::gvt3_credential_values_json(), CREDENTIAL3_ID);
// Verifying Prover1 Credential
thread::sleep(std::time::Duration::from_secs(1));
let to = time::get_time().sec as u64;
let proof_request = json!({
"nonce":"123432421212",
"name":"proof_req_1",
"version":"0.1",
"requested_attributes": json!({
"attr1_referent": json!({
"name":"name"
})
}),
"requested_predicates": json!({}),
"non_revoked": json!({ "to": to.clone() })
}).to_string();
let verifier = Verifier::new(&proof_request);
let proof_json = prover1.make_proof(&pool, &proof_request, "attr1_referent", None, to);
// Verifier verifies revealed attribute
verifier.verify_revealed(&proof_json, "attr1_referent", "Alex");
let valid = verifier.verify(&pool, &proof_json);
assert!(valid);
issuer.close();
prover1.close();
prover2.close();
prover3.close();
pool.close();
}
#[cfg(feature = "revocation_tests")]
#[cfg(any(feature = "force_full_interaction_tests", not(target_os = "android")))]
#[cfg(not(feature = "only_high_cases"))]
#[test]
fn anoncreds_revocation_interaction_test_issuance_by_demand_three_credentials_post_common_entry_proving_all() {
let setup = Setup::empty();
let pool = Pool::new(&setup.name);
let mut issuer = Issuer::new(&pool);
let mut prover1 = Prover::new(Some("prover1_master_secret"));
let mut prover2 = Prover::new(Some("prover2_master_secret"));
let mut prover3 = Prover::new(Some("prover3_master_secret"));
// ISSUER post to Ledger Schema, CredentialDefinition, RevocationRegistry
issuer.create_initial_ledger_state(&pool, r#"{"max_cred_num":5, "issuance_type":"ISSUANCE_ON_DEMAND"}"#);
/*ISSUANCE CREDENTIAL FOR PROVER1*/
let (_prover1_cred_rev_id, revoc_reg_delta1_json) = multi_steps_create_revocation_credential(&pool, &issuer, &mut prover1, &anoncreds::gvt_credential_values_json(), CREDENTIAL1_ID);
let revoc_reg_delta1_json = revoc_reg_delta1_json.unwrap();
/*ISSUANCE CREDENTIAL FOR PROVER2*/
let (_prover2_cred_rev_id, revoc_reg_delta2_json) = multi_steps_create_revocation_credential(&pool, &issuer, &mut prover2, &anoncreds::gvt2_credential_values_json(), CREDENTIAL2_ID);
let revoc_reg_delta2_json = revoc_reg_delta2_json.unwrap();
// Issuer merge Revocation Registry Deltas
let revoc_reg_delta_json = anoncreds::issuer_merge_revocation_registry_deltas(&revoc_reg_delta1_json, &revoc_reg_delta2_json).unwrap();
/*ISSUANCE CREDENTIAL FOR PROVER3*/
let (_prover3_cred_rev_id, revoc_reg_delta3_json) = multi_steps_create_revocation_credential(&pool, &issuer, &mut prover3, &anoncreds::gvt3_credential_values_json(), CREDENTIAL3_ID);
let revoc_reg_delta3_json = revoc_reg_delta3_json.unwrap();
// Issuer merge Revocation Registry Deltas
let _revoc_reg_delta_json = anoncreds::issuer_merge_revocation_registry_deltas(&revoc_reg_delta_json, &revoc_reg_delta3_json).unwrap();
// TODO: test if the issuer can submit one delta instead of multiple deltas consequently
// let rev_reg_entry_request =
// ledger::build_revoc_reg_entry_request(&issuer_did, &rev_reg_id, REVOC_REG_TYPE, &revoc_reg_delta_json).unwrap();
// ledger::sign_and_submit_request(pool_handle, issuer_wallet_handle, &issuer_did, &rev_reg_entry_request).unwrap();
// Verifying Prover1 Credential
thread::sleep(std::time::Duration::from_secs(1));
let to = time::get_time().sec as u64;
let proof_request = json!({
"nonce":"123432421212",
"name":"proof_req_1",
"version":"0.1",
"requested_attributes": json!({
"attr1_referent": json!({
"name":"name"
})
}),
"requested_predicates": json!({}),
"non_revoked": json!({ "to": to.clone() })
}).to_string();
let verifier = Verifier::new(&proof_request);
let proof_json = prover1.make_proof(&pool, &proof_request, "attr1_referent", None, to);
verifier.verify_revealed(&proof_json, "attr1_referent", "Alex");
let valid = verifier.verify(&pool, &proof_json);
assert!(valid);
// Verifying Prover2 Credential
let proof_json = prover2.make_proof(&pool, &proof_request, "attr1_referent", None, to);
// Verifier verifies proof from Prover2
verifier.verify_revealed(&proof_json, "attr1_referent", "Alexander");
let valid = verifier.verify(&pool, &proof_json);
assert!(valid);
// Verifying Prover3 Credential
let proof_json = prover3.make_proof(&pool, &proof_request, "attr1_referent", None, to);
// Verifier verifies proof from Prover2
verifier.verify_revealed(&proof_json, "attr1_referent", "Artem");
let valid = verifier.verify(&pool, &proof_json);
assert!(valid);
issuer.close();
prover1.close();
prover2.close();
prover3.close();
pool.close();
}
#[cfg(feature = "revocation_tests")]
#[ignore]
#[test]
fn anoncreds_revocation_interaction_test_issuance_by_demand_fully_qualified_did() {
let setup = Setup::empty();
let pool = Pool::new(&setup.name);
let (wallet_handle, wallet_config) = wallet::create_and_open_default_wallet(format!("wallet_for_pool_{}", pool.pool_handle).borrow()).unwrap();
let mut issuer = Issuer {
// Issuer creates wallet, gets wallet handle
issuer_wallet_handle: wallet_handle,
// Issuer create DID
issuer_wallet_config: wallet_config,
issuer_did: did::create_store_and_publish_my_did_from_trustee_v1(wallet_handle, pool.pool_handle).unwrap().0,
schema_id: String::new(),
rev_reg_id: String::new(),
cred_def_id: String::new(),
issuance_type: String::new(),
tails_writer_config: anoncreds::tails_writer_config(),
};
// Prover creates wallet, gets wallet handle
let (prover_wallet_handle, prover_wallet_config) = wallet::create_and_open_default_wallet("interactions_prover").unwrap();
// Prover create DID
let my_did_json = json!({"method_name": "sov"}).to_string();
let (prover_did, prover_verkey) = did::create_my_did(prover_wallet_handle, &my_did_json).unwrap();
// Prover creates Master Secret
let master_secret_id = COMMON_MASTER_SECRET;
anoncreds::prover_create_master_secret(prover_wallet_handle, COMMON_MASTER_SECRET).unwrap();
let mut prover = Prover {
wallet_handle: prover_wallet_handle,
wallet_config: prover_wallet_config,
did: prover_did.clone(),
verkey: prover_verkey.clone(),
master_secret_id: String::from(master_secret_id),
cred_def_id: None,
cred_req_metadata_json: None,
};
// Issuer publish Prover DID
pool.submit_nym(&issuer.issuer_did, issuer.issuer_wallet_handle, &prover.did, Some(&prover.verkey));
// ISSUER post to Ledger Schema, CredentialDefinition, RevocationRegistry
issuer.create_initial_ledger_state(&pool, r#"{"max_cred_num":5, "issuance_type":"ISSUANCE_ON_DEMAND"}"#);
///////////////////////////////////////////////////////////////////////////////////////////////////////
// Issuance Credential for Prover
// Issuer creates Credential Offer
let cred_offer_json = issuer.make_credential_offer();
// Prover makes credential request
let cred_req_json = prover.make_credential_request(&pool, &cred_offer_json);
// Issuer issues credential
let (cred_json, _, _) = issuer.issue_credential(&pool, &cred_offer_json, &cred_req_json, &anoncreds::gvt_credential_values_json());
// Prover stores credentials
prover.store_credentials(&pool, &cred_json, CREDENTIAL1_ID);
// Basic check
let credentials = anoncreds::prover_get_credentials(prover.wallet_handle, &json!({"schema_name": GVT_SCHEMA_NAME}).to_string()).unwrap();
let credentials: Vec<serde_json::Value> = serde_json::from_str(&credentials).unwrap();
assert_eq!(credentials.len(), 1);
/////////////////////////////////////////////////////////////////////////////////////////////////
// Verifying Prover's Credential
thread::sleep(std::time::Duration::from_secs(1));
let to = time::get_time().sec as u64;
// Verify proof in a short identifiers
let proof_request = json!({
"nonce":"123432421212",
"name":"proof_req_1",
"version":"0.1",
"requested_attributes": json!({
"attr1_referent": json!({
"name":"name",
"restrictions": {
"cred_def_id": issuer.cred_def_id,
}
})
}),
"requested_predicates": json!({
"predicate1_referent": json!({ "name":"age", "p_type":">=", "p_value":18 })
}),
"non_revoked": json!({ "to": to.clone() })
}).to_string();
let proof_request = anoncreds::to_unqualified(&proof_request).unwrap();
let verifier = Verifier::new(&proof_request);
let proof_json = prover.make_proof(&pool, &proof_request, "attr1_referent", None, to);
// Verifier verifies revealed attribute
verifier.verify_revealed(&proof_json, "attr1_referent", "Alex");
let valid = verifier.verify(&pool, &proof_json);
assert!(valid);
// Verify proof in a fully qualified identifiers
let proof_request = json!({
"nonce":"123432421212",
"name":"proof_req_1",
"version":"0.1",
"requested_attributes": json!({
"attr1_referent": json!({
"name":"name",
"restrictions": {
"cred_def_id": issuer.cred_def_id.clone()
}
})
}),
"requested_predicates": json!({
"predicate1_referent": json!({ "name":"age", "p_type":">=", "p_value":18 })
}),
"non_revoked": json!({ "to": to.clone() }),
"ver": "2.0"
}).to_string();
let verifier = Verifier::new(&proof_request);
let proof_json = prover.make_proof(&pool, &proof_request, "attr1_referent", None, to);
// Verifier verifies revealed attribute
verifier.verify_revealed(&proof_json, "attr1_referent", "Alex");
let valid = verifier.verify(&pool, &proof_json);
assert!(valid);
issuer.close();
prover.close();
pool.close();
}
#[cfg(feature = "revocation_tests")]
#[ignore]
#[test]
fn anoncreds_revocation_interaction_test_issuance_by_demand_fully_qualified_issuer_unqualified_prover() {
let setup = Setup::empty();
let pool = Pool::new(&setup.name);
let (wallet_handle, wallet_config) = wallet::create_and_open_default_wallet(format!("wallet_for_pool_{}", pool.pool_handle).borrow()).unwrap();
let mut issuer = Issuer {
// Issuer creates wallet, gets wallet handle
issuer_wallet_handle: wallet_handle,
// Issuer create DID
issuer_wallet_config: wallet_config,
issuer_did: did::create_store_and_publish_my_did_from_trustee_v1(wallet_handle, pool.pool_handle).unwrap().0,
schema_id: String::new(),
rev_reg_id: String::new(),
cred_def_id: String::new(),
issuance_type: String::new(),
tails_writer_config: anoncreds::tails_writer_config(),
};
// Prover creates wallet, gets wallet handle
let (prover_wallet_handle, prover_wallet_config) = wallet::create_and_open_default_wallet("interactions_prover").unwrap();
// Prover create DID
let (prover_did, prover_verkey) = did::create_my_did(prover_wallet_handle, "{}").unwrap();
// Prover creates Master Secret
let master_secret_id = COMMON_MASTER_SECRET;
anoncreds::prover_create_master_secret(prover_wallet_handle, COMMON_MASTER_SECRET).unwrap();
let mut prover = Prover {
wallet_handle: prover_wallet_handle,
wallet_config: prover_wallet_config,