refactor: more specific arguments for encode patch

This commit is contained in:
Adhiraj Singh
2021-11-25 13:33:56 +05:30
parent 669fcaa438
commit 25f76b2f66
2 changed files with 15 additions and 10 deletions

View File

@@ -406,11 +406,13 @@ export const makeChatsSocket = (config: SocketConfig) => {
await mutationMutex.mutex( await mutationMutex.mutex(
async() => { async() => {
await resyncAppState([name]) await resyncAppState([name])
const initial = await authState.keys.getAppStateSyncVersion(name)
const { patch, state } = await encodeSyncdPatch( const { patch, state } = await encodeSyncdPatch(
patchCreate, patchCreate,
authState, authState.creds.myAppStateKeyId!,
initial,
authState.keys,
) )
const initial = await authState.keys.getAppStateSyncVersion(name)
// temp: verify it was encoded correctly // temp: verify it was encoded correctly
const result = await decodePatches(name, [{ ...patch, version: { version: state.version }, }], initial, authState.keys.getAppStateSyncKey) const result = await decodePatches(name, [{ ...patch, version: { version: state.version }, }], initial, authState.keys.getAppStateSyncKey)

View File

@@ -110,23 +110,26 @@ export const newLTHashState = (): LTHashState => ({ version: 0, hash: Buffer.all
export const encodeSyncdPatch = async( export const encodeSyncdPatch = async(
{ type, index, syncAction, apiVersion, operation }: WAPatchCreate, { type, index, syncAction, apiVersion, operation }: WAPatchCreate,
{ creds: { myAppStateKeyId }, keys }: AuthenticationState myAppStateKeyId: string,
state: LTHashState,
keys: SignalKeyStore
) => { ) => {
const key = !!myAppStateKeyId ? await keys.getAppStateSyncKey(myAppStateKeyId) : undefined const key = !!myAppStateKeyId ? await keys.getAppStateSyncKey(myAppStateKeyId) : undefined
if(!key) { if(!key) {
throw new Boom(`myAppStateKey not present`, { statusCode: 404 }) throw new Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 })
} }
const encKeyId = Buffer.from(myAppStateKeyId, 'base64') const encKeyId = Buffer.from(myAppStateKeyId, 'base64')
const state = { ...await keys.getAppStateSyncVersion(type) } state = { ...state, indexValueMap: { ...state.indexValueMap } }
const indexBuffer = Buffer.from(JSON.stringify(index)) const indexBuffer = Buffer.from(JSON.stringify(index))
const encoded = proto.SyncActionData.encode({ const dataProto = proto.SyncActionData.fromObject({
index: indexBuffer, index: indexBuffer,
value: syncAction, value: syncAction,
padding: new Uint8Array(0), padding: new Uint8Array(0),
version: apiVersion version: apiVersion
}).finish() })
const encoded = proto.SyncActionData.encode(dataProto).finish()
const keyValue = mutationKeys(key!.keyData!) const keyValue = mutationKeys(key!.keyData!)
@@ -225,12 +228,12 @@ export const decodeSyncdMutations = async(
} }
const indexStr = Buffer.from(syncAction.index).toString() const indexStr = Buffer.from(syncAction.index).toString()
const mutation: ChatMutation = { const mutation: ChatMutation = {
action: syncAction.value!, syncAction,
index: JSON.parse(indexStr), index: JSON.parse(indexStr),
indexMac: record.index!.blob!, indexMac: record.index!.blob!,
valueMac: ogValueMac, valueMac: ogValueMac,
operation: operation operation: operation,
} }
mutations.push(mutation) mutations.push(mutation)