# GitHub Repository and Updates

This document defines how NextGram distributes plugins through GitHub. It covers the
repository layout, catalog metadata, signed index, app flow, moderation and updates.
It replaces the earlier "signed catalog on `plugins.nextgram.dev`" sketch.

Status: **app client, UI, updates, revocations and publishing tooling implemented and
verified against a signed catalog**. The repository scaffold, index builder, signing
tools and release workflows live in the separate `nextgram-plugins` repository;
the catalog repository is public and the official public key is pinned in the app.
Better Blur is ready to publish; the first release is waiting for the first-party
signing secrets. Custom repositories are deferred to the next release.

## 1. Principles

1. **GitHub is transport, not trust.** The app trusts a signed index key and each
   plugin's signing key, not the repository owner.
2. **Every package is verified by the existing verifier.** A repository entry cannot
   bypass manifest, ZIP, DEX, permission or signature checks.
3. **No silent installs.** A repository install or update always opens the consent
   screen. Updates show the permission diff.
4. **Immutable downloads.** An artifact URL points to a GitHub Release asset or a
   commit-pinned raw file. The app also checks SHA-256 from the signed index.
5. **Source stays reviewable.** The repository keeps metadata, source links, icons and
   screenshots. Large `.ngp` binaries live in GitHub Releases.
6. **Repository compromise is contained.** A stolen index key can mark bad plugins as
   verified, but cannot sign a package update without the publisher key.
7. **Manual fallback always works.** A `.ngp` from a chat, a file manager or the file
   picker installs without any repository.

## 2. Default repository

| Item | Value |
| --- | --- |
| Repository | `https://github.com/gitwelk/nextgram-plugins` |
| Default branch | `main` |
| Index | `catalog/index.json` |
| Index signature | `catalog/index.sig` |
| Revocations | `catalog/revocations.json` + `catalog/revocations.sig`, published manually when needed |
| Metadata | `plugins/<plugin-id>/entry.json` |
| Artifacts | GitHub Releases, tag `<plugin-id>-v<version>` |
| App entry point | **NextGram Settings → Plugins → Repository** |

The official repository key fingerprint is pinned in the app. The key is used only by
the release workflow. Pull request validation runs with read-only permissions and no
signing secrets.

## 3. Repository layout

```text
nextgram-plugins/
  README.md
  CONTRIBUTING.md
  KEYS.md
  LICENSE
  AGENTS.md
  .github/
    pull_request_template.md
    workflows/validate.yml
    workflows/auto-release.yml
    workflows/tag-release.yml
    workflows/release-plugin.yml
    workflows/publish.yml
  catalog/
    index.json
    index.sig
    revocations.json
    revocations.sig
  plugins/
    <plugin-id>/
      entry.json
      icon.png
      screenshots/
        main.webp
      README.md
  templates/
    entry.json
  schemas/
    catalog-entry.schema.json
    index.schema.json
    revocations.schema.json
  tools/
    validate_catalog.py
    prepare_release.py
    detect_pending_releases.py
    build_index.py
    sign_index.py
    verify_index.py
    generate_key.py
```

`catalog/index.json` and `catalog/index.sig` are generated by CI. The revocation feed
is published by the maintainers with the same signing key. Do not edit the generated
files by hand. A pull request changes `plugins/<id>/entry.json`, images and
documentation only.

A plugin does not have to include source in this repository. `entry.json` must link to
the source repository and to the exact commit or tag that produced the artifact. Source
mirrors are welcome when the author agrees.

## 4. Catalog entry

`plugins/<id>/entry.json` is the human-edited catalog record. The plugin package has
its own `plugin.json`; this file adds presentation and release information.

```json
{
  "schemaVersion": 1,
  "id": "dev.example.hello",
  "name": "Hello NextGram",
  "summary": "A minimal example plugin with a settings screen and a message action.",
  "category": "examples",
  "tags": ["sample", "settings", "chat"],
  "license": "GPL-3.0",
  "source": "https://github.com/gitwelk/nextgram-plugins/tree/main/plugins/dev.example.hello",
  "homepage": "https://github.com/gitwelk/NextGram",
  "support": "https://github.com/gitwelk/nextgram-plugins/issues",
  "authors": [
    {"name": "NextGram", "url": "https://github.com/gitwelk"}
  ],
  "icon": "icon.png",
  "screenshots": ["screenshots/settings.webp"],
  "presentation": {
    "accent": "#9D6BFF",
    "cover": "cover.webp"
  },
  "releases": [
    {
      "version": "1.0.0",
      "versionCode": 1,
      "tag": "dev.example.hello-v1.0.0",
      "asset": "dev.example.hello-1.0.0.ngp"
    }
  ]
}
```

