@pontalabs/baileys 1.0.10 → 1.1.0

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 (119) hide show
  1. package/README.md +96 -85
  2. package/lib/Defaults/connection.js +39 -0
  3. package/lib/Defaults/constants.js +50 -0
  4. package/lib/Defaults/history.js +13 -0
  5. package/lib/Defaults/index.js +36 -151
  6. package/lib/Defaults/media.js +43 -0
  7. package/lib/Defaults/phonenumber-mcc.json +221 -221
  8. package/lib/Defaults/prefix.js +12 -0
  9. package/lib/Defaults/vialeys-version.json +7 -0
  10. package/lib/Signal/Group/ciphertext-message.js +10 -15
  11. package/lib/Signal/Group/group-session-builder.js +34 -60
  12. package/lib/Signal/Group/group_cipher.js +69 -76
  13. package/lib/Signal/Group/index.js +41 -60
  14. package/lib/Signal/Group/keyhelper.js +12 -57
  15. package/lib/Signal/Group/sender-chain-key.js +20 -33
  16. package/lib/Signal/Group/sender-key-distribution-message.js +43 -48
  17. package/lib/Signal/Group/sender-key-message.js +58 -53
  18. package/lib/Signal/Group/sender-key-name.js +21 -31
  19. package/lib/Signal/Group/sender-key-record.js +21 -34
  20. package/lib/Signal/Group/sender-key-state.js +59 -106
  21. package/lib/Signal/Group/sender-message-key.js +16 -21
  22. package/lib/Signal/libsignal.js +282 -203
  23. package/lib/Signal/lid-mapping.js +164 -184
  24. package/lib/Socket/Client/index.js +31 -22
  25. package/lib/Socket/Client/types.js +9 -15
  26. package/lib/Socket/Client/websocket.js +42 -37
  27. package/lib/Socket/business.js +212 -336
  28. package/lib/Socket/chats.js +696 -940
  29. package/lib/Socket/community.js +454 -369
  30. package/lib/Socket/groups.js +278 -280
  31. package/lib/Socket/index.js +9 -21
  32. package/lib/Socket/messages-recv.js +1027 -1570
  33. package/lib/Socket/messages-send.js +1180 -1136
  34. package/lib/Socket/mex.js +59 -0
  35. package/lib/Socket/newsletter.js +193 -251
  36. package/lib/Socket/socket.js +758 -898
  37. package/lib/Store/index.js +35 -24
  38. package/lib/Store/make-cache-manager-store.js +53 -66
  39. package/lib/Store/make-in-memory-store.js +319 -325
  40. package/lib/Store/make-ordered-dictionary.js +49 -57
  41. package/lib/Store/object-repository.js +10 -15
  42. package/lib/Types/Auth.js +2 -3
  43. package/lib/Types/Bussines.js +2 -3
  44. package/lib/Types/Call.js +2 -3
  45. package/lib/Types/Chat.js +10 -9
  46. package/lib/Types/Contact.js +2 -3
  47. package/lib/Types/Events.js +2 -3
  48. package/lib/Types/GroupMetadata.js +2 -3
  49. package/lib/Types/Label.js +6 -12
  50. package/lib/Types/LabelAssociation.js +4 -13
  51. package/lib/Types/Message.js +10 -18
  52. package/lib/Types/MexUpdates.js +15 -18
  53. package/lib/Types/Newsletter.js +26 -34
  54. package/lib/Types/Product.js +2 -3
  55. package/lib/Types/Signal.js +2 -3
  56. package/lib/Types/Socket.js +2 -3
  57. package/lib/Types/State.js +8 -13
  58. package/lib/Types/USync.js +2 -3
  59. package/lib/Types/index.js +49 -40
  60. package/lib/Utils/auth-utils.js +190 -495
  61. package/lib/Utils/browser-utils.js +29 -0
  62. package/lib/Utils/business.js +177 -199
  63. package/lib/Utils/chat-utils.js +711 -624
  64. package/lib/Utils/crypto.js +116 -161
  65. package/lib/Utils/decode-wa-message.js +225 -321
  66. package/lib/Utils/event-buffer.js +363 -405
  67. package/lib/Utils/generics.js +318 -472
  68. package/lib/Utils/history.js +72 -76
  69. package/lib/Utils/index.js +49 -41
  70. package/lib/Utils/link-preview.js +59 -103
  71. package/lib/Utils/logger.js +4 -7
  72. package/lib/Utils/lt-hash.js +29 -42
  73. package/lib/Utils/make-mutex.js +26 -42
  74. package/lib/Utils/message-retry-manager.js +134 -160
  75. package/lib/Utils/messages-media.js +665 -731
  76. package/lib/Utils/messages.js +518 -1
  77. package/lib/Utils/noise-handler.js +127 -125
  78. package/lib/Utils/pre-key-manager.js +86 -0
  79. package/lib/Utils/process-message.js +739 -351
  80. package/lib/Utils/signal.js +146 -129
  81. package/lib/Utils/use-multi-file-auth-state.js +68 -135
  82. package/lib/Utils/validate-connection.js +183 -142
  83. package/lib/Utils/vialeys-event-stream.js +41 -0
  84. package/lib/WABinary/constants.js +1281 -1289
  85. package/lib/WABinary/decode.js +145 -200
  86. package/lib/WABinary/encode.js +149 -202
  87. package/lib/WABinary/generic-utils.js +79 -97
  88. package/lib/WABinary/index.js +36 -25
  89. package/lib/WABinary/jid-utils.js +91 -123
  90. package/lib/WABinary/types.js +2 -3
  91. package/lib/WAM/BinaryInfo.js +8 -13
  92. package/lib/WAM/constants.js +16584 -11464
  93. package/lib/WAM/encode.js +117 -133
  94. package/lib/WAM/index.js +34 -23
  95. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +62 -69
  96. package/lib/WAUSync/Protocols/USyncContactProtocol.js +11 -25
  97. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +32 -44
  98. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +13 -23
  99. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +24 -38
  100. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +17 -31
  101. package/lib/WAUSync/Protocols/index.js +37 -26
  102. package/lib/WAUSync/USyncQuery.js +66 -66
  103. package/lib/WAUSync/USyncUser.js +13 -18
  104. package/lib/WAUSync/index.js +34 -23
  105. package/lib/index.js +95 -28
  106. package/package.json +4 -3
  107. package/lib/Defaults/baileys-version.json +0 -3
  108. package/lib/Signal/Group/queue-job.d.ts +0 -1
  109. package/lib/Signal/Group/queue-job.js +0 -57
  110. package/lib/Socket/usync.d.ts +0 -37
  111. package/lib/Socket/usync.js +0 -83
  112. package/lib/Utils/baileys-event-stream.d.ts +0 -18
  113. package/lib/Utils/baileys-event-stream.js +0 -70
  114. package/lib/Utils/signal-housekeeping.d.ts +0 -19
  115. package/lib/Utils/signal-housekeeping.js +0 -73
  116. package/lib/Utils/use-mongo-file-auth-state.d.ts +0 -6
  117. package/lib/Utils/use-mongo-file-auth-state.js +0 -84
  118. package/lib/Utils/use-single-file-auth-state.d.ts +0 -13
  119. package/lib/Utils/use-single-file-auth-state.js +0 -80
