slimmed down package, fixed issues

This commit is contained in:
Adhiraj
2020-08-15 22:44:10 +05:30
parent 6c4d96af32
commit 1360bef9bb
3 changed files with 42 additions and 49 deletions

View File

@@ -21,7 +21,7 @@
"test": "mocha --timeout 60000 -r ts-node/register src/*/Tests.ts", "test": "mocha --timeout 60000 -r ts-node/register src/*/Tests.ts",
"lint": "eslint '*/*.ts' --quiet --fix", "lint": "eslint '*/*.ts' --quiet --fix",
"build": "tsc", "build": "tsc",
"build:docs":"typedoc", "build:docs": "typedoc",
"example": "npx ts-node Example/example.ts", "example": "npx ts-node Example/example.ts",
"browser-decode": "npx ts-node src/WAConnection/BrowserMessageDecoding.ts" "browser-decode": "npx ts-node src/WAConnection/BrowserMessageDecoding.ts"
}, },
@@ -45,15 +45,9 @@
"@types/mocha": "^7.0.2", "@types/mocha": "^7.0.2",
"@types/node": "^14.0.14", "@types/node": "^14.0.14",
"@types/ws": "^7.2.6", "@types/ws": "^7.2.6",
"@typescript-eslint/eslint-plugin": "^3.5.0",
"@typescript-eslint/parser": "^3.5.0",
"assert": "^2.0.0", "assert": "^2.0.0",
"dotenv": "^8.2.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", "mocha": "^8.0.1",
"prettier": "^2.0.5",
"ts-node-dev": "^1.0.0-pre.49", "ts-node-dev": "^1.0.0-pre.49",
"typedoc": "^0.18.0", "typedoc": "^0.18.0",
"typescript": "^3.9.5" "typescript": "^3.9.5"

View File

@@ -109,11 +109,16 @@ export default class WAConnectionConnector extends WAConnectionValidator {
if (json[1].duplicate) json = await this.registerCallbackOneTime (['response', 'type:chat']) if (json[1].duplicate) json = await this.registerCallbackOneTime (['response', 'type:chat'])
if (!json[2]) return 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.count = +chat.count
chat.messages = [] chat.messages = []
chats.insert (chat) // chats data (log json to see what it looks like) chats.insert (chat) // chats data (log json to see what it looks like)
}) })
.filter (Boolean)
if (chats.all().length > 0) return waitForConvos() if (chats.all().length > 0) return waitForConvos()
} }

View File

@@ -104,52 +104,46 @@ export default class WAConnectionValidator extends WAConnectionBase {
return this.userMetaData return this.userMetaData
} }
if (json.connected) { if (!json.secret) {
// only if we're connected // if we didn't get a secret, we don't need it, we're validated
if (!json.secret) { return onValidationSuccess()
// 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) {
const secret = Buffer.from(json.secret, 'base64') throw new Error ('incorrect secret length received: ' + secret.length)
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))
// generate shared key from our private key & the secret shared by the server // expand the key to 80 bytes using HKDF
const sharedKey = Curve.sharedKey(this.curveKeys.private, secret.slice(0, 32)) const expandedKey = Utils.hkdf(sharedKey as Buffer, 80)
// expand the key to 80 bytes using HKDF
const expandedKey = Utils.hkdf(sharedKey as Buffer, 80)
// perform HMAC validation. // perform HMAC validation.
const hmacValidationKey = expandedKey.slice(32, 64) const hmacValidationKey = expandedKey.slice(32, 64)
const hmacValidationMessage = Buffer.concat([secret.slice(0, 32), secret.slice(64, secret.length)]) 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))) { if (hmac.equals(secret.slice(32, 64))) {
// computed HMAC should equal secret[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 // 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] // they are encrypted using key: expandedKey[0:32]
const encryptedAESKeys = Buffer.concat([ const encryptedAESKeys = Buffer.concat([
expandedKey.slice(64, expandedKey.length), expandedKey.slice(64, expandedKey.length),
secret.slice(64, secret.length), secret.slice(64, secret.length),
]) ])
const decryptedKeys = Utils.aesDecrypt(encryptedAESKeys, expandedKey.slice(0, 32)) const decryptedKeys = Utils.aesDecrypt(encryptedAESKeys, expandedKey.slice(0, 32))
// set the credentials // set the credentials
this.authInfo = { this.authInfo = {
encKey: decryptedKeys.slice(0, 32), // first 32 bytes form the key to encrypt/decrypt messages 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 macKey: decryptedKeys.slice(32, 64), // last 32 bytes from the key to sign messages
clientToken: json.clientToken, clientToken: json.clientToken,
serverToken: json.serverToken, serverToken: json.serverToken,
clientID: this.authInfo.clientID, clientID: this.authInfo.clientID,
}
return onValidationSuccess()
} else {
// if the checksums didn't match
throw new BaileysError ('HMAC validation failed', json)
} }
return onValidationSuccess()
} else { } else {
// if we didn't get the connected field (usually we get this message when one opens WhatsApp on their phone) // if the checksums didn't match
throw new BaileysError (`invalid JSON`, json) throw new BaileysError ('HMAC validation failed', json)
} }
} }
/** /**