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
@type challenge() :: %{c: pos_integer(), s: pos_integer(), d: pos_integer()}
Compact Cap challenge parameters sent to the widget.
Redeem request body decoded from JSON.
@type response() :: %{challenge: challenge(), token: binary(), expires: pos_integer()}
Challenge response shape expected by the Cap widget.
@type stored_challenge() :: %{challenge: challenge(), expires: pos_integer()}
Stored challenge data with an absolute Unix millisecond expiry.
Functions
@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}.
@spec prng(binary(), non_neg_integer()) :: binary()
Generates deterministic hex output compatible with Cap's widget PRNG.
@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.
Verifies one {salt, target} challenge pair against a nonce.
This is useful for parity tests against Cap's WASM solver vectors.
Verifies all nonce solutions for a compact Cap challenge.