scope-bca-core 0.8.6

Scope Blockchain Analysis - Core library for blockchain data analysis
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
//! # Smart Contract Analysis Module
//!
//! Provides comprehensive contract analysis capabilities for EVM-compatible
//! blockchain contracts, including:
//!
//! - **Source code retrieval** from block explorers (Etherscan, Sourcify)
//! - **ABI parsing** and function signature decoding (4byte.directory)
//! - **Proxy pattern detection** (EIP-1967, EIP-1822, UUPS, Transparent)
//! - **Access control mapping** (Ownable, AccessControl, Roles)
//! - **Vulnerability heuristics** (reentrancy, unchecked calls, selfdestruct, etc.)
//! - **DeFi protocol checks** (oracle manipulation, flash loans, lending, swaps)
//! - **External intelligence** (GitHub linking, audit report discovery)
//!
//! ## Architecture
//!
//! The module is organized into submodules that build on each other:
//!
//! ```text
//! contract::source       → Raw data retrieval (source, ABI, bytecode metadata)
//! contract::abi          → Function signature lookup and calldata decoding
//! contract::proxy        → Proxy pattern identification and implementation resolution
//! contract::access       → Access control and privilege mapping
//! contract::vulnerability → Security heuristic scanning
//! contract::defi         → DeFi-specific protocol analysis
//! contract::external     → GitHub repo linking and audit report discovery
//! ```

pub mod abi;
pub mod access;
pub mod defi;
pub mod external;
pub mod proxy;
pub mod source;
pub mod vulnerability;

use serde::{Deserialize, Serialize};

/// Complete contract analysis result aggregating all submodule outputs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractAnalysis {
    /// Contract address analyzed.
    pub address: String,
    /// Chain the contract is deployed on.
    pub chain: String,
    /// Whether the contract source is verified on the block explorer.
    pub is_verified: bool,
    /// Contract source information (if verified).
    pub source_info: Option<source::ContractSource>,
    /// Proxy detection results.
    pub proxy_info: Option<proxy::ProxyInfo>,
    /// Access control mapping.
    pub access_control: Option<access::AccessControlMap>,
    /// Vulnerability scan findings.
    pub vulnerabilities: Vec<vulnerability::VulnerabilityFinding>,
    /// DeFi protocol analysis.
    pub defi_analysis: Option<defi::DefiAnalysis>,
    /// External intelligence (GitHub, audits).
    pub external_info: Option<external::ExternalInfo>,
    /// Overall security score (0-100, higher = safer).
    pub security_score: u32,
    /// Human-readable security summary.
    pub security_summary: String,
}

/// Run a full contract analysis pipeline on an address.
///
/// Orchestrates all submodules: source retrieval, proxy detection,
/// access control, vulnerability scanning, DeFi checks, and external intel.
pub async fn analyze_contract(
    address: &str,
    chain: &str,
    client: &dyn crate::chains::ChainClient,
    http_client: &dyn crate::http::HttpClient,
) -> crate::error::Result<ContractAnalysis> {
    // Step 1: Verify it's actually a contract
    let code = client.get_code(address).await?;
    if code.is_empty() || code == "0x" {
        return Err(crate::error::ScopeError::Chain(format!(
            "{} is an EOA (externally owned account), not a contract",
            address
        )));
    }

    // Step 2: Fetch source code and ABI
    let source_result = source::fetch_contract_source(address, chain, http_client).await;
    let source_info = source_result.ok();
    let is_verified = source_info.is_some();

    // Step 3: Proxy detection (uses bytecode + source metadata)
    let proxy_info = proxy::detect_proxy(
        address,
        chain,
        &code,
        source_info.as_ref(),
        client,
        http_client,
    )
    .await
    .ok();

    // Step 4: Access control analysis (requires source)
    let access_control = source_info.as_ref().map(access::analyze_access_control);

    // Step 5: Vulnerability scan (requires source + ABI context)
    let vulnerabilities = if let Some(src) = source_info.as_ref() {
        vulnerability::scan_vulnerabilities(src)
    } else {
        vulnerability::scan_bytecode_only(&code)
    };

    // Step 6: DeFi protocol checks
    let defi_analysis = source_info.as_ref().map(defi::analyze_defi_patterns);

    // Step 7: External intelligence
    let external_info =
        external::gather_external_info(address, chain, source_info.as_ref(), http_client)
            .await
            .ok();

    // Compute security score
    let security_score = compute_security_score(
        is_verified,
        &proxy_info,
        &access_control,
        &vulnerabilities,
        &defi_analysis,
        &external_info,
    );

    let security_summary = generate_security_summary(
        is_verified,
        &proxy_info,
        &access_control,
        &vulnerabilities,
        security_score,
    );

    Ok(ContractAnalysis {
        address: address.to_string(),
        chain: chain.to_string(),
        is_verified,
        source_info,
        proxy_info,
        access_control,
        vulnerabilities,
        defi_analysis,
        external_info,
        security_score,
        security_summary,
    })
}

