From 4aadc9dc6c63b4e7be4de095a092316955cd4c2d Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Tue, 8 Nov 2022 22:33:53 +0530 Subject: [PATCH] fix: add option for appStateMacVerification --- src/Defaults/index.ts | 4 ++++ src/Socket/chats.ts | 20 +++++++++++++++++--- src/Types/Socket.ts | 6 ++++++ src/Utils/chat-utils.ts | 2 +- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index e6c6e00..8c3a360 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -55,6 +55,10 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = { transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 }, generateHighQualityLinkPreview: false, options: { }, + appStateMacVerification: { + patch: false, + snapshot: false, + }, getMessage: async() => undefined } diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index 84b7ce9..9a5eef3 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -11,7 +11,13 @@ import { makeSocket } from './socket' const MAX_SYNC_ATTEMPTS = 2 export const makeChatsSocket = (config: SocketConfig) => { - const { logger, markOnlineOnConnect, shouldSyncHistoryMessage, fireInitQueries } = config + const { + logger, + markOnlineOnConnect, + shouldSyncHistoryMessage, + fireInitQueries, + appStateMacVerification, + } = config const sock = makeSocket(config) const { ev, @@ -360,7 +366,13 @@ export const makeChatsSocket = (config: SocketConfig) => { const { patches, hasMorePatches, snapshot } = decoded[name] try { if(snapshot) { - const { state: newState, mutationMap } = await decodeSyncdSnapshot(name, snapshot, getAppStateSyncKey, initialVersionMap[name]) + const { state: newState, mutationMap } = await decodeSyncdSnapshot( + name, + snapshot, + getAppStateSyncKey, + initialVersionMap[name], + appStateMacVerification.snapshot + ) states[name] = newState Object.assign(globalMutationMap, mutationMap) @@ -377,7 +389,9 @@ export const makeChatsSocket = (config: SocketConfig) => { states[name], getAppStateSyncKey, config.options, - initialVersionMap[name] + initialVersionMap[name], + logger, + appStateMacVerification.patch ) await authState.keys.set({ 'app-state-sync-version': { [name]: newState } }) diff --git a/src/Types/Socket.ts b/src/Types/Socket.ts index f693a37..4e43cf8 100644 --- a/src/Types/Socket.ts +++ b/src/Types/Socket.ts @@ -70,6 +70,12 @@ export type SocketConfig = { * */ generateHighQualityLinkPreview: boolean + /** verify app state MACs */ + appStateMacVerification: { + patch: boolean + snapshot: boolean + } + /** options for axios */ options: AxiosRequestConfig /** diff --git a/src/Utils/chat-utils.ts b/src/Utils/chat-utils.ts index 31bda6f..2ce6695 100644 --- a/src/Utils/chat-utils.ts +++ b/src/Utils/chat-utils.ts @@ -445,7 +445,7 @@ export const decodePatches = async( mutationMap[index!] = mutation } : (() => { }), - validateMacs + true ) newState.hash = decodeResult.hash