@mrlegendbot/baileys 1.2.1 → 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/Utils/generics.js
CHANGED
|
@@ -1,393 +1,370 @@
|
|
|
1
1
|
import { Boom } from '@hapi/boom';
|
|
2
2
|
import { createHash, randomBytes } from 'crypto';
|
|
3
|
-
import { proto } from '../../WAProto/
|
|
4
|
-
const baileysVersion = [2, 3000,
|
|
3
|
+
import { proto } from '../../WAProto/compiler.js';
|
|
4
|
+
const baileysVersion = [2, 3000, 1035194821];
|
|
5
5
|
import { DisconnectReason } from '../Types/index.js';
|
|
6
6
|
import { getAllBinaryNodeChildren, jidDecode } from '../WABinary/index.js';
|
|
7
7
|
import { sha256 } from './crypto.js';
|
|
8
8
|
export const BufferJSON = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
9
|
+
replacer: (k, value) => {
|
|
10
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === 'Buffer') {
|
|
11
|
+
return {
|
|
12
|
+
type: 'Buffer',
|
|
13
|
+
data: Buffer.from(value?.data || value).toString('base64')
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
},
|
|
18
|
+
reviver: (_, value) => {
|
|
19
|
+
if (typeof value === 'object' && value !== null && value.type === 'Buffer' && typeof value.data === 'string') {
|
|
20
|
+
return Buffer.from(value.data, 'base64');
|
|
21
|
+
}
|
|
22
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
23
|
+
const keys = Object.keys(value);
|
|
24
|
+
if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {
|
|
25
|
+
const values = Object.values(value);
|
|
26
|
+
if (values.every(v => typeof v === 'number')) {
|
|
27
|
+
return Buffer.from(values);
|
|
29
28
|
}
|
|
30
|
-
|
|
29
|
+
}
|
|
31
30
|
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
32
33
|
};
|
|
33
34
|
export const getKeyAuthor = (key, meId = 'me') => (key?.fromMe ? meId : key?.participantAlt || key?.remoteJidAlt || key?.participant || key?.remoteJid) || '';
|
|
34
|
-
export const isStringNullOrEmpty =
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const padLength = (pad[0] & 0x0f) + 1;
|
|
40
|
-
return Buffer.concat([msg, Buffer.alloc(padLength, padLength)]);
|
|
35
|
+
export const isStringNullOrEmpty = value => value == null || value === '';
|
|
36
|
+
export const writeRandomPadMax16 = msg => {
|
|
37
|
+
const pad = randomBytes(1);
|
|
38
|
+
const padLength = (pad[0] & 0x0f) + 1;
|
|
39
|
+
return Buffer.concat([msg, Buffer.alloc(padLength, padLength)]);
|
|
41
40
|
};
|
|
42
|
-
export const unpadRandomMax16 =
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
41
|
+
export const unpadRandomMax16 = e => {
|
|
42
|
+
const t = new Uint8Array(e);
|
|
43
|
+
if (0 === t.length) {
|
|
44
|
+
throw new Error('unpadPkcs7 given empty bytes');
|
|
45
|
+
}
|
|
46
|
+
var r = t[t.length - 1];
|
|
47
|
+
if (r > t.length) {
|
|
48
|
+
throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`);
|
|
49
|
+
}
|
|
50
|
+
return new Uint8Array(t.buffer, t.byteOffset, t.length - r);
|
|
52
51
|
};
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return '2:' + sha256Hash.slice(0, 6);
|
|
52
|
+
export const generateParticipantHashV2 = participants => {
|
|
53
|
+
participants.sort();
|
|
54
|
+
const sha256Hash = sha256(Buffer.from(participants.join(''))).toString('base64');
|
|
55
|
+
return '2:' + sha256Hash.slice(0, 6);
|
|
58
56
|
};
|
|
59
|
-
export const encodeWAMessage =
|
|
57
|
+
export const encodeWAMessage = message => writeRandomPadMax16(proto.Message.encode(message).finish());
|
|
60
58
|
export const generateRegistrationId = () => {
|
|
61
|
-
|
|
59
|
+
return Uint16Array.from(randomBytes(2))[0] & 16383;
|
|
62
60
|
};
|
|
63
61
|
export const encodeBigEndian = (e, t = 4) => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
62
|
+
let r = e;
|
|
63
|
+
const a = new Uint8Array(t);
|
|
64
|
+
for (let i = t - 1; i >= 0; i--) {
|
|
65
|
+
a[i] = 255 & r;
|
|
66
|
+
r >>>= 8;
|
|
67
|
+
}
|
|
68
|
+
return a;
|
|
71
69
|
};
|
|
72
|
-
export const toNumber =
|
|
73
|
-
/** unix timestamp of a date in seconds */
|
|
70
|
+
export const toNumber = t => typeof t === 'object' && t ? 'toNumber' in t ? t.toNumber() : t.low : t || 0;
|
|
74
71
|
export const unixTimestampSeconds = (date = new Date()) => Math.floor(date.getTime() / 1000);
|
|
75
72
|
export const debouncedTimeout = (intervalMs = 1000, task) => {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
73
|
+
let timeout;
|
|
74
|
+
return {
|
|
75
|
+
start: (newIntervalMs, newTask) => {
|
|
76
|
+
task = newTask || task;
|
|
77
|
+
intervalMs = newIntervalMs || intervalMs;
|
|
78
|
+
timeout && clearTimeout(timeout);
|
|
79
|
+
timeout = setTimeout(() => task?.(), intervalMs);
|
|
80
|
+
},
|
|
81
|
+
cancel: () => {
|
|
82
|
+
timeout && clearTimeout(timeout);
|
|
83
|
+
timeout = undefined;
|
|
84
|
+
},
|
|
85
|
+
setTask: newTask => task = newTask,
|
|
86
|
+
setInterval: newInterval => intervalMs = newInterval
|
|
87
|
+
};
|
|
91
88
|
};
|
|
92
|
-
export const delay =
|
|
93
|
-
export const delayCancellable =
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
89
|
+
export const delay = ms => delayCancellable(ms).delay;
|
|
90
|
+
export const delayCancellable = ms => {
|
|
91
|
+
const stack = new Error().stack;
|
|
92
|
+
let timeout;
|
|
93
|
+
let reject;
|
|
94
|
+
const delay = new Promise((resolve, _reject) => {
|
|
95
|
+
timeout = setTimeout(resolve, ms);
|
|
96
|
+
reject = _reject;
|
|
97
|
+
});
|
|
98
|
+
const cancel = () => {
|
|
99
|
+
clearTimeout(timeout);
|
|
100
|
+
reject(new Boom('Cancelled', {
|
|
101
|
+
statusCode: 500,
|
|
102
|
+
data: {
|
|
103
|
+
stack
|
|
104
|
+
}
|
|
105
|
+
}));
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
delay,
|
|
109
|
+
cancel
|
|
110
|
+
};
|
|
111
111
|
};
|
|
112
112
|
export async function promiseTimeout(ms, promise) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
113
|
+
if (!ms) {
|
|
114
|
+
return new Promise(promise);
|
|
115
|
+
}
|
|
116
|
+
const stack = new Error().stack;
|
|
117
|
+
const {
|
|
118
|
+
delay,
|
|
119
|
+
cancel
|
|
120
|
+
} = delayCancellable(ms);
|
|
121
|
+
const p = new Promise((resolve, reject) => {
|
|
122
|
+
delay.then(() => reject(new Boom('Timed Out', {
|
|
123
|
+
statusCode: DisconnectReason.timedOut,
|
|
124
|
+
data: {
|
|
125
|
+
stack
|
|
126
|
+
}
|
|
127
|
+
}))).catch(err => reject(err));
|
|
128
|
+
promise(resolve, reject);
|
|
129
|
+
}).finally(cancel);
|
|
130
|
+
return p;
|
|
131
131
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
data.write(id.user, 8);
|
|
141
|
-
data.write('@c.us', 8 + id.user.length);
|
|
142
|
-
}
|
|
132
|
+
export const generateMessageIDV2 = userId => {
|
|
133
|
+
const data = Buffer.alloc(8 + 20 + 16);
|
|
134
|
+
data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)));
|
|
135
|
+
if (userId) {
|
|
136
|
+
const id = jidDecode(userId);
|
|
137
|
+
if (id?.user) {
|
|
138
|
+
data.write(id.user, 8);
|
|
139
|
+
data.write('@c.us', 8 + id.user.length);
|
|
143
140
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
141
|
+
}
|
|
142
|
+
const random = randomBytes(16);
|
|
143
|
+
random.copy(data, 28);
|
|
144
|
+
const hash = createHash('sha256').update(data).digest();
|
|
145
|
+
return '3EB0' + hash.toString('hex').toUpperCase().substring(0, 18);
|
|
148
146
|
};
|
|
149
|
-
// generate a random ID to attach to a message
|
|
150
147
|
export const generateMessageID = () => '3EB0' + randomBytes(18).toString('hex').toUpperCase();
|
|
151
148
|
export function bindWaitForEvent(ev, event) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
resolve();
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
ev.on(event, listener);
|
|
168
|
-
}).finally(() => {
|
|
169
|
-
ev.off(event, listener);
|
|
170
|
-
ev.off('connection.update', closeListener);
|
|
171
|
-
});
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
export const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update');
|
|
175
|
-
/**
|
|
176
|
-
* utility that fetches latest baileys version from the master branch.
|
|
177
|
-
* Use to ensure your WA connection is always on the latest version
|
|
178
|
-
*/
|
|
179
|
-
export const fetchLatestBaileysVersion = async (options = {}) => {
|
|
180
|
-
const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/index.ts';
|
|
181
|
-
try {
|
|
182
|
-
const response = await fetch(URL, {
|
|
183
|
-
dispatcher: options.dispatcher,
|
|
184
|
-
method: 'GET',
|
|
185
|
-
headers: options.headers
|
|
186
|
-
});
|
|
187
|
-
if (!response.ok) {
|
|
188
|
-
throw new Boom(`Failed to fetch latest Baileys version: ${response.statusText}`, { statusCode: response.status });
|
|
189
|
-
}
|
|
190
|
-
const text = await response.text();
|
|
191
|
-
// Extract version from line 7 (const version = [...])
|
|
192
|
-
const lines = text.split('\n');
|
|
193
|
-
const versionLine = lines[6]; // Line 7 (0-indexed)
|
|
194
|
-
const versionMatch = versionLine.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/);
|
|
195
|
-
if (versionMatch) {
|
|
196
|
-
const version = [parseInt(versionMatch[1]), parseInt(versionMatch[2]), parseInt(versionMatch[3])];
|
|
197
|
-
return {
|
|
198
|
-
version,
|
|
199
|
-
isLatest: true
|
|
200
|
-
};
|
|
149
|
+
return async (check, timeoutMs) => {
|
|
150
|
+
let listener;
|
|
151
|
+
let closeListener;
|
|
152
|
+
await promiseTimeout(timeoutMs, (resolve, reject) => {
|
|
153
|
+
closeListener = ({
|
|
154
|
+
connection,
|
|
155
|
+
lastDisconnect
|
|
156
|
+
}) => {
|
|
157
|
+
if (connection === 'close') {
|
|
158
|
+
reject(lastDisconnect?.error || new Boom('Connection Closed', {
|
|
159
|
+
statusCode: DisconnectReason.connectionClosed
|
|
160
|
+
}));
|
|
201
161
|
}
|
|
202
|
-
|
|
203
|
-
|
|
162
|
+
};
|
|
163
|
+
ev.on('connection.update', closeListener);
|
|
164
|
+
listener = async update => {
|
|
165
|
+
if (await check(update)) {
|
|
166
|
+
resolve();
|
|
204
167
|
}
|
|
168
|
+
};
|
|
169
|
+
ev.on(event, listener);
|
|
170
|
+
}).finally(() => {
|
|
171
|
+
ev.off(event, listener);
|
|
172
|
+
ev.off('connection.update', closeListener);
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
export const bindWaitForConnectionUpdate = ev => bindWaitForEvent(ev, 'connection.update');
|
|
177
|
+
export const fetchLatestBaileysVersion = async (options = {}) => {
|
|
178
|
+
const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/index.ts';
|
|
179
|
+
try {
|
|
180
|
+
const response = await fetch(URL, {
|
|
181
|
+
dispatcher: options.dispatcher,
|
|
182
|
+
method: 'GET',
|
|
183
|
+
headers: options.headers
|
|
184
|
+
});
|
|
185
|
+
if (!response.ok) {
|
|
186
|
+
throw new Boom(`Failed to fetch latest Baileys version: ${response.statusText}`, {
|
|
187
|
+
statusCode: response.status
|
|
188
|
+
});
|
|
205
189
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
190
|
+
const text = await response.text();
|
|
191
|
+
const lines = text.split('\n');
|
|
192
|
+
const versionLine = lines[6];
|
|
193
|
+
const versionMatch = versionLine.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/);
|
|
194
|
+
if (versionMatch) {
|
|
195
|
+
const version = [parseInt(versionMatch[1]), parseInt(versionMatch[2]), parseInt(versionMatch[3])];
|
|
196
|
+
return {
|
|
197
|
+
version,
|
|
198
|
+
isLatest: true
|
|
199
|
+
};
|
|
200
|
+
} else {
|
|
201
|
+
throw new Error('Could not parse version from Defaults/index.ts');
|
|
212
202
|
}
|
|
203
|
+
} catch (error) {
|
|
204
|
+
return {
|
|
205
|
+
version: baileysVersion,
|
|
206
|
+
isLatest: false,
|
|
207
|
+
error
|
|
208
|
+
};
|
|
209
|
+
}
|
|
213
210
|
};
|
|
214
|
-
/**
|
|
215
|
-
* A utility that fetches the latest web version of whatsapp.
|
|
216
|
-
* Use to ensure your WA connection is always on the latest version
|
|
217
|
-
*/
|
|
218
211
|
export const fetchLatestWaWebVersion = async (options = {}) => {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
if (!match?.[1]) {
|
|
238
|
-
return {
|
|
239
|
-
version: baileysVersion,
|
|
240
|
-
isLatest: false,
|
|
241
|
-
error: {
|
|
242
|
-
message: 'Could not find client revision in the fetched content'
|
|
243
|
-
}
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
const clientRevision = match[1];
|
|
247
|
-
return {
|
|
248
|
-
version: [2, 3000, +clientRevision],
|
|
249
|
-
isLatest: true
|
|
250
|
-
};
|
|
212
|
+
try {
|
|
213
|
+
const defaultHeaders = {
|
|
214
|
+
'sec-fetch-site': 'none',
|
|
215
|
+
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
|
|
216
|
+
};
|
|
217
|
+
const headers = {
|
|
218
|
+
...defaultHeaders,
|
|
219
|
+
...options.headers
|
|
220
|
+
};
|
|
221
|
+
const response = await fetch('https://web.whatsapp.com/sw.js', {
|
|
222
|
+
...options,
|
|
223
|
+
method: 'GET',
|
|
224
|
+
headers
|
|
225
|
+
});
|
|
226
|
+
if (!response.ok) {
|
|
227
|
+
throw new Boom(`Failed to fetch sw.js: ${response.statusText}`, {
|
|
228
|
+
statusCode: response.status
|
|
229
|
+
});
|
|
251
230
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
231
|
+
const data = await response.text();
|
|
232
|
+
const regex = /\\?"client_revision\\?":\s*(\d+)/;
|
|
233
|
+
const match = data.match(regex);
|
|
234
|
+
if (!match?.[1]) {
|
|
235
|
+
return {
|
|
236
|
+
version: baileysVersion,
|
|
237
|
+
isLatest: false,
|
|
238
|
+
error: {
|
|
239
|
+
message: 'Could not find client revision in the fetched content'
|
|
240
|
+
}
|
|
241
|
+
};
|
|
258
242
|
}
|
|
243
|
+
const clientRevision = match[1];
|
|
244
|
+
return {
|
|
245
|
+
version: [2, 3000, +clientRevision],
|
|
246
|
+
isLatest: true
|
|
247
|
+
};
|
|
248
|
+
} catch (error) {
|
|
249
|
+
return {
|
|
250
|
+
version: baileysVersion,
|
|
251
|
+
isLatest: false,
|
|
252
|
+
error
|
|
253
|
+
};
|
|
254
|
+
}
|
|
259
255
|
};
|
|
260
|
-
/** unique message tag prefix for MD clients */
|
|
261
256
|
export const generateMdTagPrefix = () => {
|
|
262
|
-
|
|
263
|
-
|
|
257
|
+
const bytes = randomBytes(4);
|
|
258
|
+
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
|
|
264
259
|
};
|
|
265
260
|
const STATUS_MAP = {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
261
|
+
sender: proto.WebMessageInfo.Status.SERVER_ACK,
|
|
262
|
+
played: proto.WebMessageInfo.Status.PLAYED,
|
|
263
|
+
read: proto.WebMessageInfo.Status.READ,
|
|
264
|
+
'read-self': proto.WebMessageInfo.Status.READ
|
|
270
265
|
};
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
if (typeof type === 'undefined') {
|
|
278
|
-
return proto.WebMessageInfo.Status.DELIVERY_ACK;
|
|
279
|
-
}
|
|
280
|
-
return status;
|
|
266
|
+
export const getStatusFromReceiptType = type => {
|
|
267
|
+
const status = STATUS_MAP[type];
|
|
268
|
+
if (typeof type === 'undefined') {
|
|
269
|
+
return proto.WebMessageInfo.Status.DELIVERY_ACK;
|
|
270
|
+
}
|
|
271
|
+
return status;
|
|
281
272
|
};
|
|
282
273
|
const CODE_MAP = {
|
|
283
|
-
|
|
274
|
+
conflict: DisconnectReason.connectionReplaced
|
|
284
275
|
};
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
return {
|
|
297
|
-
reason,
|
|
298
|
-
statusCode
|
|
299
|
-
};
|
|
276
|
+
export const getErrorCodeFromStreamError = node => {
|
|
277
|
+
const [reasonNode] = getAllBinaryNodeChildren(node);
|
|
278
|
+
let reason = reasonNode?.tag || 'unknown';
|
|
279
|
+
const statusCode = +(node.attrs.code || CODE_MAP[reason] || DisconnectReason.badSession);
|
|
280
|
+
if (statusCode === DisconnectReason.restartRequired) {
|
|
281
|
+
reason = 'restart required';
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
reason,
|
|
285
|
+
statusCode
|
|
286
|
+
};
|
|
300
287
|
};
|
|
301
|
-
export const getCallStatusFromNode = ({
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
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
|
-
|
|
288
|
+
export const getCallStatusFromNode = ({
|
|
289
|
+
tag,
|
|
290
|
+
attrs
|
|
291
|
+
}) => {
|
|
292
|
+
let status;
|
|
293
|
+
switch (tag) {
|
|
294
|
+
case 'offer':
|
|
295
|
+
case 'offer_notice':
|
|
296
|
+
status = 'offer';
|
|
297
|
+
break;
|
|
298
|
+
case 'terminate':
|
|
299
|
+
if (attrs.reason === 'timeout') {
|
|
300
|
+
status = 'timeout';
|
|
301
|
+
} else {
|
|
302
|
+
status = 'terminate';
|
|
303
|
+
}
|
|
304
|
+
break;
|
|
305
|
+
case 'preaccept':
|
|
306
|
+
status = 'preaccept';
|
|
307
|
+
break;
|
|
308
|
+
case 'transport':
|
|
309
|
+
status = 'transport';
|
|
310
|
+
break;
|
|
311
|
+
case 'relaylatency':
|
|
312
|
+
status = 'relaylatency';
|
|
313
|
+
break;
|
|
314
|
+
case 'reject':
|
|
315
|
+
status = 'reject';
|
|
316
|
+
break;
|
|
317
|
+
case 'accept':
|
|
318
|
+
status = 'accept';
|
|
319
|
+
break;
|
|
320
|
+
default:
|
|
321
|
+
status = 'ringing';
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
return status;
|
|
337
325
|
};
|
|
338
326
|
const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: ';
|
|
339
|
-
export const getCodeFromWSError =
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
else if (
|
|
348
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
349
|
-
error?.code?.startsWith('E') ||
|
|
350
|
-
error?.message?.includes('timed out')) {
|
|
351
|
-
// handle ETIMEOUT, ENOTFOUND etc
|
|
352
|
-
statusCode = 408;
|
|
327
|
+
export const getCodeFromWSError = error => {
|
|
328
|
+
let statusCode = 500;
|
|
329
|
+
if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
|
|
330
|
+
const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length);
|
|
331
|
+
if (!Number.isNaN(code) && code >= 400) {
|
|
332
|
+
statusCode = code;
|
|
353
333
|
}
|
|
354
|
-
|
|
334
|
+
} else if (error?.code?.startsWith('E') || error?.message?.includes('timed out')) {
|
|
335
|
+
statusCode = 408;
|
|
336
|
+
}
|
|
337
|
+
return statusCode;
|
|
355
338
|
};
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
* @param platform AuthenticationCreds.platform
|
|
359
|
-
*/
|
|
360
|
-
export const isWABusinessPlatform = (platform) => {
|
|
361
|
-
return platform === 'smbi' || platform === 'smba';
|
|
339
|
+
export const isWABusinessPlatform = platform => {
|
|
340
|
+
return platform === 'smbi' || platform === 'smba';
|
|
362
341
|
};
|
|
363
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
364
342
|
export function trimUndefined(obj) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
}
|
|
343
|
+
for (const key in obj) {
|
|
344
|
+
if (typeof obj[key] === 'undefined') {
|
|
345
|
+
delete obj[key];
|
|
369
346
|
}
|
|
370
|
-
|
|
347
|
+
}
|
|
348
|
+
return obj;
|
|
371
349
|
}
|
|
372
350
|
const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ';
|
|
373
351
|
export function bytesToCrockford(buffer) {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
if (bitCount > 0) {
|
|
386
|
-
crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31));
|
|
352
|
+
let value = 0;
|
|
353
|
+
let bitCount = 0;
|
|
354
|
+
const crockford = [];
|
|
355
|
+
for (const element of buffer) {
|
|
356
|
+
value = value << 8 | element & 0xff;
|
|
357
|
+
bitCount += 8;
|
|
358
|
+
while (bitCount >= 5) {
|
|
359
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt(value >>> bitCount - 5 & 31));
|
|
360
|
+
bitCount -= 5;
|
|
387
361
|
}
|
|
388
|
-
|
|
362
|
+
}
|
|
363
|
+
if (bitCount > 0) {
|
|
364
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt(value << 5 - bitCount & 31));
|
|
365
|
+
}
|
|
366
|
+
return crockford.join('');
|
|
389
367
|
}
|
|
390
368
|
export function encodeNewsletterMessage(message) {
|
|
391
|
-
|
|
369
|
+
return proto.Message.encode(message).finish();
|
|
392
370
|
}
|
|
393
|
-
//# sourceMappingURL=generics.js.map
|