From 1360bef9bbe5c9ed45f148e3b6781dffb536dc54 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Sat, 15 Aug 2020 22:44:10 +0530 Subject: [PATCH] slimmed down package, fixed issues --- package.json | 8 +--- src/WAConnection/Connect.ts | 7 +++- src/WAConnection/Validation.ts | 76 ++++++++++++++++------------------ 3 files changed, 42 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index 2b5552d..41970a2 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "test": "mocha --timeout 60000 -r ts-node/register src/*/Tests.ts", "lint": "eslint '*/*.ts' --quiet --fix", "build": "tsc", - "build:docs":"typedoc", + "build:docs": "typedoc", "example": "npx ts-node Example/example.ts", "browser-decode": "npx ts-node src/WAConnection/BrowserMessageDecoding.ts" }, @@ -45,15 +45,9 @@ "@types/mocha": "^7.0.2", "@types/node": "^14.0.14", "@types/ws": "^7.2.6", - "@typescript-eslint/eslint-plugin": "^3.5.0", - "@typescript-eslint/parser": "^3.5.0", "assert": "^2.0.0", "dotenv": "^8.2.0", - "eslint": "^7.3.1", - "eslint-config-prettier": "^6.11.0", - "eslint-plugin-prettier": "^3.1.4", "mocha": "^8.0.1", - "prettier": "^2.0.5", "ts-node-dev": "^1.0.0-pre.49", "typedoc": "^0.18.0", "typescript": "^3.9.5" diff --git a/src/WAConnection/Connect.ts b/src/WAConnection/Connect.ts index 70012af..ebd331e 100644 --- a/src/WAConnection/Connect.ts +++ b/src/WAConnection/Connect.ts @@ -109,11 +109,16 @@ export default class WAConnectionConnector extends WAConnectionValidator { if (json[1].duplicate) json = await this.registerCallbackOneTime (['response', 'type:chat']) if (!json[2]) return - json[2].forEach(([_, chat]: [any, WAChat]) => { + json[2].forEach(([item, chat]: [any, WAChat]) => { + if (!chat) { + this.log (`unexpectedly got null chat: ${item}, ${chat}`, MessageLogLevel.info) + return + } chat.count = +chat.count chat.messages = [] chats.insert (chat) // chats data (log json to see what it looks like) }) + .filter (Boolean) if (chats.all().length > 0) return waitForConvos() } diff --git a/src/WAConnection/Validation.ts b/src/WAConnection/Validation.ts index cf16b9f..11f8eea 100644 --- a/src/WAConnection/Validation.ts +++ b/src/WAConnection/Validation.ts @@ -104,52 +104,46 @@ export default class WAConnectionValidator extends WAConnectionBase { return this.userMetaData } - if (json.connected) { - // only if we're connected - if (!json.secret) { - // if we didn't get a secret, we don't need it, we're validated - return onValidationSuccess() - } - const secret = Buffer.from(json.secret, 'base64') - if (secret.length !== 144) { - throw new Error ('incorrect secret length received: ' + secret.length) - } - // generate shared key from our private key & the secret shared by the server - const sharedKey = Curve.sharedKey(this.curveKeys.private, secret.slice(0, 32)) - // expand the key to 80 bytes using HKDF - const expandedKey = Utils.hkdf(sharedKey as Buffer, 80) + if (!json.secret) { + // if we didn't get a secret, we don't need it, we're validated + return onValidationSuccess() + } + const secret = Buffer.from(json.secret, 'base64') + if (secret.length !== 144) { + throw new Error ('incorrect secret length received: ' + secret.length) + } + // generate shared key from our private key & the secret shared by the server + const sharedKey = Curve.sharedKey(this.curveKeys.private, secret.slice(0, 32)) + // expand the key to 80 bytes using HKDF + const expandedKey = Utils.hkdf(sharedKey as Buffer, 80) - // perform HMAC validation. - const hmacValidationKey = expandedKey.slice(32, 64) - const hmacValidationMessage = Buffer.concat([secret.slice(0, 32), secret.slice(64, secret.length)]) + // perform HMAC validation. + const hmacValidationKey = expandedKey.slice(32, 64) + const hmacValidationMessage = Buffer.concat([secret.slice(0, 32), secret.slice(64, secret.length)]) - const hmac = Utils.hmacSign(hmacValidationMessage, hmacValidationKey) + const hmac = Utils.hmacSign(hmacValidationMessage, hmacValidationKey) - if (hmac.equals(secret.slice(32, 64))) { - // computed HMAC should equal secret[32:64] - // expandedKey[64:] + secret[64:] are the keys, encrypted using AES, that are used to encrypt/decrypt the messages recieved from WhatsApp - // they are encrypted using key: expandedKey[0:32] - const encryptedAESKeys = Buffer.concat([ - expandedKey.slice(64, expandedKey.length), - secret.slice(64, secret.length), - ]) - const decryptedKeys = Utils.aesDecrypt(encryptedAESKeys, expandedKey.slice(0, 32)) - // set the credentials - this.authInfo = { - encKey: decryptedKeys.slice(0, 32), // first 32 bytes form the key to encrypt/decrypt messages - macKey: decryptedKeys.slice(32, 64), // last 32 bytes from the key to sign messages - clientToken: json.clientToken, - serverToken: json.serverToken, - clientID: this.authInfo.clientID, - } - return onValidationSuccess() - } else { - // if the checksums didn't match - throw new BaileysError ('HMAC validation failed', json) + if (hmac.equals(secret.slice(32, 64))) { + // computed HMAC should equal secret[32:64] + // expandedKey[64:] + secret[64:] are the keys, encrypted using AES, that are used to encrypt/decrypt the messages recieved from WhatsApp + // they are encrypted using key: expandedKey[0:32] + const encryptedAESKeys = Buffer.concat([ + expandedKey.slice(64, expandedKey.length), + secret.slice(64, secret.length), + ]) + const decryptedKeys = Utils.aesDecrypt(encryptedAESKeys, expandedKey.slice(0, 32)) + // set the credentials + this.authInfo = { + encKey: decryptedKeys.slice(0, 32), // first 32 bytes form the key to encrypt/decrypt messages + macKey: decryptedKeys.slice(32, 64), // last 32 bytes from the key to sign messages + clientToken: json.clientToken, + serverToken: json.serverToken, + clientID: this.authInfo.clientID, } + return onValidationSuccess() } else { - // if we didn't get the connected field (usually we get this message when one opens WhatsApp on their phone) - throw new BaileysError (`invalid JSON`, json) + // if the checksums didn't match + throw new BaileysError ('HMAC validation failed', json) } } /**