{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://developers.chia.health/contracts/partner/assets/contract.schema.json",
  "title": "Chia Partner Catalog Asset Contract",
  "description": "Schema for the separately versioned authenticated binary asset route contract.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "contract_id",
    "version",
    "description",
    "environments",
    "authentication",
    "request",
    "authorization",
    "responses",
    "integrity",
    "client_obligations",
    "security_and_logging"
  ],
  "properties": {
    "schema_version": {
      "const": "2.1.0"
    },
    "contract_id": {
      "const": "chia.partner.catalog_asset"
    },
    "version": {
      "type": "string",
      "pattern": "^2\\.[0-9]+\\.[0-9]+$"
    },
    "description": {
      "type": "string",
      "minLength": 1
    },
    "environments": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "sandbox",
        "production"
      ],
      "properties": {
        "sandbox": {
          "$ref": "#/$defs/sandboxEnvironment"
        },
        "production": {
          "$ref": "#/$defs/productionEnvironment"
        }
      }
    },
    "authentication": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "header",
        "scheme",
        "credential_class",
        "required_scope",
        "credential_source",
        "environment_must_match_origin",
        "partner_and_environment_derived_from_credential",
        "finance_credentials_rejected"
      ],
      "properties": {
        "header": {
          "const": "Authorization"
        },
        "scheme": {
          "const": "Bearer"
        },
        "credential_class": {
          "const": "runtime"
        },
        "required_scope": {
          "const": "catalog:read"
        },
        "credential_source": {
          "const": "partner_confidential_backend_only"
        },
        "environment_must_match_origin": {
          "const": true
        },
        "partner_and_environment_derived_from_credential": {
          "const": true
        },
        "finance_credentials_rejected": {
          "const": true
        }
      }
    },
    "request": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "method",
        "path_template",
        "opaque_asset_id",
        "application_headers",
        "query_string_allowed",
        "request_body_allowed",
        "cookies_allowed",
        "range_allowed",
        "conditional_requests_allowed",
        "cors_preflight_allowed",
        "redirects_allowed",
        "other_methods_allowed",
        "other_paths_allowed"
      ],
      "properties": {
        "method": {
          "const": "GET"
        },
        "path_template": {
          "const": "/a/{opaque_asset_id}"
        },
        "opaque_asset_id": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "pattern",
            "random_and_non_semantic",
            "partner_environment_publication_bound"
          ],
          "properties": {
            "pattern": {
              "const": "^ast_[A-Za-z0-9]{8,64}$"
            },
            "random_and_non_semantic": {
              "const": true
            },
            "partner_environment_publication_bound": {
              "const": true
            }
          }
        },
        "application_headers": {
          "type": "array",
          "const": [
            "Authorization"
          ]
        },
        "query_string_allowed": {
          "const": false
        },
        "request_body_allowed": {
          "const": false
        },
        "cookies_allowed": {
          "const": false
        },
        "range_allowed": {
          "const": false
        },
        "conditional_requests_allowed": {
          "const": false
        },
        "cors_preflight_allowed": {
          "const": false
        },
        "redirects_allowed": {
          "const": false
        },
        "other_methods_allowed": {
          "const": false
        },
        "other_paths_allowed": {
          "const": false
        }
      }
    },
    "authorization": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "rechecked_on_every_fetch",
        "required_publication_state",
        "asset_must_be_bound_to_current_tenant_publication",
        "stale_catalog_reference_rejected",
        "etag_does_not_extend_authorization"
      ],
      "properties": {
        "rechecked_on_every_fetch": {
          "const": true
        },
        "required_publication_state": {
          "type": "array",
          "const": [
            "effective",
            "unexpired",
            "not_withdrawn"
          ]
        },
        "asset_must_be_bound_to_current_tenant_publication": {
          "const": true
        },
        "stale_catalog_reference_rejected": {
          "const": true
        },
        "etag_does_not_extend_authorization": {
          "const": true
        }
      }
    },
    "responses": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "success",
        "generic_error",
        "rate_limited",
        "server_error"
      ],
      "properties": {
        "success": {
          "$ref": "#/$defs/success"
        },
        "generic_error": {
          "$ref": "#/$defs/genericError"
        },
        "rate_limited": {
          "$ref": "#/$defs/rateLimited"
        },
        "server_error": {
          "$ref": "#/$defs/serverError"
        }
      }
    },
    "integrity": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "digest_algorithm",
        "catalog_field",
        "catalog_digest_encoding",
        "content_digest_encoding",
        "strong_etag_is_quoted_catalog_sha256",
        "bytes_immutable_for_asset_id",
        "malware_and_decoder_scan_required_before_publication"
      ],
      "properties": {
        "digest_algorithm": {
          "const": "sha-256"
        },
        "catalog_field": {
          "const": "presentation.image.sha256"
        },
        "catalog_digest_encoding": {
          "const": "lowercase_hex"
        },
        "content_digest_encoding": {
          "const": "RFC9530_base64"
        },
        "strong_etag_is_quoted_catalog_sha256": {
          "const": true
        },
        "bytes_immutable_for_asset_id": {
          "const": true
        },
        "malware_and_decoder_scan_required_before_publication": {
          "const": true
        }
      }
    },
    "client_obligations": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "stream_to_caller_supplied_sink",
        "verify_content_type_length_and_sha256_before_use",
        "mirror_under_random_neutral_partner_path",
        "expose_chia_asset_url_or_credential_to_mobile",
        "bake_asset_into_mobile_release",
        "retain_past_publication_cache_or_expiry",
        "purge_on_expiry_withdrawal_or_digest_failure",
        "per_user_fetch_or_access_log"
      ],
      "properties": {
        "stream_to_caller_supplied_sink": {
          "const": true
        },
        "verify_content_type_length_and_sha256_before_use": {
          "const": true
        },
        "mirror_under_random_neutral_partner_path": {
          "const": true
        },
        "expose_chia_asset_url_or_credential_to_mobile": {
          "const": false
        },
        "bake_asset_into_mobile_release": {
          "const": false
        },
        "retain_past_publication_cache_or_expiry": {
          "const": false
        },
        "purge_on_expiry_withdrawal_or_digest_failure": {
          "const": true
        },
        "per_user_fetch_or_access_log": {
          "const": false
        }
      }
    },
    "security_and_logging": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "cors_enabled",
        "cookies_emitted",
        "html_or_remote_subresource_allowed",
        "normalized_log_path",
        "log_fields",
        "forbidden_log_fields"
      ],
      "properties": {
        "cors_enabled": {
          "const": false
        },
        "cookies_emitted": {
          "const": false
        },
        "html_or_remote_subresource_allowed": {
          "const": false
        },
        "normalized_log_path": {
          "const": "/a/{opaque_asset_id}"
        },
        "log_fields": {
          "type": "array",
          "const": [
            "partner",
            "environment",
            "safe_status_class",
            "byte_count",
            "request_id"
          ]
        },
        "forbidden_log_fields": {
          "type": "array",
          "const": [
            "raw_opaque_path",
            "authorization",
            "query",
            "referrer",
            "user_agent",
            "cookie",
            "request_body",
            "response_body"
          ]
        }
      }
    }
  },
  "$defs": {
    "sandboxEnvironment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "origin",
        "origin_status",
        "livemode",
        "credential_prefix"
      ],
      "properties": {
        "origin": {
          "const": "https://assets.sandbox.invalid"
        },
        "origin_status": {
          "const": "synthetic_placeholder_use_signed_onboarding_origin"
        },
        "livemode": {
          "const": false
        },
        "credential_prefix": {
          "const": "chia_test_"
        }
      }
    },
    "productionEnvironment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "origin",
        "origin_status",
        "livemode",
        "credential_prefix"
      ],
      "properties": {
        "origin": {
          "const": "https://assets.production.invalid"
        },
        "origin_status": {
          "const": "synthetic_placeholder_use_signed_onboarding_origin"
        },
        "livemode": {
          "const": true
        },
        "credential_prefix": {
          "const": "chia_live_"
        }
      }
    },
    "success": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "content_types",
        "maximum_content_length_bytes",
        "maximum_width_pixels",
        "maximum_height_pixels",
        "required_headers",
        "header_contract",
        "forbidden_headers",
        "body"
      ],
      "properties": {
        "status": {
          "const": 200
        },
        "content_types": {
          "type": "array",
          "const": [
            "image/avif",
            "image/webp",
            "image/png"
          ]
        },
        "maximum_content_length_bytes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 10485760
        },
        "maximum_width_pixels": {
          "type": "integer",
          "minimum": 1,
          "maximum": 8192
        },
        "maximum_height_pixels": {
          "type": "integer",
          "minimum": 1,
          "maximum": 8192
        },
        "required_headers": {
          "type": "array",
          "const": [
            "Content-Type",
            "Content-Length",
            "ETag",
            "Content-Digest",
            "Cache-Control",
            "X-Content-Type-Options",
            "Chia-Request-Id"
          ]
        },
        "header_contract": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "ETag",
            "Content_Digest",
            "Cache_Control",
            "X_Content_Type_Options",
            "Chia_Request_Id"
          ],
          "properties": {
            "ETag": {
              "const": "\"<lowercase_sha256_hex>\""
            },
            "Content_Digest": {
              "const": "sha-256=:<base64_sha256_digest>:"
            },
            "Cache_Control": {
              "const": "private, no-store"
            },
            "X_Content_Type_Options": {
              "const": "nosniff"
            },
            "Chia_Request_Id": {
              "const": "req_<opaque>"
            }
          }
        },
        "forbidden_headers": {
          "type": "array",
          "const": [
            "Set-Cookie",
            "Access-Control-Allow-Origin",
            "Access-Control-Allow-Credentials",
            "Content-Range",
            "Location",
            "Content-Disposition"
          ]
        },
        "body": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "genericError": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "equivalent_failures",
        "content_type",
        "cache_control",
        "body",
        "non_enumerating"
      ],
      "properties": {
        "status": {
          "const": 404
        },
        "equivalent_failures": {
          "type": "array",
          "const": [
            "missing_or_invalid_credential",
            "wrong_environment_credential",
            "finance_credential",
            "missing_scope",
            "unknown_asset",
            "inactive_or_unbound_asset",
            "expired_or_withdrawn_publication",
            "stale_catalog_reference",
            "query_string",
            "request_body",
            "cookie",
            "range",
            "conditional_request",
            "cors_preflight",
            "unknown_method",
            "unknown_path"
          ]
        },
        "content_type": {
          "const": "application/json"
        },
        "cache_control": {
          "const": "no-store"
        },
        "body": {
          "$ref": "#/$defs/genericErrorBody"
        },
        "non_enumerating": {
          "const": true
        }
      }
    },
    "genericErrorBody": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "error"
      ],
      "properties": {
        "error": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "code",
            "message",
            "param",
            "request_id",
            "retryable"
          ],
          "properties": {
            "type": {
              "const": "invalid_request_error"
            },
            "code": {
              "const": "catalog_asset_unavailable"
            },
            "message": {
              "const": "The catalog asset is not available."
            },
            "param": {
              "type": "null"
            },
            "request_id": {
              "const": "req_<opaque>"
            },
            "retryable": {
              "const": false
            }
          }
        }
      }
    },
    "rateLimited": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "content_type",
        "required_headers",
        "cache_control",
        "error_code",
        "retryable"
      ],
      "properties": {
        "status": {
          "const": 429
        },
        "content_type": {
          "const": "application/json"
        },
        "required_headers": {
          "type": "array",
          "const": [
            "Chia-Request-Id",
            "Retry-After"
          ]
        },
        "cache_control": {
          "const": "no-store"
        },
        "error_code": {
          "const": "rate_limit_exceeded"
        },
        "retryable": {
          "const": true
        }
      }
    },
    "serverError": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "statuses",
        "content_type",
        "required_headers",
        "cache_control",
        "error_code",
        "internal_details_forbidden"
      ],
      "properties": {
        "statuses": {
          "type": "array",
          "const": [
            500,
            503
          ]
        },
        "content_type": {
          "const": "application/json"
        },
        "required_headers": {
          "type": "array",
          "const": [
            "Chia-Request-Id"
          ]
        },
        "cache_control": {
          "const": "no-store"
        },
        "error_code": {
          "const": "internal_error"
        },
        "internal_details_forbidden": {
          "const": true
        }
      }
    }
  }
}
