# `BaileysEx.Auth.KeyStore`
[🔗](https://github.com/jeffhuen/baileys_ex/blob/main/lib/baileys_ex/auth/key_store.ex#L1)

Persistence-backed transactional Signal key store.

This module wraps an auth persistence backend with the same `get/3`, `set/2`,
and `transaction/3` shape used by the runtime `Signal.Store` contract. Reads
go through ETS, transaction work is cached on an explicit transaction handle, and commit
failures roll back to the previous persisted snapshot before surfacing an
error to the caller. When a persistence backend exports context-aware
callbacks such as `load_keys/3` or `save_keys/4`, the store passes the
configured `:persistence_context` as the first argument; otherwise it falls
back to the behaviour's context-free callbacks.

# `state`

```elixir
@type state() :: %{
  table: :ets.tid(),
  persistence_module: module(),
  persistence_context: term(),
  locks: map(),
  monitor_keys: map(),
  known_ids: map(),
  max_commit_retries: pos_integer(),
  delay_between_tries_ms: non_neg_integer()
}
```

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `clear`

```elixir
@spec clear(BaileysEx.Auth.KeyStore.Ref.t() | BaileysEx.Auth.KeyStore.TxRef.t()) ::
  :ok
```

Clears all keys from persistence.

# `get`

```elixir
@spec get(
  BaileysEx.Auth.KeyStore.Ref.t() | BaileysEx.Auth.KeyStore.TxRef.t(),
  BaileysEx.Signal.Store.data_type(),
  [String.t()]
) :: BaileysEx.Signal.Store.data_entries()
```

Fetches an array of identifiers for a given data type.

# `in_transaction?`

```elixir
@spec in_transaction?(
  BaileysEx.Auth.KeyStore.Ref.t()
  | BaileysEx.Auth.KeyStore.TxRef.t()
) :: boolean()
```

Returns true if the current process is in an active transaction context.

# `set`

```elixir
@spec set(
  BaileysEx.Auth.KeyStore.Ref.t() | BaileysEx.Auth.KeyStore.TxRef.t(),
  BaileysEx.Signal.Store.data_set()
) :: :ok
```

Sets arbitrary mutations into the persistence backend.

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

Starts the transactional key store linked to the current process.

Options:

- `:persistence_module` - module implementing `BaileysEx.Auth.Persistence`
  (defaults to `BaileysEx.Auth.NativeFilePersistence`)
- `:persistence_context` - backend-specific context passed to the built-in
  context-aware persistence callbacks when exported

# `transaction`

```elixir
@spec transaction(
  BaileysEx.Auth.KeyStore.Ref.t() | BaileysEx.Auth.KeyStore.TxRef.t(),
  String.t(),
  (BaileysEx.Auth.KeyStore.TxRef.t() -&gt; result)
) :: result
when result: var
```

Acquires an exclusive lock tied to `key` before running the `fun`.
Errors safely roll back changes if commit fails.

# `wrap`

```elixir
@spec wrap(pid()) :: BaileysEx.Auth.KeyStore.Ref.t()
```

Creates a read-only query ref struct to pass directly into reads.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
