Skip to content

SiweMessage ​

php
use Iltumio\SiwePhp\SiweMessage;

Constructor ​

new SiweMessage($param) accepts an array of fields or a serialized SIWE message string. Construction parses or assigns fields and validates them. Invalid input throws; catch \Throwable at your request boundary.

Fields ​

FieldValue and behavior
domainRequired authority, such as example.com or example.com:3000; no URL scheme, path or whitespace.
addressRequired Ethereum address passing EIP-55 validation.
uriRequired valid URL identifying the authentication request.
versionSIWE version 1; use the string '1'.
chainIdPositive integer, or a digit string normalized to an integer within PHP's integer range. Defaults to 1 on serialization if omitted.
nonceAt least eight alphanumeric characters; generated securely when absent from the field array.
issuedAtRFC 3339 timestamp; populated on first serialization when omitted.
statementOptional single line of printable ASCII text.
expirationTimeOptional RFC 3339 expiration timestamp. Verification fails at or after this time.
notBeforeOptional RFC 3339 timestamp. Verification fails before this time.
requestIdOptional request identifier using the permitted URI characters or percent escapes.
resourcesArray of valid resource URLs; defaults to [].

Fields are public. Prefer constructing a new message to mutating a challenge that has already been issued.

Methods ​

MethodBehavior
prepareMessage()Returns the signable message string via toMessage().
toMessage()Validates and serializes fields; populates missing serialization defaults.
validateMessage()Validates current fields and throws on invalid values. Accepts no arguments.
fromParsedMessage($fields)Assigns recognized fields and normalizes the chain ID; used internally by the constructor. Does not independently perform full validation.
verify(array $params, array $opts = [])Returns a Guzzle promise for signature and constraint verification.

Verification parameters ​

ParameterBehavior
signatureRequired hexadecimal signature string with 0x prefix.
domainOptional exact comparison with the message domain; supply the application's expected domain for login.
nonceOptional exact comparison with the message nonce; supply the stored challenge nonce for login.
timeOptional RFC 3339 verification time; defaults to the current time.

Verification options ​

OptionBehavior
suppressExceptionsWhen truthy, verification failures resolve to a structured failure result. Otherwise the promise is rejected. Defaults to disabled.
providerUrlHTTP(S) Ethereum RPC URL for contract verification. Empty string disables RPC; omission selects the default mainnet provider for chain ID 1 only.

Unknown parameter or option keys are rejected. providerUrl, when provided, must be a string.

Results ​

Success: ['success' => true, 'data' => $message].

Suppressed failure: ['success' => false, 'data' => $message, 'error' => $error]. Read $error->getMessage() for the reason. Constructor exceptions occur before verification and are not suppressed by this option.

A PHP port of SIWE. See the repository for license details.