@x402/extensions 2.2.0 → 2.3.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.
Files changed (33) hide show
  1. package/README.md +322 -93
  2. package/dist/cjs/bazaar/index.d.ts +3 -562
  3. package/dist/cjs/bazaar/index.js +12 -0
  4. package/dist/cjs/bazaar/index.js.map +1 -1
  5. package/dist/cjs/index-DvDlinmy.d.ts +575 -0
  6. package/dist/cjs/index.d.ts +4 -1
  7. package/dist/cjs/index.js +1008 -2
  8. package/dist/cjs/index.js.map +1 -1
  9. package/dist/cjs/payment-identifier/index.d.ts +345 -0
  10. package/dist/cjs/payment-identifier/index.js +285 -0
  11. package/dist/cjs/payment-identifier/index.js.map +1 -0
  12. package/dist/cjs/sign-in-with-x/index.d.ts +1054 -1
  13. package/dist/cjs/sign-in-with-x/index.js +766 -0
  14. package/dist/cjs/sign-in-with-x/index.js.map +1 -1
  15. package/dist/esm/bazaar/index.d.mts +3 -562
  16. package/dist/esm/bazaar/index.mjs +1 -1
  17. package/dist/esm/chunk-73HCOE6N.mjs +233 -0
  18. package/dist/esm/chunk-73HCOE6N.mjs.map +1 -0
  19. package/dist/esm/{chunk-WB72GLC2.mjs → chunk-DFJ4ZQFO.mjs} +13 -1
  20. package/dist/esm/chunk-DFJ4ZQFO.mjs.map +1 -0
  21. package/dist/esm/chunk-E3F2XHTI.mjs +719 -0
  22. package/dist/esm/chunk-E3F2XHTI.mjs.map +1 -0
  23. package/dist/esm/index-DvDlinmy.d.mts +575 -0
  24. package/dist/esm/index.d.mts +4 -1
  25. package/dist/esm/index.mjs +102 -3
  26. package/dist/esm/payment-identifier/index.d.mts +345 -0
  27. package/dist/esm/payment-identifier/index.mjs +39 -0
  28. package/dist/esm/sign-in-with-x/index.d.mts +1054 -1
  29. package/dist/esm/sign-in-with-x/index.mjs +66 -1
  30. package/package.json +16 -2
  31. package/dist/esm/chunk-MKFJ5AA3.mjs +0 -1
  32. package/dist/esm/chunk-WB72GLC2.mjs.map +0 -1
  33. /package/dist/esm/{chunk-MKFJ5AA3.mjs.map → payment-identifier/index.mjs.map} +0 -0
@@ -1,2 +1,1055 @@
1
+ import { z } from 'zod';
2
+ import { ResourceServerExtension } from '@x402/core/types';
1
3
 
