{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://rctp.dev/schemas/approval-request.schema.json",
  "title": "Approval request",
  "description": "Carried in the body of a 202 ask decision (Draft 0.4 9.2; binding 7). challenge = b64url:base64url(SHA-256(JCS(authorization_details))). For listing.create and listing.set_terms the challenge is computed after the hosted page submits the terms, over details that include terms_commitment, so the 202 body has none (B-4). display never contains private terms (INV-1).",
  "type": "object",
  "properties": {
    "approval_id": {
      "$ref": "common.schema.json#/$defs/ApprovalId"
    },
    "action": {
      "$ref": "common.schema.json#/$defs/Action"
    },
    "listing_hash": {
      "$ref": "common.schema.json#/$defs/Digest"
    },
    "amount": {
      "$ref": "common.schema.json#/$defs/Money"
    },
    "counterparty": {
      "$ref": "common.schema.json#/$defs/CounterpartyBands"
    },
    "display": {
      "$ref": "common.schema.json#/$defs/DisplayText"
    },
    "expires_at": {
      "$ref": "common.schema.json#/$defs/Timestamp"
    },
    "challenge": {
      "type": "string",
      "pattern": "^b64url:[A-Za-z0-9_-]{43}$"
    },
    "allowed_levels": {
      "type": "array",
      "items": {
        "$ref": "common.schema.json#/$defs/ApprovalLevel"
      },
      "minItems": 1,
      "uniqueItems": true
    },
    "hosted_url": {
      "type": "string",
      "format": "uri"
    },
    "authorization_details": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/AuthorizationDetail"
      },
      "minItems": 1,
      "maxItems": 1
    }
  },
  "additionalProperties": false,
  "required": [
    "approval_id",
    "action",
    "counterparty",
    "display",
    "expires_at",
    "allowed_levels",
    "authorization_details"
  ],
  "allOf": [
    {
      "if": {
        "properties": {
          "action": {
            "enum": [
              "listing.create",
              "listing.set_terms"
            ]
          }
        },
        "required": [
          "action"
        ]
      },
      "then": {
        "properties": {
          "authorization_details": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "term_set"
              ]
            }
          }
        }
      },
      "else": {
        "required": [
          "challenge"
        ]
      }
    }
  ],
  "$defs": {
    "AuthorizationDetail": {
      "type": "object",
      "properties": {
        "type": {
          "const": "dev.rctp.approval"
        },
        "approval_id": {
          "$ref": "common.schema.json#/$defs/ApprovalId"
        },
        "action": {
          "$ref": "common.schema.json#/$defs/Action"
        },
        "listing_hash": {
          "$ref": "common.schema.json#/$defs/Digest"
        },
        "amount": {
          "$ref": "common.schema.json#/$defs/Money"
        },
        "counterparty": {
          "$ref": "common.schema.json#/$defs/CounterpartyBands"
        },
        "display_digests": {
          "type": "object",
          "additionalProperties": {
            "$ref": "common.schema.json#/$defs/Digest"
          },
          "minProperties": 1
        },
        "expires_at": {
          "$ref": "common.schema.json#/$defs/Timestamp"
        },
        "allowed_levels": {
          "type": "array",
          "items": {
            "$ref": "common.schema.json#/$defs/ApprovalLevel"
          },
          "minItems": 1
        },
        "nonce": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_-]{22,}$"
        },
        "term_set": {
          "$ref": "common.schema.json#/$defs/TermSetId",
          "description": "listing.create and listing.set_terms only"
        },
        "terms_commitment": {
          "$ref": "common.schema.json#/$defs/Digest",
          "description": "listing.create and listing.set_terms only, once the seller has filled the terms form"
        }
      },
      "additionalProperties": false,
      "required": [
        "type",
        "approval_id",
        "action",
        "counterparty",
        "display_digests",
        "expires_at",
        "allowed_levels",
        "nonce"
      ],
      "description": "RFC 9396 authorization details object the challenge is bound to"
    }
  }
}
