forked from hyperledger/anoncreds-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiple-credentials.rs
301 lines (272 loc) · 9.36 KB
/
multiple-credentials.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
use std::collections::HashMap;
use anoncreds::{
data_types::{
pres_request::{NonRevokedInterval, PresentationRequestPayload},
rev_reg_def::RevocationRegistryDefinitionId,
schema::SchemaId,
},
types::PresentationRequest,
verifier,
};
use serde_json::json;
mod utils;
pub static ISSUER_ID: &str = "mock:issuer_id/path&q=bar";
pub static PROVER_ID: &str = "mock:prover_id/path&q=bar";
pub static REV_IDX_1: u32 = 9;
pub static REV_IDX_2: u32 = 9;
pub static MAX_CRED_NUM: u32 = 10;
pub static TF_PATH: &str = "../.tmp";
// NonRevoked Interval consts
const GLOBAL_FROM: u64 = 5;
const GLOBAL_TO: u64 = 25;
const LOCAL_FROM: u64 = 10;
const OVERRIDE_LOCAL_FROM: u64 = 8;
const LOCAL_TO: u64 = 20;
const TS_WITHIN_LOCAL_OVERRIDE: u64 = 9;
const SCHEMA_ID_1: &str = "mock:uri:schema1";
const SCHEMA_ID_2: &str = "mock:uri:schema2";
const SCHEMA_1: &str = r#"{"name":"gvt","version":"1.0","attrNames":["name","sex","age","height"],"issuerId":"mock:issuer_id/path&q=bar"}"#;
const SCHEMA_2: &str = r#"{"name":"hogwarts","version":"1.0","attrNames":["wand","house","year"],"issuerId":"mock:issuer_id/path&q=hogwarts"}"#;
static CRED_DEF_ID_1: &'static str = "mock:uri:1";
static CRED_DEF_ID_2: &'static str = "mock:uri:2";
static REV_REG_ID_1: &'static str = "mock:uri:revregid1";
static REV_REG_ID_2: &'static str = "mock:uri:revregid2";
fn create_request(input: &ReqInput) -> PresentationRequest {
let nonce = verifier::generate_nonce().unwrap();
let json = json!({
"nonce": nonce,
"name":input.req_name ,
"version":"0.1",
"requested_attributes":{
"attr1_referent":{
"name":"name",
"issuer_id": input.issuer,
},
"attr2_referent":{
"name":"sex"
},
"attr3_referent":{"name":"phone"},
"attr4_referent":{
"names": ["height"],
},
"attr5_referent": {"names": ["wand", "house", "year"]},
},
"requested_predicates":{
"predicate1_referent":{"name":"age","p_type":">=","p_value":18}
},
});
let mut presentation: PresentationRequestPayload = serde_json::from_value(json).unwrap();
presentation.non_revoked = input.global_nonrevoke.clone();
for ni in input.attr_nonrevoke.iter() {
let at = presentation.requested_attributes.get_mut(ni.0).unwrap();
at.non_revoked = Some(ni.1.clone());
}
for ni in input.pred_nonrevoke.iter() {
let at = presentation.requested_predicates.get_mut(ni.0).unwrap();
at.non_revoked = Some(ni.1.clone());
}
log::info!("\n Request: {:?}", presentation);
PresentationRequest::PresentationRequestV1(presentation)
}
fn create_issuer_data<'a>() -> utils::IssuerValues<'a> {
// These are what the issuer knows
// Credential 1 is revocable
// Credential 2 is non-revocable
// There are 2 definitions, issued by 1 issuer
let issuer1_creds: utils::IssuerValues = HashMap::from([
(
CRED_DEF_ID_1,
(
SCHEMA_ID_1,
HashMap::from([
("sex", "male"),
("name", "Alex"),
("height", "175"),
("age", "28"),
]),
true,
REV_REG_ID_1,
REV_IDX_1,
),
),
(
CRED_DEF_ID_2,
(
SCHEMA_ID_2,
HashMap::from([
("wand", "dragon-heart-string"),
("house", "Hufflepuff"),
("year", "1990"),
]),
false,
REV_REG_ID_2,
REV_IDX_2,
),
),
]);
issuer1_creds
}
pub struct ReqInput<'a> {
pub req_name: &'a str,
pub issuer: &'a str,
pub global_nonrevoke: Option<NonRevokedInterval>,
pub attr_nonrevoke: Vec<(&'a str, NonRevokedInterval)>,
pub pred_nonrevoke: Vec<(&'a str, NonRevokedInterval)>,
}
fn test_requests_generate<'a>() -> Vec<ReqInput<'a>> {
let r0 = ReqInput {
req_name: "global_rev",
issuer: ISSUER_ID,
global_nonrevoke: Some(NonRevokedInterval::new(Some(GLOBAL_FROM), Some(GLOBAL_TO))),
attr_nonrevoke: vec![],
pred_nonrevoke: vec![],
};
let r1 = ReqInput {
req_name: "local_rev",
issuer: ISSUER_ID,
global_nonrevoke: None,
attr_nonrevoke: vec![
(
"attr2_referent",
NonRevokedInterval::new(Some(LOCAL_FROM), Some(LOCAL_TO)),
),
(
"attr5_referent",
NonRevokedInterval::new(Some(LOCAL_FROM), Some(LOCAL_TO)),
),
],
pred_nonrevoke: vec![],
};
let r2 = ReqInput {
req_name: "both_rev_attr",
issuer: ISSUER_ID,
global_nonrevoke: Some(NonRevokedInterval::new(Some(GLOBAL_FROM), Some(GLOBAL_TO))),
attr_nonrevoke: vec![
(
"attr2_referent",
NonRevokedInterval::new(Some(LOCAL_FROM), Some(LOCAL_TO)),
),
(
"attr5_referent",
NonRevokedInterval::new(Some(LOCAL_FROM), Some(LOCAL_TO)),
),
],
pred_nonrevoke: vec![],
};
let r3 = ReqInput {
req_name: "both_rev_pred",
issuer: ISSUER_ID,
global_nonrevoke: Some(NonRevokedInterval::new(Some(GLOBAL_FROM), Some(GLOBAL_TO))),
attr_nonrevoke: vec![],
pred_nonrevoke: vec![(
"predicate1_referent",
NonRevokedInterval::new(Some(LOCAL_FROM), Some(LOCAL_TO)),
)],
};
let r4 = ReqInput {
req_name: "no_rev",
issuer: ISSUER_ID,
global_nonrevoke: None,
attr_nonrevoke: vec![],
pred_nonrevoke: vec![],
};
vec![r0, r1, r2, r3, r4]
}
#[test]
fn anoncreds_with_multiple_credentials_per_request() {
env_logger::init();
let mut mock = utils::Mock::new(&[ISSUER_ID], &[PROVER_ID], TF_PATH, MAX_CRED_NUM);
let issuer1_creds = create_issuer_data();
let schemas = HashMap::from([
(
SchemaId::new_unchecked(SCHEMA_ID_1),
serde_json::from_str(SCHEMA_1).unwrap(),
),
(
SchemaId::new_unchecked(SCHEMA_ID_2),
serde_json::from_str(SCHEMA_2).unwrap(),
),
]);
mock.ledger.schemas = schemas;
// These are within interval
let time_initial_rev_reg = 8u64;
let time_after_credential = TS_WITHIN_LOCAL_OVERRIDE;
let issuance_by_default = true;
// This returns Presentation Requests with following nonrevoked intervals
// [0]: Global
// [1]: Local for attributes belonging to both credentials
// [2]: Global and Local attributes , where local is more stringent
// [3]: Global and Local predeicate, where local is more stringent
// [4]: no NRP required
let reqs: Vec<PresentationRequest> = test_requests_generate()
.iter()
.map(|x| create_request(&x))
.collect();
// 1: Issuer setup (credate cred defs, rev defs(optional), cred_offers)
mock.issuer_setup(
ISSUER_ID,
PROVER_ID,
&issuer1_creds,
time_initial_rev_reg,
issuance_by_default,
);
// 2: prover requests and gets credential stored in their wallets
mock.issuer_create_credential_and_store_in_prover_wallet(
ISSUER_ID,
PROVER_ID,
&issuer1_creds,
time_initial_rev_reg,
time_after_credential,
);
// 3. Prover creates revocation states for all credentials with ledger values
// rev_states are stored in the prover wallet
mock.prover_creates_revocation_states(PROVER_ID, time_after_credential);
// 4. Prover create presentations
let prover_values: utils::ProverValues = HashMap::from([
(
CRED_DEF_ID_1,
(
vec!["attr1_referent", "attr2_referent", "attr4_referent"],
vec!["predicate1_referent"],
),
),
(CRED_DEF_ID_2, (vec!["attr5_referent"], vec![])),
]);
let self_attested = HashMap::from([("attr3_referent".to_string(), "8-800-300".to_string())]);
let mut presentations = vec![];
for req in &reqs {
let p = mock.prover_creates_presentation(
PROVER_ID,
prover_values.clone(),
self_attested.clone(),
req,
);
presentations.push(p)
}
// 5. Verifier verifies one presentation per request
//
// Without override fails
let overrides = vec![None; 5];
let results =
mock.verifer_verifies_presentations_for_requests(&presentations, &reqs, &overrides);
assert!(results[0].is_ok());
assert!(results[4].is_ok());
assert!(results[1].is_err());
assert!(results[2].is_err());
assert!(results[3].is_err());
// Create overrides for timestamps
let id = RevocationRegistryDefinitionId::new_unchecked(REV_REG_ID_1);
let override_rev1 = HashMap::from([(&id, HashMap::from([(LOCAL_FROM, OVERRIDE_LOCAL_FROM)]))]);
let overrides = vec![
None,
Some(&override_rev1),
Some(&override_rev1),
Some(&override_rev1),
None,
];
let results =
mock.verifer_verifies_presentations_for_requests(&presentations, &reqs, &overrides);
assert!(results[1].is_ok());
assert!(results[2].is_ok());
assert!(results[3].is_ok());
}