@tuwaio/siwx-core 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,192 +1,211 @@
1
1
  /**
2
- * @fileoverview Core type definitions for the CAIP-122 (Sign-In With X) standard.
3
- * These types are chain-agnostic and form the foundation of the entire siwx ecosystem.
2
+ * @file Core type definitions for the CAIP-122 (Sign-In With X) standard.
3
+ * These types are chain-agnostic and are shared by every SIWX package.
4
4
  *
5
5
  * @see {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-122.md CAIP-122 Specification}
6
6
  */
7
7
  /**
8
- * Supported CAIP-2 chain namespace identifiers.
9
- * Only EVM (eip155) and Solana are supported in v1.
8
+ * CAIP-2 chain namespaces supported by SIWX: `eip155` (EVM) and `solana`.
10
9
  */
11
10
  type SiwxChainNamespace = 'eip155' | 'solana';
12
11
  /**
13
- * A fully qualified CAIP-2 chain ID string.
12
+ * A CAIP-2 chain ID in a supported namespace: `{namespace}:{reference}`.
13
+ *
14
14
  * @example "eip155:1" (Ethereum Mainnet)
15
15
  * @example "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpK" (Solana Mainnet)
16
16
  */
17
17
  type SiwxChainId = `${SiwxChainNamespace}:${string}`;
18
18
  /**
19
- * The lifecycle status of a SIWX authentication session.
19
+ * Lifecycle status of a client-side sign-in.
20
+ *
21
+ * `@tuwaio/siwx-react` moves through `idle` → `building` (nonce and message) → `signing` (wallet prompt) →
22
+ * `verifying` (backend) → `authenticated` or `error`.
20
23
  */
21
24
  type SiwxStatus = 'idle' | 'building' | 'signing' | 'verifying' | 'authenticated' | 'error';
22
25
  /**
23
- * The complete set of fields required to build a CAIP-122 compliant message.
24
- * All fields follow the CAIP-122 specification.
26
+ * Fields of a CAIP-122 sign-in message. They are the input of `buildMessage` and the output of
27
+ * `parseMessage`; `validateMessage` checks their format.
25
28
  */
26
29
  interface SiwxMessageFields {
27
30
  /**
28
- * The RFC 3986 URI of the domain requesting the sign-in.
31
+ * RFC 3986 authority (host and optional port) requesting the sign-in, without a scheme.
29
32
  * @example "app.tuwa.io"
30
33
  */
31
34
  domain: string;
32
35
  /**
33
- * The blockchain account address performing the sign-in, CAIP-10 compliant.
36
+ * CAIP-10 account ID of the signer: `{namespace}:{chainReference}:{address}`.
34
37
  * @example "eip155:1:0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B"
35
38
  */
36
39
  address: string;
37
40
  /**
38
- * A statement (human-readable) that the user will sign.
39
- * Must not contain '\n'.
41
+ * Optional human-readable statement shown to the user. Must be a single line: `validateMessage`
42
+ * rejects statements that contain `\n`.
40
43
  */
41
44
  statement?: string;
42
45
  /**
43
- * The RFC 3986 URI referring to the resource that is the subject of the sign-in.
46
+ * RFC 3986 URI of the resource that is the subject of the sign-in. `validateMessage` requires an
47
+ * `http://` or `https://` URI.
44
48
  * @example "https://app.tuwa.io"
45
49
  */
46
50
  uri: string;
47
51
  /**
48
- * The version of the CAIP-122 message specification.
49
- * Currently always "1".
52
+ * Version of the CAIP-122 message format. Always `"1"`.
50
53
  */
51
54
  version: '1';
52
55
  /**
53
- * The CAIP-2 chain ID to which the session is bound.
56
+ * CAIP-2 chain ID the session is bound to.
54
57
  * @example "eip155:1"
55
58
  */
56
59
  chainId: SiwxChainId;
57
60
  /**
58
- * A unique, secure, randomly generated string used to prevent replay attacks.
59
- * @example "32891757"
61
+ * Random value that binds the signature to one sign-in attempt and prevents replay. `validateMessage`
62
+ * requires at least 8 alphanumeric characters; `generateNonce` returns 32 hex characters.
63
+ * @example "a4f3b2c1d0e5f6789abc0123456789ab"
60
64
  */
61
65
  nonce: string;
62
66
  /**
63
- * The ISO 8601 datetime string of when the message was generated.
64
- * @example "2021-09-30T16:25:24Z"
67
+ * ISO 8601 date-time when the message was created.
68
+ * @example "2026-08-06T08:00:00.000Z"
65
69
  */
66
70
  issuedAt: string;
67
71
  /**
68
- * Optional ISO 8601 datetime string after which the session is no longer valid.
72
+ * Optional ISO 8601 date-time after which the signed message is no longer valid.
69
73
  */
70
74
  expirationTime?: string;
71
75
  /**
72
- * Optional ISO 8601 datetime string when the session is valid from.
76
+ * Optional ISO 8601 date-time before which the signed message is not yet valid. `validateMessage` rejects the
77
+ * message until then.
73
78
  */
74
79
  notBefore?: string;
75
80
  /**
76
- * Optional system-specific identifier for the request.
81
+ * Optional system-specific identifier of the request.
77
82
  */
78
83
  requestId?: string;
79
84
  /**
80
- * Optional list of URIs the session is valid for.
85
+ * Optional list of URIs the user wishes to have resolved as part of the sign-in.
81
86
  */
82
87
  resources?: string[];
83
88
  }
