Files
WT-Offline/scripts/sign-launcher-release.ts
T

73 lines
3.2 KiB
TypeScript

import { access, readFile, writeFile } from 'node:fs/promises';
import { dirname, join, resolve } from 'node:path';
import { packSigningPublicKey, signEd25519, verifyEd25519 } from '@wto/patcher';
import {
installerDigest, launcherSignaturePayload, portableSignaturePayload,
} from '../apps/launcher/src/main/services/app-update/installer-integrity.ts';
import {
launcherReleaseFiles, LauncherReleaseManifest,
portableLauncherReleaseFiles, PortableReleaseManifest,
} from '../apps/launcher/src/shared/schemas/app-update.ts';
const repository = resolve(import.meta.dirname, '..');
async function main(): Promise<void> {
const { version } = JSON.parse(await readFile(join(repository, 'apps', 'launcher', 'package.json'), 'utf8')) as { version: string };
const files = launcherReleaseFiles(version);
const outputFolder = join(repository, 'build', 'windows-installer');
const installer = join(outputFolder, files.installer);
const privateKey = process.env['PATCH_SIGNING_KEY'];
const publicKey = packSigningPublicKey();
if (!privateKey || !publicKey) throw new Error('The project signing key pair is required.');
const manifest = LauncherReleaseManifest.parse({
kind: 'wto-launcher-release',
format: 1,
platform: 'win32-x64',
version,
...await installerDigest(installer),
});
const bytes = Buffer.from(JSON.stringify(manifest, null, 2) + '\n', 'utf8');
const payload = launcherSignaturePayload(bytes);
const signature = signEd25519(payload, privateKey);
if (!verifyEd25519(payload, signature, publicKey)) throw new Error('The release key does not match the bundled public key.');
const folder = dirname(installer);
await writeFile(join(folder, files.manifest), bytes);
await writeFile(join(folder, files.signature), signature, 'utf8');
for (const file of Object.values(files)) console.info(join(folder, file));
// Sign the portable archive with a separate manifest so older launchers whose installer manifest
// schema is strict keep validating releases without error.
const portableFiles = portableLauncherReleaseFiles(version);
const portableArchive = join(folder, portableFiles.archive);
if (await fileExists(portableArchive)) {
const portableManifest = PortableReleaseManifest.parse({
kind: 'wto-launcher-portable-release',
format: 1,
platform: 'win32-x64',
version,
...await installerDigest(portableArchive),
});
const portableBytes = Buffer.from(JSON.stringify(portableManifest, null, 2) + '\n', 'utf8');
const portablePayload = portableSignaturePayload(portableBytes);
const portableSig = signEd25519(portablePayload, privateKey);
if (!verifyEd25519(portablePayload, portableSig, publicKey)) {
throw new Error('The portable release key does not match the bundled public key.');
}
await writeFile(join(folder, portableFiles.manifest), portableBytes);
await writeFile(join(folder, portableFiles.signature), portableSig, 'utf8');
for (const file of Object.values(portableFiles)) console.info(join(folder, file));
}
}
async function fileExists(path: string): Promise<boolean> {
try {
await access(path);
return true;
} catch {
return false;
}
}
await main();