{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "/assets/schemas/transaction-envelope.schema.json",
  "title": "Byteplane Unsigned Transaction Envelope",
  "description": "A deterministic local preview of an unsigned contract transaction. It records prepared data and preflight observations without claiming submission or finality.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "kind",
    "version",
    "mode",
    "origin",
    "status",
    "createdAt",
    "network",
    "transaction",
    "method",
    "preflight",
    "note",
    "digest"
  ],
  "properties": {
    "kind": { "const": "byteplane.unsigned-transaction-envelope" },
    "version": { "const": "1.0" },
    "mode": { "enum": ["local", "sample"] },
    "origin": { "const": "local" },
    "status": { "const": "not-submitted" },
    "createdAt": { "type": "string", "format": "date-time" },
    "network": {
      "type": "object",
      "additionalProperties": false,
      "required": ["chainId"],
      "properties": {
        "chainId": {
          "type": ["string", "null"],
          "pattern": "^0x(?:0|[1-9a-f][a-f0-9]*)$"
        }
      }
    },
    "transaction": {
      "type": "object",
      "additionalProperties": false,
      "required": ["from", "to", "value", "data", "nonce"],
      "properties": {
        "from": {
          "type": ["string", "null"],
          "pattern": "^0x[a-fA-F0-9]{40}$"
        },
        "to": {
          "type": ["string", "null"],
          "pattern": "^0x[a-fA-F0-9]{40}$"
        },
        "value": {
          "type": "string",
          "pattern": "^0x(?:0|[1-9a-f][a-f0-9]*)$"
        },
        "data": {
          "type": "string",
          "pattern": "^0x(?:[a-f0-9]{2})*$"
        },
        "nonce": {
          "type": ["string", "null"],
          "pattern": "^0x(?:0|[1-9a-f][a-f0-9]*)$"
        }
      }
    },
    "method": {
      "oneOf": [
        { "type": "null" },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["name", "signature", "selector", "arguments"],
          "properties": {
            "name": { "type": "string", "minLength": 1 },
            "signature": { "type": "string", "minLength": 3 },
            "selector": {
              "type": ["string", "null"],
              "pattern": "^0x[a-f0-9]{8}$"
            },
            "arguments": { "type": "array" }
          }
        }
      ]
    },
    "preflight": {
      "type": "object",
      "additionalProperties": false,
      "required": ["call", "gasEstimate", "warnings"],
      "properties": {
        "call": { "$ref": "#/$defs/check" },
        "gasEstimate": { "$ref": "#/$defs/check" },
        "warnings": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        }
      }
    },
    "note": { "type": "string", "minLength": 1 },
    "digest": {
      "type": "string",
      "pattern": "^sha256:[a-f0-9]{64}$",
      "description": "SHA-256 of the canonical envelope after removing this digest field."
    }
  },
  "$defs": {
    "check": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "result", "message"],
      "properties": {
        "status": { "enum": ["not-run", "passed", "failed"] },
        "result": { "type": ["string", "null"] },
        "message": { "type": ["string", "null"] }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": { "mode": { "const": "sample" } },
        "required": ["mode"]
      },
      "then": {
        "properties": {
          "network": {
            "properties": { "chainId": { "type": "null" } }
          },
          "transaction": {
            "properties": { "to": { "type": "null" } }
          }
        }
      }
    },
    {
      "if": {
        "properties": { "mode": { "const": "local" } },
        "required": ["mode"]
      },
      "then": {
        "properties": {
          "network": {
            "properties": { "chainId": { "type": "string" } }
          },
          "transaction": {
            "properties": { "to": { "type": "string" } }
          }
        }
      }
    }
  ]
}
