@x402/extensions 2.2.0 → 2.3.1

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 +1066 -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 +1090 -1
  13. package/dist/cjs/sign-in-with-x/index.js +824 -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-IASAX5HM.mjs +775 -0
  22. package/dist/esm/chunk-IASAX5HM.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 +106 -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 +1090 -1
  29. package/dist/esm/sign-in-with-x/index.mjs +70 -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,1091 @@
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
+ * Wallet adapter style Solana signer.
319
+ * Compatible with @solana/wallet-adapter, Phantom/Solflare wallet APIs.
320
+ */
321
+ interface WalletAdapterSigner {
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
+ * Solana Kit KeyPairSigner style.
331
+ * Compatible with createKeyPairSignerFromBytes and generateKeyPairSigner from @solana/kit.
332
+ */
333
+ type SolanaKitSigner = {
334
+ /** Solana address (Base58 encoded string) */
335
+ address: string;
336
+ /** Sign messages - accepts messages with content and signatures */
337
+ signMessages: (messages: Array<{
338
+ content: Uint8Array;
339
+ signatures: Record<string, unknown>;
340
+ }>) => Promise<Array<Record<string, Uint8Array>>>;
341
+ };
342
+ /**
343
+ * Union type for Solana signers - supports both wallet adapter and @solana/kit.
344
+ */
345
+ type SolanaSigner = WalletAdapterSigner | SolanaKitSigner;
346
+ /**
347
+ * Union type for SIWX signers - supports both EVM and Solana wallets.
348
+ */
349
+ type SIWxSigner = EVMSigner | SolanaSigner;
350
+ /**
351
+ * Get address from an EVM signer.
352
+ *
353
+ * @param signer - EVM wallet signer instance
354
+ * @returns The wallet address as a hex string
355
+ */
356
+ declare function getEVMAddress(signer: EVMSigner): string;
357
+ /**
358
+ * Get address from a Solana signer.
359
+ * Supports both wallet adapter (publicKey) and @solana/kit (address) interfaces.
360
+ *
361
+ * @param signer - Solana wallet signer instance
362
+ * @returns The wallet address as a Base58 string
363
+ */
364
+ declare function getSolanaAddress(signer: SolanaSigner): string;
365
+ /**
366
+ * Sign a message with an EVM wallet.
367
+ * Returns hex-encoded signature.
368
+ *
369
+ * @param message - The message to sign
370
+ * @param signer - EVM wallet signer instance
371
+ * @returns Hex-encoded signature
372
+ */
373
+ declare function signEVMMessage(message: string, signer: EVMSigner): Promise<string>;
374
+ /**
375
+ * Sign a message with a Solana wallet.
376
+ * Returns Base58-encoded signature.
377
+ * Supports both wallet adapter (signMessage) and @solana/kit (signMessages) interfaces.
378
+ *
379
+ * @param message - The message to sign
380
+ * @param signer - Solana wallet signer instance
381
+ * @returns Base58-encoded signature
382
+ */
383
+ declare function signSolanaMessage(message: string, signer: SolanaSigner): Promise<string>;
384
+
385
+ /**
386
+ * Complete client flow for SIWX extension
387
+ *
388
+ * Combines message construction, signing, and payload creation.
389
+ * Supports both EVM and Solana wallets.
390
+ */
391
+
392
+ /**
393
+ * Complete SIWX info with chain-specific fields.
394
+ * Used by utility functions that need the selected chain information.
395
+ */
396
+ type CompleteSIWxInfo = SIWxExtensionInfo & {
397
+ chainId: string;
398
+ type: SignatureType;
399
+ signatureScheme?: SignatureScheme;
400
+ };
401
+ /**
402
+ * Create a complete SIWX payload from server extension info with selected chain.
403
+ *
404
+ * Routes to EVM or Solana signing based on the chainId prefix:
405
+ * - `eip155:*` → EVM signing
406
+ * - `solana:*` → Solana signing
407
+ *
408
+ * @param serverExtension - Server extension info with chain selected (includes chainId, type)
409
+ * @param signer - Wallet that can sign messages (EVMSigner or SolanaSigner)
410
+ * @returns Complete SIWX payload with signature
411
+ *
412
+ * @example
413
+ * ```typescript
414
+ * // EVM wallet
415
+ * const completeInfo = { ...extension.info, chainId: "eip155:8453", type: "eip191" };
416
+ * const payload = await createSIWxPayload(completeInfo, evmWallet);
417
+ * ```
418
+ */
419
+ declare function createSIWxPayload(serverExtension: CompleteSIWxInfo, signer: SIWxSigner): Promise<SIWxPayload>;
420
+
421
+ /**
422
+ * Solana Sign-In-With-X (SIWS) support
423
+ *
424
+ * Implements CAIP-122 compliant message format and Ed25519 signature verification
425
+ * for Solana wallets.
426
+ */
427
+
428
+ /**
429
+ * Common Solana network CAIP-2 identifiers.
430
+ * Uses genesis hash as the chain reference per CAIP-30.
431
+ */
432
+ declare const SOLANA_MAINNET = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp";
433
+ declare const SOLANA_DEVNET = "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1";
434
+ declare const SOLANA_TESTNET = "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z";
435
+ /**
436
+ * Extract chain reference from CAIP-2 Solana chainId.
437
+ *
438
+ * @param chainId - CAIP-2 format chain ID (e.g., "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp")
439
+ * @returns Chain reference (genesis hash)
440
+ *
441
+ * @example
442
+ * ```typescript
443
+ * extractSolanaChainReference("solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp") // "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"
444
+ * ```
445
+ */
446
+ declare function extractSolanaChainReference(chainId: string): string;
447
+ /**
448
+ * Format SIWS message following CAIP-122 ABNF specification.
449
+ *
450
+ * The message format is identical to SIWE (EIP-4361) but uses "Solana account"
451
+ * instead of "Ethereum account" in the header line.
452
+ *
453
+ * @param info - Server-provided extension info
454
+ * @param address - Client's Solana wallet address (Base58 encoded public key)
455
+ * @returns Message string ready for signing
456
+ *
457
+ * @example
458
+ * ```typescript
459
+ * const message = formatSIWSMessage(serverInfo, "BSmWDgE9ex6dZYbiTsJGcwMEgFp8q4aWh92hdErQPeVW");
460
+ * // Returns:
461
+ * // "api.example.com wants you to sign in with your Solana account:
462
+ * // BSmWDgE9ex6dZYbiTsJGcwMEgFp8q4aWh92hdErQPeVW
463
+ * //
464
+ * // Sign in to access your content
465
+ * //
466
+ * // URI: https://api.example.com/data
467
+ * // Version: 1
468
+ * // Chain ID: mainnet
469
+ * // Nonce: abc123
470
+ * // Issued At: 2024-01-01T00:00:00.000Z"
471
+ * ```
472
+ */
473
+ declare function formatSIWSMessage(info: CompleteSIWxInfo, address: string): string;
474
+ /**
475
+ * Verify Ed25519 signature for SIWS.
476
+ *
477
+ * @param message - The SIWS message that was signed
478
+ * @param signature - Ed25519 signature bytes
479
+ * @param publicKey - Solana public key bytes (32 bytes)
480
+ * @returns true if signature is valid
481
+ *
482
+ * @example
483
+ * ```typescript
484
+ * const messageBytes = new TextEncoder().encode(message);
485
+ * const valid = verifySolanaSignature(message, signatureBytes, publicKeyBytes);
486
+ * ```
487
+ */
488
+ declare function verifySolanaSignature(message: string, signature: Uint8Array, publicKey: Uint8Array): boolean;
489
+ /**
490
+ * Decode Base58 string to bytes.
491
+ *
492
+ * Solana uses Base58 encoding (Bitcoin alphabet) for addresses and signatures.
493
+ *
494
+ * @param encoded - Base58 encoded string
495
+ * @returns Decoded bytes
496
+ * @throws Error if string contains invalid Base58 characters
497
+ *
498
+ * @example
499
+ * ```typescript
500
+ * const publicKeyBytes = decodeBase58("BSmWDgE9ex6dZYbiTsJGcwMEgFp8q4aWh92hdErQPeVW");
501
+ * // Returns Uint8Array of 32 bytes
502
+ * ```
503
+ */
504
+ declare function decodeBase58(encoded: string): Uint8Array;
505
+ /**
506
+ * Encode bytes to Base58 string.
507
+ *
508
+ * @param bytes - Bytes to encode
509
+ * @returns Base58 encoded string
510
+ */
511
+ declare function encodeBase58(bytes: Uint8Array): string;
512
+ /**
513
+ * Detect if a signer is Solana-compatible.
514
+ * Checks for Solana-specific properties that don't exist on EVM signers.
515
+ *
516
+ * @param signer - The signer to check
517
+ * @returns true if the signer is a Solana signer
518
+ */
519
+ declare function isSolanaSigner(signer: SIWxSigner): boolean;
520
+
521
+ /**
522
+ * Server-side declaration helper for SIWX extension
523
+ *
524
+ * Helps servers declare SIWX authentication requirements in PaymentRequired responses.
525
+ */
526
+
527
+ /**
528
+ * Internal type for SIWX declaration with stored options.
529
+ * The _options field is used by enrichPaymentRequiredResponse to derive
530
+ * values from request context.
531
+ */
532
+ interface SIWxDeclaration extends SIWxExtension {
533
+ _options: DeclareSIWxOptions;
534
+ }
535
+ /**
536
+ * Create SIWX extension declaration for PaymentRequired.extensions
537
+ *
538
+ * Most fields are derived automatically from request context when using
539
+ * siwxResourceServerExtension:
540
+ * - `network`: From payment requirements (accepts[].network)
541
+ * - `resourceUri`: From request URL
542
+ * - `domain`: Parsed from resourceUri
543
+ *
544
+ * Explicit values in options override automatic derivation.
545
+ *
546
+ * @param options - Configuration options (most are optional)
547
+ * @returns Extension object ready for PaymentRequired.extensions
548
+ *
549
+ * @example
550
+ * ```typescript
551
+ * // Minimal - derives network, domain, resourceUri from context
552
+ * const extensions = declareSIWxExtension({
553
+ * statement: 'Sign in to access your purchased content',
554
+ * });
555
+ *
556
+ * // With explicit network (overrides accepts)
557
+ * const extensions = declareSIWxExtension({
558
+ * network: 'eip155:8453',
559
+ * statement: 'Sign in to access',
560
+ * });
561
+ *
562
+ * // Full explicit config (no derivation)
563
+ * const extensions = declareSIWxExtension({
564
+ * domain: 'api.example.com',
565
+ * resourceUri: 'https://api.example.com/data',
566
+ * network: ['eip155:8453', 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'],
567
+ * statement: 'Sign in to access',
568
+ * expirationSeconds: 300,
569
+ * });
570
+ * ```
571
+ */
572
+ declare function declareSIWxExtension(options?: DeclareSIWxOptions): Record<string, SIWxDeclaration>;
573
+
574
+ /**
575
+ * Server-side ResourceServerExtension for SIWX
576
+ *
577
+ * Provides enrichPaymentRequiredResponse hook to:
578
+ * - Derive missing fields from request context (network, resourceUri, domain)
579
+ * - Refresh time-based fields per request (nonce, issuedAt, expirationTime)
580
+ */
581
+
582
+ /**
583
+ * SIWX Resource Server Extension.
584
+ *
585
+ * Implements enrichPaymentRequiredResponse hook to:
586
+ * 1. Derive missing fields from context (network from requirements, URL from resourceInfo)
587
+ * 2. Refresh time-based fields (nonce, issuedAt, expirationTime) per request
588
+ *
589
+ * @example
590
+ * ```typescript
591
+ * import { siwxResourceServerExtension } from "@x402/extensions/sign-in-with-x";
592
+ *
593
+ * const resourceServer = new x402ResourceServer(facilitator)
594
+ * .registerExtension(siwxResourceServerExtension)
595
+ * .onAfterSettle(createSIWxSettleHook({ storage }));
596
+ * ```
597
+ */
598
+ declare const siwxResourceServerExtension: ResourceServerExtension;
599
+
600
+ /**
601
+ * Header parsing for SIWX extension
602
+ *
603
+ * Parses the SIGN-IN-WITH-X header from client requests.
604
+ * Requires base64-encoded JSON per x402 v2 spec.
605
+ */
606
+
607
+ /**
608
+ * Parse SIGN-IN-WITH-X header into structured payload.
609
+ *
610
+ * Expects base64-encoded JSON per x402 v2 spec (CHANGELOG-v2.md line 335).
611
+ *
612
+ * @param header - The SIGN-IN-WITH-X header value (base64-encoded JSON)
613
+ * @returns Parsed SIWX payload
614
+ * @throws Error if header is invalid or missing required fields
615
+ *
616
+ * @example
617
+ * ```typescript
618
+ * const header = request.headers.get('SIGN-IN-WITH-X');
619
+ * if (header) {
620
+ * const payload = parseSIWxHeader(header);
621
+ * // payload.address, payload.signature, etc.
622
+ * }
623
+ * ```
624
+ */
625
+ declare function parseSIWxHeader(header: string): SIWxPayload;
626
+
627
+ /**
628
+ * Message validation for SIWX extension
629
+ *
630
+ * Validates SIWX payload fields before cryptographic verification.
631
+ * Per CHANGELOG-v2.md validation rules (lines 318-329).
632
+ */
633
+
634
+ /**
635
+ * Validate SIWX message fields.
636
+ *
637
+ * Performs validation per spec (CHANGELOG-v2.md lines 318-329):
638
+ * - Domain binding: domain MUST match server's domain
639
+ * - URI validation: uri must refer to base url of resource
640
+ * - Temporal validation:
641
+ * - issuedAt MUST be recent (< 5 minutes by default)
642
+ * - expirationTime MUST be in the future
643
+ * - notBefore (if present) MUST be in the past
644
+ * - Nonce: MUST be unique (via optional checkNonce callback)
645
+ *
646
+ * @param message - The SIWX payload to validate
647
+ * @param expectedResourceUri - Expected resource URI (for domain/URI matching)
648
+ * @param options - Validation options
649
+ * @returns Validation result
650
+ *
651
+ * @example
652
+ * ```typescript
653
+ * const payload = parseSIWxHeader(header);
654
+ * const result = await validateSIWxMessage(
655
+ * payload,
656
+ * 'https://api.example.com/data',
657
+ * { checkNonce: (n) => !usedNonces.has(n) }
658
+ * );
659
+ *
660
+ * if (!result.valid) {
661
+ * return { error: result.error };
662
+ * }
663
+ * ```
664
+ */
665
+ declare function validateSIWxMessage(message: SIWxPayload, expectedResourceUri: string, options?: SIWxValidationOptions): Promise<SIWxValidationResult>;
666
+
667
+ /**
668
+ * Signature verification for SIWX extension
669
+ *
670
+ * Routes to chain-specific verification based on chainId namespace:
671
+ * - EVM (eip155:*): EOA by default, smart wallet (EIP-1271/EIP-6492) with verifier
672
+ * - Solana (solana:*): Ed25519 signature verification via tweetnacl
673
+ */
674
+
675
+ /**
676
+ * Verify SIWX signature cryptographically.
677
+ *
678
+ * Routes to the appropriate chain-specific verification based on the
679
+ * chainId namespace prefix:
680
+ * - `eip155:*` → EVM verification (EOA by default, smart wallet with verifier)
681
+ * - `solana:*` → Ed25519 signature verification
682
+ *
683
+ * @param payload - The SIWX payload containing signature
684
+ * @param options - Optional verification options
685
+ * @returns Verification result with recovered address if valid
686
+ *
687
+ * @example
688
+ * ```typescript
689
+ * // EOA-only verification (default)
690
+ * const result = await verifySIWxSignature(payload);
691
+ *
692
+ * // Smart wallet verification
693
+ * import { createPublicClient, http } from 'viem';
694
+ * import { base } from 'viem/chains';
695
+ *
696
+ * const publicClient = createPublicClient({ chain: base, transport: http() });
697
+ * const result = await verifySIWxSignature(payload, {
698
+ * evmVerifier: publicClient.verifyMessage,
699
+ * });
700
+ *
701
+ * if (result.valid) {
702
+ * console.log('Verified wallet:', result.address);
703
+ * } else {
704
+ * console.error('Verification failed:', result.error);
705
+ * }
706
+ * ```
707
+ */
708
+ declare function verifySIWxSignature(payload: SIWxPayload, options?: SIWxVerifyOptions): Promise<SIWxVerifyResult>;
709
+
710
+ /**
711
+ * JSON Schema builder for SIWX extension
712
+ *
713
+ * Per CHANGELOG-v2.md lines 276-292
714
+ */
715
+
716
+ /**
717
+ * Build JSON Schema for SIWX extension validation.
718
+ * This schema validates the client proof payload structure.
719
+ *
720
+ * @returns JSON Schema for validating SIWX client payloads
721
+ */
722
+ declare function buildSIWxSchema(): SIWxExtensionSchema;
723
+
724
+ /**
725
+ * CAIP-122 message construction for SIWX extension
726
+ *
727
+ * Constructs the canonical message string for signing.
728
+ * Routes to chain-specific formatters based on chainId namespace.
729
+ */
730
+
731
+ /**
732
+ * Construct CAIP-122 compliant message string for signing.
733
+ *
734
+ * Routes to the appropriate chain-specific message formatter based on the
735
+ * chainId namespace prefix:
736
+ * - `eip155:*` → SIWE (EIP-4361) format via siwe library
737
+ * - `solana:*` → SIWS format
738
+ *
739
+ * @param serverInfo - Server extension info with chain selected (includes chainId)
740
+ * @param address - Client wallet address
741
+ * @returns Message string ready for signing
742
+ * @throws Error if chainId namespace is not supported
743
+ *
744
+ * @example
745
+ * ```typescript
746
+ * // EVM (Ethereum, Base, etc.)
747
+ * const completeInfo = { ...extension.info, chainId: "eip155:8453", type: "eip191" };
748
+ * const evmMessage = createSIWxMessage(completeInfo, "0x1234...");
749
+ * ```
750
+ */
751
+ declare function createSIWxMessage(serverInfo: CompleteSIWxInfo, address: string): string;
752
+
753
+ /**
754
+ * Header encoding for SIWX extension
755
+ *
756
+ * Encodes SIWX payload for the SIGN-IN-WITH-X HTTP header.
757
+ * Per CHANGELOG-v2.md line 335: header should be base64-encoded.
758
+ */
759
+
760
+ /**
761
+ * Encode SIWX payload for SIGN-IN-WITH-X header.
762
+ *
763
+ * Uses base64 encoding per x402 v2 spec (CHANGELOG-v2.md line 335).
764
+ *
765
+ * @param payload - Complete SIWX payload with signature
766
+ * @returns Base64-encoded JSON string
767
+ *
768
+ * @example
769
+ * ```typescript
770
+ * const payload = await createSIWxPayload(serverInfo, signer);
771
+ * const header = encodeSIWxHeader(payload);
772
+ *
773
+ * fetch(url, {
774
+ * headers: { 'SIGN-IN-WITH-X': header }
775
+ * });
776
+ * ```
777
+ */
778
+ declare function encodeSIWxHeader(payload: SIWxPayload): string;
779
+
780
+ /**
781
+ * Fetch wrapper for SIWX authentication.
782
+ *
783
+ * Provides a convenient wrapper around fetch that automatically handles
784
+ * SIWX authentication when a 402 response includes SIWX extension info.
785
+ */
786
+
787
+ /**
788
+ * Wraps fetch to automatically handle SIWX authentication.
789
+ *
790
+ * When a 402 response is received with a SIWX extension:
791
+ * 1. Extracts SIWX info from PAYMENT-REQUIRED header
792
+ * 2. Creates signed SIWX proof using the provided signer
793
+ * 3. Retries the request with the SIWX header
794
+ *
795
+ * If the 402 response doesn't include SIWX extension info, the original
796
+ * response is returned unchanged (allowing payment handling to proceed).
797
+ *
798
+ * @param fetch - The fetch function to wrap (typically globalThis.fetch)
799
+ * @param signer - Wallet signer (EVMSigner or SolanaSigner)
800
+ * @returns A wrapped fetch function that handles SIWX authentication
801
+ *
802
+ * @example
803
+ * ```typescript
804
+ * import { wrapFetchWithSIWx } from '@x402/extensions/sign-in-with-x';
805
+ * import { privateKeyToAccount } from 'viem/accounts';
806
+ *
807
+ * const signer = privateKeyToAccount(privateKey);
808
+ * const fetchWithSIWx = wrapFetchWithSIWx(fetch, signer);
809
+ *
810
+ * // Request that may require SIWX auth (for returning paid users)
811
+ * const response = await fetchWithSIWx('https://api.example.com/data');
812
+ * ```
813
+ */
814
+ declare function wrapFetchWithSIWx(fetch: typeof globalThis.fetch, signer: SIWxSigner): (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
815
+
816
+ /**
817
+ * EVM Sign-In-With-Ethereum (SIWE) support
818
+ *
819
+ * Implements EIP-4361 compliant message format and signature verification
820
+ * for EVM chains (Ethereum, Base, Polygon, etc.)
821
+ */
822
+
823
+ /**
824
+ * Extract numeric chain ID from CAIP-2 EVM chainId.
825
+ *
826
+ * @param chainId - CAIP-2 format chain ID (e.g., "eip155:8453")
827
+ * @returns Numeric chain ID (e.g., 8453)
828
+ * @throws Error if chainId format is invalid
829
+ *
830
+ * @example
831
+ * ```typescript
832
+ * extractEVMChainId("eip155:1") // 1 (Ethereum mainnet)
833
+ * extractEVMChainId("eip155:8453") // 8453 (Base)
834
+ * extractEVMChainId("eip155:137") // 137 (Polygon)
835
+ * ```
836
+ */
837
+ declare function extractEVMChainId(chainId: string): number;
838
+ /**
839
+ * Format SIWE message following EIP-4361 specification.
840
+ *
841
+ * Uses the siwe library to ensure message format matches verification.
842
+ *
843
+ * @param info - Server-provided extension info
844
+ * @param address - Client's EVM wallet address (0x-prefixed)
845
+ * @returns Message string ready for signing
846
+ *
847
+ * @example
848
+ * ```typescript
849
+ * const message = formatSIWEMessage(serverInfo, "0x1234...abcd");
850
+ * // Returns EIP-4361 formatted message:
851
+ * // "api.example.com wants you to sign in with your Ethereum account:
852
+ * // 0x1234...abcd
853
+ * //
854
+ * // Sign in to access your content
855
+ * //
856
+ * // URI: https://api.example.com/data
857
+ * // Version: 1
858
+ * // Chain ID: 8453
859
+ * // Nonce: abc123
860
+ * // Issued At: 2024-01-01T00:00:00.000Z"
861
+ * ```
862
+ */
863
+ declare function formatSIWEMessage(info: CompleteSIWxInfo, address: string): string;
864
+ /**
865
+ * Verify EVM signature.
866
+ *
867
+ * Supports:
868
+ * - EOA signatures (standard ECDSA via EIP-191) - always available
869
+ * - EIP-1271 (deployed smart contract wallets) - requires verifier
870
+ * - EIP-6492 (counterfactual/pre-deploy smart wallets) - requires verifier
871
+ *
872
+ * @param message - The SIWE message that was signed
873
+ * @param address - The claimed signer address
874
+ * @param signature - The signature to verify
875
+ * @param verifier - Optional message verifier for smart wallet support.
876
+ * Pass publicClient.verifyMessage for EIP-1271/EIP-6492 support.
877
+ * Without this, only EOA signatures are verified.
878
+ * @returns true if signature is valid
879
+ *
880
+ * @example
881
+ * ```typescript
882
+ * // EOA-only verification (default, no RPC required)
883
+ * const valid = await verifyEVMSignature(message, address, signature);
884
+ *
885
+ * // Smart wallet verification with viem PublicClient
886
+ * import { createPublicClient, http } from 'viem';
887
+ * import { base } from 'viem/chains';
888
+ *
889
+ * const publicClient = createPublicClient({ chain: base, transport: http() });
890
+ * const valid = await verifyEVMSignature(
891
+ * message,
892
+ * address,
893
+ * signature,
894
+ * publicClient.verifyMessage
895
+ * );
896
+ * ```
897
+ */
898
+ declare function verifyEVMSignature(message: string, address: string, signature: string, verifier?: EVMMessageVerifier): Promise<boolean>;
899
+ /**
900
+ * Detect if a signer is EVM-compatible.
901
+ * Checks for EVM-specific properties.
902
+ *
903
+ * @param signer - The signer to check
904
+ * @returns true if the signer is an EVM signer
905
+ */
906
+ declare function isEVMSigner(signer: SIWxSigner): boolean;
907
+
908
+ /**
909
+ * Storage interface for SIWX payment tracking.
910
+ *
911
+ * Implementations track which addresses have paid for which resources,
912
+ * enabling SIWX authentication to grant access without re-payment.
913
+ *
914
+ * Optionally supports nonce tracking to prevent signature replay attacks.
915
+ */
916
+ interface SIWxStorage {
917
+ /**
918
+ * Check if an address has paid for a resource.
919
+ *
920
+ * @param resource - The resource path (e.g., "/weather")
921
+ * @param address - The wallet address to check
922
+ * @returns True if the address has paid for the resource
923
+ */
924
+ hasPaid(resource: string, address: string): boolean | Promise<boolean>;
925
+ /**
926
+ * Record that an address has paid for a resource.
927
+ *
928
+ * @param resource - The resource path
929
+ * @param address - The wallet address that paid
930
+ */
931
+ recordPayment(resource: string, address: string): void | Promise<void>;
932
+ /**
933
+ * Check if a nonce has already been used (optional).
934
+ *
935
+ * Implementing this method prevents signature replay attacks where
936
+ * an intercepted SIWX header could be reused by an attacker.
937
+ *
938
+ * @param nonce - The nonce from the SIWX payload
939
+ * @returns True if the nonce has been used
940
+ */
941
+ hasUsedNonce?(nonce: string): boolean | Promise<boolean>;
942
+ /**
943
+ * Record that a nonce has been used (optional).
944
+ *
945
+ * Called after successfully granting access via SIWX.
946
+ * Implementations should consider adding expiration to avoid unbounded growth.
947
+ *
948
+ * @param nonce - The nonce to record as used
949
+ */
950
+ recordNonce?(nonce: string): void | Promise<void>;
951
+ }
952
+ /**
953
+ * In-memory implementation of SIWxStorage.
954
+ *
955
+ * Suitable for development and single-instance deployments.
956
+ * For production multi-instance deployments, use a persistent storage implementation.
957
+ */
958
+ declare class InMemorySIWxStorage implements SIWxStorage {
959
+ private paidAddresses;
960
+ /**
961
+ * Check if an address has paid for a resource.
962
+ *
963
+ * @param resource - The resource path
964
+ * @param address - The wallet address to check
965
+ * @returns True if the address has paid
966
+ */
967
+ hasPaid(resource: string, address: string): boolean;
968
+ /**
969
+ * Record that an address has paid for a resource.
970
+ *
971
+ * @param resource - The resource path
972
+ * @param address - The wallet address that paid
973
+ */
974
+ recordPayment(resource: string, address: string): void;
975
+ }
976
+
977
+ /**
978
+ * SIWX Lifecycle Hooks
979
+ *
980
+ * Pre-built hooks for integrating SIWX authentication with x402 servers and clients.
981
+ */
982
+
983
+ /**
984
+ * Options for creating server-side SIWX hooks.
985
+ */
986
+ interface CreateSIWxHookOptions {
987
+ /** Storage for tracking paid addresses */
988
+ storage: SIWxStorage;
989
+ /** Options for signature verification (e.g., EVM smart wallet support) */
990
+ verifyOptions?: SIWxVerifyOptions;
991
+ /** Optional callback for logging/debugging */
992
+ onEvent?: (event: SIWxHookEvent) => void;
993
+ }
994
+ /**
995
+ * Events emitted by SIWX hooks for logging/debugging.
996
+ */
997
+ type SIWxHookEvent = {
998
+ type: "payment_recorded";
999
+ resource: string;
1000
+ address: string;
1001
+ } | {
1002
+ type: "access_granted";
1003
+ resource: string;
1004
+ address: string;
1005
+ } | {
1006
+ type: "validation_failed";
1007
+ resource: string;
1008
+ error?: string;
1009
+ } | {
1010
+ type: "nonce_reused";
1011
+ resource: string;
1012
+ nonce: string;
1013
+ } | {
1014
+ type: "siwx_header_sent";
1015
+ resource: string;
1016
+ };
1017
+ /**
1018
+ * Creates an onAfterSettle hook that records payments for SIWX.
1019
+ *
1020
+ * @param options - Hook configuration
1021
+ * @returns Hook function for x402ResourceServer.onAfterSettle()
1022
+ *
1023
+ * @example
1024
+ * ```typescript
1025
+ * const storage = new InMemorySIWxStorage();
1026
+ * const resourceServer = new x402ResourceServer(facilitator)
1027
+ * .onAfterSettle(createSIWxSettleHook({ storage }));
1028
+ * ```
1029
+ */
1030
+ declare function createSIWxSettleHook(options: CreateSIWxHookOptions): (ctx: {
1031
+ paymentPayload: {
1032
+ payload: unknown;
1033
+ resource: {
1034
+ url: string;
1035
+ };
1036
+ };
1037
+ result: {
1038
+ success: boolean;
1039
+ payer?: string;
1040
+ };
1041
+ }) => Promise<void>;
1042
+ /**
1043
+ * Creates an onProtectedRequest hook that validates SIWX auth before payment.
1044
+ *
1045
+ * @param options - Hook configuration
1046
+ * @returns Hook function for x402HTTPResourceServer.onProtectedRequest()
1047
+ *
1048
+ * @example
1049
+ * ```typescript
1050
+ * const storage = new InMemorySIWxStorage();
1051
+ * const httpServer = new x402HTTPResourceServer(resourceServer, routes)
1052
+ * .onProtectedRequest(createSIWxRequestHook({ storage }));
1053
+ * ```
1054
+ */
1055
+ declare function createSIWxRequestHook(options: CreateSIWxHookOptions): (context: {
1056
+ adapter: {
1057
+ getHeader(name: string): string | undefined;
1058
+ getUrl(): string;
1059
+ };
1060
+ path: string;
1061
+ }) => Promise<void | {
1062
+ grantAccess: true;
1063
+ }>;
1064
+ /**
1065
+ * Creates an onPaymentRequired hook for client-side SIWX authentication.
1066
+ *
1067
+ * Matches the signer type to a compatible chain in supportedChains.
1068
+ * For EVM signers: matches any eip191 chain
1069
+ * For Solana signers: matches any ed25519 chain
1070
+ *
1071
+ * @param signer - Wallet signer for creating SIWX proofs
1072
+ * @returns Hook function for x402HTTPClient.onPaymentRequired()
1073
+ *
1074
+ * @example
1075
+ * ```typescript
1076
+ * const httpClient = new x402HTTPClient(client)
1077
+ * .onPaymentRequired(createSIWxClientHook(signer));
1078
+ * ```
1079
+ */
1080
+ declare function createSIWxClientHook(signer: SIWxSigner): (context: {
1081
+ paymentRequired: {
1082
+ accepts?: Array<{
1083
+ network: string;
1084
+ }>;
1085
+ extensions?: Record<string, unknown>;
1086
+ };
1087
+ }) => Promise<{
1088
+ headers: Record<string, string>;
1089
+ } | void>;
1090
+
1091
+ 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, isEVMSigner, isSolanaSigner, parseSIWxHeader, signEVMMessage, signSolanaMessage, siwxResourceServerExtension, validateSIWxMessage, verifyEVMSignature, verifySIWxSignature, verifySolanaSignature, wrapFetchWithSIWx };