From dda909eb0546396c8c970cb146924d0a48c73998 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Wed, 13 Apr 2022 13:20:43 +0530 Subject: [PATCH 1/3] feat: add "--no-store" + "--no-reply" flags to example --- Example/example.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Example/example.ts b/Example/example.ts index 9e4e259e..8e8aeb1b 100644 --- a/Example/example.ts +++ b/Example/example.ts @@ -5,13 +5,16 @@ import MAIN_LOGGER from '../src/Utils/logger' const logger = MAIN_LOGGER.child({ }) logger.level = 'trace' +const useStore = !process.argv.includes('--no-store') +const doReplies = !process.argv.includes('--no-reply') + // the store maintains the data of the WA connection in memory // can be written out to a file & read from it -const store = makeInMemoryStore({ logger }) -store.readFromFile('./baileys_store_multi.json') +const store = useStore ? makeInMemoryStore({ logger }) : undefined +store?.readFromFile('./baileys_store_multi.json') // save every 10s setInterval(() => { - store.writeToFile('./baileys_store_multi.json') + store?.writeToFile('./baileys_store_multi.json') }, 10_000) const { state, saveState } = useSingleFileAuthState('./auth_info_multi.json') @@ -35,7 +38,7 @@ const startSock = async() => { } }) - store.bind(sock.ev) + store?.bind(sock.ev) const sendMessageWTyping = async(msg: AnyMessageContent, jid: string) => { await sock.presenceSubscribe(jid) @@ -57,7 +60,7 @@ const startSock = async() => { console.log(JSON.stringify(m, undefined, 2)) const msg = m.messages[0] - if(!msg.key.fromMe && m.type === 'notify') { + if(!msg.key.fromMe && m.type === 'notify' && doReplies) { console.log('replying to', m.messages[0].key.remoteJid) await sock!.sendReadReceipt(msg.key.remoteJid, msg.key.participant, [msg.key.id]) await sendMessageWTyping({ text: 'Hello there!' }, msg.key.remoteJid) From 647138ffe52e784fb6fb92e5759c5ff00b5457bc Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Wed, 13 Apr 2022 13:20:56 +0530 Subject: [PATCH 2/3] chore: remove redundant ID flags in query --- src/Socket/chats.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index a3fe7879..75f3a533 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -542,7 +542,6 @@ export const makeChatsSocket = (config: SocketConfig) => { to: S_WHATSAPP_NET, xmlns: 'abt', type: 'get', - id: generateMessageTag(), }, content: [ { tag: 'props', attrs: { protocol: '1' } } @@ -569,7 +568,6 @@ export const makeChatsSocket = (config: SocketConfig) => { to: S_WHATSAPP_NET, xmlns: 'w', type: 'get', - id: generateMessageTag(), }, content: [ { tag: 'props', attrs: { } } From 0cf17fb392ce98857c5aadf1a6b958f5eefa51cc Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Wed, 13 Apr 2022 13:21:15 +0530 Subject: [PATCH 3/3] refactor: use Buffer instead of Binary --- src/Utils/noise-handler.ts | 4 ++-- src/Utils/validate-connection.ts | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Utils/noise-handler.ts b/src/Utils/noise-handler.ts index f57509fa..96a65d44 100644 --- a/src/Utils/noise-handler.ts +++ b/src/Utils/noise-handler.ts @@ -18,7 +18,7 @@ export const makeNoiseHandler = ({ public: publicKey, private: privateKey }: Key const authenticate = (data: Uint8Array) => { if(!isFinished) { - hash = sha256(Buffer.from(Binary.build(hash, data).readByteArray())) + hash = sha256(Buffer.concat([hash, data])) } } @@ -84,7 +84,7 @@ export const makeNoiseHandler = ({ public: publicKey, private: privateKey }: Key isFinished = true } - const data = Binary.build(NOISE_MODE).readBuffer() + const data = Buffer.from(NOISE_MODE) let hash = Buffer.from(data.byteLength === 32 ? data : sha256(Buffer.from(data))) let salt = hash let encKey = hash diff --git a/src/Utils/validate-connection.ts b/src/Utils/validate-connection.ts index 3b0ca1b4..9e25f92d 100644 --- a/src/Utils/validate-connection.ts +++ b/src/Utils/validate-connection.ts @@ -2,7 +2,7 @@ import { Boom } from '@hapi/boom' import { createHash } from 'crypto' import { proto } from '../../WAProto' import type { AuthenticationCreds, SignalCreds, SocketConfig } from '../Types' -import { Binary, BinaryNode, getAllBinaryNodeChildren, jidDecode, S_WHATSAPP_NET } from '../WABinary' +import { BinaryNode, getAllBinaryNodeChildren, jidDecode, S_WHATSAPP_NET } from '../WABinary' import { Curve, hmacSign } from './crypto' import { encodeInt } from './generics' import { createSignalIdentity } from './signal' @@ -116,12 +116,21 @@ export const configureSuccessfulPairing = ( const account = proto.ADVSignedDeviceIdentity.decode(details) const { accountSignatureKey, accountSignature } = account - const accountMsg = Binary.build(new Uint8Array([6, 0]), account.details, signedIdentityKey.public).readByteArray() + const accountMsg = Buffer.concat([ + Buffer.from([6, 0]), + account.details, + signedIdentityKey.public + ]) if(!Curve.verify(accountSignatureKey, accountMsg, accountSignature)) { throw new Boom('Failed to verify account signature') } - const deviceMsg = Binary.build(new Uint8Array([6, 1]), account.details, signedIdentityKey.public, account.accountSignatureKey).readByteArray() + const deviceMsg = Buffer.concat([ + new Uint8Array([6, 1]), + account.details, + signedIdentityKey.public, + account.accountSignatureKey + ]) account.deviceSignature = Curve.sign(signedIdentityKey.private, deviceMsg) const identity = createSignalIdentity(jid, accountSignatureKey)