@ubedsockets/baileys 1.6.6 → 1.6.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/WAProto/GenerateStatics.sh +4 -3
  2. package/WAProto/WAProto.proto +511 -1215
  3. package/WAProto/index.js +131318 -59331
  4. package/lib/Defaults/index.js +92 -76
  5. package/lib/Signal/Group/ciphertext-message.js +5 -2
  6. package/lib/Signal/Group/group-session-builder.js +41 -7
  7. package/lib/Signal/Group/group_cipher.js +51 -37
  8. package/lib/Signal/Group/index.js +57 -12
  9. package/lib/Signal/Group/keyhelper.js +44 -7
  10. package/lib/Signal/Group/sender-chain-key.js +15 -7
  11. package/lib/Signal/Group/sender-key-distribution-message.js +11 -8
  12. package/lib/Signal/Group/sender-key-message.js +12 -9
  13. package/lib/Signal/Group/sender-key-name.js +5 -2
  14. package/lib/Signal/Group/sender-key-record.js +21 -9
  15. package/lib/Signal/Group/sender-key-state.js +42 -27
  16. package/lib/Signal/Group/sender-message-key.js +7 -4
  17. package/lib/Signal/libsignal.js +90 -347
  18. package/lib/Socket/Client/index.js +18 -3
  19. package/lib/Socket/Client/types.js +6 -4
  20. package/lib/Socket/Client/websocket.js +78 -21
  21. package/lib/Socket/business.js +43 -162
  22. package/lib/Socket/chats.js +249 -571
  23. package/lib/Socket/groups.js +99 -114
  24. package/lib/Socket/index.js +9 -11
  25. package/lib/Socket/messages-recv.js +559 -1277
  26. package/lib/Socket/messages-send.js +669 -674
  27. package/lib/Socket/newsletter.js +286 -149
  28. package/lib/Socket/socket.js +235 -548
  29. package/lib/Types/Auth.js +2 -2
  30. package/lib/Types/Call.js +2 -2
  31. package/lib/Types/Chat.js +4 -8
  32. package/lib/Types/Contact.js +2 -2
  33. package/lib/Types/Events.js +2 -2
  34. package/lib/Types/GroupMetadata.js +2 -2
  35. package/lib/Types/Label.js +5 -3
  36. package/lib/Types/LabelAssociation.js +5 -3
  37. package/lib/Types/Message.js +7 -11
  38. package/lib/Types/Product.js +2 -2
  39. package/lib/Types/Signal.js +2 -2
  40. package/lib/Types/Socket.js +2 -3
  41. package/lib/Types/State.js +2 -56
  42. package/lib/Types/USync.js +2 -2
  43. package/lib/Types/index.js +31 -15
  44. package/lib/Utils/auth-utils.js +136 -239
  45. package/lib/Utils/business.js +69 -66
  46. package/lib/Utils/chat-utils.js +238 -380
  47. package/lib/Utils/crypto.js +127 -56
  48. package/lib/Utils/decode-wa-message.js +63 -173
  49. package/lib/Utils/event-buffer.js +70 -174
  50. package/lib/Utils/generics.js +207 -159
  51. package/lib/Utils/history.js +53 -93
  52. package/lib/Utils/index.js +33 -22
  53. package/lib/Utils/link-preview.js +58 -17
  54. package/lib/Utils/logger.js +7 -3
  55. package/lib/Utils/lt-hash.js +46 -3
  56. package/lib/Utils/make-mutex.js +34 -24
  57. package/lib/Utils/messages-media.js +744 -483
  58. package/lib/Utils/messages.js +621 -411
  59. package/lib/Utils/noise-handler.js +99 -149
  60. package/lib/Utils/process-message.js +150 -459
  61. package/lib/Utils/signal.js +73 -112
  62. package/lib/Utils/use-multi-file-auth-state.js +27 -23
  63. package/lib/Utils/validate-connection.js +104 -74
  64. package/lib/WABinary/constants.js +11 -9
  65. package/lib/WABinary/decode.js +52 -49
  66. package/lib/WABinary/encode.js +49 -19
  67. package/lib/WABinary/generic-utils.js +43 -46
  68. package/lib/WABinary/index.js +21 -6
  69. package/lib/WABinary/jid-utils.js +45 -72
  70. package/lib/WABinary/types.js +2 -2
  71. package/lib/WAM/BinaryInfo.js +5 -2
  72. package/lib/WAM/constants.js +11958 -19461
  73. package/lib/WAM/encode.js +22 -17
  74. package/lib/WAM/index.js +19 -4
  75. package/lib/WAUSync/Protocols/USyncContactProtocol.js +13 -33
  76. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +14 -11
  77. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +11 -8
  78. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +13 -9
  79. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +22 -20
  80. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +8 -13
  81. package/lib/WAUSync/Protocols/index.js +20 -6
  82. package/lib/WAUSync/USyncQuery.js +35 -44
  83. package/lib/WAUSync/USyncUser.js +5 -10
  84. package/lib/WAUSync/index.js +19 -4
  85. package/lib/index.js +70 -11
  86. package/package.json +60 -100
  87. package/engine-requirements.js +0 -10
