@mrlegendbot/baileys 1.2.3 → 1.2.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/README.upstream.md +1312 -0
- package/WAProto/WAProto.json +1 -0
- package/WAProto/WAProto.proto +6275 -0
- package/WAProto/compiler.js +550 -0
- package/WAProto/index.js +2 -97691
- package/lib/Defaults/index.js +89 -92
- package/lib/Signal/Group/ciphertext-message.js +9 -10
- package/lib/Signal/Group/group-session-builder.js +21 -22
- package/lib/Signal/Group/group_cipher.js +75 -69
- package/lib/Signal/Group/index.js +0 -1
- package/lib/Signal/Group/keyhelper.js +9 -10
- package/lib/Signal/Group/sender-chain-key.js +21 -22
- package/lib/Signal/Group/sender-key-distribution-message.js +51 -60
- package/lib/Signal/Group/sender-key-message.js +56 -62
- package/lib/Signal/Group/sender-key-name.js +38 -40
- package/lib/Signal/Group/sender-key-record.js +33 -34
- package/lib/Signal/Group/sender-key-state.js +72 -76
- package/lib/Signal/Group/sender-message-key.js +22 -23
- package/lib/Signal/libsignal.js +506 -405
- package/lib/Signal/lid-mapping.js +276 -258
- package/lib/Socket/Client/index.js +0 -1
- package/lib/Socket/Client/types.js +6 -7
- package/lib/Socket/Client/websocket.js +48 -49
- package/lib/Socket/business.js +349 -369
- package/lib/Socket/chats.js +1172 -1148
- package/lib/Socket/communities.js +473 -413
- package/lib/Socket/groups.js +387 -331
- package/lib/Socket/index.js +6 -8
- package/lib/Socket/messages-recv.js +1782 -1719
- package/lib/Socket/messages-send.js +1503 -2450
- package/lib/Socket/mex.js +40 -35
- package/lib/Socket/newsletter.js +239 -191
- package/lib/Socket/socket.js +992 -923
- package/lib/Store/index.js +8 -0
- package/lib/Store/keyed-db.js +108 -0
- package/lib/Store/make-cache-manager-store.js +70 -0
- package/lib/Store/make-in-memory-store.js +417 -0
- package/lib/Store/make-ordered-dictionary.js +69 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.js +0 -1
- package/lib/Types/Bussines.js +0 -1
- package/lib/Types/Call.js +0 -1
- package/lib/Types/Chat.js +1 -8
- package/lib/Types/Contact.js +0 -1
- package/lib/Types/Events.js +1 -2
- package/lib/Types/GroupMetadata.js +0 -1
- package/lib/Types/Label.js +20 -22
- package/lib/Types/LabelAssociation.js +2 -4
- package/lib/Types/Message.js +3 -5
- package/lib/Types/Mex.js +26 -31
- package/lib/Types/Product.js +0 -1
- package/lib/Types/Signal.js +1 -2
- package/lib/Types/Socket.js +2 -3
- package/lib/Types/State.js +4 -52
- package/lib/Types/USync.js +0 -1
- package/lib/Types/index.js +10 -11
- package/lib/Utils/auth-utils.js +240 -277
- package/lib/Utils/browser-utils.js +20 -23
- package/lib/Utils/business.js +200 -204
- package/lib/Utils/chat-utils.js +836 -810
- package/lib/Utils/companion-reg-client-utils.js +23 -25
- package/lib/Utils/crypto.js +70 -86
- package/lib/Utils/decode-wa-message.js +277 -284
- package/lib/Utils/event-buffer.js +592 -595
- package/lib/Utils/generics.js +312 -335
- package/lib/Utils/history.js +123 -115
- package/lib/Utils/index.js +1 -2
- package/lib/Utils/link-preview.js +74 -73
- package/lib/Utils/logger.js +3 -2
- package/lib/Utils/lt-hash.js +0 -6
- package/lib/Utils/make-mutex.js +27 -27
- package/lib/Utils/message-retry-manager.js +213 -254
- package/lib/Utils/messages-media.js +741 -715
- package/lib/Utils/messages.js +791 -827
- package/lib/Utils/noise-handler.js +215 -187
- package/lib/Utils/pre-key-manager.js +69 -98
- package/lib/Utils/process-message.js +617 -586
- package/lib/Utils/reporting-utils.js +553 -234
- package/lib/Utils/signal.js +217 -170
- package/lib/Utils/stanza-ack.js +27 -35
- package/lib/Utils/sync-action-utils.js +37 -41
- package/lib/Utils/tc-token-utils.js +120 -141
- package/lib/Utils/use-multi-file-auth-state.js +87 -105
- package/lib/Utils/use-sqlite-auth-state.js +162 -0
- package/lib/Utils/validate-connection.js +190 -188
- package/lib/WABinary/constants.js +28 -1294
- package/lib/WABinary/decode.js +245 -249
- package/lib/WABinary/encode.js +201 -206
- package/lib/WABinary/generic-utils.js +86 -89
- package/lib/WABinary/index.js +0 -1
- package/lib/WABinary/jid-utils.js +68 -77
- package/lib/WABinary/types.js +0 -1
- package/lib/WAM/BinaryInfo.js +7 -8
- package/lib/WAM/constants.js +19892 -22853
- package/lib/WAM/encode.js +121 -132
- package/lib/WAM/index.js +0 -1
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +46 -43
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +47 -48
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -23
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -33
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -20
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -44
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
- package/lib/WAUSync/Protocols/index.js +0 -1
- package/lib/WAUSync/USyncQuery.js +79 -88
- package/lib/WAUSync/USyncUser.js +28 -29
- package/lib/WAUSync/index.js +0 -1
- package/lib/index.js +2 -2
- package/package.json +22 -86
- package/WAProto/GenerateStatics.sh +0 -3
- package/WAProto/fix-imports.js +0 -85
- package/WAProto/index.d.ts +0 -14017
- package/lib/Defaults/index.d.ts +0 -75
- package/lib/Signal/Group/ciphertext-message.d.ts +0 -10
- package/lib/Signal/Group/group-session-builder.d.ts +0 -15
- package/lib/Signal/Group/group_cipher.d.ts +0 -17
- package/lib/Signal/Group/index.d.ts +0 -12
- package/lib/Signal/Group/keyhelper.d.ts +0 -11
- package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
- package/lib/Signal/Group/sender-key-message.d.ts +0 -19
- package/lib/Signal/Group/sender-key-name.d.ts +0 -18
- package/lib/Signal/Group/sender-key-record.d.ts +0 -31
- package/lib/Signal/Group/sender-key-state.d.ts +0 -39
- package/lib/Signal/Group/sender-message-key.d.ts +0 -12
- package/lib/Signal/libsignal.d.ts +0 -5
- package/lib/Signal/lid-mapping.d.ts +0 -23
- package/lib/Socket/Client/index.d.ts +0 -3
- package/lib/Socket/Client/types.d.ts +0 -16
- package/lib/Socket/Client/websocket.d.ts +0 -13
- package/lib/Socket/business.d.ts +0 -217
- package/lib/Socket/chats.d.ts +0 -125
- package/lib/Socket/communities.d.ts +0 -273
- package/lib/Socket/groups.d.ts +0 -161
- package/lib/Socket/index.d.ts +0 -260
- package/lib/Socket/messages-recv.d.ts +0 -203
- package/lib/Socket/messages-send.d.ts +0 -199
- package/lib/Socket/mex.d.ts +0 -3
- package/lib/Socket/newsletter.d.ts +0 -170
- package/lib/Socket/socket.d.ts +0 -59
- package/lib/Types/Auth.d.ts +0 -117
- package/lib/Types/Bussines.d.ts +0 -25
- package/lib/Types/Call.d.ts +0 -15
- package/lib/Types/Chat.d.ts +0 -124
- package/lib/Types/Contact.d.ts +0 -26
- package/lib/Types/Events.d.ts +0 -256
- package/lib/Types/GroupMetadata.d.ts +0 -71
- package/lib/Types/Label.d.ts +0 -47
- package/lib/Types/LabelAssociation.d.ts +0 -30
- package/lib/Types/Message.d.ts +0 -320
- package/lib/Types/Mex.d.ts +0 -141
- package/lib/Types/Product.d.ts +0 -79
- package/lib/Types/Signal.d.ts +0 -87
- package/lib/Types/Socket.d.ts +0 -136
- package/lib/Types/State.d.ts +0 -97
- package/lib/Types/USync.d.ts +0 -26
- package/lib/Types/index.d.ts +0 -66
- package/lib/Utils/auth-utils.d.ts +0 -24
- package/lib/Utils/browser-utils.d.ts +0 -4
- package/lib/Utils/business.d.ts +0 -23
- package/lib/Utils/chat-utils.d.ts +0 -100
- package/lib/Utils/companion-reg-client-utils.d.ts +0 -17
- package/lib/Utils/crypto.d.ts +0 -37
- package/lib/Utils/decode-wa-message.d.ts +0 -66
- package/lib/Utils/event-buffer.d.ts +0 -36
- package/lib/Utils/generics.d.ts +0 -92
- package/lib/Utils/history.d.ts +0 -24
- package/lib/Utils/identity-change-handler.d.ts +0 -44
- package/lib/Utils/identity-change-handler.js +0 -50
- package/lib/Utils/index.d.ts +0 -22
- package/lib/Utils/link-preview.d.ts +0 -21
- package/lib/Utils/logger.d.ts +0 -12
- package/lib/Utils/lt-hash.d.ts +0 -8
- package/lib/Utils/make-mutex.d.ts +0 -9
- package/lib/Utils/message-retry-manager.d.ts +0 -115
- package/lib/Utils/messages-media.d.ts +0 -133
- package/lib/Utils/messages.d.ts +0 -91
- package/lib/Utils/noise-handler.d.ts +0 -20
- package/lib/Utils/offline-node-processor.d.ts +0 -17
- package/lib/Utils/offline-node-processor.js +0 -40
- package/lib/Utils/pre-key-manager.d.ts +0 -28
- package/lib/Utils/process-message.d.ts +0 -60
- package/lib/Utils/reporting-utils.d.ts +0 -11
- package/lib/Utils/signal.d.ts +0 -47
- package/lib/Utils/stanza-ack.d.ts +0 -11
- package/lib/Utils/sync-action-utils.d.ts +0 -19
- package/lib/Utils/tc-token-utils.d.ts +0 -37
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
- package/lib/Utils/validate-connection.d.ts +0 -11
- package/lib/WABinary/constants.d.ts +0 -28
- package/lib/WABinary/decode.d.ts +0 -7
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -15
- package/lib/WABinary/index.d.ts +0 -6
- package/lib/WABinary/jid-utils.d.ts +0 -48
- package/lib/WABinary/types.d.ts +0 -19
- package/lib/WAM/BinaryInfo.d.ts +0 -9
- package/lib/WAM/constants.d.ts +0 -40
- package/lib/WAM/encode.d.ts +0 -3
- package/lib/WAM/index.d.ts +0 -4
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -23
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -13
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -13
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -26
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/index.d.ts +0 -6
- package/lib/WAUSync/USyncQuery.d.ts +0 -30
- package/lib/WAUSync/USyncUser.d.ts +0 -17
- package/lib/WAUSync/index.d.ts +0 -4
- package/lib/index.d.ts +0 -12
package/lib/Socket/socket.js
CHANGED
|
@@ -2,9 +2,8 @@ import { Boom } from '@hapi/boom';
|
|
|
2
2
|
import { randomBytes } from 'crypto';
|
|
3
3
|
import { URL } from 'url';
|
|
4
4
|
import { promisify } from 'util';
|
|
5
|
-
import { proto } from '../../WAProto/
|
|
5
|
+
import { proto } from '../../WAProto/compiler.js';
|
|
6
6
|
import { DEF_CALLBACK_PREFIX, DEF_TAG_PREFIX, INITIAL_PREKEY_COUNT, MIN_PREKEY_COUNT, NOISE_WA_HEADER, PROCESSABLE_HISTORY_TYPES, TimeMs, UPLOAD_TIMEOUT } from '../Defaults/index.js';
|
|
7
|
-
import { QueryIds, ReachoutTimelockEnforcementType } from '../Types/index.js';
|
|
8
7
|
import { DisconnectReason, XWAPaths } from '../Types/index.js';
|
|
9
8
|
import { addTransactionCapability, aesEncryptCTR, bindWaitForConnectionUpdate, buildPairingQRData, bytesToCrockford, configureSuccessfulPairing, Curve, derivePairingCodeKey, generateLoginNode, generateMdTagPrefix, generateRegistrationNode, getCodeFromWSError, getCompanionPlatformId, getErrorCodeFromStreamError, getNextPreKeysNode, makeEventBuffer, makeNoiseHandler, promiseTimeout, signedKeyPair, xmppSignedPreKey } from '../Utils/index.js';
|
|
10
9
|
import { assertNodeErrorFree, binaryNodeToString, encodeBinaryNode, getAllBinaryNodeChildren, getBinaryNodeChild, getBinaryNodeChildren, isLidUser, jidDecode, jidEncode, S_WHATSAPP_NET } from '../WABinary/index.js';
|
|
@@ -12,959 +11,1029 @@ import { BinaryInfo } from '../WAM/BinaryInfo.js';
|
|
|
12
11
|
import { USyncQuery, USyncUser } from '../WAUSync/index.js';
|
|
13
12
|
import { WebSocketClient } from './Client/index.js';
|
|
14
13
|
import { executeWMexQuery } from './mex.js';
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const ephemeralKeyPair = Curve.generateKeyPair();
|
|
47
|
-
/** WA noise protocol wrapper */
|
|
48
|
-
const noise = makeNoiseHandler({
|
|
49
|
-
keyPair: ephemeralKeyPair,
|
|
50
|
-
NOISE_HEADER: NOISE_WA_HEADER,
|
|
51
|
-
logger,
|
|
52
|
-
routingInfo: authState?.creds?.routingInfo
|
|
14
|
+
export const makeSocket = config => {
|
|
15
|
+
const {
|
|
16
|
+
waWebSocketUrl,
|
|
17
|
+
connectTimeoutMs,
|
|
18
|
+
logger,
|
|
19
|
+
keepAliveIntervalMs,
|
|
20
|
+
browser,
|
|
21
|
+
auth: authState,
|
|
22
|
+
printQRInTerminal,
|
|
23
|
+
defaultQueryTimeoutMs,
|
|
24
|
+
transactionOpts,
|
|
25
|
+
qrTimeout,
|
|
26
|
+
makeSignalRepository
|
|
27
|
+
} = config;
|
|
28
|
+
const publicWAMBuffer = new BinaryInfo();
|
|
29
|
+
let serverTimeOffsetMs = 0;
|
|
30
|
+
const uqTagId = generateMdTagPrefix();
|
|
31
|
+
const generateMessageTag = () => `${uqTagId}${epoch++}`;
|
|
32
|
+
if (printQRInTerminal) {
|
|
33
|
+
logger.warn({}, '⚠️ The printQRInTerminal option has been deprecated. You will no longer receive QR codes in the terminal automatically. Please listen to the connection.update event yourself and handle the QR your way. You can remove this message by removing this opttion. This message will be removed in a future version.');
|
|
34
|
+
}
|
|
35
|
+
const syncDisabled = PROCESSABLE_HISTORY_TYPES.map(syncType => config.shouldSyncHistoryMessage({
|
|
36
|
+
syncType
|
|
37
|
+
})).filter(x => x === false).length === PROCESSABLE_HISTORY_TYPES.length;
|
|
38
|
+
if (syncDisabled) {
|
|
39
|
+
logger.warn('⚠️ DANGER: DISABLING ALL SYNC BY shouldSyncHistoryMsg PREVENTS BAILEYS FROM ACCESSING INITIAL LID MAPPINGS, LEADING TO INSTABILIY AND SESSION ERRORS');
|
|
40
|
+
}
|
|
41
|
+
const url = typeof waWebSocketUrl === 'string' ? new URL(waWebSocketUrl) : waWebSocketUrl;
|
|
42
|
+
if (config.mobile || url.protocol === 'tcp:') {
|
|
43
|
+
throw new Boom('Mobile API is not supported anymore', {
|
|
44
|
+
statusCode: DisconnectReason.loggedOut
|
|
53
45
|
});
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
ws.on('close', onErr);
|
|
102
|
-
ws.on('error', onErr);
|
|
103
|
-
return () => reject(new Boom('Query Cancelled'));
|
|
104
|
-
});
|
|
105
|
-
return result;
|
|
106
|
-
}
|
|
107
|
-
catch (error) {
|
|
108
|
-
// Catch timeout and return undefined instead of throwing
|
|
109
|
-
if (error instanceof Boom && error.output?.statusCode === DisconnectReason.timedOut) {
|
|
110
|
-
logger?.warn?.({ msgId }, 'timed out waiting for message');
|
|
111
|
-
return undefined;
|
|
112
|
-
}
|
|
113
|
-
throw error;
|
|
114
|
-
}
|
|
115
|
-
finally {
|
|
116
|
-
if (onRecv)
|
|
117
|
-
ws.off(`TAG:${msgId}`, onRecv);
|
|
118
|
-
if (onErr) {
|
|
119
|
-
ws.off('close', onErr);
|
|
120
|
-
ws.off('error', onErr);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
/** send a query, and wait for its response. auto-generates message ID if not provided */
|
|
125
|
-
const query = async (node, timeoutMs) => {
|
|
126
|
-
if (!node.attrs.id) {
|
|
127
|
-
node.attrs.id = generateMessageTag();
|
|
128
|
-
}
|
|
129
|
-
const msgId = node.attrs.id;
|
|
130
|
-
const result = await promiseTimeout(timeoutMs, async (resolve, reject) => {
|
|
131
|
-
const result = waitForMessage(msgId, timeoutMs).catch(reject);
|
|
132
|
-
sendNode(node)
|
|
133
|
-
.then(async () => resolve(await result))
|
|
134
|
-
.catch(reject);
|
|
135
|
-
});
|
|
136
|
-
if (result && 'tag' in result) {
|
|
137
|
-
assertNodeErrorFree(result);
|
|
138
|
-
}
|
|
139
|
-
return result;
|
|
140
|
-
};
|
|
141
|
-
// Validate current key-bundle on server; on failure, trigger pre-key upload and rethrow
|
|
142
|
-
const digestKeyBundle = async () => {
|
|
143
|
-
const res = await query({
|
|
144
|
-
tag: 'iq',
|
|
145
|
-
attrs: { to: S_WHATSAPP_NET, type: 'get', xmlns: 'encrypt' },
|
|
146
|
-
content: [{ tag: 'digest', attrs: {} }]
|
|
147
|
-
});
|
|
148
|
-
const digestNode = getBinaryNodeChild(res, 'digest');
|
|
149
|
-
if (!digestNode) {
|
|
150
|
-
await uploadPreKeys();
|
|
151
|
-
throw new Error('encrypt/get digest returned no digest node');
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
// Rotate our signed pre-key on server; on failure, run digest as fallback and rethrow
|
|
155
|
-
const rotateSignedPreKey = async () => {
|
|
156
|
-
const newId = (creds.signedPreKey.keyId || 0) + 1;
|
|
157
|
-
const skey = await signedKeyPair(creds.signedIdentityKey, newId);
|
|
158
|
-
await query({
|
|
159
|
-
tag: 'iq',
|
|
160
|
-
attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'encrypt' },
|
|
161
|
-
content: [
|
|
162
|
-
{
|
|
163
|
-
tag: 'rotate',
|
|
164
|
-
attrs: {},
|
|
165
|
-
content: [xmppSignedPreKey(skey)]
|
|
166
|
-
}
|
|
167
|
-
]
|
|
168
|
-
});
|
|
169
|
-
// Persist new signed pre-key in creds
|
|
170
|
-
ev.emit('creds.update', { signedPreKey: skey });
|
|
171
|
-
};
|
|
172
|
-
const executeUSyncQuery = async (usyncQuery) => {
|
|
173
|
-
if (usyncQuery.protocols.length === 0) {
|
|
174
|
-
throw new Boom('USyncQuery must have at least one protocol');
|
|
175
|
-
}
|
|
176
|
-
// todo: validate users, throw WARNING on no valid users
|
|
177
|
-
// variable below has only validated users
|
|
178
|
-
const validUsers = usyncQuery.users;
|
|
179
|
-
const userNodes = validUsers.map(user => {
|
|
180
|
-
return {
|
|
181
|
-
tag: 'user',
|
|
182
|
-
attrs: {
|
|
183
|
-
jid: !user.phone ? user.id : undefined
|
|
184
|
-
},
|
|
185
|
-
content: usyncQuery.protocols.map(a => a.getUserElement(user)).filter(a => a !== null)
|
|
186
|
-
};
|
|
187
|
-
});
|
|
188
|
-
const listNode = {
|
|
189
|
-
tag: 'list',
|
|
190
|
-
attrs: {},
|
|
191
|
-
content: userNodes
|
|
192
|
-
};
|
|
193
|
-
const queryNode = {
|
|
194
|
-
tag: 'query',
|
|
195
|
-
attrs: {},
|
|
196
|
-
content: usyncQuery.protocols.map(a => a.getQueryElement())
|
|
197
|
-
};
|
|
198
|
-
const iq = {
|
|
199
|
-
tag: 'iq',
|
|
200
|
-
attrs: {
|
|
201
|
-
to: S_WHATSAPP_NET,
|
|
202
|
-
type: 'get',
|
|
203
|
-
xmlns: 'usync'
|
|
204
|
-
},
|
|
205
|
-
content: [
|
|
206
|
-
{
|
|
207
|
-
tag: 'usync',
|
|
208
|
-
attrs: {
|
|
209
|
-
context: usyncQuery.context,
|
|
210
|
-
mode: usyncQuery.mode,
|
|
211
|
-
sid: generateMessageTag(),
|
|
212
|
-
last: 'true',
|
|
213
|
-
index: '0'
|
|
214
|
-
},
|
|
215
|
-
content: [queryNode, listNode]
|
|
216
|
-
}
|
|
217
|
-
]
|
|
46
|
+
}
|
|
47
|
+
if (url.protocol === 'wss' && authState?.creds?.routingInfo) {
|
|
48
|
+
url.searchParams.append('ED', authState.creds.routingInfo.toString('base64url'));
|
|
49
|
+
}
|
|
50
|
+
const ephemeralKeyPair = Curve.generateKeyPair();
|
|
51
|
+
const noise = makeNoiseHandler({
|
|
52
|
+
keyPair: ephemeralKeyPair,
|
|
53
|
+
NOISE_HEADER: NOISE_WA_HEADER,
|
|
54
|
+
logger,
|
|
55
|
+
routingInfo: authState?.creds?.routingInfo
|
|
56
|
+
});
|
|
57
|
+
const ws = new WebSocketClient(url, config);
|
|
58
|
+
ws.connect();
|
|
59
|
+
const sendPromise = promisify(ws.send);
|
|
60
|
+
const sendRawMessage = async data => {
|
|
61
|
+
if (!ws.isOpen) {
|
|
62
|
+
throw new Boom('Connection Closed', {
|
|
63
|
+
statusCode: DisconnectReason.connectionClosed
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
const bytes = noise.encodeFrame(data);
|
|
67
|
+
await promiseTimeout(connectTimeoutMs, async (resolve, reject) => {
|
|
68
|
+
try {
|
|
69
|
+
await sendPromise.call(ws, bytes);
|
|
70
|
+
resolve();
|
|
71
|
+
} catch (error) {
|
|
72
|
+
reject(error);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
const sendNode = frame => {
|
|
77
|
+
if (logger.level === 'trace') {
|
|
78
|
+
logger.trace({
|
|
79
|
+
xml: binaryNodeToString(frame),
|
|
80
|
+
msg: 'xml send'
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
const buff = encodeBinaryNode(frame);
|
|
84
|
+
return sendRawMessage(buff);
|
|
85
|
+
};
|
|
86
|
+
const waitForMessage = async (msgId, timeoutMs = defaultQueryTimeoutMs) => {
|
|
87
|
+
let onRecv;
|
|
88
|
+
let onErr;
|
|
89
|
+
try {
|
|
90
|
+
const result = await promiseTimeout(timeoutMs, (resolve, reject) => {
|
|
91
|
+
onRecv = data => {
|
|
92
|
+
resolve(data);
|
|
218
93
|
};
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
let usyncQuery = new USyncQuery();
|
|
224
|
-
let contactEnabled = false;
|
|
225
|
-
for (const jid of phoneNumber) {
|
|
226
|
-
if (isLidUser(jid)) {
|
|
227
|
-
logger?.warn('LIDs are not supported with onWhatsApp');
|
|
228
|
-
continue;
|
|
229
|
-
}
|
|
230
|
-
else {
|
|
231
|
-
if (!contactEnabled) {
|
|
232
|
-
contactEnabled = true;
|
|
233
|
-
usyncQuery = usyncQuery.withContactProtocol();
|
|
234
|
-
}
|
|
235
|
-
const phone = `+${jid.replace('+', '').split('@')[0]?.split(':')[0]}`;
|
|
236
|
-
usyncQuery.withUser(new USyncUser().withPhone(phone));
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
if (usyncQuery.users.length === 0) {
|
|
240
|
-
return []; // return early without forcing an empty query
|
|
241
|
-
}
|
|
242
|
-
const results = await executeUSyncQuery(usyncQuery);
|
|
243
|
-
if (results) {
|
|
244
|
-
return results.list.filter(a => !!a.contact).map(({ contact, id }) => ({ jid: id, exists: contact }));
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
const pnFromLIDUSync = async (jids) => {
|
|
248
|
-
const usyncQuery = new USyncQuery().withLIDProtocol().withContext('background');
|
|
249
|
-
for (const jid of jids) {
|
|
250
|
-
if (isLidUser(jid)) {
|
|
251
|
-
logger?.warn('LID user found in LID fetch call');
|
|
252
|
-
continue;
|
|
253
|
-
}
|
|
254
|
-
else {
|
|
255
|
-
usyncQuery.withUser(new USyncUser().withId(jid));
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
if (usyncQuery.users.length === 0) {
|
|
259
|
-
return []; // return early without forcing an empty query
|
|
260
|
-
}
|
|
261
|
-
const results = await executeUSyncQuery(usyncQuery);
|
|
262
|
-
if (results) {
|
|
263
|
-
return results.list.filter(a => !!a.lid).map(({ lid, id }) => ({ pn: id, lid: lid }));
|
|
264
|
-
}
|
|
265
|
-
return [];
|
|
266
|
-
};
|
|
267
|
-
const ev = makeEventBuffer(logger);
|
|
268
|
-
const { creds } = authState;
|
|
269
|
-
// add transaction capability
|
|
270
|
-
const keys = addTransactionCapability(authState.keys, logger, transactionOpts);
|
|
271
|
-
const signalRepository = makeSignalRepository({ creds, keys }, logger, pnFromLIDUSync);
|
|
272
|
-
let lastDateRecv;
|
|
273
|
-
let epoch = 1;
|
|
274
|
-
let keepAliveReq;
|
|
275
|
-
let qrTimer;
|
|
276
|
-
let closed = false;
|
|
277
|
-
const socketEndHandlers = [];
|
|
278
|
-
/** log & process any unexpected errors */
|
|
279
|
-
const onUnexpectedError = (err, msg) => {
|
|
280
|
-
logger.error({ err }, `unexpected error in '${msg}'`);
|
|
281
|
-
};
|
|
282
|
-
/** await the next incoming message */
|
|
283
|
-
const awaitNextMessage = async (sendMsg) => {
|
|
284
|
-
if (!ws.isOpen) {
|
|
285
|
-
throw new Boom('Connection Closed', {
|
|
286
|
-
statusCode: DisconnectReason.connectionClosed
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
let onOpen;
|
|
290
|
-
let onClose;
|
|
291
|
-
const result = promiseTimeout(connectTimeoutMs, (resolve, reject) => {
|
|
292
|
-
onOpen = resolve;
|
|
293
|
-
onClose = mapWebSocketError(reject);
|
|
294
|
-
ws.on('frame', onOpen);
|
|
295
|
-
ws.on('close', onClose);
|
|
296
|
-
ws.on('error', onClose);
|
|
297
|
-
}).finally(() => {
|
|
298
|
-
ws.off('frame', onOpen);
|
|
299
|
-
ws.off('close', onClose);
|
|
300
|
-
ws.off('error', onClose);
|
|
301
|
-
});
|
|
302
|
-
if (sendMsg) {
|
|
303
|
-
sendRawMessage(sendMsg).catch(onClose);
|
|
304
|
-
}
|
|
305
|
-
return result;
|
|
306
|
-
};
|
|
307
|
-
/** connection handshake */
|
|
308
|
-
const validateConnection = async () => {
|
|
309
|
-
let helloMsg = {
|
|
310
|
-
clientHello: { ephemeral: ephemeralKeyPair.public }
|
|
94
|
+
onErr = err => {
|
|
95
|
+
reject(err || new Boom('Connection Closed', {
|
|
96
|
+
statusCode: DisconnectReason.connectionClosed
|
|
97
|
+
}));
|
|
311
98
|
};
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
}
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
99
|
+
ws.on(`TAG:${msgId}`, onRecv);
|
|
100
|
+
ws.on('close', onErr);
|
|
101
|
+
ws.on('error', onErr);
|
|
102
|
+
return () => reject(new Boom('Query Cancelled'));
|
|
103
|
+
});
|
|
104
|
+
return result;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if (error instanceof Boom && error.output?.statusCode === DisconnectReason.timedOut) {
|
|
107
|
+
logger?.warn?.({
|
|
108
|
+
msgId
|
|
109
|
+
}, 'timed out waiting for message');
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
throw error;
|
|
113
|
+
} finally {
|
|
114
|
+
if (onRecv) ws.off(`TAG:${msgId}`, onRecv);
|
|
115
|
+
if (onErr) {
|
|
116
|
+
ws.off('close', onErr);
|
|
117
|
+
ws.off('error', onErr);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const query = async (node, timeoutMs) => {
|
|
122
|
+
if (!node.attrs.id) {
|
|
123
|
+
node.attrs.id = generateMessageTag();
|
|
124
|
+
}
|
|
125
|
+
const msgId = node.attrs.id;
|
|
126
|
+
const result = await promiseTimeout(timeoutMs, async (resolve, reject) => {
|
|
127
|
+
const result = waitForMessage(msgId, timeoutMs).catch(reject);
|
|
128
|
+
sendNode(node).then(async () => resolve(await result)).catch(reject);
|
|
129
|
+
});
|
|
130
|
+
if (result && 'tag' in result) {
|
|
131
|
+
assertNodeErrorFree(result);
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
};
|
|
135
|
+
const digestKeyBundle = async () => {
|
|
136
|
+
const res = await query({
|
|
137
|
+
tag: 'iq',
|
|
138
|
+
attrs: {
|
|
139
|
+
to: S_WHATSAPP_NET,
|
|
140
|
+
type: 'get',
|
|
141
|
+
xmlns: 'encrypt'
|
|
142
|
+
},
|
|
143
|
+
content: [{
|
|
144
|
+
tag: 'digest',
|
|
145
|
+
attrs: {}
|
|
146
|
+
}]
|
|
147
|
+
});
|
|
148
|
+
const digestNode = getBinaryNodeChild(res, 'digest');
|
|
149
|
+
if (!digestNode) {
|
|
150
|
+
await uploadPreKeys();
|
|
151
|
+
throw new Error('encrypt/get digest returned no digest node');
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const rotateSignedPreKey = async () => {
|
|
155
|
+
const newId = (creds.signedPreKey.keyId || 0) + 1;
|
|
156
|
+
const skey = await signedKeyPair(creds.signedIdentityKey, newId);
|
|
157
|
+
await query({
|
|
158
|
+
tag: 'iq',
|
|
159
|
+
attrs: {
|
|
160
|
+
to: S_WHATSAPP_NET,
|
|
161
|
+
type: 'set',
|
|
162
|
+
xmlns: 'encrypt'
|
|
163
|
+
},
|
|
164
|
+
content: [{
|
|
165
|
+
tag: 'rotate',
|
|
166
|
+
attrs: {},
|
|
167
|
+
content: [xmppSignedPreKey(skey)]
|
|
168
|
+
}]
|
|
169
|
+
});
|
|
170
|
+
ev.emit('creds.update', {
|
|
171
|
+
signedPreKey: skey
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
const executeUSyncQuery = async usyncQuery => {
|
|
175
|
+
if (usyncQuery.protocols.length === 0) {
|
|
176
|
+
throw new Boom('USyncQuery must have at least one protocol');
|
|
177
|
+
}
|
|
178
|
+
const validUsers = usyncQuery.users;
|
|
179
|
+
const userNodes = validUsers.map(user => {
|
|
180
|
+
return {
|
|
181
|
+
tag: 'user',
|
|
182
|
+
attrs: {
|
|
183
|
+
jid: !user.phone ? user.id : undefined
|
|
184
|
+
},
|
|
185
|
+
content: usyncQuery.protocols.map(a => a.getUserElement(user)).filter(a => a !== null)
|
|
186
|
+
};
|
|
187
|
+
});
|
|
188
|
+
const listNode = {
|
|
189
|
+
tag: 'list',
|
|
190
|
+
attrs: {},
|
|
191
|
+
content: userNodes
|
|
351
192
|
};
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
if (uploadPreKeysPromise) {
|
|
357
|
-
logger.debug('Pre-key upload already in progress, waiting for completion');
|
|
358
|
-
await uploadPreKeysPromise;
|
|
359
|
-
return;
|
|
360
|
-
}
|
|
361
|
-
const uploadLogic = async (retryCount) => {
|
|
362
|
-
logger.info({ count, retryCount }, 'uploading pre-keys');
|
|
363
|
-
// Generate and save pre-keys atomically (prevents ID collisions on retry)
|
|
364
|
-
const node = await keys.transaction(async () => {
|
|
365
|
-
logger.debug({ requestedCount: count }, 'generating pre-keys with requested count');
|
|
366
|
-
const { update, node } = await getNextPreKeysNode({ creds, keys }, count);
|
|
367
|
-
// Update credentials immediately to prevent duplicate IDs on retry
|
|
368
|
-
ev.emit('creds.update', update);
|
|
369
|
-
return node;
|
|
370
|
-
}, creds?.me?.id || 'upload-pre-keys');
|
|
371
|
-
// Upload to server (outside transaction, can fail without affecting local keys)
|
|
372
|
-
try {
|
|
373
|
-
await query(node);
|
|
374
|
-
logger.info({ count }, 'uploaded pre-keys successfully');
|
|
375
|
-
}
|
|
376
|
-
catch (uploadError) {
|
|
377
|
-
logger.error({ uploadError: uploadError.toString(), count }, 'Failed to upload pre-keys to server');
|
|
378
|
-
// Recurse into uploadLogic; calling uploadPreKeys would await its own in-flight promise.
|
|
379
|
-
if (retryCount < 3) {
|
|
380
|
-
const backoffDelay = Math.min(1000 * Math.pow(2, retryCount), 10000);
|
|
381
|
-
logger.info(`Retrying pre-key upload in ${backoffDelay}ms`);
|
|
382
|
-
await new Promise(resolve => setTimeout(resolve, backoffDelay));
|
|
383
|
-
return uploadLogic(retryCount + 1);
|
|
384
|
-
}
|
|
385
|
-
throw uploadError;
|
|
386
|
-
}
|
|
387
|
-
};
|
|
388
|
-
// Add timeout protection
|
|
389
|
-
uploadPreKeysPromise = Promise.race([
|
|
390
|
-
uploadLogic(0),
|
|
391
|
-
new Promise((_, reject) => setTimeout(() => reject(new Boom('Pre-key upload timeout', { statusCode: 408 })), UPLOAD_TIMEOUT))
|
|
392
|
-
]);
|
|
393
|
-
try {
|
|
394
|
-
await uploadPreKeysPromise;
|
|
395
|
-
}
|
|
396
|
-
finally {
|
|
397
|
-
uploadPreKeysPromise = null;
|
|
398
|
-
}
|
|
193
|
+
const queryNode = {
|
|
194
|
+
tag: 'query',
|
|
195
|
+
attrs: {},
|
|
196
|
+
content: usyncQuery.protocols.map(a => a.getQueryElement())
|
|
399
197
|
};
|
|
400
|
-
const
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
198
|
+
const iq = {
|
|
199
|
+
tag: 'iq',
|
|
200
|
+
attrs: {
|
|
201
|
+
to: S_WHATSAPP_NET,
|
|
202
|
+
type: 'get',
|
|
203
|
+
xmlns: 'usync'
|
|
204
|
+
},
|
|
205
|
+
content: [{
|
|
206
|
+
tag: 'usync',
|
|
207
|
+
attrs: {
|
|
208
|
+
context: usyncQuery.context,
|
|
209
|
+
mode: usyncQuery.mode,
|
|
210
|
+
sid: generateMessageTag(),
|
|
211
|
+
last: 'true',
|
|
212
|
+
index: '0'
|
|
213
|
+
},
|
|
214
|
+
content: [queryNode, listNode]
|
|
215
|
+
}]
|
|
408
216
|
};
|
|
409
|
-
const
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
if (shouldUpload) {
|
|
424
|
-
const reasons = [];
|
|
425
|
-
if (lowServerCount)
|
|
426
|
-
reasons.push(`server count low (${preKeyCount})`);
|
|
427
|
-
if (missingCurrentPreKey)
|
|
428
|
-
reasons.push(`current prekey ${currentPreKeyId} missing from storage`);
|
|
429
|
-
logger.info(`Uploading PreKeys due to: ${reasons.join(', ')}`);
|
|
430
|
-
await uploadPreKeys(count);
|
|
431
|
-
}
|
|
432
|
-
else {
|
|
433
|
-
logger.info(`PreKey validation passed - Server: ${preKeyCount}, Current prekey ${currentPreKeyId} exists`);
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
catch (error) {
|
|
437
|
-
logger.error({ error }, 'Failed to check/upload pre-keys during initialization');
|
|
438
|
-
// Don't throw - allow connection to continue even if pre-key check fails
|
|
217
|
+
const result = await query(iq);
|
|
218
|
+
return usyncQuery.parseUSyncQueryResult(result);
|
|
219
|
+
};
|
|
220
|
+
const onWhatsApp = async (...phoneNumber) => {
|
|
221
|
+
let usyncQuery = new USyncQuery();
|
|
222
|
+
let contactEnabled = false;
|
|
223
|
+
for (const jid of phoneNumber) {
|
|
224
|
+
if (isLidUser(jid)) {
|
|
225
|
+
logger?.warn('LIDs are not supported with onWhatsApp');
|
|
226
|
+
continue;
|
|
227
|
+
} else {
|
|
228
|
+
if (!contactEnabled) {
|
|
229
|
+
contactEnabled = true;
|
|
230
|
+
usyncQuery = usyncQuery.withContactProtocol();
|
|
439
231
|
}
|
|
232
|
+
const phone = `+${jid.replace('+', '').split('@')[0]?.split(':')[0]}`;
|
|
233
|
+
usyncQuery.withUser(new USyncUser().withPhone(phone));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (usyncQuery.users.length === 0) {
|
|
237
|
+
return [];
|
|
238
|
+
}
|
|
239
|
+
const results = await executeUSyncQuery(usyncQuery);
|
|
240
|
+
if (results) {
|
|
241
|
+
return results.list.filter(a => !!a.contact).map(({
|
|
242
|
+
contact,
|
|
243
|
+
id
|
|
244
|
+
}) => ({
|
|
245
|
+
jid: id,
|
|
246
|
+
exists: contact
|
|
247
|
+
}));
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
const pnFromLIDUSync = async jids => {
|
|
251
|
+
const usyncQuery = new USyncQuery().withLIDProtocol().withContext('background');
|
|
252
|
+
for (const jid of jids) {
|
|
253
|
+
if (isLidUser(jid)) {
|
|
254
|
+
logger?.warn('LID user found in LID fetch call');
|
|
255
|
+
continue;
|
|
256
|
+
} else {
|
|
257
|
+
usyncQuery.withUser(new USyncUser().withId(jid));
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (usyncQuery.users.length === 0) {
|
|
261
|
+
return [];
|
|
262
|
+
}
|
|
263
|
+
const results = await executeUSyncQuery(usyncQuery);
|
|
264
|
+
if (results) {
|
|
265
|
+
return results.list.filter(a => !!a.lid).map(({
|
|
266
|
+
lid,
|
|
267
|
+
id
|
|
268
|
+
}) => ({
|
|
269
|
+
pn: id,
|
|
270
|
+
lid: lid
|
|
271
|
+
}));
|
|
272
|
+
}
|
|
273
|
+
return [];
|
|
274
|
+
};
|
|
275
|
+
const ev = makeEventBuffer(logger);
|
|
276
|
+
const {
|
|
277
|
+
creds
|
|
278
|
+
} = authState;
|
|
279
|
+
const keys = addTransactionCapability(authState.keys, logger, transactionOpts);
|
|
280
|
+
const signalRepository = makeSignalRepository({
|
|
281
|
+
creds,
|
|
282
|
+
keys
|
|
283
|
+
}, logger, pnFromLIDUSync);
|
|
284
|
+
let lastDateRecv;
|
|
285
|
+
let epoch = 1;
|
|
286
|
+
let keepAliveReq;
|
|
287
|
+
let qrTimer;
|
|
288
|
+
let closed = false;
|
|
289
|
+
const socketEndHandlers = [];
|
|
290
|
+
const onUnexpectedError = (err, msg) => {
|
|
291
|
+
logger.error({
|
|
292
|
+
err
|
|
293
|
+
}, `unexpected error in '${msg}'`);
|
|
294
|
+
};
|
|
295
|
+
const awaitNextMessage = async sendMsg => {
|
|
296
|
+
if (!ws.isOpen) {
|
|
297
|
+
throw new Boom('Connection Closed', {
|
|
298
|
+
statusCode: DisconnectReason.connectionClosed
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
let onOpen;
|
|
302
|
+
let onClose;
|
|
303
|
+
const result = promiseTimeout(connectTimeoutMs, (resolve, reject) => {
|
|
304
|
+
onOpen = resolve;
|
|
305
|
+
onClose = mapWebSocketError(reject);
|
|
306
|
+
ws.on('frame', onOpen);
|
|
307
|
+
ws.on('close', onClose);
|
|
308
|
+
ws.on('error', onClose);
|
|
309
|
+
}).finally(() => {
|
|
310
|
+
ws.off('frame', onOpen);
|
|
311
|
+
ws.off('close', onClose);
|
|
312
|
+
ws.off('error', onClose);
|
|
313
|
+
});
|
|
314
|
+
if (sendMsg) {
|
|
315
|
+
sendRawMessage(sendMsg).catch(onClose);
|
|
316
|
+
}
|
|
317
|
+
return result;
|
|
318
|
+
};
|
|
319
|
+
const validateConnection = async () => {
|
|
320
|
+
let helloMsg = {
|
|
321
|
+
clientHello: {
|
|
322
|
+
ephemeral: ephemeralKeyPair.public
|
|
323
|
+
}
|
|
440
324
|
};
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
};
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
325
|
+
helloMsg = proto.HandshakeMessage.fromObject(helloMsg);
|
|
326
|
+
logger.info({
|
|
327
|
+
browser,
|
|
328
|
+
helloMsg
|
|
329
|
+
}, 'connected to WA');
|
|
330
|
+
const init = proto.HandshakeMessage.encode(helloMsg).finish();
|
|
331
|
+
const result = await awaitNextMessage(init);
|
|
332
|
+
const handshake = proto.HandshakeMessage.decode(result);
|
|
333
|
+
logger.trace({
|
|
334
|
+
handshake
|
|
335
|
+
}, 'handshake recv from WA');
|
|
336
|
+
const keyEnc = noise.processHandshake(handshake, creds.noiseKey);
|
|
337
|
+
let node;
|
|
338
|
+
if (!creds.me) {
|
|
339
|
+
node = generateRegistrationNode(creds, config);
|
|
340
|
+
logger.info({
|
|
341
|
+
node
|
|
342
|
+
}, 'not logged in, attempting registration...');
|
|
343
|
+
} else {
|
|
344
|
+
node = generateLoginNode(creds.me.id, config);
|
|
345
|
+
logger.info({
|
|
346
|
+
node
|
|
347
|
+
}, 'logging in...');
|
|
348
|
+
}
|
|
349
|
+
const payloadEnc = noise.encrypt(proto.ClientPayload.encode(node).finish());
|
|
350
|
+
await sendRawMessage(proto.HandshakeMessage.encode({
|
|
351
|
+
clientFinish: {
|
|
352
|
+
static: keyEnc,
|
|
353
|
+
payload: payloadEnc
|
|
354
|
+
}
|
|
355
|
+
}).finish());
|
|
356
|
+
await noise.finishInit();
|
|
357
|
+
startKeepAliveRequest();
|
|
358
|
+
};
|
|
359
|
+
const getAvailablePreKeysOnServer = async () => {
|
|
360
|
+
const result = await query({
|
|
361
|
+
tag: 'iq',
|
|
362
|
+
attrs: {
|
|
363
|
+
id: generateMessageTag(),
|
|
364
|
+
xmlns: 'encrypt',
|
|
365
|
+
type: 'get',
|
|
366
|
+
to: S_WHATSAPP_NET
|
|
367
|
+
},
|
|
368
|
+
content: [{
|
|
369
|
+
tag: 'count',
|
|
370
|
+
attrs: {}
|
|
371
|
+
}]
|
|
372
|
+
});
|
|
373
|
+
const countChild = getBinaryNodeChild(result, 'count');
|
|
374
|
+
return +countChild.attrs.value;
|
|
375
|
+
};
|
|
376
|
+
let uploadPreKeysPromise = null;
|
|
377
|
+
const uploadPreKeys = async (count = MIN_PREKEY_COUNT) => {
|
|
378
|
+
if (uploadPreKeysPromise) {
|
|
379
|
+
logger.debug('Pre-key upload already in progress, waiting for completion');
|
|
380
|
+
await uploadPreKeysPromise;
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
const uploadLogic = async retryCount => {
|
|
384
|
+
logger.info({
|
|
385
|
+
count,
|
|
386
|
+
retryCount
|
|
387
|
+
}, 'uploading pre-keys');
|
|
388
|
+
const node = await keys.transaction(async () => {
|
|
389
|
+
logger.debug({
|
|
390
|
+
requestedCount: count
|
|
391
|
+
}, 'generating pre-keys with requested count');
|
|
392
|
+
const {
|
|
393
|
+
update,
|
|
394
|
+
node
|
|
395
|
+
} = await getNextPreKeysNode({
|
|
396
|
+
creds,
|
|
397
|
+
keys
|
|
398
|
+
}, count);
|
|
399
|
+
ev.emit('creds.update', update);
|
|
400
|
+
return node;
|
|
401
|
+
}, creds?.me?.id || 'upload-pre-keys');
|
|
402
|
+
try {
|
|
403
|
+
await query(node);
|
|
404
|
+
logger.info({
|
|
405
|
+
count
|
|
406
|
+
}, 'uploaded pre-keys successfully');
|
|
407
|
+
} catch (uploadError) {
|
|
408
|
+
logger.error({
|
|
409
|
+
uploadError: uploadError.toString(),
|
|
410
|
+
count
|
|
411
|
+
}, 'Failed to upload pre-keys to server');
|
|
412
|
+
if (retryCount < 3) {
|
|
413
|
+
const backoffDelay = Math.min(1000 * Math.pow(2, retryCount), 10000);
|
|
414
|
+
logger.info(`Retrying pre-key upload in ${backoffDelay}ms`);
|
|
415
|
+
await new Promise(resolve => setTimeout(resolve, backoffDelay));
|
|
416
|
+
return uploadLogic(retryCount + 1);
|
|
498
417
|
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
lastDisconnect: {
|
|
502
|
-
error,
|
|
503
|
-
date: new Date()
|
|
504
|
-
}
|
|
505
|
-
});
|
|
506
|
-
ev.removeAllListeners('connection.update');
|
|
507
|
-
ev.destroy();
|
|
418
|
+
throw uploadError;
|
|
419
|
+
}
|
|
508
420
|
};
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
421
|
+
uploadPreKeysPromise = Promise.race([uploadLogic(0), new Promise((_, reject) => setTimeout(() => reject(new Boom('Pre-key upload timeout', {
|
|
422
|
+
statusCode: 408
|
|
423
|
+
})), UPLOAD_TIMEOUT))]);
|
|
424
|
+
try {
|
|
425
|
+
await uploadPreKeysPromise;
|
|
426
|
+
} finally {
|
|
427
|
+
uploadPreKeysPromise = null;
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
const verifyCurrentPreKeyExists = async () => {
|
|
431
|
+
const currentPreKeyId = creds.nextPreKeyId - 1;
|
|
432
|
+
if (currentPreKeyId <= 0) {
|
|
433
|
+
return {
|
|
434
|
+
exists: false,
|
|
435
|
+
currentPreKeyId: 0
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
const preKeys = await keys.get('pre-key', [currentPreKeyId.toString()]);
|
|
439
|
+
const exists = !!preKeys[currentPreKeyId.toString()];
|
|
440
|
+
return {
|
|
441
|
+
exists,
|
|
442
|
+
currentPreKeyId
|
|
529
443
|
};
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
444
|
+
};
|
|
445
|
+
const uploadPreKeysToServerIfRequired = async () => {
|
|
446
|
+
try {
|
|
447
|
+
let count = 0;
|
|
448
|
+
const preKeyCount = await getAvailablePreKeysOnServer();
|
|
449
|
+
if (preKeyCount === 0) count = INITIAL_PREKEY_COUNT;else count = MIN_PREKEY_COUNT;
|
|
450
|
+
const {
|
|
451
|
+
exists: currentPreKeyExists,
|
|
452
|
+
currentPreKeyId
|
|
453
|
+
} = await verifyCurrentPreKeyExists();
|
|
454
|
+
logger.info(`${preKeyCount} pre-keys found on server`);
|
|
455
|
+
logger.info(`Current prekey ID: ${currentPreKeyId}, exists in storage: ${currentPreKeyExists}`);
|
|
456
|
+
const lowServerCount = preKeyCount <= count;
|
|
457
|
+
const missingCurrentPreKey = !currentPreKeyExists && currentPreKeyId > 0;
|
|
458
|
+
const shouldUpload = lowServerCount || missingCurrentPreKey;
|
|
459
|
+
if (shouldUpload) {
|
|
460
|
+
const reasons = [];
|
|
461
|
+
if (lowServerCount) reasons.push(`server count low (${preKeyCount})`);
|
|
462
|
+
if (missingCurrentPreKey) reasons.push(`current prekey ${currentPreKeyId} missing from storage`);
|
|
463
|
+
logger.info(`Uploading PreKeys due to: ${reasons.join(', ')}`);
|
|
464
|
+
await uploadPreKeys(count);
|
|
465
|
+
} else {
|
|
466
|
+
logger.info(`PreKey validation passed - Server: ${preKeyCount}, Current prekey ${currentPreKeyId} exists`);
|
|
467
|
+
}
|
|
468
|
+
} catch (error) {
|
|
469
|
+
logger.error({
|
|
470
|
+
error
|
|
471
|
+
}, 'Failed to check/upload pre-keys during initialization');
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
const onMessageReceived = async data => {
|
|
475
|
+
await noise.decodeFrame(data, frame => {
|
|
476
|
+
lastDateRecv = new Date();
|
|
477
|
+
let anyTriggered = false;
|
|
478
|
+
anyTriggered = ws.emit('frame', frame);
|
|
479
|
+
if (!(frame instanceof Uint8Array)) {
|
|
480
|
+
const msgId = frame.attrs.id;
|
|
481
|
+
if (logger.level === 'trace') {
|
|
482
|
+
logger.trace({
|
|
483
|
+
xml: binaryNodeToString(frame),
|
|
484
|
+
msg: 'recv xml'
|
|
485
|
+
});
|
|
541
486
|
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
xmlns: 'w:p'
|
|
551
|
-
},
|
|
552
|
-
content: [{ tag: 'ping', attrs: {} }]
|
|
553
|
-
}).catch(err => {
|
|
554
|
-
logger.error({ trace: err.stack }, 'error in sending keep alive');
|
|
555
|
-
});
|
|
487
|
+
anyTriggered = ws.emit(`${DEF_TAG_PREFIX}${msgId}`, frame) || anyTriggered;
|
|
488
|
+
const l0 = frame.tag;
|
|
489
|
+
const l1 = frame.attrs || {};
|
|
490
|
+
const l2 = Array.isArray(frame.content) ? frame.content[0]?.tag : '';
|
|
491
|
+
for (const key of Object.keys(l1)) {
|
|
492
|
+
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0},${key}:${l1[key]},${l2}`, frame) || anyTriggered;
|
|
493
|
+
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0},${key}:${l1[key]}`, frame) || anyTriggered;
|
|
494
|
+
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0},${key}`, frame) || anyTriggered;
|
|
556
495
|
}
|
|
557
|
-
|
|
558
|
-
|
|
496
|
+
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0},,${l2}`, frame) || anyTriggered;
|
|
497
|
+
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0}`, frame) || anyTriggered;
|
|
498
|
+
if (!anyTriggered && logger.level === 'debug') {
|
|
499
|
+
logger.debug({
|
|
500
|
+
unhandled: true,
|
|
501
|
+
msgId,
|
|
502
|
+
fromMe: false,
|
|
503
|
+
frame
|
|
504
|
+
}, 'communication recv');
|
|
559
505
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
};
|
|
509
|
+
const end = async error => {
|
|
510
|
+
if (closed) {
|
|
511
|
+
logger.trace({
|
|
512
|
+
trace: error?.stack
|
|
513
|
+
}, 'connection already closed');
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
closed = true;
|
|
517
|
+
logger.info({
|
|
518
|
+
trace: error?.stack
|
|
519
|
+
}, error ? 'connection errored' : 'connection closed');
|
|
520
|
+
clearInterval(keepAliveReq);
|
|
521
|
+
clearTimeout(qrTimer);
|
|
522
|
+
ws.removeAllListeners('close');
|
|
523
|
+
ws.removeAllListeners('open');
|
|
524
|
+
ws.removeAllListeners('message');
|
|
525
|
+
signalRepository.close?.();
|
|
526
|
+
if (!ws.isClosed && !ws.isClosing) {
|
|
527
|
+
try {
|
|
528
|
+
await ws.close();
|
|
529
|
+
} catch {}
|
|
530
|
+
}
|
|
531
|
+
for (const handler of socketEndHandlers) {
|
|
532
|
+
try {
|
|
533
|
+
await handler(error);
|
|
534
|
+
} catch (err) {
|
|
535
|
+
logger.error({
|
|
536
|
+
err
|
|
537
|
+
}, 'error in socket end handler');
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
ev.emit('connection.update', {
|
|
541
|
+
connection: 'close',
|
|
542
|
+
lastDisconnect: {
|
|
543
|
+
error,
|
|
544
|
+
date: new Date()
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
ev.removeAllListeners('connection.update');
|
|
548
|
+
ev.destroy();
|
|
549
|
+
};
|
|
550
|
+
const waitForSocketOpen = async () => {
|
|
551
|
+
if (ws.isOpen) {
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
if (ws.isClosed || ws.isClosing) {
|
|
555
|
+
throw new Boom('Connection Closed', {
|
|
556
|
+
statusCode: DisconnectReason.connectionClosed
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
let onOpen;
|
|
560
|
+
let onClose;
|
|
561
|
+
await new Promise((resolve, reject) => {
|
|
562
|
+
onOpen = () => resolve(undefined);
|
|
563
|
+
onClose = mapWebSocketError(reject);
|
|
564
|
+
ws.on('open', onOpen);
|
|
565
|
+
ws.on('close', onClose);
|
|
566
|
+
ws.on('error', onClose);
|
|
567
|
+
}).finally(() => {
|
|
568
|
+
ws.off('open', onOpen);
|
|
569
|
+
ws.off('close', onClose);
|
|
570
|
+
ws.off('error', onClose);
|
|
571
|
+
});
|
|
572
|
+
};
|
|
573
|
+
const startKeepAliveRequest = () => keepAliveReq = setInterval(() => {
|
|
574
|
+
if (!lastDateRecv) {
|
|
575
|
+
lastDateRecv = new Date();
|
|
576
|
+
}
|
|
577
|
+
const diff = Date.now() - lastDateRecv.getTime();
|
|
578
|
+
if (diff > keepAliveIntervalMs + 5000) {
|
|
579
|
+
void end(new Boom('Connection was lost', {
|
|
580
|
+
statusCode: DisconnectReason.connectionLost
|
|
581
|
+
}));
|
|
582
|
+
} else if (ws.isOpen) {
|
|
583
|
+
query({
|
|
563
584
|
tag: 'iq',
|
|
564
585
|
attrs: {
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
586
|
+
id: generateMessageTag(),
|
|
587
|
+
to: S_WHATSAPP_NET,
|
|
588
|
+
type: 'get',
|
|
589
|
+
xmlns: 'w:p'
|
|
568
590
|
},
|
|
569
|
-
content: [{
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
591
|
+
content: [{
|
|
592
|
+
tag: 'ping',
|
|
593
|
+
attrs: {}
|
|
594
|
+
}]
|
|
595
|
+
}).catch(err => {
|
|
596
|
+
logger.error({
|
|
597
|
+
trace: err.stack
|
|
598
|
+
}, 'error in sending keep alive');
|
|
599
|
+
});
|
|
600
|
+
} else {
|
|
601
|
+
logger.warn('keep alive called when WS not open');
|
|
602
|
+
}
|
|
603
|
+
}, keepAliveIntervalMs);
|
|
604
|
+
const sendPassiveIq = tag => query({
|
|
605
|
+
tag: 'iq',
|
|
606
|
+
attrs: {
|
|
607
|
+
to: S_WHATSAPP_NET,
|
|
608
|
+
xmlns: 'passive',
|
|
609
|
+
type: 'set'
|
|
610
|
+
},
|
|
611
|
+
content: [{
|
|
612
|
+
tag,
|
|
613
|
+
attrs: {}
|
|
614
|
+
}]
|
|
615
|
+
});
|
|
616
|
+
const logout = async msg => {
|
|
617
|
+
const jid = authState.creds.me?.id;
|
|
618
|
+
if (jid) {
|
|
619
|
+
await sendNode({
|
|
620
|
+
tag: 'iq',
|
|
621
|
+
attrs: {
|
|
622
|
+
to: S_WHATSAPP_NET,
|
|
623
|
+
type: 'set',
|
|
624
|
+
id: generateMessageTag(),
|
|
625
|
+
xmlns: 'md'
|
|
626
|
+
},
|
|
627
|
+
content: [{
|
|
628
|
+
tag: 'remove-companion-device',
|
|
629
|
+
attrs: {
|
|
630
|
+
jid,
|
|
631
|
+
reason: 'user_initiated'
|
|
632
|
+
}
|
|
633
|
+
}]
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
void end(new Boom(msg || 'Intentional Logout', {
|
|
637
|
+
statusCode: DisconnectReason.loggedOut
|
|
638
|
+
}));
|
|
639
|
+
};
|
|
640
|
+
const requestPairingCode = async (phoneNumber, customPairingCode) => {
|
|
641
|
+
const pairingCode = customPairingCode ?? bytesToCrockford(randomBytes(5));
|
|
642
|
+
if (customPairingCode && customPairingCode?.length !== 8) {
|
|
643
|
+
throw new Error('Custom pairing code must be exactly 8 chars');
|
|
644
|
+
}
|
|
645
|
+
authState.creds.pairingCode = pairingCode;
|
|
646
|
+
authState.creds.me = {
|
|
647
|
+
id: jidEncode(phoneNumber, 's.whatsapp.net'),
|
|
648
|
+
name: '~'
|
|
595
649
|
};
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
650
|
+
ev.emit('creds.update', authState.creds);
|
|
651
|
+
await sendNode({
|
|
652
|
+
tag: 'iq',
|
|
653
|
+
attrs: {
|
|
654
|
+
to: S_WHATSAPP_NET,
|
|
655
|
+
type: 'set',
|
|
656
|
+
id: generateMessageTag(),
|
|
657
|
+
xmlns: 'md'
|
|
658
|
+
},
|
|
659
|
+
content: [{
|
|
660
|
+
tag: 'link_code_companion_reg',
|
|
661
|
+
attrs: {
|
|
662
|
+
jid: authState.creds.me.id,
|
|
663
|
+
stage: 'companion_hello',
|
|
664
|
+
should_show_push_notification: 'true'
|
|
665
|
+
},
|
|
666
|
+
content: [{
|
|
667
|
+
tag: 'link_code_pairing_wrapped_companion_ephemeral_pub',
|
|
668
|
+
attrs: {},
|
|
669
|
+
content: await generatePairingKey()
|
|
670
|
+
}, {
|
|
671
|
+
tag: 'companion_server_auth_key_pub',
|
|
672
|
+
attrs: {},
|
|
673
|
+
content: authState.creds.noiseKey.public
|
|
674
|
+
}, {
|
|
675
|
+
tag: 'companion_platform_id',
|
|
676
|
+
attrs: {},
|
|
677
|
+
content: getCompanionPlatformId(browser)
|
|
678
|
+
}, {
|
|
679
|
+
tag: 'companion_platform_display',
|
|
680
|
+
attrs: {},
|
|
681
|
+
content: `${browser[1]} (${browser[0]})`
|
|
682
|
+
}, {
|
|
683
|
+
tag: 'link_code_pairing_nonce',
|
|
684
|
+
attrs: {},
|
|
685
|
+
content: '0'
|
|
686
|
+
}]
|
|
687
|
+
}]
|
|
688
|
+
});
|
|
689
|
+
return authState.creds.pairingCode;
|
|
690
|
+
};
|
|
691
|
+
async function generatePairingKey() {
|
|
692
|
+
const salt = randomBytes(32);
|
|
693
|
+
const randomIv = randomBytes(16);
|
|
694
|
+
const key = await derivePairingCodeKey(authState.creds.pairingCode, salt);
|
|
695
|
+
const ciphered = aesEncryptCTR(authState.creds.pairingEphemeralKeyPair.public, key, randomIv);
|
|
696
|
+
return Buffer.concat([salt, randomIv, ciphered]);
|
|
697
|
+
}
|
|
698
|
+
const sendWAMBuffer = wamBuffer => {
|
|
699
|
+
return query({
|
|
700
|
+
tag: 'iq',
|
|
701
|
+
attrs: {
|
|
702
|
+
to: S_WHATSAPP_NET,
|
|
703
|
+
id: generateMessageTag(),
|
|
704
|
+
xmlns: 'w:stats'
|
|
705
|
+
},
|
|
706
|
+
content: [{
|
|
707
|
+
tag: 'add',
|
|
708
|
+
attrs: {
|
|
709
|
+
t: Math.round(Date.now() / 1000) + ''
|
|
710
|
+
},
|
|
711
|
+
content: wamBuffer
|
|
712
|
+
}]
|
|
713
|
+
});
|
|
714
|
+
};
|
|
715
|
+
ws.on('message', onMessageReceived);
|
|
716
|
+
ws.on('open', async () => {
|
|
717
|
+
try {
|
|
718
|
+
await validateConnection();
|
|
719
|
+
} catch (err) {
|
|
720
|
+
logger.error({
|
|
721
|
+
err
|
|
722
|
+
}, 'error in validating connection');
|
|
723
|
+
void end(err);
|
|
724
|
+
}
|
|
725
|
+
});
|
|
726
|
+
ws.on('error', mapWebSocketError(end));
|
|
727
|
+
ws.on('close', () => void end(new Boom('Connection Terminated', {
|
|
728
|
+
statusCode: DisconnectReason.connectionClosed
|
|
729
|
+
})));
|
|
730
|
+
ws.on('CB:xmlstreamend', () => void end(new Boom('Connection Terminated by Server', {
|
|
731
|
+
statusCode: DisconnectReason.connectionClosed
|
|
732
|
+
})));
|
|
733
|
+
ws.on('CB:iq,type:set,pair-device', async stanza => {
|
|
734
|
+
const iq = {
|
|
735
|
+
tag: 'iq',
|
|
736
|
+
attrs: {
|
|
737
|
+
to: S_WHATSAPP_NET,
|
|
738
|
+
type: 'result',
|
|
739
|
+
id: stanza.attrs.id
|
|
740
|
+
}
|
|
654
741
|
};
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
const
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
742
|
+
await sendNode(iq);
|
|
743
|
+
const pairDeviceNode = getBinaryNodeChild(stanza, 'pair-device');
|
|
744
|
+
const refNodes = getBinaryNodeChildren(pairDeviceNode, 'ref');
|
|
745
|
+
const noiseKeyB64 = Buffer.from(creds.noiseKey.public).toString('base64');
|
|
746
|
+
const identityKeyB64 = Buffer.from(creds.signedIdentityKey.public).toString('base64');
|
|
747
|
+
const advB64 = creds.advSecretKey;
|
|
748
|
+
let qrMs = qrTimeout || 60000;
|
|
749
|
+
const genPairQR = () => {
|
|
750
|
+
if (!ws.isOpen) {
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
const refNode = refNodes.shift();
|
|
754
|
+
if (!refNode) {
|
|
755
|
+
void end(new Boom('QR refs attempts ended', {
|
|
756
|
+
statusCode: DisconnectReason.timedOut
|
|
757
|
+
}));
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
const ref = refNode.content.toString('utf-8');
|
|
761
|
+
const qr = buildPairingQRData(ref, noiseKeyB64, identityKeyB64, advB64, browser);
|
|
762
|
+
ev.emit('connection.update', {
|
|
763
|
+
qr
|
|
764
|
+
});
|
|
765
|
+
qrTimer = setTimeout(genPairQR, qrMs);
|
|
766
|
+
qrMs = qrTimeout || 20000;
|
|
678
767
|
};
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
768
|
+
genPairQR();
|
|
769
|
+
});
|
|
770
|
+
ws.on('CB:iq,,pair-success', async stanza => {
|
|
771
|
+
logger.debug('pair success recv');
|
|
772
|
+
try {
|
|
773
|
+
updateServerTimeOffset(stanza);
|
|
774
|
+
const {
|
|
775
|
+
reply,
|
|
776
|
+
creds: updatedCreds
|
|
777
|
+
} = configureSuccessfulPairing(stanza, creds);
|
|
778
|
+
logger.info({
|
|
779
|
+
me: updatedCreds.me,
|
|
780
|
+
platform: updatedCreds.platform
|
|
781
|
+
}, 'pairing configured successfully, expect to restart the connection...');
|
|
782
|
+
ev.emit('creds.update', updatedCreds);
|
|
783
|
+
ev.emit('connection.update', {
|
|
784
|
+
isNewLogin: true,
|
|
785
|
+
qr: undefined
|
|
786
|
+
});
|
|
787
|
+
await sendNode(reply);
|
|
788
|
+
void sendUnifiedSession();
|
|
789
|
+
} catch (error) {
|
|
790
|
+
logger.info({
|
|
791
|
+
trace: error.stack
|
|
792
|
+
}, 'error in pairing');
|
|
793
|
+
void end(error);
|
|
794
|
+
}
|
|
795
|
+
});
|
|
796
|
+
ws.on('CB:success', async node => {
|
|
797
|
+
try {
|
|
798
|
+
updateServerTimeOffset(node);
|
|
799
|
+
await uploadPreKeysToServerIfRequired();
|
|
800
|
+
await sendPassiveIq('active');
|
|
801
|
+
try {
|
|
802
|
+
await digestKeyBundle();
|
|
803
|
+
} catch (e) {
|
|
804
|
+
logger.warn({
|
|
805
|
+
e
|
|
806
|
+
}, 'failed to run digest after login');
|
|
807
|
+
}
|
|
808
|
+
} catch (err) {
|
|
809
|
+
logger.warn({
|
|
810
|
+
err
|
|
811
|
+
}, 'failed to send initial passive iq');
|
|
812
|
+
}
|
|
813
|
+
logger.info('opened connection to WA');
|
|
814
|
+
clearTimeout(qrTimer);
|
|
815
|
+
ev.emit('creds.update', {
|
|
816
|
+
me: {
|
|
817
|
+
...authState.creds.me,
|
|
818
|
+
lid: node.attrs.lid
|
|
819
|
+
}
|
|
726
820
|
});
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
ws.on('CB:iq,,pair-success', async (stanza) => {
|
|
730
|
-
logger.debug('pair success recv');
|
|
731
|
-
try {
|
|
732
|
-
updateServerTimeOffset(stanza);
|
|
733
|
-
const { reply, creds: updatedCreds } = configureSuccessfulPairing(stanza, creds);
|
|
734
|
-
logger.info({ me: updatedCreds.me, platform: updatedCreds.platform }, 'pairing configured successfully, expect to restart the connection...');
|
|
735
|
-
ev.emit('creds.update', updatedCreds);
|
|
736
|
-
ev.emit('connection.update', { isNewLogin: true, qr: undefined });
|
|
737
|
-
await sendNode(reply);
|
|
738
|
-
void sendUnifiedSession();
|
|
739
|
-
}
|
|
740
|
-
catch (error) {
|
|
741
|
-
logger.info({ trace: error.stack }, 'error in pairing');
|
|
742
|
-
void end(error);
|
|
743
|
-
}
|
|
821
|
+
ev.emit('connection.update', {
|
|
822
|
+
connection: 'open'
|
|
744
823
|
});
|
|
745
|
-
|
|
746
|
-
|
|
824
|
+
void sendUnifiedSession();
|
|
825
|
+
if (node.attrs.lid && authState.creds.me?.id) {
|
|
826
|
+
const myLID = node.attrs.lid;
|
|
827
|
+
process.nextTick(async () => {
|
|
747
828
|
try {
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
829
|
+
const myPN = authState.creds.me.id;
|
|
830
|
+
await signalRepository.lidMapping.storeLIDPNMappings([{
|
|
831
|
+
lid: myLID,
|
|
832
|
+
pn: myPN
|
|
833
|
+
}]);
|
|
834
|
+
const {
|
|
835
|
+
user,
|
|
836
|
+
device
|
|
837
|
+
} = jidDecode(myPN);
|
|
838
|
+
await authState.keys.set({
|
|
839
|
+
'device-list': {
|
|
840
|
+
[user]: [device?.toString() || '0']
|
|
754
841
|
}
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
void sendUnifiedSession();
|
|
767
|
-
if (node.attrs.lid && authState.creds.me?.id) {
|
|
768
|
-
const myLID = node.attrs.lid;
|
|
769
|
-
process.nextTick(async () => {
|
|
770
|
-
try {
|
|
771
|
-
const myPN = authState.creds.me.id;
|
|
772
|
-
// Store our own LID-PN mapping
|
|
773
|
-
await signalRepository.lidMapping.storeLIDPNMappings([{ lid: myLID, pn: myPN }]);
|
|
774
|
-
// Create device list for our own user (needed for bulk migration)
|
|
775
|
-
const { user, device } = jidDecode(myPN);
|
|
776
|
-
await authState.keys.set({
|
|
777
|
-
'device-list': {
|
|
778
|
-
[user]: [device?.toString() || '0']
|
|
779
|
-
}
|
|
780
|
-
});
|
|
781
|
-
// migrate our own session
|
|
782
|
-
await signalRepository.migrateSession(myPN, myLID);
|
|
783
|
-
logger.info({ myPN, myLID }, 'Own LID session created successfully');
|
|
784
|
-
}
|
|
785
|
-
catch (error) {
|
|
786
|
-
logger.error({ error, lid: myLID }, 'Failed to create own LID session');
|
|
787
|
-
}
|
|
788
|
-
});
|
|
842
|
+
});
|
|
843
|
+
await signalRepository.migrateSession(myPN, myLID);
|
|
844
|
+
logger.info({
|
|
845
|
+
myPN,
|
|
846
|
+
myLID
|
|
847
|
+
}, 'Own LID session created successfully');
|
|
848
|
+
} catch (error) {
|
|
849
|
+
logger.error({
|
|
850
|
+
error,
|
|
851
|
+
lid: myLID
|
|
852
|
+
}, 'Failed to create own LID session');
|
|
789
853
|
}
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
});
|
|
857
|
+
ws.on('CB:stream:error', node => {
|
|
858
|
+
const [reasonNode] = getAllBinaryNodeChildren(node);
|
|
859
|
+
logger.error({
|
|
860
|
+
reasonNode,
|
|
861
|
+
fullErrorNode: node
|
|
862
|
+
}, 'stream errored out');
|
|
863
|
+
const {
|
|
864
|
+
reason,
|
|
865
|
+
statusCode
|
|
866
|
+
} = getErrorCodeFromStreamError(node);
|
|
867
|
+
void end(new Boom(`Stream Errored (${reason})`, {
|
|
868
|
+
statusCode,
|
|
869
|
+
data: reasonNode || node
|
|
870
|
+
}));
|
|
871
|
+
});
|
|
872
|
+
ws.on('CB:failure', node => {
|
|
873
|
+
const reason = +(node.attrs.reason || 500);
|
|
874
|
+
void end(new Boom('Connection Failure', {
|
|
875
|
+
statusCode: reason,
|
|
876
|
+
data: node.attrs
|
|
877
|
+
}));
|
|
878
|
+
});
|
|
879
|
+
ws.on('CB:ib,,downgrade_webclient', () => {
|
|
880
|
+
void end(new Boom('Multi-device beta not joined', {
|
|
881
|
+
statusCode: DisconnectReason.multideviceMismatch
|
|
882
|
+
}));
|
|
883
|
+
});
|
|
884
|
+
ws.on('CB:ib,,offline_preview', async node => {
|
|
885
|
+
logger.info('offline preview received', JSON.stringify(node));
|
|
886
|
+
await sendNode({
|
|
887
|
+
tag: 'ib',
|
|
888
|
+
attrs: {},
|
|
889
|
+
content: [{
|
|
890
|
+
tag: 'offline_batch',
|
|
891
|
+
attrs: {
|
|
892
|
+
count: '100'
|
|
828
893
|
}
|
|
829
|
-
|
|
894
|
+
}]
|
|
830
895
|
});
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
896
|
+
});
|
|
897
|
+
ws.on('CB:ib,,edge_routing', node => {
|
|
898
|
+
const edgeRoutingNode = getBinaryNodeChild(node, 'edge_routing');
|
|
899
|
+
const routingInfo = getBinaryNodeChild(edgeRoutingNode, 'routing_info');
|
|
900
|
+
if (routingInfo?.content) {
|
|
901
|
+
authState.creds.routingInfo = Buffer.from(routingInfo?.content);
|
|
902
|
+
ev.emit('creds.update', authState.creds);
|
|
903
|
+
}
|
|
904
|
+
});
|
|
905
|
+
let didStartBuffer = false;
|
|
906
|
+
process.nextTick(() => {
|
|
907
|
+
if (creds.me?.id) {
|
|
908
|
+
ev.buffer();
|
|
909
|
+
didStartBuffer = true;
|
|
910
|
+
}
|
|
911
|
+
ev.emit('connection.update', {
|
|
912
|
+
connection: 'connecting',
|
|
913
|
+
receivedPendingNotifications: false,
|
|
914
|
+
qr: undefined
|
|
841
915
|
});
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
});
|
|
854
|
-
}
|
|
855
|
-
Object.assign(creds, update);
|
|
916
|
+
});
|
|
917
|
+
ws.on('CB:ib,,offline', node => {
|
|
918
|
+
const child = getBinaryNodeChild(node, 'offline');
|
|
919
|
+
const offlineNotifs = +(child?.attrs.count || 0);
|
|
920
|
+
logger.info(`handled ${offlineNotifs} offline messages/notifications`);
|
|
921
|
+
if (didStartBuffer) {
|
|
922
|
+
ev.flush();
|
|
923
|
+
logger.trace('flushed events for initial buffer');
|
|
924
|
+
}
|
|
925
|
+
ev.emit('connection.update', {
|
|
926
|
+
receivedPendingNotifications: true
|
|
856
927
|
});
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
logger.debug({ offset: serverTimeOffsetMs }, 'calculated server time offset');
|
|
869
|
-
};
|
|
870
|
-
const getUnifiedSessionId = () => {
|
|
871
|
-
const offsetMs = 3 * TimeMs.Day;
|
|
872
|
-
const now = Date.now() + serverTimeOffsetMs;
|
|
873
|
-
const id = (now + offsetMs) % TimeMs.Week;
|
|
874
|
-
return id.toString();
|
|
875
|
-
};
|
|
876
|
-
const sendUnifiedSession = async () => {
|
|
877
|
-
if (!ws.isOpen) {
|
|
878
|
-
return;
|
|
879
|
-
}
|
|
880
|
-
const node = {
|
|
881
|
-
tag: 'ib',
|
|
882
|
-
attrs: {},
|
|
883
|
-
content: [
|
|
884
|
-
{
|
|
885
|
-
tag: 'unified_session',
|
|
886
|
-
attrs: {
|
|
887
|
-
id: getUnifiedSessionId()
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
]
|
|
891
|
-
};
|
|
892
|
-
try {
|
|
893
|
-
await sendNode(node);
|
|
928
|
+
});
|
|
929
|
+
ev.on('creds.update', update => {
|
|
930
|
+
const name = update.me?.name;
|
|
931
|
+
if (creds.me?.name !== name) {
|
|
932
|
+
logger.debug({
|
|
933
|
+
name
|
|
934
|
+
}, 'updated pushName');
|
|
935
|
+
sendNode({
|
|
936
|
+
tag: 'presence',
|
|
937
|
+
attrs: {
|
|
938
|
+
name: name
|
|
894
939
|
}
|
|
895
|
-
|
|
896
|
-
|
|
940
|
+
}).catch(err => {
|
|
941
|
+
logger.warn({
|
|
942
|
+
trace: err.stack
|
|
943
|
+
}, 'error in sending presence update on name change');
|
|
944
|
+
});
|
|
945
|
+
}
|
|
946
|
+
Object.assign(creds, update);
|
|
947
|
+
});
|
|
948
|
+
const updateServerTimeOffset = ({
|
|
949
|
+
attrs
|
|
950
|
+
}) => {
|
|
951
|
+
const tValue = attrs?.t;
|
|
952
|
+
if (!tValue) {
|
|
953
|
+
return;
|
|
954
|
+
}
|
|
955
|
+
const parsed = Number(tValue);
|
|
956
|
+
if (Number.isNaN(parsed) || parsed <= 0) {
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
959
|
+
const localMs = Date.now();
|
|
960
|
+
serverTimeOffsetMs = parsed * 1000 - localMs;
|
|
961
|
+
logger.debug({
|
|
962
|
+
offset: serverTimeOffsetMs
|
|
963
|
+
}, 'calculated server time offset');
|
|
964
|
+
};
|
|
965
|
+
const getUnifiedSessionId = () => {
|
|
966
|
+
const offsetMs = 3 * TimeMs.Day;
|
|
967
|
+
const now = Date.now() + serverTimeOffsetMs;
|
|
968
|
+
const id = (now + offsetMs) % TimeMs.Week;
|
|
969
|
+
return id.toString();
|
|
970
|
+
};
|
|
971
|
+
const sendUnifiedSession = async () => {
|
|
972
|
+
if (!ws.isOpen) {
|
|
973
|
+
return;
|
|
974
|
+
}
|
|
975
|
+
const node = {
|
|
976
|
+
tag: 'ib',
|
|
977
|
+
attrs: {},
|
|
978
|
+
content: [{
|
|
979
|
+
tag: 'unified_session',
|
|
980
|
+
attrs: {
|
|
981
|
+
id: getUnifiedSessionId()
|
|
897
982
|
}
|
|
983
|
+
}]
|
|
898
984
|
};
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
}
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
}
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
uploadPreKeysToServerIfRequired,
|
|
946
|
-
digestKeyBundle,
|
|
947
|
-
rotateSignedPreKey,
|
|
948
|
-
requestPairingCode,
|
|
949
|
-
updateServerTimeOffset,
|
|
950
|
-
sendUnifiedSession,
|
|
951
|
-
wamBuffer: publicWAMBuffer,
|
|
952
|
-
/** Waits for the connection to WA to reach a state */
|
|
953
|
-
waitForConnectionUpdate: bindWaitForConnectionUpdate(ev),
|
|
954
|
-
sendWAMBuffer,
|
|
955
|
-
executeUSyncQuery,
|
|
956
|
-
onWhatsApp,
|
|
957
|
-
fetchAccountReachoutTimelock,
|
|
958
|
-
fetchNewChatMessageCap
|
|
959
|
-
};
|
|
985
|
+
try {
|
|
986
|
+
await sendNode(node);
|
|
987
|
+
} catch (error) {
|
|
988
|
+
logger.debug({
|
|
989
|
+
error
|
|
990
|
+
}, 'failed to send unified_session telemetry');
|
|
991
|
+
}
|
|
992
|
+
};
|
|
993
|
+
const registerSocketEndHandler = handler => {
|
|
994
|
+
socketEndHandlers.push(handler);
|
|
995
|
+
};
|
|
996
|
+
return {
|
|
997
|
+
type: 'md',
|
|
998
|
+
ws,
|
|
999
|
+
ev,
|
|
1000
|
+
authState: {
|
|
1001
|
+
creds,
|
|
1002
|
+
keys
|
|
1003
|
+
},
|
|
1004
|
+
signalRepository,
|
|
1005
|
+
get user() {
|
|
1006
|
+
return authState.creds.me;
|
|
1007
|
+
},
|
|
1008
|
+
generateMessageTag,
|
|
1009
|
+
query,
|
|
1010
|
+
waitForMessage,
|
|
1011
|
+
waitForSocketOpen,
|
|
1012
|
+
sendRawMessage,
|
|
1013
|
+
sendNode,
|
|
1014
|
+
logout,
|
|
1015
|
+
end,
|
|
1016
|
+
registerSocketEndHandler,
|
|
1017
|
+
onUnexpectedError,
|
|
1018
|
+
uploadPreKeys,
|
|
1019
|
+
uploadPreKeysToServerIfRequired,
|
|
1020
|
+
digestKeyBundle,
|
|
1021
|
+
rotateSignedPreKey,
|
|
1022
|
+
requestPairingCode,
|
|
1023
|
+
updateServerTimeOffset,
|
|
1024
|
+
sendUnifiedSession,
|
|
1025
|
+
wamBuffer: publicWAMBuffer,
|
|
1026
|
+
waitForConnectionUpdate: bindWaitForConnectionUpdate(ev),
|
|
1027
|
+
sendWAMBuffer,
|
|
1028
|
+
executeUSyncQuery,
|
|
1029
|
+
onWhatsApp
|
|
1030
|
+
};
|
|
960
1031
|
};
|
|
961
|
-
/**
|
|
962
|
-
* map the websocket error to the right type
|
|
963
|
-
* so it can be retried by the caller
|
|
964
|
-
* */
|
|
965
1032
|
function mapWebSocketError(handler) {
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
1033
|
+
return error => {
|
|
1034
|
+
handler(new Boom(`WebSocket Error (${error?.message})`, {
|
|
1035
|
+
statusCode: getCodeFromWSError(error),
|
|
1036
|
+
data: error
|
|
1037
|
+
}));
|
|
1038
|
+
};
|
|
969
1039
|
}
|
|
970
|
-
//# sourceMappingURL=socket.js.map
|