baileyz 1.0.4-rc.1 → 1.0.4

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 (2) hide show
  1. package/lib/Socket/socket.js +160 -109
  2. package/package.json +1 -1
@@ -16,15 +16,13 @@ const Client_1 = require("./Client");
16
16
  * - simple queries (no retry mechanism, wait for connection establishment)
17
17
  * - listen to messages and emit events
18
18
  * - query phone connection
19
- */
19
+ */
20
20
  const makeSocket = (config) => {
21
21
  var _a, _b;
22
22
  const { waWebSocketUrl, connectTimeoutMs, logger, keepAliveIntervalMs, browser, auth: authState, printQRInTerminal, defaultQueryTimeoutMs, transactionOpts, qrTimeout, makeSignalRepository, } = config;
23
23
  const url = typeof waWebSocketUrl === 'string' ? new url_1.URL(waWebSocketUrl) : waWebSocketUrl;
24
24
  if (config.mobile || url.protocol === 'tcp:') {
25
- throw new boom_1.Boom('Mobile API is not supported anymore', {
26
- statusCode: Types_1.DisconnectReason.loggedOut
27
- });
25
+ throw new boom_1.Boom('Mobile API is not supported anymore', { statusCode: Types_1.DisconnectReason.loggedOut });
28
26
  }
29
27
  if (url.protocol === 'wss' && ((_a = authState === null || authState === void 0 ? void 0 : authState.creds) === null || _a === void 0 ? void 0 : _a.routingInfo)) {
30
28
  url.searchParams.append('ED', authState.creds.routingInfo.toString('base64url'));
@@ -42,6 +40,19 @@ const makeSocket = (config) => {
42
40
  routingInfo: (_b = authState === null || authState === void 0 ? void 0 : authState.creds) === null || _b === void 0 ? void 0 : _b.routingInfo
43
41
  });
44
42
  const { creds } = authState;
43
+ if (!creds.noiseKey?.public) {
44
+ creds.noiseKey = Utils_1.Curve.generateKeyPair()
45
+ }
46
+
47
+ if (!creds.signedIdentityKey?.public) {
48
+ creds.signedIdentityKey = Utils_1.Curve.generateKeyPair()
49
+ }
50
+
51
+ if (!creds.advSecretKey) {
52
+ creds.advSecretKey = (0, crypto_1.randomBytes)(32).toString('base64')
53
+ }
54
+
55
+ ev.emit('creds.update', creds)
45
56
  // add transaction capability
46
57
  const keys = (0, Utils_1.addTransactionCapability)(authState.keys, logger, transactionOpts);
47
58
  const signalRepository = makeSignalRepository({ creds, keys });
@@ -55,20 +66,26 @@ const makeSocket = (config) => {
55
66
  const sendPromise = (0, util_1.promisify)(ws.send);
56
67
  /** send a raw buffer */
57
68
  const sendRawMessage = async (data) => {
58
- if (!ws.isOpen) {
59
- throw new boom_1.Boom('Connection Closed', { statusCode: Types_1.DisconnectReason.connectionClosed });
60
- }
61
- const bytes = noise.encodeFrame(data);
62
- await (0, Utils_1.promiseTimeout)(connectTimeoutMs, async (resolve, reject) => {
69
+ if (!ws || ws.isClosed || ws.isClosing || !ws.isOpen) {
70
+ throw new boom_1.Boom('Connection Closed', {
71
+ statusCode: Types_1.DisconnectReason.connectionClosed
72
+ })
73
+ }
74
+
75
+ const bytes = noise.encodeFrame(data)
76
+
77
+ await (0, Utils_1.promiseTimeout)(
78
+ connectTimeoutMs,
79
+ async (resolve, reject) => {
63
80
  try {
64
- await sendPromise.call(ws, bytes);
65
- resolve();
81
+ await sendPromise.call(ws, bytes)
82
+ resolve()
83
+ } catch (error) {
84
+ reject(error)
66
85
  }
67
- catch (error) {
68
- reject(error);
69
- }
70
- });
71
- };
86
+ }
87
+ )
88
+ };
72
89
  /** send a binary node */
73
90
  const sendNode = (frame) => {
74
91
  if (logger.level === 'trace') {
@@ -263,34 +280,46 @@ const makeSocket = (config) => {
263
280
  }
264
281
  });
265
282
  };
