oxirs-did 0.2.4

W3C DID and Verifiable Credentials implementation with Signed RDF Graphs for OxiRS
Documentation
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
//! # Credential Store
//!
//! An in-memory W3C Verifiable Credential store providing CRUD operations,
//! revocation tracking, status resolution, and flexible filtering.
//!
//! ## Example
//!
//! ```rust
//! use oxirs_did::credential_store::{
//!     CredentialStore, CredentialSubject, VerifiableCredential,
//! };
//! use std::collections::HashMap;
//!
//! let mut store = CredentialStore::new();
//!
//! let subject = CredentialSubject {
//!     id: "did:example:alice".to_string(),
//!     claims: HashMap::from([("name".to_string(), "Alice".to_string())]),
//! };
//!
//! let vc = VerifiableCredential {
//!     id: "urn:vc:001".to_string(),
//!     types: vec!["VerifiableCredential".to_string()],
//!     issuer: "did:example:issuer".to_string(),
//!     issuance_date: "2024-01-01".to_string(),
//!     expiration_date: None,
//!     subject,
//!     proof: None,
//! };
//!
//! store.store(vc).expect("store failed");
//! assert_eq!(store.count(), 1);
//! ```

use std::collections::{HashMap, HashSet};

// ─── Domain types ─────────────────────────────────────────────────────────────

/// Credential subject: the entity the credential is about
#[derive(Debug, Clone, PartialEq)]
pub struct CredentialSubject {
    /// DID or identifier of the subject
    pub id: String,
    /// Arbitrary string-valued claims about the subject
    pub claims: HashMap<String, String>,
}

/// A W3C Verifiable Credential
#[derive(Debug, Clone)]
pub struct VerifiableCredential {
    /// Unique credential identifier (URI)
    pub id: String,
    /// Credential types (must include `"VerifiableCredential"`)
    pub types: Vec<String>,
    /// Issuer DID
    pub issuer: String,
    /// ISO 8601 issuance date string (e.g. `"2024-01-15"`)
    pub issuance_date: String,
    /// Optional ISO 8601 expiration date string
    pub expiration_date: Option<String>,
    /// The credential subject
    pub subject: CredentialSubject,
    /// Optional compact proof / JWS string
    pub proof: Option<String>,
}

/// The resolved status of a credential at a given point in time
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CredentialStatus {
    /// Credential is valid and not expired
    Valid,
    /// Credential's expiration date is in the past (compared to `current_date`)
    Expired,
    /// Credential has been explicitly revoked
    Revoked,
    /// Credential is not yet active / issuance date is in the future
    Pending,
}

/// Filter criteria for searching credentials
///
/// Each `Some(value)` field acts as an AND-filter; `None` fields are ignored.
#[derive(Debug, Clone, Default)]
pub struct CredentialFilter {
    /// Match credentials issued by this DID (exact)
    pub issuer: Option<String>,
    /// Match credentials whose subject id equals this value
    pub subject_id: Option<String>,
    /// Match credentials that include this type string
    pub credential_type: Option<String>,
    /// Match credentials with this resolved status
    pub status: Option<CredentialStatus>,
}

/// Errors returned by [`CredentialStore`] operations
#[derive(Debug)]
pub enum StoreError {
    /// A credential with the same `id` already exists in the store
    DuplicateId(String),
    /// The credential failed a basic validity check
    InvalidCredential(String),
}

impl std::fmt::Display for StoreError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            StoreError::DuplicateId(id) => write!(f, "duplicate credential id: {}", id),
            StoreError::InvalidCredential(msg) => write!(f, "invalid credential: {}", msg),
        }
    }
}

impl std::error::Error for StoreError {}

// ─── Credential store ─────────────────────────────────────────────────────────

/// In-memory store for Verifiable Credentials
pub struct CredentialStore {
    credentials: HashMap<String, VerifiableCredential>,
    revoked: HashSet<String>,
}

impl Default for CredentialStore {
    fn default() -> Self {
        Self::new()
    }
}

