mtcute/packages/tl/scripts/gen-rsa-keys.ts
teidesu ec736f8590 some changes
i've been wanting to name a commit like this for my entire life, lol. seriously though, a lot has changed:
 - extracted TL-related stuff to `@mtcute/tl-utils` and `@mtcute/tl-runtime`, rewrote codegen in TS
 - updated to layer 134, moved to int64 identifiers
 - rewritten networking (mtproto), rewritten updates handling
 - *lots* of refactoring

 still a very early version though, there are a lot of improvements to be made, but at least it runs, lol

 also tl-reference will not be updated anytime soon because i want to rewrite it
2021-11-23 00:03:59 +03:00

64 lines
1.7 KiB
TypeScript

import { TlPublicKey } from '../binary/rsa-keys'
import { join } from 'path'
import readline from 'readline'
import { createReadStream } from 'fs'
import { NodeCryptoProvider } from '@mtcute/core/src/utils/crypto'
import { parsePublicKey } from '@mtcute/core/src/utils/crypto/keys'
import { writeFile } from 'fs/promises'
import { ESM_PRELUDE } from './constants'
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)