@kelvdra/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 +1478 -0
- package/WAProto/GenerateStatics.sh +3 -0
- package/WAProto/WAProto.proto +4633 -0
- package/WAProto/fix-imports.js +29 -0
- package/WAProto/index.d.ts +37016 -0
- package/WAProto/index.js +169659 -0
- package/engine-requirements.js +10 -0
- package/lib/Defaults/index.d.ts +62 -0
- package/lib/Defaults/index.d.ts.map +1 -0
- package/lib/Defaults/index.js +115 -0
- package/lib/Defaults/index.js.map +1 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
- package/lib/Signal/Group/ciphertext-message.d.ts.map +1 -0
- package/lib/Signal/Group/ciphertext-message.js +12 -0
- package/lib/Signal/Group/ciphertext-message.js.map +1 -0
- package/lib/Signal/Group/group-session-builder.d.ts +15 -0
- package/lib/Signal/Group/group-session-builder.d.ts.map +1 -0
- package/lib/Signal/Group/group-session-builder.js +30 -0
- package/lib/Signal/Group/group-session-builder.js.map +1 -0
- package/lib/Signal/Group/group_cipher.d.ts +17 -0
- package/lib/Signal/Group/group_cipher.d.ts.map +1 -0
- package/lib/Signal/Group/group_cipher.js +82 -0
- package/lib/Signal/Group/group_cipher.js.map +1 -0
- package/lib/Signal/Group/index.d.ts +12 -0
- package/lib/Signal/Group/index.d.ts.map +1 -0
- package/lib/Signal/Group/index.js +12 -0
- package/lib/Signal/Group/index.js.map +1 -0
- package/lib/Signal/Group/keyhelper.d.ts +11 -0
- package/lib/Signal/Group/keyhelper.d.ts.map +1 -0
- package/lib/Signal/Group/keyhelper.js +18 -0
- package/lib/Signal/Group/keyhelper.js.map +1 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
- package/lib/Signal/Group/sender-chain-key.d.ts.map +1 -0
- package/lib/Signal/Group/sender-chain-key.js +26 -0
- package/lib/Signal/Group/sender-chain-key.js.map +1 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
- package/lib/Signal/Group/sender-key-distribution-message.js.map +1 -0
- package/lib/Signal/Group/sender-key-message.d.ts +19 -0
- package/lib/Signal/Group/sender-key-message.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js.map +1 -0
- package/lib/Signal/Group/sender-key-name.d.ts +18 -0
- package/lib/Signal/Group/sender-key-name.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-name.js +48 -0
- package/lib/Signal/Group/sender-key-name.js.map +1 -0
- package/lib/Signal/Group/sender-key-record.d.ts +31 -0
- package/lib/Signal/Group/sender-key-record.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-record.js +41 -0
- package/lib/Signal/Group/sender-key-record.js.map +1 -0
- package/lib/Signal/Group/sender-key-state.d.ts +39 -0
- package/lib/Signal/Group/sender-key-state.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-state.js +84 -0
- package/lib/Signal/Group/sender-key-state.js.map +1 -0
- package/lib/Signal/Group/sender-message-key.d.ts +12 -0
- package/lib/Signal/Group/sender-message-key.d.ts.map +1 -0
- package/lib/Signal/Group/sender-message-key.js +26 -0
- package/lib/Signal/Group/sender-message-key.js.map +1 -0
- package/lib/Signal/libsignal.d.ts +5 -0
- package/lib/Signal/libsignal.d.ts.map +1 -0
- package/lib/Signal/libsignal.js +342 -0
- package/lib/Signal/libsignal.js.map +1 -0
- package/lib/Signal/lid-mapping.d.ts +23 -0
- package/lib/Signal/lid-mapping.d.ts.map +1 -0
- package/lib/Signal/lid-mapping.js +171 -0
- package/lib/Signal/lid-mapping.js.map +1 -0
- package/lib/Socket/Client/index.d.ts +3 -0
- package/lib/Socket/Client/index.d.ts.map +1 -0
- package/lib/Socket/Client/index.js +3 -0
- package/lib/Socket/Client/index.js.map +1 -0
- package/lib/Socket/Client/types.d.ts +16 -0
- package/lib/Socket/Client/types.d.ts.map +1 -0
- package/lib/Socket/Client/types.js +11 -0
- package/lib/Socket/Client/types.js.map +1 -0
- package/lib/Socket/Client/websocket.d.ts +13 -0
- package/lib/Socket/Client/websocket.d.ts.map +1 -0
- package/lib/Socket/Client/websocket.js +50 -0
- package/lib/Socket/Client/websocket.js.map +1 -0
- package/lib/Socket/business.d.ts +188 -0
- package/lib/Socket/business.d.ts.map +1 -0
- package/lib/Socket/business.js +376 -0
- package/lib/Socket/business.js.map +1 -0
- package/lib/Socket/chats.d.ts +98 -0
- package/lib/Socket/chats.d.ts.map +1 -0
- package/lib/Socket/chats.js +962 -0
- package/lib/Socket/chats.js.map +1 -0
- package/lib/Socket/communities.d.ts +244 -0
- package/lib/Socket/communities.d.ts.map +1 -0
- package/lib/Socket/communities.js +431 -0
- package/lib/Socket/communities.js.map +1 -0
- package/lib/Socket/groups.d.ts +137 -0
- package/lib/Socket/groups.d.ts.map +1 -0
- package/lib/Socket/groups.js +327 -0
- package/lib/Socket/groups.js.map +1 -0
- package/lib/Socket/hydra.d.ts +174 -0
- package/lib/Socket/hydra.js +715 -0
- package/lib/Socket/index.d.ts +231 -0
- package/lib/Socket/index.d.ts.map +1 -0
- package/lib/Socket/index.js +18 -0
- package/lib/Socket/index.js.map +1 -0
- package/lib/Socket/messages-recv.d.ts +173 -0
- package/lib/Socket/messages-recv.d.ts.map +1 -0
- package/lib/Socket/messages-recv.js +1228 -0
- package/lib/Socket/messages-recv.js.map +1 -0
- package/lib/Socket/messages-send.d.ts +169 -0
- package/lib/Socket/messages-send.d.ts.map +1 -0
- package/lib/Socket/messages-send.js +1367 -0
- package/lib/Socket/messages-send.js.map +1 -0
- package/lib/Socket/mex.d.ts +3 -0
- package/lib/Socket/mex.d.ts.map +1 -0
- package/lib/Socket/mex.js +42 -0
- package/lib/Socket/mex.js.map +1 -0
- package/lib/Socket/newsletter.d.ts +147 -0
- package/lib/Socket/newsletter.d.ts.map +1 -0
- package/lib/Socket/newsletter.js +181 -0
- package/lib/Socket/newsletter.js.map +1 -0
- package/lib/Socket/socket.d.ts +51 -0
- package/lib/Socket/socket.d.ts.map +1 -0
- package/lib/Socket/socket.js +841 -0
- package/lib/Socket/socket.js.map +1 -0
- package/lib/Store/index.d.ts +4 -0
- package/lib/Store/index.js +4 -0
- package/lib/Store/make-cache-manager-store.d.ts +14 -0
- package/lib/Store/make-cache-manager-store.js +81 -0
- package/lib/Store/make-in-memory-store.d.ts +123 -0
- package/lib/Store/make-in-memory-store.js +416 -0
- package/lib/Store/make-ordered-dictionary.d.ts +12 -0
- package/lib/Store/make-ordered-dictionary.js +82 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +31 -0
- package/lib/Types/Auth.d.ts +111 -0
- package/lib/Types/Auth.d.ts.map +1 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Auth.js.map +1 -0
- package/lib/Types/Bussines.d.ts +25 -0
- package/lib/Types/Bussines.d.ts.map +1 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Bussines.js.map +1 -0
- package/lib/Types/Call.d.ts +14 -0
- package/lib/Types/Call.d.ts.map +1 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Call.js.map +1 -0
- package/lib/Types/Chat.d.ts +123 -0
- package/lib/Types/Chat.d.ts.map +1 -0
- package/lib/Types/Chat.js +8 -0
- package/lib/Types/Chat.js.map +1 -0
- package/lib/Types/Contact.d.ts +24 -0
- package/lib/Types/Contact.d.ts.map +1 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Contact.js.map +1 -0
- package/lib/Types/Events.d.ts +202 -0
- package/lib/Types/Events.d.ts.map +1 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/Events.js.map +1 -0
- package/lib/Types/GroupMetadata.d.ts +67 -0
- package/lib/Types/GroupMetadata.d.ts.map +1 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/GroupMetadata.js.map +1 -0
- package/lib/Types/Label.d.ts +47 -0
- package/lib/Types/Label.d.ts.map +1 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/Label.js.map +1 -0
- package/lib/Types/LabelAssociation.d.ts +30 -0
- package/lib/Types/LabelAssociation.d.ts.map +1 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/LabelAssociation.js.map +1 -0
- package/lib/Types/Message.d.ts +303 -0
- package/lib/Types/Message.d.ts.map +1 -0
- package/lib/Types/Message.js +11 -0
- package/lib/Types/Message.js.map +1 -0
- package/lib/Types/Newsletter.d.ts +135 -0
- package/lib/Types/Newsletter.d.ts.map +1 -0
- package/lib/Types/Newsletter.js +31 -0
- package/lib/Types/Newsletter.js.map +1 -0
- package/lib/Types/Product.d.ts +79 -0
- package/lib/Types/Product.d.ts.map +1 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Product.js.map +1 -0
- package/lib/Types/Signal.d.ts +76 -0
- package/lib/Types/Signal.d.ts.map +1 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Signal.js.map +1 -0
- package/lib/Types/Socket.d.ts +133 -0
- package/lib/Types/Socket.d.ts.map +1 -0
- package/lib/Types/Socket.js +3 -0
- package/lib/Types/Socket.js.map +1 -0
- package/lib/Types/State.d.ts +39 -0
- package/lib/Types/State.d.ts.map +1 -0
- package/lib/Types/State.js +13 -0
- package/lib/Types/State.js.map +1 -0
- package/lib/Types/USync.d.ts +26 -0
- package/lib/Types/USync.d.ts.map +1 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/USync.js.map +1 -0
- package/lib/Types/index.d.ts +65 -0
- package/lib/Types/index.d.ts.map +1 -0
- package/lib/Types/index.js +26 -0
- package/lib/Types/index.js.map +1 -0
- package/lib/Utils/auth-utils.d.ts +19 -0
- package/lib/Utils/auth-utils.d.ts.map +1 -0
- package/lib/Utils/auth-utils.js +257 -0
- package/lib/Utils/auth-utils.js.map +1 -0
- package/lib/Utils/baileys-event-stream.d.ts +17 -0
- package/lib/Utils/baileys-event-stream.d.ts.map +1 -0
- package/lib/Utils/baileys-event-stream.js +56 -0
- package/lib/Utils/baileys-event-stream.js.map +1 -0
- package/lib/Utils/browser-utils.d.ts +4 -0
- package/lib/Utils/browser-utils.d.ts.map +1 -0
- package/lib/Utils/browser-utils.js +28 -0
- package/lib/Utils/browser-utils.js.map +1 -0
- package/lib/Utils/business.d.ts +23 -0
- package/lib/Utils/business.d.ts.map +1 -0
- package/lib/Utils/business.js +231 -0
- package/lib/Utils/business.js.map +1 -0
- package/lib/Utils/chat-utils.d.ts +70 -0
- package/lib/Utils/chat-utils.d.ts.map +1 -0
- package/lib/Utils/chat-utils.js +763 -0
- package/lib/Utils/chat-utils.js.map +1 -0
- package/lib/Utils/crypto.d.ts +41 -0
- package/lib/Utils/crypto.d.ts.map +1 -0
- package/lib/Utils/crypto.js +142 -0
- package/lib/Utils/crypto.js.map +1 -0
- package/lib/Utils/decode-wa-message.d.ts +48 -0
- package/lib/Utils/decode-wa-message.d.ts.map +1 -0
- package/lib/Utils/decode-wa-message.js +279 -0
- package/lib/Utils/decode-wa-message.js.map +1 -0
- package/lib/Utils/event-buffer.d.ts +34 -0
- package/lib/Utils/event-buffer.d.ts.map +1 -0
- package/lib/Utils/event-buffer.js +548 -0
- package/lib/Utils/event-buffer.js.map +1 -0
- package/lib/Utils/generics.d.ts +90 -0
- package/lib/Utils/generics.d.ts.map +1 -0
- package/lib/Utils/generics.js +381 -0
- package/lib/Utils/generics.js.map +1 -0
- package/lib/Utils/history.d.ts +19 -0
- package/lib/Utils/history.d.ts.map +1 -0
- package/lib/Utils/history.js +84 -0
- package/lib/Utils/history.js.map +1 -0
- package/lib/Utils/index.d.ts +20 -0
- package/lib/Utils/index.d.ts.map +1 -0
- package/lib/Utils/index.js +20 -0
- package/lib/Utils/index.js.map +1 -0
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.d.ts.map +1 -0
- package/lib/Utils/link-preview.js +85 -0
- package/lib/Utils/link-preview.js.map +1 -0
- package/lib/Utils/logger.d.ts +12 -0
- package/lib/Utils/logger.d.ts.map +1 -0
- package/lib/Utils/logger.js +3 -0
- package/lib/Utils/logger.js.map +1 -0
- package/lib/Utils/lt-hash.d.ts +13 -0
- package/lib/Utils/lt-hash.d.ts.map +1 -0
- package/lib/Utils/lt-hash.js +48 -0
- package/lib/Utils/lt-hash.js.map +1 -0
- package/lib/Utils/make-mutex.d.ts +8 -0
- package/lib/Utils/make-mutex.d.ts.map +1 -0
- package/lib/Utils/make-mutex.js +40 -0
- package/lib/Utils/make-mutex.js.map +1 -0
- package/lib/Utils/message-retry-manager.d.ts +82 -0
- package/lib/Utils/message-retry-manager.d.ts.map +1 -0
- package/lib/Utils/message-retry-manager.js +149 -0
- package/lib/Utils/message-retry-manager.js.map +1 -0
- package/lib/Utils/messages-media.d.ts +114 -0
- package/lib/Utils/messages-media.d.ts.map +1 -0
- package/lib/Utils/messages-media.js +684 -0
- package/lib/Utils/messages-media.js.map +1 -0
- package/lib/Utils/messages.d.ts +76 -0
- package/lib/Utils/messages.d.ts.map +1 -0
- package/lib/Utils/messages.js +820 -0
- package/lib/Utils/messages.js.map +1 -0
- package/lib/Utils/noise-handler.d.ts +20 -0
- package/lib/Utils/noise-handler.d.ts.map +1 -0
- package/lib/Utils/noise-handler.js +147 -0
- package/lib/Utils/noise-handler.js.map +1 -0
- package/lib/Utils/pre-key-manager.d.ts +28 -0
- package/lib/Utils/pre-key-manager.d.ts.map +1 -0
- package/lib/Utils/pre-key-manager.js +106 -0
- package/lib/Utils/pre-key-manager.js.map +1 -0
- package/lib/Utils/process-message.d.ts +42 -0
- package/lib/Utils/process-message.d.ts.map +1 -0
- package/lib/Utils/process-message.js +413 -0
- package/lib/Utils/process-message.js.map +1 -0
- package/lib/Utils/signal.d.ts +34 -0
- package/lib/Utils/signal.d.ts.map +1 -0
- package/lib/Utils/signal.js +159 -0
- package/lib/Utils/signal.js.map +1 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
- package/lib/Utils/use-multi-file-auth-state.js +121 -0
- package/lib/Utils/use-multi-file-auth-state.js.map +1 -0
- package/lib/Utils/validate-connection.d.ts +11 -0
- package/lib/Utils/validate-connection.d.ts.map +1 -0
- package/lib/Utils/validate-connection.js +195 -0
- package/lib/Utils/validate-connection.js.map +1 -0
- package/lib/WABinary/constants.d.ts +28 -0
- package/lib/WABinary/constants.d.ts.map +1 -0
- package/lib/WABinary/constants.js +1301 -0
- package/lib/WABinary/constants.js.map +1 -0
- package/lib/WABinary/decode.d.ts +7 -0
- package/lib/WABinary/decode.d.ts.map +1 -0
- package/lib/WABinary/decode.js +238 -0
- package/lib/WABinary/decode.js.map +1 -0
- package/lib/WABinary/encode.d.ts +3 -0
- package/lib/WABinary/encode.d.ts.map +1 -0
- package/lib/WABinary/encode.js +216 -0
- package/lib/WABinary/encode.js.map +1 -0
- package/lib/WABinary/generic-utils.d.ts +15 -0
- package/lib/WABinary/generic-utils.d.ts.map +1 -0
- package/lib/WABinary/generic-utils.js +111 -0
- package/lib/WABinary/generic-utils.js.map +1 -0
- package/lib/WABinary/index.d.ts +6 -0
- package/lib/WABinary/index.d.ts.map +1 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/index.js.map +1 -0
- package/lib/WABinary/jid-utils.d.ts +48 -0
- package/lib/WABinary/jid-utils.d.ts.map +1 -0
- package/lib/WABinary/jid-utils.js +96 -0
- package/lib/WABinary/jid-utils.js.map +1 -0
- package/lib/WABinary/types.d.ts +19 -0
- package/lib/WABinary/types.d.ts.map +1 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WABinary/types.js.map +1 -0
- package/lib/WAM/BinaryInfo.d.ts +9 -0
- package/lib/WAM/BinaryInfo.d.ts.map +1 -0
- package/lib/WAM/BinaryInfo.js +10 -0
- package/lib/WAM/BinaryInfo.js.map +1 -0
- package/lib/WAM/constants.d.ts +40 -0
- package/lib/WAM/constants.d.ts.map +1 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/constants.js.map +1 -0
- package/lib/WAM/encode.d.ts +3 -0
- package/lib/WAM/encode.d.ts.map +1 -0
- package/lib/WAM/encode.js +150 -0
- package/lib/WAM/encode.js.map +1 -0
- package/lib/WAM/index.d.ts +4 -0
- package/lib/WAM/index.d.ts.map +1 -0
- package/lib/WAM/index.js +4 -0
- package/lib/WAM/index.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +29 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/index.d.ts +5 -0
- package/lib/WAUSync/Protocols/index.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/index.js +5 -0
- package/lib/WAUSync/Protocols/index.js.map +1 -0
- package/lib/WAUSync/USyncQuery.d.ts +29 -0
- package/lib/WAUSync/USyncQuery.d.ts.map +1 -0
- package/lib/WAUSync/USyncQuery.js +94 -0
- package/lib/WAUSync/USyncQuery.js.map +1 -0
- package/lib/WAUSync/USyncUser.d.ts +13 -0
- package/lib/WAUSync/USyncUser.d.ts.map +1 -0
- package/lib/WAUSync/USyncUser.js +23 -0
- package/lib/WAUSync/USyncUser.js.map +1 -0
- package/lib/WAUSync/index.d.ts +4 -0
- package/lib/WAUSync/index.d.ts.map +1 -0
- package/lib/WAUSync/index.js +4 -0
- package/lib/WAUSync/index.js.map +1 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +12 -0
- package/lib/index.js.map +1 -0
- package/package.json +104 -0
|
@@ -0,0 +1,763 @@
|
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import { proto } from '../../WAProto/index.js';
|
|
3
|
+
import { LabelAssociationType } from '../Types/LabelAssociation.js';
|
|
4
|
+
import { getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidNormalizedUser } from '../WABinary/index.js';
|
|
5
|
+
import { aesDecrypt, aesEncrypt, hkdf, hmacSign } from './crypto.js';
|
|
6
|
+
import { toNumber } from './generics.js';
|
|
7
|
+
import { LT_HASH_ANTI_TAMPERING } from './lt-hash.js';
|
|
8
|
+
import { downloadContentFromMessage } from './messages-media.js';
|
|
9
|
+
const mutationKeys = async (keydata) => {
|
|
10
|
+
const expanded = await hkdf(keydata, 160, { info: 'WhatsApp Mutation Keys' });
|
|
11
|
+
return {
|
|
12
|
+
indexKey: expanded.slice(0, 32),
|
|
13
|
+
valueEncryptionKey: expanded.slice(32, 64),
|
|
14
|
+
valueMacKey: expanded.slice(64, 96),
|
|
15
|
+
snapshotMacKey: expanded.slice(96, 128),
|
|
16
|
+
patchMacKey: expanded.slice(128, 160)
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
const generateMac = (operation, data, keyId, key) => {
|
|
20
|
+
const getKeyData = () => {
|
|
21
|
+
let r;
|
|
22
|
+
switch (operation) {
|
|
23
|
+
case proto.SyncdMutation.SyncdOperation.SET:
|
|
24
|
+
r = 0x01;
|
|
25
|
+
break;
|
|
26
|
+
case proto.SyncdMutation.SyncdOperation.REMOVE:
|
|
27
|
+
r = 0x02;
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
const buff = Buffer.from([r]);
|
|
31
|
+
return Buffer.concat([buff, Buffer.from(keyId, 'base64')]);
|
|
32
|
+
};
|
|
33
|
+
const keyData = getKeyData();
|
|
34
|
+
const last = Buffer.alloc(8); // 8 bytes
|
|
35
|
+
last.set([keyData.length], last.length - 1);
|
|
36
|
+
const total = Buffer.concat([keyData, data, last]);
|
|
37
|
+
const hmac = hmacSign(total, key, 'sha512');
|
|
38
|
+
return hmac.slice(0, 32);
|
|
39
|
+
};
|
|
40
|
+
const to64BitNetworkOrder = (e) => {
|
|
41
|
+
const buff = Buffer.alloc(8);
|
|
42
|
+
buff.writeUint32BE(e, 4);
|
|
43
|
+
return buff;
|
|
44
|
+
};
|
|
45
|
+
const makeLtHashGenerator = ({ indexValueMap, hash }) => {
|
|
46
|
+
indexValueMap = { ...indexValueMap };
|
|
47
|
+
const addBuffs = [];
|
|
48
|
+
const subBuffs = [];
|
|
49
|
+
return {
|
|
50
|
+
mix: ({ indexMac, valueMac, operation }) => {
|
|
51
|
+
const indexMacBase64 = Buffer.from(indexMac).toString('base64');
|
|
52
|
+
const prevOp = indexValueMap[indexMacBase64];
|
|
53
|
+
if (operation === proto.SyncdMutation.SyncdOperation.REMOVE) {
|
|
54
|
+
if (!prevOp) {
|
|
55
|
+
throw new Boom('tried remove, but no previous op', { data: { indexMac, valueMac } });
|
|
56
|
+
}
|
|
57
|
+
// remove from index value mac, since this mutation is erased
|
|
58
|
+
delete indexValueMap[indexMacBase64];
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
addBuffs.push(new Uint8Array(valueMac).buffer);
|
|
62
|
+
// add this index into the history map
|
|
63
|
+
indexValueMap[indexMacBase64] = { valueMac };
|
|
64
|
+
}
|
|
65
|
+
if (prevOp) {
|
|
66
|
+
subBuffs.push(new Uint8Array(prevOp.valueMac).buffer);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
finish: async () => {
|
|
70
|
+
const hashArrayBuffer = new Uint8Array(hash).buffer;
|
|
71
|
+
const result = await LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs);
|
|
72
|
+
const buffer = Buffer.from(result);
|
|
73
|
+
return {
|
|
74
|
+
hash: buffer,
|
|
75
|
+
indexValueMap
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
const generateSnapshotMac = (lthash, version, name, key) => {
|
|
81
|
+
const total = Buffer.concat([lthash, to64BitNetworkOrder(version), Buffer.from(name, 'utf-8')]);
|
|
82
|
+
return hmacSign(total, key, 'sha256');
|
|
83
|
+
};
|
|
84
|
+
const generatePatchMac = (snapshotMac, valueMacs, version, type, key) => {
|
|
85
|
+
const total = Buffer.concat([snapshotMac, ...valueMacs, to64BitNetworkOrder(version), Buffer.from(type, 'utf-8')]);
|
|
86
|
+
return hmacSign(total, key);
|
|
87
|
+
};
|
|
88
|
+
export const newLTHashState = () => ({ version: 0, hash: Buffer.alloc(128), indexValueMap: {} });
|
|
89
|
+
export const encodeSyncdPatch = async ({ type, index, syncAction, apiVersion, operation }, myAppStateKeyId, state, getAppStateSyncKey) => {
|
|
90
|
+
const key = !!myAppStateKeyId ? await getAppStateSyncKey(myAppStateKeyId) : undefined;
|
|
91
|
+
if (!key) {
|
|
92
|
+
throw new Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 });
|
|
93
|
+
}
|
|
94
|
+
const encKeyId = Buffer.from(myAppStateKeyId, 'base64');
|
|
95
|
+
state = { ...state, indexValueMap: { ...state.indexValueMap } };
|
|
96
|
+
const indexBuffer = Buffer.from(JSON.stringify(index));
|
|
97
|
+
const dataProto = proto.SyncActionData.fromObject({
|
|
98
|
+
index: indexBuffer,
|
|
99
|
+
value: syncAction,
|
|
100
|
+
padding: new Uint8Array(0),
|
|
101
|
+
version: apiVersion
|
|
102
|
+
});
|
|
103
|
+
const encoded = proto.SyncActionData.encode(dataProto).finish();
|
|
104
|
+
const keyValue = await mutationKeys(key.keyData);
|
|
105
|
+
const encValue = aesEncrypt(encoded, keyValue.valueEncryptionKey);
|
|
106
|
+
const valueMac = generateMac(operation, encValue, encKeyId, keyValue.valueMacKey);
|
|
107
|
+
const indexMac = hmacSign(indexBuffer, keyValue.indexKey);
|
|
108
|
+
// update LT hash
|
|
109
|
+
const generator = makeLtHashGenerator(state);
|
|
110
|
+
generator.mix({ indexMac, valueMac, operation });
|
|
111
|
+
Object.assign(state, await generator.finish());
|
|
112
|
+
state.version += 1;
|
|
113
|
+
const snapshotMac = generateSnapshotMac(state.hash, state.version, type, keyValue.snapshotMacKey);
|
|
114
|
+
const patch = {
|
|
115
|
+
patchMac: generatePatchMac(snapshotMac, [valueMac], state.version, type, keyValue.patchMacKey),
|
|
116
|
+
snapshotMac: snapshotMac,
|
|
117
|
+
keyId: { id: encKeyId },
|
|
118
|
+
mutations: [
|
|
119
|
+
{
|
|
120
|
+
operation: operation,
|
|
121
|
+
record: {
|
|
122
|
+
index: {
|
|
123
|
+
blob: indexMac
|
|
124
|
+
},
|
|
125
|
+
value: {
|
|
126
|
+
blob: Buffer.concat([encValue, valueMac])
|
|
127
|
+
},
|
|
128
|
+
keyId: { id: encKeyId }
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
]
|
|
132
|
+
};
|
|
133
|
+
const base64Index = indexMac.toString('base64');
|
|
134
|
+
state.indexValueMap[base64Index] = { valueMac };
|
|
135
|
+
return { patch, state };
|
|
136
|
+
};
|
|
137
|
+
export const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
|
|
138
|
+
const ltGenerator = makeLtHashGenerator(initialState);
|
|
139
|
+
// indexKey used to HMAC sign record.index.blob
|
|
140
|
+
// valueEncryptionKey used to AES-256-CBC encrypt record.value.blob[0:-32]
|
|
141
|
+
// the remaining record.value.blob[0:-32] is the mac, it the HMAC sign of key.keyId + decoded proto data + length of bytes in keyId
|
|
142
|
+
for (const msgMutation of msgMutations) {
|
|
143
|
+
// if it's a syncdmutation, get the operation property
|
|
144
|
+
// otherwise, if it's only a record -- it'll be a SET mutation
|
|
145
|
+
const operation = 'operation' in msgMutation ? msgMutation.operation : proto.SyncdMutation.SyncdOperation.SET;
|
|
146
|
+
const record = 'record' in msgMutation && !!msgMutation.record ? msgMutation.record : msgMutation;
|
|
147
|
+
const key = await getKey(record.keyId.id);
|
|
148
|
+
const content = Buffer.from(record.value.blob);
|
|
149
|
+
const encContent = content.slice(0, -32);
|
|
150
|
+
const ogValueMac = content.slice(-32);
|
|
151
|
+
if (validateMacs) {
|
|
152
|
+
const contentHmac = generateMac(operation, encContent, record.keyId.id, key.valueMacKey);
|
|
153
|
+
if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
|
|
154
|
+
throw new Boom('HMAC content verification failed');
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const result = aesDecrypt(encContent, key.valueEncryptionKey);
|
|
158
|
+
const syncAction = proto.SyncActionData.decode(result);
|
|
159
|
+
if (validateMacs) {
|
|
160
|
+
const hmac = hmacSign(syncAction.index, key.indexKey);
|
|
161
|
+
if (Buffer.compare(hmac, record.index.blob) !== 0) {
|
|
162
|
+
throw new Boom('HMAC index verification failed');
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const indexStr = Buffer.from(syncAction.index).toString();
|
|
166
|
+
onMutation({ syncAction, index: JSON.parse(indexStr) });
|
|
167
|
+
ltGenerator.mix({
|
|
168
|
+
indexMac: record.index.blob,
|
|
169
|
+
valueMac: ogValueMac,
|
|
170
|
+
operation: operation
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return await ltGenerator.finish();
|
|
174
|
+
async function getKey(keyId) {
|
|
175
|
+
const base64Key = Buffer.from(keyId).toString('base64');
|
|
176
|
+
const keyEnc = await getAppStateSyncKey(base64Key);
|
|
177
|
+
if (!keyEnc) {
|
|
178
|
+
throw new Boom(`failed to find key "${base64Key}" to decode mutation`, {
|
|
179
|
+
statusCode: 404,
|
|
180
|
+
data: { msgMutations }
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
return mutationKeys(keyEnc.keyData);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
export const decodeSyncdPatch = async (msg, name, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
|
|
187
|
+
if (validateMacs) {
|
|
188
|
+
const base64Key = Buffer.from(msg.keyId.id).toString('base64');
|
|
189
|
+
const mainKeyObj = await getAppStateSyncKey(base64Key);
|
|
190
|
+
if (!mainKeyObj) {
|
|
191
|
+
throw new Boom(`failed to find key "${base64Key}" to decode patch`, { statusCode: 404, data: { msg } });
|
|
192
|
+
}
|
|
193
|
+
const mainKey = await mutationKeys(mainKeyObj.keyData);
|
|
194
|
+
const mutationmacs = msg.mutations.map(mutation => mutation.record.value.blob.slice(-32));
|
|
195
|
+
const patchMac = generatePatchMac(msg.snapshotMac, mutationmacs, toNumber(msg.version.version), name, mainKey.patchMacKey);
|
|
196
|
+
if (Buffer.compare(patchMac, msg.patchMac) !== 0) {
|
|
197
|
+
throw new Boom('Invalid patch mac');
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const result = await decodeSyncdMutations(msg.mutations, initialState, getAppStateSyncKey, onMutation, validateMacs);
|
|
201
|
+
return result;
|
|
202
|
+
};
|
|
203
|
+
export const extractSyncdPatches = async (result, options) => {
|
|
204
|
+
const syncNode = getBinaryNodeChild(result, 'sync');
|
|
205
|
+
const collectionNodes = getBinaryNodeChildren(syncNode, 'collection');
|
|
206
|
+
const final = {};
|
|
207
|
+
await Promise.all(collectionNodes.map(async (collectionNode) => {
|
|
208
|
+
const patchesNode = getBinaryNodeChild(collectionNode, 'patches');
|
|
209
|
+
const patches = getBinaryNodeChildren(patchesNode || collectionNode, 'patch');
|
|
210
|
+
const snapshotNode = getBinaryNodeChild(collectionNode, 'snapshot');
|
|
211
|
+
const syncds = [];
|
|
212
|
+
const name = collectionNode.attrs.name;
|
|
213
|
+
const hasMorePatches = collectionNode.attrs.has_more_patches === 'true';
|
|
214
|
+
let snapshot = undefined;
|
|
215
|
+
if (snapshotNode && !!snapshotNode.content) {
|
|
216
|
+
if (!Buffer.isBuffer(snapshotNode)) {
|
|
217
|
+
snapshotNode.content = Buffer.from(Object.values(snapshotNode.content));
|
|
218
|
+
}
|
|
219
|
+
const blobRef = proto.ExternalBlobReference.decode(snapshotNode.content);
|
|
220
|
+
const data = await downloadExternalBlob(blobRef, options);
|
|
221
|
+
snapshot = proto.SyncdSnapshot.decode(data);
|
|
222
|
+
}
|
|
223
|
+
for (let { content } of patches) {
|
|
224
|
+
if (content) {
|
|
225
|
+
if (!Buffer.isBuffer(content)) {
|
|
226
|
+
content = Buffer.from(Object.values(content));
|
|
227
|
+
}
|
|
228
|
+
const syncd = proto.SyncdPatch.decode(content);
|
|
229
|
+
if (!syncd.version) {
|
|
230
|
+
syncd.version = { version: +collectionNode.attrs.version + 1 };
|
|
231
|
+
}
|
|
232
|
+
syncds.push(syncd);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
final[name] = { patches: syncds, hasMorePatches, snapshot };
|
|
236
|
+
}));
|
|
237
|
+
return final;
|
|
238
|
+
};
|
|
239
|
+
export const downloadExternalBlob = async (blob, options) => {
|
|
240
|
+
const stream = await downloadContentFromMessage(blob, 'md-app-state', { options });
|
|
241
|
+
const bufferArray = [];
|
|
242
|
+
for await (const chunk of stream) {
|
|
243
|
+
bufferArray.push(chunk);
|
|
244
|
+
}
|
|
245
|
+
return Buffer.concat(bufferArray);
|
|
246
|
+
};
|
|
247
|
+
export const downloadExternalPatch = async (blob, options) => {
|
|
248
|
+
const buffer = await downloadExternalBlob(blob, options);
|
|
249
|
+
const syncData = proto.SyncdMutations.decode(buffer);
|
|
250
|
+
return syncData;
|
|
251
|
+
};
|
|
252
|
+
export const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, minimumVersionNumber, validateMacs = true) => {
|
|
253
|
+
const newState = newLTHashState();
|
|
254
|
+
newState.version = toNumber(snapshot.version.version);
|
|
255
|
+
const mutationMap = {};
|
|
256
|
+
const areMutationsRequired = typeof minimumVersionNumber === 'undefined' || newState.version > minimumVersionNumber;
|
|
257
|
+
const { hash, indexValueMap } = await decodeSyncdMutations(snapshot.records, newState, getAppStateSyncKey, areMutationsRequired
|
|
258
|
+
? mutation => {
|
|
259
|
+
const index = mutation.syncAction.index?.toString();
|
|
260
|
+
mutationMap[index] = mutation;
|
|
261
|
+
}
|
|
262
|
+
: () => { }, validateMacs);
|
|
263
|
+
newState.hash = hash;
|
|
264
|
+
newState.indexValueMap = indexValueMap;
|
|
265
|
+
if (validateMacs) {
|
|
266
|
+
const base64Key = Buffer.from(snapshot.keyId.id).toString('base64');
|
|
267
|
+
const keyEnc = await getAppStateSyncKey(base64Key);
|
|
268
|
+
if (!keyEnc) {
|
|
269
|
+
throw new Boom(`failed to find key "${base64Key}" to decode mutation`);
|
|
270
|
+
}
|
|
271
|
+
const result = await mutationKeys(keyEnc.keyData);
|
|
272
|
+
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
|
|
273
|
+
if (Buffer.compare(snapshot.mac, computedSnapshotMac) !== 0) {
|
|
274
|
+
throw new Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return {
|
|
278
|
+
state: newState,
|
|
279
|
+
mutationMap
|
|
280
|
+
};
|
|
281
|
+
};
|
|
282
|
+
export const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options, minimumVersionNumber, logger, validateMacs = true) => {
|
|
283
|
+
const newState = {
|
|
284
|
+
...initial,
|
|
285
|
+
indexValueMap: { ...initial.indexValueMap }
|
|
286
|
+
};
|
|
287
|
+
const mutationMap = {};
|
|
288
|
+
for (const syncd of syncds) {
|
|
289
|
+
const { version, keyId, snapshotMac } = syncd;
|
|
290
|
+
if (syncd.externalMutations) {
|
|
291
|
+
logger?.trace({ name, version }, 'downloading external patch');
|
|
292
|
+
const ref = await downloadExternalPatch(syncd.externalMutations, options);
|
|
293
|
+
logger?.debug({ name, version, mutations: ref.mutations.length }, 'downloaded external patch');
|
|
294
|
+
syncd.mutations?.push(...ref.mutations);
|
|
295
|
+
}
|
|
296
|
+
const patchVersion = toNumber(version.version);
|
|
297
|
+
newState.version = patchVersion;
|
|
298
|
+
const shouldMutate = typeof minimumVersionNumber === 'undefined' || patchVersion > minimumVersionNumber;
|
|
299
|
+
const decodeResult = await decodeSyncdPatch(syncd, name, newState, getAppStateSyncKey, shouldMutate
|
|
300
|
+
? mutation => {
|
|
301
|
+
const index = mutation.syncAction.index?.toString();
|
|
302
|
+
mutationMap[index] = mutation;
|
|
303
|
+
}
|
|
304
|
+
: () => { }, true);
|
|
305
|
+
newState.hash = decodeResult.hash;
|
|
306
|
+
newState.indexValueMap = decodeResult.indexValueMap;
|
|
307
|
+
if (validateMacs) {
|
|
308
|
+
const base64Key = Buffer.from(keyId.id).toString('base64');
|
|
309
|
+
const keyEnc = await getAppStateSyncKey(base64Key);
|
|
310
|
+
if (!keyEnc) {
|
|
311
|
+
throw new Boom(`failed to find key "${base64Key}" to decode mutation`);
|
|
312
|
+
}
|
|
313
|
+
const result = await mutationKeys(keyEnc.keyData);
|
|
314
|
+
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
|
|
315
|
+
if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
|
|
316
|
+
throw new Boom(`failed to verify LTHash at ${newState.version} of ${name}`);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
// clear memory used up by the mutations
|
|
320
|
+
syncd.mutations = [];
|
|
321
|
+
}
|
|
322
|
+
return { state: newState, mutationMap };
|
|
323
|
+
};
|
|
324
|
+
export const chatModificationToAppPatch = (mod, jid) => {
|
|
325
|
+
const OP = proto.SyncdMutation.SyncdOperation;
|
|
326
|
+
const getMessageRange = (lastMessages) => {
|
|
327
|
+
let messageRange;
|
|
328
|
+
if (Array.isArray(lastMessages)) {
|
|
329
|
+
const lastMsg = lastMessages[lastMessages.length - 1];
|
|
330
|
+
messageRange = {
|
|
331
|
+
lastMessageTimestamp: lastMsg?.messageTimestamp,
|
|
332
|
+
messages: lastMessages?.length
|
|
333
|
+
? lastMessages.map(m => {
|
|
334
|
+
if (!m.key?.id || !m.key?.remoteJid) {
|
|
335
|
+
throw new Boom('Incomplete key', { statusCode: 400, data: m });
|
|
336
|
+
}
|
|
337
|
+
if (isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
|
|
338
|
+
throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m });
|
|
339
|
+
}
|
|
340
|
+
if (!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
|
|
341
|
+
throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m });
|
|
342
|
+
}
|
|
343
|
+
if (m.key.participant) {
|
|
344
|
+
m.key.participant = jidNormalizedUser(m.key.participant);
|
|
345
|
+
}
|
|
346
|
+
return m;
|
|
347
|
+
})
|
|
348
|
+
: undefined
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
messageRange = lastMessages;
|
|
353
|
+
}
|
|
354
|
+
return messageRange;
|
|
355
|
+
};
|
|
356
|
+
let patch;
|
|
357
|
+
if ('mute' in mod) {
|
|
358
|
+
patch = {
|
|
359
|
+
syncAction: {
|
|
360
|
+
muteAction: {
|
|
361
|
+
muted: !!mod.mute,
|
|
362
|
+
muteEndTimestamp: mod.mute || undefined
|
|
363
|
+
}
|
|
364
|
+
},
|
|
365
|
+
index: ['mute', jid],
|
|
366
|
+
type: 'regular_high',
|
|
367
|
+
apiVersion: 2,
|
|
368
|
+
operation: OP.SET
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
else if ('archive' in mod) {
|
|
372
|
+
patch = {
|
|
373
|
+
syncAction: {
|
|
374
|
+
archiveChatAction: {
|
|
375
|
+
archived: !!mod.archive,
|
|
376
|
+
messageRange: getMessageRange(mod.lastMessages)
|
|
377
|
+
}
|
|
378
|
+
},
|
|
379
|
+
index: ['archive', jid],
|
|
380
|
+
type: 'regular_low',
|
|
381
|
+
apiVersion: 3,
|
|
382
|
+
operation: OP.SET
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
else if ('markRead' in mod) {
|
|
386
|
+
patch = {
|
|
387
|
+
syncAction: {
|
|
388
|
+
markChatAsReadAction: {
|
|
389
|
+
read: mod.markRead,
|
|
390
|
+
messageRange: getMessageRange(mod.lastMessages)
|
|
391
|
+
}
|
|
392
|
+
},
|
|
393
|
+
index: ['markChatAsRead', jid],
|
|
394
|
+
type: 'regular_low',
|
|
395
|
+
apiVersion: 3,
|
|
396
|
+
operation: OP.SET
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
else if ('deleteForMe' in mod) {
|
|
400
|
+
const { timestamp, key, deleteMedia } = mod.deleteForMe;
|
|
401
|
+
patch = {
|
|
402
|
+
syncAction: {
|
|
403
|
+
deleteMessageForMeAction: {
|
|
404
|
+
deleteMedia,
|
|
405
|
+
messageTimestamp: timestamp
|
|
406
|
+
}
|
|
407
|
+
},
|
|
408
|
+
index: ['deleteMessageForMe', jid, key.id, key.fromMe ? '1' : '0', '0'],
|
|
409
|
+
type: 'regular_high',
|
|
410
|
+
apiVersion: 3,
|
|
411
|
+
operation: OP.SET
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
else if ('clear' in mod) {
|
|
415
|
+
patch = {
|
|
416
|
+
syncAction: {
|
|
417
|
+
clearChatAction: {
|
|
418
|
+
messageRange: getMessageRange(mod.lastMessages)
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
index: ['clearChat', jid, '1' /*the option here is 0 when keep starred messages is enabled*/, '0'],
|
|
422
|
+
type: 'regular_high',
|
|
423
|
+
apiVersion: 6,
|
|
424
|
+
operation: OP.SET
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
else if ('pin' in mod) {
|
|
428
|
+
patch = {
|
|
429
|
+
syncAction: {
|
|
430
|
+
pinAction: {
|
|
431
|
+
pinned: !!mod.pin
|
|
432
|
+
}
|
|
433
|
+
},
|
|
434
|
+
index: ['pin_v1', jid],
|
|
435
|
+
type: 'regular_low',
|
|
436
|
+
apiVersion: 5,
|
|
437
|
+
operation: OP.SET
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
else if ('contact' in mod) {
|
|
441
|
+
patch = {
|
|
442
|
+
syncAction: {
|
|
443
|
+
contactAction: mod.contact || {}
|
|
444
|
+
},
|
|
445
|
+
index: ['contact', jid],
|
|
446
|
+
type: 'critical_unblock_low',
|
|
447
|
+
apiVersion: 2,
|
|
448
|
+
operation: mod.contact ? OP.SET : OP.REMOVE
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
else if ('disableLinkPreviews' in mod) {
|
|
452
|
+
patch = {
|
|
453
|
+
syncAction: {
|
|
454
|
+
privacySettingDisableLinkPreviewsAction: mod.disableLinkPreviews || {}
|
|
455
|
+
},
|
|
456
|
+
index: ['setting_disableLinkPreviews'],
|
|
457
|
+
type: 'regular',
|
|
458
|
+
apiVersion: 8,
|
|
459
|
+
operation: OP.SET
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
else if ('star' in mod) {
|
|
463
|
+
const key = mod.star.messages[0];
|
|
464
|
+
patch = {
|
|
465
|
+
syncAction: {
|
|
466
|
+
starAction: {
|
|
467
|
+
starred: !!mod.star.star
|
|
468
|
+
}
|
|
469
|
+
},
|
|
470
|
+
index: ['star', jid, key.id, key.fromMe ? '1' : '0', '0'],
|
|
471
|
+
type: 'regular_low',
|
|
472
|
+
apiVersion: 2,
|
|
473
|
+
operation: OP.SET
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
else if ('delete' in mod) {
|
|
477
|
+
patch = {
|
|
478
|
+
syncAction: {
|
|
479
|
+
deleteChatAction: {
|
|
480
|
+
messageRange: getMessageRange(mod.lastMessages)
|
|
481
|
+
}
|
|
482
|
+
},
|
|
483
|
+
index: ['deleteChat', jid, '1'],
|
|
484
|
+
type: 'regular_high',
|
|
485
|
+
apiVersion: 6,
|
|
486
|
+
operation: OP.SET
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
else if ('pushNameSetting' in mod) {
|
|
490
|
+
patch = {
|
|
491
|
+
syncAction: {
|
|
492
|
+
pushNameSetting: {
|
|
493
|
+
name: mod.pushNameSetting
|
|
494
|
+
}
|
|
495
|
+
},
|
|
496
|
+
index: ['setting_pushName'],
|
|
497
|
+
type: 'critical_block',
|
|
498
|
+
apiVersion: 1,
|
|
499
|
+
operation: OP.SET
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
else if ('quickReply' in mod) {
|
|
503
|
+
patch = {
|
|
504
|
+
syncAction: {
|
|
505
|
+
quickReplyAction: {
|
|
506
|
+
count: 0,
|
|
507
|
+
deleted: mod.quickReply.deleted || false,
|
|
508
|
+
keywords: [],
|
|
509
|
+
message: mod.quickReply.message || '',
|
|
510
|
+
shortcut: mod.quickReply.shortcut || ''
|
|
511
|
+
}
|
|
512
|
+
},
|
|
513
|
+
index: ['quick_reply', mod.quickReply.timestamp || String(Math.floor(Date.now() / 1000))],
|
|
514
|
+
type: 'regular',
|
|
515
|
+
apiVersion: 2,
|
|
516
|
+
operation: OP.SET
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
else if ('addLabel' in mod) {
|
|
520
|
+
patch = {
|
|
521
|
+
syncAction: {
|
|
522
|
+
labelEditAction: {
|
|
523
|
+
name: mod.addLabel.name,
|
|
524
|
+
color: mod.addLabel.color,
|
|
525
|
+
predefinedId: mod.addLabel.predefinedId,
|
|
526
|
+
deleted: mod.addLabel.deleted
|
|
527
|
+
}
|
|
528
|
+
},
|
|
529
|
+
index: ['label_edit', mod.addLabel.id],
|
|
530
|
+
type: 'regular',
|
|
531
|
+
apiVersion: 3,
|
|
532
|
+
operation: OP.SET
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
else if ('addChatLabel' in mod) {
|
|
536
|
+
patch = {
|
|
537
|
+
syncAction: {
|
|
538
|
+
labelAssociationAction: {
|
|
539
|
+
labeled: true
|
|
540
|
+
}
|
|
541
|
+
},
|
|
542
|
+
index: [LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
|
|
543
|
+
type: 'regular',
|
|
544
|
+
apiVersion: 3,
|
|
545
|
+
operation: OP.SET
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
else if ('removeChatLabel' in mod) {
|
|
549
|
+
patch = {
|
|
550
|
+
syncAction: {
|
|
551
|
+
labelAssociationAction: {
|
|
552
|
+
labeled: false
|
|
553
|
+
}
|
|
554
|
+
},
|
|
555
|
+
index: [LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
|
|
556
|
+
type: 'regular',
|
|
557
|
+
apiVersion: 3,
|
|
558
|
+
operation: OP.SET
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
else if ('addMessageLabel' in mod) {
|
|
562
|
+
patch = {
|
|
563
|
+
syncAction: {
|
|
564
|
+
labelAssociationAction: {
|
|
565
|
+
labeled: true
|
|
566
|
+
}
|
|
567
|
+
},
|
|
568
|
+
index: [LabelAssociationType.Message, mod.addMessageLabel.labelId, jid, mod.addMessageLabel.messageId, '0', '0'],
|
|
569
|
+
type: 'regular',
|
|
570
|
+
apiVersion: 3,
|
|
571
|
+
operation: OP.SET
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
else if ('removeMessageLabel' in mod) {
|
|
575
|
+
patch = {
|
|
576
|
+
syncAction: {
|
|
577
|
+
labelAssociationAction: {
|
|
578
|
+
labeled: false
|
|
579
|
+
}
|
|
580
|
+
},
|
|
581
|
+
index: [
|
|
582
|
+
LabelAssociationType.Message,
|
|
583
|
+
mod.removeMessageLabel.labelId,
|
|
584
|
+
jid,
|
|
585
|
+
mod.removeMessageLabel.messageId,
|
|
586
|
+
'0',
|
|
587
|
+
'0'
|
|
588
|
+
],
|
|
589
|
+
type: 'regular',
|
|
590
|
+
apiVersion: 3,
|
|
591
|
+
operation: OP.SET
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
else {
|
|
595
|
+
throw new Boom('not supported');
|
|
596
|
+
}
|
|
597
|
+
patch.syncAction.timestamp = Date.now();
|
|
598
|
+
return patch;
|
|
599
|
+
};
|
|
600
|
+
export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
|
|
601
|
+
const isInitialSync = !!initialSyncOpts;
|
|
602
|
+
const accountSettings = initialSyncOpts?.accountSettings;
|
|
603
|
+
logger?.trace({ syncAction, initialSync: !!initialSyncOpts }, 'processing sync action');
|
|
604
|
+
const { syncAction: { value: action }, index: [type, id, msgId, fromMe] } = syncAction;
|
|
605
|
+
if (action?.muteAction) {
|
|
606
|
+
ev.emit('chats.update', [
|
|
607
|
+
{
|
|
608
|
+
id,
|
|
609
|
+
muteEndTime: action.muteAction?.muted ? toNumber(action.muteAction.muteEndTimestamp) : null,
|
|
610
|
+
conditional: getChatUpdateConditional(id, undefined)
|
|
611
|
+
}
|
|
612
|
+
]);
|
|
613
|
+
}
|
|
614
|
+
else if (action?.archiveChatAction || type === 'archive' || type === 'unarchive') {
|
|
615
|
+
// okay so we've to do some annoying computation here
|
|
616
|
+
// when we're initially syncing the app state
|
|
617
|
+
// there are a few cases we need to handle
|
|
618
|
+
// 1. if the account unarchiveChats setting is true
|
|
619
|
+
// a. if the chat is archived, and no further messages have been received -- simple, keep archived
|
|
620
|
+
// b. if the chat was archived, and the user received messages from the other person afterwards
|
|
621
|
+
// then the chat should be marked unarchved --
|
|
622
|
+
// we compare the timestamp of latest message from the other person to determine this
|
|
623
|
+
// 2. if the account unarchiveChats setting is false -- then it doesn't matter,
|
|
624
|
+
// it'll always take an app state action to mark in unarchived -- which we'll get anyway
|
|
625
|
+
const archiveAction = action?.archiveChatAction;
|
|
626
|
+
const isArchived = archiveAction ? archiveAction.archived : type === 'archive';
|
|
627
|
+
// // basically we don't need to fire an "archive" update if the chat is being marked unarchvied
|
|
628
|
+
// // this only applies for the initial sync
|
|
629
|
+
// if(isInitialSync && !isArchived) {
|
|
630
|
+
// isArchived = false
|
|
631
|
+
// }
|
|
632
|
+
const msgRange = !accountSettings?.unarchiveChats ? undefined : archiveAction?.messageRange;
|
|
633
|
+
// logger?.debug({ chat: id, syncAction }, 'message range archive')
|
|
634
|
+
ev.emit('chats.update', [
|
|
635
|
+
{
|
|
636
|
+
id,
|
|
637
|
+
archived: isArchived,
|
|
638
|
+
conditional: getChatUpdateConditional(id, msgRange)
|
|
639
|
+
}
|
|
640
|
+
]);
|
|
641
|
+
}
|
|
642
|
+
else if (action?.markChatAsReadAction) {
|
|
643
|
+
const markReadAction = action.markChatAsReadAction;
|
|
644
|
+
// basically we don't need to fire an "read" update if the chat is being marked as read
|
|
645
|
+
// because the chat is read by default
|
|
646
|
+
// this only applies for the initial sync
|
|
647
|
+
const isNullUpdate = isInitialSync && markReadAction.read;
|
|
648
|
+
ev.emit('chats.update', [
|
|
649
|
+
{
|
|
650
|
+
id,
|
|
651
|
+
unreadCount: isNullUpdate ? null : !!markReadAction?.read ? 0 : -1,
|
|
652
|
+
conditional: getChatUpdateConditional(id, markReadAction?.messageRange)
|
|
653
|
+
}
|
|
654
|
+
]);
|
|
655
|
+
}
|
|
656
|
+
else if (action?.deleteMessageForMeAction || type === 'deleteMessageForMe') {
|
|
657
|
+
ev.emit('messages.delete', {
|
|
658
|
+
keys: [
|
|
659
|
+
{
|
|
660
|
+
remoteJid: id,
|
|
661
|
+
id: msgId,
|
|
662
|
+
fromMe: fromMe === '1'
|
|
663
|
+
}
|
|
664
|
+
]
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
else if (action?.contactAction) {
|
|
668
|
+
ev.emit('contacts.upsert', [
|
|
669
|
+
{
|
|
670
|
+
id: id,
|
|
671
|
+
name: action.contactAction.fullName,
|
|
672
|
+
lid: action.contactAction.lidJid || undefined,
|
|
673
|
+
phoneNumber: action.contactAction.pnJid || undefined
|
|
674
|
+
}
|
|
675
|
+
]);
|
|
676
|
+
}
|
|
677
|
+
else if (action?.pushNameSetting) {
|
|
678
|
+
const name = action?.pushNameSetting?.name;
|
|
679
|
+
if (name && me?.name !== name) {
|
|
680
|
+
ev.emit('creds.update', { me: { ...me, name } });
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
else if (action?.pinAction) {
|
|
684
|
+
ev.emit('chats.update', [
|
|
685
|
+
{
|
|
686
|
+
id,
|
|
687
|
+
pinned: action.pinAction?.pinned ? toNumber(action.timestamp) : null,
|
|
688
|
+
conditional: getChatUpdateConditional(id, undefined)
|
|
689
|
+
}
|
|
690
|
+
]);
|
|
691
|
+
}
|
|
692
|
+
else if (action?.unarchiveChatsSetting) {
|
|
693
|
+
const unarchiveChats = !!action.unarchiveChatsSetting.unarchiveChats;
|
|
694
|
+
ev.emit('creds.update', { accountSettings: { unarchiveChats } });
|
|
695
|
+
logger?.info(`archive setting updated => '${action.unarchiveChatsSetting.unarchiveChats}'`);
|
|
696
|
+
if (accountSettings) {
|
|
697
|
+
accountSettings.unarchiveChats = unarchiveChats;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
else if (action?.starAction || type === 'star') {
|
|
701
|
+
let starred = action?.starAction?.starred;
|
|
702
|
+
if (typeof starred !== 'boolean') {
|
|
703
|
+
starred = syncAction.index[syncAction.index.length - 1] === '1';
|
|
704
|
+
}
|
|
705
|
+
ev.emit('messages.update', [
|
|
706
|
+
{
|
|
707
|
+
key: { remoteJid: id, id: msgId, fromMe: fromMe === '1' },
|
|
708
|
+
update: { starred }
|
|
709
|
+
}
|
|
710
|
+
]);
|
|
711
|
+
}
|
|
712
|
+
else if (action?.deleteChatAction || type === 'deleteChat') {
|
|
713
|
+
if (!isInitialSync) {
|
|
714
|
+
ev.emit('chats.delete', [id]);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
else if (action?.labelEditAction) {
|
|
718
|
+
const { name, color, deleted, predefinedId } = action.labelEditAction;
|
|
719
|
+
ev.emit('labels.edit', {
|
|
720
|
+
id: id,
|
|
721
|
+
name: name,
|
|
722
|
+
color: color,
|
|
723
|
+
deleted: deleted,
|
|
724
|
+
predefinedId: predefinedId ? String(predefinedId) : undefined
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
else if (action?.labelAssociationAction) {
|
|
728
|
+
ev.emit('labels.association', {
|
|
729
|
+
type: action.labelAssociationAction.labeled ? 'add' : 'remove',
|
|
730
|
+
association: type === LabelAssociationType.Chat
|
|
731
|
+
? {
|
|
732
|
+
type: LabelAssociationType.Chat,
|
|
733
|
+
chatId: syncAction.index[2],
|
|
734
|
+
labelId: syncAction.index[1]
|
|
735
|
+
}
|
|
736
|
+
: {
|
|
737
|
+
type: LabelAssociationType.Message,
|
|
738
|
+
chatId: syncAction.index[2],
|
|
739
|
+
messageId: syncAction.index[3],
|
|
740
|
+
labelId: syncAction.index[1]
|
|
741
|
+
}
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
else {
|
|
745
|
+
logger?.debug({ syncAction, id }, 'unprocessable update');
|
|
746
|
+
}
|
|
747
|
+
function getChatUpdateConditional(id, msgRange) {
|
|
748
|
+
return isInitialSync
|
|
749
|
+
? data => {
|
|
750
|
+
const chat = data.historySets.chats[id] || data.chatUpserts[id];
|
|
751
|
+
if (chat) {
|
|
752
|
+
return msgRange ? isValidPatchBasedOnMessageRange(chat, msgRange) : true;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
: undefined;
|
|
756
|
+
}
|
|
757
|
+
function isValidPatchBasedOnMessageRange(chat, msgRange) {
|
|
758
|
+
const lastMsgTimestamp = Number(msgRange?.lastMessageTimestamp || msgRange?.lastSystemMessageTimestamp || 0);
|
|
759
|
+
const chatLastMsgTimestamp = Number(chat?.lastMessageRecvTimestamp || 0);
|
|
760
|
+
return lastMsgTimestamp >= chatLastMsgTimestamp;
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
//# sourceMappingURL=chat-utils.js.map
|