const { spawnSync } = require('child_process'); const path = require('path'); const fs = require('fs'); const keytool = 'C:\\\\Program Files\\\\Eclipse Adoptium\\\\jdk-17.0.14.7-hotspot\\\\bin\\\\keytool.exe'; const allowedDirectory = fs.realpathSync(path.resolve('apps/mobile-rn/android/app')); const required = [ 'D3RO_RELEASE_STORE_FILE', 'D3RO_RELEASE_STORE_PASSWORD', 'D3RO_RELEASE_KEY_ALIAS', 'D3RO_RELEASE_KEY_PASSWORD', ]; const missing = required.filter((name) => !process.env[name]); if (missing.length > 0) { throw new Error(`Missing required release signing settings: ${missing.join(', ')}`); } const keystorePath = path.resolve(process.env.D3RO_RELEASE_STORE_FILE); if (path.dirname(keystorePath) !== allowedDirectory || !/\.(?:keystore|p12)$/i.test(keystorePath)) { throw new Error('D3RO_RELEASE_STORE_FILE must be a keystore inside android/app'); } if (fs.existsSync(keystorePath)) { throw new Error('Refusing to overwrite an existing release keystore'); } if (process.env.D3RO_RELEASE_STORE_PASSWORD.length < 20) { throw new Error('D3RO_RELEASE_STORE_PASSWORD must contain at least 20 characters'); } if (process.env.D3RO_RELEASE_STORE_PASSWORD !== process.env.D3RO_RELEASE_KEY_PASSWORD) { throw new Error('PKCS12 store and key passwords must match'); } if (!/^[A-Za-z0-9._-]{3,64}$/.test(process.env.D3RO_RELEASE_KEY_ALIAS)) { throw new Error('D3RO_RELEASE_KEY_ALIAS has an invalid format'); } const args = [ '-genkeypair', '-v', '-keystore', keystorePath, '-storetype', 'PKCS12', '-alias', process.env.D3RO_RELEASE_KEY_ALIAS, '-keyalg', 'RSA', '-keysize', '3072', '-validity', '10000', '-storepass:env', 'D3RO_RELEASE_STORE_PASSWORD', '-keypass:env', 'D3RO_RELEASE_KEY_PASSWORD', '-dname', process.env.D3RO_RELEASE_DNAME || 'CN=D3RO Voice AI, OU=Technology, O=Chanpaca Inc, L=Seoul, ST=Seoul, C=KR', ]; const result = spawnSync(keytool, args, { stdio: 'inherit', env: process.env }); if (result.error) throw result.error; if (result.status !== 0) throw new Error(`Keytool exited with status ${result.status}`); fs.chmodSync(keystorePath, 0o600); console.log(`Created release keystore at ${keystorePath}`);