tenzro-identity 0.1.0

Tenzro Decentralized Identity Protocol (TDIP) — unified human and machine identity, W3C DID, delegation scopes, cascading revocation
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
//! Identity verification for the Tenzro Decentralized Identity Protocol
//!
//! Provides credential chain verification, trust chain validation,
//! and identity attestation checking.
//!
//! # CRITICAL #41 — Recursive trust chain traversal
//!
//! The verifier walks credential issuer chains recursively from a leaf
//! credential up to a configured trust root, validating at every hop:
//!
//! 1. The issuer DID resolves to an `Active` identity in the registry
//! 2. The credential's cryptographic proof verifies against one of the
//!    issuer's `AssertionMethod` public keys
//! 3. The credential is not expired
//! 4. The chain terminates at a registered trust root, OR the issuer
//!    itself holds a credential of the same type that we can recursively
//!    verify
//!
//! Cycle detection is enforced via a `HashSet<String>` of visited issuer
//! DIDs, and recursion is bounded by `max_chain_depth` (default 10) to
//! prevent stack overflow / DoS from maliciously crafted issuer rings.

use crate::credential::{TenzroCredentialType, VerifiableCredential};
use crate::error::{IdentityError, Result};
use crate::identity::{IdentityData, IdentityStatus, KeyPurpose, TenzroIdentity};
use crate::registry::IdentityRegistry;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::sync::Arc;
use tenzro_types::identity::KycTier;

/// Default maximum trust chain traversal depth.
///
/// 10 hops is more than enough for any realistic credential ladder
/// (root CA → regional CA → issuer → subject is 4 hops) while still
/// providing a hard DoS guard.
pub const DEFAULT_MAX_CHAIN_DEPTH: usize = 10;

/// Result of a single credential's chain verification.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CredentialChainResult {
    /// Whether the credential's full chain is valid
    pub valid: bool,
    /// Type of credential that was verified
    pub credential_type: String,
    /// Number of hops walked from leaf to root
    pub chain_length: usize,
    /// DID of the trust root that terminated the chain (if any)
    pub terminating_root: Option<String>,
    /// Any issues found during verification
    pub issues: Vec<String>,
}

/// Result of a trust chain verification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrustChainResult {
    /// Whether the chain is valid
    pub valid: bool,
    /// The DID being verified
    pub subject_did: String,
    /// The controller DID (if machine identity)
    pub controller_did: Option<String>,
    /// The controller's KYC tier (if machine with controller)
    pub controller_kyc_tier: Option<KycTier>,
    /// Number of valid (non-expired) credentials on the identity
    pub valid_credentials: usize,
    /// Number of credentials whose full issuer chain verified
    /// (CRITICAL #41). May be lower than `valid_credentials` if
    /// some credentials don't anchor to a trust root.
    pub verified_chains: usize,
    /// Per-credential chain verification results (CRITICAL #41).
    pub chain_results: Vec<CredentialChainResult>,
    /// Any issues found during verification
    pub issues: Vec<String>,
}

/// Identity verifier for checking trust chains and credential validity
pub struct IdentityVerifier {
    registry: Arc<IdentityRegistry>,
    /// DIDs of trusted root issuers (CRITICAL #41).
    ///
    /// A credential chain is considered "anchored" only when traversal
    /// terminates at one of these DIDs. If the set is empty, the verifier
    /// falls back to "trust any self-signed root" — but `verify_trust_chain`
    /// reports `NoTrustRoot` issues for any unanchored credential.
    trust_roots: HashSet<String>,
    /// Maximum recursion depth for chain traversal (CRITICAL #41).
    max_chain_depth: usize,
    /// If true, credentials whose chain does not anchor to a trust root
    /// cause the overall trust chain result to be marked invalid.
    /// Default `false` — chains without roots produce a non-fatal issue
    /// so existing call-sites that don't configure trust roots keep working.
    require_trust_root: bool,
}

impl IdentityVerifier {
    /// Creates a new identity verifier
    pub fn new(registry: Arc<IdentityRegistry>) -> Self {
        Self {
            registry,
            trust_roots: HashSet::new(),
            max_chain_depth: DEFAULT_MAX_CHAIN_DEPTH,
            require_trust_root: false,
        }
    }

    /// Adds a single trust root DID (CRITICAL #41).
    pub fn with_trust_root(mut self, did: impl Into<String>) -> Self {
        self.trust_roots.insert(did.into());
        self
    }

    /// Replaces the entire trust root set (CRITICAL #41).
    pub fn with_trust_roots<I, S>(mut self, dids: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.trust_roots = dids.into_iter().map(Into::into).collect();
        self
    }

