vta-sdk 0.63.0

SDK for Verifiable Trust Agents operating in Verifiable Trust Communities
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
//! A vetter proving to an applicant that the community named them a vetter.
//!
//! A community names a vetter by issuing them a **vetter role credential**: a
//! DTG Verifiable Authority Credential (`AuthorityCredential`) issued by the
//! community with `issuerScope` `public`, whose `credentialSubject.authority` is
//! `{ scope: <community DID>, actions: ["role:vetter"], maxAttenuation: 0 }`,
//! carrying a `credentialStatus` (vtc/vetting/vetters/grant/0.1). The community counts
//! statements against its own record of the grant; an applicant, who holds no
//! such record, is shown the credential instead.
//!
//! The vetter answers an applicant's `vetting/request/0.1` with a Verifiable
//! Presentation of it, carried as `eligibilityVp` on the `#response` — before
//! any session exists (dtgwg-trust-tasks-tf `specs/vetting/request/0.1`,
//! rule 5). The presentation is bound to that request: `nonce` is the request
//! document's `id`, and `domain` is the request's `joinDid` — the applicant.
//! The vetter signs it with `authentication` ([`build_eligibility_vp`]). The
//! applicant checks that the presentation answers its own request, that its
//! holder is the vetter it asked, and that the community signed a role
//! credential for exactly that vetter, for exactly that community, in the role
//! the requirements name ([`verify_eligibility_vp`]).
//!
//! What this does **not** establish: that the grant has not been revoked.
//! Status-list resolution needs the network and a fetch profile, so it is left
//! to the caller, who gets the `credentialStatus` from
//! [`VerifiedEligibility::credential_status`]. A revoked grant also stops the
//! vetter's statements counting at the community, which is the check that
//! decides admission.

use affinidi_data_integrity::{DataIntegrityProof, SignOptions, crypto_suites::CryptoSuite};
use affinidi_secrets_resolver::secrets::Secret;
use chrono::{DateTime, Utc};
use dtg_credentials::{DTGCredentialType, IssuerScope};
use serde::Deserialize;
use serde_json::{Value, json};

use super::card::CLOCK_SKEW;
use super::{VettingError, did_of, verify_attached_proof};
use crate::protocols::vetting::{role_matches, role_of_action};
use crate::trust_task_proof::TrustTaskVmResolver;

const VP_WHAT: &str = "eligibility presentation";
const ROLE_WHAT: &str = "vetter role credential";

/// The presentation's proof purpose: the holder authenticates to the applicant.
pub const ELIGIBILITY_PROOF_PURPOSE: &str = "authentication";

/// The role credential's proof purpose: the community asserts the grant.
const ROLE_CREDENTIAL_PROOF_PURPOSE: &str = "assertionMethod";

const VC_CONTEXT_V2: &str = "https://www.w3.org/ns/credentials/v2";

/// `(community DID, roles)` of a community role credential, or `None` when
/// `credential` is not one.
///
/// A community role credential is a DTG VAC — parsed strictly, as
/// [`verify_eligibility_vp`] parses it: the v1 context, exactly one subtype
/// (`AuthorityCredential`), a declared `issuerScope` — whose `issuer` is its own
/// `authority.scope` (the community conferring authority in itself), carrying
/// no `authority.parent`, with at least one `role:<name>` action. The roles are
/// the `<name>`s, in action order.
///
/// Checks the shape only — no proof, window or scope declaration is verified.
/// Use it to pick candidates out of a wallet before presenting them.
#[must_use]
pub fn community_roles(credential: &Value) -> Option<(String, Vec<String>)> {
    // Without its proof: a two-key community's grant carries a proof set
    // (VTI-57), and this checks the shape only.
    let vac = super::dtg_shape(credential).ok()?;
    if vac.type_() != DTGCredentialType::Authority {
        return None;
    }
    let authority = vac.credential().authority()?;
    if authority.scope != vac.issuer() || authority.parent.is_some() {
        return None;
    }
    let roles: Vec<String> = authority
        .actions
        .iter()
        .filter_map(|a| role_of_action(a))
        .map(str::to_string)
        .collect();
    if roles.is_empty() {
        return None;
    }
    Some((authority.scope.clone(), roles))
}

