Overview
Policies are the core decision-making engine of the ZKScore Trust Layer. They define flexible rules that evaluate user attestations to make access control, risk assessment, and other trust-based decisions in your applications.Think of policies as smart contracts for trust decisions. They take user attestations as input and produce structured decisions as output, enabling your application to make automated trust-based choices.
What are Policies?
A policy is a structured set of conditions that can be evaluated against a user’s attestations to make decisions. Policies are:- Composable - Can be combined and nested
- Versioned - Support evolution and updates
- Flexible - Handle complex decision logic
- Testable - Can be validated before deployment
- Transparent - Decision logic is auditable
Policy Structure
Every policy follows a standardized JSON structure:Policy Components
1. Policy Metadata
2. Conditions
Conditions define the requirements that must be met for a policy to pass.Basic Condition Types
Attestation Existence
Check if a user has specific attestations:Data Conditions
Evaluate specific fields in attestation data:eq- Equal tone- Not equal togt- Greater thangte- Greater than or equal tolt- Less thanlte- Less than or equal toin- In arraynin- Not in arraycontains- Array contains valueregex- Regular expression match
Composite Conditions
Combine multiple conditions with logical operators:AND- All conditions must be trueOR- At least one condition must be trueNOT- Condition must be false
Time-based Conditions
Evaluate time-based requirements:Score Conditions
Evaluate trust scores:3. Decision Structure
The decision defines what happens when a policy passes or fails.Allow Decision
Deny Decision
Conditional Decision
Creating Policies
1. Via API (Recommended)
2. Via Smart Contract
3. Policy Templates
ZKScore provides pre-built policy templates for common use cases:Policy Evaluation
1. Basic Evaluation
2. Batch Evaluation
3. Advanced Evaluation with Custom Context
Policy Types and Use Cases
1. Access Control Policies
Control who can access features or resources:2. Risk Assessment Policies
Evaluate risk levels for financial decisions:3. Compliance Policies
Ensure regulatory and legal compliance:4. Reputation Policies
Calculate and evaluate reputation scores:Policy Testing and Validation
1. Test Policy with Sample Data
2. Policy Validation
3. Policy Simulation
Policy Management
1. Update Existing Policies
2. Policy Versioning
3. Policy Deprecation
Integration Examples
1. Express.js Middleware
2. React Hook for Policy Evaluation
3. Smart Contract Integration
Best Practices
1. Policy Design
- Keep it simple: Start with basic conditions and add complexity gradually
- Clear error messages: Provide helpful feedback when policies fail
- Version control: Use semantic versioning for policy updates
- Documentation: Document policy logic and use cases
- Testing: Test policies with various scenarios before deployment
2. Performance
- Efficient conditions: Order conditions by selectivity (most selective first)
- Caching: Cache policy evaluation results when appropriate
- Batch evaluation: Use batch evaluation for multiple policies
- Indexing: Ensure attestation data is properly indexed
3. Security
- Input validation: Validate all policy inputs and context data
- Access control: Restrict policy creation and modification to authorized users
- Audit trails: Log all policy evaluations and decisions
- Regular review: Periodically review and update policies
4. User Experience
- Clear requirements: Make policy requirements transparent to users
- Helpful feedback: Provide actionable suggestions when policies fail
- Progressive disclosure: Gradually introduce users to policy requirements
- Alternative paths: Provide alternative ways to meet policy requirements