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:
Claude
2026-02-08 20:49:18 +00:00
parent 395f3aace5
commit 2c18b734ee
13 changed files with 76 additions and 169 deletions
+15 -15
View File
@@ -24,8 +24,8 @@ class TransportState {
private readonly iv = new Uint8Array(IV_LENGTH)
constructor(
private readonly encKey: Buffer,
private readonly decKey: Buffer
private readonly encKey: Uint8Array,
private readonly decKey: Uint8Array
) {}
encrypt(plaintext: Uint8Array): Uint8Array {
@@ -116,22 +116,22 @@ export const makeNoiseHandler = ({
return result
}
const localHKDF = async (data: Uint8Array): Promise<[Buffer, Buffer]> => {
const key = await hkdf(Buffer.from(data), 64, { salt, info: '' })
return [key.subarray(0, 32) as Buffer, key.subarray(32) as Buffer]
const localHKDF = (data: Uint8Array): [Uint8Array, Uint8Array] => {
const key = hkdf(Buffer.from(data), 64, { salt, info: '' })
return [key.subarray(0, 32), key.subarray(32)]
}
const mixIntoKey = async (data: Uint8Array) => {
const [write, read] = await localHKDF(data)
salt = write
encKey = read
decKey = read
const mixIntoKey = (data: Uint8Array) => {
const [write, read] = localHKDF(data)
salt = write as Buffer
encKey = read as Buffer
decKey = read as Buffer
counter = 0
}
const finishInit = async () => {
isWaitingForTransport = true
const [write, read] = await localHKDF(new Uint8Array(0))
const [write, read] = localHKDF(new Uint8Array(0))
transport = new TransportState(write, read)
isWaitingForTransport = false
@@ -179,12 +179,12 @@ export const makeNoiseHandler = ({
authenticate,
mixIntoKey,
finishInit,
processHandshake: async ({ serverHello }: proto.HandshakeMessage, noiseKey: KeyPair) => {
processHandshake: ({ serverHello }: proto.HandshakeMessage, noiseKey: KeyPair) => {
authenticate(serverHello!.ephemeral!)
await mixIntoKey(Curve.sharedKey(privateKey, serverHello!.ephemeral!))
mixIntoKey(Curve.sharedKey(privateKey, serverHello!.ephemeral!))
const decStaticContent = decrypt(serverHello!.static!)
await mixIntoKey(Curve.sharedKey(privateKey, decStaticContent))
mixIntoKey(Curve.sharedKey(privateKey, decStaticContent))
const certDecoded = decrypt(serverHello!.payload!)
@@ -223,7 +223,7 @@ export const makeNoiseHandler = ({
}
const keyEnc = encrypt(noiseKey.public)
await mixIntoKey(Curve.sharedKey(noiseKey.private, serverHello!.ephemeral!))
mixIntoKey(Curve.sharedKey(noiseKey.private, serverHello!.ephemeral!))
return keyEnc
},