/// Present `credentials` to an applicant, bound to the request being answered.
///
/// `challenge` becomes the presentation's `nonce` and must be the `id` of the
/// applicant's `vetting/request` document; `domain` must be that request's
/// `joinDid`, the applicant. The holder is the DID of `holder`'s key.
///
/// # Errors
///
/// [`VettingError::WrongSigner`] if no DID can be read from `holder`'s id, or
/// [`VettingError::Sign`].
pub async fn build_eligibility_vp(
    holder: &Secret,
    credentials: Vec<Value>,
    challenge: &str,
    domain: &str,
) -> Result<Value, VettingError> {
    let holder_did = did_of(&holder.id);
    if holder_did.len() <= "did:".len() || !holder_did.starts_with("did:") {
        return Err(VettingError::WrongSigner {
            what: VP_WHAT,
            role: "holder",
        });
    }
    let mut vp = json!({
        "@context": [VC_CONTEXT_V2],
        "type": ["VerifiablePresentation"],
        "holder": holder_did,
        "verifiableCredential": credentials,
        "nonce": challenge,
        "domain": domain,
    });
    let proof = DataIntegrityProof::sign(
        &vp,
        holder,
        SignOptions::new()
            .with_proof_purpose(ELIGIBILITY_PROOF_PURPOSE)
            .with_cryptosuite(CryptoSuite::EddsaJcs2022),
    )
    .await
    .map_err(|e| VettingError::Sign(e.to_string()))?;
    vp.as_object_mut()
        .expect("presentation is an object")
        .insert(
            "proof".into(),
            serde_json::to_value(proof).map_err(|e| VettingError::Sign(e.to_string()))?,
        );
    Ok(vp)
}

/// What the applicant expects an eligibility presentation to prove.
#[derive(Debug, Clone, Copy)]
pub struct EligibilityExpectations<'a> {
    /// The vetter the request was sent to.
    pub vetter: &'a str,
    /// The community the applicant is joining.
    pub community: &'a str,
    /// `eligibleVetters.role` from the community's requirements.
    pub role: &'a str,
    /// The `id` of the applicant's `vetting/request` document, which the
    /// presentation's `nonce` must carry.
    pub challenge: &'a str,
    /// The applicant's `joinDid` from that request, which the presentation's
    /// `domain` must carry.
    pub domain: &'a str,
    /// Verification time.
    pub now: DateTime<Utc>,
}

/// A role credential that makes the presenting vetter eligible.
///
/// Only [`verify_eligibility_vp`] builds one. Revocation is not checked: read
/// [`credential_status`](Self::credential_status) and resolve it.
#[derive(Debug, Clone)]
pub struct VerifiedEligibility {
    credential_id: Option<String>,
    valid_from: DateTime<Utc>,
    valid_until: DateTime<Utc>,
    credential_status: Option<Value>,
}

impl VerifiedEligibility {
    /// The role credential's `id`.
    #[must_use]
    pub fn credential_id(&self) -> Option<&str> {
        self.credential_id.as_deref()
    }

    /// The role credential's `validFrom`.
    #[must_use]
    pub fn valid_from(&self) -> DateTime<Utc> {
        self.valid_from
    }

    /// The role credential's `validUntil`.
    #[must_use]
    pub fn valid_until(&self) -> DateTime<Utc> {
        self.valid_until
    }

    /// The role credential's `credentialStatus`, for the caller to resolve.
    #[must_use]
    pub fn credential_status(&self) -> Option<&Value> {
        self.credential_status.as_ref()
    }
}

#[derive(Deserialize)]
struct WirePresentation {
    #[serde(rename = "type")]
    types: OneOrMany,
    holder: String,
    #[serde(default)]
    nonce: Option<String>,
    #[serde(default)]
    domain: Option<String>,
    #[serde(rename = "verifiableCredential", default)]
    credentials: Vec<Value>,
}

