# Build and sign

A production plugin is one signed `.ngp` file. The build command differs by kind; the
manifest, verifier and signature rules are identical.

## Choose the kind

| Kind | Payload | Signing | Restart after change |
| --- | --- | --- | --- |
| `ts` (`script`) | `script/main.js` | Same JAR signature | Not for script-only changes |
| `java` (`code`) | `android/dex/classes.dex` | Same JAR signature | Yes |
| `hybrid` | both | Same JAR signature | Yes when the DEX changes |

## TypeScript

From the plugin project:

```bash
nextgram-plugin build
nextgram-plugin pack --key key.pem --cert cert.pem
nextgram-plugin verify build/*.ngp
```

`pack` rebuilds, validates `plugin.json`, collects `plugin.json`, `assets/**`,
`script/main.js`, optional `android/dex/classes.dex`, `LICENSE`, `README.md`,
`CHANGELOG.md` and `i18n/**`, then writes a deterministic ZIP and signs it.

Use `--unsigned` only for developer-mode testing. An unsigned package carries a permanent
warning and is not accepted for a normal install.

## Java and Kotlin

Run these commands from `NextGram-android/`. The shell builder compiles the sources,
produces one DEX at `--min-api 23` and signs the package with JAR signing:

```bash
./gradlew :nextgram-plugin-sdk:compileJava :nextgram-plugin-sdk-android:assembleDebug
docs/plugins/tools/build-ngp.sh path/to/my-plugin hello.ngp
jarsigner -verify hello.ngp
```

Signing reads `NGP_KEYSTORE`, `NGP_KEY_ALIAS`, `NGP_STORE_PASS` and `NGP_KEY_PASS`:

```bash
export NGP_KEYSTORE="$PWD/keys/release.jks"
export NGP_KEY_ALIAS="nextgram"
export NGP_STORE_PASS="…"
export NGP_KEY_PASS="…"
docs/plugins/tools/build-ngp.sh path/to/my-plugin hello.ngp
```

Without `NGP_KEYSTORE` the script creates a temporary development key. Never publish a
package signed with it.

## Create a signing key

OpenSSL keys are used by the TypeScript CLI. EC P-256 is the recommended default:

```bash
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 \
  -keyout key.pem -out cert.pem -days 3650 -nodes \
  -subj "/CN=Your Plugin Publisher"
```

RSA 2048 or larger also works:

```bash
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out key.pem
openssl req -x509 -new -key key.pem -out cert.pem -days 3650 \
  -subj "/CN=Your Plugin Publisher"
```

For the Java builder, create a PKCS#12 keystore:

```bash
keytool -genkeypair -keystore release.jks -storetype PKCS12 \
  -storepass "$STORE_PASS" -keypass "$KEY_PASS" \
  -alias nextgram -keyalg RSA -keysize 2048 -validity 10000 \
  -dname "CN=Your Plugin Publisher"
```

Keep the private key out of the repository and back it up. Losing it means you cannot ship
an update to the same plugin ID; a different key is a different publisher.

## What the signature covers

The package uses standard JAR signing:

- `META-INF/MANIFEST.MF` carries a SHA-256 digest for every content entry;
- `META-INF/NGP.SF` carries `SHA-256-Digest-Manifest`;
- `META-INF/NGP.RSA` (RSA) or `META-INF/NGP.EC` (EC) carries a PKCS#7 detached
  signature over `NGP.SF`.

The verifier checks coverage: every content entry must be listed, and an entry added over
the signature is rejected. Updates must use the same signing key as the installed version
and a strictly higher `versionCode`. Downgrades are rejected.

## Package preflight

Before publishing, confirm:

- [ ] `id` is immutable, lowercase reverse-DNS and not under `app.nextgram.*`.
- [ ] `version` is semver; `versionCode` is higher than the installed and catalog versions.
- [ ] `plugin.json` is at most 256 KB and contains only documented fields.
- [ ] The payload matches the kind: only `script/` for `ts`, only `android/` for `java`,
      both for `hybrid`.
- [ ] The package is at most 64 MB; the uncompressed total is at most 192 MB; there are at
      most 4096 entries.
- [ ] `script/main.js` is at most 8 MB; `classes.dex` is at most 32 MB.
- [ ] `assets/icon.png` or `icon.webp` is at most 2 MB and at most 1024×1024.
- [ ] No native libraries, nested archives, executable scripts, symlinks or `..` paths.
- [ ] `verify` passes and `jarsigner -verify` reports a valid signature.
- [ ] The signing key matches the previous release of this plugin ID.

## Install and test

Open the `.ngp` from a file manager, a chat document, the share sheet or
**NextGram Settings → Plugins → Install from file**. The consent screen shows the publisher
fingerprint, version, permissions and the trusted-code warning.

Test the plugin manager path as well: enable, disable, re-enable, update over an older
version, then uninstall. A TypeScript-only plugin should reload live; a Java or hybrid
plugin needs a restart when its DEX changes.

## Publish

The signed `.ngp` is attached to a GitHub Release, never committed. Catalog metadata,
tag and asset naming are described in [Catalog & updates](/distribution/).