    /// Sets the maximum recursion depth for trust chain traversal (CRITICAL #41).
    pub fn with_max_chain_depth(mut self, depth: usize) -> Self {
        self.max_chain_depth = depth.max(1);
        self
    }

    /// If set, credentials whose chain does not terminate at a registered
    /// trust root will cause `verify_trust_chain` to mark the result as
    /// invalid (CRITICAL #41).
    pub fn require_trust_root(mut self, required: bool) -> Self {
        self.require_trust_root = required;
        self
    }

    /// Returns the configured trust roots.
    pub fn trust_roots(&self) -> &HashSet<String> {
        &self.trust_roots
    }

    /// Returns whether the given DID is a registered trust root.
    pub fn is_trust_root(&self, did: &str) -> bool {
        self.trust_roots.contains(did)
    }

    /// Verifies the complete trust chain for an identity (CRITICAL #41).
    ///
    /// For human identities, verifies the identity is active, all credentials
    /// pass cryptographic verification, and each credential's issuer chain
    /// terminates at a recognized trust root.
    ///
    /// For machine identities, also verifies the controller chain and
    /// credential inheritance.
    pub fn verify_trust_chain(&self, did: &str) -> Result<TrustChainResult> {
        let identity = self.registry.resolve(did)?;
        let mut issues = Vec::new();

        // Check identity status
        if identity.status != IdentityStatus::Active {
            issues.push(format!("identity is {:?}", identity.status));
        }

        // Count non-expired credentials
        let valid_credentials = identity.credentials.iter().filter(|c| c.is_valid()).count();
        let expired_count = identity.credentials.len() - valid_credentials;
        if expired_count > 0 {
            issues.push(format!("{} expired credentials", expired_count));
        }

        // CRITICAL #41: walk the issuer chain for each credential
        let mut chain_results = Vec::with_capacity(identity.credentials.len());
        let mut verified_chains = 0usize;
        for cred in &identity.credentials {
            if !cred.is_valid() {
                // Skip expired creds — already counted above
                continue;
            }
            let result = self.verify_credential_chain(cred, did);
            if result.valid {
                verified_chains += 1;
            } else {
                for problem in &result.issues {
                    issues.push(format!(
                        "credential {} chain invalid: {}",
                        result.credential_type, problem
                    ));
                }
                if self.require_trust_root && result.terminating_root.is_none() {
                    // Already counted via issues; nothing extra
                }
            }
            chain_results.push(result);
        }

        // Machine-specific checks
        let (ctrl_did, ctrl_kyc) = match &identity.identity_data {
            IdentityData::Human { .. } | IdentityData::Institution { .. } => {
                (None, identity.kyc_tier())
            }
            IdentityData::Machine { controller_did, .. } => {
                if let Some(ctrl) = controller_did {
                    match self.registry.resolve(ctrl) {
                        Ok(controller) => {
                            if controller.status != IdentityStatus::Active {
                                issues.push(format!(
                                    "controller {} is {:?}",
                                    ctrl, controller.status
                                ));
                            }

                            // Verify inherited credentials match controller's
                            for cred in &identity.credentials {
                                let controller_has = controller.credentials.iter().any(|cc| {
                                    cc.tenzro_type == cred.tenzro_type
                                        && cc.issuer == cred.issuer
                                        && cc.is_valid()
                                });
                                if !controller_has {
                                    issues.push(format!(
                                        "inherited credential {:?} not found in controller",
                                        cred.tenzro_type
                                    ));
                                }
                            }

                            (Some(ctrl.clone()), controller.kyc_tier())
                        }
                        Err(_) => {
                            issues.push(format!("controller {} not found", ctrl));
                            (Some(ctrl.clone()), None)
                        }
                    }
                } else {
                    (None, None)
                }
            }
        };

        Ok(TrustChainResult {
            valid: issues.is_empty(),
            subject_did: did.to_string(),
            controller_did: ctrl_did,
            controller_kyc_tier: ctrl_kyc,
            valid_credentials,
            verified_chains,
            chain_results,
            issues,
        })
    }

