{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://dynamicfeed.ai/schemas/trust-receipt-v1.json",
  "title": "Dynamic Feed Trust Receipt v1 envelope",
  "description": "Public schema for the staging-validated Trust Receipt v1 implementation draft. Production deployment and public feature activation are not authorized; this schema is non-normative and is not a DFEP specification.",
  "x-dynamic-feed-classification": "Public",
  "x-dynamic-feed-status": "Staging-validated implementation draft; production unauthorized; non-normative",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "receipt",
    "payload_sha256",
    "signature"
  ],
  "properties": {
    "receipt": {
      "$ref": "#/$defs/receipt"
    },
    "payload_sha256": {
      "$ref": "#/$defs/labelledSha256"
    },
    "signature": {
      "$ref": "#/$defs/signature"
    }
  },
  "$defs": {
    "safeInteger": {
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991
    },
    "canonicalValue": {
      "description": "A value in the df-jcs-1 JSON data model. Implementations must additionally reject floating-point JSON tokens and unpaired Unicode surrogates as specified by df-jcs-1.",
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "boolean"
        },
        {
          "$ref": "#/$defs/safeInteger"
        },
        {
          "type": "string"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/canonicalValue"
          }
        },
        {
          "$ref": "#/$defs/canonicalObject"
        }
      ]
    },
    "canonicalObject": {
      "type": "object",
      "additionalProperties": {
        "$ref": "#/$defs/canonicalValue"
      }
    },
    "nonEmptyString": {
      "type": "string",
      "minLength": 1
    },
    "utcTimestamp": {
      "type": "string",
      "format": "date-time",
      "pattern": "^(?!0000)[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])T(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](?:\\.[0-9]+)?Z$",
      "x-dynamic-feed-format-assertion": "mandatory-rfc3339-utc"
    },
    "receiptId": {
      "type": "string",
      "pattern": "^tr_[0-9a-f]{32}$"
    },
    "requestId": {
      "type": "string",
      "pattern": "^req_[0-9a-f]{32}$"
    },
    "labelledSha256": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "keyId": {
      "type": "string",
      "pattern": "^df-ed25519-[0-9a-f]{12}$"
    },
    "role": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "role"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "role": {
          "enum": [
            "claimant",
            "issuer",
            "observer",
            "witness"
          ]
        }
      }
    },
    "roles": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "claimant",
        "issuer",
        "observer",
        "witness"
      ],
      "properties": {
        "claimant": {
          "allOf": [
            {
              "$ref": "#/$defs/role"
            },
            {
              "properties": {
                "role": {
                  "const": "claimant"
                }
              }
            }
          ]
        },
        "issuer": {
          "allOf": [
            {
              "$ref": "#/$defs/role"
            },
            {
              "properties": {
                "role": {
                  "const": "issuer"
                }
              }
            }
          ]
        },
        "observer": {
          "allOf": [
            {
              "$ref": "#/$defs/role"
            },
            {
              "properties": {
                "role": {
                  "const": "observer"
                }
              }
            }
          ]
        },
        "witness": {
          "allOf": [
            {
              "$ref": "#/$defs/role"
            },
            {
              "properties": {
                "role": {
                  "const": "witness"
                }
              }
            }
          ]
        }
      }
    },
    "request": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "operation",
        "capability",
        "parameters",
        "caller_claim"
      ],
      "properties": {
        "operation": {
          "enum": [
            "resolve_live_fact",
            "verify_live_claim"
          ]
        },
        "capability": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "parameters": {
          "$ref": "#/$defs/canonicalObject"
        },
        "caller_claim": {
          "oneOf": [
            {
              "type": "null"
            },
            {
              "$ref": "#/$defs/canonicalObject"
            }
          ]
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "operation": {
                "const": "resolve_live_fact"
              }
            },
            "required": [
              "operation"
            ]
          },
          "then": {
            "properties": {
              "caller_claim": {
                "type": "null"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "operation": {
                "const": "verify_live_claim"
              }
            },
            "required": [
              "operation"
            ]
          },
          "then": {
            "properties": {
              "caller_claim": {
                "$ref": "#/$defs/canonicalObject"
              }
            }
          }
        }
      ]
    },
    "result": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "verification_state",
        "freshness_state",
        "value",
        "limitations"
      ],
      "properties": {
        "status": {
          "enum": [
            "ok",
            "degraded",
            "error"
          ]
        },
        "verification_state": {
          "enum": [
            "verified",
            "partially_verified",
            "conflicting",
            "unverified",
            "stale",
            "unavailable",
            "invalid"
          ]
        },
        "freshness_state": {
          "enum": [
            "fresh",
            "stale",
            "unavailable"
          ]
        },
        "value": {
          "$ref": "#/$defs/canonicalValue"
        },
        "limitations": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "freshness_state": {
                "const": "stale"
              }
            },
            "required": [
              "freshness_state"
            ]
          },
          "then": {
            "not": {
              "properties": {
                "verification_state": {
                  "const": "verified"
                }
              },
              "required": [
                "verification_state"
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "freshness_state": {
                "const": "unavailable"
              }
            },
            "required": [
              "freshness_state"
            ]
          },
          "then": {
            "not": {
              "properties": {
                "verification_state": {
                  "const": "verified"
                }
              },
              "required": [
                "verification_state"
              ]
            }
          }
        }
      ]
    },
    "freshness": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state",
        "age_seconds",
        "maximum_age_seconds"
      ],
      "properties": {
        "state": {
          "enum": [
            "fresh",
            "stale",
            "unavailable"
          ]
        },
        "age_seconds": {
          "oneOf": [
            {
              "type": "null"
            },
            {
              "allOf": [
                {
                  "$ref": "#/$defs/safeInteger"
                },
                {
                  "minimum": 0
                }
              ]
            }
          ]
        },
        "maximum_age_seconds": {
          "allOf": [
            {
              "$ref": "#/$defs/safeInteger"
            },
            {
              "minimum": 0
            }
          ]
        }
      }
    },
    "sourceObservation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "source_id",
        "source_name",
        "source_url",
        "license",
        "commercial_use",
        "observed_at",
        "retrieved_at",
        "freshness",
        "adapter_response_sha256",
        "normalized_evidence_sha256",
        "raw_evidence_sha256",
        "raw_evidence_available"
      ],
      "properties": {
        "source_id": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "source_name": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "source_url": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "license": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "commercial_use": {
          "type": "boolean"
        },
        "observed_at": {
          "oneOf": [
            {
              "type": "null"
            },
            {
              "$ref": "#/$defs/utcTimestamp"
            }
          ]
        },
        "retrieved_at": {
          "$ref": "#/$defs/utcTimestamp"
        },
        "freshness": {
          "$ref": "#/$defs/freshness"
        },
        "adapter_response_sha256": {
          "$ref": "#/$defs/labelledSha256"
        },
        "normalized_evidence_sha256": {
          "$ref": "#/$defs/labelledSha256"
        },
        "raw_evidence_sha256": {
          "oneOf": [
            {
              "type": "null"
            },
            {
              "$ref": "#/$defs/labelledSha256"
            }
          ]
        },
        "raw_evidence_available": {
          "type": "boolean"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "freshness": {
                "properties": {
                  "state": {
                    "const": "unavailable"
                  }
                },
                "required": [
                  "state"
                ]
              }
            },
            "required": [
              "freshness"
            ]
          },
          "then": {
            "properties": {
              "observed_at": {
                "type": "null"
              },
              "freshness": {
                "properties": {
                  "age_seconds": {
                    "type": "null"
                  }
                }
              }
            }
          },
          "else": {
            "properties": {
              "observed_at": {
                "$ref": "#/$defs/utcTimestamp"
              },
              "freshness": {
                "properties": {
                  "age_seconds": {
                    "allOf": [
                      {
                        "$ref": "#/$defs/safeInteger"
                      },
                      {
                        "minimum": 0
                      }
                    ]
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "raw_evidence_available": {
                "const": true
              }
            },
            "required": [
              "raw_evidence_available"
            ]
          },
          "then": {
            "properties": {
              "raw_evidence_sha256": {
                "$ref": "#/$defs/labelledSha256"
              }
            }
          },
          "else": {
            "properties": {
              "raw_evidence_sha256": {
                "type": "null"
              }
            }
          }
        }
      ]
    },
    "transformation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "adapter",
        "parser_version"
      ],
      "properties": {
        "adapter": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "parser_version": {
          "$ref": "#/$defs/nonEmptyString"
        }
      }
    },
    "receipt": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schema_version",
        "canonicalization",
        "receipt_id",
        "request_id",
        "kind",
        "issued_at",
        "roles",
        "request",
        "result",
        "source_observations",
        "transformation",
        "correction_of"
      ],
      "properties": {
        "schema_version": {
          "const": "trust-receipt/v1"
        },
        "canonicalization": {
          "const": "df-jcs-1"
        },
        "receipt_id": {
          "$ref": "#/$defs/receiptId"
        },
        "request_id": {
          "$ref": "#/$defs/requestId"
        },
        "kind": {
          "enum": [
            "observation",
            "verification",
            "correction"
          ]
        },
        "issued_at": {
          "$ref": "#/$defs/utcTimestamp"
        },
        "roles": {
          "$ref": "#/$defs/roles"
        },
        "request": {
          "$ref": "#/$defs/request"
        },
        "result": {
          "$ref": "#/$defs/result"
        },
        "source_observations": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/sourceObservation"
          }
        },
        "transformation": {
          "$ref": "#/$defs/transformation"
        },
        "correction_of": {
          "oneOf": [
            {
              "type": "null"
            },
            {
              "$ref": "#/$defs/receiptId"
            }
          ]
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "kind": {
                "const": "correction"
              }
            },
            "required": [
              "kind"
            ]
          },
          "then": {
            "properties": {
              "correction_of": {
                "$ref": "#/$defs/receiptId"
              }
            }
          },
          "else": {
            "properties": {
              "correction_of": {
                "type": "null"
              }
            }
          }
        }
      ]
    },
    "signature": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "alg",
        "key_id",
        "canonicalization",
        "sig"
      ],
      "properties": {
        "alg": {
          "const": "Ed25519"
        },
        "key_id": {
          "$ref": "#/$defs/keyId"
        },
        "canonicalization": {
          "const": "df-jcs-1"
        },
        "sig": {
          "type": "string",
          "minLength": 1,
          "description": "Non-empty signature text at schema-validation time. Cryptographic verification additionally requires a valid base64url representation that decodes to exactly 64 bytes."
        }
      }
    }
  }
}
