mtcute/packages/tl/scripts/gen-rsa-keys.ts
Alina Tumanova f5976a2d74
ESM + end-to-end tests (#11)
* feat: moved tl-runtime to esm and native ArrayBuffers

* feat: migration to esm

* fix(core): web-related fixes

* test: finally, some good fucking e2e

* chore: fixed linters etc

* ci: added e2e to ci

* build(tl): fixed gen-code on node 20

* fix: codegen Uint8Array, not Buffer

never `git reset --hard` kids

* build: only do type-aware linting for `packages/*`

* build: ignore no-unresolved in ci for e2e

* fix: node 16 doesn't have subtle crypto apparently?

* fix(tests): use Uint8Array

for gods sake please can i just merge this already

* ci: don't parallel tasks in ci

because machines are utter garbage and it may just randomly break

* ci: pass secrets to e2e tests

* ci: separate cli command for ci

apparently im retarded

* fix: run codegen in e2e

im actually retarded

* ci: more fixes for e2e

* ci: debugging stuff

* ci: still debugging

* ci: hopefully fix ci???
2023-10-16 19:23:53 +03:00

60 lines
1.6 KiB
TypeScript

import { createReadStream } from 'fs'
import { writeFile } from 'fs/promises'
import { join } from 'path'
import readline from 'readline'
import { NodeCryptoProvider } from '@mtcute/core/src/utils/crypto/node-crypto.js'
import { parsePublicKey } from '@mtcute/core/utils.js'
import { TlPublicKey } from '../binary/rsa-keys.js'
import { __dirname, ESM_PRELUDE } from './constants.js'
const IN_TXT_FILE = join(__dirname, '.rsa-keys.txt')
const OUT_JS_FILE = join(__dirname, '../binary/rsa-keys.js')
interface InputKey {
kind: 'old' | 'new'
pem: string
}
async function* parseInputFile(): AsyncIterableIterator<InputKey> {
const rl = readline.createInterface({
input: createReadStream(IN_TXT_FILE),
crlfDelay: Infinity,
})
let currentKind: InputKey['kind'] = 'old'
let current = ''
for await (const line of rl) {
if (line === '### OLD ###') currentKind = 'old'
if (line === '### NEW ###') currentKind = 'new'
if (line[0] === '#') continue
current += line + '\n'
if (line === '-----END RSA PUBLIC KEY-----') {
yield {
kind: currentKind,
pem: current.trim(),
}
current = ''
}
}
}
async function main() {
const crypto = new NodeCryptoProvider()
const obj: Record<string, TlPublicKey> = {}
for await (const key of parseInputFile()) {
const parsed = await parsePublicKey(crypto, key.pem, key.kind === 'old')
obj[parsed.fingerprint] = parsed
}
await writeFile(OUT_JS_FILE, ESM_PRELUDE + "exports.default=JSON.parse('" + JSON.stringify(obj) + "');")
}
main().catch(console.error)