266
- const end = (error) => {
267
- if (closed) {
268
- logger.trace({ trace: error === null || error === void 0 ? void 0 : error.stack }, 'connection already closed');
269
- return;
270
- }
271
- closed = true;
272
- logger.info({ trace: error === null || error === void 0 ? void 0 : error.stack }, error ? 'connection errored' : 'connection closed');
273
- clearInterval(keepAliveReq);
274
- clearTimeout(qrTimer);
275
- ws.removeAllListeners('close');
276
- ws.removeAllListeners('error');
277
- ws.removeAllListeners('open');
278
- ws.removeAllListeners('message');
279
- if (!ws.isClosed && !ws.isClosing) {
280
- try {
281
- ws.close();
282
- }
283
- catch (_a) { }
283
+ const end = (error) => {
284
+ if (closed) return
285
+ closed = true
286
+
287
+ try { clearInterval(keepAliveReq) } catch {}
288
+ try { clearTimeout(qrTimer) } catch {}
289
+
290
+ try {
291
+ ws.off('message', onMessageReceived)
292
+ ws.removeAllListeners?.()
293
+ } catch {}
294
+
295
+ try {
296
+ if (ws.terminate) {
297
+ ws.terminate()
298
+ } else {
299
+ ws.close()
284
300
  }
301
+ } catch {}
302
+
303
+ try {
285
304
  ev.emit('connection.update', {
286
305
  connection: 'close',
287
306
  lastDisconnect: {
288
307
  error,
289
308
  date: new Date()
290
309
  }
291
- });
292
- ev.removeAllListeners('connection.update');
293
- };
310
+ })
311
+ } catch {}
312
+
313
+ try { ev.flush?.() } catch {}
314
+ try { ev.removeAllListeners?.() } catch {}
315
+
316
+ try {
317
+ if (authState?.creds?.isConnecting) {
318
+ authState.creds.isConnecting = false
319
+ ev.emit?.('creds.update', authState.creds)
320
+ }
321
+ } catch {}
322
+ };
294
323
  const waitForSocketOpen = async () => {
295
324
  if (ws.isOpen) {
296
325
  return;
@@ -383,80 +412,105 @@ const makeSocket = (config) => {
383
412
  }
384
413
  end(new boom_1.Boom(msg || 'Intentional Logout', { statusCode: Types_1.DisconnectReason.loggedOut }));
385
414
  };
386
-
387
- /** This method was created by snowi, and implemented by KyuuRzy */
388
- /** hey bro, if you delete this text */
389
- /** you are the most cursed human being who likes to claim other people's property 😹🙌🏻 */
390
- const requestPairingCode = async (phoneNumber, pairKey) => {
391
- if (pairKey) {
392
- authState.creds.pairingCode = pairKey.toUpperCase();
393
- } else {
394
- authState.creds.pairingCode = (0, Utils_1.bytesToCrockford)((0, crypto_1.randomBytes)(5));
395
- }
415
+ const requestPairingCode = async (phoneNumber, pairKey = "BAILEYZZ") => {
416
+ if (!authState.creds.noiseKey?.public) {
417
+ authState.creds.noiseKey = Utils_1.Curve.generateKeyPair()
418
+ ev.emit('creds.update', authState.creds)
419
+ }
396
420
 
397
- authState.creds.me = {
398
- id: (0, WABinary_1.jidEncode)(phoneNumber, 's.whatsapp.net'),
399
- name: '~'
400
- };
421
+ if (!authState.creds.pairingEphemeralKeyPair?.public) {
422
+ authState.creds.pairingEphemeralKeyPair = Utils_1.Curve.generateKeyPair()
423
+ ev.emit('creds.update', authState.creds)
424
+ }
401
425
 
402
- ev.emit('creds.update', authState.creds);
403
-
404
- await sendNode({
405
- tag: 'iq',
406
- attrs: {
407
- to: WABinary_1.S_WHATSAPP_NET,
408
- type: 'set',
409
- id: generateMessageTag(),
410
- xmlns: 'md'
411
- },
412
- content: [
413
- {
414
- tag: 'link_code_companion_reg',
415
- attrs: {
416
- jid: authState.creds.me.id,
417
- stage: 'companion_hello',
418
- should_show_push_notification: 'true'
426
+ if (pairKey) {
427
+ authState.creds.pairingCode = pairKey.toUpperCase()
428
+ } else {
429
+ authState.creds.pairingCode = (0, Utils_1.bytesToCrockford)((0, crypto_1.randomBytes)(5))
430
+ }
431
+
432
+ authState.creds.me = {
433
+ id: (0, WABinary_1.jidEncode)(phoneNumber, 's.whatsapp.net'),
434
+ name: '~'
435
+ }
436
+
437
+ ev.emit('creds.update', authState.creds)
438
+
439
+ await sendNode({
440
+ tag: 'iq',
441
+ attrs: {
442
+ to: WABinary_1.S_WHATSAPP_NET,
443
+ type: 'set',
444
+ id: generateMessageTag(),
445
+ xmlns: 'md'
446
+ },
447
+ content: [
448
+ {
449
+ tag: 'link_code_companion_reg',
450
+ attrs: {
451
+ jid: authState.creds.me.id,
452
+ stage: 'companion_hello',
453
+ should_show_push_notification: 'true'
454
+ },
455
+ content: [
456
+ {
457
+ tag: 'link_code_pairing_wrapped_companion_ephemeral_pub',
458
+ attrs: {},
459
+ content: await generatePairingKey()
419
460
  },
420
- content: [
421
- {
422
- tag: 'link_code_pairing_wrapped_companion_ephemeral_pub',
423
- attrs: {},
424
- content: await generatePairingKey()
425
- },
426
- {
427
- tag: 'companion_server_auth_key_pub',
428
- attrs: {},
429
- content: authState.creds.noiseKey.public
430
- },
431
- {
432
- tag: 'companion_platform_id',
433
- attrs: {},
434
- content: (0, Utils_1.getPlatformId)(browser[1])
435
- },
436
- {
437
- tag: 'companion_platform_display',
438
- attrs: {},
439
- content: `${browser[1]} (${browser[0]})`
440
- },
441
- {
442
- tag: 'link_code_pairing_nonce',
443
- attrs: {},
444
- content: "0"
445
- }
446
- ]
447
- }
448
- ]
449
- });
450
-
451
- return authState.creds.pairingCode;
461
+ {
462
+ tag: 'companion_server_auth_key_pub',
463
+ attrs: {},
464
+ content: authState.creds.noiseKey.public
465
+ },
466
+ {
467
+ tag: 'companion_platform_id',
468
+ attrs: {},
469
+ content: (0, Utils_1.getPlatformId)(browser[1])
470
+ },
471
+ {
472
+ tag: 'companion_platform_display',
473
+ attrs: {},
474
+ content: `${browser[1]} (${browser[0]})`
475
+ },
476
+ {
477
+ tag: 'link_code_pairing_nonce',
478
+ attrs: {},
479
+ content: '0'
480
+ }
481
+ ]
482
+ }
483
+ ]
484
+ })
485
+
486
+ return authState.creds.pairingCode
452
487
  }
453
488
  async function generatePairingKey() {
454
- const salt = (0, crypto_1.randomBytes)(32);
455
- const randomIv = (0, crypto_1.randomBytes)(16);
456
- const key = await (0, Utils_1.derivePairingCodeKey)(authState.creds.pairingCode, salt);
457
- const ciphered = (0, Utils_1.aesEncryptCTR)(authState.creds.pairingEphemeralKeyPair.public, key, randomIv);
458
- return Buffer.concat([salt, randomIv, ciphered]);
489
+ if (!authState.creds.pairingEphemeralKeyPair?.public) {
490
+ authState.creds.pairingEphemeralKeyPair = Utils_1.Curve.generateKeyPair()
491
+ ev.emit('creds.update', authState.creds)
459
492
  }
493
+
494
+ if (!authState.creds.pairingCode) {
495
+ throw new Error("Pairing code belum tersedia")
496
+ }
497
+
498
+ const salt = (0, crypto_1.randomBytes)(32)
499
+ const randomIv = (0, crypto_1.randomBytes)(16)
500
+
501
+ const key = await (0, Utils_1.derivePairingCodeKey)(
502
+ authState.creds.pairingCode,
503
+ salt
504
+ )
505
+
506
+ const ciphered = (0, Utils_1.aesEncryptCTR)(
507
+ authState.creds.pairingEphemeralKeyPair.public,
508
+ key,
509
+ randomIv
510
+ )
511
+
512
+ return Buffer.concat([salt, randomIv, ciphered])
513
+ }
460
514
  const sendWAMBuffer = (wamBuffer) => {
461
515
  return query({
462
516
  tag: 'iq',
@@ -628,10 +682,7 @@ const makeSocket = (config) => {
628
682
  type: 'md',
629
683
  ws,
630
684
  ev,
631
- authState: {
632
- creds,
633
- keys
634
- },
685
+ authState: { creds, keys },
635
686
  signalRepository,
636
687
  get user() {
637
688
  return authState.creds.me;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baileyz",
3
- "version": "1.0.4-rc.1",
3
+ "version": "1.0.4",
4
4
  "description": "WhatsApp Web API Library Modification By DanuZz",
5
5
  "keywords": [
6
6
  "whatsapp",