#[derive(Deserialize)]
#[serde(untagged)]
enum OneOrMany {
    One(String),
    Many(Vec<String>),
}

impl OneOrMany {
    fn contains(&self, wanted: &str) -> bool {
        match self {
            Self::One(t) => t == wanted,
            Self::Many(ts) => ts.iter().any(|t| t == wanted),
        }
    }
}

/// Verify a vetter's eligibility presentation.
///
/// Holds when the presentation is signed for `authentication` by its holder,
/// the holder is `expect.vetter`, `nonce` and `domain` are the request's `id`
/// and `joinDid`, and
/// it carries a role credential — a DTG VAC under the v1 context, declaring
/// `issuerScope` `public`, with no `authority.parent` — that `expect.community`
/// signed for `assertionMethod`, naming `expect.vetter`, whose `authority.scope`
/// is `expect.community` and whose `authority.actions` include `role:<role>`
/// for a role that [`role_matches`] `expect.role`, valid at `expect.now`.
///
/// # Errors
///
/// The first failed check: [`VettingError::Malformed`],
/// [`VettingError::WrongSigner`], [`VettingError::Binding`],
/// [`VettingError::Proof`], [`VettingError::Expired`], or
/// [`VettingError::NoRoleCredential`] when no credential names this vetter in
/// this role for this community. When several candidates fail, the first one's
/// error is returned.
pub async fn verify_eligibility_vp(
    vp: &Value,
    expect: &EligibilityExpectations<'_>,
    resolver: &TrustTaskVmResolver,
) -> Result<VerifiedEligibility, VettingError> {
    let wire: WirePresentation =
        serde_json::from_value(vp.clone()).map_err(|e| VettingError::Malformed {
            what: VP_WHAT,
            detail: e.to_string(),
        })?;
    if !wire.types.contains("VerifiablePresentation") {
        return Err(VettingError::Malformed {
            what: VP_WHAT,
            detail: "type does not include VerifiablePresentation".into(),
        });
    }
    if wire.holder != expect.vetter {
        return Err(VettingError::WrongSigner {
            what: VP_WHAT,
            role: "vetter",
        });
    }
    if wire.nonce.as_deref() != Some(expect.challenge) {
        return Err(VettingError::Binding("nonce"));
    }
    if wire.domain.as_deref() != Some(expect.domain) {
        return Err(VettingError::Binding("domain"));
    }
    let signer = verify_attached_proof(VP_WHAT, vp, ELIGIBILITY_PROOF_PURPOSE, resolver).await?;
    if signer != wire.holder {
        return Err(VettingError::WrongSigner {
            what: VP_WHAT,
            role: "holder",
        });
    }

    let mut first_error = None;
    for credential in &wire.credentials {
        let Some((community, roles)) = community_roles(credential) else {
            continue;
        };
        if community != expect.community || !roles.iter().any(|r| role_matches(r, expect.role)) {
            continue;
        }
        match verify_role_credential(credential, expect, resolver).await {
            Ok(verified) => return Ok(verified),
            Err(e) => {
                first_error.get_or_insert(e);
            }
        }
    }
    Err(first_error.unwrap_or(VettingError::NoRoleCredential))
}

