Skip to content

Fix TypeScript/ESLint errors, add unit tests, and fix failing SRP tests#619

Open
Moustafa-Elgammal wants to merge 5 commits intomorethanwords:masterfrom
Moustafa-Elgammal:feature/solve-error-deprications
Open

Fix TypeScript/ESLint errors, add unit tests, and fix failing SRP tests#619
Moustafa-Elgammal wants to merge 5 commits intomorethanwords:masterfrom
Moustafa-Elgammal:feature/solve-error-deprications

Conversation

@Moustafa-Elgammal
Copy link
Copy Markdown
Contributor

Summary

  • Resolve all TypeScript strict-mode and ESLint errors introduced by the TypeScript 5.7 upgrade (stricter Uint8Array<ArrayBufferLike> generics and await-thenable lint rule)
  • Add a Docker-based test runner and structured unit tests covering each fix category
  • Fix two long-failing SRP (2FA) tests by correcting stale mock test vectors and making the crypto subtle object robust against test-environment timing

TypeScript & ESLint fixes (47 files)

Uint8Array<ArrayBufferLike> → explicit generic casts

TypeScript 5.7 narrows Uint8Array to carry its buffer type as a generic parameter. Many call sites passed Uint8Array<ArrayBufferLike> where Uint8Array<ArrayBuffer> (or BlobPart[], BufferSource, etc.) was expected. Fixed with targeted as casts at each boundary:

  • blobConstruct.ts — cast blobParts to BlobPart[] for the Blob constructor
  • tl_utils.ts — cast buffer.buffer to ArrayBuffer in TLDeserialization, fix fetchRawBytes overload signatures, and cast gzipUncompress result to Uint8Array<ArrayBuffer>
  • srp.ts — cast buffer arguments to Uint8Array<ArrayBuffer> for invokeCrypto('pbkdf2', ...)
  • passcode/utils.ts — cast salt to Uint8Array<ArrayBuffer> for WebCrypto PBKDF2 params
  • serviceWorker/stream.ts, rtmp.ts — cast body bytes to Uint8Array<ArrayBuffer> for Response constructor
  • sha1.ts, sha256.ts, aesCtrUtils.ts, aesCTR.ts, aesLocal.ts — cast digest/encrypt inputs
  • p2PEncryptor.ts — cast encrypted buffer slices
  • 30+ component files — cast Promise.all([...MaybePromise]) arrays with as Promise<T>[] to satisfy the await-thenable ESLint rule

Array.prototype.at(-1) → index replacement

starGiftInfo.tsx used .at(-1) which is not available in the es2015 compilation target. Replaced with arr[arr.length - 1].


subtle.ts — lazy crypto proxy

The original module captured window.crypto.subtle at import time:

// before
const subtle = typeof(window) !== 'undefined' && 'crypto' in window
  ? window.crypto.subtle : self.crypto.subtle;

In the vitest/jsdom environment (with isolate: false), modules are evaluated during test collection — before setupFiles installs self.crypto. This could cause crypto operations to bind to an uninitialised or wrong subtle object.

Replaced with a Proxy that resolves getSubtle() on every call:

// after
const getSubtle = () => window.crypto?.subtle ?? self.crypto.subtle;
const subtle = new Proxy({} as SubtleCrypto, {
  get(_t, prop) { return (...args) => getSubtle()[prop](...args); }
});

SRP mock test vectors corrected

src/tests/srp.test.ts had two failing tests since the passwordHashed and M1 values in src/mock/srp.ts were stale. A standalone Node.js webcrypto script independently computed the correct values by following the Telegram SRP spec step-by-step (SHA-256 → SHA-256 → PBKDF2-SHA512 × 100 000 → SHA-256). The code was verified correct; only the mock was wrong.

Updated:

  • passwordHashed — correct 32-byte SHA-256 output of the full password-hashing chain
  • M1 — correct 32-byte proof-of-password value derived from the updated hash

A matched the existing mock exactly, confirming BigInteger modular exponentiation is correct.


Test infrastructure

Docker test runner (docker-compose.yaml) — added tweb.test service that runs pnpm test inside the container, enabling reproducible CI-style test execution.

Unit tests — replaced the monolithic fixes.test.ts with four focused files:

File Coverage
blobConstruct.test.ts blobConstruct with various BlobPart inputs
bufferConcats.test.ts bufferConcats + Uint8Array.prototype.concat polyfill
promiseAll.test.ts Promise.all with mixed MaybePromise arrays
typeCasting.test.ts Array last-element indexing, ArrayBuffer casts, Response body casts

crypto_methods.test.ts — the existing pbkdf2 test had an empty body (all assertions were commented out). Replaced with assertions that verify output length (64 bytes / SHA-512) and determinism.


Test results

Test Files  11 passed (11)
     Tests  70 passed (70)
@morethanwords
Copy link
Copy Markdown
Owner

Thank you for the PR! I'm actually using typescript@5.8.2 (as in pnpm-lock.yaml), so the issues that you fixed here is not actual, both Promise and Uint8Array. However, I could merge the tests part of the PR, if you create it again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants