|
| 1 | +"""Pydantic schemas for ERC-8004 registry entries and normalized attestations. |
| 2 | +
|
| 3 | +ERC-8004 registries (per EIP-8004): |
| 4 | +- Identity Registry: links agent DID → on-chain address + identity metadata |
| 5 | +- Reputation Registry: stores feedback entries (numeric score + signed payload) |
| 6 | +- Validation Registry: stores verified-work attestations (proof of completed task) |
| 7 | +
|
| 8 | +Each entry carries a CTEF-formatted payload in its `data` field, signed |
| 9 | +Ed25519 by the attestation issuer (which may differ from the entry submitter). |
| 10 | +""" |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from datetime import datetime |
| 14 | +from enum import Enum |
| 15 | +from typing import Any, Optional |
| 16 | + |
| 17 | +from pydantic import BaseModel, Field |
| 18 | + |
| 19 | + |
| 20 | +class ERC8004Registry(str, Enum): |
| 21 | + """The three ERC-8004 on-chain registries.""" |
| 22 | + |
| 23 | + IDENTITY = "identity" |
| 24 | + REPUTATION = "reputation" |
| 25 | + VALIDATION = "validation" |
| 26 | + |
| 27 | + |
| 28 | +class ERC8004Entry(BaseModel): |
| 29 | + """Raw entry as returned from an ERC-8004 registry contract call. |
| 30 | +
|
| 31 | + The `data` field carries an opaque payload — for AgentGraph integration, |
| 32 | + that payload is expected to be a CTEF-formatted attestation (JCS-canonical |
| 33 | + JSON with Ed25519 signature). Other consumers may use different payload |
| 34 | + formats; ERC-8004 itself does not constrain the payload shape. |
| 35 | + """ |
| 36 | + |
| 37 | + registry: ERC8004Registry |
| 38 | + entry_id: int = Field(ge=0, description="Sequential entry index in the registry") |
| 39 | + submitter: str = Field( |
| 40 | + pattern=r"^0x[0-9a-fA-F]{40}$", |
| 41 | + description="Ethereum address that submitted the entry", |
| 42 | + ) |
| 43 | + subject_did: Optional[str] = Field( |
| 44 | + default=None, |
| 45 | + description="DID this entry attests about (e.g. did:web:agent.example.com)", |
| 46 | + ) |
| 47 | + data: bytes = Field(description="Raw entry payload bytes (typically CTEF JSON)") |
| 48 | + block_number: int = Field(ge=0) |
| 49 | + block_timestamp: datetime |
| 50 | + tx_hash: str = Field(pattern=r"^0x[0-9a-fA-F]{64}$") |
| 51 | + |
| 52 | + |
| 53 | +class NormalizedAttestation(BaseModel): |
| 54 | + """ERC-8004 entry normalized into AgentGraph composite-trust-score input. |
| 55 | +
|
| 56 | + Produced by `attestation_normalizer.normalize()` after: |
| 57 | + 1. Reading the entry from the on-chain registry |
| 58 | + 2. Parsing the `data` field as CTEF v0.3.1 envelope |
| 59 | + 3. Verifying the CTEF Ed25519 signature (substrate-side) |
| 60 | + 4. Verifying the entry submitter's Ethereum signature (registry-side) |
| 61 | + """ |
| 62 | + |
| 63 | + source_urn: str = Field(description="urn:erc8004:{registry}:<entry_id> originating URN") |
| 64 | + claim_type: str = Field(description="CTEF claim_type — one of {identity, transport, authority, continuity}") |
| 65 | + claim_subtype: Optional[str] = Field(default=None) |
| 66 | + subject_did: str = Field(description="The DID this attestation is about") |
| 67 | + provider_did: str = Field(description="The DID of the attestation issuer (CTEF provider)") |
| 68 | + payload: dict[str, Any] = Field(description="Parsed CTEF envelope payload") |
| 69 | + signature_verified: bool = Field( |
| 70 | + description="True iff CTEF Ed25519 signature verified against provider's published JWKS", |
| 71 | + ) |
| 72 | + registry_signature_verified: bool = Field( |
| 73 | + description="True iff ERC-8004 entry submitter signature verified on-chain", |
| 74 | + ) |
| 75 | + issued_at: datetime |
| 76 | + expires_at: Optional[datetime] = Field(default=None) |
| 77 | + freshness_ttl_remaining_seconds: Optional[int] = Field(default=None) |
| 78 | + |
| 79 | + @property |
| 80 | + def is_admissible(self) -> bool: |
| 81 | + """Both signature layers must verify and attestation must not be expired.""" |
| 82 | + return ( |
| 83 | + self.signature_verified |
| 84 | + and self.registry_signature_verified |
| 85 | + and (self.expires_at is None or self.expires_at > datetime.utcnow()) |
| 86 | + ) |
0 commit comments