@rexxhayanasi/elaina-baileys 1.1.0-DEV.1 → 1.1.0-DEV.2

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.
@@ -412,46 +412,30 @@ const end = (error) => {
412
412
  }
413
413
  end(new boom_1.Boom(msg || 'Intentional Logout', { statusCode: Types_1.DisconnectReason.loggedOut }));
414
414
  };
415
-
416
- const getPairingJid = () => {
417
- if (authState.creds?.me?.id) {
418
- return authState.creds.me.id
419
- }
420
-
421
- if (!authState.creds?.pairingPhone) {
422
- throw new Error('pairingPhone belum tersedia')
423
- }
424
-
425
- const phone = authState.creds.pairingPhone.replace(/\D/g, '')
426
- return `${phone}@s.whatsapp.net`
427
- }
428
415
  const requestPairingCode = async (phoneNumber, pairKey = "ELAINAMD") => {
429
- // FIX: normalisasi nomor (WA v6 wajib)
430
- authState.creds.pairingPhone = phoneNumber.replace(/\D/g, '')
431
- ev.emit('creds.update', authState.creds)
432
-
433
416
  if (!authState.creds.noiseKey?.public) {
434
417
  authState.creds.noiseKey = Utils_1.Curve.generateKeyPair()
435
418
  ev.emit('creds.update', authState.creds)
436
419
  }
437
420
 
438
- // FIX: pairingEphemeralKeyPair HARUS STABIL
439
- if (!authState.creds.pairingEphemeralKeyPair) {
421
+ if (!authState.creds.pairingEphemeralKeyPair?.public) {
440
422
  authState.creds.pairingEphemeralKeyPair = Utils_1.Curve.generateKeyPair()
441
423
  ev.emit('creds.update', authState.creds)
442
424
  }
443
425
 
444
- // FIX: pairing code konsisten
445
- authState.creds.pairingCode = pairKey
446
- ? pairKey.toUpperCase()
447
- : (0, Utils_1.bytesToCrockford)((0, crypto_1.randomBytes)(5))
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
+ }
448
431
 
449
- // FIX: salt pairing harus stabil
450
- if (!authState.creds.pairingSalt) {
451
- authState.creds.pairingSalt = (0, crypto_1.randomBytes)(32)
452
- ev.emit('creds.update', authState.creds)
432
+ authState.creds.me = {
433
+ id: (0, WABinary_1.jidEncode)(phoneNumber, 's.whatsapp.net'),
434
+ name: '~'
453
435
  }
454
436
 
437
+ ev.emit('creds.update', authState.creds)
438
+
455
439
  await sendNode({
456
440
  tag: 'iq',
457
441
  attrs: {
@@ -464,8 +448,7 @@ const end = (error) => {
464
448
  {
465
449
  tag: 'link_code_companion_reg',
466
450
  attrs: {
467
- // FIX PALING PENTING (Baileys v6)
468
- jid: getPairingJid(),
451
+ jid: authState.creds.me.id,
469
452
  stage: 'companion_hello',
470
453
  should_show_push_notification: 'true'
471
454
  },
@@ -493,7 +476,7 @@ const end = (error) => {
493
476
  {
494
477
  tag: 'link_code_pairing_nonce',
495
478
  attrs: {},
496
- content: Buffer.from([0])
479
+ content: '0'
497
480
  }
498
481
  ]
499
482
  }
@@ -501,20 +484,19 @@ const end = (error) => {
501
484
  })
502
485
 
503
486
  return authState.creds.pairingCode
504
- }
505
-
487
+ }
506
488
  async function generatePairingKey() {
507
489
  if (!authState.creds.pairingEphemeralKeyPair?.public) {
508
- throw new Error('pairingEphemeralKeyPair tidak tersedia')
490
+ authState.creds.pairingEphemeralKeyPair = Utils_1.Curve.generateKeyPair()
491
+ ev.emit('creds.update', authState.creds)
509
492
  }
510
493
 
511
494
  if (!authState.creds.pairingCode) {
512
- throw new Error('pairingCode belum tersedia')
495
+ throw new Error("Pairing code belum tersedia")
513
496
  }
514
497
 
515
- // FIX: pakai salt yang sama
516
- const salt = authState.creds.pairingSalt
517
- const iv = (0, crypto_1.randomBytes)(16)
498
+ const salt = (0, crypto_1.randomBytes)(32)
499
+ const randomIv = (0, crypto_1.randomBytes)(16)
518
500
 
519
501
  const key = await (0, Utils_1.derivePairingCodeKey)(
520
502
  authState.creds.pairingCode,
@@ -524,10 +506,10 @@ const end = (error) => {
524
506
  const ciphered = (0, Utils_1.aesEncryptCTR)(
525
507
  authState.creds.pairingEphemeralKeyPair.public,
526
508
  key,
527
- iv
509
+ randomIv
528
510
  )
529
511
 
530
- return Buffer.concat([salt, iv, ciphered])
512
+ return Buffer.concat([salt, randomIv, ciphered])
531
513
  }
532
514
  const sendWAMBuffer = (wamBuffer) => {
533
515
  return query({
@@ -548,19 +530,14 @@ const end = (error) => {
548
530
  };
549
531
  ws.on('message', onMessageReceived);
550
532
  ws.on('open', async () => {
551
- try {
552
- if (!authState.creds.isConnecting) {
553
- authState.creds.isConnecting = true;
554
- ev.emit('creds.update', authState.creds);
533
+ try {
534
+ await validateConnection();
555
535
  }
556
-
557
- await validateConnection();
558
- }
559
- catch (err) {
560
- logger.error({ err }, 'error in validating connection');
561
- end(err);
562
- }
563
- });
536
+ catch (err) {
537
+ logger.error({ err }, 'error in validating connection');
538
+ end(err);
539
+ }
540
+ });
564
541
  ws.on('error', mapWebSocketError(end));
565
542
  ws.on('close', () => end(new boom_1.Boom('Connection Terminated', { statusCode: Types_1.DisconnectReason.connectionClosed })));
566
543
  // the server terminated the connection
@@ -602,39 +579,34 @@ const end = (error) => {
602
579
  // device paired for the first time
603
580
  // if device pairs successfully, the server asks to restart the connection
604
581
  ws.on('CB:iq,,pair-success', async (stanza) => {
605
- try {
606
- const { reply, creds: updatedCreds } =
607
- (0, Utils_1.configureSuccessfulPairing)(stanza, creds)
608
-
609
- // FIX: reset state pairing
610
- delete updatedCreds.pairingCode
611
- delete updatedCreds.pairingPhone
612
- delete updatedCreds.pairingSalt
613
- delete updatedCreds.pairingEphemeralKeyPair
614
- updatedCreds.isConnecting = false
615
-
616
- ev.emit('creds.update', updatedCreds)
617
- ev.emit('connection.update', { isNewLogin: true, qr: undefined })
618
-
619
- await sendNode(reply)
620
- } catch (err) {
621
- end(err)
622
- }
623
- });
582
+ logger.debug('pair success recv');
583
+ try {
584
+ const { reply, creds: updatedCreds } = (0, Utils_1.configureSuccessfulPairing)(stanza, creds);
585
+ logger.info({ me: updatedCreds.me, platform: updatedCreds.platform }, 'pairing configured successfully, expect to restart the connection...');
586
+ ev.emit('creds.update', updatedCreds);
587
+ ev.emit('connection.update', { isNewLogin: true, qr: undefined });
588
+ await sendNode(reply);
589
+ }
590
+ catch (error) {
591
+ logger.info({ trace: error.stack }, 'error in pairing');
592
+ end(error);
593
+ }
594
+ });
624
595
  // login complete
625
596
  ws.on('CB:success', async (node) => {
626
- try {
627
- await uploadPreKeysToServerIfRequired()
628
- await sendPassiveIq('active')
629
-
630
- authState.creds.isConnecting = false
631
- ev.emit('creds.update', authState.creds)
632
-
633
- ev.emit('connection.update', { connection: 'open' })
634
- } catch (err) {
635
- end(err)
636
- }
637
- });
597
+ try {
598
+ await uploadPreKeysToServerIfRequired();
599
+ await sendPassiveIq('active');
600
+ logger.info('opened connection to WA');
601
+ clearTimeout(qrTimer); // will never happen in all likelyhood -- but just in case WA sends success on first try
602
+ ev.emit('creds.update', { me: { ...authState.creds.me, lid: node.attrs.lid } });
603
+ ev.emit('connection.update', { connection: 'open' });
604
+ }
605
+ catch (err) {
606
+ logger.error({ err }, 'error opening connection');
607
+ end(err);
608
+ }
609
+ });
638
610
  ws.on('CB:stream:error', (node) => {
639
611
  logger.error({ node }, 'stream errored out');
640
612
  const { reason, statusCode } = (0, Utils_1.getErrorCodeFromStreamError)(node);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rexxhayanasi/elaina-baileys",
3
- "version": "1.1.0-DEV.1",
3
+ "version": "1.1.0-DEV.2",
4
4
  "description": "Custom Baileys WhatsApp API",
5
5
  "keywords": [
6
6
  "baileys",