Siwe (siwe v0.5.1)

Siwe provides validation and parsing for Sign-In with Ethereum messages and signatures.

Link to this section Summary

Functions

Generates an alphanumeric nonce for use in SIWE messages.

Parses a Sign In With Ethereum message string into the Message struct, or reports an error

Tests that a message and signature pair correspond and that the current time is valid (after not_before, and before expiration_time)

Converts a Message struct to a Sign In With Ethereum message string, or reports an error

Given a Message, signature, and optionally, domain, nonce and timestamp, returns true if: the current time or timestamp, if provided, is between the messages' not_before and expiration_time the Message.address signing the Message would produce the signature. the domain, if provided, matches Message.domain the nonce, if provided, matches Message.nonce

Given a Message struct and a signature, returns true if the Message.address signing the Message would produce the signature.

Link to this section Functions

Link to this function

generate_nonce()

Specs

generate_nonce() :: String.t()

Generates an alphanumeric nonce for use in SIWE messages.

Specs

parse(String.t()) :: {:ok | :error, Siwe.Message.t() | String.t()}

Parses a Sign In With Ethereum message string into the Message struct, or reports an error

Link to this function

parse_if_valid(msg, sig)

Specs

parse_if_valid(String.t(), String.t()) ::
  {:ok | :error, Siwe.Message.t() | String.t()}

Tests that a message and signature pair correspond and that the current time is valid (after not_before, and before expiration_time)

Returns a Message structure based on the passed message

## Examples iex> Siwe.parseifvalid(Enum.join(["login.xyz wants you to sign in with your Ethereum account:", ...> "0xfA151B5453CE69ABf60f0dbdE71F6C9C5868800E", ...> "", ...> "Sign-In With Ethereum Example Statement", ...> "", ...> "URI: https://login.xyz", ...> "Version: 1", ...> "Chain ID: 1", ...> "Nonce: ToTaLLyRanDOM", ...> "Issued At: 2021-12-17T00:38:39.834Z", ...> ], "\n"), ...> "0x8d1327a1abbdf172875e5be41706c50fc3bede8af363b67aefbb543d6d082fb76a22057d7cb6d668ceba883f7d70ab7f1dc015b76b51d226af9d610fa20360ad1c") {:ok, %{ __struct: Siwe, address: "0xfA151B5453CE69ABf60f0dbdE71F6C9C5868800E", chain_id: 1, domain: "login.xyz", expiration_time: nil, issued_at: "2021-12-17T00:38:39.834Z", nonce: "ToTaLLyRanDOM", not_before: nil, request_id: nil, resources: [], statement: "Sign-In With Ethereum Example Statement", uri: "https://login.xyz", version: "1" }}

Specs

to_str(Siwe.Message.t()) :: {:ok | :error, String.t()}

Converts a Message struct to a Sign In With Ethereum message string, or reports an error

Link to this function

verify(msg, sig, domain_binding, match_nonce, timestamp)

Specs

verify(
  Siwe.Message.t(),
  String.t(),
  String.t() | nil.t(),
  String.t() | nil.t(),
  String.t() | nil.t()
) :: boolean()

Given a Message, signature, and optionally, domain, nonce and timestamp, returns true if: the current time or timestamp, if provided, is between the messages' not_before and expiration_time the Message.address signing the Message would produce the signature. the domain, if provided, matches Message.domain the nonce, if provided, matches Message.nonce

Link to this function

verify_sig(msg, sig)

Specs

verify_sig(Siwe.Message.t(), String.t()) :: boolean()

Given a Message struct and a signature, returns true if the Message.address signing the Message would produce the signature.