Rules:

- `id` matches the directory name and the installed plugin ID. The `app.nextgram.*`
  namespace is reserved for official built-ins.
- `versionCode` increases for every release and matches the package manifest.
- `tag` and `asset` are required for a published release. `sha256`, `size` and
  `signatureFingerprint` are added by CI; a contributor may include them for review but
  CI remains authoritative.
- `source` is an HTTPS link to the source at the released commit.
- `license` is an SPDX identifier.
- `icon` is a PNG or WebP under the plugin directory, at most 2 MB and 1024×1024.
- `screenshots` contains at most 6 images, each at most 4 MB.
- `presentation` is optional. `accent` is `#RRGGBB`; `cover` is a PNG or WebP under the
  plugin directory. They style the repository card and details header only. They do not
  change the Telegram theme.
- Unknown fields are rejected by the validator until the schema is extended.

`entry.json` is not trusted at install time. The app reads the generated index and then
verifies the actual `.ngp`.

## 5. Generated index

CI builds `catalog/index.json` from all valid entries and release assets:

```json
{
  "format": "ngp-index",
  "formatVersion": 1,
  "repository": "https://github.com/gitwelk/nextgram-plugins",
  "generatedAt": "2026-12-03T12:00:00Z",
  "commit": "0123456789abcdef0123456789abcdef01234567",
  "plugins": [
    {
      "id": "dev.example.hello",
      "name": "Hello NextGram",
      "summary": "A minimal example plugin with a settings screen and a message action.",
      "category": "examples",
      "tags": ["sample", "settings", "chat"],
      "license": "GPL-3.0",
      "source": "https://github.com/gitwelk/nextgram-plugins/tree/0123456789abcdef0123456789abcdef01234567/plugins/dev.example.hello",
      "authors": [{"name": "NextGram", "url": "https://github.com/gitwelk"}],
      "iconUrl": "https://raw.githubusercontent.com/gitwelk/nextgram-plugins/0123456789abcdef0123456789abcdef01234567/plugins/dev.example.hello/icon.png",
      "coverUrl": "https://raw.githubusercontent.com/gitwelk/nextgram-plugins/0123456789abcdef0123456789abcdef01234567/plugins/dev.example.hello/cover.webp",
      "accent": "#9D6BFF",
      "latest": {
        "version": "1.0.0",
        "versionCode": 1,
        "api": 1,
        "minHost": ">=13.4.0",
        "downloadUrl": "https://github.com/gitwelk/nextgram-plugins/releases/download/dev.example.hello-v1.0.0/dev.example.hello-1.0.0.ngp",
        "sha256": "hex...",
        "size": 48213,
        "signatureFingerprint": "hex...",
        "publishedAt": "2026-12-03T11:00:00Z",
        "verified": true
      },
      "versions": []
    }
  ]
}
```

Index rules:

- `commit` is the repository commit that produced the index. Icon and cover URLs are
  pinned to it.
- `downloadUrl` is a GitHub Release asset URL. A self-contained repository may use a
  commit-pinned raw path instead; arbitrary mutable branch URLs are rejected.
- `verified` is attached to a release, not to a plugin forever. A new unreviewed version
  of a previously verified plugin is `verified: false`.
- `latest` is the highest published `versionCode`, not the highest semver string.
- `versions` keeps older releases for rollback and diagnostics. It is optional and may
  be truncated by CI.
- `index.sig` is a detached ECDSA P-256 signature over the exact bytes of
  `index.json`. The file contains base64-encoded ASN.1 DER with a trailing newline.
- `revocations.json` uses the format in [security.md](/security/) and has the same
  detached signature scheme.

## 6. App flow

### 6.1 Browse

1. The user opens **NextGram Settings → Plugins → Repository**.
2. The app fetches `catalog/index.json` and `catalog/index.sig` over HTTPS, with an
   `ETag`/`If-None-Match` cache.
3. The app verifies the index signature with the pinned key. A missing or invalid
   signature fails closed and shows an error.
4. The repository screen shows search, categories, featured entries and an **Updates**
   section. Icons and covers are loaded through the existing image loader with size
   limits and cached on disk.
5. Plugin details show name, summary, author, source link, license, screenshots,
   permissions, signature fingerprint, changelog and verified badge.

No account identifier, phone number, user ID or device ID is sent. The request is a
plain HTTPS GET.

### 6.2 Install

1. The app downloads the `.ngp` from `downloadUrl` with the 64 MB cap.
2. SHA-256 and size are checked against the signed index.
3. The file goes through `NgpVerifier` exactly like a chat or file-manager install:
   manifest, ZIP safety, limits, DEX, icon and signature.
