Skip to content

Commit

Permalink
fix: add option for appStateMacVerification
Browse files Browse the repository at this point in the history
  • Loading branch information
adiwajshing committed Nov 8, 2022
1 parent f892a1e commit 4aadc9d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Defaults/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
20 changes: 17 additions & 3 deletions src/Socket/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand All @@ -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 } })
Expand Down
6 changes: 6 additions & 0 deletions src/Types/Socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ export type SocketConfig = {
* */
generateHighQualityLinkPreview: boolean

/** verify app state MACs */
appStateMacVerification: {
patch: boolean
snapshot: boolean
}

/** options for axios */
options: AxiosRequestConfig<any>
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/chat-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ export const decodePatches = async(
mutationMap[index!] = mutation
}
: (() => { }),
validateMacs
true
)

newState.hash = decodeResult.hash
Expand Down

0 comments on commit 4aadc9d

Please sign in to comment.