feat: replace async crypto with sync Rust WASM (port of Baileys b5c1741)
Surgically applies the changes from WhiskeySockets/Baileys commit b5c1741
("feat: replace async crypto with sync Rust WASM" by jlucaso1) while
preserving all custom modifications (interactive messages, carousels,
albums, sticker packs, native flow buttons, Prometheus metrics, etc).
Changes:
- Replace async hkdf/md5 (Web Crypto API) with sync re-exports from whatsapp-rust-bridge@0.5.2
- Replace LTHash class with LTHashAntiTampering from WASM
- Replace mutationKeys() with expandAppStateKeys() from WASM
- Remove ~25 unnecessary await keywords across crypto call chain
- Update Buffer→Uint8Array types for MediaDecryptionKeyInfo and internal crypto functions
- Make noise handshake, media retry encrypt/decrypt, and reporting token generation synchronous
Performance impact:
- Eliminates Promise overhead on every HKDF/LTHash operation
- Significant improvement during app state sync (hundreds of mutations per reconnection)
- Sync crypto reduces event loop pressure under high session load
Custom code preserved (zero conflicts):
- messages-send.ts: All interactive message, carousel, album, sticker pack logic intact
- Types/Message.ts: All custom types (NativeFlowButton, Carousel, Album, etc.) intact
- All Prometheus metrics, circuit breakers, session TTL logic intact
https://claude.ai/code/session_01Ffc5YrPuqv8N9SwEuSM8mr
This commit is contained in:
+2
-64
@@ -1,65 +1,3 @@
|
||||
import { hkdf } from './crypto'
|
||||
import { LTHashAntiTampering } from 'whatsapp-rust-bridge'
|
||||
|
||||
/**
|
||||
* LT Hash is a summation based hash algorithm that maintains the integrity of a piece of data
|
||||
* over a series of mutations. You can add/remove mutations and it'll return a hash equal to
|
||||
* if the same series of mutations was made sequentially.
|
||||
*/
|
||||
const o = 128
|
||||
|
||||
class LTHash {
|
||||
salt: string
|
||||
|
||||
constructor(e: string) {
|
||||
this.salt = e
|
||||
}
|
||||
|
||||
async add(e: ArrayBuffer, t: ArrayBuffer[]): Promise<ArrayBuffer> {
|
||||
for (const item of t) {
|
||||
e = await this._addSingle(e, item)
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
async subtract(e: ArrayBuffer, t: ArrayBuffer[]): Promise<ArrayBuffer> {
|
||||
for (const item of t) {
|
||||
e = await this._subtractSingle(e, item)
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
async subtractThenAdd(e: ArrayBuffer, addList: ArrayBuffer[], subtractList: ArrayBuffer[]): Promise<ArrayBuffer> {
|
||||
const subtracted = await this.subtract(e, subtractList)
|
||||
return this.add(subtracted, addList)
|
||||
}
|
||||
|
||||
private async _addSingle(e: ArrayBuffer, t: ArrayBuffer): Promise<ArrayBuffer> {
|
||||
const derived = new Uint8Array(await hkdf(Buffer.from(t), o, { info: this.salt })).buffer
|
||||
return this.performPointwiseWithOverflow(e, derived, (a, b) => a + b)
|
||||
}
|
||||
|
||||
private async _subtractSingle(e: ArrayBuffer, t: ArrayBuffer): Promise<ArrayBuffer> {
|
||||
const derived = new Uint8Array(await hkdf(Buffer.from(t), o, { info: this.salt })).buffer
|
||||
return this.performPointwiseWithOverflow(e, derived, (a, b) => a - b)
|
||||
}
|
||||
|
||||
private performPointwiseWithOverflow(
|
||||
e: ArrayBuffer,
|
||||
t: ArrayBuffer,
|
||||
op: (a: number, b: number) => number
|
||||
): ArrayBuffer {
|
||||
const n = new DataView(e)
|
||||
const i = new DataView(t)
|
||||
const out = new ArrayBuffer(n.byteLength)
|
||||
const s = new DataView(out)
|
||||
|
||||
for (let offset = 0; offset < n.byteLength; offset += 2) {
|
||||
s.setUint16(offset, op(n.getUint16(offset, true), i.getUint16(offset, true)), true)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
}
|
||||
export const LT_HASH_ANTI_TAMPERING = new LTHash('WhatsApp Patch Integrity')
|
||||
export const LT_HASH_ANTI_TAMPERING = new LTHashAntiTampering()
|
||||
|
||||
Reference in New Issue
Block a user