4. The normal consent screen opens with source, permissions, key fingerprint and the
   trusted-code warning. For an update it also shows the permission diff and changelog.
5. Install is atomic. A failed update leaves the previous version working.

### 6.3 Update check

- **Check now** is always available in the plugin manager. Plugin details show an update
  action when a verified update is available.
- **Check daily** is opt-in and off by default. When enabled it runs at most once per 24
  hours and uses the same anonymous HTTPS request.
- An update is offered only when the repository index has the same plugin ID, a higher
  `versionCode`, a compatible `api`/`minHost` and the same package signing key.
- The update is never installed silently. Declining keeps the old version.
- If the index check fails, the current plugin stays installed and the error is shown.

### 6.4 Revocation

- Critical revocations disable the plugin as soon as a verified list is applied and again
  at the next start, with a non-dismissible notice.
- Non-critical revocations show a warning and recommend uninstalling.
- The user can override a non-critical revocation with an explicit warning. The override
  is stored per plugin, fingerprint and revocation version. Critical malware revocation
  is not overridable.
- Revocations are checked on repository refresh and on update checks, not silently in the
  background by default.

### 6.5 Custom repositories (deferred)

Custom repositories are not part of v1. The app uses the pinned official catalog only.
A future release can add an HTTPS repository URL with the same `catalog/index.json` and
`catalog/index.sig` layout, a fingerprint confirmation on first use, and the same rule
that a custom index cannot grant trust to a package signed by a different key.

## 7. Moderation

The repository is curated, but review is not a sandbox. Code plugins run as trusted
code; the warning stays in the installer.

A pull request must:

- add a valid `plugins/<id>/entry.json` and pass the schema;
- ship a signed `.ngp` release asset built from the linked source;
- use the same signing key as previous releases of that ID;
- increase `versionCode`;
- include an icon, a short summary and at least one screenshot for UI plugins;
- justify dangerous permissions in the pull request;
- include a license and source link;
- avoid native libraries, downloads of executable code and protected surfaces.

CI checks structure, schema, ID/version monotonicity, image limits, manifest and
signature of the artifact. A maintainer reviews behavior, permissions, source and
license. `verified: true` means "reviewed by the NextGram maintainers", not "safe".

Takedown and incident handling follow [security.md](/security/): remove the entry,
publish a revocation, notify users and keep the package hash for diagnostics.

## 8. Presentation metadata

`presentation.cover` and `presentation.accent` are optional catalog decoration:

- the cover appears in repository cards and the details header;
- the accent tints the plugin icon background and the install button;
- both are validated at index build time and ignored if invalid;
- neither changes Telegram theme colors, chat backgrounds or the app shell.

A future package-level cover can be added to `plugin.json` and `assets/` after the
theme runtime exists. Until then the catalog cover is display-only and is not copied
into the installed package.

## 9. Implementation order

1. **Repository scaffold** — done in `nextgram-plugins/`.
2. **Schema, validator and pull request workflow** — delivered, including tooling
   tests and revocation validation.
3. **Release workflow** — delivered in `publish.yml`: build the index from entries,
   read artifacts, sign `index.json`, commit the catalog. `release-plugin.yml` builds
   and signs first-party `dev.nextgram.*` packages, creates the GitHub Release and
   calls `publish-index`; `auto-release.yml` dispatches it when a merged
   `plugin.json` raises `versionCode`, and `tag-release.yml` does the same for a
   pushed `dev.nextgram.<id>-v<version>` tag. The matching public key is pinned in the app;
   the protected index and first-party signing secrets still have to be configured
   before the first release.
4. **App index client** — delivered: fetch, ETag cache, signature verification, strict
   parsing and a developer-mode unsigned fallback.
5. **Repository UI** — delivered: list, search, categories, details, download and
   install through the existing consent screen. Remote icons use an initial/accent
   fallback.
6. **Updates** — delivered: `versionCode` comparison, permission diff, changelog,
   opt-in daily check and explicit update install.
7. **Revocations** — delivered: verified refresh, critical disable, notices and
   non-critical override.
8. **Custom repositories** — trust-on-first-use and fingerprint display. Pending.
9. **Share to chat** — send an installed `.ngp` as a document. Pending.
10. **Moderation automation** — labels, review checklist, revocation tooling. Pending.

## 10. Open decisions

1. The custom repository trust model: trust-on-first-use or an explicit key import.
   Deferred to the next release.
2. Catalog governance: who reviews `verified` plugins and how takedowns are handled.
3. Whether CI builds third-party artifacts from source or only verifies author-built
   artifacts. Verification is the v1 baseline.
4. Final cover image dimensions and accent validation rules.
