{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://musicairegister.org/mard/0.2/schema.json",
  "title": "MARD 0.2 bundle or proof",
  "description": "The shape of a MARD 0.2 (Music AI Rights Declaration) bundle or single-work proof, following SPEC.md sections 3 to 10. A schema cannot express what the checker (verify.py) enforces: ISWC check digits, Unicode NFC and lone surrogates, repeated member names, the spelling of numbers (a checker refuses 1.0 and 1e0), the ordering and uniqueness of works by sort key, that a sort key is its work's ISWC, the Merkle root and audit paths, the Ed25519 signature, fingerprints matching keys, that each timestamp proof is for the signed file, real calendar dates and instants, ascending territories, and the cross-rules of SPEC 4.5 and 5 (an active declaration must declare something; a withdrawn one supersedes another and declares nothing; catalog_default must reserve a use; a work must not repeat the declaration's licensing_route; a work with nothing reserved needs a catalog_default). A file that passes this schema is not thereby a MARD file: run verify.py. A declaration is dated evidence of a public, machine-readable reservation; it does not stop anyone training a model.",
  "oneOf": [
    {
      "$ref": "#/$defs/bundle"
    },
    {
      "$ref": "#/$defs/proof_file"
    }
  ],
  "$defs": {
    "base64": {
      "type": "string",
      "pattern": "^[A-Za-z0-9+/]*={0,2}$",
      "description": "Standard base64 with padding."
    },
    "hash": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$",
      "description": "SHA-256, 64 lowercase hexadecimal characters."
    },
    "salt": {
      "type": "string",
      "pattern": "^[0-9a-f]{32}$",
      "description": "16 random bytes as 32 lowercase hexadecimal characters."
    },
    "uuid": {
      "type": "string",
      "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
    },
    "instant": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$",
      "description": "UTC to the second."
    },
    "date": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
    },
    "iswc": {
      "type": "string",
      "pattern": "^T[0-9]{10}$"
    },
    "isrc": {
      "type": "string",
      "pattern": "^[A-Z]{2}[A-Z0-9]{3}[0-9]{7}$"
    },
    "ipi_name": {
      "type": "string",
      "pattern": "^[0-9]{11}$"
    },
    "sort_key": {
      "type": "string",
      "pattern": "^(iswc:T[0-9]{10}|local:[A-Za-z0-9._-]{1,64})$"
    },
    "count": {
      "type": "integer",
      "minimum": 0,
      "maximum": 9007199254740991
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 200,
      "pattern": "^[^\\u0000-\\u001f\\u007f-\\u009f\\u061c\\u200e\\u200f\\u2028\\u2029\\u202a-\\u202e\\u2066-\\u2069]*$"
    },
    "uses": {
      "type": "object",
      "properties": {
        "UseForDataMining": {
          "enum": [
            "reserved",
            "not_stated"
          ]
        },
        "UseForTrainingGenerativeAI": {
          "enum": [
            "reserved",
            "not_stated"
          ]
        },
        "UseForTrainingNonGenerativeAI": {
          "enum": [
            "reserved",
            "not_stated"
          ]
        }
      },
      "required": [
        "UseForDataMining",
        "UseForTrainingGenerativeAI",
        "UseForTrainingNonGenerativeAI"
      ],
      "additionalProperties": false,
      "description": "Exactly the three DDEX allowed value set 12 UseType names."
    },
    "route": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "enquiries": {
              "const": "closed"
            }
          },
          "required": [
            "enquiries"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "enquiries": {
              "const": "direct"
            },
            "contact": {
              "type": "string",
              "minLength": 1,
              "maxLength": 300,
              "pattern": "^(?:(?:https://[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?(?::[0-9]{1,5})?(?:[/?#][\\x21-\\x7e]*)?)|(?:[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]{1,64}@[A-Za-z0-9-]+(?:\\.[A-Za-z0-9-]+)+))$",
              "description": "An https address with a plain host, or a plain email address (SPEC 4.3)."
            }
          },
          "required": [
            "enquiries",
            "contact"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "enquiries": {
              "const": "via_society"
            },
            "society": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200,
              "pattern": "^[^\\u0000-\\u001f\\u007f-\\u009f\\u061c\\u200e\\u200f\\u2028\\u2029\\u202a-\\u202e\\u2066-\\u2069]*$"
            }
          },
          "required": [
            "enquiries"
          ],
          "additionalProperties": false
        }
      ]
    },
    "declarant": {
      "type": "object",
      "properties": {
        "name": {
          "$ref": "#/$defs/name"
        },
        "role": {
          "enum": [
            "publisher",
            "writer",
            "administrator",
            "agent_for_members",
            null
          ],
          "description": "null when no role is stated. A role is never assumed."
        },
        "identifiers": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "object",
            "properties": {
              "scheme": {
                "const": "ipi_name"
              },
              "value": {
                "$ref": "#/$defs/ipi_name"
              }
            },
            "required": [
              "scheme",
              "value"
            ],
            "additionalProperties": false
          }
        }
      },
      "required": [
        "name",
        "role",
        "identifiers"
      ],
      "additionalProperties": false,
      "description": "The declarant's own statement unless an attestation says otherwise."
    },
    "territories": {
      "oneOf": [
        {
          "const": [
            "world"
          ]
        },
        {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^[A-Z]{2}$"
          }
        }
      ]
    },
    "attestation": {
      "type": "object",
      "properties": {
        "type": {
          "enum": [
            "email_domain",
            "domain_matches_public_record",
            "society_acknowledgement",
            "registration_rating"
          ]
        },
        "value": {
          "type": "string",
          "minLength": 1,
          "maxLength": 300,
          "pattern": "^[^\\u0000-\\u001f\\u007f-\\u009f\\u061c\\u200e\\u200f\\u2028\\u2029\\u202a-\\u202e\\u2066-\\u2069]*$"
        },
        "checked_at": {
          "$ref": "#/$defs/date"
        },
        "details": {
          "type": "object",
          "minProperties": 1,
          "maxProperties": 16,
          "propertyNames": {
            "pattern": "^[a-z][a-z0-9_]{0,31}$"
          },
          "additionalProperties": {
            "oneOf": [
              {
                "type": "string",
                "minLength": 1,
                "maxLength": 300,
                "pattern": "^[^\\u0000-\\u001f\\u007f-\\u009f\\u061c\\u200e\\u200f\\u2028\\u2029\\u202a-\\u202e\\u2066-\\u2069]*$"
              },
              {
                "type": "integer",
                "minimum": 0,
                "maximum": 9007199254740991
              }
            ]
          },
          "description": "Optional. A flat object of text and whole numbers; SPEC 4.4 lists the names defined for each type. A checker accepts a name it does not know."
        }
      },
      "required": [
        "type",
        "value",
        "checked_at"
      ],
      "additionalProperties": false
    },
    "declaration": {
      "type": "object",
      "properties": {
        "format": {
          "const": "mard/0.2"
        },
        "declaration_id": {
          "$ref": "#/$defs/uuid"
        },
        "stream": {
          "type": "string",
          "pattern": "^[A-Za-z0-9._~-]{1,128}$"
        },
        "supersedes": {
          "oneOf": [
            {
              "$ref": "#/$defs/uuid"
            },
            {
              "type": "null"
            }
          ]
        },
        "status": {
          "enum": [
            "active",
            "withdrawn"
          ]
        },
        "declared_at": {
          "$ref": "#/$defs/instant"
        },
        "declarant": {
          "$ref": "#/$defs/declarant"
        },
        "issuer": {
          "type": "object",
          "properties": {
            "name": {
              "$ref": "#/$defs/name"
            },
            "signing_key": {
              "$ref": "#/$defs/hash"
            }
          },
          "required": [
            "name",
            "signing_key"
          ],
          "additionalProperties": false
        },
        "authority": {
          "type": "object",
          "properties": {
            "territories": {
              "$ref": "#/$defs/territories"
            }
          },
          "required": [
            "territories"
          ],
          "additionalProperties": false
        },
        "catalog_default": {
          "oneOf": [
            {
              "type": "null"
            },
            {
              "type": "object",
              "properties": {
                "uses": {
                  "$ref": "#/$defs/uses"
                },
                "confirmed_at": {
                  "$ref": "#/$defs/instant"
                }
              },
              "required": [
                "uses",
                "confirmed_at"
              ],
              "additionalProperties": false
            }
          ]
        },
        "licensing_route": {
          "$ref": "#/$defs/route"
        },
        "works_commitment": {
          "type": "object",
          "properties": {
            "tree": {
              "const": "rfc6962-sha256"
            },
            "work_count": {
              "$ref": "#/$defs/count"
            },
            "root": {
              "$ref": "#/$defs/hash"
            }
          },
          "required": [
            "tree",
            "work_count",
            "root"
          ],
          "additionalProperties": false
        },
        "attestations": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/attestation"
          }
        }
      },
      "required": [
        "format",
        "declaration_id",
        "stream",
        "supersedes",
        "status",
        "declared_at",
        "declarant",
        "issuer",
        "authority",
        "catalog_default",
        "licensing_route",
        "works_commitment",
        "attestations"
      ],
      "additionalProperties": false
    },
    "issuer_key": {
      "type": "object",
      "properties": {
        "algorithm": {
          "const": "ed25519"
        },
        "public_key": {
          "allOf": [
            {
              "$ref": "#/$defs/base64"
            },
            {
              "minLength": 44,
              "maxLength": 44
            }
          ],
          "description": "Base64 of the raw 32-byte public key."
        },
        "fingerprint": {
          "$ref": "#/$defs/hash"
        },
        "holder": {
          "$ref": "#/$defs/name"
        },
        "valid_from": {
          "$ref": "#/$defs/date"
        }
      },
      "required": [
        "algorithm",
        "public_key",
        "fingerprint",
        "holder",
        "valid_from"
      ],
      "description": "An issuer may add members of its own; a checker ignores them.",
      "additionalProperties": {
        "$ref": "#/$defs/any"
      }
    },
    "work": {
      "type": "object",
      "properties": {
        "title": {
          "type": "string",
          "minLength": 0,
          "maxLength": 1000,
          "pattern": "^[^\\u0000-\\u001f\\u007f-\\u009f\\u061c\\u200e\\u200f\\u2028\\u2029\\u202a-\\u202e\\u2066-\\u2069]*$"
        },
        "iswc": {
          "oneOf": [
            {
              "$ref": "#/$defs/iswc"
            },
            {
              "type": "null"
            }
          ]
        },
        "components": {
          "enum": [
            [
              "music"
            ],
            [
              "lyrics"
            ],
            [
              "music",
              "lyrics"
            ]
          ]
        },
        "embodied_in_isrcs": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/isrc"
          }
        },
        "writers_commitment": {
          "oneOf": [
            {
              "$ref": "#/$defs/hash"
            },
            {
              "type": "null"
            }
          ]
        },
        "uses": {
          "$ref": "#/$defs/uses"
        },
        "licensing_route": {
          "$ref": "#/$defs/route"
        }
      },
      "required": [
        "title",
        "iswc",
        "components",
        "embodied_in_isrcs",
        "writers_commitment",
        "uses"
      ],
      "additionalProperties": false
    },
    "record": {
      "type": "object",
      "properties": {
        "salt": {
          "$ref": "#/$defs/salt"
        },
        "sort_key": {
          "$ref": "#/$defs/sort_key"
        },
        "work": {
          "$ref": "#/$defs/work"
        }
      },
      "required": [
        "salt",
        "sort_key",
        "work"
      ],
      "additionalProperties": false
    },
    "timestamp": {
      "type": "object",
      "required": [
        "type"
      ],
      "properties": {
        "type": {
          "type": "string",
          "minLength": 1,
          "maxLength": 50,
          "pattern": "^[^\\u0000-\\u001f\\u007f-\\u009f\\u061c\\u200e\\u200f\\u2028\\u2029\\u202a-\\u202e\\u2066-\\u2069]*$"
        }
      },
      "if": {
        "properties": {
          "type": {
            "const": "opentimestamps"
          }
        }
      },
      "then": {
        "type": "object",
        "properties": {
          "type": {
            "const": "opentimestamps"
          },
          "proof": {
            "$ref": "#/$defs/base64"
          }
        },
        "required": [
          "type",
          "proof"
        ],
        "additionalProperties": false
      },
      "description": "Types other than opentimestamps are allowed: a checker reports them as not checked.",
      "additionalProperties": {
        "$ref": "#/$defs/any"
      }
    },
    "timestamps": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/timestamp"
      }
    },
    "path": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/hash"
      }
    },
    "neighbour": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "properties": {
            "leaf_index": {
              "$ref": "#/$defs/count"
            },
            "leaf": {
              "type": "object",
              "properties": {
                "salt": {
                  "$ref": "#/$defs/salt"
                },
                "sort_key": {
                  "$ref": "#/$defs/sort_key"
                },
                "work_sha256": {
                  "$ref": "#/$defs/hash"
                }
              },
              "required": [
                "salt",
                "sort_key",
                "work_sha256"
              ],
              "additionalProperties": false
            },
            "audit_path": {
              "$ref": "#/$defs/path"
            }
          },
          "required": [
            "leaf_index",
            "leaf",
            "audit_path"
          ],
          "additionalProperties": false
        }
      ]
    },
    "single_proof": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "type": {
              "const": "inclusion"
            },
            "leaf_index": {
              "$ref": "#/$defs/count"
            },
            "leaf": {
              "$ref": "#/$defs/record"
            },
            "audit_path": {
              "$ref": "#/$defs/path"
            }
          },
          "required": [
            "type",
            "leaf_index",
            "leaf",
            "audit_path"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "const": "absence"
            },
            "sort_key": {
              "$ref": "#/$defs/sort_key"
            },
            "left": {
              "$ref": "#/$defs/neighbour"
            },
            "right": {
              "$ref": "#/$defs/neighbour"
            }
          },
          "required": [
            "type",
            "sort_key",
            "left",
            "right"
          ],
          "additionalProperties": false
        }
      ]
    },
    "bundle": {
      "type": "object",
      "properties": {
        "format": {
          "const": "mard-bundle/0.2"
        },
        "declaration": {
          "$ref": "#/$defs/declaration"
        },
        "signature": {
          "allOf": [
            {
              "$ref": "#/$defs/base64"
            },
            {
              "minLength": 88,
              "maxLength": 88
            }
          ]
        },
        "issuer_key": {
          "$ref": "#/$defs/issuer_key"
        },
        "timestamps": {
          "$ref": "#/$defs/timestamps"
        },
        "works": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/record"
          }
        }
      },
      "required": [
        "format",
        "declaration",
        "signature",
        "issuer_key",
        "timestamps",
        "works"
      ],
      "additionalProperties": false
    },
    "proof_file": {
      "type": "object",
      "properties": {
        "format": {
          "const": "mard-proof/0.2"
        },
        "declaration": {
          "$ref": "#/$defs/declaration"
        },
        "signature": {
          "allOf": [
            {
              "$ref": "#/$defs/base64"
            },
            {
              "minLength": 88,
              "maxLength": 88
            }
          ]
        },
        "issuer_key": {
          "$ref": "#/$defs/issuer_key"
        },
        "timestamps": {
          "$ref": "#/$defs/timestamps"
        },
        "proof": {
          "$ref": "#/$defs/single_proof"
        }
      },
      "required": [
        "format",
        "declaration",
        "signature",
        "issuer_key",
        "timestamps",
        "proof"
      ],
      "additionalProperties": false
    },
    "any": {
      "description": "Any JSON value the format allows where an issuer may add members of its own: whole numbers from 0 to 9007199254740991 only.",
      "anyOf": [
        {
          "type": "null"
        },
        {
          "type": "boolean"
        },
        {
          "type": "string",
          "pattern": "^[^\\u0000-\\u001f\\u007f-\\u009f\\u061c\\u200e\\u200f\\u2028\\u2029\\u202a-\\u202e\\u2066-\\u2069]*$"
        },
        {
          "type": "integer",
          "minimum": 0,
          "maximum": 9007199254740991
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/any"
          }
        },
        {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/any"
          }
        }
      ]
    }
  }
}
