import { readFile, writeFile } from 'node:fs/promises'; import { dirname, join, resolve } from 'node:path'; import { packSigningPublicKey, signEd25519, verifyEd25519 } from '@wto/patcher'; import { installerDigest, launcherSignaturePayload } from '../apps/launcher/src/main/services/app-update/installer-integrity.ts'; import { launcherReleaseFiles, LauncherReleaseManifest } from '../apps/launcher/src/shared/schemas/app-update.ts'; const repository = resolve(import.meta.dirname, '..'); async function main(): Promise { const { version } = JSON.parse(await readFile(join(repository, 'apps', 'launcher', 'package.json'), 'utf8')) as { version: string }; const files = launcherReleaseFiles(version); const installer = join(repository, 'build', 'windows-installer', 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)); } await main();