Amarula.Protocol.Signal.PreKeys (amarula v0.1.0)

View Source

One-time prekey generation and the encrypt IQ that uploads them, ported from the prekey helpers in src/Utils/signal.ts (generateOrGetPreKeys, getNextPreKeysNode, xmppPreKey, xmppSignedPreKey).

Prekeys live in the creds map under :pre_keys (integer id => %{public, private}, raw 32-byte X25519), alongside the upload watermarks :next_pre_key_id and :first_unuploaded_pre_key_id (both start at 1). SessionStore.build/1 reads :pre_keys to serve the responder X3DH when a PreKeySignalMessage references one of our ids.

Summary

Functions

Ensure range prekeys exist past the uploaded watermark, generating only the missing ones. Returns {new_pre_keys, last_pre_key_id, upload_range} where upload_range is {first_id, count} of the keys to put in the upload IQ. Mirrors generateOrGetPreKeys.

Reserve the next count one-time prekeys: generate any missing ones, advance the watermarks, and return {updated_creds, [{id, pair}]}. Used by both the bulk upload IQ and the retry-receipt key bundle (Baileys getNextPreKeys).

Build the encrypt upload IQ for the next count prekeys and the updated creds (new prekeys merged in, watermarks advanced). Mirrors getNextPreKeysNode; the caller assigns the IQ id attribute.

The KEY_BUNDLE_TYPE byte used in <type> nodes.

The <key> node for a one-time prekey (id is 3-byte big-endian).

The <skey> node for the signed prekey (id is 3-byte big-endian).

Functions

generate_or_get_pre_keys(creds, range)

@spec generate_or_get_pre_keys(map(), pos_integer()) ::
  {map(), integer(), {integer(), integer()}}

Ensure range prekeys exist past the uploaded watermark, generating only the missing ones. Returns {new_pre_keys, last_pre_key_id, upload_range} where upload_range is {first_id, count} of the keys to put in the upload IQ. Mirrors generateOrGetPreKeys.

get_next_pre_keys(creds, count)

@spec get_next_pre_keys(map(), pos_integer()) :: {map(), [{integer(), map()}]}

Reserve the next count one-time prekeys: generate any missing ones, advance the watermarks, and return {updated_creds, [{id, pair}]}. Used by both the bulk upload IQ and the retry-receipt key bundle (Baileys getNextPreKeys).

get_next_pre_keys_node(creds, count)

@spec get_next_pre_keys_node(map(), pos_integer()) ::
  {map(), Amarula.Protocol.Binary.Node.t()}

Build the encrypt upload IQ for the next count prekeys and the updated creds (new prekeys merged in, watermarks advanced). Mirrors getNextPreKeysNode; the caller assigns the IQ id attribute.

Returns {updated_creds, node}.

initial_pre_key_count()

key_bundle_type()

@spec key_bundle_type() :: binary()

The KEY_BUNDLE_TYPE byte used in <type> nodes.

min_pre_key_count()

xmpp_pre_key(pair, id)

@spec xmpp_pre_key(map(), integer()) :: Amarula.Protocol.Binary.Node.t()

The <key> node for a one-time prekey (id is 3-byte big-endian).

xmpp_signed_pre_key(signed)

@spec xmpp_signed_pre_key(map()) :: Amarula.Protocol.Binary.Node.t()

The <skey> node for the signed prekey (id is 3-byte big-endian).