Skip to main content

kya_validator/
lib.rs

1#![allow(unsafe_op_in_unsafe_fn)]
2
3#[cfg(feature = "python")]
4use pyo3::exceptions::PyValueError;
5#[cfg(feature = "python")]
6use pyo3::prelude::*;
7
8#[cfg(not(target_arch = "wasm32"))]
9pub mod blockchain;
10pub mod inspector;
11pub mod policy;
12pub mod policy_advanced;
13pub mod resolver;
14pub mod tee;
15pub mod types;
16pub mod validator;
17pub mod verifier;
18
19#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
20pub mod streaming;
21#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
22pub mod wasm_async;
23
24#[cfg(feature = "wasm")]
25pub mod wasm;
26
27// Export types for wasm modules
28pub use types::{
29    CryptoReport, DigestConfig, HashAlgorithm, LinkCheckConfig, Manifest, ManifestProof,
30    PolicyContext, ValidationConfig, ValidationMode, ValidationOptions, ValidationReport,
31    VerificationMethod,
32};
33
34// Async WASM types
35#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
36pub use wasm_async::{AsyncValidationState, SolvencyResult, TEEResult};
37
38// Streaming Validation types
39#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
40pub use streaming::{ChunkValidationResult, StreamingValidationState, StreamingValidator};
41
42#[cfg(feature = "python")]
43#[pyfunction]
44#[allow(unsafe_op_in_unsafe_fn)]
45fn validate_manifest_json(manifest_json: &str) -> PyResult<String> {
46    let manifest: serde_json::Value = serde_json::from_str(manifest_json)
47        .map_err(|err| PyValueError::new_err(format!("Invalid JSON: {}", err)))?;
48    let report = crate::validator::validate_manifest_value(&manifest);
49    serde_json::to_string_pretty(&report)
50        .map_err(|err| PyValueError::new_err(format!("Failed to serialize report: {}", err)))
51}
52
53#[cfg(feature = "python")]
54#[pyfunction]
55#[allow(unsafe_op_in_unsafe_fn)]
56fn validate_manifest_json_with_config(manifest_json: &str, config_json: &str) -> PyResult<String> {
57    let manifest: serde_json::Value = serde_json::from_str(manifest_json)
58        .map_err(|err| PyValueError::new_err(format!("Invalid JSON: {}", err)))?;
59    let config: ValidationConfig = serde_json::from_str(config_json)
60        .map_err(|err| PyValueError::new_err(format!("Invalid config JSON: {}", err)))?;
61    let report = crate::validator::validate_manifest_with_config(&manifest, &config);
62    serde_json::to_string_pretty(&report)
63        .map_err(|err| PyValueError::new_err(format!("Failed to serialize report: {}", err)))
64}
65
66#[cfg(feature = "python")]
67#[pyfunction]
68#[allow(unsafe_op_in_unsafe_fn)]
69fn verify_tee_evidence_json(evidence_json: &str) -> PyResult<String> {
70    let evidence: crate::tee::TeeEvidence = serde_json::from_str(evidence_json)
71        .map_err(|err| PyValueError::new_err(format!("Invalid TEE evidence JSON: {}", err)))?;
72    let report = crate::tee::verify_tee_evidence(&evidence)
73        .map_err(|err| PyValueError::new_err(format!("TEE verification failed: {}", err)))?;
74    serde_json::to_string_pretty(&report)
75        .map_err(|err| PyValueError::new_err(format!("Failed to serialize report: {}", err)))
76}
77
78#[cfg(feature = "python")]
79#[pyfunction]
80#[allow(unsafe_op_in_unsafe_fn)]
81fn inspect_manifest_json(manifest_json: &str) -> PyResult<String> {
82    let manifest: Manifest = serde_json::from_str(manifest_json)
83        .map_err(|err| PyValueError::new_err(format!("Invalid manifest JSON: {}", err)))?;
84    let options = ValidationOptions::default();
85    let (valid, errors) = crate::inspector::inspect_manifest(&manifest, &options);
86    let result = serde_json::json!({
87        "valid": valid,
88        "errors": errors,
89    });
90    serde_json::to_string_pretty(&result)
91        .map_err(|err| PyValueError::new_err(format!("Failed to serialize result: {}", err)))
92}
93
94#[cfg(feature = "python")]
95#[pyfunction]
96#[allow(unsafe_op_in_unsafe_fn)]
97fn resolve_did_pkh_json(did: &str) -> PyResult<String> {
98    let info = crate::resolver::parse_did_pkh(did)
99        .map_err(|err| PyValueError::new_err(format!("Invalid did:pkh: {}", err)))?;
100    let network = format!("{:?}", info.network);
101    let result = serde_json::json!({
102        "network": network,
103        "address": info.address,
104        "chainId": info.chain_id,
105    });
106    serde_json::to_string_pretty(&result)
107        .map_err(|err| PyValueError::new_err(format!("Failed to serialize result: {}", err)))
108}
109
110#[cfg(feature = "python")]
111#[pyfunction]
112#[allow(unsafe_op_in_unsafe_fn)]
113fn get_version() -> PyResult<String> {
114    Ok(env!("CARGO_PKG_VERSION").to_string())
115}
116
117#[cfg(feature = "python")]
118#[pyfunction]
119#[allow(unsafe_op_in_unsafe_fn)]
120fn get_name() -> PyResult<String> {
121    Ok(env!("CARGO_PKG_NAME").to_string())
122}
123
124#[cfg(feature = "python")]
125#[pymodule]
126fn _core(_py: Python<'_>, module: &PyModule) -> PyResult<()> {
127    module.add_function(wrap_pyfunction!(validate_manifest_json, module)?)?;
128    module.add_function(wrap_pyfunction!(
129        validate_manifest_json_with_config,
130        module
131    )?)?;
132    module.add_function(wrap_pyfunction!(verify_tee_evidence_json, module)?)?;
133    module.add_function(wrap_pyfunction!(inspect_manifest_json, module)?)?;
134    module.add_function(wrap_pyfunction!(resolve_did_pkh_json, module)?)?;
135    module.add_function(wrap_pyfunction!(get_version, module)?)?;
136    module.add_function(wrap_pyfunction!(get_name, module)?)?;
137    Ok(())
138}