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.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, '../data/rsa-keys.txt') const OUT_JS_FILE = join(__dirname, '../binary/rsa-keys.js') interface InputKey { kind: 'old' | 'new' pem: string } async function* parseInputFile(): AsyncIterableIterator { 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 = {} for await (const key of parseInputFile()) { const parsed = 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)