@prototypevip/baileys 0.0.1
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 +23 -0
- package/WAProto/index.js +131118 -0
- package/engine-requirements.js +1 -0
- package/lib/Defaults/index.js +187 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-session-builder.js +41 -0
- package/lib/Signal/Group/group_cipher.js +108 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.js +14 -0
- package/lib/Signal/Group/sender-chain-key.js +31 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +79 -0
- package/lib/Signal/Group/sender-key-name.js +49 -0
- package/lib/Signal/Group/sender-key-record.js +46 -0
- package/lib/Signal/Group/sender-key-state.js +104 -0
- package/lib/Signal/Group/sender-message-key.js +29 -0
- package/lib/Signal/libsignal.js +532 -0
- package/lib/Signal/lid-mapping.js +312 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +64 -0
- package/lib/Socket/business.js +293 -0
- package/lib/Socket/chats.js +1124 -0
- package/lib/Socket/communities.js +528 -0
- package/lib/Socket/groups.js +465 -0
- package/lib/Socket/index.js +14 -0
- package/lib/Socket/messages-recv.js +2325 -0
- package/lib/Socket/messages-send.js +1746 -0
- package/lib/Socket/mex.js +55 -0
- package/lib/Socket/newsletter.js +317 -0
- package/lib/Socket/socket.js +1185 -0
- package/lib/Store/cache-factory.js +20 -0
- package/lib/Store/index.js +5 -0
- package/lib/Store/make-in-memory-store.js +523 -0
- package/lib/Store/make-ordered-dictionary.js +203 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Store/prototype-store.js +958 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.js +18 -0
- package/lib/Types/Mex.js +37 -0
- package/lib/Types/Newsletter.js +36 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/RichType.js +25 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +78 -0
- package/lib/Types/USync.js +1 -0
- package/lib/Types/index.js +33 -0
- package/lib/Utils/auth-utils.js +303 -0
- package/lib/Utils/browser-utils.js +32 -0
- package/lib/Utils/business.js +245 -0
- package/lib/Utils/chat-utils.js +971 -0
- package/lib/Utils/companion-reg-client-utils.js +54 -0
- package/lib/Utils/constants.js +24 -0
- package/lib/Utils/crypto.js +133 -0
- package/lib/Utils/cstoken-utils.js +41 -0
- package/lib/Utils/decode-wa-message.js +406 -0
- package/lib/Utils/event-buffer.js +616 -0
- package/lib/Utils/generics.js +438 -0
- package/lib/Utils/history.js +147 -0
- package/lib/Utils/identity-change-handler.js +63 -0
- package/lib/Utils/index.js +29 -0
- package/lib/Utils/interactive-message-handler.js +478 -0
- package/lib/Utils/link-preview.js +91 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.js +6 -0
- package/lib/Utils/make-mutex.js +31 -0
- package/lib/Utils/message-retry-manager.js +291 -0
- package/lib/Utils/messages-media.js +881 -0
- package/lib/Utils/messages.js +1434 -0
- package/lib/Utils/noise-handler.js +229 -0
- package/lib/Utils/normalize-message-identity.js +73 -0
- package/lib/Utils/offline-node-processor.js +50 -0
- package/lib/Utils/pre-key-manager.js +119 -0
- package/lib/Utils/process-message.js +648 -0
- package/lib/Utils/reporting-utils.js +355 -0
- package/lib/Utils/rich-message-utils.js +264 -0
- package/lib/Utils/signal.js +233 -0
- package/lib/Utils/stanza-ack.js +33 -0
- package/lib/Utils/sticker-utils.js +221 -0
- package/lib/Utils/sync-action-utils.js +53 -0
- package/lib/Utils/tc-token-utils.js +182 -0
- package/lib/Utils/use-multi-file-auth-state.js +118 -0
- package/lib/Utils/use-prototype-auth-state.js +217 -0
- package/lib/Utils/use-single-file-auth-state.js +94 -0
- package/lib/Utils/validate-connection.js +235 -0
- package/lib/WABinary/constants.js +1468 -0
- package/lib/WABinary/decode.js +258 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.js +202 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.js +94 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.js +20669 -0
- package/lib/WAM/encode.js +151 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +59 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +20 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +29 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +59 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +21 -0
- package/lib/WAUSync/Protocols/index.js +4 -0
- package/lib/WAUSync/USyncQuery.js +133 -0
- package/lib/WAUSync/USyncUser.js +22 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +12 -0
- package/package.json +78 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { KEY_BUNDLE_TYPE } from "../Defaults/index.js";
|
|
2
|
+
import {
|
|
3
|
+
assertNodeErrorFree,
|
|
4
|
+
getBinaryNodeChild,
|
|
5
|
+
getBinaryNodeChildBuffer,
|
|
6
|
+
getBinaryNodeChildren,
|
|
7
|
+
getBinaryNodeChildUInt,
|
|
8
|
+
getServerFromDomainType,
|
|
9
|
+
jidDecode,
|
|
10
|
+
S_WHATSAPP_NET,
|
|
11
|
+
WAJIDDomains,
|
|
12
|
+
} from "../WABinary/index.js";
|
|
13
|
+
import { Curve, generateSignalPubKey } from "./crypto.js";
|
|
14
|
+
import { encodeBigEndian } from "./generics.js";
|
|
15
|
+
function chunk(array, size) {
|
|
16
|
+
const chunks = [];
|
|
17
|
+
for (let i = 0; i < array.length; i += size) {
|
|
18
|
+
chunks.push(array.slice(i, i + size));
|
|
19
|
+
}
|
|
20
|
+
return chunks;
|
|
21
|
+
}
|
|
22
|
+
export const createSignalIdentity = (wid, accountSignatureKey) => {
|
|
23
|
+
return {
|
|
24
|
+
identifier: { name: wid, deviceId: 0 },
|
|
25
|
+
identifierKey: generateSignalPubKey(accountSignatureKey),
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export const getPreKeys = async ({ get }, min, limit) => {
|
|
29
|
+
const idList = [];
|
|
30
|
+
for (let id = min; id < limit; id++) {
|
|
31
|
+
idList.push(id.toString());
|
|
32
|
+
}
|
|
33
|
+
return get("pre-key", idList);
|
|
34
|
+
};
|
|
35
|
+
export const generateOrGetPreKeys = (creds, range) => {
|
|
36
|
+
const avaliable = creds.nextPreKeyId - creds.firstUnuploadedPreKeyId;
|
|
37
|
+
const remaining = range - avaliable;
|
|
38
|
+
const lastPreKeyId = creds.nextPreKeyId + remaining - 1;
|
|
39
|
+
const newPreKeys = {};
|
|
40
|
+
if (remaining > 0) {
|
|
41
|
+
for (let i = creds.nextPreKeyId; i <= lastPreKeyId; i++) {
|
|
42
|
+
newPreKeys[i] = Curve.generateKeyPair();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
newPreKeys: newPreKeys,
|
|
47
|
+
lastPreKeyId: lastPreKeyId,
|
|
48
|
+
preKeysRange: [creds.firstUnuploadedPreKeyId, range],
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
export const xmppSignedPreKey = (key) => ({
|
|
52
|
+
tag: "skey",
|
|
53
|
+
attrs: {},
|
|
54
|
+
content: [
|
|
55
|
+
{ tag: "id", attrs: {}, content: encodeBigEndian(key.keyId, 3) },
|
|
56
|
+
{ tag: "value", attrs: {}, content: key.keyPair.public },
|
|
57
|
+
{ tag: "signature", attrs: {}, content: key.signature },
|
|
58
|
+
],
|
|
59
|
+
});
|
|
60
|
+
export const xmppPreKey = (pair, id) => ({
|
|
61
|
+
tag: "key",
|
|
62
|
+
attrs: {},
|
|
63
|
+
content: [
|
|
64
|
+
{ tag: "id", attrs: {}, content: encodeBigEndian(id, 3) },
|
|
65
|
+
{ tag: "value", attrs: {}, content: pair.public },
|
|
66
|
+
],
|
|
67
|
+
});
|
|
68
|
+
export const parseAndInjectE2ESessions = async (node, repository) => {
|
|
69
|
+
const extractKey = (key) =>
|
|
70
|
+
key
|
|
71
|
+
? {
|
|
72
|
+
keyId: getBinaryNodeChildUInt(key, "id", 3),
|
|
73
|
+
publicKey: generateSignalPubKey(
|
|
74
|
+
getBinaryNodeChildBuffer(key, "value"),
|
|
75
|
+
),
|
|
76
|
+
signature: getBinaryNodeChildBuffer(key, "signature"),
|
|
77
|
+
}
|
|
78
|
+
: undefined;
|
|
79
|
+
const nodes = getBinaryNodeChildren(getBinaryNodeChild(node, "list"), "user");
|
|
80
|
+
for (const node of nodes) {
|
|
81
|
+
assertNodeErrorFree(node);
|
|
82
|
+
}
|
|
83
|
+
// Most of the work in repository.injectE2ESession is CPU intensive, not IO
|
|
84
|
+
// So Promise.all doesn't really help here,
|
|
85
|
+
// but blocks even loop if we're using it inside keys.transaction, and it makes it "sync" actually
|
|
86
|
+
// This way we chunk it in smaller parts and between those parts we can yield to the event loop
|
|
87
|
+
// It's rare case when you need to E2E sessions for so many users, but it's possible
|
|
88
|
+
const chunkSize = 100;
|
|
89
|
+
const chunks = chunk(nodes, chunkSize);
|
|
90
|
+
for (const nodesChunk of chunks) {
|
|
91
|
+
for (const node of nodesChunk) {
|
|
92
|
+
const signedKey = getBinaryNodeChild(node, "skey");
|
|
93
|
+
const key = getBinaryNodeChild(node, "key");
|
|
94
|
+
const identity = getBinaryNodeChildBuffer(node, "identity");
|
|
95
|
+
const jid = node.attrs.jid;
|
|
96
|
+
const registrationId = getBinaryNodeChildUInt(node, "registration", 4);
|
|
97
|
+
await repository.injectE2ESession({
|
|
98
|
+
jid: jid,
|
|
99
|
+
session: {
|
|
100
|
+
registrationId: registrationId,
|
|
101
|
+
identityKey: generateSignalPubKey(identity),
|
|
102
|
+
signedPreKey: extractKey(signedKey),
|
|
103
|
+
preKey: extractKey(key),
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
export const extractDeviceJids = (result, myJid, myLid, excludeZeroDevices) => {
|
|
110
|
+
const { user: myUser, device: myDevice } = jidDecode(myJid);
|
|
111
|
+
const extracted = [];
|
|
112
|
+
for (const userResult of result) {
|
|
113
|
+
const { devices, id } = userResult;
|
|
114
|
+
const decoded = jidDecode(id),
|
|
115
|
+
{ user, server } = decoded;
|
|
116
|
+
let { domainType } = decoded;
|
|
117
|
+
const deviceList = devices?.deviceList;
|
|
118
|
+
if (!Array.isArray(deviceList)) continue;
|
|
119
|
+
for (const { id: device, keyIndex, isHosted } of deviceList) {
|
|
120
|
+
if (
|
|
121
|
+
(!excludeZeroDevices || device !== 0) && // if zero devices are not-excluded, or device is non zero
|
|
122
|
+
((myUser !== user && myLid !== user) || myDevice !== device) && // either different user or if me user, not this device
|
|
123
|
+
(device === 0 || !!keyIndex)
|
|
124
|
+
) {
|
|
125
|
+
if (isHosted) {
|
|
126
|
+
domainType =
|
|
127
|
+
domainType === WAJIDDomains.LID
|
|
128
|
+
? WAJIDDomains.HOSTED_LID
|
|
129
|
+
: WAJIDDomains.HOSTED;
|
|
130
|
+
}
|
|
131
|
+
extracted.push({
|
|
132
|
+
user: user,
|
|
133
|
+
device: device,
|
|
134
|
+
domainType: domainType,
|
|
135
|
+
server: getServerFromDomainType(server, domainType),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return extracted;
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* get the next N keys for upload or processing
|
|
144
|
+
* @param count number of pre-keys to get or generate
|
|
145
|
+
*/ export const getNextPreKeys = async ({ creds, keys }, count) => {
|
|
146
|
+
const { newPreKeys, lastPreKeyId, preKeysRange } = generateOrGetPreKeys(
|
|
147
|
+
creds,
|
|
148
|
+
count,
|
|
149
|
+
);
|
|
150
|
+
const update = {
|
|
151
|
+
nextPreKeyId: Math.max(lastPreKeyId + 1, creds.nextPreKeyId),
|
|
152
|
+
firstUnuploadedPreKeyId: Math.max(
|
|
153
|
+
creds.firstUnuploadedPreKeyId,
|
|
154
|
+
lastPreKeyId + 1,
|
|
155
|
+
),
|
|
156
|
+
};
|
|
157
|
+
await keys.set({ "pre-key": newPreKeys });
|
|
158
|
+
const preKeys = await getPreKeys(
|
|
159
|
+
keys,
|
|
160
|
+
preKeysRange[0],
|
|
161
|
+
preKeysRange[0] + preKeysRange[1],
|
|
162
|
+
);
|
|
163
|
+
return { update: update, preKeys: preKeys };
|
|
164
|
+
};
|
|
165
|
+
export const getNextPreKeysNode = async (state, count) => {
|
|
166
|
+
const { creds } = state;
|
|
167
|
+
const { update, preKeys } = await getNextPreKeys(state, count);
|
|
168
|
+
const node = {
|
|
169
|
+
tag: "iq",
|
|
170
|
+
attrs: { xmlns: "encrypt", type: "set", to: S_WHATSAPP_NET },
|
|
171
|
+
content: [
|
|
172
|
+
{
|
|
173
|
+
tag: "registration",
|
|
174
|
+
attrs: {},
|
|
175
|
+
content: encodeBigEndian(creds.registrationId),
|
|
176
|
+
},
|
|
177
|
+
{ tag: "type", attrs: {}, content: KEY_BUNDLE_TYPE },
|
|
178
|
+
{ tag: "identity", attrs: {}, content: creds.signedIdentityKey.public },
|
|
179
|
+
{
|
|
180
|
+
tag: "list",
|
|
181
|
+
attrs: {},
|
|
182
|
+
content: Object.keys(preKeys).map((k) => xmppPreKey(preKeys[+k], +k)),
|
|
183
|
+
},
|
|
184
|
+
xmppSignedPreKey(creds.signedPreKey),
|
|
185
|
+
],
|
|
186
|
+
};
|
|
187
|
+
return { update: update, node: node };
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const isValidUInt = (n) => typeof n === "number" && Number.isInteger(n);
|
|
191
|
+
export const extractE2ESessionFromRetryReceipt = (receipt) => {
|
|
192
|
+
const keysNode = getBinaryNodeChild(receipt, "keys");
|
|
193
|
+
if (!keysNode) return null;
|
|
194
|
+
const typeBuf = getBinaryNodeChildBuffer(keysNode, "type");
|
|
195
|
+
if (!typeBuf || typeBuf.length !== 1 || typeBuf[0] !== KEY_BUNDLE_TYPE[0])
|
|
196
|
+
return null;
|
|
197
|
+
const identity = getBinaryNodeChildBuffer(keysNode, "identity");
|
|
198
|
+
const skey = getBinaryNodeChild(keysNode, "skey");
|
|
199
|
+
if (!identity || identity.length !== 32 || !skey) return null;
|
|
200
|
+
const registrationId = getBinaryNodeChildUInt(receipt, "registration", 4);
|
|
201
|
+
if (!isValidUInt(registrationId)) return null;
|
|
202
|
+
const signedPubKey = getBinaryNodeChildBuffer(skey, "value");
|
|
203
|
+
const signedSig = getBinaryNodeChildBuffer(skey, "signature");
|
|
204
|
+
const signedKeyId = getBinaryNodeChildUInt(skey, "id", 3);
|
|
205
|
+
if (
|
|
206
|
+
!signedPubKey ||
|
|
207
|
+
signedPubKey.length !== 32 ||
|
|
208
|
+
!signedSig ||
|
|
209
|
+
!isValidUInt(signedKeyId)
|
|
210
|
+
) {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
const preKeyNode = getBinaryNodeChild(keysNode, "key");
|
|
214
|
+
let preKey;
|
|
215
|
+
if (preKeyNode) {
|
|
216
|
+
const preKeyPub = getBinaryNodeChildBuffer(preKeyNode, "value");
|
|
217
|
+
const preKeyId = getBinaryNodeChildUInt(preKeyNode, "id", 3);
|
|
218
|
+
if (!preKeyPub || preKeyPub.length !== 32 || !isValidUInt(preKeyId)) {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
preKey = { keyId: preKeyId, publicKey: generateSignalPubKey(preKeyPub) };
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
registrationId,
|
|
225
|
+
identityKey: generateSignalPubKey(identity),
|
|
226
|
+
signedPreKey: {
|
|
227
|
+
keyId: signedKeyId,
|
|
228
|
+
publicKey: generateSignalPubKey(signedPubKey),
|
|
229
|
+
signature: signedSig,
|
|
230
|
+
},
|
|
231
|
+
preKey,
|
|
232
|
+
};
|
|
233
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds an ACK stanza for a received node.
|
|
3
|
+
* Pure function -- no I/O, no side effects.
|
|
4
|
+
*
|
|
5
|
+
* Mirrors WhatsApp Web's ACK construction:
|
|
6
|
+
* - WAWebHandleMsgSendAck.sendAck / sendNack
|
|
7
|
+
* - WAWebCreateNackFromStanza.createNackFromStanza
|
|
8
|
+
*/
|
|
9
|
+
export function buildAckStanza(node, errorCode, meId) {
|
|
10
|
+
const { tag, attrs } = node;
|
|
11
|
+
const stanza = {
|
|
12
|
+
tag: "ack",
|
|
13
|
+
attrs: { id: attrs.id, to: attrs.from, class: tag },
|
|
14
|
+
};
|
|
15
|
+
if (errorCode) {
|
|
16
|
+
stanza.attrs.error = errorCode.toString();
|
|
17
|
+
}
|
|
18
|
+
if (attrs.participant) {
|
|
19
|
+
stanza.attrs.participant = attrs.participant;
|
|
20
|
+
}
|
|
21
|
+
if (attrs.recipient) {
|
|
22
|
+
stanza.attrs.recipient = attrs.recipient;
|
|
23
|
+
}
|
|
24
|
+
// WA Web always includes type when present: `n.type || DROP_ATTR`
|
|
25
|
+
if (attrs.type) {
|
|
26
|
+
stanza.attrs.type = attrs.type;
|
|
27
|
+
}
|
|
28
|
+
// WA Web WAWebHandleMsgSendAck.sendAck/sendNack always include `from` for message-class ACKs
|
|
29
|
+
if (tag === "message" && meId) {
|
|
30
|
+
stanza.attrs.from = meId;
|
|
31
|
+
}
|
|
32
|
+
return stanza;
|
|
33
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { exec } from "child_process";
|
|
2
|
+
import { promises as fs } from "fs";
|
|
3
|
+
import { tmpdir } from "os";
|
|
4
|
+
import { join } from "path";
|
|
5
|
+
import { generateMessageIDV2 } from "./generics.js";
|
|
6
|
+
import { getImageProcessingLibrary } from "./messages-media.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Add metadata (EXIF) to a WebP buffer
|
|
10
|
+
*/
|
|
11
|
+
export function writeExif(webpBuffer, metadata) {
|
|
12
|
+
const json = {
|
|
13
|
+
"sticker-pack-id": metadata.stickerPackId || "",
|
|
14
|
+
"sticker-pack-name": metadata.packname || "",
|
|
15
|
+
"sticker-pack-publisher": metadata.author || "",
|
|
16
|
+
emojis: metadata.categories || [""],
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const exifAttr = Buffer.from([
|
|
20
|
+
0x49, 0x49, 0x2a, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57,
|
|
21
|
+
0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00,
|
|
22
|
+
]);
|
|
23
|
+
const jsonBuff = Buffer.from(JSON.stringify(json));
|
|
24
|
+
const exifRaw = Buffer.concat([exifAttr, jsonBuff]);
|
|
25
|
+
exifRaw.writeUInt32LE(jsonBuff.length, 14);
|
|
26
|
+
|
|
27
|
+
const exifHeader = Buffer.alloc(8);
|
|
28
|
+
exifHeader.write("EXIF", 0);
|
|
29
|
+
exifHeader.writeUInt32LE(exifRaw.length, 4);
|
|
30
|
+
const exifChunk = Buffer.concat([
|
|
31
|
+
exifHeader,
|
|
32
|
+
exifRaw,
|
|
33
|
+
exifRaw.length % 2 ? Buffer.from([0]) : Buffer.alloc(0),
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
let offset = 12;
|
|
37
|
+
let chunks = [];
|
|
38
|
+
while (offset < webpBuffer.length - 8) {
|
|
39
|
+
const chunkFourCC = webpBuffer.toString("ascii", offset, offset + 4);
|
|
40
|
+
const chunkSize = webpBuffer.readUInt32LE(offset + 4);
|
|
41
|
+
const totalSize = 8 + chunkSize + (chunkSize % 2);
|
|
42
|
+
chunks.push({
|
|
43
|
+
fourCC: chunkFourCC,
|
|
44
|
+
data: webpBuffer.subarray(offset, offset + totalSize),
|
|
45
|
+
});
|
|
46
|
+
offset += totalSize;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let vp8x = chunks.find((c) => c.fourCC === "VP8X");
|
|
50
|
+
if (vp8x) {
|
|
51
|
+
vp8x.data = Buffer.from(vp8x.data);
|
|
52
|
+
vp8x.data[8] |= 0b00001000; // Enable EXIF bit
|
|
53
|
+
} else {
|
|
54
|
+
let width = 0,
|
|
55
|
+
height = 0,
|
|
56
|
+
hasAlpha = false,
|
|
57
|
+
hasAnim = false;
|
|
58
|
+
for (const c of chunks) {
|
|
59
|
+
if (c.fourCC === "VP8 ") {
|
|
60
|
+
const data = c.data.subarray(8);
|
|
61
|
+
width = ((data[7] << 8) | data[6]) & 0x3fff;
|
|
62
|
+
height = ((data[9] << 8) | data[8]) & 0x3fff;
|
|
63
|
+
} else if (c.fourCC === "VP8L") {
|
|
64
|
+
const data = c.data.subarray(8);
|
|
65
|
+
width = (((data[2] & 0x3f) << 8) | data[1]) + 1;
|
|
66
|
+
height =
|
|
67
|
+
((((data[4] << 16) | (data[3] << 8) | data[2]) >> 6) & 0x3fff) + 1;
|
|
68
|
+
hasAlpha = !!(data[4] & 0x10);
|
|
69
|
+
} else if (c.fourCC === "ALPH") {
|
|
70
|
+
hasAlpha = true;
|
|
71
|
+
} else if (c.fourCC === "ANIM") {
|
|
72
|
+
hasAnim = true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const vp8xBuf = Buffer.alloc(18);
|
|
77
|
+
vp8xBuf.write("VP8X", 0);
|
|
78
|
+
vp8xBuf.writeUInt32LE(10, 4);
|
|
79
|
+
let flags = 0b00001000; // EXIF
|
|
80
|
+
if (hasAlpha) flags |= 0x10;
|
|
81
|
+
if (hasAnim) flags |= 0x02;
|
|
82
|
+
vp8xBuf[8] = flags;
|
|
83
|
+
|
|
84
|
+
vp8xBuf.writeUIntLE((width || 512) - 1, 12, 3);
|
|
85
|
+
vp8xBuf.writeUIntLE((height || 512) - 1, 15, 3);
|
|
86
|
+
vp8x = { fourCC: "VP8X", data: vp8xBuf };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const otherChunks = chunks.filter(
|
|
90
|
+
(c) => c.fourCC !== "VP8X" && c.fourCC !== "EXIF",
|
|
91
|
+
);
|
|
92
|
+
const out = Buffer.concat([
|
|
93
|
+
webpBuffer.subarray(0, 12),
|
|
94
|
+
vp8x.data,
|
|
95
|
+
exifChunk,
|
|
96
|
+
...otherChunks.map((c) => c.data),
|
|
97
|
+
]);
|
|
98
|
+
|
|
99
|
+
out.writeUInt32LE(out.length - 8, 4);
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Checks if a WebP buffer is animated
|
|
105
|
+
*/
|
|
106
|
+
export const isAnimatedWebP = (buffer) => {
|
|
107
|
+
if (
|
|
108
|
+
buffer.length < 12 ||
|
|
109
|
+
buffer[0] !== 82 ||
|
|
110
|
+
buffer[1] !== 73 ||
|
|
111
|
+
buffer[2] !== 70 ||
|
|
112
|
+
buffer[3] !== 70 ||
|
|
113
|
+
buffer[8] !== 87 ||
|
|
114
|
+
buffer[9] !== 69 ||
|
|
115
|
+
buffer[10] !== 66 ||
|
|
116
|
+
buffer[11] !== 80
|
|
117
|
+
) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
let offset = 12;
|
|
121
|
+
while (offset < buffer.length - 8) {
|
|
122
|
+
const chunkFourCC = buffer.toString("ascii", offset, offset + 4);
|
|
123
|
+
const chunkSize = buffer.readUInt32LE(offset + 4);
|
|
124
|
+
if (chunkFourCC === "VP8X") {
|
|
125
|
+
const flagsOffset = offset + 8;
|
|
126
|
+
if (flagsOffset < buffer.length) {
|
|
127
|
+
const flags = buffer[flagsOffset];
|
|
128
|
+
if (flags & 2) {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
} else if (chunkFourCC === "ANIM" || chunkFourCC === "ANMF") {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
offset += 8 + chunkSize + (chunkSize % 2);
|
|
136
|
+
}
|
|
137
|
+
return false;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Checks if a buffer is a WebP file
|
|
142
|
+
*/
|
|
143
|
+
export const isWebPBuffer = (buffer) => {
|
|
144
|
+
return (
|
|
145
|
+
buffer.length >= 12 &&
|
|
146
|
+
buffer[0] === 82 &&
|
|
147
|
+
buffer[1] === 73 &&
|
|
148
|
+
buffer[2] === 70 &&
|
|
149
|
+
buffer[3] === 70 &&
|
|
150
|
+
buffer[8] === 87 &&
|
|
151
|
+
buffer[9] === 69 &&
|
|
152
|
+
buffer[10] === 66 &&
|
|
153
|
+
buffer[11] === 80
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Convert media buffer (image, video, sticker) to WebP sticker with EXIF
|
|
159
|
+
*/
|
|
160
|
+
export async function mediaToWebp(buffer, metadata = {}) {
|
|
161
|
+
const author = metadata.author || "TAKASUGI";
|
|
162
|
+
const packName = metadata.packName || "SHINSUKE";
|
|
163
|
+
|
|
164
|
+
let webpBuffer;
|
|
165
|
+
const isWebp = isWebPBuffer(buffer);
|
|
166
|
+
const isAnimated = isWebp && isAnimatedWebP(buffer);
|
|
167
|
+
|
|
168
|
+
const lib = await getImageProcessingLibrary();
|
|
169
|
+
|
|
170
|
+
if (lib.sharp) {
|
|
171
|
+
// Try processing with sharp first (handles images and both static/animated webp)
|
|
172
|
+
try {
|
|
173
|
+
webpBuffer = await lib
|
|
174
|
+
.sharp(buffer, isAnimated ? { animated: true } : {})
|
|
175
|
+
.resize(512, 512, { fit: "inside" })
|
|
176
|
+
.webp()
|
|
177
|
+
.toBuffer();
|
|
178
|
+
} catch (e) {
|
|
179
|
+
// Fallback to ffmpeg if sharp fails (e.g. video or unsupported format)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (!webpBuffer) {
|
|
184
|
+
// Handle as video or unsupported sharp format using ffmpeg
|
|
185
|
+
const tmpFileIn = join(tmpdir(), `sticker-in-${generateMessageIDV2()}`);
|
|
186
|
+
const tmpFileOut = join(
|
|
187
|
+
tmpdir(),
|
|
188
|
+
`sticker-out-${generateMessageIDV2()}.webp`,
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
await fs.writeFile(tmpFileIn, buffer);
|
|
192
|
+
|
|
193
|
+
try {
|
|
194
|
+
await new Promise((resolve, reject) => {
|
|
195
|
+
// Optimized ffmpeg command for stickers (max 6s, 512x512, an, contain fit)
|
|
196
|
+
const ffmpegCmd = `ffmpeg -i ${tmpFileIn} -vcodec libwebp -filter:v "fps=fps=20,scale=512:512:flags=lanczos:force_original_aspect_ratio=decrease,pad=512:512:(ow-iw)/2:(oh-ih)/2:color=#00000000" -lossless 1 -loop 0 -preset default -an -vsync 0 -t 6 ${tmpFileOut}`;
|
|
197
|
+
exec(ffmpegCmd, (err) => {
|
|
198
|
+
if (err) reject(err);
|
|
199
|
+
else resolve();
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
webpBuffer = await fs.readFile(tmpFileOut);
|
|
204
|
+
} finally {
|
|
205
|
+
await Promise.all([
|
|
206
|
+
fs.unlink(tmpFileIn).catch(() => {}),
|
|
207
|
+
fs.unlink(tmpFileOut).catch(() => {}),
|
|
208
|
+
]);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (!webpBuffer) {
|
|
213
|
+
throw new Error("Failed to convert media to WebP");
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return writeExif(webpBuffer, {
|
|
217
|
+
author,
|
|
218
|
+
packname: packName,
|
|
219
|
+
stickerPackId: generateMessageIDV2(),
|
|
220
|
+
});
|
|
221
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { proto } from "../../WAProto/index.js";
|
|
2
|
+
import { isLidUser, isPnUser } from "../WABinary/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Process contactAction and return events to emit.
|
|
5
|
+
* Pure function - no side effects.
|
|
6
|
+
*/ export const processContactAction = (action, id, logger) => {
|
|
7
|
+
const results = [];
|
|
8
|
+
if (!id) {
|
|
9
|
+
logger?.warn(
|
|
10
|
+
{
|
|
11
|
+
hasFullName: !!action.fullName,
|
|
12
|
+
hasLidJid: !!action.lidJid,
|
|
13
|
+
hasPnJid: !!action.pnJid,
|
|
14
|
+
},
|
|
15
|
+
"contactAction sync: missing id in index",
|
|
16
|
+
);
|
|
17
|
+
return results;
|
|
18
|
+
}
|
|
19
|
+
const lidJid = action.lidJid;
|
|
20
|
+
const idIsPn = isPnUser(id);
|
|
21
|
+
// PN is in index[1], not in contactAction.pnJid which is usually null
|
|
22
|
+
const phoneNumber = idIsPn ? id : action.pnJid || undefined;
|
|
23
|
+
// Always emit contacts.upsert
|
|
24
|
+
results.push({
|
|
25
|
+
event: "contacts.upsert",
|
|
26
|
+
data: [
|
|
27
|
+
{
|
|
28
|
+
id: id,
|
|
29
|
+
name:
|
|
30
|
+
action.fullName || action.firstName || action.username || undefined,
|
|
31
|
+
lid: lidJid || undefined,
|
|
32
|
+
phoneNumber: phoneNumber,
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
});
|
|
36
|
+
// Emit lid-mapping.update if we have valid LID-PN pair
|
|
37
|
+
if (lidJid && isLidUser(lidJid) && idIsPn) {
|
|
38
|
+
results.push({
|
|
39
|
+
event: "lid-mapping.update",
|
|
40
|
+
data: { lid: lidJid, pn: id },
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return results;
|
|
44
|
+
};
|
|
45
|
+
export const emitSyncActionResults = (ev, results) => {
|
|
46
|
+
for (const result of results) {
|
|
47
|
+
if (result.event === "contacts.upsert") {
|
|
48
|
+
ev.emit("contacts.upsert", result.data);
|
|
49
|
+
} else {
|
|
50
|
+
ev.emit("lid-mapping.update", result.data);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|