2
- export { }
4
+ /**
5
+ * Type definitions for the Sign-In-With-X (SIWX) extension
6
+ *
7
+ * Implements CAIP-122 standard for chain-agnostic wallet-based identity assertions.
8
+ * Per x402 v2 spec: typescript/site/CHANGELOG-v2.md lines 237-341
9
+ */
10
+
11
+ /**
12
+ * Extension identifier constant
13
+ */
14
+ declare const SIGN_IN_WITH_X = "sign-in-with-x";
15
+ /**
16
+ * Supported signature schemes per CHANGELOG-v2.md line 271.
17
+ *
18
+ * NOTE: This is primarily informational. Actual signature verification
19
+ * is determined by the chainId prefix, not this field:
20
+ * - `eip155:*` chains use EVM verification (handles eip191, eip712, eip1271, eip6492 automatically)
21
+ * - `solana:*` chains use Ed25519 verification (siws)
22
+ *
23
+ * The signatureScheme field serves as a hint for clients to select
24
+ * the appropriate signing UX.
25
+ */
26
+ type SignatureScheme = "eip191" | "eip1271" | "eip6492" | "siws";
27
+ /** Signature algorithm type per CAIP-122 */
28
+ type SignatureType = "eip191" | "ed25519";
29
+ /**
30
+ * Supported chain configuration in supportedChains array.
31
+ * Specifies which chains the server accepts for authentication.
32
+ */
33
+ interface SupportedChain {
34
+ /** CAIP-2 chain identifier (e.g., "eip155:8453", "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp") */
35
+ chainId: string;
36
+ /** Signature algorithm type per CAIP-122 */
37
+ type: SignatureType;
38
+ /** Optional signature scheme hint (informational) */
39
+ signatureScheme?: SignatureScheme;
40
+ }
41
+ /**
42
+ * Server-declared extension info included in PaymentRequired.extensions.
43
+ * Contains message metadata shared across all supported chains.
44
+ * Per CHANGELOG-v2.md lines 263-272
45
+ */
46
+ interface SIWxExtensionInfo {
47
+ /** Server's domain */
48
+ domain: string;
49
+ /** Full resource URI */
50
+ uri: string;
51
+ /** Human-readable purpose for signing */
52
+ statement?: string;
53
+ /** CAIP-122 version, always "1" */
54
+ version: string;
55
+ /** Cryptographic nonce (SDK auto-generates) */
56
+ nonce: string;
57
+ /** ISO 8601 timestamp (SDK auto-generates) */
58
+ issuedAt: string;
59
+ /** Optional expiry (default: +5 min) */
60
+ expirationTime?: string;
61
+ /** Optional validity start */
62
+ notBefore?: string;
63
+ /** Optional correlation ID */
64
+ requestId?: string;
65
+ /** Associated resources */
66
+ resources?: string[];
67
+ }
68
+ /**
69
+ * JSON Schema for SIWX extension validation
70
+ * Per CHANGELOG-v2.md lines 276-292
71
+ */
72
+ interface SIWxExtensionSchema {
73
+ $schema: string;
74
+ type: "object";
75
+ properties: {
76
+ domain: {
77
+ type: "string";
78
+ };
79
+ address: {
80
+ type: "string";
81
+ };
82
+ statement?: {
83
+ type: "string";
84
+ };
85
+ uri: {
86
+ type: "string";
87
+ format: "uri";
88
+ };
89
+ version: {
90
+ type: "string";
91
+ };
92
+ chainId: {
93
+ type: "string";
94
+ };
95
+ type: {
96
+ type: "string";
97
+ };
98
+ nonce: {
99
+ type: "string";
100
+ };
101
+ issuedAt: {
102
+ type: "string";
103
+ format: "date-time";
104
+ };
105
+ expirationTime?: {
106
+ type: "string";
107
+ format: "date-time";
108
+ };
109
+ notBefore?: {
110
+ type: "string";
111
+ format: "date-time";
112
+ };
113
+ requestId?: {
114
+ type: "string";
115
+ };
116
+ resources?: {
117
+ type: "array";
118
+ items: {
119
+ type: "string";
120
+ format: "uri";
121
+ };
122
+ };
123
+ signature: {
124
+ type: "string";
125
+ };
126
+ };
127
+ required: string[];
128
+ }
129
+ /**
130
+ * Complete SIWX extension structure (info + supportedChains + schema).
131
+ * Follows standard x402 v2 extension pattern with multi-chain support.
132
+ */
133
+ interface SIWxExtension {
134
+ info: SIWxExtensionInfo;
135
+ supportedChains: SupportedChain[];
136
+ schema: SIWxExtensionSchema;
137
+ }
138
+ /**
139
+ * Zod schema for SIWX payload validation
140
+ * Client proof payload sent in SIGN-IN-WITH-X header
141
+ * Per CHANGELOG-v2.md lines 301-315
142
+ */
143
+ declare const SIWxPayloadSchema: z.ZodObject<{
144
+ domain: z.ZodString;
145
+ address: z.ZodString;
146
+ statement: z.ZodOptional<z.ZodString>;
147
+ uri: z.ZodString;
148
+ version: z.ZodString;
149
+ chainId: z.ZodString;
150
+ type: z.ZodEnum<["eip191", "ed25519"]>;
151
+ nonce: z.ZodString;
152
+ issuedAt: z.ZodString;
153
+ expirationTime: z.ZodOptional<z.ZodString>;
154
+ notBefore: z.ZodOptional<z.ZodString>;
155
+ requestId: z.ZodOptional<z.ZodString>;
156
+ resources: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
157
+ signatureScheme: z.ZodOptional<z.ZodEnum<["eip191", "eip1271", "eip6492", "siws"]>>;
158
+ signature: z.ZodString;
159
+ }, "strip", z.ZodTypeAny, {
160
+ type: "eip191" | "ed25519";
161
+ uri: string;
162
+ domain: string;
163
+ address: string;
164
+ version: string;
165
+ chainId: string;
166
+ nonce: string;
167
+ issuedAt: string;
168
+ signature: string;
169
+ statement?: string | undefined;
170
+ expirationTime?: string | undefined;
171
+ notBefore?: string | undefined;
172
+ requestId?: string | undefined;
173
+ resources?: string[] | undefined;
174
+ signatureScheme?: "eip191" | "eip1271" | "eip6492" | "siws" | undefined;
175
+ }, {
176
+ type: "eip191" | "ed25519";
177
+ uri: string;
178
+ domain: string;
179
+ address: string;
180
+ version: string;
181
+ chainId: string;
182
+ nonce: string;
183
+ issuedAt: string;
184
+ signature: string;
185
+ statement?: string | undefined;
186
+ expirationTime?: string | undefined;
187
+ notBefore?: string | undefined;
188
+ requestId?: string | undefined;
189
+ resources?: string[] | undefined;
190
+ signatureScheme?: "eip191" | "eip1271" | "eip6492" | "siws" | undefined;
191
+ }>;
192
+ /**
193
+ * Client proof payload type (inferred from zod schema)
194
+ */
195
+ type SIWxPayload = z.infer<typeof SIWxPayloadSchema>;
196
+ /**
197
+ * Options for declaring SIWX extension on server.
198
+ *
199
+ * Most fields are optional and derived automatically from request context:
200
+ * - `domain`: Parsed from resourceUri or request URL
201
+ * - `resourceUri`: From request URL
202
+ * - `network`: From payment requirements (accepts[].network)
203
+ *
204
+ * Explicit values override automatic derivation.
205
+ */
206
+ interface DeclareSIWxOptions {
207
+ /** Server's domain. If omitted, derived from resourceUri or request URL. */
208
+ domain?: string;
209
+ /** Full resource URI. If omitted, derived from request URL. */
210
+ resourceUri?: string;
211
+ /** Human-readable purpose */
212
+ statement?: string;
213
+ /** CAIP-122 version (default: "1") */
214
+ version?: string;
215
+ /**
216
+ * Network(s) to support. If omitted, derived from payment requirements.
217
+ * - Single chain: "eip155:8453" or "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"
218
+ * - Multi-chain: ["eip155:8453", "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"]
219
+ */
220
+ network?: string | string[];
221
+ /**
222
+ * Optional expiration duration in seconds.
223
+ * - Number (e.g., 300): Signature expires after this many seconds
224
+ * - undefined: Infinite expiration (no expirationTime field in wire format)
225
+ */
226
+ expirationSeconds?: number;
227
+ }
228
+ /**
229
+ * Validation result from validateSIWxMessage
230
+ */
231
+ interface SIWxValidationResult {
232
+ valid: boolean;
233
+ error?: string;
234
+ }
235
+ /**
236
+ * Options for message validation
237
+ */
238
+ interface SIWxValidationOptions {
239
+ /** Maximum age for issuedAt in milliseconds (default: 5 minutes) */
240
+ maxAge?: number;
241
+ /** Custom nonce validation function */
242
+ checkNonce?: (nonce: string) => boolean | Promise<boolean>;
243
+ }
244
+ /**
245
+ * Result from signature verification
246
+ */
247
+ interface SIWxVerifyResult {
248
+ valid: boolean;
249
+ /** Recovered/verified address (checksummed) */
250
+ address?: string;
251
+ error?: string;
252
+ }
253
+ /**
254
+ * EVM message verifier function type.
255
+ * Compatible with viem's publicClient.verifyMessage().
256
+ *
257
+ * When provided to verifySIWxSignature, enables:
258
+ * - EIP-1271 (deployed smart contract wallets)
259
+ * - EIP-6492 (counterfactual/pre-deploy smart wallets)
260
+ *
261
+ * Without a verifier, only EOA signatures (EIP-191) can be verified.
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * import { createPublicClient, http } from 'viem';
266
+ * import { base } from 'viem/chains';
267
+ *
268
+ * const publicClient = createPublicClient({ chain: base, transport: http() });
269
+ * // publicClient.verifyMessage satisfies EVMMessageVerifier
270
+ * ```
271
+ */
272
+ type EVMMessageVerifier = (args: {
273
+ address: `0x${string}`;
274
+ message: string;
275
+ signature: `0x${string}`;
276
+ }) => Promise<boolean>;
277
+ /**
278
+ * Options for SIWX signature verification
279
+ */
280
+ interface SIWxVerifyOptions {
281
+ /**
282
+ * EVM message verifier for smart wallet support.
283
+ *
284
+ * Pass `publicClient.verifyMessage` from viem to enable verification of:
285
+ * - Smart contract wallets (EIP-1271)
286
+ * - Counterfactual/undeployed smart wallets (EIP-6492)
287
+ *
288
+ * If not provided, only EOA signatures are verified using standalone
289
+ * ECDSA recovery (no RPC calls required).
290
+ */
291
+ evmVerifier?: EVMMessageVerifier;
292
+ }
293
+
294
+ /**
295
+ * Message signing for SIWX extension
296
+ *
297
+ * Client-side helpers for signing SIWX messages.
298
+ * Supports both EVM (viem) and Solana wallet adapters.
299
+ */
300
+ /**
301
+ * Signer interface for EVM SIWX message signing.
302
+ * Compatible with viem WalletClient and PrivateKeyAccount.
303
+ */
304
+ interface EVMSigner {
305
+ /** Sign a message and return hex-encoded signature */
306
+ signMessage: (args: {
307
+ message: string;
308
+ account?: unknown;
309
+ }) => Promise<string>;
310
+ /** Account object (for WalletClient) */
311
+ account?: {
312
+ address: string;
313
+ };
314
+ /** Direct address (for PrivateKeyAccount) */
315
+ address?: string;
316
+ }
317
+ /**
318
+ * Signer interface for Solana SIWX message signing.
319
+ * Compatible with @solana/wallet-adapter and Phantom/Solflare wallet APIs.
320
+ */
321
+ interface SolanaSigner {
322
+ /** Sign a message and return raw signature bytes */
323
+ signMessage: (message: Uint8Array) => Promise<Uint8Array>;
324
+ /** Solana public key (Base58 encoded string or PublicKey-like object) */
325
+ publicKey: string | {
326
+ toBase58: () => string;
327
+ };
328
+ }
329
+ /**
330
+ * Union type for SIWX signers - supports both EVM and Solana wallets.
331
+ */
332
+ type SIWxSigner = EVMSigner | SolanaSigner;
333
+ /**
334
+ * Get address from an EVM signer.
335
+ *
336
+ * @param signer - EVM wallet signer instance
337
+ * @returns The wallet address as a hex string
338
+ */
339
+ declare function getEVMAddress(signer: EVMSigner): string;
340
+ /**
341
+ * Get address from a Solana signer.
342
+ *
343
+ * @param signer - Solana wallet signer instance
344
+ * @returns The wallet address as a Base58 string
345
+ */
346
+ declare function getSolanaAddress(signer: SolanaSigner): string;
347
+ /**
348
+ * Sign a message with an EVM wallet.
349
+ * Returns hex-encoded signature.
350
+ *
351
+ * @param message - The message to sign
352
+ * @param signer - EVM wallet signer instance
353
+ * @returns Hex-encoded signature
354
+ */
355
+ declare function signEVMMessage(message: string, signer: EVMSigner): Promise<string>;
356
+ /**
357
+ * Sign a message with a Solana wallet.
358
+ * Returns Base58-encoded signature.
359
+ *
360
+ * @param message - The message to sign
361
+ * @param signer - Solana wallet signer instance
362
+ * @returns Base58-encoded signature
363
+ */
364
+ declare function signSolanaMessage(message: string, signer: SolanaSigner): Promise<string>;
365
+
366
+ /**
367
+ * Complete client flow for SIWX extension
368
+ *
369
+ * Combines message construction, signing, and payload creation.
370
+ * Supports both EVM and Solana wallets.
371
+ */
372
+
373
+ /**
374
+ * Complete SIWX info with chain-specific fields.
375
+ * Used by utility functions that need the selected chain information.
376
+ */
377
+ type CompleteSIWxInfo = SIWxExtensionInfo & {
378
+ chainId: string;
379
+ type: SignatureType;
380
+ signatureScheme?: SignatureScheme;
381
+ };
382
+ /**
383
+ * Create a complete SIWX payload from server extension info with selected chain.
384
+ *
385
+ * Routes to EVM or Solana signing based on the chainId prefix:
386
+ * - `eip155:*` → EVM signing
387
+ * - `solana:*` → Solana signing
388
+ *
389
+ * @param serverExtension - Server extension info with chain selected (includes chainId, type)
390
+ * @param signer - Wallet that can sign messages (EVMSigner or SolanaSigner)
391
+ * @returns Complete SIWX payload with signature
392
+ *
393
+ * @example
394
+ * ```typescript
395
+ * // EVM wallet
396
+ * const completeInfo = { ...extension.info, chainId: "eip155:8453", type: "eip191" };
397
+ * const payload = await createSIWxPayload(completeInfo, evmWallet);
398
+ * ```
399
+ */
400
+ declare function createSIWxPayload(serverExtension: CompleteSIWxInfo, signer: SIWxSigner): Promise<SIWxPayload>;
401
+
402
+ /**
403
+ * Solana Sign-In-With-X (SIWS) support
404
+ *
405
+ * Implements CAIP-122 compliant message format and Ed25519 signature verification
406
+ * for Solana wallets.
407
+ */
408
+
409
+ /**
410
+ * Common Solana network CAIP-2 identifiers.
411
+ * Uses genesis hash as the chain reference per CAIP-30.
412
+ */
413
+ declare const SOLANA_MAINNET = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp";
414
+ declare const SOLANA_DEVNET = "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1";
415
+ declare const SOLANA_TESTNET = "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z";
416
+ /**
417
+ * Extract chain reference from CAIP-2 Solana chainId.
418
+ *
419
+ * @param chainId - CAIP-2 format chain ID (e.g., "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp")
420
+ * @returns Chain reference (genesis hash)
421
+ *
422
+ * @example
423
+ * ```typescript
424
+ * extractSolanaChainReference("solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp") // "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"
425
+ * ```
426
+ */
427
+ declare function extractSolanaChainReference(chainId: string): string;
428
+ /**
429
+ * Format SIWS message following CAIP-122 ABNF specification.
430
+ *
431
+ * The message format is identical to SIWE (EIP-4361) but uses "Solana account"
432
+ * instead of "Ethereum account" in the header line.
433
+ *
434
+ * @param info - Server-provided extension info
435
+ * @param address - Client's Solana wallet address (Base58 encoded public key)
436
+ * @returns Message string ready for signing
437
+ *
438
+ * @example
439
+ * ```typescript
440
+ * const message = formatSIWSMessage(serverInfo, "BSmWDgE9ex6dZYbiTsJGcwMEgFp8q4aWh92hdErQPeVW");
441
+ * // Returns:
442
+ * // "api.example.com wants you to sign in with your Solana account:
443
+ * // BSmWDgE9ex6dZYbiTsJGcwMEgFp8q4aWh92hdErQPeVW
444
+ * //
445
+ * // Sign in to access your content
446
+ * //
447
+ * // URI: https://api.example.com/data
448
+ * // Version: 1
449
+ * // Chain ID: mainnet
450
+ * // Nonce: abc123
451
+ * // Issued At: 2024-01-01T00:00:00.000Z"
452
+ * ```
453
+ */
454
+ declare function formatSIWSMessage(info: CompleteSIWxInfo, address: string): string;
455
+ /**
456
+ * Verify Ed25519 signature for SIWS.
457
+ *
458
+ * @param message - The SIWS message that was signed
459
+ * @param signature - Ed25519 signature bytes
460
+ * @param publicKey - Solana public key bytes (32 bytes)
461
+ * @returns true if signature is valid
462
+ *
463
+ * @example
464
+ * ```typescript
465
+ * const messageBytes = new TextEncoder().encode(message);
466
+ * const valid = verifySolanaSignature(message, signatureBytes, publicKeyBytes);
467
+ * ```
468
+ */
469
+ declare function verifySolanaSignature(message: string, signature: Uint8Array, publicKey: Uint8Array): boolean;
470
+ /**
471
+ * Decode Base58 string to bytes.
472
+ *
473
+ * Solana uses Base58 encoding (Bitcoin alphabet) for addresses and signatures.
474
+ *
475
+ * @param encoded - Base58 encoded string
476
+ * @returns Decoded bytes
477
+ * @throws Error if string contains invalid Base58 characters
478
+ *
479
+ * @example
480
+ * ```typescript
481
+ * const publicKeyBytes = decodeBase58("BSmWDgE9ex6dZYbiTsJGcwMEgFp8q4aWh92hdErQPeVW");
482
+ * // Returns Uint8Array of 32 bytes
483
+ * ```
484
+ */
485
+ declare function decodeBase58(encoded: string): Uint8Array;
486
+ /**
487
+ * Encode bytes to Base58 string.
488
+ *
489
+ * @param bytes - Bytes to encode
490
+ * @returns Base58 encoded string
491
+ */
492
+ declare function encodeBase58(bytes: Uint8Array): string;
493
+
494
+ /**
495
+ * Server-side declaration helper for SIWX extension
496
+ *
497
+ * Helps servers declare SIWX authentication requirements in PaymentRequired responses.
498
+ */
499
+
500
+ /**
501
+ * Internal type for SIWX declaration with stored options.
502
+ * The _options field is used by enrichPaymentRequiredResponse to derive
503
+ * values from request context.
504
+ */
505
+ interface SIWxDeclaration extends SIWxExtension {
506
+ _options: DeclareSIWxOptions;
507
+ }
508
+ /**
509
+ * Create SIWX extension declaration for PaymentRequired.extensions
510
+ *
511
+ * Most fields are derived automatically from request context when using
512
+ * siwxResourceServerExtension:
513
+ * - `network`: From payment requirements (accepts[].network)
514
+ * - `resourceUri`: From request URL
515
+ * - `domain`: Parsed from resourceUri
516
+ *
517
+ * Explicit values in options override automatic derivation.
518
+ *
519
+ * @param options - Configuration options (most are optional)
520
+ * @returns Extension object ready for PaymentRequired.extensions
521
+ *
522
+ * @example
523
+ * ```typescript
524
+ * // Minimal - derives network, domain, resourceUri from context
525
+ * const extensions = declareSIWxExtension({
526
+ * statement: 'Sign in to access your purchased content',
527
+ * });
528
+ *
529
+ * // With explicit network (overrides accepts)
530
+ * const extensions = declareSIWxExtension({
531
+ * network: 'eip155:8453',
532
+ * statement: 'Sign in to access',
533
+ * });
534
+ *
535
+ * // Full explicit config (no derivation)
536
+ * const extensions = declareSIWxExtension({
537
+ * domain: 'api.example.com',
538
+ * resourceUri: 'https://api.example.com/data',
539
+ * network: ['eip155:8453', 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'],
540
+ * statement: 'Sign in to access',
541
+ * expirationSeconds: 300,
542
+ * });
543
+ * ```
544
+ */
545
+ declare function declareSIWxExtension(options?: DeclareSIWxOptions): Record<string, SIWxDeclaration>;
546
+
547
+ /**
548
+ * Server-side ResourceServerExtension for SIWX
549
+ *
550
+ * Provides enrichPaymentRequiredResponse hook to:
551
+ * - Derive missing fields from request context (network, resourceUri, domain)
552
+ * - Refresh time-based fields per request (nonce, issuedAt, expirationTime)
553
+ */
554
+
555
+ /**
556
+ * SIWX Resource Server Extension.
557
+ *
558
+ * Implements enrichPaymentRequiredResponse hook to:
559
+ * 1. Derive missing fields from context (network from requirements, URL from resourceInfo)
560
+ * 2. Refresh time-based fields (nonce, issuedAt, expirationTime) per request
561
+ *
562
+ * @example
563
+ * ```typescript
564
+ * import { siwxResourceServerExtension } from "@x402/extensions/sign-in-with-x";
565
+ *
566
+ * const resourceServer = new x402ResourceServer(facilitator)
567
+ * .registerExtension(siwxResourceServerExtension)
568
+ * .onAfterSettle(createSIWxSettleHook({ storage }));
569
+ * ```
570
+ */
571
+ declare const siwxResourceServerExtension: ResourceServerExtension;
572
+
573
+ /**
574
+ * Header parsing for SIWX extension
575
+ *
576
+ * Parses the SIGN-IN-WITH-X header from client requests.
577
+ * Requires base64-encoded JSON per x402 v2 spec.
578
+ */
579
+
580
+ /**
581
+ * Parse SIGN-IN-WITH-X header into structured payload.
582
+ *
583
+ * Expects base64-encoded JSON per x402 v2 spec (CHANGELOG-v2.md line 335).
584
+ *
585
+ * @param header - The SIGN-IN-WITH-X header value (base64-encoded JSON)
586
+ * @returns Parsed SIWX payload
587
+ * @throws Error if header is invalid or missing required fields
588
+ *
589
+ * @example
590
+ * ```typescript
591
+ * const header = request.headers.get('SIGN-IN-WITH-X');
592
+ * if (header) {
593
+ * const payload = parseSIWxHeader(header);
594
+ * // payload.address, payload.signature, etc.
595
+ * }
596
+ * ```
597
+ */
598
+ declare function parseSIWxHeader(header: string): SIWxPayload;
599
+
600
+ /**
601
+ * Message validation for SIWX extension
602
+ *
603
+ * Validates SIWX payload fields before cryptographic verification.
604
+ * Per CHANGELOG-v2.md validation rules (lines 318-329).
605
+ */
606
+
607
+ /**
608
+ * Validate SIWX message fields.
609
+ *
610
+ * Performs validation per spec (CHANGELOG-v2.md lines 318-329):
611
+ * - Domain binding: domain MUST match server's domain
612
+ * - URI validation: uri must refer to base url of resource
613
+ * - Temporal validation:
614
+ * - issuedAt MUST be recent (< 5 minutes by default)
615
+ * - expirationTime MUST be in the future
616
+ * - notBefore (if present) MUST be in the past
617
+ * - Nonce: MUST be unique (via optional checkNonce callback)
618
+ *
619
+ * @param message - The SIWX payload to validate
620
+ * @param expectedResourceUri - Expected resource URI (for domain/URI matching)
621
+ * @param options - Validation options
622
+ * @returns Validation result
623
+ *
624
+ * @example
625
+ * ```typescript
626
+ * const payload = parseSIWxHeader(header);
627
+ * const result = await validateSIWxMessage(
628
+ * payload,
629
+ * 'https://api.example.com/data',
630
+ * { checkNonce: (n) => !usedNonces.has(n) }
631
+ * );
632
+ *
633
+ * if (!result.valid) {
634
+ * return { error: result.error };
635
+ * }
636
+ * ```
637
+ */
638
+ declare function validateSIWxMessage(message: SIWxPayload, expectedResourceUri: string, options?: SIWxValidationOptions): Promise<SIWxValidationResult>;
639
+
640
+ /**
641
+ * Signature verification for SIWX extension
642
+ *
643
+ * Routes to chain-specific verification based on chainId namespace:
644
+ * - EVM (eip155:*): EOA by default, smart wallet (EIP-1271/EIP-6492) with verifier
645
+ * - Solana (solana:*): Ed25519 signature verification via tweetnacl
646
+ */
647
+
648
+ /**
649
+ * Verify SIWX signature cryptographically.
650
+ *
651
+ * Routes to the appropriate chain-specific verification based on the
652
+ * chainId namespace prefix:
653
+ * - `eip155:*` → EVM verification (EOA by default, smart wallet with verifier)
654
+ * - `solana:*` → Ed25519 signature verification
655
+ *
656
+ * @param payload - The SIWX payload containing signature
657
+ * @param options - Optional verification options
658
+ * @returns Verification result with recovered address if valid
659
+ *
660
+ * @example
661
+ * ```typescript
662
+ * // EOA-only verification (default)
663
+ * const result = await verifySIWxSignature(payload);
664
+ *
665
+ * // Smart wallet verification
666
+ * import { createPublicClient, http } from 'viem';
667
+ * import { base } from 'viem/chains';
668
+ *
669
+ * const publicClient = createPublicClient({ chain: base, transport: http() });
670
+ * const result = await verifySIWxSignature(payload, {
671
+ * evmVerifier: publicClient.verifyMessage,
672
+ * });
673
+ *
674
+ * if (result.valid) {
675
+ * console.log('Verified wallet:', result.address);
676
+ * } else {
677
+ * console.error('Verification failed:', result.error);
678
+ * }
679
+ * ```
680
+ */
681
+ declare function verifySIWxSignature(payload: SIWxPayload, options?: SIWxVerifyOptions): Promise<SIWxVerifyResult>;
682
+
683
+ /**
684
+ * JSON Schema builder for SIWX extension
685
+ *
686
+ * Per CHANGELOG-v2.md lines 276-292
687
+ */
688
+
689
+ /**
690
+ * Build JSON Schema for SIWX extension validation.
691
+ * This schema validates the client proof payload structure.
692
+ *
693
+ * @returns JSON Schema for validating SIWX client payloads
694
+ */
695
+ declare function buildSIWxSchema(): SIWxExtensionSchema;
696
+
697
+ /**
698
+ * CAIP-122 message construction for SIWX extension
699
+ *
700
+ * Constructs the canonical message string for signing.
701
+ * Routes to chain-specific formatters based on chainId namespace.
702
+ */
703
+
704
+ /**
705
+ * Construct CAIP-122 compliant message string for signing.
706
+ *
707
+ * Routes to the appropriate chain-specific message formatter based on the
708
+ * chainId namespace prefix:
709
+ * - `eip155:*` → SIWE (EIP-4361) format via siwe library
710
+ * - `solana:*` → SIWS format
711
+ *
712
+ * @param serverInfo - Server extension info with chain selected (includes chainId)
713
+ * @param address - Client wallet address
714
+ * @returns Message string ready for signing
715
+ * @throws Error if chainId namespace is not supported
716
+ *
717
+ * @example
718
+ * ```typescript
719
+ * // EVM (Ethereum, Base, etc.)
720
+ * const completeInfo = { ...extension.info, chainId: "eip155:8453", type: "eip191" };
721
+ * const evmMessage = createSIWxMessage(completeInfo, "0x1234...");
722
+ * ```
723
+ */
724
+ declare function createSIWxMessage(serverInfo: CompleteSIWxInfo, address: string): string;
725
+
726
+ /**
727
+ * Header encoding for SIWX extension
728
+ *
729
+ * Encodes SIWX payload for the SIGN-IN-WITH-X HTTP header.
730
+ * Per CHANGELOG-v2.md line 335: header should be base64-encoded.
731
+ */
732
+
733
+ /**
734
+ * Encode SIWX payload for SIGN-IN-WITH-X header.
735
+ *
736
+ * Uses base64 encoding per x402 v2 spec (CHANGELOG-v2.md line 335).
737
+ *
738
+ * @param payload - Complete SIWX payload with signature
739
+ * @returns Base64-encoded JSON string
740
+ *
741
+ * @example
742
+ * ```typescript
743
+ * const payload = await createSIWxPayload(serverInfo, signer);
744
+ * const header = encodeSIWxHeader(payload);
745
+ *
746
+ * fetch(url, {
747
+ * headers: { 'SIGN-IN-WITH-X': header }
748
+ * });
749
+ * ```
750
+ */
751
+ declare function encodeSIWxHeader(payload: SIWxPayload): string;
752
+
753
+ /**
754
+ * Fetch wrapper for SIWX authentication.
755
+ *
756
+ * Provides a convenient wrapper around fetch that automatically handles
757
+ * SIWX authentication when a 402 response includes SIWX extension info.
758
+ */
759
+
760
+ /**
761
+ * Wraps fetch to automatically handle SIWX authentication.
762
+ *
763
+ * When a 402 response is received with a SIWX extension:
764
+ * 1. Extracts SIWX info from PAYMENT-REQUIRED header
765
+ * 2. Creates signed SIWX proof using the provided signer
766
+ * 3. Retries the request with the SIWX header
767
+ *
768
+ * If the 402 response doesn't include SIWX extension info, the original
769
+ * response is returned unchanged (allowing payment handling to proceed).
770
+ *
771
+ * @param fetch - The fetch function to wrap (typically globalThis.fetch)
772
+ * @param signer - Wallet signer (EVMSigner or SolanaSigner)
773
+ * @returns A wrapped fetch function that handles SIWX authentication
774
+ *
775
+ * @example
776
+ * ```typescript
777
+ * import { wrapFetchWithSIWx } from '@x402/extensions/sign-in-with-x';
778
+ * import { privateKeyToAccount } from 'viem/accounts';
779
+ *
780
+ * const signer = privateKeyToAccount(privateKey);
781
+ * const fetchWithSIWx = wrapFetchWithSIWx(fetch, signer);
782
+ *
783
+ * // Request that may require SIWX auth (for returning paid users)
784
+ * const response = await fetchWithSIWx('https://api.example.com/data');
785
+ * ```
786
+ */
787
+ declare function wrapFetchWithSIWx(fetch: typeof globalThis.fetch, signer: SIWxSigner): (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
788
+
789
+ /**
790
+ * EVM Sign-In-With-Ethereum (SIWE) support
791
+ *
792
+ * Implements EIP-4361 compliant message format and signature verification
793
+ * for EVM chains (Ethereum, Base, Polygon, etc.)
794
+ */
795
+
796
+ /**
797
+ * Extract numeric chain ID from CAIP-2 EVM chainId.
798
+ *
799
+ * @param chainId - CAIP-2 format chain ID (e.g., "eip155:8453")
800
+ * @returns Numeric chain ID (e.g., 8453)
801
+ * @throws Error if chainId format is invalid
802
+ *
803
+ * @example
804
+ * ```typescript
805
+ * extractEVMChainId("eip155:1") // 1 (Ethereum mainnet)
806
+ * extractEVMChainId("eip155:8453") // 8453 (Base)
807
+ * extractEVMChainId("eip155:137") // 137 (Polygon)
808
+ * ```
809
+ */
810
+ declare function extractEVMChainId(chainId: string): number;
811
+ /**
812
+ * Format SIWE message following EIP-4361 specification.
813
+ *
814
+ * Uses the siwe library to ensure message format matches verification.
815
+ *
816
+ * @param info - Server-provided extension info
817
+ * @param address - Client's EVM wallet address (0x-prefixed)
818
+ * @returns Message string ready for signing
819
+ *
820
+ * @example
821
+ * ```typescript
822
+ * const message = formatSIWEMessage(serverInfo, "0x1234...abcd");
823
+ * // Returns EIP-4361 formatted message:
824
+ * // "api.example.com wants you to sign in with your Ethereum account:
825
+ * // 0x1234...abcd
826
+ * //
827
+ * // Sign in to access your content
828
+ * //
829
+ * // URI: https://api.example.com/data
830
+ * // Version: 1
831
+ * // Chain ID: 8453
832
+ * // Nonce: abc123
833
+ * // Issued At: 2024-01-01T00:00:00.000Z"
834
+ * ```
835
+ */
836
+ declare function formatSIWEMessage(info: CompleteSIWxInfo, address: string): string;
837
+ /**
838
+ * Verify EVM signature.
839
+ *
840
+ * Supports:
841
+ * - EOA signatures (standard ECDSA via EIP-191) - always available
842
+ * - EIP-1271 (deployed smart contract wallets) - requires verifier
843
+ * - EIP-6492 (counterfactual/pre-deploy smart wallets) - requires verifier
844
+ *
845
+ * @param message - The SIWE message that was signed
846
+ * @param address - The claimed signer address
847
+ * @param signature - The signature to verify
848
+ * @param verifier - Optional message verifier for smart wallet support.
849
+ * Pass publicClient.verifyMessage for EIP-1271/EIP-6492 support.
850
+ * Without this, only EOA signatures are verified.
851
+ * @returns true if signature is valid
852
+ *
853
+ * @example
854
+ * ```typescript
855
+ * // EOA-only verification (default, no RPC required)
856
+ * const valid = await verifyEVMSignature(message, address, signature);
857
+ *
858
+ * // Smart wallet verification with viem PublicClient
859
+ * import { createPublicClient, http } from 'viem';
860
+ * import { base } from 'viem/chains';
861
+ *
862
+ * const publicClient = createPublicClient({ chain: base, transport: http() });
863
+ * const valid = await verifyEVMSignature(
864
+ * message,
865
+ * address,
866
+ * signature,
867
+ * publicClient.verifyMessage
868
+ * );
869
+ * ```
870
+ */
871
+ declare function verifyEVMSignature(message: string, address: string, signature: string, verifier?: EVMMessageVerifier): Promise<boolean>;
872
+
873
+ /**
874
+ * Storage interface for SIWX payment tracking.
875
+ *
876
+ * Implementations track which addresses have paid for which resources,
877
+ * enabling SIWX authentication to grant access without re-payment.
878
+ *
879
+ * Optionally supports nonce tracking to prevent signature replay attacks.
880
+ */
881
+ interface SIWxStorage {
882
+ /**
883
+ * Check if an address has paid for a resource.
884
+ *
885
+ * @param resource - The resource path (e.g., "/weather")
886
+ * @param address - The wallet address to check
887
+ * @returns True if the address has paid for the resource
888
+ */
889
+ hasPaid(resource: string, address: string): boolean | Promise<boolean>;
890
+ /**
891
+ * Record that an address has paid for a resource.
892
+ *
893
+ * @param resource - The resource path
894
+ * @param address - The wallet address that paid
895
+ */
896
+ recordPayment(resource: string, address: string): void | Promise<void>;
897
+ /**
898
+ * Check if a nonce has already been used (optional).
899
+ *
900
+ * Implementing this method prevents signature replay attacks where
901
+ * an intercepted SIWX header could be reused by an attacker.
902
+ *
903
+ * @param nonce - The nonce from the SIWX payload
904
+ * @returns True if the nonce has been used
905
+ */
906
+ hasUsedNonce?(nonce: string): boolean | Promise<boolean>;
907
+ /**
908
+ * Record that a nonce has been used (optional).
909
+ *
910
+ * Called after successfully granting access via SIWX.
911
+ * Implementations should consider adding expiration to avoid unbounded growth.
912
+ *
913
+ * @param nonce - The nonce to record as used
914
+ */
915
+ recordNonce?(nonce: string): void | Promise<void>;
916
+ }
917
+ /**
918
+ * In-memory implementation of SIWxStorage.
919
+ *
920
+ * Suitable for development and single-instance deployments.
921
+ * For production multi-instance deployments, use a persistent storage implementation.
922
+ */
923
+ declare class InMemorySIWxStorage implements SIWxStorage {
924
+ private paidAddresses;
925
+ /**
926
+ * Check if an address has paid for a resource.
927
+ *
928
+ * @param resource - The resource path
929
+ * @param address - The wallet address to check
930
+ * @returns True if the address has paid
931
+ */
932
+ hasPaid(resource: string, address: string): boolean;
933
+ /**
934
+ * Record that an address has paid for a resource.
935
+ *
936
+ * @param resource - The resource path
937
+ * @param address - The wallet address that paid
938
+ */
939
+ recordPayment(resource: string, address: string): void;
940
+ }
941
+
942
+ /**
943
+ * SIWX Lifecycle Hooks
944
+ *
945
+ * Pre-built hooks for integrating SIWX authentication with x402 servers and clients.
946
+ */
947
+
948
+ /**
949
+ * Options for creating server-side SIWX hooks.
950
+ */
951
+ interface CreateSIWxHookOptions {
952
+ /** Storage for tracking paid addresses */
953
+ storage: SIWxStorage;
954
+ /** Options for signature verification (e.g., EVM smart wallet support) */
955
+ verifyOptions?: SIWxVerifyOptions;
956
+ /** Optional callback for logging/debugging */
957
+ onEvent?: (event: SIWxHookEvent) => void;
958
+ }
959
+ /**
960
+ * Events emitted by SIWX hooks for logging/debugging.
961
+ */
962
+ type SIWxHookEvent = {
963
+ type: "payment_recorded";
964
+ resource: string;
965
+ address: string;
966
+ } | {
967
+ type: "access_granted";
968
+ resource: string;
969
+ address: string;
970
+ } | {
971
+ type: "validation_failed";
972
+ resource: string;
973
+ error?: string;
974
+ } | {
975
+ type: "nonce_reused";
976
+ resource: string;
977
+ nonce: string;
978
+ } | {
979
+ type: "siwx_header_sent";
980
+ resource: string;
981
+ };
982
+ /**
983
+ * Creates an onAfterSettle hook that records payments for SIWX.
984
+ *
985
+ * @param options - Hook configuration
986
+ * @returns Hook function for x402ResourceServer.onAfterSettle()
987
+ *
988
+ * @example
989
+ * ```typescript
990
+ * const storage = new InMemorySIWxStorage();
991
+ * const resourceServer = new x402ResourceServer(facilitator)
992
+ * .onAfterSettle(createSIWxSettleHook({ storage }));
993
+ * ```
994
+ */
995
+ declare function createSIWxSettleHook(options: CreateSIWxHookOptions): (ctx: {
996
+ paymentPayload: {
997
+ payload: unknown;
998
+ resource: {
999
+ url: string;
1000
+ };
1001
+ };
1002
+ result: {
1003
+ success: boolean;
1004
+ payer?: string;
1005
+ };
1006
+ }) => Promise<void>;
1007
+ /**
1008
+ * Creates an onProtectedRequest hook that validates SIWX auth before payment.
1009
+ *
1010
+ * @param options - Hook configuration
1011
+ * @returns Hook function for x402HTTPResourceServer.onProtectedRequest()
1012
+ *
1013
+ * @example
1014
+ * ```typescript
1015
+ * const storage = new InMemorySIWxStorage();
1016
+ * const httpServer = new x402HTTPResourceServer(resourceServer, routes)
1017
+ * .onProtectedRequest(createSIWxRequestHook({ storage }));
1018
+ * ```
1019
+ */
1020
+ declare function createSIWxRequestHook(options: CreateSIWxHookOptions): (context: {
1021
+ adapter: {
1022
+ getHeader(name: string): string | undefined;
1023
+ getUrl(): string;
1024
+ };
1025
+ path: string;
1026
+ }) => Promise<void | {
1027
+ grantAccess: true;
1028
+ }>;
1029
+ /**
1030
+ * Creates an onPaymentRequired hook for client-side SIWX authentication.
1031
+ *
1032
+ * Uses the network from payment requirements to select the appropriate chain
1033
+ * from the server's supportedChains.
1034
+ *
1035
+ * @param signer - Wallet signer for creating SIWX proofs
1036
+ * @returns Hook function for x402HTTPClient.onPaymentRequired()
1037
+ *
1038
+ * @example
1039
+ * ```typescript
1040
+ * const httpClient = new x402HTTPClient(client)
1041
+ * .onPaymentRequired(createSIWxClientHook(signer));
1042
+ * ```
1043
+ */
1044
+ declare function createSIWxClientHook(signer: SIWxSigner): (context: {
1045
+ paymentRequired: {
1046
+ accepts?: Array<{
1047
+ network: string;
1048
+ }>;
1049
+ extensions?: Record<string, unknown>;
1050
+ };
1051
+ }) => Promise<{
1052
+ headers: Record<string, string>;
1053
+ } | void>;
1054
+
1055
+ export { type CompleteSIWxInfo, type CreateSIWxHookOptions, type DeclareSIWxOptions, type EVMMessageVerifier, type EVMSigner, InMemorySIWxStorage, SIGN_IN_WITH_X, type SIWxExtension, type SIWxExtensionInfo, type SIWxExtensionSchema, type SIWxHookEvent, type SIWxPayload, SIWxPayloadSchema, type SIWxSigner, type SIWxStorage, type SIWxValidationOptions, type SIWxValidationResult, type SIWxVerifyOptions, type SIWxVerifyResult, SOLANA_DEVNET, SOLANA_MAINNET, SOLANA_TESTNET, type SignatureScheme, type SignatureType, type SolanaSigner, type SupportedChain, buildSIWxSchema, createSIWxClientHook, createSIWxMessage, createSIWxPayload, createSIWxRequestHook, createSIWxSettleHook, declareSIWxExtension, decodeBase58, encodeBase58, encodeSIWxHeader, extractEVMChainId, extractSolanaChainReference, formatSIWEMessage, formatSIWSMessage, getEVMAddress, getSolanaAddress, parseSIWxHeader, signEVMMessage, signSolanaMessage, siwxResourceServerExtension, validateSIWxMessage, verifyEVMSignature, verifySIWxSignature, verifySolanaSignature, wrapFetchWithSIWx };