    /// Recursively verifies a credential's issuer chain (CRITICAL #41).
    ///
    /// Walks from the credential's `issuer` DID up through any credentials
    /// the issuer holds of the same type, until either (a) a registered
    /// trust root is reached, (b) a self-issued credential is reached
    /// (the issuer is itself a trust root), (c) the chain breaks, or
    /// (d) the maximum depth is exceeded.
    ///
    /// At each hop the verifier:
    /// - Resolves the issuer identity (returning early if NotFound)
    /// - Confirms the issuer is `Active`
    /// - Verifies the credential's cryptographic proof against an
    ///   `AssertionMethod` public key on the issuer
    /// - Detects cycles via the `visited` set
    pub fn verify_credential_chain(
        &self,
        credential: &VerifiableCredential,
        subject_did: &str,
    ) -> CredentialChainResult {
        let credential_type = credential.tenzro_type.type_name().to_string();
        let mut visited = HashSet::new();
        // Subject is implicitly visited so a self-issued credential cannot
        // immediately recurse on itself unless the subject is a trust root.
        visited.insert(subject_did.to_string());

        let mut issues = Vec::new();
        let mut chain_length = 0usize;
        let mut terminating_root: Option<String> = None;

        match self.walk_chain(
            credential,
            &mut visited,
            &mut chain_length,
            &mut terminating_root,
            0,
        ) {
            Ok(()) => {}
            Err(e) => issues.push(e.to_string()),
        }

        // A chain is "valid" only if no errors AND it terminated at a
        // recognized trust root (when trust roots are configured) OR no
        // trust roots are configured at all (rootless mode).
        let anchored = terminating_root.is_some();
        let needs_root = !self.trust_roots.is_empty();
        let valid = issues.is_empty() && (!needs_root || anchored);

        if needs_root && !anchored && issues.is_empty() {
            issues.push("chain did not terminate at a registered trust root".to_string());
        }

        CredentialChainResult {
            valid,
            credential_type,
            chain_length,
            terminating_root,
            issues,
        }
    }

    /// Recursive worker for `verify_credential_chain`.
    fn walk_chain(
        &self,
        credential: &VerifiableCredential,
        visited: &mut HashSet<String>,
        chain_length: &mut usize,
        terminating_root: &mut Option<String>,
        depth: usize,
    ) -> Result<()> {
        // Depth guard
        if depth >= self.max_chain_depth {
            return Err(IdentityError::TrustChainTooDeep {
                max_depth: self.max_chain_depth,
            });
        }

        // Expiration / time-bounds
        if !credential.is_valid() {
            return Err(IdentityError::TrustChainBroken {
                issuer: credential.issuer.clone(),
                reason: "credential is expired or not yet valid".to_string(),
            });
        }

        let issuer_did = credential.issuer.clone();

        // Cycle detection. The subject DID is pre-populated in `visited`
        // by `verify_credential_chain`, which means a self-issued
        // credential at depth 0 would falsely trip the cycle check.
        // Special-case: at depth 0, when the credential is self-issued
        // (issuer == subject == single visited entry), skip the insert
        // — the recursion termination logic below will handle the
        // self-issued case correctly.
        let is_self_issued_at_root =
            depth == 0 && credential.credential_subject.id == issuer_did;
        if !is_self_issued_at_root && !visited.insert(issuer_did.clone()) {
            return Err(IdentityError::TrustChainCycle { issuer: issuer_did });
        }

        // Resolve issuer
        let issuer = self
            .registry
            .resolve(&issuer_did)
            .map_err(|e| IdentityError::TrustChainBroken {
                issuer: issuer_did.clone(),
                reason: format!("could not resolve issuer: {}", e),
            })?;

        // Issuer must be Active
        if issuer.status != IdentityStatus::Active {
            return Err(IdentityError::TrustChainBroken {
                issuer: issuer_did,
                reason: format!("issuer is {:?}", issuer.status),
            });
        }

        // Verify the credential's cryptographic proof against any of the
        // issuer's AssertionMethod public keys. If the credential carries
        // no proof, that is itself a chain break — we cannot trust an
        // unsigned credential during recursive traversal.
        if credential.proof.is_none() {
            return Err(IdentityError::TrustChainBroken {
                issuer: issuer_did,
                reason: "credential carries no cryptographic proof".to_string(),
            });
        }
        if !verify_credential_against_issuer(credential, &issuer) {
            return Err(IdentityError::TrustChainBroken {
                issuer: issuer_did,
                reason: "credential signature does not verify against any \
                         AssertionMethod key on the issuer"
                    .to_string(),
            });
        }

        *chain_length += 1;

        // Termination check #1: issuer is a registered trust root
        if self.trust_roots.contains(&issuer_did) {
            *terminating_root = Some(issuer_did);
            return Ok(());
        }

        // Termination check #2: self-issued credential AND no trust roots
        // configured. In rootless mode (empty trust root set) we accept
        // self-issued credentials as terminal so call sites that have not
        // configured roots keep working.
        if credential.credential_subject.id == issuer_did {
            // self-issued — only terminal in rootless mode
            if self.trust_roots.is_empty() {
                return Ok(());
            }
            // Otherwise self-issued is NOT a trust anchor; the issuer must
            // hold a credential of the same type from a higher authority,
            // which we recurse into below.
        }

        // Recurse: pick the longest-lived valid credential of the same
        // type the issuer holds. If none, the chain terminates here.
        let next_credential = issuer
            .credentials
            .iter()
            .filter(|c| c.tenzro_type == credential.tenzro_type && c.is_valid())
            .max_by_key(|c| c.expiration_date.unwrap_or(chrono::DateTime::<chrono::Utc>::MAX_UTC));

        if let Some(next) = next_credential {
            return self.walk_chain(next, visited, chain_length, terminating_root, depth + 1);
        }

        // No higher credential found. If a trust root is required and we
        // never reached one, the caller (`verify_credential_chain`) will
        // mark the result invalid.
        Ok(())
    }

