@t402/core 2.3.0 → 2.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.
Files changed (48) hide show
  1. package/dist/cjs/client/index.d.ts +3 -2
  2. package/dist/cjs/client/index.js +70 -2
  3. package/dist/cjs/client/index.js.map +1 -1
  4. package/dist/cjs/facilitator/index.d.ts +2 -1
  5. package/dist/cjs/http/index.d.ts +13 -6
  6. package/dist/cjs/http/index.js +73 -3
  7. package/dist/cjs/http/index.js.map +1 -1
  8. package/dist/cjs/mechanisms-C7yK91d4.d.ts +1126 -0
  9. package/dist/cjs/server/index.d.ts +3 -2
  10. package/dist/cjs/server/index.js +67 -1
  11. package/dist/cjs/server/index.js.map +1 -1
  12. package/dist/cjs/{t402HTTPClient-GIweW6nh.d.ts → t402HTTPClient-DmkFydNG.d.ts} +1 -1
  13. package/dist/cjs/{t402HTTPResourceServer-CcpZF3af.d.ts → t402HTTPResourceServer-CybruqCk.d.ts} +1 -1
  14. package/dist/cjs/types/index.d.ts +2 -1
  15. package/dist/cjs/types/index.js +469 -2
  16. package/dist/cjs/types/index.js.map +1 -1
  17. package/dist/cjs/types/v1/index.d.ts +2 -1
  18. package/dist/cjs/utils/index.d.ts +29 -2
  19. package/dist/cjs/utils/index.js +53 -0
  20. package/dist/cjs/utils/index.js.map +1 -1
  21. package/dist/esm/chunk-KPNYZYDS.mjs +100 -0
  22. package/dist/esm/chunk-KPNYZYDS.mjs.map +1 -0
  23. package/dist/esm/{chunk-3IUBYRYG.mjs → chunk-LJ4M5Z5U.mjs} +51 -1
  24. package/dist/esm/chunk-LJ4M5Z5U.mjs.map +1 -0
  25. package/dist/esm/{chunk-773TNE2Y.mjs → chunk-REMGOG6C.mjs} +16 -5
  26. package/dist/esm/{chunk-773TNE2Y.mjs.map → chunk-REMGOG6C.mjs.map} +1 -1
  27. package/dist/esm/client/index.d.mts +3 -2
  28. package/dist/esm/client/index.mjs +3 -2
  29. package/dist/esm/client/index.mjs.map +1 -1
  30. package/dist/esm/facilitator/index.d.mts +2 -1
  31. package/dist/esm/http/index.d.mts +13 -6
  32. package/dist/esm/http/index.mjs +3 -2
  33. package/dist/esm/mechanisms-C7yK91d4.d.mts +1126 -0
  34. package/dist/esm/server/index.d.mts +3 -2
  35. package/dist/esm/server/index.mjs +3 -2
  36. package/dist/esm/server/index.mjs.map +1 -1
  37. package/dist/esm/{t402HTTPClient-DbZqiMSO.d.mts → t402HTTPClient-Bgjn3TRU.d.mts} +1 -1
  38. package/dist/esm/{t402HTTPResourceServer-BbQ6HVBC.d.mts → t402HTTPResourceServer-DuZIzhRI.d.mts} +1 -1
  39. package/dist/esm/types/index.d.mts +2 -1
  40. package/dist/esm/types/index.mjs +304 -1
  41. package/dist/esm/types/index.mjs.map +1 -1
  42. package/dist/esm/types/v1/index.d.mts +2 -1
  43. package/dist/esm/utils/index.d.mts +29 -2
  44. package/dist/esm/utils/index.mjs +7 -1
  45. package/package.json +3 -3
  46. package/dist/cjs/mechanisms-B-vz5yOj.d.ts +0 -443
  47. package/dist/esm/chunk-3IUBYRYG.mjs.map +0 -1
  48. package/dist/esm/mechanisms-B-vz5yOj.d.mts +0 -443