84
89
  /**
85
- * Policy parameters for server-side CAIP-122 message verification.
86
- * Enforces strict security constraints including domain, URI, chain ID, and timing windows.
90
+ * Rules that bind a CAIP-122 message to your application. Enforced by {@link validatePolicy}, and by
91
+ * {@link validateMessage} when passed in its options.
92
+ *
93
+ * Every rule below is optional and is skipped when its field is omitted. Two timing rules always apply, even without
94
+ * a policy in {@link validateMessage}: messages whose `issuedAt` lies in the future beyond `clockSkewSeconds` are
95
+ * rejected, and so are (unless `enforceNotBefore` is `false`) messages whose `notBefore` has not been reached.
96
+ * Always set at least `expectedDomain` on the server.
87
97
  */
88
98
  interface SiwxVerificationPolicy {
89
99
  /**
90
- * Expected domain(s) requesting sign-in (e.g. "tuwa.io" or ["tuwa.io", "staging.tuwa.io"]).
100
+ * Accepted value(s) of the message `domain`, compared case-insensitively.
101
+ * @example "tuwa.io"
102
+ * @example ["tuwa.io", "staging.tuwa.io"]
91
103
  */
92
104
  expectedDomain?: string | string[];
93
105
  /**
94
- * Expected RFC 3986 URI(s) subject of sign-in (e.g. "https://tuwa.io").
106
+ * Accepted value(s) of the message `uri`. A message URI matches an expected URI when it is equal to it,
107
+ * starts with it followed by `/`, or has the same origin (scheme, host and port).
108
+ * @example "https://tuwa.io"
95
109
  */
96
110
  expectedUri?: string | string[];
97
111
  /**
98
- * List of allowed CAIP-2 chain IDs (e.g. ["eip155:1", "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpK"]).
112
+ * Allowed CAIP-2 chain IDs. The message `chainId` must equal one of them exactly (bare references such as `"1"`
113
+ * never match). An empty array allows every chain.
114
+ * @example ["eip155:1", "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpK"]
99
115
  */
100
116
  allowedChainIds?: string[];
101
117
  /**
102
- * Whether the CAIP-122 message MUST include an `expirationTime`.
103
- * Strongly recommended for zero-infrastructure stateless demo profiles.
118
+ * Rejects messages without an `expirationTime`. Recommended for the stateless demo profile of
119
+ * `@tuwaio/siwx-server`.
104
120
  */
105
121
  requireExpirationTime?: boolean;
106
122
  /**
107
- * Maximum allowed age of the message's `issuedAt` in seconds.
108
- * Prevents accepting stale sign-in messages.
109
- * @default 300 (5 minutes)
123
+ * Maximum age of the message `issuedAt`, in seconds (plus `clockSkewSeconds`). Rejects stale messages.
124
+ * There is no default: when omitted, the age is not checked.
110
125
  */
111
126
  maxIssuedAtAgeSeconds?: number;
112
127
  /**
113
- * Maximum allowed session lifetime in seconds (expirationTime - issuedAt).
128
+ * Maximum signed session lifetime (`expirationTime - issuedAt`), in seconds. Checked only when the message
129
+ * has an `expirationTime`.
114
130
  */
115
131
  maxSessionLifetimeSeconds?: number;
116
132
  /**
117
- * Allowed clock skew in seconds when validating timestamps.
118
- * @default 60 (1 minute)
133
+ * Allowed clock difference between signer and verifier, in seconds. Applied to `issuedAt`, `notBefore` and
134
+ * `expirationTime` checks.
135
+ * @default 60
119
136
  */
120
137
  clockSkewSeconds?: number;
121
138
  /**
122
- * Whether to enforce the `notBefore` timestamp if present in the message.
139
+ * Rejects messages whose `notBefore` is still in the future (beyond `clockSkewSeconds`).
123
140
  * @default true
124
141
  */
125
142
  enforceNotBefore?: boolean;
126
143
  }