@@ -1,288 +1,233 @@
1
- "use strict"
2
-
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k
5
- var desc = Object.getOwnPropertyDescriptor(m, k)
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k] } }
8
- }
9
- Object.defineProperty(o, k2, desc)
10
- }) : (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k
12
- o[k2] = m[k]
13
- }))
14
-
15
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16
- Object.defineProperty(o, "default", { enumerable: true, value: v })
17
- }) : function(o, v) {
18
- o["default"] = v
19
- })
20
-
21
- var __importStar = (this && this.__importStar) || function (mod) {
22
- if (mod && mod.__esModule) return mod
23
- var result = {}
24
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k)
25
- __setModuleDefault(result, mod)
26
- return result
27
- }
28
-
29
- Object.defineProperty(exports, "__esModule", { value: true })
30
-
31
- const util_1 = require("util")
32
- const zlib_1 = require("zlib")
33
- const constants = __importStar(require("./constants"))
34
- const jid_utils_1 = require("./jid-utils")
35
- const inflatePromise = util_1.promisify(zlib_1.inflate)
36
-
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const { promisify: promisify } = require("util");
4
+ const { inflate: inflate } = require("zlib");
5
+ const { jidEncode: jidEncode, WAJIDDomains: WAJIDDomains } = require("./jid-utils");
6
+ const constants = require("./constants");
7
+ const inflatePromise = promisify(inflate);
37
8
  const decompressingIfRequired = async (buffer) => {
38
9
  if (2 & buffer.readUInt8()) {
39
- buffer = await inflatePromise(buffer.slice(1))
40
- }
41
- else { // nodes with no compression have a 0x00 prefix, we remove that
42
- buffer = buffer.slice(1)
10
+ buffer = await inflatePromise(buffer.slice(1));
11
+ } else {
12
+ buffer = buffer.slice(1);
43
13
  }
44
- return buffer
45
- }
46
-
14
+ return buffer;
15
+ };
47
16
  const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