    /// Checks if an identity has a specific credential type
    pub fn has_credential(&self, did: &str, credential_type: &TenzroCredentialType) -> Result<bool> {
        let identity = self.registry.resolve(did)?;
        Ok(identity
            .credentials
            .iter()
            .any(|c| &c.tenzro_type == credential_type && c.is_valid()))
    }

    /// Checks if an identity meets a minimum KYC tier (directly or via controller)
    pub fn meets_kyc_tier(&self, did: &str, required_tier: KycTier) -> Result<bool> {
        let identity = self.registry.resolve(did)?;

        match &identity.identity_data {
            IdentityData::Human { kyc_tier, .. } => Ok(*kyc_tier >= required_tier),
            IdentityData::Institution { kyb_tier, .. } => Ok(*kyb_tier >= required_tier),
            IdentityData::Machine { controller_did, .. } => {
                if let Some(ctrl) = controller_did {
                    let controller = self.registry.resolve(ctrl)?;
                    match &controller.identity_data {
                        IdentityData::Human { kyc_tier, .. } => Ok(*kyc_tier >= required_tier),
                        IdentityData::Institution { kyb_tier, .. } => Ok(*kyb_tier >= required_tier),
                        _ => Ok(false),
                    }
                } else {
                    // Autonomous machines don't have KYC
                    Ok(false)
                }
            }
        }
    }

    /// Validates that a machine identity can perform an operation
    pub fn validate_operation(
        &self,
        machine_did: &str,
        operation: &str,
        value: Option<u128>,
    ) -> Result<bool> {
        let identity = self.registry.resolve(machine_did)?;

        if identity.status != IdentityStatus::Active {
            return Ok(false);
        }

        match &identity.identity_data {
            IdentityData::Machine {
                delegation_scope, ..
            } => {
                if !delegation_scope.is_active() {
                    return Ok(false);
                }
                if !delegation_scope.is_operation_allowed(operation) {
                    return Ok(false);
                }
                if let Some(v) = value
                    && !delegation_scope.is_value_allowed(v)
                {
                    return Ok(false);
                }
                Ok(true)
            }
            IdentityData::Human { .. } | IdentityData::Institution { .. } => {
                // Humans and institutions act through their own wallet
                // signature, not a delegation scope.
                Ok(true)
            }
        }
    }
}

