Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| acfaaff68b | |||
| 4fa7d4ee11 |
@@ -1,7 +1,7 @@
|
|||||||
syntax = "proto3";
|
syntax = "proto3";
|
||||||
package proto;
|
package proto;
|
||||||
|
|
||||||
/// WhatsApp Version: 2.3000.1034427372
|
/// WhatsApp Version: 2.3000.1034302344
|
||||||
|
|
||||||
message ADVDeviceIdentity {
|
message ADVDeviceIdentity {
|
||||||
optional uint32 rawId = 1;
|
optional uint32 rawId = 1;
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
{"version":[2,3000,1038487394]}
|
{"version":[2,3000,1034382497]}
|
||||||
|
|||||||
+13
-79
@@ -302,25 +302,6 @@ export function makeLibSignalRepository(
|
|||||||
// Promise instead of each spawning their own DB transactions.
|
// Promise instead of each spawning their own DB transactions.
|
||||||
const migrationInFlight = new Map<string, Promise<{ migrated: number; skipped: number; total: number }>>()
|
const migrationInFlight = new Map<string, Promise<{ migrated: number; skipped: number; total: number }>>()
|
||||||
|
|
||||||
// Resolve PN JID to its canonical LID JID for transaction locking.
|
|
||||||
// This prevents PN/LID race conditions where concurrent operations for the
|
|
||||||
// same logical contact acquire different mutex locks because one uses PN
|
|
||||||
// and the other uses LID. (Aligned with WABA behavior — all operations use LID internally.)
|
|
||||||
const resolveCanonicalJid = async(jid: string): Promise<string> => {
|
|
||||||
if (isAnyLidUser(jid)) {
|
|
||||||
return jid
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isAnyPnUser(jid)) {
|
|
||||||
const lid = await lidMapping.getLIDForPN(jid)
|
|
||||||
if (lid) {
|
|
||||||
return lid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return jid
|
|
||||||
}
|
|
||||||
|
|
||||||
const repository: SignalRepositoryWithLIDStore = {
|
const repository: SignalRepositoryWithLIDStore = {
|
||||||
decryptGroupMessage({ group, authorJid, msg }) {
|
decryptGroupMessage({ group, authorJid, msg }) {
|
||||||
const senderName = jidToSignalSenderKeyName(group, authorJid)
|
const senderName = jidToSignalSenderKeyName(group, authorJid)
|
||||||
@@ -415,24 +396,23 @@ export function makeLibSignalRepository(
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use canonical JID (PN→LID resolved) as transaction key to prevent
|
// If it's not a sync message, we need to ensure atomicity
|
||||||
// PN/LID race conditions on the same logical session.
|
// For regular messages, we use a transaction to ensure atomicity
|
||||||
const canonicalJid = await resolveCanonicalJid(jid)
|
|
||||||
return parsedKeys.transaction(async () => {
|
return parsedKeys.transaction(async () => {
|
||||||
return await doDecrypt()
|
return await doDecrypt()
|
||||||
}, canonicalJid)
|
}, jid)
|
||||||
},
|
},
|
||||||
|
|
||||||
async encryptMessage({ jid, data }) {
|
async encryptMessage({ jid, data }) {
|
||||||
const addr = jidToSignalProtocolAddress(jid)
|
const addr = jidToSignalProtocolAddress(jid)
|
||||||
const cipher = new libsignal.SessionCipher(storage, addr)
|
const cipher = new libsignal.SessionCipher(storage, addr)
|
||||||
|
|
||||||
const canonicalJid = await resolveCanonicalJid(jid)
|
// Use transaction to ensure atomicity
|
||||||
return parsedKeys.transaction(async () => {
|
return parsedKeys.transaction(async () => {
|
||||||
const { type: sigType, body } = await cipher.encrypt(data)
|
const { type: sigType, body } = await cipher.encrypt(data)
|
||||||
const type = sigType === 3 ? 'pkmsg' : 'msg'
|
const type = sigType === 3 ? 'pkmsg' : 'msg'
|
||||||
return { type, ciphertext: Buffer.from(body, 'binary') }
|
return { type, ciphertext: Buffer.from(body, 'binary') }
|
||||||
}, canonicalJid)
|
}, jid)
|
||||||
},
|
},
|
||||||
|
|
||||||
async encryptGroupMessage({ group, meId, data }) {
|
async encryptGroupMessage({ group, meId, data }) {
|
||||||
@@ -674,10 +654,9 @@ export function makeLibSignalRepository(
|
|||||||
// Session exists (guaranteed from device discovery)
|
// Session exists (guaranteed from device discovery)
|
||||||
const fromSession = libsignal.SessionRecord.deserialize(pnSession)
|
const fromSession = libsignal.SessionRecord.deserialize(pnSession)
|
||||||
if (fromSession.haveOpenSession()) {
|
if (fromSession.haveOpenSession()) {
|
||||||
// Queue for bulk update: copy to LID, retain PN session.
|
// Queue for bulk update: copy to LID, delete from PN
|
||||||
// WABA retains both PN and LID sessions during migration to avoid
|
|
||||||
// No Session errors if messages arrive via PN before migration completes.
|
|
||||||
sessionUpdates[lidAddrStr] = fromSession.serialize()
|
sessionUpdates[lidAddrStr] = fromSession.serialize()
|
||||||
|
sessionUpdates[pnAddrStr] = null
|
||||||
|
|
||||||
migratedCount++
|
migratedCount++
|
||||||
}
|
}
|
||||||
@@ -797,13 +776,6 @@ function signalStorage(
|
|||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delayed PreKey deletion: grace period to handle race conditions
|
|
||||||
// where two pkmsg with the same preKeyId arrive nearly simultaneously.
|
|
||||||
// WABA deletes immediately (33ms), but we add a 5-min grace period
|
|
||||||
// because we can't handle "Invalid PreKey ID" errors at the native level.
|
|
||||||
const PREKEY_GRACE_PERIOD_MS = 5 * 60 * 1000 // 5 minutes
|
|
||||||
const pendingPreKeyDeletions = new Map<string, ReturnType<typeof setTimeout>>()
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
loadSession: async (id: string) => {
|
loadSession: async (id: string) => {
|
||||||
try {
|
try {
|
||||||
@@ -836,26 +808,7 @@ function signalStorage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
removePreKey: (id: number) => {
|
removePreKey: (id: number) => keys.set({ 'pre-key': { [id]: null } }),
|
||||||
const keyId = id.toString()
|
|
||||||
// Clear any existing timer for this key
|
|
||||||
const existing = pendingPreKeyDeletions.get(keyId)
|
|
||||||
if (existing) {
|
|
||||||
clearTimeout(existing)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schedule deletion after grace period
|
|
||||||
const timer = setTimeout(async () => {
|
|
||||||
pendingPreKeyDeletions.delete(keyId)
|
|
||||||
try {
|
|
||||||
await keys.set({ 'pre-key': { [id]: null } })
|
|
||||||
} catch {
|
|
||||||
// Keystore may be destroyed if connection closed — safe to ignore
|
|
||||||
}
|
|
||||||
}, PREKEY_GRACE_PERIOD_MS)
|
|
||||||
|
|
||||||
pendingPreKeyDeletions.set(keyId, timer)
|
|
||||||
},
|
|
||||||
loadSignedPreKey: () => {
|
loadSignedPreKey: () => {
|
||||||
const key = creds.signedPreKey
|
const key = creds.signedPreKey
|
||||||
return {
|
return {
|
||||||
@@ -945,24 +898,14 @@ function signalStorage(
|
|||||||
// IDENTITY KEY CHANGED - contact reinstalled WhatsApp or switched devices
|
// IDENTITY KEY CHANGED - contact reinstalled WhatsApp or switched devices
|
||||||
const previousFingerprint = generateKeyFingerprint(existingKey)
|
const previousFingerprint = generateKeyFingerprint(existingKey)
|
||||||
|
|
||||||
// Delete old session and save new identity key atomically.
|
// Delete old session and save new identity key atomically
|
||||||
// Store identity in BOTH LID and PN addresses (WABA stores in both
|
|
||||||
// recipient_account_type=0 and type=1 with CONFLICT_REPLACE).
|
|
||||||
const identityUpdates: Record<string, Uint8Array> = { [wireJid]: identityKey }
|
|
||||||
if (wireJid !== id) {
|
|
||||||
identityUpdates[id] = identityKey
|
|
||||||
}
|
|
||||||
|
|
||||||
await keys.set({
|
await keys.set({
|
||||||
session: { [wireJid]: null },
|
session: { [wireJid]: null },
|
||||||
'identity-key': identityUpdates
|
'identity-key': { [wireJid]: identityKey }
|
||||||
})
|
})
|
||||||
|
|
||||||
// Update cache for both addresses
|
// Update cache
|
||||||
identityKeyCache.set(wireJid, identityKey)
|
identityKeyCache.set(wireJid, identityKey)
|
||||||
if (wireJid !== id) {
|
|
||||||
identityKeyCache.set(id, identityKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Record metrics
|
// Record metrics
|
||||||
metrics.signalIdentityChanges?.inc({ type: 'changed' })
|
metrics.signalIdentityChanges?.inc({ type: 'changed' })
|
||||||
@@ -998,19 +941,10 @@ function signalStorage(
|
|||||||
|
|
||||||
if (!existingKey) {
|
if (!existingKey) {
|
||||||
// NEW CONTACT - Trust On First Use (TOFU)
|
// NEW CONTACT - Trust On First Use (TOFU)
|
||||||
// Store in both LID and PN addresses (aligned with WABA dual identity storage)
|
await keys.set({ 'identity-key': { [wireJid]: identityKey } })
|
||||||
const identityUpdates: Record<string, Uint8Array> = { [wireJid]: identityKey }
|
|
||||||
if (wireJid !== id) {
|
|
||||||
identityUpdates[id] = identityKey
|
|
||||||
}
|
|
||||||
|
|
||||||
await keys.set({ 'identity-key': identityUpdates })
|
// Update cache
|
||||||
|
|
||||||
// Update cache for both addresses
|
|
||||||
identityKeyCache.set(wireJid, identityKey)
|
identityKeyCache.set(wireJid, identityKey)
|
||||||
if (wireJid !== id) {
|
|
||||||
identityKeyCache.set(id, identityKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Record metrics
|
// Record metrics
|
||||||
metrics.signalIdentityChanges?.inc({ type: 'new' })
|
metrics.signalIdentityChanges?.inc({ type: 'new' })
|
||||||
|
|||||||
@@ -1314,7 +1314,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
let shouldRecreateSession = false
|
let shouldRecreateSession = false
|
||||||
let recreateReason = ''
|
let recreateReason = ''
|
||||||
|
|
||||||
if (enableAutoSessionRecreation && messageRetryManager && retryCount >= 1) {
|
if (enableAutoSessionRecreation && messageRetryManager && retryCount > 1) {
|
||||||
try {
|
try {
|
||||||
// Check if we have a session with this JID
|
// Check if we have a session with this JID
|
||||||
const sessionId = signalRepository.jidToSignalProtocolAddress(fromJid)
|
const sessionId = signalRepository.jidToSignalProtocolAddress(fromJid)
|
||||||
@@ -2033,7 +2033,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
let shouldRecreateSession = false
|
let shouldRecreateSession = false
|
||||||
let recreateReason = ''
|
let recreateReason = ''
|
||||||
|
|
||||||
if (enableAutoSessionRecreation && messageRetryManager && retryCount >= 1) {
|
if (enableAutoSessionRecreation && messageRetryManager && retryCount > 1) {
|
||||||
try {
|
try {
|
||||||
const sessionId = signalRepository.jidToSignalProtocolAddress(participant)
|
const sessionId = signalRepository.jidToSignalProtocolAddress(participant)
|
||||||
|
|
||||||
|
|||||||
@@ -54,12 +54,14 @@ export type WAMediaUpload = Buffer | WAMediaPayloadStream | WAMediaPayloadURL
|
|||||||
* Individual sticker in a sticker pack
|
* Individual sticker in a sticker pack
|
||||||
*/
|
*/
|
||||||
export type Sticker = {
|
export type Sticker = {
|
||||||
/** Buffer, Stream or URL of the sticker image (will be converted to WebP if needed) */
|
/** Buffer, Stream or URL of the sticker image (WebP or Lottie/WAS format) */
|
||||||
data: WAMediaUpload
|
data: WAMediaUpload
|
||||||
/** Array of emojis associated with this sticker (max 3 recommended per WhatsApp standards) */
|
/** Array of emojis associated with this sticker (max 3 recommended per WhatsApp standards) */
|
||||||
emojis?: string[]
|
emojis?: string[]
|
||||||
/** Accessibility label for screen readers (max 125 chars for static, 255 for animated) */
|
/** Accessibility label for screen readers (max 125 chars for static, 255 for animated) */
|
||||||
accessibilityLabel?: string
|
accessibilityLabel?: string
|
||||||
|
/** Force Lottie format detection (auto-detected if omitted) */
|
||||||
|
isLottie?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import {
|
|||||||
isJidStatusBroadcast,
|
isJidStatusBroadcast,
|
||||||
isLidUser,
|
isLidUser,
|
||||||
isPnUser,
|
isPnUser,
|
||||||
|
jidDecode
|
||||||
// transferDevice
|
// transferDevice
|
||||||
} from '../WABinary'
|
} from '../WABinary'
|
||||||
import { unpadRandomMax16 } from './generics'
|
import { unpadRandomMax16 } from './generics'
|
||||||
@@ -487,9 +488,8 @@ export function isCorruptedSessionError(error: any): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clean up corrupted session for a specific device JID.
|
* Clean up corrupted session by deleting all device sessions for a JID.
|
||||||
* WABA behavior: DELETE sessions WHERE recipient_id=? AND device_id=?
|
* Signal Protocol will automatically recreate the session on next message.
|
||||||
* Only deletes the exact device that was corrupted, not all devices.
|
|
||||||
*
|
*
|
||||||
* NOTE: This should NOT be called on every Bad MAC error (hot path).
|
* NOTE: This should NOT be called on every Bad MAC error (hot path).
|
||||||
* Instead, let the retry+pkmsg flow handle recovery naturally (like WhatsApp does).
|
* Instead, let the retry+pkmsg flow handle recovery naturally (like WhatsApp does).
|
||||||
@@ -500,7 +500,45 @@ export async function cleanupCorruptedSession(
|
|||||||
repository: SignalRepositoryWithLIDStore,
|
repository: SignalRepositoryWithLIDStore,
|
||||||
logger: ILogger
|
logger: ILogger
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
await repository.deleteSession([jid])
|
const { user, device } = jidDecode(jid) || {}
|
||||||
logger.info({ jid }, 'Cleaned up corrupted session for specific device')
|
if (!user) {
|
||||||
return 1
|
logger.warn({ jid }, 'Cannot cleanup session - invalid JID')
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build list of JIDs to delete (primary + secondary devices)
|
||||||
|
const jidsToDelete: string[] = []
|
||||||
|
|
||||||
|
// Determine domain type correctly (handle hosted JIDs)
|
||||||
|
// JID formats:
|
||||||
|
// - PN: user@s.whatsapp.net
|
||||||
|
// - LID: user@lid
|
||||||
|
// - Hosted PN: user@hosted
|
||||||
|
// - Hosted LID: user@hosted.lid
|
||||||
|
const isLID = jid.endsWith('@lid') || jid.endsWith('@hosted.lid')
|
||||||
|
const isHosted = jid.includes('@hosted')
|
||||||
|
|
||||||
|
let domain: string
|
||||||
|
if (isLID) {
|
||||||
|
domain = isHosted ? 'hosted.lid' : 'lid'
|
||||||
|
} else {
|
||||||
|
domain = isHosted ? 'hosted' : 's.whatsapp.net'
|
||||||
|
}
|
||||||
|
|
||||||
|
// Primary device (0)
|
||||||
|
jidsToDelete.push(`${user}@${domain}`)
|
||||||
|
|
||||||
|
// Secondary devices (1-5 common range for Web/Desktop/etc)
|
||||||
|
for (let i = 1; i <= 5; i++) {
|
||||||
|
jidsToDelete.push(`${user}:${i}@${domain}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If specific device was identified and > 5, ensure it's included
|
||||||
|
if (device !== undefined && device > 5) {
|
||||||
|
jidsToDelete.push(`${user}:${device}@${domain}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
await repository.deleteSession(jidsToDelete)
|
||||||
|
|
||||||
|
return jidsToDelete.length
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -218,7 +218,7 @@ export class MessageRetryManager {
|
|||||||
if (errorCode !== undefined && MAC_ERROR_CODES.has(errorCode)) {
|
if (errorCode !== undefined && MAC_ERROR_CODES.has(errorCode)) {
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
const prevTime = this.sessionRecreateHistory.get(jid)
|
const prevTime = this.sessionRecreateHistory.get(jid)
|
||||||
const MAC_ERROR_COOLDOWN_MS = 1_000 // 1 second — WABA recovers faster
|
const MAC_ERROR_COOLDOWN_MS = 10_000 // 10 seconds
|
||||||
|
|
||||||
if (prevTime && now - prevTime < MAC_ERROR_COOLDOWN_MS) {
|
if (prevTime && now - prevTime < MAC_ERROR_COOLDOWN_MS) {
|
||||||
const reasonName = RetryReason[errorCode] || `code_${errorCode}`
|
const reasonName = RetryReason[errorCode] || `code_${errorCode}`
|
||||||
|
|||||||
+120
-19
@@ -2,6 +2,7 @@ import { Boom } from '@hapi/boom'
|
|||||||
import { createHash } from 'crypto'
|
import { createHash } from 'crypto'
|
||||||
import { zipSync } from 'fflate'
|
import { zipSync } from 'fflate'
|
||||||
import { promises as fs } from 'fs'
|
import { promises as fs } from 'fs'
|
||||||
|
import { gzipSync, gunzipSync } from 'zlib'
|
||||||
import { proto } from '../../WAProto/index.js'
|
import { proto } from '../../WAProto/index.js'
|
||||||
import type { MediaType } from '../Defaults/index.js'
|
import type { MediaType } from '../Defaults/index.js'
|
||||||
import type { StickerPack, WAMediaUpload, WAMediaUploadFunction } from '../Types/Message.js'
|
import type { StickerPack, WAMediaUpload, WAMediaUploadFunction } from '../Types/Message.js'
|
||||||
@@ -101,6 +102,49 @@ export const isAnimatedWebP = (buffer: Buffer): boolean => {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detecta se um buffer é Lottie JSON (raw ou gzip-compressed/WAS)
|
||||||
|
*
|
||||||
|
* WABA usa mimetype `application/was` para stickers Lottie.
|
||||||
|
* WAS (WhatsApp Animated Sticker) = gzip-compressed Lottie JSON.
|
||||||
|
*
|
||||||
|
* Detecção:
|
||||||
|
* - Gzip (0x1f 0x8b): descomprime e verifica se é Lottie JSON
|
||||||
|
* - JSON bruto: verifica campos Lottie obrigatórios (v, ip, op, layers)
|
||||||
|
*
|
||||||
|
* @param buffer - Buffer to check
|
||||||
|
* @returns true if buffer is Lottie/WAS format
|
||||||
|
*/
|
||||||
|
export const isLottieBuffer = (buffer: Buffer): boolean => {
|
||||||
|
if (buffer.length < 2) return false
|
||||||
|
|
||||||
|
let jsonBuffer: Buffer
|
||||||
|
|
||||||
|
// Check if gzip-compressed (WAS format)
|
||||||
|
if (buffer[0] === 0x1f && buffer[1] === 0x8b) {
|
||||||
|
try {
|
||||||
|
// SECURITY: Limit decompressed output to 50MB to prevent decompression bombs
|
||||||
|
jsonBuffer = gunzipSync(buffer, { maxOutputLength: 50 * 1024 * 1024 }) as Buffer
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else if (buffer[0] === 0x7b) {
|
||||||
|
// Starts with '{' - could be raw Lottie JSON
|
||||||
|
jsonBuffer = buffer
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate Lottie JSON structure: must have v, ip, op, AND layers
|
||||||
|
try {
|
||||||
|
const str = jsonBuffer.toString('utf8', 0, Math.min(jsonBuffer.length, 4096))
|
||||||
|
// Quick check for ALL required Lottie fields
|
||||||
|
return str.includes('"v"') && str.includes('"layers"') && str.includes('"ip"') && str.includes('"op"')
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converte uma imagem para WebP usando Sharp
|
* Converte uma imagem para WebP usando Sharp
|
||||||
* Preserva o buffer original se já for WebP para manter EXIF e animações
|
* Preserva o buffer original se já for WebP para manter EXIF e animações
|
||||||
@@ -114,12 +158,25 @@ export const isAnimatedWebP = (buffer: Buffer): boolean => {
|
|||||||
const convertToWebP = async (
|
const convertToWebP = async (
|
||||||
buffer: Buffer,
|
buffer: Buffer,
|
||||||
logger?: ILogger
|
logger?: ILogger
|
||||||
): Promise<{ webpBuffer: Buffer; isAnimated: boolean }> => {
|
): Promise<{ webpBuffer: Buffer; isAnimated: boolean; isLottie: boolean }> => {
|
||||||
|
// Lottie/WAS: ensure gzip-compressed format (WAS = gzip Lottie JSON)
|
||||||
|
if (isLottieBuffer(buffer)) {
|
||||||
|
let wasBuffer = buffer
|
||||||
|
// Raw Lottie JSON (starts with '{') must be gzipped to produce valid WAS
|
||||||
|
if (buffer[0] === 0x7b) {
|
||||||
|
logger?.trace('Raw Lottie JSON detected, gzip-compressing to WAS format')
|
||||||
|
wasBuffer = gzipSync(buffer) as Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
logger?.trace('Input is Lottie/WAS format')
|
||||||
|
return { webpBuffer: wasBuffer, isAnimated: true, isLottie: true }
|
||||||
|
}
|
||||||
|
|
||||||
// Se já é WebP, preserva o buffer original (mantém EXIF e animações)
|
// Se já é WebP, preserva o buffer original (mantém EXIF e animações)
|
||||||
if (isWebPBuffer(buffer)) {
|
if (isWebPBuffer(buffer)) {
|
||||||
const isAnimated = isAnimatedWebP(buffer)
|
const isAnimated = isAnimatedWebP(buffer)
|
||||||
logger?.trace({ isAnimated }, 'Input is already WebP, preserving original buffer')
|
logger?.trace({ isAnimated }, 'Input is already WebP, preserving original buffer')
|
||||||
return { webpBuffer: buffer, isAnimated }
|
return { webpBuffer: buffer, isAnimated, isLottie: false }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tenta usar Sharp para converter
|
// Tenta usar Sharp para converter
|
||||||
@@ -135,7 +192,7 @@ const convertToWebP = async (
|
|||||||
logger?.trace('Converting image to WebP using Sharp')
|
logger?.trace('Converting image to WebP using Sharp')
|
||||||
const webpBuffer = await lib.sharp.default(buffer).webp().toBuffer()
|
const webpBuffer = await lib.sharp.default(buffer).webp().toBuffer()
|
||||||
|
|
||||||
return { webpBuffer, isAnimated: false }
|
return { webpBuffer, isAnimated: false, isLottie: false }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -338,9 +395,11 @@ export type PrepareStickerPackMessageOptions = {
|
|||||||
*
|
*
|
||||||
* **Especificações WhatsApp:**
|
* **Especificações WhatsApp:**
|
||||||
* - 3-30 stickers por pack (oficial)
|
* - 3-30 stickers por pack (oficial)
|
||||||
* - WebP obrigatório
|
* - WebP ou Lottie/WAS (application/was)
|
||||||
|
* - Stickers: 512x512 pixels (auto-resize)
|
||||||
* - Recomendado: 100KB por sticker estático, 500KB animado
|
* - Recomendado: 100KB por sticker estático, 500KB animado
|
||||||
* - Tray icon: 252x252 pixels
|
* - Tray icon: 96x96 pixels (PNG no ZIP)
|
||||||
|
* - Thumbnail: 252x252 pixels (JPEG, upload separado)
|
||||||
*
|
*
|
||||||
* @param stickerPack - Sticker pack data with stickers, cover, name, publisher
|
* @param stickerPack - Sticker pack data with stickers, cover, name, publisher
|
||||||
* @param options - Upload function and optional logger
|
* @param options - Upload function and optional logger
|
||||||
@@ -464,9 +523,36 @@ export const prepareStickerPackMessage = async (
|
|||||||
// Obtém buffer do sticker
|
// Obtém buffer do sticker
|
||||||
const buffer = await mediaToBuffer(sticker.data, `sticker ${i + 1}`)
|
const buffer = await mediaToBuffer(sticker.data, `sticker ${i + 1}`)
|
||||||
|
|
||||||
// Converte para WebP
|
// Detecta formato e converte se necessário
|
||||||
// eslint-disable-next-line prefer-const
|
// eslint-disable-next-line prefer-const
|
||||||
let { webpBuffer, isAnimated } = await convertToWebP(buffer, logger)
|
let { webpBuffer, isAnimated, isLottie } = await convertToWebP(buffer, logger)
|
||||||
|
|
||||||
|
// Validate explicit isLottie flag — must match detected format
|
||||||
|
if (sticker.isLottie !== undefined && sticker.isLottie !== isLottie) {
|
||||||
|
throw new Boom(
|
||||||
|
`Sticker ${i + 1}: explicit isLottie=${sticker.isLottie} does not match detected format (detected=${isLottie})`,
|
||||||
|
{ statusCode: 400 }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WABA: Enforce 512x512 dimensions for WebP stickers (Lottie is vector, skip)
|
||||||
|
if (!isLottie && isWebPBuffer(webpBuffer)) {
|
||||||
|
const lib = await getImageProcessingLibrary()
|
||||||
|
if (lib?.sharp) {
|
||||||
|
const metadata = await lib.sharp.default(webpBuffer).metadata()
|
||||||
|
if (metadata.width !== 512 || metadata.height !== 512) {
|
||||||
|
logger?.trace(
|
||||||
|
{ index: i, width: metadata.width, height: metadata.height },
|
||||||
|
'Resizing sticker to 512x512 (WABA standard)'
|
||||||
|
)
|
||||||
|
webpBuffer = await lib.sharp
|
||||||
|
.default(webpBuffer)
|
||||||
|
.resize(512, 512, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
|
||||||
|
.webp()
|
||||||
|
.toBuffer()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ENHANCEMENT: Auto-compression if exceeds 1MB (try quality 70, then 50)
|
// ENHANCEMENT: Auto-compression if exceeds 1MB (try quality 70, then 50)
|
||||||
const MAX_STICKER_SIZE = 1024 * 1024 // 1MB
|
const MAX_STICKER_SIZE = 1024 * 1024 // 1MB
|
||||||
@@ -483,7 +569,7 @@ export const prepareStickerPackMessage = async (
|
|||||||
if (lib?.sharp) {
|
if (lib?.sharp) {
|
||||||
// Try quality 70
|
// Try quality 70
|
||||||
try {
|
try {
|
||||||
const compressed70 = await lib.sharp.default(buffer).webp({ quality: 70 }).toBuffer()
|
const compressed70 = await lib.sharp.default(webpBuffer).webp({ quality: 70 }).toBuffer()
|
||||||
|
|
||||||
// eslint-disable-next-line max-depth
|
// eslint-disable-next-line max-depth
|
||||||
if (compressed70.length <= MAX_STICKER_SIZE) {
|
if (compressed70.length <= MAX_STICKER_SIZE) {
|
||||||
@@ -498,7 +584,7 @@ export const prepareStickerPackMessage = async (
|
|||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
// Try quality 50
|
// Try quality 50
|
||||||
const compressed50 = await lib.sharp.default(buffer).webp({ quality: 50 }).toBuffer()
|
const compressed50 = await lib.sharp.default(webpBuffer).webp({ quality: 50 }).toBuffer()
|
||||||
|
|
||||||
// eslint-disable-next-line max-depth
|
// eslint-disable-next-line max-depth
|
||||||
if (compressed50.length <= MAX_STICKER_SIZE) {
|
if (compressed50.length <= MAX_STICKER_SIZE) {
|
||||||
@@ -547,12 +633,13 @@ export const prepareStickerPackMessage = async (
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gera nome do arquivo: hash.webp (deduplicação automática)
|
// Gera nome do arquivo: hash.webp ou hash.was (WABA: plain_file_hash.ext)
|
||||||
const sha256Hash = generateSha256Hash(webpBuffer)
|
const sha256Hash = generateSha256Hash(webpBuffer)
|
||||||
const fileName = `${sha256Hash}.webp`
|
const extension = isLottie ? 'was' : 'webp'
|
||||||
|
const fileName = `${sha256Hash}.${extension}`
|
||||||
|
|
||||||
logger?.trace(
|
logger?.trace(
|
||||||
{ index: i, fileName, sizeKB: finalSizeKB.toFixed(2), isAnimated },
|
{ index: i, fileName, sizeKB: finalSizeKB.toFixed(2), isAnimated, isLottie },
|
||||||
'Sticker processed successfully'
|
'Sticker processed successfully'
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -560,6 +647,7 @@ export const prepareStickerPackMessage = async (
|
|||||||
fileName,
|
fileName,
|
||||||
webpBuffer,
|
webpBuffer,
|
||||||
isAnimated,
|
isAnimated,
|
||||||
|
isLottie,
|
||||||
emojis: sticker.emojis || [],
|
emojis: sticker.emojis || [],
|
||||||
accessibilityLabel: sticker.accessibilityLabel
|
accessibilityLabel: sticker.accessibilityLabel
|
||||||
}
|
}
|
||||||
@@ -578,7 +666,7 @@ export const prepareStickerPackMessage = async (
|
|||||||
for (const result of processedStickers) {
|
for (const result of processedStickers) {
|
||||||
if (!result) continue
|
if (!result) continue
|
||||||
|
|
||||||
const { fileName, webpBuffer, isAnimated, emojis, accessibilityLabel } = result
|
const { fileName, webpBuffer, isAnimated, isLottie, emojis, accessibilityLabel } = result
|
||||||
|
|
||||||
// SECURITY FIX #7: Check if this hash already exists (duplicate sticker)
|
// SECURITY FIX #7: Check if this hash already exists (duplicate sticker)
|
||||||
const existingMetadata = metadataByHash.get(fileName)
|
const existingMetadata = metadataByHash.get(fileName)
|
||||||
@@ -605,13 +693,14 @@ export const prepareStickerPackMessage = async (
|
|||||||
// New sticker - add to ZIP and create metadata
|
// New sticker - add to ZIP and create metadata
|
||||||
stickerData[fileName] = [new Uint8Array(webpBuffer), { level: 0 as 0 }]
|
stickerData[fileName] = [new Uint8Array(webpBuffer), { level: 0 as 0 }]
|
||||||
|
|
||||||
|
// WABA: mimetype é 'application/was' para Lottie, 'image/webp' para WebP
|
||||||
const metadata: proto.Message.StickerPackMessage.ISticker = {
|
const metadata: proto.Message.StickerPackMessage.ISticker = {
|
||||||
fileName,
|
fileName,
|
||||||
isAnimated,
|
isAnimated,
|
||||||
emojis,
|
emojis,
|
||||||
accessibilityLabel,
|
accessibilityLabel,
|
||||||
isLottie: false,
|
isLottie,
|
||||||
mimetype: 'image/webp'
|
mimetype: isLottie ? 'application/was' : 'image/webp'
|
||||||
}
|
}
|
||||||
|
|
||||||
metadataByHash.set(fileName, metadata)
|
metadataByHash.set(fileName, metadata)
|
||||||
@@ -636,10 +725,22 @@ export const prepareStickerPackMessage = async (
|
|||||||
logger?.trace('Processing cover image')
|
logger?.trace('Processing cover image')
|
||||||
coverBuffer = await mediaToBuffer(cover, 'cover image')
|
coverBuffer = await mediaToBuffer(cover, 'cover image')
|
||||||
|
|
||||||
// Converte cover para WebP e adiciona ao ZIP
|
// Tray icon uses PNG format, 96x96 pixels (official client standard)
|
||||||
const result = await convertToWebP(coverBuffer, logger)
|
const lib = await getImageProcessingLibrary()
|
||||||
coverWebP = result.webpBuffer
|
if (!lib?.sharp) {
|
||||||
coverFileName = `${stickerPackId}.webp`
|
throw new Boom(
|
||||||
|
'Sharp library is required for cover/tray icon processing. Install with: yarn add sharp',
|
||||||
|
{ statusCode: 400 }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
coverWebP = await lib.sharp
|
||||||
|
.default(coverBuffer)
|
||||||
|
.resize(96, 96, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
|
||||||
|
.png()
|
||||||
|
.toBuffer()
|
||||||
|
|
||||||
|
coverFileName = `${stickerPackId}.png`
|
||||||
stickerData[coverFileName] = [new Uint8Array(coverWebP), { level: 0 as 0 }]
|
stickerData[coverFileName] = [new Uint8Array(coverWebP), { level: 0 as 0 }]
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Boom(`Failed to process cover image: ${(error as Error).message}`, {
|
throw new Boom(`Failed to process cover image: ${(error as Error).message}`, {
|
||||||
|
|||||||
Reference in New Issue
Block a user