@genuxofficial/baileys 1.0.0
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/LICENSE +21 -0
- package/README.md +1 -0
- package/WAProto/GenerateStatics.sh +4 -0
- package/WAProto/WAProto.proto +3344 -0
- package/WAProto/index.d.ts +37016 -0
- package/WAProto/index.js +101044 -0
- package/WASignalGroup/GroupProtocol.js +1697 -0
- package/WASignalGroup/ciphertext_message.js +16 -0
- package/WASignalGroup/group_cipher.js +120 -0
- package/WASignalGroup/group_session_builder.js +46 -0
- package/WASignalGroup/index.js +5 -0
- package/WASignalGroup/keyhelper.js +21 -0
- package/WASignalGroup/protobufs.js +3 -0
- package/WASignalGroup/queue_job.js +69 -0
- package/WASignalGroup/sender_chain_key.js +50 -0
- package/WASignalGroup/sender_key_distribution_message.js +78 -0
- package/WASignalGroup/sender_key_message.js +92 -0
- package/WASignalGroup/sender_key_name.js +70 -0
- package/WASignalGroup/sender_key_record.js +56 -0
- package/WASignalGroup/sender_key_state.js +129 -0
- package/WASignalGroup/sender_message_key.js +39 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.d.ts +53 -0
- package/lib/Defaults/index.js +106 -0
- package/lib/Signal/libsignal.d.ts +3 -0
- package/lib/Signal/libsignal.js +152 -0
- package/lib/Socket/Client/index.d.ts +2 -0
- package/lib/Socket/Client/index.js +18 -0
- package/lib/Socket/Client/types.d.ts +17 -0
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.d.ts +12 -0
- package/lib/Socket/Client/websocket.js +62 -0
- package/lib/Socket/business.d.ts +149 -0
- package/lib/Socket/business.js +260 -0
- package/lib/Socket/chats.d.ts +85 -0
- package/lib/Socket/chats.js +850 -0
- package/lib/Socket/groups.d.ts +124 -0
- package/lib/Socket/groups.js +314 -0
- package/lib/Socket/index.d.ts +149 -0
- package/lib/Socket/index.js +10 -0
- package/lib/Socket/messages-recv.d.ts +136 -0
- package/lib/Socket/messages-recv.js +912 -0
- package/lib/Socket/messages-send.d.ts +130 -0
- package/lib/Socket/messages-send.js +673 -0
- package/lib/Socket/socket.d.ts +45 -0
- package/lib/Socket/socket.js +622 -0
- package/lib/Socket/usync.d.ts +38 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +10 -0
- package/lib/Store/make-cache-manager-store.d.ts +14 -0
- package/lib/Store/make-cache-manager-store.js +83 -0
- package/lib/Store/make-in-memory-store.d.ts +118 -0
- package/lib/Store/make-in-memory-store.js +420 -0
- package/lib/Store/make-ordered-dictionary.d.ts +13 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.d.ts +105 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.d.ts +13 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.d.ts +108 -0
- package/lib/Types/Chat.js +4 -0
- package/lib/Types/Contact.d.ts +19 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.d.ts +172 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.d.ts +56 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.d.ts +46 -0
- package/lib/Types/Label.js +27 -0
- package/lib/Types/LabelAssociation.d.ts +29 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.d.ts +271 -0
- package/lib/Types/Message.js +9 -0
- package/lib/Types/Product.d.ts +78 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.d.ts +57 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.d.ts +118 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.d.ts +27 -0
- package/lib/Types/State.js +2 -0
- package/lib/Types/USync.d.ts +25 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.d.ts +63 -0
- package/lib/Types/index.js +41 -0
- package/lib/Utils/auth-utils.d.ts +18 -0
- package/lib/Utils/auth-utils.js +200 -0
- package/lib/Utils/baileys-event-stream.d.ts +16 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/business.d.ts +22 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/chat-utils.d.ts +72 -0
- package/lib/Utils/chat-utils.js +745 -0
- package/lib/Utils/crypto.d.ts +42 -0
- package/lib/Utils/crypto.js +181 -0
- package/lib/Utils/decode-wa-message.d.ts +36 -0
- package/lib/Utils/decode-wa-message.js +205 -0
- package/lib/Utils/event-buffer.d.ts +35 -0
- package/lib/Utils/event-buffer.js +520 -0
- package/lib/Utils/generics.d.ts +94 -0
- package/lib/Utils/generics.js +433 -0
- package/lib/Utils/history.d.ts +19 -0
- package/lib/Utils/history.js +94 -0
- package/lib/Utils/index.d.ts +17 -0
- package/lib/Utils/index.js +33 -0
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.js +116 -0
- package/lib/Utils/logger.d.ts +11 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.d.ts +7 -0
- package/lib/Utils/make-mutex.js +44 -0
- package/lib/Utils/messages-media.d.ts +108 -0
- package/lib/Utils/messages-media.js +667 -0
- package/lib/Utils/messages.d.ts +78 -0
- package/lib/Utils/messages.js +760 -0
- package/lib/Utils/noise-handler.d.ts +21 -0
- package/lib/Utils/noise-handler.js +150 -0
- package/lib/Utils/process-message.d.ts +42 -0
- package/lib/Utils/process-message.js +373 -0
- package/lib/Utils/signal.d.ts +33 -0
- package/lib/Utils/signal.js +153 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +12 -0
- package/lib/Utils/use-multi-file-auth-state.js +94 -0
- package/lib/Utils/validate-connection.d.ts +10 -0
- package/lib/Utils/validate-connection.js +175 -0
- package/lib/WABinary/constants.d.ts +27 -0
- package/lib/WABinary/constants.js +40 -0
- package/lib/WABinary/decode.d.ts +8 -0
- package/lib/WABinary/decode.js +254 -0
- package/lib/WABinary/encode.d.ts +4 -0
- package/lib/WABinary/encode.js +234 -0
- package/lib/WABinary/generic-utils.d.ts +16 -0
- package/lib/WABinary/generic-utils.js +110 -0
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.d.ts +31 -0
- package/lib/WABinary/jid-utils.js +62 -0
- package/lib/WABinary/types.d.ts +18 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.d.ts +18 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.d.ts +39 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.d.ts +4 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/index.d.ts +4 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.d.ts +26 -0
- package/lib/WAUSync/USyncQuery.js +79 -0
- package/lib/WAUSync/USyncUser.d.ts +10 -0
- package/lib/WAUSync/USyncUser.js +22 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +31 -0
- package/package.json +104 -0
@@ -0,0 +1,254 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
26
|
+
exports.decodeBinaryNode = exports.decodeDecompressedBinaryNode = exports.decompressingIfRequired = void 0;
|
27
|
+
const util_1 = require("util");
|
28
|
+
const zlib_1 = require("zlib");
|
29
|
+
const constants = __importStar(require("./constants"));
|
30
|
+
const jid_utils_1 = require("./jid-utils");
|
31
|
+
const inflatePromise = (0, util_1.promisify)(zlib_1.inflate);
|
32
|
+
const decompressingIfRequired = async (buffer) => {
|
33
|
+
if (2 & buffer.readUInt8()) {
|
34
|
+
buffer = await inflatePromise(buffer.slice(1));
|
35
|
+
}
|
36
|
+
else { // nodes with no compression have a 0x00 prefix, we remove that
|
37
|
+
buffer = buffer.slice(1);
|
38
|
+
}
|
39
|
+
return buffer;
|
40
|
+
};
|
41
|
+
exports.decompressingIfRequired = decompressingIfRequired;
|
42
|
+
const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
|
43
|
+
const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts;
|
44
|
+
const checkEOS = (length) => {
|
45
|
+
if (indexRef.index + length > buffer.length) {
|
46
|
+
throw new Error('end of stream');
|
47
|
+
}
|
48
|
+
};
|
49
|
+
const next = () => {
|
50
|
+
const value = buffer[indexRef.index];
|
51
|
+
indexRef.index += 1;
|
52
|
+
return value;
|
53
|
+
};
|
54
|
+
const readByte = () => {
|
55
|
+
checkEOS(1);
|
56
|
+
return next();
|
57
|
+
};
|
58
|
+
const readBytes = (n) => {
|
59
|
+
checkEOS(n);
|
60
|
+
const value = buffer.slice(indexRef.index, indexRef.index + n);
|
61
|
+
indexRef.index += n;
|
62
|
+
return value;
|
63
|
+
};
|
64
|
+
const readStringFromChars = (length) => {
|
65
|
+
return readBytes(length).toString('utf-8');
|
66
|
+
};
|
67
|
+
const readInt = (n, littleEndian = false) => {
|
68
|
+
checkEOS(n);
|
69
|
+
let val = 0;
|
70
|
+
for (let i = 0; i < n; i++) {
|
71
|
+
const shift = littleEndian ? i : n - 1 - i;
|
72
|
+
val |= next() << (shift * 8);
|
73
|
+
}
|
74
|
+
return val;
|
75
|
+
};
|
76
|
+
const readInt20 = () => {
|
77
|
+
checkEOS(3);
|
78
|
+
return ((next() & 15) << 16) + (next() << 8) + next();
|
79
|
+
};
|
80
|
+
const unpackHex = (value) => {
|
81
|
+
if (value >= 0 && value < 16) {
|
82
|
+
return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10;
|
83
|
+
}
|
84
|
+
throw new Error('invalid hex: ' + value);
|
85
|
+
};
|
86
|
+
const unpackNibble = (value) => {
|
87
|
+
if (value >= 0 && value <= 9) {
|
88
|
+
return '0'.charCodeAt(0) + value;
|
89
|
+
}
|
90
|
+
switch (value) {
|
91
|
+
case 10:
|
92
|
+
return '-'.charCodeAt(0);
|
93
|
+
case 11:
|
94
|
+
return '.'.charCodeAt(0);
|
95
|
+
case 15:
|
96
|
+
return '\0'.charCodeAt(0);
|
97
|
+
default:
|
98
|
+
throw new Error('invalid nibble: ' + value);
|
99
|
+
}
|
100
|
+
};
|
101
|
+
const unpackByte = (tag, value) => {
|
102
|
+
if (tag === TAGS.NIBBLE_8) {
|
103
|
+
return unpackNibble(value);
|
104
|
+
}
|
105
|
+
else if (tag === TAGS.HEX_8) {
|
106
|
+
return unpackHex(value);
|
107
|
+
}
|
108
|
+
else {
|
109
|
+
throw new Error('unknown tag: ' + tag);
|
110
|
+
}
|
111
|
+
};
|
112
|
+
const readPacked8 = (tag) => {
|
113
|
+
const startByte = readByte();
|
114
|
+
let value = '';
|
115
|
+
for (let i = 0; i < (startByte & 127); i++) {
|
116
|
+
const curByte = readByte();
|
117
|
+
value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4));
|
118
|
+
value += String.fromCharCode(unpackByte(tag, curByte & 0x0f));
|
119
|
+
}
|
120
|
+
if (startByte >> 7 !== 0) {
|
121
|
+
value = value.slice(0, -1);
|
122
|
+
}
|
123
|
+
return value;
|
124
|
+
};
|
125
|
+
const isListTag = (tag) => {
|
126
|
+
return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16;
|
127
|
+
};
|
128
|
+
const readListSize = (tag) => {
|
129
|
+
switch (tag) {
|
130
|
+
case TAGS.LIST_EMPTY:
|
131
|
+
return 0;
|
132
|
+
case TAGS.LIST_8:
|
133
|
+
return readByte();
|
134
|
+
case TAGS.LIST_16:
|
135
|
+
return readInt(2);
|
136
|
+
default:
|
137
|
+
throw new Error('invalid tag for list size: ' + tag);
|
138
|
+
}
|
139
|
+
};
|
140
|
+
const readJidPair = () => {
|
141
|
+
const i = readString(readByte());
|
142
|
+
const j = readString(readByte());
|
143
|
+
if (j) {
|
144
|
+
return (i || '') + '@' + j;
|
145
|
+
}
|
146
|
+
throw new Error('invalid jid pair: ' + i + ', ' + j);
|
147
|
+
};
|
148
|
+
const readAdJid = () => {
|
149
|
+
const agent = readByte();
|
150
|
+
const device = readByte();
|
151
|
+
const user = readString(readByte());
|
152
|
+
return (0, jid_utils_1.jidEncode)(user, agent === 0 ? 's.whatsapp.net' : 'lid', device);
|
153
|
+
};
|
154
|
+
const readString = (tag) => {
|
155
|
+
if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
|
156
|
+
return SINGLE_BYTE_TOKENS[tag] || '';
|
157
|
+
}
|
158
|
+
switch (tag) {
|
159
|
+
case TAGS.DICTIONARY_0:
|
160
|
+
case TAGS.DICTIONARY_1:
|
161
|
+
case TAGS.DICTIONARY_2:
|
162
|
+
case TAGS.DICTIONARY_3:
|
163
|
+
return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte());
|
164
|
+
case TAGS.LIST_EMPTY:
|
165
|
+
return '';
|
166
|
+
case TAGS.BINARY_8:
|
167
|
+
return readStringFromChars(readByte());
|
168
|
+
case TAGS.BINARY_20:
|
169
|
+
return readStringFromChars(readInt20());
|
170
|
+
case TAGS.BINARY_32:
|
171
|
+
return readStringFromChars(readInt(4));
|
172
|
+
case TAGS.JID_PAIR:
|
173
|
+
return readJidPair();
|
174
|
+
case TAGS.AD_JID:
|
175
|
+
return readAdJid();
|
176
|
+
case TAGS.HEX_8:
|
177
|
+
case TAGS.NIBBLE_8:
|
178
|
+
return readPacked8(tag);
|
179
|
+
default:
|
180
|
+
throw new Error('invalid string with tag: ' + tag);
|
181
|
+
}
|
182
|
+
};
|
183
|
+
const readList = (tag) => {
|
184
|
+
const items = [];
|
185
|
+
const size = readListSize(tag);
|
186
|
+
for (let i = 0; i < size; i++) {
|
187
|
+
items.push((0, exports.decodeDecompressedBinaryNode)(buffer, opts, indexRef));
|
188
|
+
}
|
189
|
+
return items;
|
190
|
+
};
|
191
|
+
const getTokenDouble = (index1, index2) => {
|
192
|
+
const dict = DOUBLE_BYTE_TOKENS[index1];
|
193
|
+
if (!dict) {
|
194
|
+
throw new Error(`Invalid double token dict (${index1})`);
|
195
|
+
}
|
196
|
+
const value = dict[index2];
|
197
|
+
if (typeof value === 'undefined') {
|
198
|
+
throw new Error(`Invalid double token (${index2})`);
|
199
|
+
}
|
200
|
+
return value;
|
201
|
+
};
|
202
|
+
const listSize = readListSize(readByte());
|
203
|
+
const header = readString(readByte());
|
204
|
+
if (!listSize || !header.length) {
|
205
|
+
throw new Error('invalid node');
|
206
|
+
}
|
207
|
+
const attrs = {};
|
208
|
+
let data;
|
209
|
+
if (listSize === 0 || !header) {
|
210
|
+
throw new Error('invalid node');
|
211
|
+
}
|
212
|
+
// read the attributes in
|
213
|
+
const attributesLength = (listSize - 1) >> 1;
|
214
|
+
for (let i = 0; i < attributesLength; i++) {
|
215
|
+
const key = readString(readByte());
|
216
|
+
const value = readString(readByte());
|
217
|
+
attrs[key] = value;
|
218
|
+
}
|
219
|
+
if (listSize % 2 === 0) {
|
220
|
+
const tag = readByte();
|
221
|
+
if (isListTag(tag)) {
|
222
|
+
data = readList(tag);
|
223
|
+
}
|
224
|
+
else {
|
225
|
+
let decoded;
|
226
|
+
switch (tag) {
|
227
|
+
case TAGS.BINARY_8:
|
228
|
+
decoded = readBytes(readByte());
|
229
|
+
break;
|
230
|
+
case TAGS.BINARY_20:
|
231
|
+
decoded = readBytes(readInt20());
|
232
|
+
break;
|
233
|
+
case TAGS.BINARY_32:
|
234
|
+
decoded = readBytes(readInt(4));
|
235
|
+
break;
|
236
|
+
default:
|
237
|
+
decoded = readString(tag);
|
238
|
+
break;
|
239
|
+
}
|
240
|
+
data = decoded;
|
241
|
+
}
|
242
|
+
}
|
243
|
+
return {
|
244
|
+
tag: header,
|
245
|
+
attrs,
|
246
|
+
content: data
|
247
|
+
};
|
248
|
+
};
|
249
|
+
exports.decodeDecompressedBinaryNode = decodeDecompressedBinaryNode;
|
250
|
+
const decodeBinaryNode = async (buff) => {
|
251
|
+
const decompBuff = await (0, exports.decompressingIfRequired)(buff);
|
252
|
+
return (0, exports.decodeDecompressedBinaryNode)(decompBuff, constants);
|
253
|
+
};
|
254
|
+
exports.decodeBinaryNode = decodeBinaryNode;
|
@@ -0,0 +1,4 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
/// <reference types="node" />
|
3
|
+
import type { BinaryNode, BinaryNodeCodingOptions } from './types';
|
4
|
+
export declare const encodeBinaryNode: (node: BinaryNode, opts?: Pick<BinaryNodeCodingOptions, 'TAGS' | 'TOKEN_MAP'>, buffer?: number[]) => Buffer;
|
@@ -0,0 +1,234 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
26
|
+
exports.encodeBinaryNode = void 0;
|
27
|
+
const constants = __importStar(require("./constants"));
|
28
|
+
const jid_utils_1 = require("./jid-utils");
|
29
|
+
const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
|
30
|
+
const encoded = encodeBinaryNodeInner(node, opts, buffer);
|
31
|
+
return Buffer.from(encoded);
|
32
|
+
};
|
33
|
+
exports.encodeBinaryNode = encodeBinaryNode;
|
34
|
+
const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
35
|
+
const { TAGS, TOKEN_MAP } = opts;
|
36
|
+
const pushByte = (value) => buffer.push(value & 0xff);
|
37
|
+
const pushInt = (value, n, littleEndian = false) => {
|
38
|
+
for (let i = 0; i < n; i++) {
|
39
|
+
const curShift = littleEndian ? i : n - 1 - i;
|
40
|
+
buffer.push((value >> (curShift * 8)) & 0xff);
|
41
|
+
}
|
42
|
+
};
|
43
|
+
const pushBytes = (bytes) => {
|
44
|
+
for (const b of bytes) {
|
45
|
+
buffer.push(b);
|
46
|
+
}
|
47
|
+
};
|
48
|
+
const pushInt16 = (value) => {
|
49
|
+
pushBytes([(value >> 8) & 0xff, value & 0xff]);
|
50
|
+
};
|
51
|
+
const pushInt20 = (value) => (pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]));
|
52
|
+
const writeByteLength = (length) => {
|
53
|
+
if (length >= 4294967296) {
|
54
|
+
throw new Error('string too large to encode: ' + length);
|
55
|
+
}
|
56
|
+
if (length >= 1 << 20) {
|
57
|
+
pushByte(TAGS.BINARY_32);
|
58
|
+
pushInt(length, 4); // 32 bit integer
|
59
|
+
}
|
60
|
+
else if (length >= 256) {
|
61
|
+
pushByte(TAGS.BINARY_20);
|
62
|
+
pushInt20(length);
|
63
|
+
}
|
64
|
+
else {
|
65
|
+
pushByte(TAGS.BINARY_8);
|
66
|
+
pushByte(length);
|
67
|
+
}
|
68
|
+
};
|
69
|
+
const writeStringRaw = (str) => {
|
70
|
+
const bytes = Buffer.from(str, 'utf-8');
|
71
|
+
writeByteLength(bytes.length);
|
72
|
+
pushBytes(bytes);
|
73
|
+
};
|
74
|
+
const writeJid = ({ domainType, device, user, server }) => {
|
75
|
+
if (typeof device !== 'undefined') {
|
76
|
+
pushByte(TAGS.AD_JID);
|
77
|
+
pushByte(domainType || 0);
|
78
|
+
pushByte(device || 0);
|
79
|
+
writeString(user);
|
80
|
+
}
|
81
|
+
else {
|
82
|
+
pushByte(TAGS.JID_PAIR);
|
83
|
+
if (user.length) {
|
84
|
+
writeString(user);
|
85
|
+
}
|
86
|
+
else {
|
87
|
+
pushByte(TAGS.LIST_EMPTY);
|
88
|
+
}
|
89
|
+
writeString(server);
|
90
|
+
}
|
91
|
+
};
|
92
|
+
const packNibble = (char) => {
|
93
|
+
switch (char) {
|
94
|
+
case '-':
|
95
|
+
return 10;
|
96
|
+
case '.':
|
97
|
+
return 11;
|
98
|
+
case '\0':
|
99
|
+
return 15;
|
100
|
+
default:
|
101
|
+
if (char >= '0' && char <= '9') {
|
102
|
+
return char.charCodeAt(0) - '0'.charCodeAt(0);
|
103
|
+
}
|
104
|
+
throw new Error(`invalid byte for nibble "${char}"`);
|
105
|
+
}
|
106
|
+
};
|
107
|
+
const packHex = (char) => {
|
108
|
+
if (char >= '0' && char <= '9') {
|
109
|
+
return char.charCodeAt(0) - '0'.charCodeAt(0);
|
110
|
+
}
|
111
|
+
if (char >= 'A' && char <= 'F') {
|
112
|
+
return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0);
|
113
|
+
}
|
114
|
+
if (char >= 'a' && char <= 'f') {
|
115
|
+
return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0);
|
116
|
+
}
|
117
|
+
if (char === '\0') {
|
118
|
+
return 15;
|
119
|
+
}
|
120
|
+
throw new Error(`Invalid hex char "${char}"`);
|
121
|
+
};
|
122
|
+
const writePackedBytes = (str, type) => {
|
123
|
+
if (str.length > TAGS.PACKED_MAX) {
|
124
|
+
throw new Error('Too many bytes to pack');
|
125
|
+
}
|
126
|
+
pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8);
|
127
|
+
let roundedLength = Math.ceil(str.length / 2.0);
|
128
|
+
if (str.length % 2 !== 0) {
|
129
|
+
roundedLength |= 128;
|
130
|
+
}
|
131
|
+
pushByte(roundedLength);
|
132
|
+
const packFunction = type === 'nibble' ? packNibble : packHex;
|
133
|
+
const packBytePair = (v1, v2) => {
|
134
|
+
const result = (packFunction(v1) << 4) | packFunction(v2);
|
135
|
+
return result;
|
136
|
+
};
|
137
|
+
const strLengthHalf = Math.floor(str.length / 2);
|
138
|
+
for (let i = 0; i < strLengthHalf; i++) {
|
139
|
+
pushByte(packBytePair(str[2 * i], str[2 * i + 1]));
|
140
|
+
}
|
141
|
+
if (str.length % 2 !== 0) {
|
142
|
+
pushByte(packBytePair(str[str.length - 1], '\x00'));
|
143
|
+
}
|
144
|
+
};
|
145
|
+
const isNibble = (str) => {
|
146
|
+
if (str.length > TAGS.PACKED_MAX) {
|
147
|
+
return false;
|
148
|
+
}
|
149
|
+
for (const char of str) {
|
150
|
+
const isInNibbleRange = char >= '0' && char <= '9';
|
151
|
+
if (!isInNibbleRange && char !== '-' && char !== '.') {
|
152
|
+
return false;
|
153
|
+
}
|
154
|
+
}
|
155
|
+
return true;
|
156
|
+
};
|
157
|
+
const isHex = (str) => {
|
158
|
+
if (str.length > TAGS.PACKED_MAX) {
|
159
|
+
return false;
|
160
|
+
}
|
161
|
+
for (const char of str) {
|
162
|
+
const isInNibbleRange = char >= '0' && char <= '9';
|
163
|
+
if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
|
164
|
+
return false;
|
165
|
+
}
|
166
|
+
}
|
167
|
+
return true;
|
168
|
+
};
|
169
|
+
const writeString = (str) => {
|
170
|
+
const tokenIndex = TOKEN_MAP[str];
|
171
|
+
if (tokenIndex) {
|
172
|
+
if (typeof tokenIndex.dict === 'number') {
|
173
|
+
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
|
174
|
+
}
|
175
|
+
pushByte(tokenIndex.index);
|
176
|
+
}
|
177
|
+
else if (isNibble(str)) {
|
178
|
+
writePackedBytes(str, 'nibble');
|
179
|
+
}
|
180
|
+
else if (isHex(str)) {
|
181
|
+
writePackedBytes(str, 'hex');
|
182
|
+
}
|
183
|
+
else if (str) {
|
184
|
+
const decodedJid = (0, jid_utils_1.jidDecode)(str);
|
185
|
+
if (decodedJid) {
|
186
|
+
writeJid(decodedJid);
|
187
|
+
}
|
188
|
+
else {
|
189
|
+
writeStringRaw(str);
|
190
|
+
}
|
191
|
+
}
|
192
|
+
};
|
193
|
+
const writeListStart = (listSize) => {
|
194
|
+
if (listSize === 0) {
|
195
|
+
pushByte(TAGS.LIST_EMPTY);
|
196
|
+
}
|
197
|
+
else if (listSize < 256) {
|
198
|
+
pushBytes([TAGS.LIST_8, listSize]);
|
199
|
+
}
|
200
|
+
else {
|
201
|
+
pushByte(TAGS.LIST_16);
|
202
|
+
pushInt16(listSize);
|
203
|
+
}
|
204
|
+
};
|
205
|
+
const validAttributes = Object.keys(attrs).filter(k => (typeof attrs[k] !== 'undefined' && attrs[k] !== null));
|
206
|
+
writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));
|
207
|
+
writeString(tag);
|
208
|
+
for (const key of validAttributes) {
|
209
|
+
if (typeof attrs[key] === 'string') {
|
210
|
+
writeString(key);
|
211
|
+
writeString(attrs[key]);
|
212
|
+
}
|
213
|
+
}
|
214
|
+
if (typeof content === 'string') {
|
215
|
+
writeString(content);
|
216
|
+
}
|
217
|
+
else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
|
218
|
+
writeByteLength(content.length);
|
219
|
+
pushBytes(content);
|
220
|
+
}
|
221
|
+
else if (Array.isArray(content)) {
|
222
|
+
writeListStart(content.length);
|
223
|
+
for (const item of content) {
|
224
|
+
encodeBinaryNodeInner(item, opts, buffer);
|
225
|
+
}
|
226
|
+
}
|
227
|
+
else if (typeof content === 'undefined') {
|
228
|
+
// do nothing
|
229
|
+
}
|
230
|
+
else {
|
231
|
+
throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);
|
232
|
+
}
|
233
|
+
return buffer;
|
234
|
+
};
|
@@ -0,0 +1,16 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
/// <reference types="node" />
|
3
|
+
import { proto } from '../../WAProto';
|
4
|
+
import { BinaryNode } from './types';
|
5
|
+
export declare const getBinaryNodeChildren: (node: BinaryNode | undefined, childTag: string) => BinaryNode[];
|
6
|
+
export declare const getAllBinaryNodeChildren: ({ content }: BinaryNode) => BinaryNode[];
|
7
|
+
export declare const getBinaryNodeChild: (node: BinaryNode | undefined, childTag: string) => BinaryNode | undefined;
|
8
|
+
export declare const getBinaryNodeChildBuffer: (node: BinaryNode | undefined, childTag: string) => Uint8Array | Buffer | undefined;
|
9
|
+
export declare const getBinaryNodeChildString: (node: BinaryNode | undefined, childTag: string) => string | undefined;
|
10
|
+
export declare const getBinaryNodeChildUInt: (node: BinaryNode, childTag: string, length: number) => number | undefined;
|
11
|
+
export declare const assertNodeErrorFree: (node: BinaryNode) => void;
|
12
|
+
export declare const reduceBinaryNodeToDictionary: (node: BinaryNode, tag: string) => {
|
13
|
+
[_: string]: string;
|
14
|
+
};
|
15
|
+
export declare const getBinaryNodeMessages: ({ content }: BinaryNode) => proto.WebMessageInfo[];
|
16
|
+
export declare function binaryNodeToString(node: BinaryNode | BinaryNode['content'], i?: number): any;
|
@@ -0,0 +1,110 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.binaryNodeToString = exports.getBinaryNodeMessages = exports.reduceBinaryNodeToDictionary = exports.assertNodeErrorFree = exports.getBinaryNodeChildUInt = exports.getBinaryNodeChildString = exports.getBinaryNodeChildBuffer = exports.getBinaryNodeChild = exports.getAllBinaryNodeChildren = exports.getBinaryNodeChildren = void 0;
|
4
|
+
const boom_1 = require("@hapi/boom");
|
5
|
+
const WAProto_1 = require("../../WAProto");
|
6
|
+
// some extra useful utilities
|
7
|
+
const getBinaryNodeChildren = (node, childTag) => {
|
8
|
+
if (Array.isArray(node === null || node === void 0 ? void 0 : node.content)) {
|
9
|
+
return node.content.filter(item => item.tag === childTag);
|
10
|
+
}
|
11
|
+
return [];
|
12
|
+
};
|
13
|
+
exports.getBinaryNodeChildren = getBinaryNodeChildren;
|
14
|
+
const getAllBinaryNodeChildren = ({ content }) => {
|
15
|
+
if (Array.isArray(content)) {
|
16
|
+
return content;
|
17
|
+
}
|
18
|
+
return [];
|
19
|
+
};
|
20
|
+
exports.getAllBinaryNodeChildren = getAllBinaryNodeChildren;
|
21
|
+
const getBinaryNodeChild = (node, childTag) => {
|
22
|
+
if (Array.isArray(node === null || node === void 0 ? void 0 : node.content)) {
|
23
|
+
return node === null || node === void 0 ? void 0 : node.content.find(item => item.tag === childTag);
|
24
|
+
}
|
25
|
+
};
|
26
|
+
exports.getBinaryNodeChild = getBinaryNodeChild;
|
27
|
+
const getBinaryNodeChildBuffer = (node, childTag) => {
|
28
|
+
var _a;
|
29
|
+
const child = (_a = (0, exports.getBinaryNodeChild)(node, childTag)) === null || _a === void 0 ? void 0 : _a.content;
|
30
|
+
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
31
|
+
return child;
|
32
|
+
}
|
33
|
+
};
|
34
|
+
exports.getBinaryNodeChildBuffer = getBinaryNodeChildBuffer;
|
35
|
+
const getBinaryNodeChildString = (node, childTag) => {
|
36
|
+
var _a;
|
37
|
+
const child = (_a = (0, exports.getBinaryNodeChild)(node, childTag)) === null || _a === void 0 ? void 0 : _a.content;
|
38
|
+
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
39
|
+
return Buffer.from(child).toString('utf-8');
|
40
|
+
}
|
41
|
+
else if (typeof child === 'string') {
|
42
|
+
return child;
|
43
|
+
}
|
44
|
+
};
|
45
|
+
exports.getBinaryNodeChildString = getBinaryNodeChildString;
|
46
|
+
const getBinaryNodeChildUInt = (node, childTag, length) => {
|
47
|
+
const buff = (0, exports.getBinaryNodeChildBuffer)(node, childTag);
|
48
|
+
if (buff) {
|
49
|
+
return bufferToUInt(buff, length);
|
50
|
+
}
|
51
|
+
};
|
52
|
+
exports.getBinaryNodeChildUInt = getBinaryNodeChildUInt;
|
53
|
+
const assertNodeErrorFree = (node) => {
|
54
|
+
const errNode = (0, exports.getBinaryNodeChild)(node, 'error');
|
55
|
+
if (errNode) {
|
56
|
+
throw new boom_1.Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code });
|
57
|
+
}
|
58
|
+
};
|
59
|
+
exports.assertNodeErrorFree = assertNodeErrorFree;
|
60
|
+
const reduceBinaryNodeToDictionary = (node, tag) => {
|
61
|
+
const nodes = (0, exports.getBinaryNodeChildren)(node, tag);
|
62
|
+
const dict = nodes.reduce((dict, { attrs }) => {
|
63
|
+
dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value;
|
64
|
+
return dict;
|
65
|
+
}, {});
|
66
|
+
return dict;
|
67
|
+
};
|
68
|
+
exports.reduceBinaryNodeToDictionary = reduceBinaryNodeToDictionary;
|
69
|
+
const getBinaryNodeMessages = ({ content }) => {
|
70
|
+
const msgs = [];
|
71
|
+
if (Array.isArray(content)) {
|
72
|
+
for (const item of content) {
|
73
|
+
if (item.tag === 'message') {
|
74
|
+
msgs.push(WAProto_1.proto.WebMessageInfo.decode(item.content));
|
75
|
+
}
|
76
|
+
}
|
77
|
+
}
|
78
|
+
return msgs;
|
79
|
+
};
|
80
|
+
exports.getBinaryNodeMessages = getBinaryNodeMessages;
|
81
|
+
function bufferToUInt(e, t) {
|
82
|
+
let a = 0;
|
83
|
+
for (let i = 0; i < t; i++) {
|
84
|
+
a = 256 * a + e[i];
|
85
|
+
}
|
86
|
+
return a;
|
87
|
+
}
|
88
|
+
const tabs = (n) => '\t'.repeat(n);
|
89
|
+
function binaryNodeToString(node, i = 0) {
|
90
|
+
if (!node) {
|
91
|
+
return node;
|
92
|
+
}
|
93
|
+
if (typeof node === 'string') {
|
94
|
+
return tabs(i) + node;
|
95
|
+
}
|
96
|
+
if (node instanceof Uint8Array) {
|
97
|
+
return tabs(i) + Buffer.from(node).toString('hex');
|
98
|
+
}
|
99
|
+
if (Array.isArray(node)) {
|
100
|
+
return node.map((x) => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n');
|
101
|
+
}
|
102
|
+
const children = binaryNodeToString(node.content, i + 1);
|
103
|
+
const tag = `<${node.tag} ${Object.entries(node.attrs || {})
|
104
|
+
.filter(([, v]) => v !== undefined)
|
105
|
+
.map(([k, v]) => `${k}='${v}'`)
|
106
|
+
.join(' ')}`;
|
107
|
+
const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>';
|
108
|
+
return tag + content;
|
109
|
+
}
|
110
|
+
exports.binaryNodeToString = binaryNodeToString;
|
@@ -0,0 +1,21 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./encode"), exports);
|
18
|
+
__exportStar(require("./decode"), exports);
|
19
|
+
__exportStar(require("./generic-utils"), exports);
|
20
|
+
__exportStar(require("./jid-utils"), exports);
|
21
|
+
__exportStar(require("./types"), exports);
|
@@ -0,0 +1,31 @@
|
|
1
|
+
export declare const S_WHATSAPP_NET = "@s.whatsapp.net";
|
2
|
+
export declare const OFFICIAL_BIZ_JID = "16505361212@c.us";
|
3
|
+
export declare const SERVER_JID = "server@c.us";
|
4
|
+
export declare const PSA_WID = "0@c.us";
|
5
|
+
export declare const STORIES_JID = "status@broadcast";
|
6
|
+
export type JidServer = 'c.us' | 'g.us' | 'broadcast' | 's.whatsapp.net' | 'call' | 'lid' | 'newsletter';
|
7
|
+
export type JidWithDevice = {
|
8
|
+
user: string;
|
9
|
+
device?: number;
|
10
|
+
};
|
11
|
+
export type FullJid = JidWithDevice & {
|
12
|
+
server: JidServer;
|
13
|
+
domainType?: number;
|
14
|
+
};
|
15
|
+
export declare const jidEncode: (user: string | number | null, server: JidServer, device?: number, agent?: number) => string;
|
16
|
+
export declare const jidDecode: (jid: string | undefined) => FullJid | undefined;
|
17
|
+
/** is the jid a user */
|
18
|
+
export declare const areJidsSameUser: (jid1: string | undefined, jid2: string | undefined) => boolean;
|
19
|
+
/** is the jid a user */
|
20
|
+
export declare const isJidUser: (jid: string | undefined) => boolean | undefined;
|
21
|
+
/** is the jid a group */
|
22
|
+
export declare const isLidUser: (jid: string | undefined) => boolean | undefined;
|
23
|
+
/** is the jid a broadcast */
|
24
|
+
export declare const isJidBroadcast: (jid: string | undefined) => boolean | undefined;
|
25
|
+
/** is the jid a group */
|
26
|
+
export declare const isJidGroup: (jid: string | undefined) => boolean | undefined;
|
27
|
+
/** is the jid the status broadcast */
|
28
|
+
export declare const isJidStatusBroadcast: (jid: string) => boolean;
|
29
|
+
/** is the jid a newsletter */
|
30
|
+
export declare const isJidNewsletter: (jid: string | undefined) => boolean | undefined;
|
31
|
+
export declare const jidNormalizedUser: (jid: string | undefined) => string;
|