From 7fe4e58a11da11ac07dcfc3f8676e7578edefeee Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Tue, 12 Apr 2022 19:22:16 +0530 Subject: [PATCH] refactor: remove useless "encodeInt" function --- src/Utils/generics.ts | 14 +++----------- src/Utils/validate-connection.ts | 9 +++++---- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/Utils/generics.ts b/src/Utils/generics.ts index a8f6aaf..c79c956 100644 --- a/src/Utils/generics.ts +++ b/src/Utils/generics.ts @@ -75,17 +75,9 @@ export const encodeWAMessage = (message: proto.IMessage) => ( ) ) -export const generateRegistrationId = () => ( - Uint16Array.from(randomBytes(2))[0] -) - -export const encodeInt = (e: number, t: number) => { - for(var r = t, a = new Uint8Array(e), i = e - 1; i >= 0; i--) { - a[i] = 255 & r - r >>>= 8 - } - - return a +export const generateRegistrationId = () => { + const buff = randomBytes(4) + return buff.readUInt32BE() } export const encodeBigEndian = (e: number, t = 4) => { diff --git a/src/Utils/validate-connection.ts b/src/Utils/validate-connection.ts index 3d4b5ba..fcc1409 100644 --- a/src/Utils/validate-connection.ts +++ b/src/Utils/validate-connection.ts @@ -1,10 +1,11 @@ import { Boom } from '@hapi/boom' import { createHash } from 'crypto' import { proto } from '../../WAProto' +import { KEY_BUNDLE_TYPE } from '../Defaults' import type { AuthenticationCreds, SignalCreds, SocketConfig } from '../Types' import { Binary, BinaryNode, getAllBinaryNodeChildren, jidDecode, S_WHATSAPP_NET } from '../WABinary' import { Curve, hmacSign } from './crypto' -import { encodeInt } from './generics' +import { encodeBigEndian } from './generics' import { createSignalIdentity } from './signal' type ClientPayloadConfig = Pick @@ -80,10 +81,10 @@ export const generateRegistrationNode = ( regData: { buildHash: appVersionBuf, companionProps: companionProto, - eRegid: encodeInt(4, registrationId), - eKeytype: encodeInt(1, 5), + eRegid: encodeBigEndian(registrationId), + eKeytype: KEY_BUNDLE_TYPE, eIdent: signedIdentityKey.public, - eSkeyId: encodeInt(3, signedPreKey.keyId), + eSkeyId: encodeBigEndian(signedPreKey.keyId, 3), eSkeyVal: signedPreKey.keyPair.public, eSkeySig: signedPreKey.signature, },