torbaileys 8.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/WAProto/WAProto.proto +5311 -0
- package/WAProto/index.js +94091 -0
- package/lib/Defaults/index.js +123 -0
- package/lib/KeyDB/BinarySearch.js +20 -0
- package/lib/KeyDB/KeyedDB.js +167 -0
- package/lib/KeyDB/index.js +4 -0
- package/lib/Signal/Group/ciphertext-message.js +13 -0
- package/lib/Signal/Group/group-session-builder.js +32 -0
- package/lib/Signal/Group/group_cipher.js +84 -0
- package/lib/Signal/Group/index.js +13 -0
- package/lib/Signal/Group/keyhelper.js +20 -0
- package/lib/Signal/Group/sender-chain-key.js +28 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +65 -0
- package/lib/Signal/Group/sender-key-message.js +68 -0
- package/lib/Signal/Group/sender-key-name.js +52 -0
- package/lib/Signal/Group/sender-key-record.js +43 -0
- package/lib/Signal/Group/sender-key-state.js +86 -0
- package/lib/Signal/Group/sender-message-key.js +28 -0
- package/lib/Signal/libsignal.js +324 -0
- package/lib/Signal/lid-mapping.js +155 -0
- package/lib/Socket/Client/index.js +4 -0
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.js +52 -0
- package/lib/Socket/Client/websocket.js.bak +53 -0
- package/lib/Socket/business.js +377 -0
- package/lib/Socket/chats.js +881 -0
- package/lib/Socket/communities.js +413 -0
- package/lib/Socket/groups.js +312 -0
- package/lib/Socket/index.js +16 -0
- package/lib/Socket/messages-recv.js +1163 -0
- package/lib/Socket/messages-send.js +1100 -0
- package/lib/Socket/mex.js +45 -0
- package/lib/Socket/newsletter.js +262 -0
- package/lib/Socket/socket.js +820 -0
- package/lib/Store/index.js +6 -0
- package/lib/Store/make-cache-manager-store.js +75 -0
- package/lib/Store/make-in-memory-store.js +290 -0
- package/lib/Store/make-ordered-dictionary.js +79 -0
- package/lib/Store/object-repository.js +25 -0
- package/lib/Types/Auth.js +3 -0
- package/lib/Types/Bussines.js +3 -0
- package/lib/Types/Call.js +3 -0
- package/lib/Types/Chat.js +9 -0
- package/lib/Types/Contact.js +3 -0
- package/lib/Types/Events.js +3 -0
- package/lib/Types/GroupMetadata.js +3 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.js +12 -0
- package/lib/Types/Newsletter.js +33 -0
- package/lib/Types/Newsletter.js.bak +33 -0
- package/lib/Types/Product.js +3 -0
- package/lib/Types/Signal.js +3 -0
- package/lib/Types/Socket.js +4 -0
- package/lib/Types/State.js +11 -0
- package/lib/Types/USync.js +3 -0
- package/lib/Types/index.js +28 -0
- package/lib/Utils/auth-utils.js +219 -0
- package/lib/Utils/baileys-event-stream.js +44 -0
- package/lib/Utils/browser-utils.js +25 -0
- package/lib/Utils/business.js +233 -0
- package/lib/Utils/chat-utils.js +752 -0
- package/lib/Utils/crypto.js +130 -0
- package/lib/Utils/decode-wa-message.js +267 -0
- package/lib/Utils/decode-wa-message.js.bak +267 -0
- package/lib/Utils/event-buffer.js +528 -0
- package/lib/Utils/generics.js +355 -0
- package/lib/Utils/history.js +87 -0
- package/lib/Utils/index.js +21 -0
- package/lib/Utils/link-preview.js +81 -0
- package/lib/Utils/logger.js +5 -0
- package/lib/Utils/lt-hash.js +45 -0
- package/lib/Utils/make-mutex.js +36 -0
- package/lib/Utils/message-retry-manager.js +113 -0
- package/lib/Utils/messages-media.js +601 -0
- package/lib/Utils/messages.js +907 -0
- package/lib/Utils/noise-handler.js +144 -0
- package/lib/Utils/pre-key-manager.js +85 -0
- package/lib/Utils/process-message.js +341 -0
- package/lib/Utils/signal.js +161 -0
- package/lib/Utils/use-multi-file-auth-state.js +111 -0
- package/lib/Utils/validate-connection.js +200 -0
- package/lib/WABinary/constants.js +1303 -0
- package/lib/WABinary/decode.js +240 -0
- package/lib/WABinary/encode.js +218 -0
- package/lib/WABinary/generic-utils.js +189 -0
- package/lib/WABinary/index.js +7 -0
- package/lib/WABinary/jid-utils.js +93 -0
- package/lib/WABinary/types.js +3 -0
- package/lib/WAM/BinaryInfo.js +11 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.js +154 -0
- package/lib/WAM/index.js +5 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +29 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +39 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +30 -0
- package/lib/WAUSync/Protocols/index.js +6 -0
- package/lib/WAUSync/USyncQuery.js +90 -0
- package/lib/WAUSync/USyncUser.js +24 -0
- package/lib/WAUSync/index.js +5 -0
- package/lib/index.js +16 -0
- package/package.json +105 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { jidEncode, WAJIDDomains } from "./jid-utils.js";
|
|
3
|
+
import * as constants from "./constants.js";
|
|
4
|
+
import { promisify } from "util";
|
|
5
|
+
import { inflate } from "zlib";
|
|
6
|
+
//=======================================================//
|
|
7
|
+
const inflatePromise = promisify(inflate);
|
|
8
|
+
export const decompressingIfRequired = async (buffer) => {
|
|
9
|
+
if (2 & buffer.readUInt8()) {
|
|
10
|
+
buffer = await inflatePromise(buffer.slice(1));
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
buffer = buffer.slice(1);
|
|
14
|
+
}
|
|
15
|
+
return buffer;
|
|
16
|
+
};
|
|
17
|
+
//=======================================================//
|
|
18
|
+
export const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
|
|
19
|
+
const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts;
|
|
20
|
+
const checkEOS = (length) => {
|
|
21
|
+
if (indexRef.index + length > buffer.length) {
|
|
22
|
+
throw new Error("end of stream");
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const next = () => {
|
|
26
|
+
const value = buffer[indexRef.index];
|
|
27
|
+
indexRef.index += 1;
|
|
28
|
+
return value;
|
|
29
|
+
};
|
|
30
|
+
const readByte = () => {
|
|
31
|
+
checkEOS(1);
|
|
32
|
+
return next();
|
|
33
|
+
};
|
|
34
|
+
const readBytes = (n) => {
|
|
35
|
+
checkEOS(n);
|
|
36
|
+
const value = buffer.slice(indexRef.index, indexRef.index + n);
|
|
37
|
+
indexRef.index += n;
|
|
38
|
+
return value;
|
|
39
|
+
};
|
|
40
|
+
const readStringFromChars = (length) => {
|
|
41
|
+
return readBytes(length).toString("utf-8");
|
|
42
|
+
};
|
|
43
|
+
const readInt = (n, littleEndian = false) => {
|
|
44
|
+
checkEOS(n);
|
|
45
|
+
let val = 0;
|
|
46
|
+
for (let i = 0; i < n; i++) {
|
|
47
|
+
const shift = littleEndian ? i : n - 1 - i;
|
|
48
|
+
val |= next() << (shift * 8);
|
|
49
|
+
}
|
|
50
|
+
return val;
|
|
51
|
+
};
|
|
52
|
+
const readInt20 = () => {
|
|
53
|
+
checkEOS(3);
|
|
54
|
+
return ((next() & 15) << 16) + (next() << 8) + next();
|
|
55
|
+
};
|
|
56
|
+
const unpackHex = (value) => {
|
|
57
|
+
if (value >= 0 && value < 16) {
|
|
58
|
+
return value < 10 ? "0".charCodeAt(0) + value : "A".charCodeAt(0) + value - 10;
|
|
59
|
+
}
|
|
60
|
+
throw new Error("invalid hex: " + value);
|
|
61
|
+
};
|
|
62
|
+
const unpackNibble = (value) => {
|
|
63
|
+
if (value >= 0 && value <= 9) {
|
|
64
|
+
return "0".charCodeAt(0) + value;
|
|
65
|
+
}
|
|
66
|
+
switch (value) {
|
|
67
|
+
case 10:
|
|
68
|
+
return "-".charCodeAt(0);
|
|
69
|
+
case 11:
|
|
70
|
+
return ".".charCodeAt(0);
|
|
71
|
+
case 15:
|
|
72
|
+
return "\0".charCodeAt(0);
|
|
73
|
+
default:
|
|
74
|
+
throw new Error("invalid nibble: " + value);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const unpackByte = (tag, value) => {
|
|
78
|
+
if (tag === TAGS.NIBBLE_8) {
|
|
79
|
+
return unpackNibble(value);
|
|
80
|
+
}
|
|
81
|
+
else if (tag === TAGS.HEX_8) {
|
|
82
|
+
return unpackHex(value);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
throw new Error("unknown tag: " + tag);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const readPacked8 = (tag) => {
|
|
89
|
+
const startByte = readByte();
|
|
90
|
+
let value = "";
|
|
91
|
+
for (let i = 0; i < (startByte & 127); i++) {
|
|
92
|
+
const curByte = readByte();
|
|
93
|
+
value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4));
|
|
94
|
+
value += String.fromCharCode(unpackByte(tag, curByte & 0x0f));
|
|
95
|
+
}
|
|
96
|
+
if (startByte >> 7 !== 0) {
|
|
97
|
+
value = value.slice(0, -1);
|
|
98
|
+
}
|
|
99
|
+
return value;
|
|
100
|
+
};
|
|
101
|
+
const isListTag = (tag) => {
|
|
102
|
+
return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16;
|
|
103
|
+
};
|
|
104
|
+
const readListSize = (tag) => {
|
|
105
|
+
switch (tag) {
|
|
106
|
+
case TAGS.LIST_EMPTY:
|
|
107
|
+
return 0;
|
|
108
|
+
case TAGS.LIST_8:
|
|
109
|
+
return readByte();
|
|
110
|
+
case TAGS.LIST_16:
|
|
111
|
+
return readInt(2);
|
|
112
|
+
default:
|
|
113
|
+
throw new Error("invalid tag for list size: " + tag);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
const readJidPair = () => {
|
|
117
|
+
const i = readString(readByte());
|
|
118
|
+
const j = readString(readByte());
|
|
119
|
+
if (j) {
|
|
120
|
+
return (i || "") + "@" + j;
|
|
121
|
+
}
|
|
122
|
+
throw new Error("invalid jid pair: " + i + ", " + j);
|
|
123
|
+
};
|
|
124
|
+
const readAdJid = () => {
|
|
125
|
+
const rawDomainType = readByte();
|
|
126
|
+
const domainType = Number(rawDomainType);
|
|
127
|
+
const device = readByte();
|
|
128
|
+
const user = readString(readByte());
|
|
129
|
+
let server = "s.whatsapp.net";
|
|
130
|
+
if (domainType === WAJIDDomains.LID) {
|
|
131
|
+
server = "lid";
|
|
132
|
+
}
|
|
133
|
+
else if (domainType === WAJIDDomains.HOSTED) {
|
|
134
|
+
server = "hosted";
|
|
135
|
+
}
|
|
136
|
+
else if (domainType === WAJIDDomains.HOSTED_LID) {
|
|
137
|
+
server = "hosted.lid";
|
|
138
|
+
}
|
|
139
|
+
return jidEncode(user, server, device);
|
|
140
|
+
};
|
|
141
|
+
const readString = (tag) => {
|
|
142
|
+
if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
|
|
143
|
+
return SINGLE_BYTE_TOKENS[tag] || "";
|
|
144
|
+
}
|
|
145
|
+
switch (tag) {
|
|
146
|
+
case TAGS.DICTIONARY_0:
|
|
147
|
+
case TAGS.DICTIONARY_1:
|
|
148
|
+
case TAGS.DICTIONARY_2:
|
|
149
|
+
case TAGS.DICTIONARY_3:
|
|
150
|
+
return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte());
|
|
151
|
+
case TAGS.LIST_EMPTY:
|
|
152
|
+
return "";
|
|
153
|
+
case TAGS.BINARY_8:
|
|
154
|
+
return readStringFromChars(readByte());
|
|
155
|
+
case TAGS.BINARY_20:
|
|
156
|
+
return readStringFromChars(readInt20());
|
|
157
|
+
case TAGS.BINARY_32:
|
|
158
|
+
return readStringFromChars(readInt(4));
|
|
159
|
+
case TAGS.JID_PAIR:
|
|
160
|
+
return readJidPair();
|
|
161
|
+
case TAGS.AD_JID:
|
|
162
|
+
return readAdJid();
|
|
163
|
+
case TAGS.HEX_8:
|
|
164
|
+
case TAGS.NIBBLE_8:
|
|
165
|
+
return readPacked8(tag);
|
|
166
|
+
default:
|
|
167
|
+
throw new Error("invalid string with tag: " + tag);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
const readList = (tag) => {
|
|
171
|
+
const items = [];
|
|
172
|
+
const size = readListSize(tag);
|
|
173
|
+
for (let i = 0; i < size; i++) {
|
|
174
|
+
items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef));
|
|
175
|
+
}
|
|
176
|
+
return items;
|
|
177
|
+
};
|
|
178
|
+
const getTokenDouble = (index1, index2) => {
|
|
179
|
+
const dict = DOUBLE_BYTE_TOKENS[index1];
|
|
180
|
+
if (!dict) {
|
|
181
|
+
throw new Error(`Invalid double token dict (${index1})`);
|
|
182
|
+
}
|
|
183
|
+
const value = dict[index2];
|
|
184
|
+
if (typeof value === "undefined") {
|
|
185
|
+
throw new Error(`Invalid double token (${index2})`);
|
|
186
|
+
}
|
|
187
|
+
return value;
|
|
188
|
+
};
|
|
189
|
+
const listSize = readListSize(readByte());
|
|
190
|
+
const header = readString(readByte());
|
|
191
|
+
if (!listSize || !header.length) {
|
|
192
|
+
throw new Error("invalid node");
|
|
193
|
+
}
|
|
194
|
+
const attrs = {};
|
|
195
|
+
let data;
|
|
196
|
+
if (listSize === 0 || !header) {
|
|
197
|
+
throw new Error("invalid node");
|
|
198
|
+
}
|
|
199
|
+
const attributesLength = (listSize - 1) >> 1;
|
|
200
|
+
for (let i = 0; i < attributesLength; i++) {
|
|
201
|
+
const key = readString(readByte());
|
|
202
|
+
const value = readString(readByte());
|
|
203
|
+
attrs[key] = value;
|
|
204
|
+
}
|
|
205
|
+
if (listSize % 2 === 0) {
|
|
206
|
+
const tag = readByte();
|
|
207
|
+
if (isListTag(tag)) {
|
|
208
|
+
data = readList(tag);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
let decoded;
|
|
212
|
+
switch (tag) {
|
|
213
|
+
case TAGS.BINARY_8:
|
|
214
|
+
decoded = readBytes(readByte());
|
|
215
|
+
break;
|
|
216
|
+
case TAGS.BINARY_20:
|
|
217
|
+
decoded = readBytes(readInt20());
|
|
218
|
+
break;
|
|
219
|
+
case TAGS.BINARY_32:
|
|
220
|
+
decoded = readBytes(readInt(4));
|
|
221
|
+
break;
|
|
222
|
+
default:
|
|
223
|
+
decoded = readString(tag);
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
data = decoded;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
tag: header,
|
|
231
|
+
attrs,
|
|
232
|
+
content: data
|
|
233
|
+
};
|
|
234
|
+
};
|
|
235
|
+
//=======================================================//
|
|
236
|
+
export const decodeBinaryNode = async (buff) => {
|
|
237
|
+
const decompBuff = await decompressingIfRequired(buff);
|
|
238
|
+
return decodeDecompressedBinaryNode(decompBuff, constants);
|
|
239
|
+
};
|
|
240
|
+
//=======================================================//
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import * as constants from "./constants.js";
|
|
3
|
+
import { jidDecode } from "./jid-utils.js";
|
|
4
|
+
//=======================================================//
|
|
5
|
+
export const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
|
|
6
|
+
const encoded = encodeBinaryNodeInner(node, opts, buffer);
|
|
7
|
+
return Buffer.from(encoded);
|
|
8
|
+
};
|
|
9
|
+
//=======================================================//
|
|
10
|
+
const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
11
|
+
const { TAGS, TOKEN_MAP } = opts;
|
|
12
|
+
const pushByte = (value) => buffer.push(value & 0xff);
|
|
13
|
+
const pushInt = (value, n, littleEndian = false) => {
|
|
14
|
+
for (let i = 0; i < n; i++) {
|
|
15
|
+
const curShift = littleEndian ? i : n - 1 - i;
|
|
16
|
+
buffer.push((value >> (curShift * 8)) & 0xff);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const pushBytes = (bytes) => {
|
|
20
|
+
for (const b of bytes) {
|
|
21
|
+
buffer.push(b);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const pushInt16 = (value) => {
|
|
25
|
+
pushBytes([(value >> 8) & 0xff, value & 0xff]);
|
|
26
|
+
};
|
|
27
|
+
const pushInt20 = (value) => pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]);
|
|
28
|
+
const writeByteLength = (length) => {
|
|
29
|
+
if (length >= 4294967296) {
|
|
30
|
+
throw new Error("string too large to encode: " + length);
|
|
31
|
+
}
|
|
32
|
+
if (length >= 1 << 20) {
|
|
33
|
+
pushByte(TAGS.BINARY_32);
|
|
34
|
+
pushInt(length, 4);
|
|
35
|
+
}
|
|
36
|
+
else if (length >= 256) {
|
|
37
|
+
pushByte(TAGS.BINARY_20);
|
|
38
|
+
pushInt20(length);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
pushByte(TAGS.BINARY_8);
|
|
42
|
+
pushByte(length);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
const writeStringRaw = (str) => {
|
|
46
|
+
const bytes = Buffer.from(str, "utf-8");
|
|
47
|
+
writeByteLength(bytes.length);
|
|
48
|
+
pushBytes(bytes);
|
|
49
|
+
};
|
|
50
|
+
const writeJid = ({ domainType, device, user, server }) => {
|
|
51
|
+
if (typeof device !== "undefined") {
|
|
52
|
+
pushByte(TAGS.AD_JID);
|
|
53
|
+
pushByte(domainType || 0);
|
|
54
|
+
pushByte(device || 0);
|
|
55
|
+
writeString(user);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
pushByte(TAGS.JID_PAIR);
|
|
59
|
+
if (user.length) {
|
|
60
|
+
writeString(user);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
64
|
+
}
|
|
65
|
+
writeString(server);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const packNibble = (char) => {
|
|
69
|
+
switch (char) {
|
|
70
|
+
case "-":
|
|
71
|
+
return 10;
|
|
72
|
+
case ".":
|
|
73
|
+
return 11;
|
|
74
|
+
case "\0":
|
|
75
|
+
return 15;
|
|
76
|
+
default:
|
|
77
|
+
if (char >= "0" && char <= "9") {
|
|
78
|
+
return char.charCodeAt(0) - "0".charCodeAt(0);
|
|
79
|
+
}
|
|
80
|
+
throw new Error(`invalid byte for nibble "${char}"`);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const packHex = (char) => {
|
|
84
|
+
if (char >= "0" && char <= "9") {
|
|
85
|
+
return char.charCodeAt(0) - "0".charCodeAt(0);
|
|
86
|
+
}
|
|
87
|
+
if (char >= "A" && char <= "F") {
|
|
88
|
+
return 10 + char.charCodeAt(0) - "A".charCodeAt(0);
|
|
89
|
+
}
|
|
90
|
+
if (char >= "a" && char <= "f") {
|
|
91
|
+
return 10 + char.charCodeAt(0) - "a".charCodeAt(0);
|
|
92
|
+
}
|
|
93
|
+
if (char === "\0") {
|
|
94
|
+
return 15;
|
|
95
|
+
}
|
|
96
|
+
throw new Error(`Invalid hex char "${char}"`);
|
|
97
|
+
};
|
|
98
|
+
const writePackedBytes = (str, type) => {
|
|
99
|
+
if (str.length > TAGS.PACKED_MAX) {
|
|
100
|
+
throw new Error("Too many bytes to pack");
|
|
101
|
+
}
|
|
102
|
+
pushByte(type === "nibble" ? TAGS.NIBBLE_8 : TAGS.HEX_8);
|
|
103
|
+
let roundedLength = Math.ceil(str.length / 2.0);
|
|
104
|
+
if (str.length % 2 !== 0) {
|
|
105
|
+
roundedLength |= 128;
|
|
106
|
+
}
|
|
107
|
+
pushByte(roundedLength);
|
|
108
|
+
const packFunction = type === "nibble" ? packNibble : packHex;
|
|
109
|
+
const packBytePair = (v1, v2) => {
|
|
110
|
+
const result = (packFunction(v1) << 4) | packFunction(v2);
|
|
111
|
+
return result;
|
|
112
|
+
};
|
|
113
|
+
const strLengthHalf = Math.floor(str.length / 2);
|
|
114
|
+
for (let i = 0; i < strLengthHalf; i++) {
|
|
115
|
+
pushByte(packBytePair(str[2 * i], str[2 * i + 1]));
|
|
116
|
+
}
|
|
117
|
+
if (str.length % 2 !== 0) {
|
|
118
|
+
pushByte(packBytePair(str[str.length - 1], "\x00"));
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const isNibble = (str) => {
|
|
122
|
+
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
for (const char of str) {
|
|
126
|
+
const isInNibbleRange = char >= "0" && char <= "9";
|
|
127
|
+
if (!isInNibbleRange && char !== "-" && char !== ".") {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return true;
|
|
132
|
+
};
|
|
133
|
+
const isHex = (str) => {
|
|
134
|
+
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
for (const char of str) {
|
|
138
|
+
const isInNibbleRange = char >= "0" && char <= "9";
|
|
139
|
+
if (!isInNibbleRange && !(char >= "A" && char <= "F")) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
};
|
|
145
|
+
const writeString = (str) => {
|
|
146
|
+
if (str === undefined || str === null) {
|
|
147
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const tokenIndex = TOKEN_MAP[str];
|
|
151
|
+
if (tokenIndex) {
|
|
152
|
+
if (typeof tokenIndex.dict === "number") {
|
|
153
|
+
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
|
|
154
|
+
}
|
|
155
|
+
pushByte(tokenIndex.index);
|
|
156
|
+
}
|
|
157
|
+
else if (isNibble(str)) {
|
|
158
|
+
writePackedBytes(str, "nibble");
|
|
159
|
+
}
|
|
160
|
+
else if (isHex(str)) {
|
|
161
|
+
writePackedBytes(str, "hex");
|
|
162
|
+
}
|
|
163
|
+
else if (str) {
|
|
164
|
+
const decodedJid = jidDecode(str);
|
|
165
|
+
if (decodedJid) {
|
|
166
|
+
writeJid(decodedJid);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
writeStringRaw(str);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
const writeListStart = (listSize) => {
|
|
174
|
+
if (listSize === 0) {
|
|
175
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
176
|
+
}
|
|
177
|
+
else if (listSize < 256) {
|
|
178
|
+
pushBytes([TAGS.LIST_8, listSize]);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
pushByte(TAGS.LIST_16);
|
|
182
|
+
pushInt16(listSize);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
if (!tag) {
|
|
186
|
+
throw new Error("Invalid node: tag cannot be undefined");
|
|
187
|
+
}
|
|
188
|
+
const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== "undefined" && attrs[k] !== null);
|
|
189
|
+
writeListStart(2 * validAttributes.length + 1 + (typeof content !== "undefined" ? 1 : 0));
|
|
190
|
+
writeString(tag);
|
|
191
|
+
for (const key of validAttributes) {
|
|
192
|
+
if (typeof attrs[key] === "string") {
|
|
193
|
+
writeString(key);
|
|
194
|
+
writeString(attrs[key]);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (typeof content === "string") {
|
|
198
|
+
writeString(content);
|
|
199
|
+
}
|
|
200
|
+
else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
|
|
201
|
+
writeByteLength(content.length);
|
|
202
|
+
pushBytes(content);
|
|
203
|
+
}
|
|
204
|
+
else if (Array.isArray(content)) {
|
|
205
|
+
const validContent = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === "string"));
|
|
206
|
+
writeListStart(validContent.length);
|
|
207
|
+
for (const item of validContent) {
|
|
208
|
+
encodeBinaryNodeInner(item, opts, buffer);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
else if (typeof content === "undefined") {
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);
|
|
215
|
+
}
|
|
216
|
+
return buffer;
|
|
217
|
+
};
|
|
218
|
+
//=======================================================//
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import { proto } from '../../WAProto/index.js';
|
|
3
|
+
import {} from './types.js';
|
|
4
|
+
import { unixTimestampSeconds } from '../Utils/index.js';
|
|
5
|
+
|
|
6
|
+
export const getBinaryNodeChildren = (node, childTag) => {
|
|
7
|
+
if (Array.isArray(node?.content)) {
|
|
8
|
+
return node.content.filter(item => item.tag === childTag);
|
|
9
|
+
}
|
|
10
|
+
return [];
|
|
11
|
+
};
|
|
12
|
+
export const getAllBinaryNodeChildren = ({ content }) => {
|
|
13
|
+
if (Array.isArray(content)) {
|
|
14
|
+
return content;
|
|
15
|
+
}
|
|
16
|
+
return [];
|
|
17
|
+
};
|
|
18
|
+
export const getBinaryNodeChild = (node, childTag) => {
|
|
19
|
+
if (Array.isArray(node?.content)) {
|
|
20
|
+
return node?.content.find(item => item.tag === childTag);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
export const getBinaryNodeChildBuffer = (node, childTag) => {
|
|
24
|
+
const child = getBinaryNodeChild(node, childTag)?.content;
|
|
25
|
+
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
|
26
|
+
return child;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
export const getBinaryNodeChildString = (node, childTag) => {
|
|
30
|
+
const child = getBinaryNodeChild(node, childTag)?.content;
|
|
31
|
+
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
|
32
|
+
return Buffer.from(child).toString('utf-8');
|
|
33
|
+
}
|
|
34
|
+
else if (typeof child === 'string') {
|
|
35
|
+
return child;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
export const getBinaryNodeChildUInt = (node, childTag, length) => {
|
|
39
|
+
const buff = getBinaryNodeChildBuffer(node, childTag);
|
|
40
|
+
if (buff) {
|
|
41
|
+
return bufferToUInt(buff, length);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
export const assertNodeErrorFree = (node) => {
|
|
45
|
+
const errNode = getBinaryNodeChild(node, 'error');
|
|
46
|
+
if (errNode) {
|
|
47
|
+
throw new Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code });
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
export const reduceBinaryNodeToDictionary = (node, tag) => {
|
|
51
|
+
const nodes = getBinaryNodeChildren(node, tag);
|
|
52
|
+
const dict = nodes.reduce((dict, { attrs }) => {
|
|
53
|
+
if (typeof attrs.name === 'string') {
|
|
54
|
+
dict[attrs.name] = attrs.value || attrs.config_value;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
dict[attrs.config_code] = attrs.value || attrs.config_value;
|
|
58
|
+
}
|
|
59
|
+
return dict;
|
|
60
|
+
}, {});
|
|
61
|
+
return dict;
|
|
62
|
+
};
|
|
63
|
+
export const getBinaryNodeMessages = ({ content }) => {
|
|
64
|
+
const msgs = [];
|
|
65
|
+
if (Array.isArray(content)) {
|
|
66
|
+
for (const item of content) {
|
|
67
|
+
if (item.tag === 'message') {
|
|
68
|
+
msgs.push(proto.WebMessageInfo.decode(item.content));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return msgs;
|
|
73
|
+
};
|
|
74
|
+
function bufferToUInt(e, t) {
|
|
75
|
+
let a = 0;
|
|
76
|
+
for (let i = 0; i < t; i++) {
|
|
77
|
+
a = 256 * a + e[i];
|
|
78
|
+
}
|
|
79
|
+
return a;
|
|
80
|
+
}
|
|
81
|
+
const tabs = (n) => '\t'.repeat(n);
|
|
82
|
+
export function binaryNodeToString(node, i = 0) {
|
|
83
|
+
if (!node) {
|
|
84
|
+
return node;
|
|
85
|
+
}
|
|
86
|
+
if (typeof node === 'string') {
|
|
87
|
+
return tabs(i) + node;
|
|
88
|
+
}
|
|
89
|
+
if (node instanceof Uint8Array) {
|
|
90
|
+
return tabs(i) + Buffer.from(node).toString('hex');
|
|
91
|
+
}
|
|
92
|
+
if (Array.isArray(node)) {
|
|
93
|
+
return node.map(x => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n');
|
|
94
|
+
}
|
|
95
|
+
const children = binaryNodeToString(node.content, i + 1);
|
|
96
|
+
const tag = `<${node.tag} ${Object.entries(node.attrs || {})
|
|
97
|
+
.filter(([, v]) => v !== undefined)
|
|
98
|
+
.map(([k, v]) => `${k}='${v}'`)
|
|
99
|
+
.join(' ')}`;
|
|
100
|
+
const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>';
|
|
101
|
+
return tag + content;
|
|
102
|
+
}
|
|
103
|
+
export const getBinaryNodeFilter = (node) => {
|
|
104
|
+
if (!Array.isArray(node)) return false
|
|
105
|
+
|
|
106
|
+
return node.some(item =>
|
|
107
|
+
['native_flow'].includes(item?.content?.[0]?.content?.[0]?.tag) ||
|
|
108
|
+
['interactive', 'buttons', 'list'].includes(item?.content?.[0]?.tag) ||
|
|
109
|
+
['hsm', 'biz'].includes(item?.tag) ||
|
|
110
|
+
['bot'].includes(item?.tag) && item?.attrs?.biz_bot === '1'
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
export const getAdditionalNode = (name) => {
|
|
114
|
+
if (name) name = name.toLowerCase()
|
|
115
|
+
const ts = unixTimestampSeconds(new Date()) - 77980457
|
|
116
|
+
|
|
117
|
+
const order_response_name = {
|
|
118
|
+
review_and_pay: 'order_details',
|
|
119
|
+
review_order: 'order_status',
|
|
120
|
+
payment_info: 'payment_info',
|
|
121
|
+
payment_status: 'payment_status',
|
|
122
|
+
payment_method: 'payment_method'
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const flow_name = {
|
|
126
|
+
cta_catalog: 'cta_catalog',
|
|
127
|
+
mpm: 'mpm',
|
|
128
|
+
call_request: 'call_permission_request',
|
|
129
|
+
view_catalog: 'automated_greeting_message_view_catalog',
|
|
130
|
+
wa_pay_detail: 'wa_payment_transaction_details',
|
|
131
|
+
send_location: 'send_location',
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if(order_response_name[name]) {
|
|
135
|
+
return [{
|
|
136
|
+
tag: 'biz',
|
|
137
|
+
attrs: {
|
|
138
|
+
native_flow_name: order_response_name[name]
|
|
139
|
+
},
|
|
140
|
+
content: []
|
|
141
|
+
}]
|
|
142
|
+
} else if (flow_name[name] || name === 'interactive' || name === 'buttons') {
|
|
143
|
+
return [{
|
|
144
|
+
tag: 'biz',
|
|
145
|
+
attrs: {
|
|
146
|
+
actual_actors: '2',
|
|
147
|
+
host_storage: '2',
|
|
148
|
+
privacy_mode_ts: `${ts}`
|
|
149
|
+
},
|
|
150
|
+
content: [{
|
|
151
|
+
tag: 'engagement',
|
|
152
|
+
attrs: {
|
|
153
|
+
customer_service_state: 'open',
|
|
154
|
+
conversation_state: 'open'
|
|
155
|
+
}
|
|
156
|
+
}, {
|
|
157
|
+
tag: 'interactive',
|
|
158
|
+
attrs: {
|
|
159
|
+
type: 'native_flow',
|
|
160
|
+
v: '1'
|
|
161
|
+
},
|
|
162
|
+
content: [{
|
|
163
|
+
tag: 'native_flow',
|
|
164
|
+
attrs: {
|
|
165
|
+
v: '9',
|
|
166
|
+
name: flow_name[name] ?? 'mixed',
|
|
167
|
+
},
|
|
168
|
+
content: []
|
|
169
|
+
}]
|
|
170
|
+
}]
|
|
171
|
+
}]
|
|
172
|
+
} else {
|
|
173
|
+
return [{
|
|
174
|
+
tag: 'biz',
|
|
175
|
+
attrs: {
|
|
176
|
+
actual_actors: '2',
|
|
177
|
+
host_storage: '2',
|
|
178
|
+
privacy_mode_ts: `${ts}`
|
|
179
|
+
},
|
|
180
|
+
content: [{
|
|
181
|
+
tag: 'engagement',
|
|
182
|
+
attrs: {
|
|
183
|
+
customer_service_state: 'open',
|
|
184
|
+
conversation_state: 'open'
|
|
185
|
+
}
|
|
186
|
+
}]
|
|
187
|
+
}]
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
export * from "./generic-utils.js";
|
|
3
|
+
export * from "./jid-utils.js";
|
|
4
|
+
export * from "./encode.js";
|
|
5
|
+
export * from "./decode.js";
|
|
6
|
+
export * from "./types.js";
|
|
7
|
+
//=======================================================//
|