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.
- package/lib/Socket/socket.js +160 -109
- package/package.json +1 -1
package/lib/Socket/socket.js
CHANGED
|
@@ -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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
68
|
-
|
|
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
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
ws.removeAllListeners(
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
if (
|
|
280
|
-
|
|
281
|
-
|
|
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
|
-
|
|
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
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
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
|
-
|
|
398
|
-
|
|
399
|
-
|
|
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
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
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
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
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
|
-
|
|
455
|
-
|
|
456
|
-
|
|
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;
|