/// Verifies a credential's cryptographic proof against any AssertionMethod
/// public key on the issuer (CRITICAL #41 helper).
///
/// Returns true if at least one assertion key on the issuer can verify the
/// credential's proof. We try every assertion key because the issuer may
/// rotate keys without re-issuing every credential.
fn verify_credential_against_issuer(
    credential: &VerifiableCredential,
    issuer: &TenzroIdentity,
) -> bool {
    // Collect candidate keys: prefer AssertionMethod, fall back to any key
    // if none have an explicit purpose set.
    let assertion_keys: Vec<&[u8]> = issuer
        .public_keys
        .iter()
        .filter(|k| {
            k.purposes.is_empty() || k.purposes.contains(&KeyPurpose::AssertionMethod)
        })
        .map(|k| k.public_key.as_slice())
        .collect();

    if assertion_keys.is_empty() {
        return false;
    }

    for key in assertion_keys {
        match credential.verify_proof(key) {
            Ok(true) => return true,
            Ok(false) => continue,
            Err(_) => continue,
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::credential::CredentialProof;
    use crate::delegation::DelegationScope;
    use crate::identity::PublicKeyInfo;
    use tenzro_crypto::composite::InMemoryHybridSigner;
    use tenzro_crypto::pq::MlDsaSigningKey;
    use tenzro_crypto::signatures::{Ed25519SignerImpl, Signer};
    use tenzro_crypto::{KeyPair, KeyType};

    /// Build a fresh hybrid signer for revocation tests in this module.
    fn revocation_test_signer() -> InMemoryHybridSigner {
        let kp = KeyPair::generate(KeyType::Ed25519).unwrap();
        let classical = Ed25519SignerImpl::new(kp).unwrap();
        InMemoryHybridSigner::new(Box::new(classical), MlDsaSigningKey::generate())
    }

    #[tokio::test]
    async fn test_verify_human_trust_chain() {
        let registry = Arc::new(IdentityRegistry::new());
        let verifier = IdentityVerifier::new(registry.clone());

        let human = registry
            .register_human_with_fee(vec![1; 32], "Alice".to_string(), KycTier::Full)
            .await
            .unwrap()
            .identity;

        let result = verifier.verify_trust_chain(&human.did_string()).unwrap();
        assert!(result.valid);
        assert!(result.controller_did.is_none());
        assert!(result.issues.is_empty());
    }

    #[tokio::test]
    async fn test_verify_machine_trust_chain() {
        let registry = Arc::new(IdentityRegistry::new());
        let verifier = IdentityVerifier::new(registry.clone());

        let human = registry
            .register_human_with_fee(vec![1; 32], "Alice".to_string(), KycTier::Full)
            .await
            .unwrap()
            .identity;

        let machine = registry
            .register_machine_with_fee(
                &human.did_string(),
                vec![2; 32],
                vec!["inference".to_string()],
                DelegationScope::unrestricted(),
            )
            .await
            .unwrap()
            .identity;

        let result = verifier.verify_trust_chain(&machine.did_string()).unwrap();
        assert!(result.valid);
        assert_eq!(result.controller_did, Some(human.did_string()));
        assert_eq!(result.controller_kyc_tier, Some(KycTier::Full));
    }

    #[tokio::test]
    async fn test_verify_revoked_chain() {
        let registry = Arc::new(IdentityRegistry::new());
        let verifier = IdentityVerifier::new(registry.clone());

        let human = registry
            .register_human_with_fee(vec![1; 32], "Alice".to_string(), KycTier::Full)
            .await
            .unwrap()
            .identity;

        let signer = revocation_test_signer();
        registry
            .revoke(
                &human.did_string(),
                "test".to_string(),
                "admin".to_string(),
                &signer,
            )
            .unwrap();

        let result = verifier.verify_trust_chain(&human.did_string()).unwrap();
        assert!(!result.valid);
        assert!(!result.issues.is_empty());
    }

    #[tokio::test]
    async fn test_meets_kyc_tier() {
        let registry = Arc::new(IdentityRegistry::new());
        let verifier = IdentityVerifier::new(registry.clone());

        let human = registry
            .register_human_with_fee(vec![1; 32], "Alice".to_string(), KycTier::Enhanced)
            .await
            .unwrap()
            .identity;

        assert!(verifier.meets_kyc_tier(&human.did_string(), KycTier::Basic).unwrap());
        assert!(verifier.meets_kyc_tier(&human.did_string(), KycTier::Enhanced).unwrap());
        assert!(!verifier.meets_kyc_tier(&human.did_string(), KycTier::Full).unwrap());
    }

    #[tokio::test]
    async fn test_validate_operation() {
        let registry = Arc::new(IdentityRegistry::new());
        let verifier = IdentityVerifier::new(registry.clone());

        let human = registry
            .register_human_with_fee(vec![1; 32], "Alice".to_string(), KycTier::Full)
            .await
            .unwrap()
            .identity;

        let machine = registry
            .register_machine_with_fee(
                &human.did_string(),
                vec![2; 32],
                vec![],
                DelegationScope::unrestricted()
                    .with_max_transaction_value(10_000)
                    .with_allowed_operations(vec!["inference".to_string()]),
            )
            .await
            .unwrap()
            .identity;

        assert!(verifier
            .validate_operation(&machine.did_string(), "inference", Some(5_000))
            .unwrap());
        assert!(!verifier
            .validate_operation(&machine.did_string(), "admin", None)
            .unwrap());
        assert!(!verifier
            .validate_operation(&machine.did_string(), "inference", Some(20_000))
            .unwrap());
    }

    // ─── CRITICAL #41 trust-chain tests ──────────────────────────────────

    /// Helper that constructs a signed credential issued by `signer_id`
    /// to `subject_id`, embedded in the registry, where the issuer
    /// identity has the corresponding Ed25519 public key registered as
    /// an AssertionMethod.
    async fn build_chain_setup() -> (
        Arc<IdentityRegistry>,
        TenzroIdentity, // root
        TenzroIdentity, // intermediate
        TenzroIdentity, // leaf
        VerifiableCredential, // intermediate cred (signed by root)
        VerifiableCredential, // leaf cred (signed by intermediate)
    ) {
        let registry = Arc::new(IdentityRegistry::new());

        // Generate keys for root, intermediate, leaf
        let root_kp = KeyPair::generate(KeyType::Ed25519).unwrap();
        let intermediate_kp = KeyPair::generate(KeyType::Ed25519).unwrap();
        let leaf_kp = KeyPair::generate(KeyType::Ed25519).unwrap();

        let root_pub = root_kp.public_key().as_bytes().to_vec();
        let intermediate_pub = intermediate_kp.public_key().as_bytes().to_vec();
        let leaf_pub = leaf_kp.public_key().as_bytes().to_vec();

        // Register the three identities
        let root = registry
            .register_human_with_fee(root_pub.clone(), "Root CA".to_string(), KycTier::Full)
            .await
            .unwrap()
            .identity;
        let intermediate = registry
            .register_human_with_fee(
                intermediate_pub.clone(),
                "Intermediate".to_string(),
                KycTier::Enhanced,
            )
            .await
            .unwrap()
            .identity;
        let leaf = registry
            .register_human_with_fee(leaf_pub.clone(), "Leaf".to_string(), KycTier::Basic)
            .await
            .unwrap()
            .identity;

        // Patch the registered identities so their public_keys carry the
        // correct AssertionMethod role with the keypair we generated
        // above. (register_human_with_fee stores the bytes verbatim; we set the
        // purpose explicitly so verify_credential_against_issuer picks
        // them up.)
        for (did, key_bytes) in [
            (root.did_string(), &root_pub),
            (intermediate.did_string(), &intermediate_pub),
            (leaf.did_string(), &leaf_pub),
        ] {
            let mut id = registry.resolve(&did).unwrap();
            id.public_keys = vec![PublicKeyInfo {
                key_id: "key-1".to_string(),
                key_type: "Ed25519".to_string(),
                public_key: key_bytes.clone(),
                purposes: vec![KeyPurpose::AssertionMethod],
            }];
            registry.upsert_identity_for_test(id);
        }

        // Build intermediate cred: subject = intermediate, issuer = root
        let mut intermediate_cred = VerifiableCredential::new(
            TenzroCredentialType::KycAttestation,
            root.did_string(),
            intermediate.did_string(),
        );
        let msg = serde_json::to_vec(&intermediate_cred.credential_subject).unwrap();
        let signer = Ed25519SignerImpl::new(root_kp).unwrap();
        let sig = signer.sign(&msg).unwrap();
        intermediate_cred = intermediate_cred.with_proof(CredentialProof::new(
            "Ed25519Signature2020",
            format!("{}#key-1", root.did_string()),
            sig.to_bytes(),
        ));

        // Build leaf cred: subject = leaf, issuer = intermediate
        let mut leaf_cred = VerifiableCredential::new(
            TenzroCredentialType::KycAttestation,
            intermediate.did_string(),
            leaf.did_string(),
        );
        let msg = serde_json::to_vec(&leaf_cred.credential_subject).unwrap();
        let signer = Ed25519SignerImpl::new(intermediate_kp).unwrap();
        let sig = signer.sign(&msg).unwrap();
        leaf_cred = leaf_cred.with_proof(CredentialProof::new(
            "Ed25519Signature2020",
            format!("{}#key-1", intermediate.did_string()),
            sig.to_bytes(),
        ));

        // Stash credentials onto the identities so the recursive walker
        // can find them when it visits the issuer.
        let mut intermediate_with_cred = registry.resolve(&intermediate.did_string()).unwrap();
        intermediate_with_cred
            .credentials
            .push(intermediate_cred.clone());
        registry.upsert_identity_for_test(intermediate_with_cred);

        let mut leaf_with_cred = registry.resolve(&leaf.did_string()).unwrap();
        leaf_with_cred.credentials.push(leaf_cred.clone());
        registry.upsert_identity_for_test(leaf_with_cred);

        (registry, root, intermediate, leaf, intermediate_cred, leaf_cred)
    }

    #[tokio::test]
    async fn test_chain_terminates_at_trust_root() {
        let (registry, root, _intermediate, leaf, _ic, leaf_cred) = build_chain_setup().await;

        let verifier = IdentityVerifier::new(registry.clone())
            .with_trust_root(root.did_string())
            .require_trust_root(true);

        let result = verifier.verify_credential_chain(&leaf_cred, &leaf.did_string());
        assert!(result.valid, "chain should verify: {:?}", result.issues);
        assert_eq!(result.terminating_root, Some(root.did_string()));
        assert_eq!(result.chain_length, 2); // leaf -> intermediate -> root
    }

    #[tokio::test]
    async fn test_chain_rejected_when_no_trust_root_reached() {
        let (registry, _root, _intermediate, leaf, _ic, leaf_cred) = build_chain_setup().await;

        // Configure a trust root that does NOT match anything in the chain
        let verifier = IdentityVerifier::new(registry.clone())
            .with_trust_root("did:tenzro:human:not-anchored".to_string())
            .require_trust_root(true);

        let result = verifier.verify_credential_chain(&leaf_cred, &leaf.did_string());
        assert!(!result.valid);
        assert!(result.terminating_root.is_none());
    }

    #[tokio::test]
    async fn test_chain_rejected_when_issuer_revoked() {
        let (registry, root, intermediate, leaf, _ic, leaf_cred) = build_chain_setup().await;

        // Revoke the intermediate
        let signer = revocation_test_signer();
        registry
            .revoke(
                &intermediate.did_string(),
                "compromised".to_string(),
                "admin".to_string(),
                &signer,
            )
            .unwrap();

        let verifier = IdentityVerifier::new(registry.clone())
            .with_trust_root(root.did_string())
            .require_trust_root(true);

        let result = verifier.verify_credential_chain(&leaf_cred, &leaf.did_string());
        assert!(!result.valid);
        assert!(
            result
                .issues
                .iter()
                .any(|i| i.contains("Revoked") || i.contains("revoked")),
            "expected revocation issue, got {:?}",
            result.issues
        );
    }

    #[tokio::test]
    async fn test_chain_rejected_when_signature_invalid() {
        let (registry, root, _intermediate, leaf, _ic, mut leaf_cred) =
            build_chain_setup().await;

        // Tamper with the proof bytes
        if let Some(proof) = leaf_cred.proof.as_mut() {
            proof.proof_value[0] ^= 0xFF;
        }

        let verifier = IdentityVerifier::new(registry.clone())
            .with_trust_root(root.did_string())
            .require_trust_root(true);

        let result = verifier.verify_credential_chain(&leaf_cred, &leaf.did_string());
        assert!(!result.valid);
        assert!(
            result
                .issues
                .iter()
                .any(|i| i.contains("signature")),
            "expected signature issue, got {:?}",
            result.issues
        );
    }

    #[tokio::test]
    async fn test_chain_rejects_unsigned_credential() {
        let (registry, root, _intermediate, leaf, _ic, mut leaf_cred) =
            build_chain_setup().await;

        leaf_cred.proof = None;

        let verifier = IdentityVerifier::new(registry.clone())
            .with_trust_root(root.did_string())
            .require_trust_root(true);

        let result = verifier.verify_credential_chain(&leaf_cred, &leaf.did_string());
        assert!(!result.valid);
        assert!(
            result
                .issues
                .iter()
                .any(|i| i.contains("no cryptographic proof")),
            "expected no-proof issue, got {:?}",
            result.issues
        );
    }

    #[tokio::test]
    async fn test_chain_cycle_detected() {
        let registry = Arc::new(IdentityRegistry::new());

        let kp_a = KeyPair::generate(KeyType::Ed25519).unwrap();
        let kp_b = KeyPair::generate(KeyType::Ed25519).unwrap();
        let pub_a = kp_a.public_key().as_bytes().to_vec();
        let pub_b = kp_b.public_key().as_bytes().to_vec();

        let id_a = registry
            .register_human_with_fee(pub_a.clone(), "A".to_string(), KycTier::Full)
            .await
            .unwrap()
            .identity;
        let id_b = registry
            .register_human_with_fee(pub_b.clone(), "B".to_string(), KycTier::Full)
            .await
            .unwrap()
            .identity;

        for (did, key) in [
            (id_a.did_string(), &pub_a),
            (id_b.did_string(), &pub_b),
        ] {
            let mut id = registry.resolve(&did).unwrap();
            id.public_keys = vec![PublicKeyInfo {
                key_id: "key-1".to_string(),
                key_type: "Ed25519".to_string(),
                public_key: key.clone(),
                purposes: vec![KeyPurpose::AssertionMethod],
            }];
            registry.upsert_identity_for_test(id);
        }

        // Cred 1: A issues to B (signed by A)
        let mut cred_a_to_b = VerifiableCredential::new(
            TenzroCredentialType::KycAttestation,
            id_a.did_string(),
            id_b.did_string(),
        );
        let msg = serde_json::to_vec(&cred_a_to_b.credential_subject).unwrap();
        let sig = Ed25519SignerImpl::new(kp_a).unwrap().sign(&msg).unwrap();
        cred_a_to_b = cred_a_to_b.with_proof(CredentialProof::new(
            "Ed25519Signature2020",
            format!("{}#key-1", id_a.did_string()),
            sig.to_bytes(),
        ));

        // Cred 2: B issues to A (signed by B)
        let mut cred_b_to_a = VerifiableCredential::new(
            TenzroCredentialType::KycAttestation,
            id_b.did_string(),
            id_a.did_string(),
        );
        let msg = serde_json::to_vec(&cred_b_to_a.credential_subject).unwrap();
        let sig = Ed25519SignerImpl::new(kp_b).unwrap().sign(&msg).unwrap();
        cred_b_to_a = cred_b_to_a.with_proof(CredentialProof::new(
            "Ed25519Signature2020",
            format!("{}#key-1", id_b.did_string()),
            sig.to_bytes(),
        ));

        // Stash creds — A holds cred_b_to_a, B holds cred_a_to_b.
        let mut a_with = registry.resolve(&id_a.did_string()).unwrap();
        a_with.credentials.push(cred_b_to_a.clone());
        registry.upsert_identity_for_test(a_with);

        let mut b_with = registry.resolve(&id_b.did_string()).unwrap();
        b_with.credentials.push(cred_a_to_b.clone());
        registry.upsert_identity_for_test(b_with);

        // Verifier with no anchoring trust root → cycle is the only termination
        let verifier = IdentityVerifier::new(registry.clone())
            .with_trust_root("did:tenzro:human:nonexistent".to_string())
            .require_trust_root(true);

        // Walk starting from B's leaf cred. Expected: A→B→A cycle detected.
        let result = verifier.verify_credential_chain(&cred_a_to_b, &id_b.did_string());
        assert!(!result.valid);
        assert!(
            result.issues.iter().any(|i| i.contains("cycle")),
            "expected cycle issue, got {:?}",
            result.issues
        );
    }

    #[tokio::test]
    async fn test_chain_max_depth_enforced() {
        let (registry, root, _intermediate, leaf, _ic, leaf_cred) = build_chain_setup().await;

        let verifier = IdentityVerifier::new(registry.clone())
            .with_trust_root(root.did_string())
            .with_max_chain_depth(1) // only 1 hop allowed
            .require_trust_root(true);

        let result = verifier.verify_credential_chain(&leaf_cred, &leaf.did_string());
        assert!(!result.valid);
        assert!(
            result.issues.iter().any(|i| i.contains("max depth")),
            "expected depth issue, got {:?}",
            result.issues
        );
    }

    #[tokio::test]
    async fn test_rootless_mode_no_trust_roots_accepts_self_issued() {
        let registry = Arc::new(IdentityRegistry::new());
        let verifier = IdentityVerifier::new(registry.clone());

        let kp = KeyPair::generate(KeyType::Ed25519).unwrap();
        let pub_bytes = kp.public_key().as_bytes().to_vec();

        let id = registry
            .register_human_with_fee(pub_bytes.clone(), "Self".to_string(), KycTier::Full)
            .await
            .unwrap()
            .identity;
        let mut idmut = registry.resolve(&id.did_string()).unwrap();
        idmut.public_keys = vec![PublicKeyInfo {
            key_id: "key-1".to_string(),
            key_type: "Ed25519".to_string(),
            public_key: pub_bytes,
            purposes: vec![KeyPurpose::AssertionMethod],
        }];
        registry.upsert_identity_for_test(idmut);

        let mut cred = VerifiableCredential::new(
            TenzroCredentialType::KycAttestation,
            id.did_string(),
            id.did_string(),
        );
        let msg = serde_json::to_vec(&cred.credential_subject).unwrap();
        let sig = Ed25519SignerImpl::new(kp).unwrap().sign(&msg).unwrap();
        cred = cred.with_proof(CredentialProof::new(
            "Ed25519",
            format!("{}#key-1", id.did_string()),
            sig.to_bytes(),
        ));

        // No trust roots configured → rootless mode → self-issued is terminal
        let result = verifier.verify_credential_chain(&cred, &id.did_string());
        assert!(result.valid, "rootless mode self-issued should pass: {:?}", result.issues);
        assert_eq!(result.chain_length, 1);
    }

    #[tokio::test]
    async fn test_trust_chain_aggregates_chain_results() {
        let (registry, root, _intermediate, leaf, _ic, leaf_cred) = build_chain_setup().await;

        let verifier = IdentityVerifier::new(registry.clone())
            .with_trust_root(root.did_string())
            .require_trust_root(true);

        // Resolve leaf and verify its trust chain (it has 1 credential)
        let result = verifier.verify_trust_chain(&leaf.did_string()).unwrap();
        assert_eq!(result.chain_results.len(), 1);
        assert_eq!(result.verified_chains, 1);
        assert!(result.valid, "leaf chain should verify: {:?}", result.issues);
        // Sanity check the embedded chain result
        assert_eq!(
            result.chain_results[0].terminating_root,
            Some(root.did_string())
        );
        // Avoid unused warning on leaf_cred
        let _ = leaf_cred;
    }
}