@@ -1,443 +0,0 @@
1
- type PaymentRequirementsV1 = {
2
- scheme: string;
3
- network: Network;
4
- maxAmountRequired: string;
5
- resource: string;
6
- description: string;
7
- mimeType: string;
8
- outputSchema: Record<string, unknown>;
9
- payTo: string;
10
- maxTimeoutSeconds: number;
11
- asset: string;
12
- extra: Record<string, unknown>;
13
- };
14
- type PaymentRequiredV1 = {
15
- t402Version: 1;
16
- error?: string;
17
- accepts: PaymentRequirementsV1[];
18
- };
19
- type PaymentPayloadV1 = {
20
- t402Version: 1;
21
- scheme: string;
22
- network: Network;
23
- payload: Record<string, unknown>;
24
- };
25
- type VerifyRequestV1 = {
26
- paymentPayload: PaymentPayloadV1;
27
- paymentRequirements: PaymentRequirementsV1;
28
- };
29
- type SettleRequestV1 = {
30
- paymentPayload: PaymentPayloadV1;
31
- paymentRequirements: PaymentRequirementsV1;
32
- };
33
- type SettleResponseV1 = {
34
- success: boolean;
35
- errorReason?: string;
36
- payer?: string;
37
- transaction: string;
38
- network: Network;
39
- };
40
- type SupportedResponseV1 = {
41
- kinds: {
42
- t402Version: number;
43
- scheme: string;
44
- network: Network;
45
- extra?: Record<string, unknown>;
46
- }[];
47
- };
48
-
49
- interface ResourceServerExtension {
50
- key: string;
51
- enrichDeclaration?: (declaration: unknown, transportContext: unknown) => unknown;
52
- }
53
-
54
- /**
55
- * Up-To Scheme Types
56
- *
57
- * The `upto` scheme authorizes transfer of up to a maximum amount,
58
- * enabling usage-based billing where the final settlement amount
59
- * is determined by actual usage.
60
- *
61
- * @example
62
- * ```typescript
63
- * // Client authorizes up to $1.00
64
- * const requirements: UptoPaymentRequirements = {
65
- * scheme: 'upto',
66
- * network: 'eip155:8453',
67
- * maxAmount: '1000000', // $1.00 in USDC
68
- * minAmount: '10000', // $0.01 minimum
69
- * asset: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
70
- * payTo: '0x...',
71
- * maxTimeoutSeconds: 300,
72
- * extra: {
73
- * unit: 'token',
74
- * unitPrice: '100',
75
- * },
76
- * };
77
- *
78
- * // Server settles for actual usage ($0.15)
79
- * const settlement: UptoSettlement = {
80
- * settleAmount: '150000',
81
- * usageDetails: {
82
- * tokensGenerated: 1500,
83
- * unitPrice: '100',
84
- * },
85
- * };
86
- * ```
87
- */
88
-
89
- /**
90
- * Extended payment requirements for the upto scheme.
91
- */
92
- interface UptoPaymentRequirements extends Omit<PaymentRequirements, "scheme" | "amount"> {
93
- /** Scheme identifier - always 'upto' */
94
- scheme: "upto";
95
- /** Network identifier (CAIP-2 format) */
96
- network: Network;
97
- /** Maximum amount the client authorizes (in smallest denomination) */
98
- maxAmount: string;
99
- /** Minimum settlement amount (prevents dust payments) */
100
- minAmount?: string;
101
- /** Asset contract address or identifier */
102
- asset: string;
103
- /** Recipient address */
104
- payTo: string;
105
- /** Maximum time in seconds before payment expires */
106
- maxTimeoutSeconds: number;
107
- /** Additional scheme-specific data */
108
- extra: UptoExtra;
109
- }
110
- /**
111
- * Extra fields specific to the upto scheme.
112
- */
113
- interface UptoExtra extends Record<string, unknown> {
114
- /** Billing unit (e.g., 'token', 'request', 'second', 'byte') */
115
- unit?: string;
116
- /** Price per unit in smallest denomination */
117
- unitPrice?: string;
118
- /** EIP-712 domain name (for EVM) */
119
- name?: string;
120
- /** EIP-712 domain version (for EVM) */
121
- version?: string;
122
- /** Router contract address (for EVM) */
123
- routerAddress?: string;
124
- }
125
- /**
126
- * Base payload structure for upto scheme.
127
- */
128
- interface UptoPayloadBase {
129
- /** Unique nonce to prevent replay attacks */
130
- nonce: string;
131
- }
132
- /**
133
- * EVM-specific upto payload using EIP-2612 Permit.
134
- */
135
- interface UptoEvmPayload extends UptoPayloadBase {
136
- /** EIP-2612 permit signature components */
137
- signature: {
138
- v: number;
139
- r: `0x${string}`;
140
- s: `0x${string}`;
141
- };
142
- /** Permit authorization parameters */
143
- authorization: {
144
- /** Token owner address */
145
- owner: `0x${string}`;
146
- /** Spender address (router contract) */
147
- spender: `0x${string}`;
148
- /** Maximum authorized value */
149
- value: string;
150
- /** Permit deadline (unix timestamp) */
151
- deadline: string;
152
- /** Permit nonce (from token contract) */
153
- nonce: number;
154
- };
155
- }
156
- /**
157
- * Alternative EVM payload with combined signature.
158
- */
159
- interface UptoEvmPayloadCompact extends UptoPayloadBase {
160
- /** Combined EIP-2612 permit signature */
161
- signature: `0x${string}`;
162
- /** Permit authorization parameters */
163
- authorization: {
164
- owner: `0x${string}`;
165
- spender: `0x${string}`;
166
- value: string;
167
- deadline: string;
168
- nonce: number;
169
- };
170
- }
171
- /**
172
- * Settlement request for upto scheme.
173
- */
174
- interface UptoSettlement {
175
- /** Actual amount to settle (must be <= maxAmount) */
176
- settleAmount: string;
177
- /** Optional usage details for auditing */
178
- usageDetails?: UptoUsageDetails;
179
- }
180
- /**
181
- * Usage details for settlement auditing.
182
- */
183
- interface UptoUsageDetails {
184
- /** Number of units consumed */
185
- unitsConsumed?: number;
186
- /** Price per unit used */
187
- unitPrice?: string;
188
- /** Type of unit */
189
- unitType?: string;
190
- /** Start timestamp of usage period */
191
- startTime?: number;
192
- /** End timestamp of usage period */
193
- endTime?: number;
194
- /** Additional metadata */
195
- metadata?: Record<string, unknown>;
196
- }
197
- /**
198
- * Settlement response for upto scheme.
199
- */
200
- interface UptoSettlementResponse {
201
- /** Whether settlement was successful */
202
- success: boolean;
203
- /** Transaction hash (if on-chain) */
204
- transactionHash?: string;
205
- /** Actual amount settled */
206
- settledAmount: string;
207
- /** Maximum amount that was authorized */
208
- maxAmount: string;
209
- /** Block number (if on-chain) */
210
- blockNumber?: number;
211
- /** Gas used (if on-chain) */
212
- gasUsed?: string;
213
- /** Error message if failed */
214
- error?: string;
215
- }
216
- /**
217
- * Validation result for upto payment.
218
- */
219
- interface UptoValidationResult {
220
- /** Whether the payment is valid */
221
- isValid: boolean;
222
- /** Reason if invalid */
223
- invalidReason?: string;
224
- /** Validated maximum amount */
225
- validatedMaxAmount?: string;
226
- /** Payer address */
227
- payer?: string;
228
- /** Expiration timestamp */
229
- expiresAt?: number;
230
- }
231
- /**
232
- * Type guard for UptoPaymentRequirements.
233
- *
234
- * @param requirements - The value to check
235
- * @returns True if the value is UptoPaymentRequirements
236
- */
237
- declare function isUptoPaymentRequirements(requirements: unknown): requirements is UptoPaymentRequirements;
238
- /**
239
- * Type guard for UptoEvmPayload.
240
- *
241
- * @param payload - The value to check
242
- * @returns True if the value is UptoEvmPayload
243
- */
244
- declare function isUptoEvmPayload(payload: unknown): payload is UptoEvmPayload;
245
- /**
246
- * Constants for upto scheme.
247
- */
248
- declare const UPTO_SCHEME: "upto";
249
- declare const UPTO_DEFAULTS: {
250
- /** Default minimum settlement amount (prevents dust) */
251
- readonly MIN_AMOUNT: "1000";
252
- /** Default maximum timeout in seconds (5 minutes) */
253
- readonly MAX_TIMEOUT_SECONDS: 300;
254
- /** Supported billing units */
255
- readonly UNITS: readonly ["token", "request", "second", "minute", "byte", "kb", "mb"];
256
- };
257
- type UptoUnit = (typeof UPTO_DEFAULTS.UNITS)[number];
258
-
259
- type Network = `${string}:${string}`;
260
- type Money = string | number;
261
- type AssetAmount = {
262
- asset: string;
263
- amount: string;
264
- extra?: Record<string, unknown>;
265
- };
266
- type Price = Money | AssetAmount;
267
-
268
- interface ResourceInfo {
269
- url: string;
270
- description?: string;
271
- mimeType?: string;
272
- }
273
- type PaymentRequirements = {
274
- scheme: string;
275
- network: Network;
276
- asset: string;
277
- amount: string;
278
- payTo: string;
279
- maxTimeoutSeconds: number;
280
- extra: Record<string, unknown>;
281
- };
282
- type PaymentRequired = {
283
- t402Version: number;
284
- error?: string;
285
- resource: ResourceInfo;
286
- accepts: PaymentRequirements[];
287
- extensions?: Record<string, unknown>;
288
- };
289
- type PaymentPayload = {
290
- t402Version: number;
291
- resource?: ResourceInfo;
292
- accepted: PaymentRequirements;
293
- payload: Record<string, unknown>;
294
- extensions?: Record<string, unknown>;
295
- };
296
-
297
- type VerifyRequest = {
298
- paymentPayload: PaymentPayload;
299
- paymentRequirements: PaymentRequirements;
300
- };
301
- type VerifyResponse = {
302
- isValid: boolean;
303
- invalidReason?: string;
304
- payer?: string;
305
- };
306
- type SettleRequest = {
307
- paymentPayload: PaymentPayload;
308
- paymentRequirements: PaymentRequirements;
309
- };
310
- type SettleResponse = {
311
- success: boolean;
312
- errorReason?: string;
313
- payer?: string;
314
- transaction: string;
315
- network: Network;
316
- };
317
- type SupportedKind = {
318
- t402Version: number;
319
- scheme: string;
320
- network: Network;
321
- extra?: Record<string, unknown>;
322
- };
323
- type SupportedResponse = {
324
- kinds: SupportedKind[];
325
- extensions: string[];
326
- signers: Record<string, string[]>;
327
- };
328
-
329
- /**
330
- * Money parser function that converts a numeric amount to an AssetAmount
331
- * Receives the amount as a decimal number (e.g., 1.50 for $1.50)
332
- * Returns null to indicate "cannot handle this amount", causing fallback to next parser
333
- * Always returns a Promise for consistency - use async/await
334
- *
335
- * @param amount - The decimal amount (e.g., 1.50)
336
- * @param network - The network identifier for context
337
- * @returns AssetAmount or null to try next parser
338
- */
339
- type MoneyParser = (amount: number, network: Network) => Promise<AssetAmount | null>;
340
- interface SchemeNetworkClient {
341
- readonly scheme: string;
342
- createPaymentPayload(t402Version: number, paymentRequirements: PaymentRequirements): Promise<Pick<PaymentPayload, "t402Version" | "payload">>;
343
- }
344
- interface SchemeNetworkFacilitator {
345
- readonly scheme: string;
346
- /**
347
- * CAIP family pattern that this facilitator supports.
348
- * Used to group signers by blockchain family in the supported response.
349
- *
350
- * @example
351
- * // EVM facilitators
352
- * readonly caipFamily = "eip155:*";
353
- *
354
- * @example
355
- * // SVM facilitators
356
- * readonly caipFamily = "solana:*";
357
- */
358
- readonly caipFamily: string;
359
- /**
360
- * Get mechanism-specific extra data needed for the supported kinds endpoint.
361
- * This method is called when building the facilitator's supported response.
362
- *
363
- * @param network - The network identifier for context
364
- * @returns Extra data object or undefined if no extra data is needed
365
- *
366
- * @example
367
- * // EVM schemes return undefined (no extra data needed)
368
- * getExtra(network: Network): undefined {
369
- * return undefined;
370
- * }
371
- *
372
- * @example
373
- * // SVM schemes return feePayer address
374
- * getExtra(network: Network): Record<string, unknown> | undefined {
375
- * return { feePayer: this.signer.address };
376
- * }
377
- */
378
- getExtra(network: Network): Record<string, unknown> | undefined;
379
- /**
380
- * Get signer addresses used by this facilitator for a given network.
381
- * These are included in the supported response to help clients understand
382
- * which addresses might sign/pay for transactions.
383
- *
384
- * Supports multiple addresses for load balancing, key rotation, and high availability.
385
- *
386
- * @param network - The network identifier
387
- * @returns Array of signer addresses (wallet addresses, fee payer addresses, etc.)
388
- *
389
- * @example
390
- * // EVM facilitator
391
- * getSigners(network: string): string[] {
392
- * return [...this.signer.getAddresses()];
393
- * }
394
- *
395
- * @example
396
- * // SVM facilitator
397
- * getSigners(network: string): string[] {
398
- * return [...this.signer.getAddresses()];
399
- * }
400
- */
401
- getSigners(network: string): string[];
402
- verify(payload: PaymentPayload, requirements: PaymentRequirements): Promise<VerifyResponse>;
403
- settle(payload: PaymentPayload, requirements: PaymentRequirements): Promise<SettleResponse>;
404
- }
405
- interface SchemeNetworkServer {
406
- readonly scheme: string;
407
- /**
408
- * Convert a user-friendly price to the scheme's specific amount and asset format
409
- * Always returns a Promise for consistency
410
- *
411
- * @param price - User-friendly price (e.g., "$0.10", "0.10", { amount: "100000", asset: "USDC" })
412
- * @param network - The network identifier for context
413
- * @returns Promise that resolves to the converted amount, asset identifier, and any extra metadata
414
- *
415
- * @example
416
- * // For EVM networks with USDC:
417
- * await parsePrice("$0.10", "eip155:8453") => { amount: "100000", asset: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913" }
418
- *
419
- * // For custom schemes:
420
- * await parsePrice("10 points", "custom:network") => { amount: "10", asset: "points" }
421
- */
422
- parsePrice(price: Price, network: Network): Promise<AssetAmount>;
423
- /**
424
- * Build payment requirements for this scheme/network combination
425
- *
426
- * @param paymentRequirements - Base payment requirements with amount/asset already set
427
- * @param supportedKind - The supported kind from facilitator's /supported endpoint
428
- * @param supportedKind.t402Version - The t402 version
429
- * @param supportedKind.scheme - The payment scheme
430
- * @param supportedKind.network - The network identifier
431
- * @param supportedKind.extra - Optional extra metadata
432
- * @param facilitatorExtensions - Extensions supported by the facilitator
433
- * @returns Enhanced payment requirements ready to be sent to clients
434
- */
435
- enhancePaymentRequirements(paymentRequirements: PaymentRequirements, supportedKind: {
436
- t402Version: number;
437
- scheme: string;
438
- network: Network;
439
- extra?: Record<string, unknown>;
440
- }, facilitatorExtensions: string[]): Promise<PaymentRequirements>;
441
- }
442
-
443
- export { type AssetAmount as A, type UptoUnit as B, isUptoPaymentRequirements as C, isUptoEvmPayload as D, UPTO_SCHEME as E, UPTO_DEFAULTS as F, type Money as M, type Network as N, type PaymentRequirements as P, type ResourceServerExtension as R, type SettleResponse as S, type UptoPaymentRequirements as U, type VerifyResponse as V, type PaymentPayload as a, type SchemeNetworkFacilitator as b, type PaymentRequired as c, type SchemeNetworkClient as d, type SupportedResponse as e, type SchemeNetworkServer as f, type SupportedKind as g, type Price as h, type PaymentRequirementsV1 as i, type PaymentRequiredV1 as j, type PaymentPayloadV1 as k, type VerifyRequestV1 as l, type SettleRequestV1 as m, type SettleResponseV1 as n, type SupportedResponseV1 as o, type VerifyRequest as p, type SettleRequest as q, type MoneyParser as r, type UptoExtra as s, type UptoPayloadBase as t, type UptoEvmPayload as u, type UptoEvmPayloadCompact as v, type UptoSettlement as w, type UptoUsageDetails as x, type UptoSettlementResponse as y, type UptoValidationResult as z };