@@ -1,78 +1,85 @@
1
- import { Boom } from '@hapi/boom';
2
- import { createHash } from 'crypto';
3
- import { proto } from '../../WAProto/index.js';
4
- import { KEY_BUNDLE_TYPE, WA_ADV_ACCOUNT_SIG_PREFIX, WA_ADV_DEVICE_SIG_PREFIX, WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX } from '../Defaults/index.js';
5
- import { getBinaryNodeChild, jidDecode, S_WHATSAPP_NET } from '../WABinary/index.js';
6
- import { Curve, hmacSign } from './crypto.js';
7
- import { encodeBigEndian } from './generics.js';
8
- import { createSignalIdentity } from './signal.js';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.encodeSignedDeviceIdentity =
4
+ exports.configureSuccessfulPairing =
5
+ exports.generateRegistrationNode =
6
+ exports.generateLoginNode = void 0;
7
+
8
+ const boom_1 = require("@hapi/boom");
9
+ const crypto_1 = require("crypto");
10
+ const WAProto_1 = require("../../WAProto");
11
+ const Defaults_1 = require("../Defaults");
12
+ const WABinary_1 = require("../WABinary");
13
+ const crypto_2 = require("./crypto");
14
+ const generics_1 = require("./generics");
15
+ const signal_1 = require("./signal");
16
+
9
17
  const getUserAgent = (config) => {
10
18
  return {
11
19
  appVersion: {
12
20
  primary: config.version[0],
13
21
  secondary: config.version[1],
14
- tertiary: config.version[2]
22
+ tertiary: config.version[2],
15
23
  },
16
- platform: proto.ClientPayload.UserAgent.Platform.WEB,
17
- releaseChannel: proto.ClientPayload.UserAgent.ReleaseChannel.RELEASE,
24
+ platform: WAProto_1.proto.ClientPayload.UserAgent.Platform.WEB,
25
+ releaseChannel: WAProto_1.proto.ClientPayload.UserAgent.ReleaseChannel.RELEASE,
18
26
  osVersion: '0.1',
19
27
  device: 'Desktop',
20
28
  osBuildNumber: '0.1',
21
29
  localeLanguageIso6391: 'en',
22
30
  mnc: '000',
23
31
  mcc: '000',
24
- localeCountryIso31661Alpha2: config.countryCode
32
+ localeCountryIso31661Alpha2: config.countryCode || 'US'
25
33
  };
26
34
  };
