{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://davisvillelabs.com/agents/davisville-receipt-envelope.v1.schema.json",
  "title": "Davisville Receipt Envelope v1",
  "type": "object",
  "required": [
    "schema",
    "receiptId",
    "issuer",
    "subject",
    "operation",
    "request",
    "authorization",
    "counterparty",
    "reversibility",
    "payment",
    "delivery",
    "outcome",
    "evidenceReferences",
    "freshness",
    "lineage",
    "limitations",
    "createdAt",
    "verification"
  ],
  "properties": {
    "schema": {
      "const": "davisville-receipt-envelope.v1"
    },
    "receiptId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 200
    },
    "issuer": {
      "type": "object",
      "required": [
        "organizationId",
        "organization",
        "productId",
        "productName",
        "canonicalUrl"
      ],
      "properties": {
        "organizationId": {
          "const": "davisville-labs"
        },
        "organization": {
          "const": "Davisville Labs"
        },
        "productId": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "productName": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "canonicalUrl": {
          "type": "string",
          "format": "uri"
        }
      },
      "additionalProperties": true
    },
    "subject": {
      "type": "object",
      "required": [
        "type"
      ],
      "properties": {
        "type": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "id": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 300
        },
        "label": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 500
        }
      },
      "additionalProperties": true
    },
    "operation": {
      "type": "object",
      "required": [
        "id"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "contractVersion": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 80
        },
        "resultSchema": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 240
        },
        "resultSchemaDigest": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 200
        }
      },
      "additionalProperties": true
    },
    "request": {
      "type": "object",
      "required": [
        "requestId",
        "canonicalization",
        "hashAlgorithm",
        "requestHash"
      ],
      "properties": {
        "requestId": {
          "type": "string",
          "minLength": 1,
          "maxLength": 240
        },
        "canonicalization": {
          "type": "string",
          "minLength": 1,
          "maxLength": 120
        },
        "hashAlgorithm": {
          "const": "sha-256"
        },
        "requestHash": {
          "type": "string",
          "pattern": "^sha256:[a-f0-9]{64}$"
        },
        "idempotencyKeyDigest": {
          "type": [
            "string",
            "null"
          ],
          "pattern": "^sha256:[a-f0-9]{64}$"
        }
      },
      "additionalProperties": false
    },
    "authorization": {
      "type": "object",
      "required": [
        "state"
      ],
      "properties": {
        "state": {
          "enum": [
            "not_checked",
            "within_scope",
            "outside_scope",
            "review_required",
            "unknown",
            "not_applicable"
          ]
        },
        "reference": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 300
        },
        "receiptDigest": {
          "type": [
            "string",
            "null"
          ],
          "pattern": "^sha256:[a-f0-9]{64}$"
        }
      },
      "additionalProperties": true
    },
    "counterparty": {
      "type": "object",
      "required": [
        "state"
      ],
      "properties": {
        "state": {
          "enum": [
            "not_checked",
            "consistent",
            "conflict",
            "review_required",
            "unknown",
            "not_applicable"
          ]
        },
        "reference": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 300
        },
        "receiptDigest": {
          "type": [
            "string",
            "null"
          ],
          "pattern": "^sha256:[a-f0-9]{64}$"
        }
      },
      "additionalProperties": true
    },
    "reversibility": {
      "type": "object",
      "required": [
        "state"
      ],
      "properties": {
        "state": {
          "enum": [
            "not_checked",
            "reversible",
            "conditionally_reversible",
            "irreversible",
            "review_required",
            "unknown",
            "not_applicable"
          ]
        },
        "reference": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 300
        },
        "receiptDigest": {
          "type": [
            "string",
            "null"
          ],
          "pattern": "^sha256:[a-f0-9]{64}$"
        }
      },
      "additionalProperties": true
    },
    "payment": {
      "type": "object",
      "required": [
        "state"
      ],
      "properties": {
        "state": {
          "enum": [
            "not_required",
            "challenge_issued",
            "attempted",
            "settled",
            "failed",
            "unknown"
          ]
        },
        "protocol": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 80
        },
        "method": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 80
        },
        "currency": {
          "type": [
            "string",
            "null"
          ],
          "pattern": "^[A-Z0-9._:-]{1,24}$"
        },
        "amountMinor": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        },
        "asset": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 80
        },
        "network": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 120
        },
        "referenceDigest": {
          "type": [
            "string",
            "null"
          ],
          "pattern": "^sha256:[a-f0-9]{64}$"
        },
        "settledAt": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        }
      },
      "additionalProperties": false
    },
    "delivery": {
      "type": "object",
      "required": [
        "state"
      ],
      "properties": {
        "state": {
          "enum": [
            "not_attempted",
            "delivered",
            "partial",
            "mismatch",
            "failed",
            "unknown"
          ]
        },
        "promisedResultSchema": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 240
        },
        "observedResultSchema": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 240
        },
        "resultDigest": {
          "type": [
            "string",
            "null"
          ],
          "pattern": "^sha256:[a-f0-9]{64}$"
        },
        "resultReference": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 500
        },
        "matchedFields": {
          "type": "array",
          "items": {
            "type": "string",
            "maxLength": 240
          },
          "uniqueItems": true
        },
        "missingFields": {
          "type": "array",
          "items": {
            "type": "string",
            "maxLength": 240
          },
          "uniqueItems": true
        },
        "completedAt": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        },
        "contract": {
          "type": "object",
          "required": [
            "schemaVersion",
            "operation",
            "sourceOperation",
            "acceptedHttpStatuses",
            "promisedResultSchema",
            "requiredFieldPaths",
            "resultDigestRequired",
            "maxDeliveryLatencyMs",
            "offerDigest",
            "contractUrl",
            "inputSchemaUrl",
            "resultSchemaUrl"
          ],
          "properties": {
            "schemaVersion": {
              "const": "davisville-delivery-contract.v1"
            },
            "operation": {
              "const": "delivery.check_result_contract"
            },
            "sourceOperation": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200
            },
            "acceptedHttpStatuses": {
              "type": "array",
              "minItems": 1,
              "uniqueItems": true,
              "items": {
                "type": "integer",
                "minimum": 100,
                "maximum": 599
              }
            },
            "promisedResultSchema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 240
            },
            "requiredFieldPaths": {
              "type": "array",
              "uniqueItems": true,
              "items": {
                "type": "string",
                "maxLength": 500
              }
            },
            "resultDigestRequired": {
              "const": true
            },
            "maxDeliveryLatencyMs": {
              "type": [
                "integer",
                "null"
              ],
              "minimum": 0,
              "maximum": 86400000
            },
            "offerDigest": {
              "type": "string",
              "pattern": "^sha256:[a-f0-9]{64}$"
            },
            "contractUrl": {
              "const": "https://davisvillelabs.com/agents/delivery-contract-v1.json"
            },
            "inputSchemaUrl": {
              "const": "https://davisvillelabs.com/agents/davisville-delivery-check-input.v1.schema.json"
            },
            "resultSchemaUrl": {
              "const": "https://davisvillelabs.com/agents/davisville-delivery-check-result.v1.schema.json"
            }
          },
          "additionalProperties": false
        },
        "conformance": {
          "type": "object",
          "required": [
            "operation",
            "state",
            "checkDigest",
            "uncertainty"
          ],
          "properties": {
            "operation": {
              "const": "delivery.check_result_contract"
            },
            "state": {
              "enum": [
                "delivered",
                "partial",
                "mismatch",
                "unknown"
              ]
            },
            "checkDigest": {
              "type": "string",
              "pattern": "^sha256:[a-f0-9]{64}$"
            },
            "uncertainty": {
              "enum": [
                "none",
                "known_gaps",
                "unknown"
              ]
            }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": true
    },
    "outcome": {
      "type": "object",
      "required": [
        "status",
        "billable",
        "uncertainty"
      ],
      "properties": {
        "status": {
          "enum": [
            "success",
            "partial",
            "rejected",
            "abstained",
            "failed",
            "unknown"
          ]
        },
        "billable": {
          "type": "boolean"
        },
        "uncertainty": {
          "enum": [
            "none",
            "known_gaps",
            "unknown"
          ]
        },
        "reasonCodes": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[a-zA-Z0-9_.:-]{1,128}$"
          },
          "uniqueItems": true
        }
      },
      "additionalProperties": true
    },
    "evidenceReferences": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "id",
          "type"
        ],
        "properties": {
          "id": {
            "type": "string",
            "minLength": 1,
            "maxLength": 240
          },
          "type": {
            "type": "string",
            "minLength": 1,
            "maxLength": 160
          },
          "uri": {
            "type": [
              "string",
              "null"
            ],
            "format": "uri"
          },
          "digest": {
            "type": [
              "string",
              "null"
            ],
            "pattern": "^sha256:[a-f0-9]{64}$"
          }
        },
        "additionalProperties": true
      }
    },
    "freshness": {
      "type": "object",
      "required": [
        "evaluatedAt",
        "state"
      ],
      "properties": {
        "evaluatedAt": {
          "type": "string",
          "format": "date-time"
        },
        "evidenceCutoffAt": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        },
        "state": {
          "enum": [
            "current",
            "stale",
            "cutoff_limited",
            "unknown",
            "not_applicable"
          ]
        }
      },
      "additionalProperties": true
    },
    "lineage": {
      "type": "object",
      "properties": {
        "predecessorReceiptIds": {
          "type": "array",
          "items": {
            "type": "string",
            "maxLength": 240
          },
          "uniqueItems": true
        },
        "relatedReceiptIds": {
          "type": "array",
          "items": {
            "type": "string",
            "maxLength": 240
          },
          "uniqueItems": true
        }
      },
      "additionalProperties": true
    },
    "limitations": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 1000
      }
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "expiresAt": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time"
    },
    "verification": {
      "type": "object",
      "required": [
        "canonicalization",
        "digestAlgorithm",
        "digestScope",
        "contentDigest",
        "signature"
      ],
      "properties": {
        "canonicalization": {
          "const": "davisville-receipt-canonical-json.v1"
        },
        "digestAlgorithm": {
          "const": "sha-256"
        },
        "digestScope": {
          "const": "envelope_without_verification_contentDigest_signature_coveredContentDigest_and_value"
        },
        "contentDigest": {
          "type": "string",
          "pattern": "^sha256:[a-f0-9]{64}$"
        },
        "signature": {
          "type": "object",
          "required": [
            "state"
          ],
          "properties": {
            "state": {
              "enum": [
                "not_provided",
                "present"
              ]
            },
            "format": {
              "type": [
                "string",
                "null"
              ],
              "enum": [
                "jwt",
                "jws_compact",
                "raw_ed25519",
                "other",
                null
              ]
            },
            "algorithm": {
              "type": [
                "string",
                "null"
              ],
              "maxLength": 80
            },
            "keyId": {
              "type": [
                "string",
                "null"
              ],
              "maxLength": 300
            },
            "publicKeyUrl": {
              "type": [
                "string",
                "null"
              ],
              "format": "uri"
            },
            "coveredContentDigest": {
              "type": [
                "string",
                "null"
              ],
              "pattern": "^sha256:[a-f0-9]{64}$"
            },
            "value": {
              "type": [
                "string",
                "null"
              ],
              "maxLength": 24000
            }
          },
          "allOf": [
            {
              "if": {
                "properties": {
                  "state": {
                    "const": "present"
                  }
                },
                "required": [
                  "state"
                ]
              },
              "then": {
                "required": [
                  "format",
                  "algorithm",
                  "keyId",
                  "coveredContentDigest",
                  "value"
                ]
              }
            }
          ],
          "additionalProperties": true
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": true
}