/// Compute an overall security score (0-100) from analysis components.
fn compute_security_score(
    is_verified: bool,
    proxy_info: &Option<proxy::ProxyInfo>,
    access_control: &Option<access::AccessControlMap>,
    vulnerabilities: &[vulnerability::VulnerabilityFinding],
    defi_analysis: &Option<defi::DefiAnalysis>,
    external_info: &Option<external::ExternalInfo>,
) -> u32 {
    let mut score: i32 = 50; // Base score

    // Verification bonus
    if is_verified {
        score += 15;
    } else {
        score -= 20;
    }

    // Proxy considerations
    if let Some(proxy) = proxy_info
        && proxy.is_proxy
    {
        score -= 5; // Proxies add complexity
        if proxy.admin_address.is_some() {
            score += 3; // At least admin is identifiable
        }
    }

    // Access control
    if let Some(ac) = access_control {
        if ac.has_renounced_ownership {
            score += 10;
        }
        if ac.has_role_based_access {
            score += 5;
        }
        if ac.uses_tx_origin {
            score -= 15;
        }
    }

    // Vulnerabilities
    for vuln in vulnerabilities {
        match vuln.severity {
            vulnerability::Severity::Critical => score -= 20,
            vulnerability::Severity::High => score -= 12,
            vulnerability::Severity::Medium => score -= 6,
            vulnerability::Severity::Low => score -= 2,
            vulnerability::Severity::Informational => score -= 1,
        }
    }

    // DeFi risk factors
    if let Some(defi) = defi_analysis {
        if defi.has_oracle_dependency {
            score -= 5;
        }
        if defi.has_flash_loan_risk {
            score -= 8;
        }
    }

    // External validation
    if let Some(ext) = external_info {
        if !ext.audit_reports.is_empty() {
            score += 15;
        }
        if ext.github_repo.is_some() {
            score += 5;
        }
    }

    score.clamp(0, 100) as u32
}