impl CredentialStore {
    /// Create an empty credential store.
    pub fn new() -> Self {
        Self {
            credentials: HashMap::new(),
            revoked: HashSet::new(),
        }
    }

    // ── Write operations ─────────────────────────────────────────────────

    /// Store a new credential.
    ///
    /// Returns [`StoreError::DuplicateId`] if a credential with the same `id`
    /// already exists, or [`StoreError::InvalidCredential`] if the `id` is empty.
    pub fn store(&mut self, vc: VerifiableCredential) -> Result<(), StoreError> {
        if vc.id.is_empty() {
            return Err(StoreError::InvalidCredential(
                "credential id must not be empty".to_string(),
            ));
        }
        if self.credentials.contains_key(&vc.id) {
            return Err(StoreError::DuplicateId(vc.id.clone()));
        }
        self.credentials.insert(vc.id.clone(), vc);
        Ok(())
    }

    /// Revoke the credential with the given `id`.
    ///
    /// Returns `true` if the credential exists (and was newly revoked or was already
    /// revoked), `false` if the credential is not found.
    pub fn revoke(&mut self, id: &str) -> bool {
        if self.credentials.contains_key(id) {
            self.revoked.insert(id.to_string());
            true
        } else {
            false
        }
    }

    // ── Read operations ──────────────────────────────────────────────────

    /// Retrieve a credential by id.
    pub fn get(&self, id: &str) -> Option<&VerifiableCredential> {
        self.credentials.get(id)
    }

    /// Returns `true` if the credential with `id` has been revoked.
    pub fn is_revoked(&self, id: &str) -> bool {
        self.revoked.contains(id)
    }

    /// Resolve the [`CredentialStatus`] of a credential.
    ///
    /// # Arguments
    /// * `id`           – credential identifier
    /// * `current_date` – ISO 8601 date string representing "now" (e.g. `"2025-06-01"`)
    ///
    /// Returns `None` if no credential with that id exists.
    pub fn status(&self, id: &str, current_date: &str) -> Option<CredentialStatus> {
        let vc = self.credentials.get(id)?;

        if self.revoked.contains(id) {
            return Some(CredentialStatus::Revoked);
        }

        // Pending: issuance date is strictly after current_date (lexicographic ISO 8601 compare)
        if vc.issuance_date.as_str() > current_date {
            return Some(CredentialStatus::Pending);
        }

        // Expired: expiration date is strictly before current_date
        if let Some(ref exp) = vc.expiration_date {
            if exp.as_str() < current_date {
                return Some(CredentialStatus::Expired);
            }
        }

        Some(CredentialStatus::Valid)
    }

    // ── Search ───────────────────────────────────────────────────────────