48
- const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts
17
+ const {
18
+ DOUBLE_BYTE_TOKENS: DOUBLE_BYTE_TOKENS,
19
+ SINGLE_BYTE_TOKENS: SINGLE_BYTE_TOKENS,
20
+ TAGS: TAGS,
21
+ } = opts;
49
22
  const checkEOS = (length) => {
50
23
  if (indexRef.index + length > buffer.length) {
51
- throw new Error('end of stream')
24
+ throw new Error("end of stream");
52
25
  }
53
- }
54
-
26
+ };
55
27
  const next = () => {
56
- const value = buffer[indexRef.index]
57
- indexRef.index += 1
58
- return value
59
- }
60
-
28
+ const value = buffer[indexRef.index];
29
+ indexRef.index += 1;
30
+ return value;
31
+ };
61
32
  const readByte = () => {
62
- checkEOS(1)
63
- return next()
64
- }
65
-
33
+ checkEOS(1);
34
+ return next();
35
+ };
66
36
  const readBytes = (n) => {
67
- checkEOS(n)
68
- const value = buffer.slice(indexRef.index, indexRef.index + n)
69
- indexRef.index += n
70
- return value
71
- }
72
-
73
- const readStringFromChars = (length) => {
74
- return readBytes(length).toString('utf-8')
75
- }
76
-
37
+ checkEOS(n);
38
+ const value = buffer.slice(indexRef.index, indexRef.index + n);
39
+ indexRef.index += n;
40
+ return value;
41
+ };
42
+ const readStringFromChars = (length) => readBytes(length).toString("utf-8");
77
43
  const readInt = (n, littleEndian = false) => {
78
- checkEOS(n)
79
- let val = 0
44
+ checkEOS(n);
45
+ let val = 0;
80
46
  for (let i = 0; i < n; i++) {
81
- const shift = littleEndian ? i : n - 1 - i
82
- val |= next() << (shift * 8)
47
+ const shift = littleEndian ? i : n - 1 - i;
48
+ val |= next() << (shift * 8);
83
49
  }
84
- return val
85
- }
86
-
50
+ return val;
51
+ };
87
52
  const readInt20 = () => {
88
- checkEOS(3)
89
- return ((next() & 15) << 16) + (next() << 8) + next()
90
- }
91
-
53
+ checkEOS(3);
54
+ return ((next() & 15) << 16) + (next() << 8) + next();
55
+ };
92
56
  const unpackHex = (value) => {
93
57
  if (value >= 0 && value < 16) {
94
- return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10
58
+ return value < 10 ? "0".charCodeAt(0) + value : "A".charCodeAt(0) + value - 10;
95
59
  }
96
- throw new Error('invalid hex: ' + value)
97
- }
98
-
60
+ throw new Error("invalid hex: " + value);
61
+ };
99
62
  const unpackNibble = (value) => {
100
63
  if (value >= 0 && value <= 9) {
101
- return '0'.charCodeAt(0) + value
64
+ return "0".charCodeAt(0) + value;
102
65
  }
103
66
  switch (value) {
104
67
  case 10:
105
- return '-'.charCodeAt(0)
68
+ return "-".charCodeAt(0);
106
69
  case 11:
107
- return '.'.charCodeAt(0)
70
+ return ".".charCodeAt(0);
108
71
  case 15:
109
- return '\0'.charCodeAt(0)
72
+ return "\0".charCodeAt(0);
110
73
  default:
111
- throw new Error('invalid nibble: ' + value)
74
+ throw new Error("invalid nibble: " + value);
112
75
  }
113
- }
114
-
76
+ };
115
77
  const unpackByte = (tag, value) => {
116
78
  if (tag === TAGS.NIBBLE_8) {
117
- return unpackNibble(value)
118
- }
119
- else if (tag === TAGS.HEX_8) {
120
- return unpackHex(value)
79
+ return unpackNibble(value);
80
+ } else if (tag === TAGS.HEX_8) {
81
+ return unpackHex(value);
82
+ } else {
83
+ throw new Error("unknown tag: " + tag);
121
84
  }
122
- else {
123
- throw new Error('unknown tag: ' + tag)
124
- }
125
- }
126
-
85
+ };
127
86
  const readPacked8 = (tag) => {
128
- const startByte = readByte()
129
- let value = ''
87
+ const startByte = readByte();
88
+ let value = "";
130
89
  for (let i = 0; i < (startByte & 127); i++) {
131
- const curByte = readByte()
132
- value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4))
133
- value += String.fromCharCode(unpackByte(tag, curByte & 0x0f))
90
+ const curByte = readByte();
91
+ value += String.fromCharCode(unpackByte(tag, (curByte & 240) >> 4));
92
+ value += String.fromCharCode(unpackByte(tag, curByte & 15));
134
93
  }