/// Generate a human-readable security summary.
fn generate_security_summary(
    is_verified: bool,
    proxy_info: &Option<proxy::ProxyInfo>,
    access_control: &Option<access::AccessControlMap>,
    vulnerabilities: &[vulnerability::VulnerabilityFinding],
    score: u32,
) -> String {
    let mut parts = Vec::new();

    // Verification status
    if is_verified {
        parts.push("Source code is verified on the block explorer.".to_string());
    } else {
        parts.push(
            "WARNING: Source code is NOT verified — unable to perform source-level analysis."
                .to_string(),
        );
    }

    // Proxy status
    if let Some(proxy) = proxy_info
        && proxy.is_proxy
    {
        parts.push(format!(
            "Contract is a {} proxy{}.",
            proxy.proxy_type,
            proxy
                .implementation_address
                .as_ref()
                .map(|a| format!(" pointing to {}", a))
                .unwrap_or_default()
        ));
    }

    // Access control
    if let Some(ac) = access_control {
        if ac.has_renounced_ownership {
            parts.push("Ownership has been renounced.".to_string());
        } else if !ac.privileged_functions.is_empty() {
            parts.push(format!(
                "{} privileged function(s) found with owner/admin restrictions.",
                ac.privileged_functions.len()
            ));
        }
        if ac.uses_tx_origin {
            parts.push("DANGER: Uses tx.origin for authorization.".to_string());
        }
    }

    // Vulnerability summary
    let critical = vulnerabilities
        .iter()
        .filter(|v| v.severity == vulnerability::Severity::Critical)
        .count();
    let high = vulnerabilities
        .iter()
        .filter(|v| v.severity == vulnerability::Severity::High)
        .count();
    if critical > 0 || high > 0 {
        parts.push(format!(
            "Found {} critical and {} high severity issue(s).",
            critical, high
        ));
    } else if vulnerabilities.is_empty() {
        parts.push("No vulnerability heuristics triggered.".to_string());
    } else {
        parts.push(format!(
            "{} lower-severity finding(s) detected.",
            vulnerabilities.len()
        ));
    }

    // Score assessment
    let rating = match score {
        80..=100 => "GOOD",
        60..=79 => "MODERATE",
        40..=59 => "CAUTION",
        20..=39 => "HIGH RISK",
        _ => "CRITICAL RISK",
    };
    parts.push(format!("Security Score: {}/100 ({})", score, rating));

    parts.join(" ")
}

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

    #[test]
    fn test_compute_security_score_verified() {
        let score = compute_security_score(true, &None, &None, &[], &None, &None);
        assert_eq!(score, 65); // 50 base + 15 verified
    }

    #[test]
    fn test_compute_security_score_unverified() {
        let score = compute_security_score(false, &None, &None, &[], &None, &None);
        assert_eq!(score, 30); // 50 base - 20 unverified
    }

    #[test]
    fn test_compute_security_score_with_critical_vuln() {
        let vulns = vec![vulnerability::VulnerabilityFinding {
            id: "TEST-001".to_string(),
            title: "Test".to_string(),
            severity: vulnerability::Severity::Critical,
            category: vulnerability::VulnCategory::Reentrancy,
            description: "Test finding".to_string(),
            source_location: None,
            recommendation: "Fix it".to_string(),
        }];
        let score = compute_security_score(true, &None, &None, &vulns, &None, &None);
        assert_eq!(score, 45); // 65 - 20 critical
    }

    #[test]
    fn test_security_summary_unverified() {
        let summary = generate_security_summary(false, &None, &None, &[], 30);
        assert!(summary.contains("NOT verified"));
        assert!(summary.contains("30/100"));
    }

    #[test]
    fn test_score_clamped_to_valid_range() {
        let many_vulns: Vec<_> = (0..10)
            .map(|i| vulnerability::VulnerabilityFinding {
                id: format!("V-{}", i),
                title: "Critical".to_string(),
                severity: vulnerability::Severity::Critical,
                category: vulnerability::VulnCategory::Reentrancy,
                description: "Bad".to_string(),
                source_location: None,
                recommendation: "Fix".to_string(),
            })
            .collect();
        let score = compute_security_score(false, &None, &None, &many_vulns, &None, &None);
        assert_eq!(score, 0); // Clamped to 0
    }

    #[test]
    fn test_compute_security_score_proxy_with_admin() {
        use proxy::ProxyInfo;

        let proxy = Some(ProxyInfo {
            is_proxy: true,
            admin_address: Some("0xadmin".to_string()),
            proxy_type: "EIP-1967".to_string(),
            implementation_address: None,
            beacon_address: None,
            details: vec![],
        });
        let score = compute_security_score(true, &proxy, &None, &[], &None, &None);
        // 50 base + 15 verified - 5 proxy + 3 admin = 63
        assert_eq!(score, 63);
    }

    #[test]
    fn test_compute_security_score_access_control_renounced_and_role_based() {
        use access::{AccessControlMap, AuthAnalysis};

        let ac = Some(AccessControlMap {
            ownership_pattern: None,
            has_renounced_ownership: true,
            has_role_based_access: true,
            uses_tx_origin: false,
            tx_origin_locations: vec![],
            modifiers: vec![],
            privileged_functions: vec![],
            roles: vec![],
            auth_analysis: AuthAnalysis {
                msg_sender_checks: 0,
                tx_origin_checks: 0,
                has_origin_sender_comparison: false,
                summary: String::new(),
            },
        });
        let score = compute_security_score(true, &None, &ac, &[], &None, &None);
        // 50 base + 15 verified + 10 renounced + 5 role-based = 80
        assert_eq!(score, 80);
    }

    #[test]
    fn test_compute_security_score_tx_origin() {
        use access::{AccessControlMap, AuthAnalysis};

        let ac = Some(AccessControlMap {
            ownership_pattern: None,
            has_renounced_ownership: false,
            has_role_based_access: false,
            uses_tx_origin: true,
            tx_origin_locations: vec![],
            modifiers: vec![],
            privileged_functions: vec![],
            roles: vec![],
            auth_analysis: AuthAnalysis {
                msg_sender_checks: 0,
                tx_origin_checks: 1,
                has_origin_sender_comparison: false,
                summary: String::new(),
            },
        });
        let score = compute_security_score(true, &None, &ac, &[], &None, &None);
        // 50 base + 15 verified - 15 tx.origin = 50
        assert_eq!(score, 50);
    }

    #[test]
    fn test_compute_security_score_defi_oracle_and_flash_loan() {
        use defi::{DefiAnalysis, ProtocolType};

        let defi = Some(DefiAnalysis {
            protocol_type: ProtocolType::Lending,
            has_oracle_dependency: true,
            oracle_info: vec![],
            has_flash_loan_risk: true,
            flash_loan_info: vec![],
            dex_integrations: vec![],
            lending_patterns: vec![],
            token_standards: vec![],
            staking_patterns: vec![],
            risk_factors: vec![],
        });
        let score = compute_security_score(true, &None, &None, &[], &defi, &None);
        // 50 base + 15 verified - 5 oracle - 8 flash loan = 52
        assert_eq!(score, 52);
    }

    #[test]
    fn test_compute_security_score_external_audit_and_github() {
        use external::{AuditReport, ExternalInfo};

        let ext = Some(ExternalInfo {
            github_repo: Some("https://github.com/org/repo".to_string()),
            audit_reports: vec![AuditReport {
                auditor: "Trail of Bits".to_string(),
                url: "https://example.com/report".to_string(),
                date: Some("2024-01-01".to_string()),
                scope: "Full".to_string(),
            }],
            sourcify_verified: None,
            deployer: None,
            explorer_url: String::new(),
            metadata: vec![],
        });
        let score = compute_security_score(true, &None, &None, &[], &None, &ext);
        // 50 base + 15 verified + 15 audits + 5 github = 85
        assert_eq!(score, 85);
    }

    #[test]
    fn test_compute_security_score_clamped_to_100() {
        use access::{AccessControlMap, AuthAnalysis};
        use external::{AuditReport, ExternalInfo};

        let ac = Some(AccessControlMap {
            ownership_pattern: None,
            has_renounced_ownership: true,
            has_role_based_access: true,
            uses_tx_origin: false,
            tx_origin_locations: vec![],
            modifiers: vec![],
            privileged_functions: vec![],
            roles: vec![],
            auth_analysis: AuthAnalysis {
                msg_sender_checks: 0,
                tx_origin_checks: 0,
                has_origin_sender_comparison: false,
                summary: String::new(),
            },
        });
        let ext = Some(ExternalInfo {
            github_repo: Some("https://github.com/org/repo".to_string()),
            audit_reports: vec![AuditReport {
                auditor: "ToB".to_string(),
                url: "https://example.com".to_string(),
                date: None,
                scope: "Full".to_string(),
            }],
            sourcify_verified: None,
            deployer: None,
            explorer_url: String::new(),
            metadata: vec![],
        });
        let score = compute_security_score(true, &None, &ac, &[], &None, &ext);
        // Would be 50+15+10+5+15+5 = 100, clamped to 100
        assert_eq!(score, 100);
    }

    #[test]
    fn test_generate_security_summary_proxy_implementation() {
        use proxy::ProxyInfo;

        let proxy = Some(ProxyInfo {
            is_proxy: true,
            proxy_type: "EIP-1967".to_string(),
            implementation_address: Some("0x1234567890123456789012345678901234567890".to_string()),
            admin_address: None,
            beacon_address: None,
            details: vec![],
        });
        let summary = generate_security_summary(true, &proxy, &None, &[], 70);
        assert!(summary.contains("pointing to 0x1234567890123456789012345678901234567890"));
    }

    #[test]
    fn test_generate_security_summary_verified_no_vulns() {
        let summary = generate_security_summary(true, &None, &None, &[], 80);
        assert!(summary.contains("No vulnerability"));
    }

    #[test]
    fn test_generate_security_summary_low_severity_vulns() {
        let vulns = vec![vulnerability::VulnerabilityFinding {
            id: "L-001".to_string(),
            title: "Low".to_string(),
            severity: vulnerability::Severity::Low,
            category: vulnerability::VulnCategory::LogicError,
            description: "Minor".to_string(),
            source_location: None,
            recommendation: "Consider".to_string(),
        }];
        let summary = generate_security_summary(true, &None, &None, &vulns, 75);
        assert!(summary.contains("lower-severity"));
    }

    #[test]
    fn test_generate_security_summary_access_control_privileged_and_tx_origin() {
        use access::{
            AccessControlMap, AuthAnalysis, PrivilegeRisk, PrivilegedFunction, SourceLocation,
        };

        let ac = Some(AccessControlMap {
            ownership_pattern: Some("Ownable".to_string()),
            has_renounced_ownership: false,
            has_role_based_access: false,
            uses_tx_origin: true,
            tx_origin_locations: vec![SourceLocation {
                file: "Contract.sol".to_string(),
                line: 10,
                snippet: "require(tx.origin == owner)".to_string(),
            }],
            modifiers: vec![],
            privileged_functions: vec![PrivilegedFunction {
                name: "withdraw".to_string(),
                modifiers: vec!["onlyOwner".to_string()],
                capability: "drain funds".to_string(),
                risk: PrivilegeRisk::Critical,
            }],
            roles: vec![],
            auth_analysis: AuthAnalysis {
                msg_sender_checks: 0,
                tx_origin_checks: 1,
                has_origin_sender_comparison: false,
                summary: String::new(),
            },
        });
        let summary = generate_security_summary(true, &None, &ac, &[], 50);
        assert!(summary.contains("privileged function"));
        assert!(summary.contains("tx.origin"));
    }

    #[test]
    fn test_generate_security_summary_score_ratings() {
        assert!(generate_security_summary(true, &None, &None, &[], 85).contains("GOOD"));
        assert!(generate_security_summary(true, &None, &None, &[], 70).contains("MODERATE"));
        assert!(generate_security_summary(true, &None, &None, &[], 50).contains("CAUTION"));
        assert!(generate_security_summary(true, &None, &None, &[], 30).contains("HIGH RISK"));
        assert!(generate_security_summary(true, &None, &None, &[], 15).contains("CRITICAL RISK"));
    }

    #[test]
    fn test_contract_analysis_struct_construction() {
        let analysis = ContractAnalysis {
            address: "0x123".to_string(),
            chain: "ethereum".to_string(),
            is_verified: true,
            source_info: None,
            proxy_info: None,
            access_control: None,
            vulnerabilities: vec![],
            defi_analysis: None,
            external_info: None,
            security_score: 75,
            security_summary: "Test summary".to_string(),
        };
        assert_eq!(analysis.address, "0x123");
        assert_eq!(analysis.chain, "ethereum");
        assert!(analysis.is_verified);
        assert_eq!(analysis.security_score, 75);
        assert!(analysis.security_summary.contains("Test"));
    }

    #[test]
    fn test_contract_analysis_serialization_roundtrip() {
        let analysis = ContractAnalysis {
            address: "0xabc".to_string(),
            chain: "polygon".to_string(),
            is_verified: false,
            source_info: None,
            proxy_info: None,
            access_control: None,
            vulnerabilities: vec![],
            defi_analysis: None,
            external_info: None,
            security_score: 42,
            security_summary: "Summary".to_string(),
        };
        let json = serde_json::to_string(&analysis).unwrap();
        let restored: ContractAnalysis = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.address, analysis.address);
        assert_eq!(restored.security_score, analysis.security_score);
    }

    #[test]
    fn test_compute_security_score_proxy_without_admin() {
        use proxy::ProxyInfo;

        let proxy = Some(ProxyInfo {
            is_proxy: true,
            admin_address: None,
            proxy_type: "EIP-1967".to_string(),
            implementation_address: Some("0ximpl".to_string()),
            beacon_address: None,
            details: vec![],
        });
        let score = compute_security_score(true, &proxy, &None, &[], &None, &None);
        // 50 base + 15 verified - 5 proxy (no admin bonus) = 60
        assert_eq!(score, 60);
    }

    #[test]
    fn test_compute_security_score_proxy_info_not_proxy() {
        use proxy::ProxyInfo;

        let proxy = Some(ProxyInfo {
            is_proxy: false,
            admin_address: None,
            proxy_type: "None".to_string(),
            implementation_address: None,
            beacon_address: None,
            details: vec![],
        });
        let score = compute_security_score(true, &proxy, &None, &[], &None, &None);
        assert_eq!(score, 65); // No proxy penalty when is_proxy is false
    }

    #[test]
    fn test_compute_security_score_severity_high() {
        let vulns = vec![vulnerability::VulnerabilityFinding {
            id: "H-001".to_string(),
            title: "High".to_string(),
            severity: vulnerability::Severity::High,
            category: vulnerability::VulnCategory::AccessControl,
            description: "High finding".to_string(),
            source_location: None,
            recommendation: "Fix".to_string(),
        }];
        let score = compute_security_score(true, &None, &None, &vulns, &None, &None);
        assert_eq!(score, 53); // 65 - 12
    }

    #[test]
    fn test_compute_security_score_severity_medium() {
        let vulns = vec![vulnerability::VulnerabilityFinding {
            id: "M-001".to_string(),
            title: "Medium".to_string(),
            severity: vulnerability::Severity::Medium,
            category: vulnerability::VulnCategory::LogicError,
            description: "Medium finding".to_string(),
            source_location: None,
            recommendation: "Fix".to_string(),
        }];
        let score = compute_security_score(true, &None, &None, &vulns, &None, &None);
        assert_eq!(score, 59); // 65 - 6
    }

    #[test]
    fn test_compute_security_score_severity_low() {
        let vulns = vec![vulnerability::VulnerabilityFinding {
            id: "L-001".to_string(),
            title: "Low".to_string(),
            severity: vulnerability::Severity::Low,
            category: vulnerability::VulnCategory::UncheckedCall,
            description: "Low finding".to_string(),
            source_location: None,
            recommendation: "Fix".to_string(),
        }];
        let score = compute_security_score(true, &None, &None, &vulns, &None, &None);
        assert_eq!(score, 63); // 65 - 2
    }

    #[test]
    fn test_compute_security_score_severity_informational() {
        let vulns = vec![vulnerability::VulnerabilityFinding {
            id: "I-001".to_string(),
            title: "Info".to_string(),
            severity: vulnerability::Severity::Informational,
            category: vulnerability::VulnCategory::Informational,
            description: "Info finding".to_string(),
            source_location: None,
            recommendation: "Fix".to_string(),
        }];
        let score = compute_security_score(true, &None, &None, &vulns, &None, &None);
        assert_eq!(score, 64); // 65 - 1
    }

    #[test]
    fn test_compute_security_score_defi_oracle_only() {
        use defi::{DefiAnalysis, ProtocolType};

        let defi = Some(DefiAnalysis {
            protocol_type: ProtocolType::DEX,
            has_oracle_dependency: true,
            oracle_info: vec![],
            has_flash_loan_risk: false,
            flash_loan_info: vec![],
            dex_integrations: vec![],
            lending_patterns: vec![],
            token_standards: vec![],
            staking_patterns: vec![],
            risk_factors: vec![],
        });
        let score = compute_security_score(true, &None, &None, &[], &defi, &None);
        assert_eq!(score, 60); // 65 - 5
    }

    #[test]
    fn test_compute_security_score_defi_flash_loan_only() {
        use defi::{DefiAnalysis, ProtocolType};

        let defi = Some(DefiAnalysis {
            protocol_type: ProtocolType::Lending,
            has_oracle_dependency: false,
            oracle_info: vec![],
            has_flash_loan_risk: true,
            flash_loan_info: vec![],
            dex_integrations: vec![],
            lending_patterns: vec![],
            token_standards: vec![],
            staking_patterns: vec![],
            risk_factors: vec![],
        });
        let score = compute_security_score(true, &None, &None, &[], &defi, &None);
        assert_eq!(score, 57); // 65 - 8
    }

    #[test]
    fn test_compute_security_score_external_github_only() {
        use external::ExternalInfo;

        let ext = Some(ExternalInfo {
            github_repo: Some("https://github.com/org/repo".to_string()),
            audit_reports: vec![],
            sourcify_verified: None,
            deployer: None,
            explorer_url: String::new(),
            metadata: vec![],
        });
        let score = compute_security_score(true, &None, &None, &[], &None, &ext);
        assert_eq!(score, 70); // 65 + 5
    }

    #[test]
    fn test_compute_security_score_external_audit_only() {
        use external::{AuditReport, ExternalInfo};

        let ext = Some(ExternalInfo {
            github_repo: None,
            audit_reports: vec![AuditReport {
                auditor: "Auditor".to_string(),
                url: "https://audit.com".to_string(),
                date: None,
                scope: "Full".to_string(),
            }],
            sourcify_verified: None,
            deployer: None,
            explorer_url: String::new(),
            metadata: vec![],
        });
        let score = compute_security_score(true, &None, &None, &[], &None, &ext);
        assert_eq!(score, 80); // 65 + 15
    }

    #[test]
    fn test_generate_security_summary_proxy_no_implementation() {
        use proxy::ProxyInfo;

        let proxy = Some(ProxyInfo {
            is_proxy: true,
            proxy_type: "EIP-1967".to_string(),
            implementation_address: None,
            admin_address: None,
            beacon_address: None,
            details: vec![],
        });
        let summary = generate_security_summary(true, &proxy, &None, &[], 65);
        assert!(summary.contains("EIP-1967"));
        assert!(summary.contains("proxy"));
        assert!(!summary.contains("pointing to"));
    }

    #[test]
    fn test_generate_security_summary_renounced_only() {
        use access::{AccessControlMap, AuthAnalysis};

        let ac = Some(AccessControlMap {
            ownership_pattern: None,
            has_renounced_ownership: true,
            has_role_based_access: false,
            uses_tx_origin: false,
            tx_origin_locations: vec![],
            modifiers: vec![],
            privileged_functions: vec![],
            roles: vec![],
            auth_analysis: AuthAnalysis {
                msg_sender_checks: 0,
                tx_origin_checks: 0,
                has_origin_sender_comparison: false,
                summary: String::new(),
            },
        });
        let summary = generate_security_summary(true, &None, &ac, &[], 80);
        assert!(summary.contains("Ownership has been renounced"));
    }

    #[test]
    fn test_generate_security_summary_critical_and_high_vulns() {
        let vulns = vec![
            vulnerability::VulnerabilityFinding {
                id: "C-001".to_string(),
                title: "Critical".to_string(),
                severity: vulnerability::Severity::Critical,
                category: vulnerability::VulnCategory::Reentrancy,
                description: "Critical".to_string(),
                source_location: None,
                recommendation: "Fix".to_string(),
            },
            vulnerability::VulnerabilityFinding {
                id: "H-001".to_string(),
                title: "High".to_string(),
                severity: vulnerability::Severity::High,
                category: vulnerability::VulnCategory::AccessControl,
                description: "High".to_string(),
                source_location: None,
                recommendation: "Fix".to_string(),
            },
        ];
        let summary = generate_security_summary(true, &None, &None, &vulns, 40);
        assert!(summary.contains("critical"));
        assert!(summary.contains("high"));
        assert!(summary.contains("1 critical"));
        assert!(summary.contains("1 high"));
    }
}