127
144
  /**
128
- * Options for validating a CAIP-122 message object.
145
+ * Options of {@link validateMessage}.
129
146
  */
130
147
  interface ValidateMessageOptions {
131
148
  /**
132
- * If true, skips the `expirationTime` validation check.
133
- * Not recommended for production use.
149
+ * Skips the check that `expirationTime` has not passed (the format is still checked).
150
+ * Not recommended in production.
134
151
  */
135
152
  skipExpiration?: boolean;
136
153
  /**
137
- * Optional verification policy to enforce on the message fields.
154
+ * Verification policy to enforce on top of the format checks. See {@link validatePolicy}.
138
155
  */
139
156
  policy?: SiwxVerificationPolicy;
140
157
  }
141
158
  /**
142
- * The result of a message validation operation.
159
+ * Result of {@link validateMessage}.
143
160
  */
144
161
  interface SiwxValidationResult {
145
- /** Whether the message fields are valid. */
162
+ /** `true` when no check failed. */
146
163
  valid: boolean;
147
- /** List of validation error messages, empty if valid. */
164
+ /** Human-readable description of every failed check; empty when `valid` is `true`. */
148
165
  errors: string[];
149
166
  }
150
167
  /**
151
- * A parsed CAIP-122 message, structurally identical to the fields used to build it.
168
+ * A CAIP-122 message parsed by `parseMessage`. Same shape as {@link SiwxMessageFields}.
152
169
  */
153
170
  type ParsedSiwxMessage = SiwxMessageFields;
154
171
  /**
155
- * The payload submitted for signature verification.
172
+ * A signed CAIP-122 message, as sent from the client to the verifier.
156
173
  */
157
174
  interface SiwxVerifyPayload {
158
- /** The raw CAIP-122 compliant message string that was signed. */
175
+ /** The exact CAIP-122 message string that was signed. */
159
176
  message: string;
160
- /** The signature produced by the wallet. */
177
+ /** The wallet signature: hex (`0x…`) for EVM, base58 for Solana. */
161
178
  signature: string;
162
179
  }
163
180
  /**
164
- * The result of a signature verification operation.
181
+ * Result of a signature verification. Verification functions return this object instead of throwing.
165
182
  */
166
183
  interface SiwxVerifyResult {
167
- /** Whether the signature is valid and the message is authentic. */
184
+ /** `true` when the message is valid and the signature matches its `address`. */
168
185
  success: boolean;
169
186
  /**
170
- * The parsed message fields if verification succeeded.
171
- * Present only when `success` is true.
187
+ * The parsed message. Present only when `success` is `true`.
172
188
  */
173
189
  data?: ParsedSiwxMessage;
174
190
  /**
175
- * A human-readable error if verification failed.
176
- * Present only when `success` is false.
191
+ * Human-readable reason of the failure. Present only when `success` is `false`.
177
192
  */
178
193
  error?: string;
179
194
  }
180
195
  /**
181
- * A chain-specific adapter interface that all siwx chain packages must implement.
196
+ * Shape of a verifier for one CAIP-2 namespace.
197
+ *
198
+ * The SIWX packages do not implement or consume this interface: the chain packages export plain functions
199
+ * (`verifyEvmSignature` in `@tuwaio/siwx-evm`, `verifyEd25519` in `@tuwaio/siwx-solana`) that you can wrap into it
200
+ * to build your own namespace registry.
182
201
  */
