|
| 1 | +/** |
| 2 | + * Выпуск supporter-лицензии, подписанной приватным ключом Ed25519. |
| 3 | + * |
| 4 | + * Запуск: pnpm license:issue --email john@example.com [--name "John Doe"] |
| 5 | + */ |
| 6 | +const { Buffer } = require('node:buffer') |
| 7 | +const { createPrivateKey, sign } = require('node:crypto') |
| 8 | +const fs = require('node:fs') |
| 9 | +const os = require('node:os') |
| 10 | +const path = require('node:path') |
| 11 | +const process = require('node:process') |
| 12 | + |
| 13 | +const privateKeyPath |
| 14 | + = process.env.MASSCODE_LICENSE_PRIVATE_KEY |
| 15 | + || path.join(os.homedir(), '.masscode', 'license-private.pem') |
| 16 | + |
| 17 | +function getArg(name) { |
| 18 | + const index = process.argv.indexOf(`--${name}`) |
| 19 | + if (index === -1 || index === process.argv.length - 1) { |
| 20 | + return undefined |
| 21 | + } |
| 22 | + return process.argv[index + 1] |
| 23 | +} |
| 24 | + |
| 25 | +const name = getArg('name') |
| 26 | +const email = getArg('email') |
| 27 | + |
| 28 | +if (!email) { |
| 29 | + console.error( |
| 30 | + 'Usage: pnpm license:issue --email john@example.com [--name "John Doe"]', |
| 31 | + ) |
| 32 | + process.exit(1) |
| 33 | +} |
| 34 | + |
| 35 | +if (!fs.existsSync(privateKeyPath)) { |
| 36 | + console.error(`Private key not found: ${privateKeyPath}`) |
| 37 | + console.error('Run "node scripts/license/keygen.js" first.') |
| 38 | + process.exit(1) |
| 39 | +} |
| 40 | + |
| 41 | +const privateKey = createPrivateKey(fs.readFileSync(privateKeyPath)) |
| 42 | + |
| 43 | +const payload = { |
| 44 | + email, |
| 45 | + ...(name ? { name } : {}), |
| 46 | + issuedAt: new Date().toISOString().slice(0, 10), |
| 47 | +} |
| 48 | + |
| 49 | +const payloadBase64 = Buffer.from(JSON.stringify(payload)).toString( |
| 50 | + 'base64url', |
| 51 | +) |
| 52 | +const signature = sign(null, Buffer.from(payloadBase64), privateKey).toString( |
| 53 | + 'base64url', |
| 54 | +) |
| 55 | + |
| 56 | +console.log(`${payloadBase64}.${signature}`) |
0 commit comments