Change the way we create message tags to index model

This commit is contained in:
Kayky Ramos
2020-07-09 18:43:22 -03:00
parent fae06e86ac
commit 369c5d065e
3 changed files with 2555 additions and 10 deletions

View File

@@ -4,8 +4,15 @@ import WS from 'ws'
import * as Utils from './Utils'
import Encoder from '../Binary/Encoder'
import Decoder from '../Binary/Decoder'
import { AuthenticationCredentials, UserMetaData, WANode, AuthenticationCredentialsBase64, WATag, MessageLogLevel, AuthenticationCredentialsBrowser } from './Constants'
import {
AuthenticationCredentials,
UserMetaData,
WANode,
AuthenticationCredentialsBase64,
WATag,
MessageLogLevel,
AuthenticationCredentialsBrowser,
} from './Constants'
/** Generate a QR code from the ref & the curve public key. This is scanned by the phone */
const generateQRCode = function ([ref, publicKey, clientID]) {
@@ -96,9 +103,9 @@ export default class WAConnectionBase {
authInfo = JSON.parse(file) as AuthenticationCredentialsBrowser
}
this.authInfo = {
clientID: authInfo.WABrowserId.replace (/\"/g, ''),
serverToken: authInfo.WAToken2.replace (/\"/g, ''),
clientToken: authInfo.WAToken1.replace (/\"/g, ''),
clientID: authInfo.WABrowserId.replace(/\"/g, ''),
serverToken: authInfo.WAToken2.replace(/\"/g, ''),
clientToken: authInfo.WAToken1.replace(/\"/g, ''),
encKey: Buffer.from(authInfo.WASecretBundle.encKey, 'base64'), // decode from base64
macKey: Buffer.from(authInfo.WASecretBundle.macKey, 'base64'), // decode from base64
}
@@ -216,7 +223,7 @@ export default class WAConnectionBase {
let buff = Utils.aesEncrypt(binary, this.authInfo.encKey) // encrypt it using AES and our encKey
const sign = Utils.hmacSign(buff, this.authInfo.macKey) // sign the message using HMAC and our macKey
tag = tag || Utils.generateMessageTag()
tag = tag || Utils.generateMessageTag(this.msgCount)
buff = Buffer.concat([
Buffer.from(tag + ','), // generate & prefix the message tag
Buffer.from(tags), // prefix some bytes that tell whatsapp what the message is about
@@ -234,7 +241,7 @@ export default class WAConnectionBase {
* @return the message tag
*/
private sendJSON(json: any[] | WANode, tag: string = null) {
tag = tag || Utils.generateMessageTag()
tag = tag || Utils.generateMessageTag(this.msgCount)
this.send(tag + ',' + JSON.stringify(json))
return tag
}

View File

@@ -38,7 +38,9 @@ export function randomBytes(length) {
return Crypto.randomBytes(length)
}
export function promiseTimeout<T>(ms: number, promise: Promise<T>) {
if (!ms) { return promise }
if (!ms) {
return promise
}
// Create a promise that rejects in <ms> milliseconds
const timeout = new Promise((_, reject) => {
const id = setTimeout(() => {
@@ -49,8 +51,10 @@ export function promiseTimeout<T>(ms: number, promise: Promise<T>) {
return Promise.race([promise, timeout]) as Promise<T>
}
// whatsapp requires a message tag for every message, we just use the timestamp as one
export function generateMessageTag() {
return new Date().getTime().toString()
export function generateMessageTag(epoch?: number) {
let tag = new Date().getTime().toString()
if (epoch) tag += '-' + epoch // attach epoch if provided
return tag
}
// generate a random 16 byte client ID
export function generateClientID() {

2534
yarn.lock Normal file

File diff suppressed because it is too large Load Diff