mtcute/packages/tl/scripts/gen-rsa-keys.ts
alina sireneva 365369be95
fix(node)!: recursive crypto-node import
breaking: moved `NodeCryptoProvider` to @mtcute/node/utils.js
2024-04-27 12:19:08 +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 { parsePublicKey } from '@mtcute/core/utils.js'
import { NodeCryptoProvider } from '@mtcute/node/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<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 = 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)