Update Utils.ts

This commit is contained in:
Adhiraj
2020-08-12 20:51:31 +05:30
parent 8202203c2e
commit 6c4d96af32

View File

@@ -93,7 +93,7 @@ export function generateClientID() {
export function generateMessageID() { export function generateMessageID() {
return randomBytes(10).toString('hex').toUpperCase() return randomBytes(10).toString('hex').toUpperCase()
} }
export function decryptWA (message: any, macKey: Buffer, encKey: Buffer, decoder: Decoder, fromMe: boolean=false): [string, Object, [number, number]?] { export function decryptWA (message: string | Buffer, macKey: Buffer, encKey: Buffer, decoder: Decoder, fromMe: boolean=false): [string, Object, [number, number]?] {
let commaIndex = message.indexOf(',') // all whatsapp messages have a tag and a comma, followed by the actual message let commaIndex = message.indexOf(',') // all whatsapp messages have a tag and a comma, followed by the actual message
if (commaIndex < 0) throw Error ('invalid message: ' + message) // if there was no comma, then this message must be not be valid if (commaIndex < 0) throw Error ('invalid message: ' + message) // if there was no comma, then this message must be not be valid
@@ -110,13 +110,13 @@ export function decryptWA (message: any, macKey: Buffer, encKey: Buffer, decoder
let json let json
let tags = null let tags = null
if (data[0] === '[' || data[0] === '{') { if (typeof data === 'string') {
// if the first character is a "[", then the data must just be plain JSON array or object // if the first character is a "[", then the data must just be plain JSON array or object
json = JSON.parse(data) // parse the JSON json = JSON.parse(data) // parse the JSON
} else { } else {
if (!macKey || !encKey) { if (!macKey || !encKey) {
// if we recieved a message that was encrypted but we don't have the keys, then there must be an error console.warn ('recieved encrypted buffer when auth creds unavailable: ' + message)
throw new Error ('recieved encrypted buffer when auth creds unavailable') return
} }
/* /*
If the data recieved was not a JSON, then it must be an encrypted message. If the data recieved was not a JSON, then it must be an encrypted message.