ExWapp.AppState.Engine.Codec (ExWapp v0.1.2)

Copy Markdown View Source

Encoding and decoding helpers for app-state patches.

This module owns the cryptographic transformation layer: it turns high-level patch descriptions into protobuf payloads for WhatsApp and validates incoming patch payloads back into decoded mutations that the local store can apply.

Summary

Functions

Decodes a list of SyncdMutation records and optionally validates their MACs.

Decodes a complete incoming patch and validates its hash-chain transition.

Encodes a high-level patch description into a SyncdPatch protobuf payload.

Types

action_value()

@type action_value() :: WAWebProtobufSyncAction.SyncActionValue.t() | nil

decoded_mutation()

@type decoded_mutation() :: %{
  operation: non_neg_integer(),
  action: action_value(),
  version: integer() | nil,
  index: [String.t()],
  index_mac: binary(),
  value_mac: binary()
}

hash_state()

@type hash_state() :: %{version: non_neg_integer(), hash: binary()}

mutation_info()

@type mutation_info() :: %{
  :index => [String.t()],
  :version => integer(),
  :value => WAWebProtobufSyncAction.SyncActionValue.t(),
  optional(:operation) => :remove
}

patch_info()

@type patch_info() :: %{
  type: String.t(),
  mutations: [mutation_info()],
  timestamp: integer() | nil
}

syncd_mutation()

@type syncd_mutation() :: WAWebProtobufsServerSync.SyncdMutation.t()

syncd_patch()

@type syncd_patch() :: WAWebProtobufsServerSync.SyncdPatch.t()

Functions

decode_mutations(mutations, key_store, validate_macs \\ true)

@spec decode_mutations([syncd_mutation()], map(), boolean()) ::
  {:ok, [decoded_mutation()]} | {:error, term()}

Decodes a list of SyncdMutation records and optionally validates their MACs.

decode_patch(patch, collection_name, hash_state, mutation_macs, key_store, validate_macs \\ true)

@spec decode_patch(syncd_patch(), String.t(), hash_state(), map(), map(), boolean()) ::
  {:ok, [decoded_mutation()], hash_state(), map()} | {:error, term()}

Decodes a complete incoming patch and validates its hash-chain transition.

Besides decoding each mutation, this verifies snapshot and patch MACs and returns the updated hash state plus the refreshed mutation-MAC map.

encode_patch(patch_info, key_id, key_data, hash_state, mutation_macs \\ %{})

@spec encode_patch(patch_info(), binary(), binary(), hash_state(), map()) ::
  {:ok, binary(), hash_state(), map()} | {:error, term()}

Encodes a high-level patch description into a SyncdPatch protobuf payload.

The returned binary is ready to be embedded in the app-state IQ request, and the hash state / mutation MAC map are advanced to reflect the encoded patch.