183
202
  interface SiwxAdapter {
184
203
  /**
185
- * The CAIP-2 namespace this adapter handles.
204
+ * CAIP-2 namespace handled by the adapter.
186
205
  */
187
206
  namespace: SiwxChainNamespace;
188
207
  /**
189
- * Verifies a CAIP-122 payload signature.
208
+ * Verifies a signed CAIP-122 message.
190
209
  * @param payload - The message and signature to verify.
191
210
  * @returns A promise resolving to the verification result.
192
211
  */
@@ -194,16 +213,18 @@ interface SiwxAdapter {
194
213
  }
195
214
 
196
215
  /**
197
- * @fileoverview CAIP-122 compliant message builder.
216
+ * @file CAIP-122 compliant message builder.
198
217
  * Generates the human-readable sign-in message string from structured fields.
199
218
  */
200
219
 
201
220
  /**
202
- * Builds a CAIP-122 compliant sign-in message string from the provided fields.
203
- * The output format follows the EIP-4361 / CAIP-122 specification exactly.
221
+ * Formats CAIP-122 message fields into the plain-text message that the wallet signs (the EIP-4361 layout used by
222
+ * CAIP-122). Optional fields are omitted when empty.
204
223
  *
205
- * @param fields - The structured fields to encode into the message.
206
- * @returns A formatted, human-readable message string ready for wallet signing.
224
+ * Pure function: it does not validate the fields. Run {@link validateMessage} first if the input is untrusted.
225
+ *
226
+ * @param fields - The message fields to format.
227
+ * @returns The message lines joined with `\n`, ready to be signed and parseable by {@link parseMessage}.
207
228
  *
208
229
  * @example
209
230
  * ```ts
@@ -222,24 +243,25 @@ interface SiwxAdapter {
222
243
  declare function buildMessage(fields: SiwxMessageFields): string;
223
244
 
224
245
  /**
225
- * @fileoverview Error classes for the @tuwaio/siwx-core package.
246
+ * @file Error classes for the @tuwaio/siwx-core package.
226
247
  * All errors are typed and carry contextual information for proper handling.
227
248
  */
228
249
  /**
229
- * Base error class for all siwx-related errors.
230
- * Extends the native Error with an optional error code for programmatic handling.
250
+ * Base class of every SIWX error. Extends the native `Error` with a machine-readable `code` (for example
251
+ * `SIWX_PARSE_ERROR`) for programmatic handling; `name` is set to the concrete class name.
231
252
  */
232
253
  declare class SiwxError extends Error {
233
254
  readonly code: string;
234
255
  /**
235
256
  * @param message - Human-readable description of the error.
236
- * @param code - Optional machine-readable error code.
257
+ * @param code - Machine-readable error code, for example `SIWX_PARSE_ERROR`. Each subclass passes its own code;
258
+ * `SIWX_ERROR` is used only by the base class.
237
259
  */
238
260
  constructor(message: string, code?: string);
239
261
  }
240
262
  /**
241
- * Thrown when a CAIP-122 message string cannot be parsed.
242
- * This indicates the message is malformed or not CAIP-122 compliant.
263
+ * Thrown by {@link parseMessage} when a string is not a well-formed CAIP-122 message (wrong header, missing
264
+ * blank lines or missing required fields). Code: `SIWX_PARSE_ERROR`.
243
265
  */
244
266
  declare class SiwxParseError extends SiwxError {
245
267
  /**
@@ -248,20 +270,24 @@ declare class SiwxParseError extends SiwxError {
248
270
  constructor(message: string);
249
271
  }
250
272
  /**
251
- * Thrown when one or more fields in a SiwxMessage fail validation.
252
- * The `errors` property contains a list of all validation failures.
273
+ * Signals that one or more message fields failed validation; `errors` lists every failure.
274
+ * Code: `SIWX_VALIDATION_ERROR`.
275
+ *
276
+ * The chain verifiers (`verifyEip191`, `verifyEip1271`, `verifyEd25519`) raise it internally and return its
277
+ * message in `SiwxVerifyResult.error`. {@link validateMessage} returns a result instead of throwing it.
253
278
  */
254
279
  declare class SiwxValidationError extends SiwxError {
255
280
  readonly errors: string[];
256
281
  /**
257
- * @param errors - Array of validation error descriptions.
282
+ * @param errors - Descriptions of the failed checks, as returned by {@link validateMessage}.
258
283
  */
259
284
  constructor(errors: string[]);
260
285
  }
261
286
  /**
262
- * Thrown when a signature verification operation fails.
263
- * Indicates the signature is invalid, the message was tampered with,
264
- * or the signer address does not match.
287
+ * Signals a failed signature check: the signature is invalid, the message was altered, or the signer does not
288
+ * match the message `address`. Code: `SIWX_VERIFICATION_ERROR`.
289
+ *
290
+ * The chain verifiers raise it internally and return its message in `SiwxVerifyResult.error`.
265
291
  */
266
292
  declare class SiwxVerificationError extends SiwxError {
267
293
  /**
@@ -270,7 +296,10 @@ declare class SiwxVerificationError extends SiwxError {
270
296
  constructor(message: string);
271
297
  }
272
298
  /**
273
- * Thrown when the session or message has expired based on the `expirationTime` field.
299
+ * Signals that the message `expirationTime` has passed. Code: `SIWX_EXPIRED_SESSION`.
300
+ *
301
+ * Not thrown by SIWX itself: expired messages are reported by {@link validateMessage} and returned as `error` by the
302
+ * verifiers. Provided for applications that want to throw a typed error from their own checks.
274
303
  */
275
304
  declare class SiwxExpiredSessionError extends SiwxError {
276
305
  readonly expirationTime: string;
@@ -280,7 +309,10 @@ declare class SiwxExpiredSessionError extends SiwxError {
280
309
  constructor(expirationTime: string);
281
310
  }
282
311
  /**
283
- * Thrown when a nonce replay attack is detected, i.e., the nonce has already been used.
312
+ * Signals that the message nonce has already been used. Code: `SIWX_NONCE_REPLAY`.
313
+ *
314
+ * `verifySiwxPayload` (`@tuwaio/siwx-server`) raises it internally when the nonce is in `usedNonces` and returns
315
+ * its message in the result.
284
316
  */
285
317
  declare class SiwxNonceReplayError extends SiwxError {
286
318
  readonly nonce: string;
@@ -290,92 +322,136 @@ declare class SiwxNonceReplayError extends SiwxError {
290
322
  constructor(nonce: string);
291
323
  }
292
324
  /**
293
- * Thrown when the chain namespace in the message is not supported.
325
+ * Signals a CAIP-2 namespace that is not supported (anything other than `eip155` and `solana`), or not supported
326
+ * by the verifier that received the message. Code: `SIWX_UNSUPPORTED_NAMESPACE`.
327
+ *
328
+ * The chain verifiers and `verifySiwxPayload` raise it internally and report it through their result.
294
329
  */
295
330
  declare class SiwxUnsupportedNamespaceError extends SiwxError {
296
331
  readonly namespace: string;
297
332
  /**
298
- * @param namespace - The unsupported namespace string extracted from the message.
333
+ * @param namespace - The namespace extracted from the message `chainId`.
299
334
  */
300
335
  constructor(namespace: string);
301
336
  }
302
337
  /**
303
- * Thrown when a verification policy rule is violated.
338
+ * Base class of the policy violation errors. Code: `SIWX_POLICY_VIOLATION`, or a specific code set by a subclass.
339
+ *
340
+ * The SIWX functions report policy violations as strings (see {@link validatePolicy}) and never throw this class or
341
+ * its subclasses. They are provided for applications that want to throw typed errors from their own checks.
304
342
  */
305
343
  declare class SiwxPolicyViolationError extends SiwxError {
306
344
  /**
307
- * @param message - Human-readable description of policy violation.
308
- * @param code - Specific policy violation error code.
345
+ * @param message - Human-readable description of the violation.
346
+ * @param code - Machine-readable error code. Defaults to `SIWX_POLICY_VIOLATION`.
309
347
  */
310
348
  constructor(message: string, code?: string);
311
349
  }
312
350
  /**
313
- * Thrown when the message domain does not match the expected domain policy.
351
+ * Policy violation: the message `domain` is not one of the expected domains. Code: `SIWX_DOMAIN_MISMATCH`.
352
+ * Not thrown by SIWX itself; see {@link SiwxPolicyViolationError}.
314
353
  */
315
354
  declare class SiwxDomainMismatchError extends SiwxPolicyViolationError {
316
355
  readonly expected: string | string[];
317
356
  readonly received: string;
357
+ /**
358
+ * @param expected - The expected domain or domains.
359
+ * @param received - The `domain` found in the message.
360
+ */
318
361
  constructor(expected: string | string[], received: string);
319
362
  }
320
363
  /**
321
- * Thrown when the message URI does not match the expected URI policy.
364
+ * Policy violation: the message `uri` does not match the expected URIs. Code: `SIWX_URI_MISMATCH`.
365
+ * Not thrown by SIWX itself; see {@link SiwxPolicyViolationError}.
322
366
  */
323
367
  declare class SiwxUriMismatchError extends SiwxPolicyViolationError {
324
368
  readonly expected: string | string[];
325
369
  readonly received: string;
370
+ /**
371
+ * @param expected - The expected URI or URIs.
372
+ * @param received - The `uri` found in the message.
373
+ */
326
374
  constructor(expected: string | string[], received: string);
327
375
  }
328
376
  /**
329
- * Thrown when the message chain ID is not in the allowed list of chain IDs.
377
+ * Policy violation: the message `chainId` is not in the allowed list. Code: `SIWX_CHAIN_NOT_ALLOWED`.
378
+ * Not thrown by SIWX itself; see {@link SiwxPolicyViolationError}.
330
379
  */
331
380
  declare class SiwxChainNotAllowedError extends SiwxPolicyViolationError {
332
381
  readonly allowedChainIds: string[];
333
382
  readonly received: string;
383
+ /**
384
+ * @param allowedChainIds - The allowed CAIP-2 chain IDs.
385
+ * @param received - The `chainId` found in the message.
386
+ */
334
387
  constructor(allowedChainIds: string[], received: string);
335
388
  }
336
389
  /**
337
- * Thrown when the message issuedAt timestamp is older than allowed by policy.
390
+ * Policy violation: the message `issuedAt` is older than the allowed maximum age. Code: `SIWX_ISSUED_AT_STALE`.
391
+ * Not thrown by SIWX itself; see {@link SiwxPolicyViolationError}.
338
392
  */
339
393
  declare class SiwxIssuedAtStaleError extends SiwxPolicyViolationError {
340
394
  readonly issuedAt: string;
341
395
  readonly maxAgeSeconds: number;
396
+ /**
397
+ * @param issuedAt - The `issuedAt` found in the message.
398
+ * @param maxAgeSeconds - The allowed maximum age, in seconds.
399
+ */
342
400
  constructor(issuedAt: string, maxAgeSeconds: number);
343
401
  }
344
402
  /**
345
- * Thrown when the message issuedAt timestamp is in the future beyond acceptable clock skew.
403
+ * Policy violation: the message `issuedAt` lies in the future beyond the allowed clock skew.
404
+ * Code: `SIWX_ISSUED_AT_FUTURE`. Not thrown by SIWX itself; see {@link SiwxPolicyViolationError}.
346
405
  */
347
406
  declare class SiwxIssuedAtFutureError extends SiwxPolicyViolationError {
348
407
  readonly issuedAt: string;
349
408
  readonly clockSkewSeconds: number;
409
+ /**
410
+ * @param issuedAt - The `issuedAt` found in the message.
411
+ * @param clockSkewSeconds - The allowed clock skew, in seconds.
412
+ */
350
413
  constructor(issuedAt: string, clockSkewSeconds: number);
351
414
  }
352
415
  /**
353
- * Thrown when the message notBefore timestamp has not yet been reached.
416
+ * Policy violation: the message `notBefore` has not been reached yet. Code: `SIWX_NOT_BEFORE`.
417
+ * Not thrown by SIWX itself; see {@link SiwxPolicyViolationError}.
354
418
  */
355
419
  declare class SiwxNotBeforeError extends SiwxPolicyViolationError {
356
420
  readonly notBefore: string;
421
+ /**
422
+ * @param notBefore - The `notBefore` found in the message.
423
+ */
357
424
  constructor(notBefore: string);
358
425
  }
359
426
  /**
360
- * Thrown when the message session duration exceeds the max allowed session lifetime.
427
+ * Policy violation: `expirationTime - issuedAt` exceeds the allowed maximum lifetime.
428
+ * Code: `SIWX_SESSION_LIFETIME_EXCEEDED`. Not thrown by SIWX itself; see {@link SiwxPolicyViolationError}.
361
429
  */
362
430
  declare class SiwxSessionLifetimeExceededError extends SiwxPolicyViolationError {
363
431
  readonly lifetimeSeconds: number;
364
432
  readonly maxLifetimeSeconds: number;
433
+ /**
434
+ * @param lifetimeSeconds - The lifetime of the message, in seconds.
435
+ * @param maxLifetimeSeconds - The allowed maximum lifetime, in seconds.
436
+ */
365
437
  constructor(lifetimeSeconds: number, maxLifetimeSeconds: number);
366
438
  }
367
439
 
368
440
  /**
369
- * @fileoverview CAIP-122 message parser.
441
+ * @file CAIP-122 message parser.
370
442
  * Converts a raw CAIP-122 compliant message string back into structured fields.
371
443
  */
372
444
 
373
445
  /**
374
- * Parses a raw CAIP-122 compliant message string into a structured `ParsedSiwxMessage` object.
446
+ * Parses a CAIP-122 message string (as produced by {@link buildMessage}) back into its fields.
447
+ *
448
+ * Pure function. It checks the message structure and the presence of the required fields (`URI`, `Version`,
449
+ * `Chain ID`, `Nonce`, `Issued At`) but not their format or timing; use {@link validateMessage} for that.
375
450
  *
376
- * @param message - The raw message string produced by `buildMessage` and signed by the wallet.
377
- * @returns The structured fields extracted from the message.
378
- * @throws {SiwxParseError} If the message is malformed or missing required fields.
451
+ * @param message - The raw message string that was signed.
452
+ * @returns The fields found in the message. `version` and `chainId` are taken as written, without validation.
453
+ * @throws {@link SiwxParseError} If the header or the blank separator lines are malformed, or a required field is
454
+ * missing.
379
455
  *
380
456
  * @example
381
457
  * ```ts
@@ -386,26 +462,41 @@ declare class SiwxSessionLifetimeExceededError extends SiwxPolicyViolationError
386
462
  declare function parseMessage(message: string): ParsedSiwxMessage;
387
463
 
388
464
  /**
389
- * @fileoverview CAIP-122 message field validators.
465
+ * @file CAIP-122 message field validators.
390
466
  * Provides individual validators and a composite `validateMessage` function.
391
467
  */
392
468
 
393
469
  /**
394
- * Validates a CAIP-122 message object against an optional verification policy.
470
+ * Checks message fields against a {@link SiwxVerificationPolicy}: domain, URI, allowed chains, required
471
+ * `expirationTime`, `issuedAt` age and future skew, `notBefore` and maximum session lifetime. See the policy fields
472
+ * for the exact matching rules.
395
473
  *
396
- * @param fields - The message fields to validate.
397
- * @param policy - The verification policy to enforce.
398
- * @param now - Reference date for timestamp validations (defaults to new Date()).
399
- * @returns An array of policy violation error messages.
474
+ * Pure function; it reports violations instead of throwing and does not check field formats (see
475
+ * {@link validateMessage}, which always runs this function).
476
+ *
477
+ * @param fields - The message fields to check.
478
+ * @param policy - The policy to enforce. When omitted, no check runs and an empty array is returned; an empty
479
+ * object still applies the `issuedAt` future skew and `notBefore` checks.
480
+ * @param now - Reference time for the timing checks. Defaults to the current time.
481
+ * @returns A human-readable description of every violation; empty when the fields satisfy the policy.
400
482
  */
401
483
  declare function validatePolicy(fields: SiwxMessageFields, policy?: SiwxVerificationPolicy, now?: Date): string[];
402
484
  /**
403
- * Validates all fields of a CAIP-122 message object.
404
- * Collects all errors and returns them together rather than failing on the first.
485
+ * Validates the format of CAIP-122 message fields and, optionally, a verification policy. Collects every failure
486
+ * instead of stopping at the first one.
487
+ *
488
+ * Checks: non-empty single-line `domain`, CAIP-10 `address`, `http(s)://` `uri`, `version` `"1"`, CAIP-2 `chainId`,
489
+ * alphanumeric `nonce` of at least 8 characters, ISO 8601 `issuedAt` / `expirationTime` / `notBefore`, single-line
490
+ * `statement`, and that `expirationTime` (plus the policy clock skew, 60 seconds by default) has not passed.
491
+ * Then it runs {@link validatePolicy} with `options.policy`, or with an empty policy: so `issuedAt` in the future
492
+ * and a `notBefore` that has not been reached are always rejected (unless `enforceNotBefore` is `false`), and the
493
+ * other policy rules apply only when set. Timing checks use the current time.
494
+ *
495
+ * Does not verify the signature; the chain packages and `@tuwaio/siwx-server` do that and call this function.
405
496
  *
406
497
  * @param fields - The message fields to validate.
407
- * @param options - Optional validation options or verification policy.
408
- * @returns A `SiwxValidationResult` with `valid: true` or a list of errors.
498
+ * @param options - Skip the expiration check or enforce a {@link SiwxVerificationPolicy} (see {@link validatePolicy}).
499
+ * @returns `{ valid: true, errors: [] }`, or `valid: false` with a description of every failed check.
409
500
  *
410
501
  * @example
411
502
  * ```ts
@@ -419,10 +510,11 @@ declare function validatePolicy(fields: SiwxMessageFields, policy?: SiwxVerifica
419
510
  */
420
511
  declare function validateMessage(fields: SiwxMessageFields, options?: ValidateMessageOptions): SiwxValidationResult;
421
512
  /**
422
- * Generates a cryptographically secure random nonce string suitable for CAIP-122 messages.
423
- * Produces a 16-byte random hex string (32 characters).
513
+ * Generates a random CAIP-122 nonce: 16 bytes from `globalThis.crypto.getRandomValues`, hex-encoded.
514
+ * Works in browsers, Node.js 20+ and edge runtimes that provide the Web Crypto API.
424
515
  *
425
- * @returns A 32-character hexadecimal nonce string.
516
+ * @returns A 32-character lowercase hexadecimal string.
517
+ * @throws {TypeError} If `globalThis.crypto` is not available in the runtime.
426
518
  *
427
519
  * @example
428
520
  * ```ts
@@ -431,24 +523,33 @@ declare function validateMessage(fields: SiwxMessageFields, options?: ValidateMe
431
523
  */
432
524
  declare function generateNonce(): string;
433
525
  /**
434
- * Minimal interface for a SIWX session or parsed CAIP-122 message.
526
+ * Minimal shape of a SIWX session or parsed CAIP-122 message accepted by {@link isSessionMatchingTarget}.
435
527
  */
436
528
  interface SiwxSessionLike {
529
+ /** CAIP-10 account ID of the session, e.g. `eip155:1:0xAb58…`. */
437
530
  address: string;
531
+ /** CAIP-2 chain ID of the session, e.g. `eip155:1`. */
438
532
  chainId?: string;
439
533
  }
440
534
  /**
441
- * Validates whether a SIWX session matches a target wallet address and optional chainId.
442
- * Handles EVM case-insensitivity, Solana case-sensitivity, and CAIP-10/CAIP-2 normalization.
535
+ * Checks whether a SIWX session belongs to a given wallet address and, optionally, chain. Use it on the server to
536
+ * make sure the signed-in account is the one a request acts on.
443
537
  *
444
- * @param session - Active SIWX session or parsed message
445
- * @param targetAddress - Target account address (plain or CAIP-10)
446
- * @param targetChainId - Target chain reference or CAIP-2 identifier
447
- * @returns True if the session matches the target address and chainId; false otherwise.
538
+ * A target that starts with `0x` or `eip155:` is treated as EVM and only matches `eip155:` sessions, compared
539
+ * case-insensitively; any other target only matches `solana:` sessions, compared case-sensitively. The chain is
540
+ * compared only when `targetChainId` is given and the session has a `chainId`.
541
+ *
542
+ * @param session - The session or parsed message to check. `null` and `undefined` never match.
543
+ * @param targetAddress - The expected account, as a plain address or a CAIP-10 account ID.
544
+ * @param targetChainId - Optional expected chain, as a chain reference (`1`, `'1'`) or a CAIP-2 ID (`'eip155:1'`).
545
+ * @returns `true` if the session matches the address and, when given, the chain; otherwise `false`.
448
546
  *
449
547
  * @example
450
548
  * ```ts
451
- * const isValid = isSessionMatchingTarget(session, '0x123...', 1);
549
+ * const session = { address: 'eip155:1:0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B', chainId: 'eip155:1' };
550
+ *
551
+ * isSessionMatchingTarget(session, '0xab5801a7d398351b8be11c439e05c5b3259aec9b', 1); // true
552
+ * isSessionMatchingTarget(session, '0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B', 'eip155:10'); // false
452
553
  * ```
453
554
  */
454
555
  declare function isSessionMatchingTarget(session: SiwxSessionLike | null | undefined, targetAddress: string, targetChainId?: string | number): boolean;