Files
WT-Offline/scripts/generate-patch-signing-key.ts
T
N0VAN0M4LY f5c5377d44 feat(patch-pack): publish signed adaptation packs and install them locally
An adaptation pack is the patch set (exe, GUI, native definitions and
version records) released on its own, so a new game build can be adapted
without a new offline version. It is a zip with a manifest, signed with
Ed25519, published as a GitHub release tagged patches-<YYYY.MM.DD[.N]>.

- packages/patcher/src/patch-pack: manifest and version numbers, zip
  writing and reading with path and size checks, signatures, a check that
  a patch set reads as a whole; tuning/patch-pack.jsonc holds the
  repository and the limits.
- scripts/generate-patch-signing-key.ts writes the public key for the
  repository and the private key outside it; scripts/build-patch-pack.ts
  packs only committed files and signs with PATCH_SIGNING_KEY.
- .github/workflows/patch-pack.yml packs, signs and publishes on a
  patches-* tag or a manual run.
- Launcher main process: find the newest pack release, download, verify,
  unpack into user data and make it active; resource paths use the
  active pack when it is usable (a repository checkout only with
  WTO_USE_PATCH_PACK=1). The pages that show and start this come later.
2026-11-29 16:08:37 +08:00

64 lines
3.0 KiB
TypeScript

/**
* One-time developer script: create the Ed25519 key pair that signs adaptation packs
* (packages/patcher/src/patch-pack/signature.ts).
*
* node scripts/generate-patch-signing-key.ts --private-out <file outside the repository> [--replace]
*
* The public key is written to packages/patcher/keys/patch-pack-signing.pub.pem, to be committed;
* launchers built from then on accept only packs signed by the matching private key. The private
* key is written to --private-out, which must lie outside the repository. Put its contents into
* the GitHub secret PATCH_SIGNING_KEY, keep a backup (for example in a password manager), then
* delete the file. Replacing an existing key (--replace) makes every launcher released before
* the change refuse packs signed with the new key.
*/
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
import { dirname, isAbsolute, relative, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';
import { generateSigningKeys, SIGNING_PUBLIC_KEY_FILE } from '@wto/patcher';
const REPOSITORY = fileURLToPath(new URL('..', import.meta.url));
const USAGE = 'usage: generate-patch-signing-key.ts --private-out <file outside the repository> [--replace]';
function main(): number {
const { values } = parseArgs({ options: { 'private-out': { type: 'string' }, replace: { type: 'boolean', default: false } } });
const privateOut = values['private-out'];
if (!privateOut) {
console.error(USAGE);
return 2;
}
const privatePath = resolve(privateOut);
// On another drive `relative` gives an absolute path.
const fromRepository = relative(REPOSITORY, privatePath);
if (!fromRepository.startsWith('..') && !isAbsolute(fromRepository)) {
console.error(`refusing to write the private key inside the repository: ${privatePath}`);
return 2;
}
if (existsSync(privatePath)) {
console.error(`refusing to overwrite ${privatePath}`);
return 2;
}
if (existsSync(SIGNING_PUBLIC_KEY_FILE) && !values.replace) {
console.error(`a public key already exists at ${SIGNING_PUBLIC_KEY_FILE}; pass --replace to make a new pair`);
return 2;
}
const keys = generateSigningKeys();
mkdirSync(dirname(privatePath), { recursive: true });
writeFileSync(privatePath, keys.privateKeyPem, { mode: 0o600, flag: 'wx' });
mkdirSync(dirname(SIGNING_PUBLIC_KEY_FILE), { recursive: true });
writeFileSync(SIGNING_PUBLIC_KEY_FILE, keys.publicKeyPem);
console.log(`public key: ${SIGNING_PUBLIC_KEY_FILE} (commit this file)`);
console.log(`private key: ${privatePath}`);
console.log('');
console.log('Next:');
console.log(' 1. GitHub repository > Settings > Secrets and variables > Actions > New repository secret,');
console.log(' name PATCH_SIGNING_KEY, value: the whole contents of the private key file.');
console.log(' 2. Keep a backup of the private key outside this computer (for example a password manager).');
console.log(' 3. Delete the private key file.');
return 0;
}
process.exitCode = main();