135
94
  if (startByte >> 7 !== 0) {
136
- value = value.slice(0, -1)
95
+ value = value.slice(0, -1);
137
96
  }
138
- return value
139
- }
140
-
141
- const isListTag = (tag) => {
142
- return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16
143
- }
144
-
97
+ return value;
98
+ };
99
+ const isListTag = (tag) =>
100
+ tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16;
145
101
  const readListSize = (tag) => {
146
102
  switch (tag) {
147
103
  case TAGS.LIST_EMPTY:
148
- return 0
104
+ return 0;
149
105
  case TAGS.LIST_8:
150
- return readByte()
106
+ return readByte();
151
107
  case TAGS.LIST_16:
152
- return readInt(2)
108
+ return readInt(2);
153
109
  default:
154
- throw new Error('invalid tag for list size: ' + tag)
110
+ throw new Error("invalid tag for list size: " + tag);
155
111
  }
156
- }
157
-
112
+ };
158
113
  const readJidPair = () => {
159
- const i = readString(readByte())
160
- const j = readString(readByte())
114
+ const i = readString(readByte());
115
+ const j = readString(readByte());
161
116
  if (j) {
162
- return (i || '') + '@' + j
117
+ return (i || "") + "@" + j;
163
118
  }
164
- throw new Error('invalid jid pair: ' + i + ', ' + j)
165
- }
166
-
119
+ throw new Error("invalid jid pair: " + i + ", " + j);
120
+ };
167
121
  const readAdJid = () => {
168
- const agent = readByte()
169
- const device = readByte()
170
- const user = readString(readByte())
171
- return jid_utils_1.jidEncode(user, agent === 0 ? 's.whatsapp.net' : 'lid', device)
172
- }
173
-
122
+ const rawDomainType = readByte();
123
+ const domainType = Number(rawDomainType);
124
+ const device = readByte();
125
+ const user = readString(readByte());
126
+ let server = "s.whatsapp.net";
127
+ if (domainType === WAJIDDomains.LID) {
128
+ server = "lid";
129
+ } else if (domainType === WAJIDDomains.HOSTED) {
130
+ server = "hosted";
131
+ } else if (domainType === WAJIDDomains.HOSTED_LID) {
132
+ server = "hosted.lid";
133
+ }
134
+ return jidEncode(user, server, device);
135
+ };
174
136
  const readString = (tag) => {
175
137
  if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
176
- return SINGLE_BYTE_TOKENS[tag] || ''
138
+ return SINGLE_BYTE_TOKENS[tag] || "";
177
139
  }
178
140
  switch (tag) {
179
141
  case TAGS.DICTIONARY_0:
180
142
  case TAGS.DICTIONARY_1:
181
143
  case TAGS.DICTIONARY_2:
182
144
  case TAGS.DICTIONARY_3:
183
- return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte())
145
+ return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte());
184
146
  case TAGS.LIST_EMPTY:
185
- return ''
147
+ return "";
186
148
  case TAGS.BINARY_8:
187
- return readStringFromChars(readByte())
149
+ return readStringFromChars(readByte());
188
150
  case TAGS.BINARY_20:
189
- return readStringFromChars(readInt20())
151
+ return readStringFromChars(readInt20());
190
152
  case TAGS.BINARY_32:
191
- return readStringFromChars(readInt(4))
153
+ return readStringFromChars(readInt(4));
192
154
  case TAGS.JID_PAIR:
193
- return readJidPair()
155
+ return readJidPair();
194
156
  case TAGS.AD_JID:
195
- return readAdJid()
157
+ return readAdJid();
196
158
  case TAGS.HEX_8:
197
159
  case TAGS.NIBBLE_8:
198
- return readPacked8(tag)
160
+ return readPacked8(tag);
199
161
  default:
200
- throw new Error('invalid string with tag: ' + tag)
162
+ throw new Error("invalid string with tag: " + tag);
201
163
  }
202
- }
203
-
164
+ };
204
165
  const readList = (tag) => {
205
- const items = []
206
- const size = readListSize(tag)
166
+ const items = [];
167
+ const size = readListSize(tag);
207
168
  for (let i = 0; i < size; i++) {
208
- items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef))
169
+ items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef));
209
170
  }
210
- return items
211
- }
212
-
171
+ return items;
172
+ };
213
173
  const getTokenDouble = (index1, index2) => {
214
- const dict = DOUBLE_BYTE_TOKENS[index1]
174
+ const dict = DOUBLE_BYTE_TOKENS[index1];
215
175
  if (!dict) {
216
- throw new Error(`Invalid double token dict (${index1})`)
176
+ throw new Error(`Invalid double token dict (${index1})`);
217
177
  }
218
- const value = dict[index2]
219
- if (typeof value === 'undefined') {
220
- throw new Error(`Invalid double token (${index2})`)
178
+ const value = dict[index2];
179
+ if (typeof value === "undefined") {
180
+ throw new Error(`Invalid double token (${index2})`);
221
181
  }
222
- return value
223
- }
224
-
225
- const listSize = readListSize(readByte())
226
- const header = readString(readByte())
227
-
182
+ return value;
183
+ };
184
+ const listSize = readListSize(readByte());
185
+ const header = readString(readByte());
228
186
  if (!listSize || !header.length) {
229
- throw new Error('invalid node')
187
+ throw new Error("invalid node");
230
188
  }
231
-
232
- const attrs = {}
233
-
234
- let data
235
-
189
+ const attrs = {};
190
+ let data;
236
191
  if (listSize === 0 || !header) {
237
- throw new Error('invalid node')
192
+ throw new Error("invalid node");
238
193
  }
239
-
240
- // read the attributes in
241
- const attributesLength = (listSize - 1) >> 1
194
+ const attributesLength = (listSize - 1) >> 1;
242
195
  for (let i = 0; i < attributesLength; i++) {
243
- const key = readString(readByte())
244
- const value = readString(readByte())
245
- attrs[key] = value
196
+ const key = readString(readByte());
197
+ const value = readString(readByte());
198
+ attrs[key] = value;
246
199
  }
247
-
248
200
  if (listSize % 2 === 0) {
249
- const tag = readByte()
201
+ const tag = readByte();
250
202
  if (isListTag(tag)) {
251
- data = readList(tag)
252
- }
253
- else {
254
- let decoded
203
+ data = readList(tag);
204
+ } else {
205
+ let decoded;
255
206
  switch (tag) {
256
207
  case TAGS.BINARY_8:
257
- decoded = readBytes(readByte())
258
- break
208
+ decoded = readBytes(readByte());
209
+ break;
259
210
  case TAGS.BINARY_20:
260
- decoded = readBytes(readInt20())
261
- break
211
+ decoded = readBytes(readInt20());
212
+ break;
262
213
  case TAGS.BINARY_32:
263
- decoded = readBytes(readInt(4))
264
- break
214
+ decoded = readBytes(readInt(4));
215
+ break;
265
216
  default:
266
- decoded = readString(tag)
267
- break
217
+ decoded = readString(tag);
218
+ break;
268
219
  }
269
- data = decoded
220
+ data = decoded;
270
221
  }
271
222
  }
272
- return {
273
- tag: header,
274
- attrs,
275
- content: data
276
- }
277
- }
278
-
223
+ return { tag: header, attrs: attrs, content: data };
224
+ };
279
225
  const decodeBinaryNode = async (buff) => {
280
- const decompBuff = await decompressingIfRequired(buff)
281
- return decodeDecompressedBinaryNode(decompBuff, constants)
282
- }
283
-
226
+ const decompBuff = await decompressingIfRequired(buff);
227
+ return decodeDecompressedBinaryNode(decompBuff, constants);
228
+ };
284
229
  module.exports = {
285
- decompressingIfRequired,
286
- decodeDecompressedBinaryNode,
287
- decodeBinaryNode
288
- }
230
+ decompressingIfRequired: decompressingIfRequired,
231
+ decodeDecompressedBinaryNode: decodeDecompressedBinaryNode,
232
+ decodeBinaryNode: decodeBinaryNode,
233
+ };