async fn verify_role_credential(
    credential: &Value,
    expect: &EligibilityExpectations<'_>,
    resolver: &TrustTaskVmResolver,
) -> Result<VerifiedEligibility, VettingError> {
    let malformed = |detail: String| VettingError::Malformed {
        what: ROLE_WHAT,
        detail,
    };
    // A strict DTG parse: the v1 context, exactly one concrete subtype, a
    // declared `issuerScope`.
    // The proof (one, or a set) is verified below over the credential as
    // received; the shape parse sets it aside (VTI-57).
    let vac = super::dtg_shape(credential).map_err(|e| malformed(e.to_string()))?;
    if vac.type_() != DTGCredentialType::Authority {
        return Err(malformed("not an AuthorityCredential".into()));
    }
    let issuer = vac.issuer().to_string();
    if issuer != expect.community {
        return Err(VettingError::WrongSigner {
            what: ROLE_WHAT,
            role: "issuer",
        });
    }
    // vetting/request/0.1: the community issued it directly — declaring
    // `public`, the only scope a community can truthfully declare — rather
    // than someone attenuating theirs.
    if vac.issuer_scope() != IssuerScope::Public {
        return Err(malformed(
            "a community role credential declares issuerScope public".into(),
        ));
    }
    let authority = vac
        .credential()
        .authority()
        .ok_or_else(|| malformed("no credentialSubject.authority".into()))?;
    if authority.parent.is_some() {
        return Err(malformed(
            "an attenuated VAC is not a community role grant".into(),
        ));
    }
    if authority.scope != expect.community {
        return Err(VettingError::Binding("authority.scope"));
    }
    if vac.subject() != expect.vetter {
        return Err(VettingError::Binding("credentialSubject"));
    }
    let valid_until = vac
        .valid_until()
        .ok_or_else(|| malformed("no validUntil — a role grant is bounded".into()))?;
    if vac.valid_from() > expect.now + CLOCK_SKEW || expect.now > valid_until + CLOCK_SKEW {
        return Err(VettingError::Expired(ROLE_WHAT));
    }
    let signer = verify_attached_proof(
        ROLE_WHAT,
        credential,
        ROLE_CREDENTIAL_PROOF_PURPOSE,
        resolver,
    )
    .await?;
    if signer != issuer {
        return Err(VettingError::WrongSigner {
            what: ROLE_WHAT,
            role: "issuer",
        });
    }
    Ok(VerifiedEligibility {
        credential_id: vac.id().map(str::to_string),
        valid_from: vac.valid_from(),
        valid_until,
        credential_status: vac.credential().credential_status.clone(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vetting::test_support::{did, secret};
    use chrono::Duration;
    use dtg_credentials::DTGCredential;

    /// The `id` of the `vetting/request` document being answered.
    const CHALLENGE: &str = "urn:uuid:3f1c9a52-8c1e-4f2b-9d7a-0b6e5c4d3a21";
    /// That request's `joinDid`: the applicant.
    const DOMAIN: &str = "did:key:z6MkApplicantJoinDid";

    /// A role VAC signed by `community`, conferring `role:<role>` at
    /// `community_did` — which a forger may set to someone else's DID.
    async fn role_vac(
        community: &Secret,
        subject: &str,
        community_did: &str,
        role: &str,
        valid_from: DateTime<Utc>,
        valid_until: DateTime<Utc>,
    ) -> Value {
        let mut credential = DTGCredential::new_vac(
            did(community),
            IssuerScope::Public,
            subject.to_string(),
            community_did.to_string(),
            vec![crate::protocols::vetting::role_action(role)],
            valid_from,
            valid_until,
        )
        .unwrap()
        .with_max_attenuation(0)
        .unwrap()
        .with_id("urn:uuid:vetter-grant".to_string());
        credential.sign(community, None).await.unwrap();
        serde_json::to_value(&credential).unwrap()
    }

    async fn live_vac(community: &Secret, vetter: &Secret, role: &str) -> Value {
        let now = Utc::now();
        let c = did(community);
        role_vac(
            community,
            &did(vetter),
            &c,
            role,
            now - Duration::days(1),
            now + Duration::days(364),
        )
        .await
    }

    async fn verify(
        vp: &Value,
        vetter: &str,
        community: &str,
        role: &str,
    ) -> Result<VerifiedEligibility, VettingError> {
        verify_eligibility_vp(
            vp,
            &EligibilityExpectations {
                vetter,
                community,
                role,
                challenge: CHALLENGE,
                domain: DOMAIN,
                now: Utc::now(),
            },
            &TrustTaskVmResolver::did_key_only(),
        )
        .await
    }

    /// `vac` re-signed so its `proof` is a set of two, as a community holding
    /// two signing keys writes it.
    async fn with_proof_set(community: &Secret, mut vac: Value) -> Value {
        let first = vac["proof"].clone();
        let mut proofless = vac.clone();
        proofless.as_object_mut().unwrap().remove("proof");
        let second = affinidi_data_integrity::DataIntegrityProof::sign(
            &proofless,
            community,
            affinidi_data_integrity::SignOptions::new().with_proof_purpose("assertionMethod"),
        )
        .await
        .unwrap();
        vac["proof"] = serde_json::json!([first, serde_json::to_value(second).unwrap()]);
        vac
    }

    /// VTI-57: a vetter grant from a two-key community carries a proof set.
    /// `community_roles` used to fail to parse it and answer `None`, so openvtc
    /// filed the grant as a plain role and never seated the vetter.
    #[tokio::test]
    async fn vti_57_a_grant_carrying_a_proof_set_names_its_roles() {
        let (community, vetter) = (secret(0xC7), secret(0x17));
        let vac = with_proof_set(&community, live_vac(&community, &vetter, "vetter").await).await;
        assert!(vac["proof"].is_array());
        assert_eq!(
            community_roles(&vac),
            Some((did(&community), vec!["vetter".to_string()]))
        );
    }

    /// VTI-57, presentation side: the same grant presented to an applicant
    /// verifies; the strict shape parse refused it as malformed.
    #[tokio::test]
    async fn vti_57_an_eligibility_vp_over_a_proof_set_grant_verifies() {
        let (community, vetter) = (secret(0xC8), secret(0x18));
        let vac = with_proof_set(&community, live_vac(&community, &vetter, "vetter").await).await;
        let vp = build_eligibility_vp(&vetter, vec![vac], CHALLENGE, DOMAIN)
            .await
            .unwrap();
        verify(&vp, &did(&vetter), &did(&community), "vetter")
            .await
            .expect("a proof-set grant verifies");
    }

    /// Setting the proof aside for the shape parse loosens nothing: a set with
    /// one tampered proof is still refused, by the proof check.
    #[tokio::test]
    async fn vti_57_a_tampered_proof_in_the_set_is_still_refused() {
        let (community, vetter) = (secret(0xC9), secret(0x19));
        let mut vac =
            with_proof_set(&community, live_vac(&community, &vetter, "vetter").await).await;
        vac["proof"][1]["proofValue"] = vac["proof"][0]["proofValue"].clone();
        vac["proof"][1]["created"] = serde_json::json!("2020-01-01T00:00:00Z");
        let vp = build_eligibility_vp(&vetter, vec![vac], CHALLENGE, DOMAIN)
            .await
            .unwrap();
        assert!(
            verify(&vp, &did(&vetter), &did(&community), "vetter")
                .await
                .is_err()
        );
    }

    #[tokio::test]
    async fn a_vetter_proves_the_community_named_them() {
        let (community, vetter) = (secret(0xC0), secret(0x11));
        let vac = live_vac(&community, &vetter, "vetter").await;
        assert_eq!(
            community_roles(&vac),
            Some((did(&community), vec!["vetter".to_string()]))
        );
        assert_eq!(vac["credentialSubject"]["authority"]["maxAttenuation"], 0);
        let vp = build_eligibility_vp(&vetter, vec![vac], CHALLENGE, DOMAIN)
            .await
            .unwrap();
        assert_eq!(vp["holder"], did(&vetter));
        assert_eq!(vp["proof"]["proofPurpose"], ELIGIBILITY_PROOF_PURPOSE);

        let verified = verify(&vp, &did(&vetter), &did(&community), "vetter")
            .await
            .unwrap();
        assert_eq!(verified.credential_id(), Some("urn:uuid:vetter-grant"));
        assert!(verified.valid_until() > verified.valid_from());
        assert!(verified.credential_status().is_none());
        // The requirements may spell the role as the ACL does.
        verify(&vp, &did(&vetter), &did(&community), "custom:vetter")
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn a_presentation_for_another_session_is_refused() {
        let (community, vetter) = (secret(0xC0), secret(0x11));
        let vac = live_vac(&community, &vetter, "vetter").await;

        let other_challenge = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopq";
        let vp = build_eligibility_vp(&vetter, vec![vac.clone()], other_challenge, DOMAIN)
            .await
            .unwrap();
        assert!(matches!(
            verify(&vp, &did(&vetter), &did(&community), "vetter").await,
            Err(VettingError::Binding("nonce"))
        ));

        let vp = build_eligibility_vp(&vetter, vec![vac], CHALLENGE, "did:web:elsewhere")
            .await
            .unwrap();
        assert!(matches!(
            verify(&vp, &did(&vetter), &did(&community), "vetter").await,
            Err(VettingError::Binding("domain"))
        ));
    }

    #[tokio::test]
    async fn a_role_credential_from_another_community_does_not_count() {
        let (community, other, vetter) = (secret(0xC0), secret(0xC1), secret(0x11));
        let vac = live_vac(&other, &vetter, "vetter").await;
        let vp = build_eligibility_vp(&vetter, vec![vac], CHALLENGE, DOMAIN)
            .await
            .unwrap();
        assert!(matches!(
            verify(&vp, &did(&vetter), &did(&community), "vetter").await,
            Err(VettingError::NoRoleCredential)
        ));

        // A credential that *claims* this community's scope but was signed by
        // another is not a community role credential at all.
        let now = Utc::now();
        let forged = role_vac(
            &other,
            &did(&vetter),
            &did(&community),
            "vetter",
            now - Duration::days(1),
            now + Duration::days(30),
        )
        .await;
        assert_eq!(community_roles(&forged), None);
        let vp = build_eligibility_vp(&vetter, vec![forged.clone()], CHALLENGE, DOMAIN)
            .await
            .unwrap();
        assert!(matches!(
            verify(&vp, &did(&vetter), &did(&community), "vetter").await,
            Err(VettingError::NoRoleCredential)
        ));

        // Relabelling the issuer to match breaks the forger's own signature.
        let mut relabelled = forged;
        relabelled["issuer"] = json!(did(&community));
        let vp = build_eligibility_vp(&vetter, vec![relabelled], CHALLENGE, DOMAIN)
            .await
            .unwrap();
        assert!(matches!(
            verify(&vp, &did(&vetter), &did(&community), "vetter").await,
            Err(VettingError::Proof { .. })
        ));
    }

    #[tokio::test]
    async fn a_different_role_does_not_count() {
        let (community, vetter) = (secret(0xC0), secret(0x11));
        let vac = live_vac(&community, &vetter, "moderator").await;
        let vp = build_eligibility_vp(&vetter, vec![vac], CHALLENGE, DOMAIN)
            .await
            .unwrap();
        assert!(matches!(
            verify(&vp, &did(&vetter), &did(&community), "vetter").await,
            Err(VettingError::NoRoleCredential)
        ));
    }

    #[tokio::test]
    async fn a_tampered_role_credential_is_refused() {
        let (community, vetter) = (secret(0xC0), secret(0x11));
        let mut vac = live_vac(&community, &vetter, "vetter").await;
        vac["validUntil"] = json!((Utc::now() + Duration::days(3650)).to_rfc3339());
        let vp = build_eligibility_vp(&vetter, vec![vac], CHALLENGE, DOMAIN)
            .await
            .unwrap();
        assert!(matches!(
            verify(&vp, &did(&vetter), &did(&community), "vetter").await,
            Err(VettingError::Proof { .. })
        ));
    }

    #[tokio::test]
    async fn an_expired_role_credential_is_refused() {
        let (community, vetter) = (secret(0xC0), secret(0x11));
        let now = Utc::now();
        let vac = role_vac(
            &community,
            &did(&vetter),
            &did(&community),
            "vetter",
            now - Duration::days(400),
            now - Duration::days(1),
        )
        .await;
        let vp = build_eligibility_vp(&vetter, vec![vac], CHALLENGE, DOMAIN)
            .await
            .unwrap();
        assert!(matches!(
            verify(&vp, &did(&vetter), &did(&community), "vetter").await,
            Err(VettingError::Expired(_))
        ));
    }

    #[tokio::test]
    async fn someone_else_presenting_a_vetters_credential_is_refused() {
        let (community, vetter, mallory) = (secret(0xC0), secret(0x11), secret(0x66));
        let vac = live_vac(&community, &vetter, "vetter").await;
        let vp = build_eligibility_vp(&mallory, vec![vac.clone()], CHALLENGE, DOMAIN)
            .await
            .unwrap();
        assert!(matches!(
            verify(&vp, &did(&vetter), &did(&community), "vetter").await,
            Err(VettingError::WrongSigner { role: "vetter", .. })
        ));

        // Mallory relabels the holder but cannot re-sign as the vetter.
        let mut relabelled = vp;
        relabelled["holder"] = json!(did(&vetter));
        assert!(
            verify(&relabelled, &did(&vetter), &did(&community), "vetter")
                .await
                .is_err()
        );

        // And a credential naming someone else, presented by its holder.
        let vp = build_eligibility_vp(&mallory, vec![vac], CHALLENGE, DOMAIN)
            .await
            .unwrap();
        assert!(matches!(
            verify(&vp, &did(&mallory), &did(&community), "vetter").await,
            Err(VettingError::Binding("credentialSubject"))
        ));
    }

    #[tokio::test]
    async fn a_holder_without_a_did_cannot_present() {
        let mut holder = secret(0x11);
        holder.id = "key-0".into();
        assert!(matches!(
            build_eligibility_vp(&holder, vec![], CHALLENGE, DOMAIN).await,
            Err(VettingError::WrongSigner { role: "holder", .. })
        ));
    }

    #[tokio::test]
    async fn a_role_credential_declaring_a_narrower_scope_is_refused() {
        let (community, vetter) = (secret(0xC0), secret(0x11));
        let now = Utc::now();
        let mut vac = DTGCredential::new_vac(
            did(&community),
            IssuerScope::Directed,
            did(&vetter),
            did(&community),
            vec!["role:vetter".into()],
            now - Duration::days(1),
            now + Duration::days(30),
        )
        .unwrap();
        vac.sign(&community, None).await.unwrap();
        let vp = build_eligibility_vp(
            &vetter,
            vec![serde_json::to_value(&vac).unwrap()],
            CHALLENGE,
            DOMAIN,
        )
        .await
        .unwrap();
        assert!(matches!(
            verify(&vp, &did(&vetter), &did(&community), "vetter").await,
            Err(VettingError::Malformed { .. })
        ));
    }

    #[test]
    fn only_community_issued_role_vacs_are_role_credentials() {
        let now = Utc::now();
        let vac = |issuer: &str, scope: &str, actions: &[&str]| {
            let vac = DTGCredential::new_vac(
                issuer.into(),
                IssuerScope::Public,
                "did:key:z".into(),
                scope.into(),
                actions.iter().map(|a| a.to_string()).collect(),
                now,
                now + Duration::days(1),
            )
            .unwrap();
            crate::vetting::tests_support_json(&vac)
        };
        assert_eq!(
            community_roles(&vac(
                "did:web:c",
                "did:web:c",
                &["role:vetter", "read", "role:mod"]
            )),
            Some(("did:web:c".into(), vec!["vetter".into(), "mod".into()]))
        );
        // Not a role action.
        assert!(community_roles(&vac("did:web:c", "did:web:c", &["vetter"])).is_none());
        // Scope other than the issuer's own.
        assert!(community_roles(&vac("did:web:c", "did:web:d", &["role:vetter"])).is_none());
        // Not a DTG v1 credential: the parse is strict.
        let mut legacy = vac("did:web:c", "did:web:c", &["role:vetter"]);
        legacy["@context"] = json!(["https://www.w3.org/ns/credentials/v2"]);
        assert!(community_roles(&legacy).is_none());
        // The retired role-endorsement shape.
        assert!(
            community_roles(&json!({
                "type": ["VerifiableCredential", "EndorsementCredential"],
                "issuer": "did:web:c",
                "credentialSubject": { "id": "did:key:z", "endorsement": {
                    "type": "CommunityRole", "role": "vetter", "communityDid": "did:web:c"
                } }
            }))
            .is_none()
        );
    }
}