{
  "openapi": "3.1.0",
  "info": {
    "title": "TWIX Data Attestation Device API",
    "version": "0.2.0",
    "description": "TDAP v0.1 smart-meter/device ingestion and proof API. Device authentication is cryptographic: devices sign canonical records with Ed25519; private device keys are never sent to this service."
  },
  "servers": [
    {
      "url": "https://validator.twixchain.com"
    }
  ],
  "tags": [
    {
      "name": "Device ingestion"
    },
    {
      "name": "Verification"
    },
    {
      "name": "Network"
    },
    {
      "name": "Playground"
    }
  ],
  "paths": {
    "/api/ingest.php": {
      "post": {
        "tags": [
          "Device ingestion"
        ],
        "summary": "Submit a signed TDAP record",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SignedEnvelope"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Accepted"
          },
          "400": {
            "description": "Invalid record/signature/sequence"
          },
          "413": {
            "description": "Payload over 64 KiB"
          },
          "429": {
            "description": "Rate limited"
          }
        }
      }
    },
    "/api/status.php": {
      "get": {
        "tags": [
          "Network"
        ],
        "summary": "Platform and TWIX network status",
        "responses": {
          "200": {
            "description": "Status"
          }
        }
      }
    },
    "/api/metrics.php": {
      "get": {
        "tags": [
          "Network"
        ],
        "summary": "Public aggregate API metrics",
        "responses": {
          "200": {
            "description": "Metrics"
          }
        }
      }
    },
    "/api/device.php": {
      "get": {
        "tags": [
          "Verification"
        ],
        "summary": "Read a registered device public identity",
        "parameters": [
          {
            "name": "id",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Device public key and state"
          },
          "404": {
            "description": "Unknown/expired device"
          }
        }
      }
    },
    "/api/proof.php": {
      "get": {
        "tags": [
          "Verification"
        ],
        "summary": "Retrieve a public TDAP proof bundle",
        "parameters": [
          {
            "name": "hash",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[a-f0-9]{64}$"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Proof bundle"
          },
          "403": {
            "description": "Record visibility does not permit public proof download"
          },
          "404": {
            "description": "Unknown hash"
          }
        }
      }
    },
    "/api/batch.php": {
      "get": {
        "tags": [
          "Verification"
        ],
        "summary": "Retrieve a Merkle batch and anchor state",
        "parameters": [
          {
            "name": "root",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[a-f0-9]{64}$"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Batch"
          },
          "404": {
            "description": "Unknown root"
          }
        }
      }
    },
    "/api/playground-device.php": {
      "post": {
        "tags": [
          "Playground"
        ],
        "summary": "Register a 24-hour browser playground identity",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "public_key"
                ],
                "properties": {
                  "public_key": {
                    "type": "string",
                    "description": "32-byte Ed25519 public key encoded base64url"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Temporary device and bearer-style playground token"
          },
          "429": {
            "description": "Creation rate limited"
          }
        }
      }
    },
    "/api/playground-batch.php": {
      "post": {
        "tags": [
          "Playground"
        ],
        "summary": "Merkle-batch records for a temporary playground device",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "device_id",
                  "token"
                ],
                "properties": {
                  "device_id": {
                    "type": "string"
                  },
                  "token": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Batch created"
          },
          "400": {
            "description": "Invalid or expired playground session"
          },
          "429": {
            "description": "Rate limited"
          }
        }
      }
    },
    "/api/playground-confirm-anchor.php": {
      "post": {
        "tags": [
          "Playground"
        ],
        "summary": "Verify and persist a playground TWIX anchor transaction",
        "description": "Requires the temporary playground token and verifies destination, selector, calldata commitments, receipt success, block height and block time against TWIX EVM RPC.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "device_id",
                  "token",
                  "batch_id",
                  "tx_hash"
                ],
                "properties": {
                  "device_id": {
                    "type": "string"
                  },
                  "token": {
                    "type": "string"
                  },
                  "batch_id": {
                    "type": "integer"
                  },
                  "tx_hash": {
                    "type": "string",
                    "pattern": "^0x[a-f0-9]{64}$"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Pending or confirmed anchor"
          },
          "400": {
            "description": "Invalid batch/transaction/session"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Record": {
        "type": "object",
        "required": [
          "protocol",
          "device_id",
          "sequence",
          "schema",
          "measured_at",
          "previous_hash",
          "payload"
        ],
        "properties": {
          "protocol": {
            "const": "twix-dap/0.1"
          },
          "device_id": {
            "type": "string"
          },
          "sequence": {
            "type": "integer",
            "minimum": 1
          },
          "schema": {
            "type": "string"
          },
          "measured_at": {
            "type": "string",
            "format": "date-time"
          },
          "previous_hash": {
            "oneOf": [
              {
                "type": "null"
              },
              {
                "type": "string",
                "pattern": "^[a-f0-9]{64}$"
              }
            ]
          },
          "payload": {
            "type": "object",
            "description": "Precision-sensitive decimals should be strings, not JSON floating-point numbers."
          }
        }
      },
      "SignedEnvelope": {
        "type": "object",
        "required": [
          "record",
          "record_hash",
          "signature"
        ],
        "properties": {
          "record": {
            "$ref": "#/components/schemas/Record"
          },
          "record_hash": {
            "type": "string",
            "pattern": "^[a-f0-9]{64}$"
          },
          "signature": {
            "type": "string",
            "description": "Ed25519 signature over the raw 32-byte record hash, base64url encoded"
          }
        }
      }
    }
  },
  "x-tdap": {
    "protocol": "twix-dap/0.1",
    "canonicalization": "TWIX Canonical JSON v0.1",
    "recordHash": "SHA-256(UTF8(\"TWIX-DAP-RECORD-v0.1\") || 0x00 || UTF8(canonical_record))",
    "signature": "Ed25519 over raw 32-byte record hash",
    "testVector": {
      "canonicalJson": "{\"device_id\":\"meter:test:001\",\"measured_at\":\"2026-08-26T20:00:00Z\",\"payload\":{\"unit\":\"kWh\",\"value\":\"14832.771\"},\"previous_hash\":null,\"protocol\":\"twix-dap/0.1\",\"schema\":\"energy.meter.reading/1\",\"sequence\":1}",
      "recordHash": "3770ad8a5ff06fb05403926524dae336e3d1bf122ecdb145502822ab8ce03798"
    }
  }
}
