diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index 9881dfc..da1c9a2 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -406,11 +406,13 @@ export const makeChatsSocket = (config: SocketConfig) => { await mutationMutex.mutex( async() => { await resyncAppState([name]) + const initial = await authState.keys.getAppStateSyncVersion(name) const { patch, state } = await encodeSyncdPatch( patchCreate, - authState, + authState.creds.myAppStateKeyId!, + initial, + authState.keys, ) - const initial = await authState.keys.getAppStateSyncVersion(name) // temp: verify it was encoded correctly const result = await decodePatches(name, [{ ...patch, version: { version: state.version }, }], initial, authState.keys.getAppStateSyncKey) diff --git a/src/Utils/chat-utils.ts b/src/Utils/chat-utils.ts index 11238d1..a496a97 100644 --- a/src/Utils/chat-utils.ts +++ b/src/Utils/chat-utils.ts @@ -110,23 +110,26 @@ export const newLTHashState = (): LTHashState => ({ version: 0, hash: Buffer.all export const encodeSyncdPatch = async( { type, index, syncAction, apiVersion, operation }: WAPatchCreate, - { creds: { myAppStateKeyId }, keys }: AuthenticationState + myAppStateKeyId: string, + state: LTHashState, + keys: SignalKeyStore ) => { const key = !!myAppStateKeyId ? await keys.getAppStateSyncKey(myAppStateKeyId) : undefined 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 state = { ...await keys.getAppStateSyncVersion(type) } + state = { ...state, indexValueMap: { ...state.indexValueMap } } const indexBuffer = Buffer.from(JSON.stringify(index)) - const encoded = proto.SyncActionData.encode({ + const dataProto = proto.SyncActionData.fromObject({ index: indexBuffer, value: syncAction, padding: new Uint8Array(0), version: apiVersion - }).finish() + }) + const encoded = proto.SyncActionData.encode(dataProto).finish() const keyValue = mutationKeys(key!.keyData!) @@ -225,12 +228,12 @@ export const decodeSyncdMutations = async( } const indexStr = Buffer.from(syncAction.index).toString() - const mutation: ChatMutation = { - action: syncAction.value!, + const mutation: ChatMutation = { + syncAction, index: JSON.parse(indexStr), indexMac: record.index!.blob!, valueMac: ogValueMac, - operation: operation + operation: operation, } mutations.push(mutation)