fix: add option for appStateMacVerification

This commit is contained in:
Adhiraj Singh
2022-11-08 22:33:53 +05:30
parent f892a1e81f
commit 4aadc9dc6c
4 changed files with 28 additions and 4 deletions

View File

@@ -55,6 +55,10 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 }, transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 },
generateHighQualityLinkPreview: false, generateHighQualityLinkPreview: false,
options: { }, options: { },
appStateMacVerification: {
patch: false,
snapshot: false,
},
getMessage: async() => undefined getMessage: async() => undefined
} }

View File

@@ -11,7 +11,13 @@ import { makeSocket } from './socket'
const MAX_SYNC_ATTEMPTS = 2 const MAX_SYNC_ATTEMPTS = 2
export const makeChatsSocket = (config: SocketConfig) => { export const makeChatsSocket = (config: SocketConfig) => {
const { logger, markOnlineOnConnect, shouldSyncHistoryMessage, fireInitQueries } = config const {
logger,
markOnlineOnConnect,
shouldSyncHistoryMessage,
fireInitQueries,
appStateMacVerification,
} = config
const sock = makeSocket(config) const sock = makeSocket(config)
const { const {
ev, ev,
@@ -360,7 +366,13 @@ export const makeChatsSocket = (config: SocketConfig) => {
const { patches, hasMorePatches, snapshot } = decoded[name] const { patches, hasMorePatches, snapshot } = decoded[name]
try { try {
if(snapshot) { 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 states[name] = newState
Object.assign(globalMutationMap, mutationMap) Object.assign(globalMutationMap, mutationMap)
@@ -377,7 +389,9 @@ export const makeChatsSocket = (config: SocketConfig) => {
states[name], states[name],
getAppStateSyncKey, getAppStateSyncKey,
config.options, config.options,
initialVersionMap[name] initialVersionMap[name],
logger,
appStateMacVerification.patch
) )
await authState.keys.set({ 'app-state-sync-version': { [name]: newState } }) await authState.keys.set({ 'app-state-sync-version': { [name]: newState } })

View File

@@ -70,6 +70,12 @@ export type SocketConfig = {
* */ * */
generateHighQualityLinkPreview: boolean generateHighQualityLinkPreview: boolean
/** verify app state MACs */
appStateMacVerification: {
patch: boolean
snapshot: boolean
}
/** options for axios */ /** options for axios */
options: AxiosRequestConfig<any> options: AxiosRequestConfig<any>
/** /**

View File

@@ -445,7 +445,7 @@ export const decodePatches = async(
mutationMap[index!] = mutation mutationMap[index!] = mutation
} }
: (() => { }), : (() => { }),
validateMacs true
) )
newState.hash = decodeResult.hash newState.hash = decodeResult.hash