    /// Search credentials matching *all* non-None fields of `filter`.
    ///
    /// `current_date` is used to resolve the status field of the filter.
    pub fn search<'a>(
        &'a self,
        filter: &CredentialFilter,
        current_date: &str,
    ) -> Vec<&'a VerifiableCredential> {
        self.credentials
            .values()
            .filter(|vc| {
                if let Some(ref issuer) = filter.issuer {
                    if &vc.issuer != issuer {
                        return false;
                    }
                }
                if let Some(ref subject_id) = filter.subject_id {
                    if &vc.subject.id != subject_id {
                        return false;
                    }
                }
                if let Some(ref cred_type) = filter.credential_type {
                    if !vc.types.contains(cred_type) {
                        return false;
                    }
                }
                if let Some(ref required_status) = filter.status {
                    let resolved = self.status(&vc.id, current_date);
                    if resolved.as_ref() != Some(required_status) {
                        return false;
                    }
                }
                true
            })
            .collect()
    }

    /// Return all credentials issued by `issuer`.
    pub fn credentials_by_issuer(&self, issuer: &str) -> Vec<&VerifiableCredential> {
        self.credentials
            .values()
            .filter(|vc| vc.issuer == issuer)
            .collect()
    }

    /// Return all credentials whose subject id equals `subject_id`.
    pub fn credentials_for_subject(&self, subject_id: &str) -> Vec<&VerifiableCredential> {
        self.credentials
            .values()
            .filter(|vc| vc.subject.id == subject_id)
            .collect()
    }

    // ── Counters ─────────────────────────────────────────────────────────

    /// Total number of stored credentials (including revoked).
    pub fn count(&self) -> usize {
        self.credentials.len()
    }

    /// Number of revoked credential ids tracked by the store.
    pub fn revoked_count(&self) -> usize {
        self.revoked.len()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn make_vc(id: &str, issuer: &str, subject_id: &str) -> VerifiableCredential {
        VerifiableCredential {
            id: id.to_string(),
            types: vec!["VerifiableCredential".to_string()],
            issuer: issuer.to_string(),
            issuance_date: "2024-01-01".to_string(),
            expiration_date: None,
            subject: CredentialSubject {
                id: subject_id.to_string(),
                claims: HashMap::new(),
            },
            proof: None,
        }
    }

    fn make_vc_with_expiry(id: &str, issuer: &str, subject_id: &str, expiry: &str) -> VerifiableCredential {
        let mut vc = make_vc(id, issuer, subject_id);
        vc.expiration_date = Some(expiry.to_string());
        vc
    }

    fn today() -> &'static str {
        "2025-06-01"
    }

    // ── store / get ────────────────────────────────────────────────────────

    #[test]
    fn test_store_and_get() {
        let mut store = CredentialStore::new();
        let vc = make_vc("urn:vc:1", "did:issuer:1", "did:alice");
        store.store(vc).expect("store failed");
        let fetched = store.get("urn:vc:1");
        assert!(fetched.is_some());
        assert_eq!(fetched.unwrap().issuer, "did:issuer:1");
    }

    #[test]
    fn test_get_nonexistent_returns_none() {
        let store = CredentialStore::new();
        assert!(store.get("urn:vc:nope").is_none());
    }

    #[test]
    fn test_store_multiple() {
        let mut store = CredentialStore::new();
        store.store(make_vc("1", "i", "s")).unwrap();
        store.store(make_vc("2", "i", "s")).unwrap();
        assert_eq!(store.count(), 2);
    }

    // ── Duplicate id error ─────────────────────────────────────────────────

    #[test]
    fn test_duplicate_id_returns_error() {
        let mut store = CredentialStore::new();
        store.store(make_vc("urn:vc:x", "i", "s")).unwrap();
        let result = store.store(make_vc("urn:vc:x", "i2", "s2"));
        assert!(result.is_err());
        match result.unwrap_err() {
            StoreError::DuplicateId(id) => assert_eq!(id, "urn:vc:x"),
            _ => panic!("expected DuplicateId"),
        }
    }

    #[test]
    fn test_empty_id_returns_invalid_credential_error() {
        let mut store = CredentialStore::new();
        let result = store.store(make_vc("", "i", "s"));
        assert!(result.is_err());
    }

    // ── revoke / is_revoked ────────────────────────────────────────────────

    #[test]
    fn test_revoke_existing() {
        let mut store = CredentialStore::new();
        store.store(make_vc("urn:vc:r", "i", "s")).unwrap();
        let ok = store.revoke("urn:vc:r");
        assert!(ok);
        assert!(store.is_revoked("urn:vc:r"));
    }

    #[test]
    fn test_revoke_nonexistent_returns_false() {
        let mut store = CredentialStore::new();
        assert!(!store.revoke("urn:vc:ghost"));
    }

    #[test]
    fn test_is_revoked_not_revoked() {
        let mut store = CredentialStore::new();
        store.store(make_vc("urn:vc:ok", "i", "s")).unwrap();
        assert!(!store.is_revoked("urn:vc:ok"));
    }

    #[test]
    fn test_revoke_twice_still_revoked() {
        let mut store = CredentialStore::new();
        store.store(make_vc("urn:vc:2x", "i", "s")).unwrap();
        store.revoke("urn:vc:2x");
        store.revoke("urn:vc:2x");
        assert!(store.is_revoked("urn:vc:2x"));
    }

    // ── status ─────────────────────────────────────────────────────────────

    #[test]
    fn test_status_valid() {
        let mut store = CredentialStore::new();
        store.store(make_vc("urn:vc:v", "i", "s")).unwrap();
        let status = store.status("urn:vc:v", today());
        assert_eq!(status, Some(CredentialStatus::Valid));
    }

    #[test]
    fn test_status_revoked() {
        let mut store = CredentialStore::new();
        store.store(make_vc("urn:vc:rv", "i", "s")).unwrap();
        store.revoke("urn:vc:rv");
        let status = store.status("urn:vc:rv", today());
        assert_eq!(status, Some(CredentialStatus::Revoked));
    }

    #[test]
    fn test_status_expired() {
        let mut store = CredentialStore::new();
        store
            .store(make_vc_with_expiry("urn:vc:exp", "i", "s", "2020-01-01"))
            .unwrap();
        let status = store.status("urn:vc:exp", today());
        assert_eq!(status, Some(CredentialStatus::Expired));
    }

    #[test]
    fn test_status_not_yet_expired() {
        let mut store = CredentialStore::new();
        store
            .store(make_vc_with_expiry("urn:vc:future", "i", "s", "2099-12-31"))
            .unwrap();
        let status = store.status("urn:vc:future", today());
        assert_eq!(status, Some(CredentialStatus::Valid));
    }

    #[test]
    fn test_status_nonexistent_returns_none() {
        let store = CredentialStore::new();
        assert!(store.status("urn:vc:nope", today()).is_none());
    }

    #[test]
    fn test_status_pending_future_issuance() {
        let mut store = CredentialStore::new();
        let mut vc = make_vc("urn:vc:pend", "i", "s");
        vc.issuance_date = "2099-01-01".to_string();
        store.store(vc).unwrap();
        let status = store.status("urn:vc:pend", today());
        assert_eq!(status, Some(CredentialStatus::Pending));
    }

    #[test]
    fn test_status_revoked_overrides_expired() {
        let mut store = CredentialStore::new();
        store
            .store(make_vc_with_expiry("urn:vc:re", "i", "s", "2020-01-01"))
            .unwrap();
        store.revoke("urn:vc:re");
        // Revoked should take priority
        let status = store.status("urn:vc:re", today());
        assert_eq!(status, Some(CredentialStatus::Revoked));
    }

    // ── search ─────────────────────────────────────────────────────────────

    #[test]
    fn test_search_by_issuer() {
        let mut store = CredentialStore::new();
        store.store(make_vc("1", "issuer-A", "alice")).unwrap();
        store.store(make_vc("2", "issuer-B", "bob")).unwrap();
        let filter = CredentialFilter {
            issuer: Some("issuer-A".to_string()),
            ..Default::default()
        };
        let results = store.search(&filter, today());
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].issuer, "issuer-A");
    }

    #[test]
    fn test_search_by_subject_id() {
        let mut store = CredentialStore::new();
        store.store(make_vc("1", "i", "did:alice")).unwrap();
        store.store(make_vc("2", "i", "did:bob")).unwrap();
        let filter = CredentialFilter {
            subject_id: Some("did:alice".to_string()),
            ..Default::default()
        };
        let results = store.search(&filter, today());
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].subject.id, "did:alice");
    }

    #[test]
    fn test_search_by_credential_type() {
        let mut store = CredentialStore::new();
        let mut vc = make_vc("1", "i", "s");
        vc.types.push("DriversLicense".to_string());
        store.store(vc).unwrap();
        store.store(make_vc("2", "i", "s2")).unwrap();

        let filter = CredentialFilter {
            credential_type: Some("DriversLicense".to_string()),
            ..Default::default()
        };
        let results = store.search(&filter, today());
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn test_search_by_status_valid() {
        let mut store = CredentialStore::new();
        store.store(make_vc("v", "i", "s")).unwrap();
        store.store(make_vc("r", "i", "s2")).unwrap();
        store.revoke("r");
        let filter = CredentialFilter {
            status: Some(CredentialStatus::Valid),
            ..Default::default()
        };
        let results = store.search(&filter, today());
        assert!(results.iter().all(|vc| vc.id == "v"));
    }

    #[test]
    fn test_search_none_filter_matches_all() {
        let mut store = CredentialStore::new();
        store.store(make_vc("1", "i1", "s1")).unwrap();
        store.store(make_vc("2", "i2", "s2")).unwrap();
        let filter = CredentialFilter::default();
        let results = store.search(&filter, today());
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_search_combined_filters() {
        let mut store = CredentialStore::new();
        store.store(make_vc("1", "issuer-A", "did:alice")).unwrap();
        store.store(make_vc("2", "issuer-A", "did:bob")).unwrap();
        store.store(make_vc("3", "issuer-B", "did:alice")).unwrap();
        let filter = CredentialFilter {
            issuer: Some("issuer-A".to_string()),
            subject_id: Some("did:alice".to_string()),
            ..Default::default()
        };
        let results = store.search(&filter, today());
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, "1");
    }

    // ── credentials_by_issuer / credentials_for_subject ──────────────────

    #[test]
    fn test_credentials_by_issuer() {
        let mut store = CredentialStore::new();
        store.store(make_vc("1", "iss-A", "s")).unwrap();
        store.store(make_vc("2", "iss-A", "s2")).unwrap();
        store.store(make_vc("3", "iss-B", "s3")).unwrap();
        let by_a = store.credentials_by_issuer("iss-A");
        assert_eq!(by_a.len(), 2);
        assert!(by_a.iter().all(|vc| vc.issuer == "iss-A"));
    }

    #[test]
    fn test_credentials_by_issuer_not_found() {
        let store = CredentialStore::new();
        assert!(store.credentials_by_issuer("nobody").is_empty());
    }

    #[test]
    fn test_credentials_for_subject() {
        let mut store = CredentialStore::new();
        store.store(make_vc("1", "i1", "did:alice")).unwrap();
        store.store(make_vc("2", "i2", "did:alice")).unwrap();
        store.store(make_vc("3", "i3", "did:bob")).unwrap();
        let for_alice = store.credentials_for_subject("did:alice");
        assert_eq!(for_alice.len(), 2);
    }

    #[test]
    fn test_credentials_for_subject_not_found() {
        let store = CredentialStore::new();
        assert!(store.credentials_for_subject("did:nobody").is_empty());
    }

    // ── count / revoked_count ──────────────────────────────────────────────

    #[test]
    fn test_count_empty_store() {
        let store = CredentialStore::new();
        assert_eq!(store.count(), 0);
    }

    #[test]
    fn test_count_after_stores() {
        let mut store = CredentialStore::new();
        store.store(make_vc("1", "i", "s")).unwrap();
        store.store(make_vc("2", "i", "s")).unwrap();
        assert_eq!(store.count(), 2);
    }

    #[test]
    fn test_revoked_count_initial_zero() {
        let store = CredentialStore::new();
        assert_eq!(store.revoked_count(), 0);
    }

    #[test]
    fn test_revoked_count_after_revocations() {
        let mut store = CredentialStore::new();
        store.store(make_vc("1", "i", "s")).unwrap();
        store.store(make_vc("2", "i", "s")).unwrap();
        store.revoke("1");
        store.revoke("2");
        assert_eq!(store.revoked_count(), 2);
    }

    #[test]
    fn test_revoked_count_does_not_double_count() {
        let mut store = CredentialStore::new();
        store.store(make_vc("1", "i", "s")).unwrap();
        store.revoke("1");
        store.revoke("1"); // second revoke
        assert_eq!(store.revoked_count(), 1);
    }

    // ── expiration date comparison ─────────────────────────────────────────

    #[test]
    fn test_expiration_boundary_same_day_valid() {
        let mut store = CredentialStore::new();
        // Expiry on the same day as current_date → expiry >= current_date → Valid
        store
            .store(make_vc_with_expiry("urn:vc:bd", "i", "s", today()))
            .unwrap();
        let status = store.status("urn:vc:bd", today());
        assert_eq!(status, Some(CredentialStatus::Valid));
    }

    #[test]
    fn test_expiration_one_day_past() {
        let mut store = CredentialStore::new();
        store
            .store(make_vc_with_expiry("urn:vc:past", "i", "s", "2025-05-31"))
            .unwrap();
        let status = store.status("urn:vc:past", today());
        assert_eq!(status, Some(CredentialStatus::Expired));
    }

    // ── CredentialSubject with claims ──────────────────────────────────────

    #[test]
    fn test_credential_subject_claims() {
        let mut claims = HashMap::new();
        claims.insert("degree".to_string(), "PhD".to_string());
        let subject = CredentialSubject {
            id: "did:example:alice".to_string(),
            claims,
        };
        assert_eq!(subject.claims.get("degree"), Some(&"PhD".to_string()));
    }

    // ── Additional coverage ─────────────────────────────────────────────────

    #[test]
    fn test_credential_store_default_is_empty() {
        let store = CredentialStore::default();
        assert_eq!(store.count(), 0);
    }

    #[test]
    fn test_vc_has_verifiable_credential_type() {
        let vc = make_vc("id1", "iss", "sub");
        assert!(vc.types.contains(&"VerifiableCredential".to_string()));
    }

    #[test]
    fn test_search_empty_store() {
        let store = CredentialStore::new();
        let filter = CredentialFilter::default();
        assert!(store.search(&filter, today()).is_empty());
    }

    #[test]
    fn test_search_revoked_status() {
        let mut store = CredentialStore::new();
        store.store(make_vc("a", "iss", "sub")).unwrap();
        store.revoke("a");
        let filter = CredentialFilter {
            status: Some(CredentialStatus::Revoked),
            ..Default::default()
        };
        let results = store.search(&filter, today());
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, "a");
    }

    #[test]
    fn test_get_returns_correct_issuer() {
        let mut store = CredentialStore::new();
        store.store(make_vc("id-x", "iss-x", "sub-x")).unwrap();
        let vc = store.get("id-x").expect("should exist");
        assert_eq!(vc.issuer, "iss-x");
        assert_eq!(vc.subject.id, "sub-x");
    }

    #[test]
    fn test_verifiable_credential_with_proof() {
        let mut vc = make_vc("proof-vc", "iss", "sub");
        vc.proof = Some("jws.payload.sig".to_string());
        let mut store = CredentialStore::new();
        store.store(vc).unwrap();
        let fetched = store.get("proof-vc").expect("should exist");
        assert!(fetched.proof.is_some());
    }

    #[test]
    fn test_verifiable_credential_without_proof() {
        let vc = make_vc("no-proof-vc", "iss", "sub");
        let mut store = CredentialStore::new();
        store.store(vc).unwrap();
        let fetched = store.get("no-proof-vc").expect("should exist");
        assert!(fetched.proof.is_none());
    }

    #[test]
    fn test_multiple_types_on_credential() {
        let mut vc = make_vc("multi-type", "iss", "sub");
        vc.types.push("UniversityDegreeCredential".to_string());
        let mut store = CredentialStore::new();
        store.store(vc).unwrap();
        let fetched = store.get("multi-type").expect("should exist");
        assert_eq!(fetched.types.len(), 2);
    }

    #[test]
    fn test_store_error_display_duplicate() {
        let err = StoreError::DuplicateId("test-id".to_string());
        let msg = err.to_string();
        assert!(msg.contains("test-id"));
    }

    #[test]
    fn test_store_error_display_invalid() {
        let err = StoreError::InvalidCredential("missing field".to_string());
        let msg = err.to_string();
        assert!(msg.contains("missing field"));
    }

    #[test]
    fn test_credentials_for_subject_multiple_issuers() {
        let mut store = CredentialStore::new();
        store.store(make_vc("1", "iss1", "did:alice")).unwrap();
        store.store(make_vc("2", "iss2", "did:alice")).unwrap();
        store.store(make_vc("3", "iss3", "did:bob")).unwrap();
        let for_alice = store.credentials_for_subject("did:alice");
        assert_eq!(for_alice.len(), 2);
        assert!(for_alice.iter().all(|vc| vc.subject.id == "did:alice"));
    }
}