35
+
27
36
  const PLATFORM_MAP = {
28
- 'Mac OS': proto.ClientPayload.WebInfo.WebSubPlatform.DARWIN,
29
- Windows: proto.ClientPayload.WebInfo.WebSubPlatform.WIN32
37
+ 'Mac OS': WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.DARWIN,
38
+ 'Windows': WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.WIN32
30
39
  };
40
+
31
41
  const getWebInfo = (config) => {
32
- let webSubPlatform = proto.ClientPayload.WebInfo.WebSubPlatform.WEB_BROWSER;
33
- if (config.syncFullHistory &&
34
- PLATFORM_MAP[config.browser[0]] &&
35
- config.browser[1] === 'Desktop') {
42
+ let webSubPlatform = WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.WEB_BROWSER;
43
+ if (config.syncFullHistory && PLATFORM_MAP[config.browser[0]] && config.browser[1] === 'Desktop') {
36
44
  webSubPlatform = PLATFORM_MAP[config.browser[0]];
37
45
  }
38
46
  return { webSubPlatform };
39
47
  };
48
+
40
49
  const getClientPayload = (config) => {
41
50
  const payload = {
42
- connectType: proto.ClientPayload.ConnectType.WIFI_UNKNOWN,
43
- connectReason: proto.ClientPayload.ConnectReason.USER_ACTIVATED,
44
- userAgent: getUserAgent(config)
51
+ connectType: WAProto_1.proto.ClientPayload.ConnectType.WIFI_UNKNOWN,
52
+ connectReason: WAProto_1.proto.ClientPayload.ConnectReason.USER_ACTIVATED,
53
+ userAgent: getUserAgent(config),
45
54
  };
46
55
  payload.webInfo = getWebInfo(config);
47
- if (config.pushName) {
48
- payload.pushName = config.pushName;
49
- }
50
56
  return payload;
51
57
  };
52
- export const generateLoginNode = (userJid, config) => {
53
- const { user, device } = jidDecode(userJid);
58
+
59
+ const generateLoginNode = (userJid, config) => {
60
+ const { user, device } = (0, WABinary_1.jidDecode)(userJid);
54
61
  const payload = {
55
62
  ...getClientPayload(config),
56
63
  passive: true,
57
64
  pull: true,
58
65
  username: +user,
59
66
  device: device,
60
- // TODO: investigate (hard set as false atm)
61
67
  lidDbMigrated: false
62
68
  };
63
- return proto.ClientPayload.fromObject(payload);
69
+ return WAProto_1.proto.ClientPayload.fromObject(payload);
64
70
  };
71
+ exports.generateLoginNode = generateLoginNode;
72
+
65
73
  const getPlatformType = (platform) => {
66
74
  const platformType = platform.toUpperCase();
67
- return (proto.DeviceProps.PlatformType[platformType] ||
68
- proto.DeviceProps.PlatformType.CHROME);
75
+ return WAProto_1.proto.DeviceProps.PlatformType[platformType] || WAProto_1.proto.DeviceProps.PlatformType.CHROME;
69
76
  };
70
- export const generateRegistrationNode = ({ registrationId, signedPreKey, signedIdentityKey }, config) => {
71
- // the app version needs to be md5 hashed
72
- // and passed in
73
- const appVersionBuf = createHash('md5')
74
- .update(config.version.join('.')) // join as string
77
+
78
+ const generateRegistrationNode = ({ registrationId, signedPreKey, signedIdentityKey }, config) => {
79
+ const appVersionBuf = (0, crypto_1.createHash)('md5') // 🅥🅨🅩🅔🅝
80
+ .update(config.version.join('.'))
75
81
  .digest();
82
+
76
83
  const companion = {
77
84
  os: config.browser[0],
78
85
  platformType: getPlatformType(config.browser[1]),
@@ -100,7 +107,9 @@ export const generateRegistrationNode = ({ registrationId, signedPreKey, signedI
100
107
  tertiary: 7
101
108
  }
102
109
  };
103
- const companionProto = proto.DeviceProps.encode(companion).finish();
110
+
111
+ const companionProto = WAProto_1.proto.DeviceProps.encode(companion).finish();
112
+
104
113
  const registerPayload = {
105
114
  ...getClientPayload(config),
106
115
  passive: false,
@@ -108,65 +117,79 @@ export const generateRegistrationNode = ({ registrationId, signedPreKey, signedI
108
117
  devicePairingData: {
109
118
  buildHash: appVersionBuf,
110
119
  deviceProps: companionProto,
111
- eRegid: encodeBigEndian(registrationId),
112
- eKeytype: KEY_BUNDLE_TYPE,
120
+ eRegid: (0, generics_1.encodeBigEndian)(registrationId),
121
+ eKeytype: Defaults_1.KEY_BUNDLE_TYPE,
113
122
  eIdent: signedIdentityKey.public,
114
- eSkeyId: encodeBigEndian(signedPreKey.keyId, 3),
123
+ eSkeyId: (0, generics_1.encodeBigEndian)(signedPreKey.keyId, 3),
115
124
  eSkeyVal: signedPreKey.keyPair.public,
116
- eSkeySig: signedPreKey.signature
117
- }
125
+ eSkeySig: signedPreKey.signature,
126
+ },
118
127
  };
119
- return proto.ClientPayload.fromObject(registerPayload);
128
+ return WAProto_1.proto.ClientPayload.fromObject(registerPayload);
120
129
  };
121
- export const configureSuccessfulPairing = (stanza, { advSecretKey, signedIdentityKey, signalIdentities }) => {
130
+ exports.generateRegistrationNode = generateRegistrationNode;
131
+
132
+ const configureSuccessfulPairing = (stanza, { advSecretKey, signedIdentityKey, signalIdentities }) => {
122
133
  const msgId = stanza.attrs.id;
123
- const pairSuccessNode = getBinaryNodeChild(stanza, 'pair-success');
124
- const deviceIdentityNode = getBinaryNodeChild(pairSuccessNode, 'device-identity');
125
- const platformNode = getBinaryNodeChild(pairSuccessNode, 'platform');
126
- const deviceNode = getBinaryNodeChild(pairSuccessNode, 'device');
127
- const businessNode = getBinaryNodeChild(pairSuccessNode, 'biz');
134
+ const pairSuccessNode = (0, WABinary_1.getBinaryNodeChild)(stanza, 'pair-success');
135
+ const deviceIdentityNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'device-identity');
136
+ const platformNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'platform');
137
+ const deviceNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'device');
138
+ const businessNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'biz');
139
+
128
140
  if (!deviceIdentityNode || !deviceNode) {
129
- throw new Boom('Missing device-identity or device in pair success node', { data: stanza });
141
+ throw new boom_1.Boom('Missing device-identity or device in pair success node', { data: stanza });
130
142
  }
143
+
131
144
  const bizName = businessNode?.attrs.name;
132
145
  const jid = deviceNode.attrs.jid;
133
146
  const lid = deviceNode.attrs.lid;
134
- const { details, hmac, accountType } = proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content);
147
+
148
+ const { details, hmac, accountType } = WAProto_1.proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content);
149
+
135
150
  let hmacPrefix = Buffer.from([]);
136
- if (accountType !== undefined && accountType === proto.ADVEncryptionType.HOSTED) {
137
- hmacPrefix = WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX;
151
+ if (accountType !== undefined && accountType === WAProto_1.proto.ADVEncryptionType.HOSTED) {
152
+ hmacPrefix = Buffer.from([0x06, 0x05]);
138
153
  }
139
- const advSign = hmacSign(Buffer.concat([hmacPrefix, details]), Buffer.from(advSecretKey, 'base64'));
154
+
155
+ const advSign = (0, crypto_2.hmacSign)(Buffer.concat([hmacPrefix, details]), Buffer.from(advSecretKey, 'base64'));
140
156
  if (Buffer.compare(hmac, advSign) !== 0) {
141
- throw new Boom('Invalid account signature');
157
+ throw new boom_1.Boom('Invalid account signature');
142
158
  }
143
- const account = proto.ADVSignedDeviceIdentity.decode(details);
159
+
160
+ const account = WAProto_1.proto.ADVSignedDeviceIdentity.decode(details);
144
161
  const { accountSignatureKey, accountSignature, details: deviceDetails } = account;
145
- const deviceIdentity = proto.ADVDeviceIdentity.decode(deviceDetails);
146
- const accountSignaturePrefix = deviceIdentity.deviceType === proto.ADVEncryptionType.HOSTED
147
- ? WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX
148
- : WA_ADV_ACCOUNT_SIG_PREFIX;
162
+
163
+ const deviceIdentity = WAProto_1.proto.ADVDeviceIdentity.decode(deviceDetails);
164
+
165
+ const accountSignaturePrefix = deviceIdentity.deviceType === WAProto_1.proto.ADVEncryptionType.HOSTED
166
+ ? Buffer.from([0x06, 0x05])
167
+ : Buffer.from([0x06, 0x00]);
149
168
  const accountMsg = Buffer.concat([accountSignaturePrefix, deviceDetails, signedIdentityKey.public]);
150
- if (!Curve.verify(accountSignatureKey, accountMsg, accountSignature)) {
151
- throw new Boom('Failed to verify account signature');
169
+
170
+ if (!crypto_2.Curve.verify(accountSignatureKey, accountMsg, accountSignature)) {
171
+ throw new boom_1.Boom('Failed to verify account signature');
152
172
  }
173
+
153
174
  const deviceMsg = Buffer.concat([
154
- WA_ADV_DEVICE_SIG_PREFIX,
175
+ Buffer.from([0x06, 0x01]),
155
176
  deviceDetails,
156
177
  signedIdentityKey.public,
157
178
  accountSignatureKey
158
179
  ]);
159
- account.deviceSignature = Curve.sign(signedIdentityKey.private, deviceMsg);
160
- const identity = createSignalIdentity(lid, accountSignatureKey);
161
- const accountEnc = encodeSignedDeviceIdentity(account, false);
180
+ account.deviceSignature = crypto_2.Curve.sign(signedIdentityKey.private, deviceMsg);
181
+
182
+ const identity = (0, signal_1.createSignalIdentity)(jid, accountSignatureKey);
183
+ const accountEnc = (0, exports.encodeSignedDeviceIdentity)(account, false);
184
+
162
185
  const reply = {
163
186
  tag: 'iq',
164
187
  attrs: {
165
- to: S_WHATSAPP_NET,
188
+ to: WABinary_1.S_WHATSAPP_NET,
166
189
  type: 'result',
167
- id: msgId
190
+ id: msgId,
168
191
  },
169
- content: [
192
+ content: [ // 🆅🆈🆉🅴🅽
170
193
  {
171
194
  tag: 'pair-device-sign',
172
195
  attrs: {},
@@ -180,24 +203,31 @@ export const configureSuccessfulPairing = (stanza, { advSecretKey, signedIdentit
180
203
  }
181
204
  ]
182
205
  };
206
+
183
207
  const authUpdate = {
184
208
  account,
185
209
  me: { id: jid, name: bizName, lid },
186
- signalIdentities: [...(signalIdentities || []), identity],
210
+ signalIdentities: [
211
+ ...(signalIdentities || []),
212
+ identity
213
+ ],
187
214
  platform: platformNode?.attrs.name
188
215
  };
216
+
189
217
  return {
190
218
  creds: authUpdate,
191
219
  reply
192
220
  };
193
221
  };
194
- export const encodeSignedDeviceIdentity = (account, includeSignatureKey) => {
222
+ exports.configureSuccessfulPairing = configureSuccessfulPairing;
223
+
224
+ const encodeSignedDeviceIdentity = (account, includeSignatureKey) => {
195
225
  account = { ...account };
196
- // set to null if we are not to include the signature key
197
- // or if we are including the signature key but it is empty
198
226
  if (!includeSignatureKey || !account.accountSignatureKey?.length) {
199
227
  account.accountSignatureKey = null;
200
228
  }
201
- return proto.ADVSignedDeviceIdentity.encode(account).finish();
229
+ return WAProto_1.proto.ADVSignedDeviceIdentity
230
+ .encode(account)
231
+ .finish();
202
232
  };
203
- //# sourceMappingURL=validate-connection.js.map
233
+ exports.encodeSignedDeviceIdentity = encodeSignedDeviceIdentity;
@@ -1,4 +1,7 @@
1
- export const TAGS = {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TOKEN_MAP = exports.SINGLE_BYTE_TOKENS = exports.DOUBLE_BYTE_TOKENS = exports.TAGS = void 0;
4
+ exports.TAGS = {
2
5
  LIST_EMPTY: 0,
3
6
  DICTIONARY_0: 236,
4
7
  DICTIONARY_1: 237,
@@ -17,7 +20,7 @@ export const TAGS = {
17
20
  NIBBLE_8: 255,
18
21
  PACKED_MAX: 127
19
22
  };
20
- export const DOUBLE_BYTE_TOKENS = [
23
+ exports.DOUBLE_BYTE_TOKENS = [
21
24
  [
22
25
  'read-self',
23
26
  'active',
@@ -1051,7 +1054,7 @@ export const DOUBLE_BYTE_TOKENS = [
1051
1054
  '1961'
1052
1055
  ]
1053
1056
  ];
1054
- export const SINGLE_BYTE_TOKENS = [
1057
+ exports.SINGLE_BYTE_TOKENS = [
1055
1058
  '',
1056
1059
  'xmlstreamstart',
1057
1060
  'xmlstreamend',
@@ -1289,13 +1292,12 @@ export const SINGLE_BYTE_TOKENS = [
1289
1292
  '03',
1290
1293
  'screen_height'
1291
1294
  ];
1292
- export const TOKEN_MAP = {};
1293
- for (const [i, SINGLE_BYTE_TOKEN] of SINGLE_BYTE_TOKENS.entries()) {
1294
- TOKEN_MAP[SINGLE_BYTE_TOKEN] = { index: i };
1295
+ exports.TOKEN_MAP = {};
1296
+ for (const [i, SINGLE_BYTE_TOKEN] of exports.SINGLE_BYTE_TOKENS.entries()) {
1297
+ exports.TOKEN_MAP[SINGLE_BYTE_TOKEN] = { index: i };
1295
1298
  }
1296
- for (const [i, DOUBLE_BYTE_TOKEN] of DOUBLE_BYTE_TOKENS.entries()) {
1299
+ for (const [i, DOUBLE_BYTE_TOKEN] of exports.DOUBLE_BYTE_TOKENS.entries()) {
1297
1300
  for (const [j, element] of DOUBLE_BYTE_TOKEN.entries()) {
1298
- TOKEN_MAP[element] = { dict: i, index: j };
1301
+ exports.TOKEN_MAP[element] = { dict: i, index: j };
1299
1302
  }
1300
1303
  }
1301
- //# sourceMappingURL=constants.js.map
@@ -1,19 +1,55 @@
1
- import { promisify } from 'util';
2
- import { inflate } from 'zlib';
3
- import * as constants from './constants.js';
4
- import { jidEncode, WAJIDDomains } from './jid-utils.js';
5
- const inflatePromise = promisify(inflate);
6
- export const decompressingIfRequired = async (buffer) => {
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.decodeBinaryNode = exports.decodeDecompressedBinaryNode = exports.decompressingIfRequired = void 0;
37
+ const util_1 = require("util");
38
+ const zlib_1 = require("zlib");
39
+ const constants = __importStar(require("./constants"));
40
+ const jid_utils_1 = require("./jid-utils");
41
+ const inflatePromise = (0, util_1.promisify)(zlib_1.inflate);
42
+ const decompressingIfRequired = async (buffer) => {
7
43
  if (2 & buffer.readUInt8()) {
8
44
  buffer = await inflatePromise(buffer.slice(1));
9
45
  }
10
- else {
11
- // nodes with no compression have a 0x00 prefix, we remove that
46
+ else { // nodes with no compression have a 0x00 prefix, we remove that
12
47
  buffer = buffer.slice(1);
13
48
  }
14
49
  return buffer;
15
50
  };
16
- export const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
51
+ exports.decompressingIfRequired = decompressingIfRequired;
52
+ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
17
53
  const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts;
18
54
  const checkEOS = (length) => {
19
55
  if (indexRef.index + length > buffer.length) {
@@ -124,37 +160,7 @@ export const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0
124
160
  const domainType = Number(rawDomainType);
125
161
  const device = readByte();
126
162
  const user = readString(readByte());
127
- let server = 's.whatsapp.net'; // default whatsapp server
128
- if (domainType === WAJIDDomains.LID) {
129
- server = 'lid';
130
- }
131
- else if (domainType === WAJIDDomains.HOSTED) {
132
- server = 'hosted';
133
- }
134
- else if (domainType === WAJIDDomains.HOSTED_LID) {
135
- server = 'hosted.lid';
136
- }
137
- return jidEncode(user, server, device);
138
- };
139
- const readFbJid = () => {
140
- const user = readString(readByte());
141
- const device = readInt(2);
142
- const server = readString(readByte());
143
- return `${user}:${device}@${server}`;
144
- };
145
- const readInteropJid = () => {
146
- const user = readString(readByte());
147
- const device = readInt(2);
148
- const integrator = readInt(2);
149
- let server = 'interop';
150
- const beforeServer = indexRef.index;
151
- try {
152
- server = readString(readByte());
153
- }
154
- catch (err) {
155
- indexRef.index = beforeServer;
156
- }
157
- return `${integrator}-${user}:${device}@${server}`;
163
+ return (0, jid_utils_1.jidEncode)(user, domainType === 0 || domainType === 128 ? 's.whatsapp.net' : 'lid', device);
158
164
  };
159
165
  const readString = (tag) => {
160
166
  if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
@@ -176,10 +182,6 @@ export const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0
176
182
  return readStringFromChars(readInt(4));
177
183
  case TAGS.JID_PAIR:
178
184
  return readJidPair();
179
- case TAGS.FB_JID:
180
- return readFbJid();
181
- case TAGS.INTEROP_JID:
182
- return readInteropJid();
183
185
  case TAGS.AD_JID:
184
186
  return readAdJid();
185
187
  case TAGS.HEX_8:
@@ -193,7 +195,7 @@ export const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0
193
195
  const items = [];
194
196
  const size = readListSize(tag);
195
197
  for (let i = 0; i < size; i++) {
196
- items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef));
198
+ items.push((0, exports.decodeDecompressedBinaryNode)(buffer, opts, indexRef));
197
199
  }
198
200
  return items;
199
201
  };
@@ -255,8 +257,9 @@ export const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0
255
257
  content: data
256
258
  };
257
259
  };
258
- export const decodeBinaryNode = async (buff) => {
259
- const decompBuff = await decompressingIfRequired(buff);
260
- return decodeDecompressedBinaryNode(decompBuff, constants);
260
+ exports.decodeDecompressedBinaryNode = decodeDecompressedBinaryNode;
261
+ const decodeBinaryNode = async (buff) => {
262
+ const decompBuff = await (0, exports.decompressingIfRequired)(buff);
263
+ return (0, exports.decodeDecompressedBinaryNode)(decompBuff, constants);
261
264
  };
262
- //# sourceMappingURL=decode.js.map
265
+ exports.decodeBinaryNode = decodeBinaryNode;
@@ -1,9 +1,46 @@
1
- import * as constants from './constants.js';
2
- import { jidDecode } from './jid-utils.js';
3
- export const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.encodeBinaryNode = void 0;
37
+ const constants = __importStar(require("./constants"));
38
+ const jid_utils_1 = require("./jid-utils");
39
+ const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
4
40
  const encoded = encodeBinaryNodeInner(node, opts, buffer);
5
41
  return Buffer.from(encoded);
6
42
  };
43
+ exports.encodeBinaryNode = encodeBinaryNode;
7
44
  const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
8
45
  const { TAGS, TOKEN_MAP } = opts;
9
46
  const pushByte = (value) => buffer.push(value & 0xff);
@@ -13,15 +50,11 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
13
50
  buffer.push((value >> (curShift * 8)) & 0xff);
14
51
  }
15
52
  };
16
- const pushBytes = (bytes) => {
17
- for (const b of bytes) {
18
- buffer.push(b);
19
- }
20
- };
53
+ const pushBytes = (bytes) => (bytes.forEach(b => buffer.push(b)));
21
54
  const pushInt16 = (value) => {
22
55
  pushBytes([(value >> 8) & 0xff, value & 0xff]);
23
56
  };
24
- const pushInt20 = (value) => pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]);
57
+ const pushInt20 = (value) => (pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]));
25
58
  const writeByteLength = (length) => {
26
59
  if (length >= 4294967296) {
27
60
  throw new Error('string too large to encode: ' + length);
@@ -119,7 +152,8 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
119
152
  if (!str || str.length > TAGS.PACKED_MAX) {
120
153
  return false;
121
154
  }
122
- for (const char of str) {
155
+ for (let i = 0; i < str.length; i++) {
156
+ const char = str[i];
123
157
  const isInNibbleRange = char >= '0' && char <= '9';
124
158
  if (!isInNibbleRange && char !== '-' && char !== '.') {
125
159
  return false;
@@ -131,7 +165,8 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
131
165
  if (!str || str.length > TAGS.PACKED_MAX) {
132
166
  return false;
133
167
  }
134
- for (const char of str) {
168
+ for (let i = 0; i < str.length; i++) {
169
+ const char = str[i];
135
170
  const isInNibbleRange = char >= '0' && char <= '9';
136
171
  if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
137
172
  return false;
@@ -144,10 +179,6 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
144
179
  pushByte(TAGS.LIST_EMPTY);
145
180
  return;
146
181
  }
147
- if (str === '') {
148
- writeStringRaw(str);
149
- return;
150
- }
151
182
  const tokenIndex = TOKEN_MAP[str];
152
183
  if (tokenIndex) {
153
184
  if (typeof tokenIndex.dict === 'number') {
@@ -161,8 +192,8 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
161
192
  else if (isHex(str)) {
162
193
  writePackedBytes(str, 'hex');
163
194
  }
164
- else {
165
- const decodedJid = jidDecode(str);
195
+ else if (str) {
196
+ const decodedJid = (0, jid_utils_1.jidDecode)(str);
166
197
  if (decodedJid) {
167
198
  writeJid(decodedJid);
168
199
  }
@@ -186,7 +217,7 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
186
217
  if (!tag) {
187
218
  throw new Error('Invalid node: tag cannot be undefined');
188
219
  }
189
- const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== 'undefined' && attrs[k] !== null);
220
+ const validAttributes = Object.keys(attrs || {}).filter(k => (typeof attrs[k] !== 'undefined' && attrs[k] !== null));
190
221
  writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));
191
222
  writeString(tag);
192
223
  for (const key of validAttributes) {
@@ -217,4 +248,3 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
217
248
  }
218
249
  return buffer;
219
250
  };
220
- //# sourceMappingURL=encode.js.map