/** * 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 [--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 [--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();