File size: 2,681 Bytes
2c41dce |
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 |
"""
Verification Agent
Verifies proof authenticity by recomputing and comparing hashes.
"""
from typing import Dict, Any
import hashlib
from datetime import datetime, timezone
from core.agent_base import Agent
from core.errors import VerificationError
from models.proof import VerificationResult, Proof
from agents.storage_agent import StorageAgent
class VerificationAgent(Agent):
"""
Verifies proof by fetching from storage and recomputing hash.
"""
def __init__(self, storage_agent: StorageAgent):
super().__init__()
self.storage_agent = storage_agent
def execute(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
"""
Verify proof authenticity.
Expected input_data:
{
"proof_id": str,
"content": bytes # Original content to verify
}
Returns:
{
"verification_result": VerificationResult
}
"""
proof_id = input_data.get("proof_id")
content = input_data.get("content")
if not proof_id:
raise VerificationError("Missing 'proof_id'")
if not content:
raise VerificationError("Missing 'content' to verify")
# Fetch original proof
proof = self.storage_agent.get_proof(proof_id)
if not proof:
raise VerificationError(f"Proof not found: {proof_id}")
# Recompute hash
computed_hash = self._compute_hash(content, proof.hash_algorithm)
# Compare hashes
is_valid = computed_hash == proof.content_hash
result = VerificationResult(
proof_id=proof_id,
is_valid=is_valid,
original_hash=proof.content_hash,
computed_hash=computed_hash,
timestamp=datetime.now(timezone.utc).isoformat(),
message="Hash match: proof is valid" if is_valid else "Hash mismatch: proof is invalid"
)
return {
"verification_result": result
}
def _compute_hash(self, content: bytes, algorithm: str) -> str:
"""
Compute hash using specified algorithm.
Args:
content: Content bytes to hash
algorithm: Hash algorithm name
Returns:
Hexadecimal hash string
"""
try:
hasher = hashlib.new(algorithm)
hasher.update(content)
return hasher.hexdigest()
except Exception as e:
raise VerificationError(f"Hash computation failed: {str(e)}") from e |