PhoenixCap.Challenge (PhoenixCap v0.1.0)

Copy Markdown View Source

Cap-compatible proof-of-work challenge generation and redemption.

This module implements the compact challenge protocol used by the Cap widget: the server stores {c, s, d} plus an opaque challenge token, while the widget derives the actual {salt, target} pairs locally from that token.

Summary

Types

Compact Cap challenge parameters sent to the widget.

Redeem request body decoded from JSON.

Challenge response shape expected by the Cap widget.

Stored challenge data with an absolute Unix millisecond expiry.

Functions

Creates and stores a single-use Cap challenge.

Generates deterministic hex output compatible with Cap's widget PRNG.

Redeems a solved challenge and issues a one-time verification token.

Verifies one {salt, target} challenge pair against a nonce.

Verifies all nonce solutions for a compact Cap challenge.

Types

challenge()

@type challenge() :: %{c: pos_integer(), s: pos_integer(), d: pos_integer()}

Compact Cap challenge parameters sent to the widget.

redeem_params()

@type redeem_params() :: %{required(binary()) => term()}

Redeem request body decoded from JSON.

response()

@type response() :: %{challenge: challenge(), token: binary(), expires: pos_integer()}

Challenge response shape expected by the Cap widget.

stored_challenge()

@type stored_challenge() :: %{challenge: challenge(), expires: pos_integer()}

Stored challenge data with an absolute Unix millisecond expiry.

Functions

create()

@spec create() :: response()

Creates and stores a single-use Cap challenge.

The returned JSON-compatible map matches the upstream @cap.js/server createChallenge response: %{challenge: %{c, s, d}, token, expires}.

prng(seed, length)

@spec prng(binary(), non_neg_integer()) :: binary()

Generates deterministic hex output compatible with Cap's widget PRNG.

redeem(conn, arg2)

@spec redeem(Plug.Conn.t(), redeem_params()) :: {:ok, map()} | {:error, binary()}

Redeems a solved challenge and issues a one-time verification token.

The challenge is consumed exactly once, matching Cap's server behavior: once a syntactically valid redeem attempt reaches stored challenge lookup, retrying the same challenge token fails.

verify_challenge_pair(salt, target, nonce)

@spec verify_challenge_pair(binary(), binary(), integer()) :: boolean()

Verifies one {salt, target} challenge pair against a nonce.

This is useful for parity tests against Cap's WASM solver vectors.

verify_solutions(token, arg2, solutions)

@spec verify_solutions(binary(), challenge(), [integer()]) ::
  :ok | {:error, :invalid_solution}

Verifies all nonce solutions for a compact Cap challenge.