@openfacilitator/sdk 0.6.3 → 0.7.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.
- package/dist/index.d.mts +154 -13
- package/dist/index.d.ts +154 -13
- package/dist/index.js +129 -26
- package/dist/index.mjs +121 -26
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -7,15 +7,14 @@ interface FacilitatorConfig {
|
|
|
7
7
|
headers?: Record<string, string>;
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
|
-
* Payment payload for
|
|
11
|
-
* Supports both x402 v1 and v2 formats
|
|
10
|
+
* Payment payload for x402 version 1. Uses flat structure with scheme/network at top level.
|
|
12
11
|
*/
|
|
13
|
-
interface
|
|
14
|
-
/** x402 version
|
|
15
|
-
x402Version: 1
|
|
12
|
+
interface PaymentPayloadV1 {
|
|
13
|
+
/** x402 version 1 */
|
|
14
|
+
x402Version: 1;
|
|
16
15
|
/** Payment scheme (e.g., "exact") */
|
|
17
16
|
scheme: string;
|
|
18
|
-
/** Network identifier - v1
|
|
17
|
+
/** Network identifier - v1 format (e.g., "base", "solana") */
|
|
19
18
|
network: string;
|
|
20
19
|
/** Payment details */
|
|
21
20
|
payload: {
|
|
@@ -25,6 +24,48 @@ interface PaymentPayload {
|
|
|
25
24
|
authorization: PaymentAuthorization;
|
|
26
25
|
};
|
|
27
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Payment payload for x402 version 2. Uses nested `accepted` structure per @x402/core spec.
|
|
29
|
+
*/
|
|
30
|
+
interface PaymentPayloadV2 {
|
|
31
|
+
/** x402 version 2 */
|
|
32
|
+
x402Version: 2;
|
|
33
|
+
/** Optional resource being paid for */
|
|
34
|
+
resource?: {
|
|
35
|
+
/** Resource URL */
|
|
36
|
+
url: string;
|
|
37
|
+
/** Human-readable description */
|
|
38
|
+
description?: string;
|
|
39
|
+
/** MIME type of resource */
|
|
40
|
+
mimeType?: string;
|
|
41
|
+
};
|
|
42
|
+
/** Accepted payment requirements (contains scheme, network, amount, etc.) */
|
|
43
|
+
accepted: {
|
|
44
|
+
/** Payment scheme (e.g., "exact") */
|
|
45
|
+
scheme: string;
|
|
46
|
+
/** Network identifier - CAIP-2 format (e.g., "eip155:8453") */
|
|
47
|
+
network: string;
|
|
48
|
+
/** Token/asset address */
|
|
49
|
+
asset: string;
|
|
50
|
+
/** Amount in base units */
|
|
51
|
+
amount: string;
|
|
52
|
+
/** Recipient address */
|
|
53
|
+
payTo: string;
|
|
54
|
+
/** Maximum timeout in seconds */
|
|
55
|
+
maxTimeoutSeconds: number;
|
|
56
|
+
/** Extra data */
|
|
57
|
+
extra?: Record<string, unknown>;
|
|
58
|
+
};
|
|
59
|
+
/** Payment details (signature, authorization, etc.) */
|
|
60
|
+
payload: Record<string, unknown>;
|
|
61
|
+
/** Optional extensions */
|
|
62
|
+
extensions?: Record<string, unknown>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Payment payload union - supports both x402 v1 and v2 formats.
|
|
66
|
+
* Use x402Version to discriminate between versions.
|
|
67
|
+
*/
|
|
68
|
+
type PaymentPayload = PaymentPayloadV1 | PaymentPayloadV2;
|
|
28
69
|
interface PaymentAuthorization {
|
|
29
70
|
/** Sender address */
|
|
30
71
|
from: string;
|
|
@@ -44,10 +85,9 @@ interface PaymentAuthorization {
|
|
|
44
85
|
[key: string]: unknown;
|
|
45
86
|
}
|
|
46
87
|
/**
|
|
47
|
-
* Payment requirements
|
|
48
|
-
* Used for validation during verify/settle
|
|
88
|
+
* Payment requirements for x402 version 1. Uses maxAmountRequired field.
|
|
49
89
|
*/
|
|
50
|
-
interface
|
|
90
|
+
interface PaymentRequirementsV1 {
|
|
51
91
|
/** Payment scheme (e.g., "exact") */
|
|
52
92
|
scheme: string;
|
|
53
93
|
/** Network identifier */
|
|
@@ -71,6 +111,31 @@ interface PaymentRequirements {
|
|
|
71
111
|
/** Extra data */
|
|
72
112
|
extra?: Record<string, unknown>;
|
|
73
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Payment requirements for x402 version 2.
|
|
116
|
+
* Uses 'amount' instead of 'maxAmountRequired' and has stricter required fields.
|
|
117
|
+
*/
|
|
118
|
+
interface PaymentRequirementsV2 {
|
|
119
|
+
/** Payment scheme (e.g., "exact") */
|
|
120
|
+
scheme: string;
|
|
121
|
+
/** Network identifier - v2 CAIP-2 format */
|
|
122
|
+
network: string;
|
|
123
|
+
/** Amount required in base units */
|
|
124
|
+
amount: string;
|
|
125
|
+
/** Token/asset address */
|
|
126
|
+
asset: string;
|
|
127
|
+
/** Recipient address */
|
|
128
|
+
payTo: string;
|
|
129
|
+
/** Maximum timeout in seconds */
|
|
130
|
+
maxTimeoutSeconds: number;
|
|
131
|
+
/** Extra data */
|
|
132
|
+
extra: Record<string, unknown>;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Payment requirements union - supports both v1 and v2 formats.
|
|
136
|
+
* V1 uses maxAmountRequired, V2 uses amount.
|
|
137
|
+
*/
|
|
138
|
+
type PaymentRequirements = PaymentRequirementsV1 | PaymentRequirementsV2;
|
|
74
139
|
interface VerifyResponse {
|
|
75
140
|
/** Whether the payment is valid */
|
|
76
141
|
isValid: boolean;
|
|
@@ -256,6 +321,65 @@ declare function getTestnets(): NetworkInfo[];
|
|
|
256
321
|
* Type guard for checking if value is a valid payment payload
|
|
257
322
|
*/
|
|
258
323
|
declare function isPaymentPayload(value: unknown): value is PaymentPayload;
|
|
324
|
+
/**
|
|
325
|
+
* Type guard for PaymentPayloadV1 (x402 version 1).
|
|
326
|
+
* Narrows PaymentPayload to v1 format with flat structure.
|
|
327
|
+
*/
|
|
328
|
+
declare function isPaymentPayloadV1(value: unknown): value is PaymentPayloadV1;
|
|
329
|
+
/**
|
|
330
|
+
* Type guard for PaymentPayloadV2 (x402 version 2).
|
|
331
|
+
* Narrows PaymentPayload to v2 format with nested `accepted` structure.
|
|
332
|
+
*/
|
|
333
|
+
declare function isPaymentPayloadV2(value: unknown): value is PaymentPayloadV2;
|
|
334
|
+
/**
|
|
335
|
+
* Type guard for PaymentRequirementsV1.
|
|
336
|
+
* V1 requirements have maxAmountRequired field.
|
|
337
|
+
*/
|
|
338
|
+
declare function isPaymentRequirementsV1(value: unknown): value is PaymentRequirementsV1;
|
|
339
|
+
/**
|
|
340
|
+
* Type guard for PaymentRequirementsV2.
|
|
341
|
+
* V2 requirements have amount but NOT maxAmountRequired.
|
|
342
|
+
*/
|
|
343
|
+
declare function isPaymentRequirementsV2(value: unknown): value is PaymentRequirementsV2;
|
|
344
|
+
/**
|
|
345
|
+
* Extract scheme and network from PaymentPayload (version-agnostic).
|
|
346
|
+
* v1 has these at top level, v2 has them nested in `accepted`.
|
|
347
|
+
*/
|
|
348
|
+
declare function getSchemeNetwork(payload: PaymentPayload): {
|
|
349
|
+
scheme: string;
|
|
350
|
+
network: string;
|
|
351
|
+
};
|
|
352
|
+
/**
|
|
353
|
+
* Get x402 version from PaymentPayload.
|
|
354
|
+
* Returns literal type 1 | 2 for exhaustiveness checking in switch statements.
|
|
355
|
+
*/
|
|
356
|
+
declare function getVersion(payload: PaymentPayload): 1 | 2;
|
|
357
|
+
/**
|
|
358
|
+
* Safely extract x402 version from an unknown payment object.
|
|
359
|
+
* Provides backward compatibility for pre-versioning payloads.
|
|
360
|
+
*
|
|
361
|
+
* - Returns 1 if x402Version is undefined/missing (backward compatibility)
|
|
362
|
+
* - Returns 1 or 2 for valid versions
|
|
363
|
+
* - Throws descriptive error for unsupported versions
|
|
364
|
+
*
|
|
365
|
+
* Use this at method entry points to validate version before processing.
|
|
366
|
+
* For type-safe access after validation, use getVersion() instead.
|
|
367
|
+
*/
|
|
368
|
+
declare function getVersionSafe(payment: unknown): 1 | 2;
|
|
369
|
+
/**
|
|
370
|
+
* Exhaustiveness check for discriminated unions.
|
|
371
|
+
* TypeScript will error at compile time if not all union members are handled.
|
|
372
|
+
*
|
|
373
|
+
* @example
|
|
374
|
+
* function handlePayload(payload: PaymentPayload) {
|
|
375
|
+
* switch (payload.x402Version) {
|
|
376
|
+
* case 1: return handleV1(payload);
|
|
377
|
+
* case 2: return handleV2(payload);
|
|
378
|
+
* default: return assertNever(payload);
|
|
379
|
+
* }
|
|
380
|
+
* }
|
|
381
|
+
*/
|
|
382
|
+
declare function assertNever(value: never, message?: string): never;
|
|
259
383
|
|
|
260
384
|
/**
|
|
261
385
|
* Claims module for reporting failures and managing refunds
|
|
@@ -380,7 +504,7 @@ declare function executeClaim(params: ExecuteClaimParams): Promise<ExecuteClaimR
|
|
|
380
504
|
interface RefundProtectionConfig {
|
|
381
505
|
/** The API key from server registration */
|
|
382
506
|
apiKey: string;
|
|
383
|
-
/** The facilitator URL */
|
|
507
|
+
/** The facilitator URL (required for standalone usage) */
|
|
384
508
|
facilitatorUrl: string;
|
|
385
509
|
/** Optional: Custom error filter - return false to skip reporting */
|
|
386
510
|
shouldReport?: (error: Error) => boolean;
|
|
@@ -389,6 +513,22 @@ interface RefundProtectionConfig {
|
|
|
389
513
|
/** Optional: Called when reporting fails */
|
|
390
514
|
onReportError?: (reportError: Error, originalError: Error) => void;
|
|
391
515
|
}
|
|
516
|
+
/**
|
|
517
|
+
* Refund protection config for use with payment middleware.
|
|
518
|
+
* The facilitatorUrl is optional and defaults to the middleware's facilitator URL.
|
|
519
|
+
*/
|
|
520
|
+
interface MiddlewareRefundConfig {
|
|
521
|
+
/** The API key from server registration */
|
|
522
|
+
apiKey: string;
|
|
523
|
+
/** The facilitator URL (optional - defaults to the middleware's facilitator URL) */
|
|
524
|
+
facilitatorUrl?: string;
|
|
525
|
+
/** Optional: Custom error filter - return false to skip reporting */
|
|
526
|
+
shouldReport?: (error: Error) => boolean;
|
|
527
|
+
/** Optional: Called when a failure is reported */
|
|
528
|
+
onReport?: (claimId: string | undefined, error: Error) => void;
|
|
529
|
+
/** Optional: Called when reporting fails */
|
|
530
|
+
onReportError?: (reportError: Error, originalError: Error) => void;
|
|
531
|
+
}
|
|
392
532
|
interface PaymentContext {
|
|
393
533
|
/** Transaction hash from settlement */
|
|
394
534
|
transactionHash: string;
|
|
@@ -518,6 +658,7 @@ declare function createPaymentContext(settleResponse: {
|
|
|
518
658
|
network: string;
|
|
519
659
|
}, paymentPayload: Record<string, unknown>, requirements?: {
|
|
520
660
|
maxAmountRequired?: string;
|
|
661
|
+
amount?: string;
|
|
521
662
|
asset?: string;
|
|
522
663
|
}): PaymentContext;
|
|
523
664
|
interface PaymentMiddlewareConfig {
|
|
@@ -526,7 +667,7 @@ interface PaymentMiddlewareConfig {
|
|
|
526
667
|
/** Function to get payment requirements for the request (single or multiple for multi-network) */
|
|
527
668
|
getRequirements: (req: unknown) => PaymentRequirements | PaymentRequirements[] | Promise<PaymentRequirements | PaymentRequirements[]>;
|
|
528
669
|
/** Optional: Refund protection config (enables auto failure reporting) */
|
|
529
|
-
refundProtection?:
|
|
670
|
+
refundProtection?: MiddlewareRefundConfig;
|
|
530
671
|
/** Optional: Custom 402 response handler */
|
|
531
672
|
on402?: (req: unknown, res: unknown, requirements: PaymentRequirements[]) => void | Promise<void>;
|
|
532
673
|
}
|
|
@@ -579,7 +720,7 @@ interface HonoPaymentConfig {
|
|
|
579
720
|
/** Function to get payment requirements for the request (single or multiple for multi-network) */
|
|
580
721
|
getRequirements: (c: HonoContext) => PaymentRequirements | PaymentRequirements[] | Promise<PaymentRequirements | PaymentRequirements[]>;
|
|
581
722
|
/** Optional: Refund protection config */
|
|
582
|
-
refundProtection?:
|
|
723
|
+
refundProtection?: MiddlewareRefundConfig;
|
|
583
724
|
}
|
|
584
725
|
/**
|
|
585
726
|
* Create Hono x402 payment middleware.
|
|
@@ -619,4 +760,4 @@ declare function honoPaymentMiddleware(config: HonoPaymentConfig): (c: HonoConte
|
|
|
619
760
|
json: (body: unknown, status?: number) => Response;
|
|
620
761
|
}, next: () => Promise<void>) => Promise<Response | undefined>;
|
|
621
762
|
|
|
622
|
-
export { type ClaimHistoryItem, type ClaimableItem, ConfigurationError, type ExecuteClaimParams, type ExecuteClaimResponse, type FacilitatorConfig, FacilitatorError, type GetClaimHistoryResponse, type GetClaimableParams, type GetClaimableResponse, type HonoPaymentConfig, type HonoRefundConfig, NETWORKS, NetworkError, type NetworkInfo, type NetworkType, OpenFacilitator, type PaymentAuthorization, type PaymentContext, type PaymentKind, type PaymentMiddlewareConfig, type PaymentPayload, type PaymentRequest, type PaymentRequirements, type RefundProtectionConfig, type ReportFailureParams, type ReportFailureResponse, type SettleResponse, SettlementError, type SupportedResponse, VerificationError, type VerifyResponse, createDefaultFacilitator, createPaymentContext, createPaymentMiddleware, createRefundMiddleware, executeClaim, getClaimHistory, getClaimable, getMainnets, getNetwork, getNetworkType, getTestnets, honoPaymentMiddleware, honoRefundMiddleware, isPaymentPayload, isValidNetwork, reportFailure, toV1NetworkId, toV2NetworkId, withRefundProtection };
|
|
763
|
+
export { type ClaimHistoryItem, type ClaimableItem, ConfigurationError, type ExecuteClaimParams, type ExecuteClaimResponse, type FacilitatorConfig, FacilitatorError, type GetClaimHistoryResponse, type GetClaimableParams, type GetClaimableResponse, type HonoPaymentConfig, type HonoRefundConfig, type MiddlewareRefundConfig, NETWORKS, NetworkError, type NetworkInfo, type NetworkType, OpenFacilitator, type PaymentAuthorization, type PaymentContext, type PaymentKind, type PaymentMiddlewareConfig, type PaymentPayload, type PaymentPayloadV1, type PaymentPayloadV2, type PaymentRequest, type PaymentRequirements, type PaymentRequirementsV1, type PaymentRequirementsV2, type RefundProtectionConfig, type ReportFailureParams, type ReportFailureResponse, type SettleResponse, SettlementError, type SupportedResponse, VerificationError, type VerifyResponse, assertNever, createDefaultFacilitator, createPaymentContext, createPaymentMiddleware, createRefundMiddleware, executeClaim, getClaimHistory, getClaimable, getMainnets, getNetwork, getNetworkType, getSchemeNetwork, getTestnets, getVersion, getVersionSafe, honoPaymentMiddleware, honoRefundMiddleware, isPaymentPayload, isPaymentPayloadV1, isPaymentPayloadV2, isPaymentRequirementsV1, isPaymentRequirementsV2, isValidNetwork, reportFailure, toV1NetworkId, toV2NetworkId, withRefundProtection };
|
package/dist/index.d.ts
CHANGED
|
@@ -7,15 +7,14 @@ interface FacilitatorConfig {
|
|
|
7
7
|
headers?: Record<string, string>;
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
|
-
* Payment payload for
|
|
11
|
-
* Supports both x402 v1 and v2 formats
|
|
10
|
+
* Payment payload for x402 version 1. Uses flat structure with scheme/network at top level.
|
|
12
11
|
*/
|
|
13
|
-
interface
|
|
14
|
-
/** x402 version
|
|
15
|
-
x402Version: 1
|
|
12
|
+
interface PaymentPayloadV1 {
|
|
13
|
+
/** x402 version 1 */
|
|
14
|
+
x402Version: 1;
|
|
16
15
|
/** Payment scheme (e.g., "exact") */
|
|
17
16
|
scheme: string;
|
|
18
|
-
/** Network identifier - v1
|
|
17
|
+
/** Network identifier - v1 format (e.g., "base", "solana") */
|
|
19
18
|
network: string;
|
|
20
19
|
/** Payment details */
|
|
21
20
|
payload: {
|
|
@@ -25,6 +24,48 @@ interface PaymentPayload {
|
|
|
25
24
|
authorization: PaymentAuthorization;
|
|
26
25
|
};
|
|
27
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Payment payload for x402 version 2. Uses nested `accepted` structure per @x402/core spec.
|
|
29
|
+
*/
|
|
30
|
+
interface PaymentPayloadV2 {
|
|
31
|
+
/** x402 version 2 */
|
|
32
|
+
x402Version: 2;
|
|
33
|
+
/** Optional resource being paid for */
|
|
34
|
+
resource?: {
|
|
35
|
+
/** Resource URL */
|
|
36
|
+
url: string;
|
|
37
|
+
/** Human-readable description */
|
|
38
|
+
description?: string;
|
|
39
|
+
/** MIME type of resource */
|
|
40
|
+
mimeType?: string;
|
|
41
|
+
};
|
|
42
|
+
/** Accepted payment requirements (contains scheme, network, amount, etc.) */
|
|
43
|
+
accepted: {
|
|
44
|
+
/** Payment scheme (e.g., "exact") */
|
|
45
|
+
scheme: string;
|
|
46
|
+
/** Network identifier - CAIP-2 format (e.g., "eip155:8453") */
|
|
47
|
+
network: string;
|
|
48
|
+
/** Token/asset address */
|
|
49
|
+
asset: string;
|
|
50
|
+
/** Amount in base units */
|
|
51
|
+
amount: string;
|
|
52
|
+
/** Recipient address */
|
|
53
|
+
payTo: string;
|
|
54
|
+
/** Maximum timeout in seconds */
|
|
55
|
+
maxTimeoutSeconds: number;
|
|
56
|
+
/** Extra data */
|
|
57
|
+
extra?: Record<string, unknown>;
|
|
58
|
+
};
|
|
59
|
+
/** Payment details (signature, authorization, etc.) */
|
|
60
|
+
payload: Record<string, unknown>;
|
|
61
|
+
/** Optional extensions */
|
|
62
|
+
extensions?: Record<string, unknown>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Payment payload union - supports both x402 v1 and v2 formats.
|
|
66
|
+
* Use x402Version to discriminate between versions.
|
|
67
|
+
*/
|
|
68
|
+
type PaymentPayload = PaymentPayloadV1 | PaymentPayloadV2;
|
|
28
69
|
interface PaymentAuthorization {
|
|
29
70
|
/** Sender address */
|
|
30
71
|
from: string;
|
|
@@ -44,10 +85,9 @@ interface PaymentAuthorization {
|
|
|
44
85
|
[key: string]: unknown;
|
|
45
86
|
}
|
|
46
87
|
/**
|
|
47
|
-
* Payment requirements
|
|
48
|
-
* Used for validation during verify/settle
|
|
88
|
+
* Payment requirements for x402 version 1. Uses maxAmountRequired field.
|
|
49
89
|
*/
|
|
50
|
-
interface
|
|
90
|
+
interface PaymentRequirementsV1 {
|
|
51
91
|
/** Payment scheme (e.g., "exact") */
|
|
52
92
|
scheme: string;
|
|
53
93
|
/** Network identifier */
|
|
@@ -71,6 +111,31 @@ interface PaymentRequirements {
|
|
|
71
111
|
/** Extra data */
|
|
72
112
|
extra?: Record<string, unknown>;
|
|
73
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Payment requirements for x402 version 2.
|
|
116
|
+
* Uses 'amount' instead of 'maxAmountRequired' and has stricter required fields.
|
|
117
|
+
*/
|
|
118
|
+
interface PaymentRequirementsV2 {
|
|
119
|
+
/** Payment scheme (e.g., "exact") */
|
|
120
|
+
scheme: string;
|
|
121
|
+
/** Network identifier - v2 CAIP-2 format */
|
|
122
|
+
network: string;
|
|
123
|
+
/** Amount required in base units */
|
|
124
|
+
amount: string;
|
|
125
|
+
/** Token/asset address */
|
|
126
|
+
asset: string;
|
|
127
|
+
/** Recipient address */
|
|
128
|
+
payTo: string;
|
|
129
|
+
/** Maximum timeout in seconds */
|
|
130
|
+
maxTimeoutSeconds: number;
|
|
131
|
+
/** Extra data */
|
|
132
|
+
extra: Record<string, unknown>;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Payment requirements union - supports both v1 and v2 formats.
|
|
136
|
+
* V1 uses maxAmountRequired, V2 uses amount.
|
|
137
|
+
*/
|
|
138
|
+
type PaymentRequirements = PaymentRequirementsV1 | PaymentRequirementsV2;
|
|
74
139
|
interface VerifyResponse {
|
|
75
140
|
/** Whether the payment is valid */
|
|
76
141
|
isValid: boolean;
|
|
@@ -256,6 +321,65 @@ declare function getTestnets(): NetworkInfo[];
|
|
|
256
321
|
* Type guard for checking if value is a valid payment payload
|
|
257
322
|
*/
|
|
258
323
|
declare function isPaymentPayload(value: unknown): value is PaymentPayload;
|
|
324
|
+
/**
|
|
325
|
+
* Type guard for PaymentPayloadV1 (x402 version 1).
|
|
326
|
+
* Narrows PaymentPayload to v1 format with flat structure.
|
|
327
|
+
*/
|
|
328
|
+
declare function isPaymentPayloadV1(value: unknown): value is PaymentPayloadV1;
|
|
329
|
+
/**
|
|
330
|
+
* Type guard for PaymentPayloadV2 (x402 version 2).
|
|
331
|
+
* Narrows PaymentPayload to v2 format with nested `accepted` structure.
|
|
332
|
+
*/
|
|
333
|
+
declare function isPaymentPayloadV2(value: unknown): value is PaymentPayloadV2;
|
|
334
|
+
/**
|
|
335
|
+
* Type guard for PaymentRequirementsV1.
|
|
336
|
+
* V1 requirements have maxAmountRequired field.
|
|
337
|
+
*/
|
|
338
|
+
declare function isPaymentRequirementsV1(value: unknown): value is PaymentRequirementsV1;
|
|
339
|
+
/**
|
|
340
|
+
* Type guard for PaymentRequirementsV2.
|
|
341
|
+
* V2 requirements have amount but NOT maxAmountRequired.
|
|
342
|
+
*/
|
|
343
|
+
declare function isPaymentRequirementsV2(value: unknown): value is PaymentRequirementsV2;
|
|
344
|
+
/**
|
|
345
|
+
* Extract scheme and network from PaymentPayload (version-agnostic).
|
|
346
|
+
* v1 has these at top level, v2 has them nested in `accepted`.
|
|
347
|
+
*/
|
|
348
|
+
declare function getSchemeNetwork(payload: PaymentPayload): {
|
|
349
|
+
scheme: string;
|
|
350
|
+
network: string;
|
|
351
|
+
};
|
|
352
|
+
/**
|
|
353
|
+
* Get x402 version from PaymentPayload.
|
|
354
|
+
* Returns literal type 1 | 2 for exhaustiveness checking in switch statements.
|
|
355
|
+
*/
|
|
356
|
+
declare function getVersion(payload: PaymentPayload): 1 | 2;
|
|
357
|
+
/**
|
|
358
|
+
* Safely extract x402 version from an unknown payment object.
|
|
359
|
+
* Provides backward compatibility for pre-versioning payloads.
|
|
360
|
+
*
|
|
361
|
+
* - Returns 1 if x402Version is undefined/missing (backward compatibility)
|
|
362
|
+
* - Returns 1 or 2 for valid versions
|
|
363
|
+
* - Throws descriptive error for unsupported versions
|
|
364
|
+
*
|
|
365
|
+
* Use this at method entry points to validate version before processing.
|
|
366
|
+
* For type-safe access after validation, use getVersion() instead.
|
|
367
|
+
*/
|
|
368
|
+
declare function getVersionSafe(payment: unknown): 1 | 2;
|
|
369
|
+
/**
|
|
370
|
+
* Exhaustiveness check for discriminated unions.
|
|
371
|
+
* TypeScript will error at compile time if not all union members are handled.
|
|
372
|
+
*
|
|
373
|
+
* @example
|
|
374
|
+
* function handlePayload(payload: PaymentPayload) {
|
|
375
|
+
* switch (payload.x402Version) {
|
|
376
|
+
* case 1: return handleV1(payload);
|
|
377
|
+
* case 2: return handleV2(payload);
|
|
378
|
+
* default: return assertNever(payload);
|
|
379
|
+
* }
|
|
380
|
+
* }
|
|
381
|
+
*/
|
|
382
|
+
declare function assertNever(value: never, message?: string): never;
|
|
259
383
|
|
|
260
384
|
/**
|
|
261
385
|
* Claims module for reporting failures and managing refunds
|
|
@@ -380,7 +504,7 @@ declare function executeClaim(params: ExecuteClaimParams): Promise<ExecuteClaimR
|
|
|
380
504
|
interface RefundProtectionConfig {
|
|
381
505
|
/** The API key from server registration */
|
|
382
506
|
apiKey: string;
|
|
383
|
-
/** The facilitator URL */
|
|
507
|
+
/** The facilitator URL (required for standalone usage) */
|
|
384
508
|
facilitatorUrl: string;
|
|
385
509
|
/** Optional: Custom error filter - return false to skip reporting */
|
|
386
510
|
shouldReport?: (error: Error) => boolean;
|
|
@@ -389,6 +513,22 @@ interface RefundProtectionConfig {
|
|
|
389
513
|
/** Optional: Called when reporting fails */
|
|
390
514
|
onReportError?: (reportError: Error, originalError: Error) => void;
|
|
391
515
|
}
|
|
516
|
+
/**
|
|
517
|
+
* Refund protection config for use with payment middleware.
|
|
518
|
+
* The facilitatorUrl is optional and defaults to the middleware's facilitator URL.
|
|
519
|
+
*/
|
|
520
|
+
interface MiddlewareRefundConfig {
|
|
521
|
+
/** The API key from server registration */
|
|
522
|
+
apiKey: string;
|
|
523
|
+
/** The facilitator URL (optional - defaults to the middleware's facilitator URL) */
|
|
524
|
+
facilitatorUrl?: string;
|
|
525
|
+
/** Optional: Custom error filter - return false to skip reporting */
|
|
526
|
+
shouldReport?: (error: Error) => boolean;
|
|
527
|
+
/** Optional: Called when a failure is reported */
|
|
528
|
+
onReport?: (claimId: string | undefined, error: Error) => void;
|
|
529
|
+
/** Optional: Called when reporting fails */
|
|
530
|
+
onReportError?: (reportError: Error, originalError: Error) => void;
|
|
531
|
+
}
|
|
392
532
|
interface PaymentContext {
|
|
393
533
|
/** Transaction hash from settlement */
|
|
394
534
|
transactionHash: string;
|
|
@@ -518,6 +658,7 @@ declare function createPaymentContext(settleResponse: {
|
|
|
518
658
|
network: string;
|
|
519
659
|
}, paymentPayload: Record<string, unknown>, requirements?: {
|
|
520
660
|
maxAmountRequired?: string;
|
|
661
|
+
amount?: string;
|
|
521
662
|
asset?: string;
|
|
522
663
|
}): PaymentContext;
|
|
523
664
|
interface PaymentMiddlewareConfig {
|
|
@@ -526,7 +667,7 @@ interface PaymentMiddlewareConfig {
|
|
|
526
667
|
/** Function to get payment requirements for the request (single or multiple for multi-network) */
|
|
527
668
|
getRequirements: (req: unknown) => PaymentRequirements | PaymentRequirements[] | Promise<PaymentRequirements | PaymentRequirements[]>;
|
|
528
669
|
/** Optional: Refund protection config (enables auto failure reporting) */
|
|
529
|
-
refundProtection?:
|
|
670
|
+
refundProtection?: MiddlewareRefundConfig;
|
|
530
671
|
/** Optional: Custom 402 response handler */
|
|
531
672
|
on402?: (req: unknown, res: unknown, requirements: PaymentRequirements[]) => void | Promise<void>;
|
|
532
673
|
}
|
|
@@ -579,7 +720,7 @@ interface HonoPaymentConfig {
|
|
|
579
720
|
/** Function to get payment requirements for the request (single or multiple for multi-network) */
|
|
580
721
|
getRequirements: (c: HonoContext) => PaymentRequirements | PaymentRequirements[] | Promise<PaymentRequirements | PaymentRequirements[]>;
|
|
581
722
|
/** Optional: Refund protection config */
|
|
582
|
-
refundProtection?:
|
|
723
|
+
refundProtection?: MiddlewareRefundConfig;
|
|
583
724
|
}
|
|
584
725
|
/**
|
|
585
726
|
* Create Hono x402 payment middleware.
|
|
@@ -619,4 +760,4 @@ declare function honoPaymentMiddleware(config: HonoPaymentConfig): (c: HonoConte
|
|
|
619
760
|
json: (body: unknown, status?: number) => Response;
|
|
620
761
|
}, next: () => Promise<void>) => Promise<Response | undefined>;
|
|
621
762
|
|
|
622
|
-
export { type ClaimHistoryItem, type ClaimableItem, ConfigurationError, type ExecuteClaimParams, type ExecuteClaimResponse, type FacilitatorConfig, FacilitatorError, type GetClaimHistoryResponse, type GetClaimableParams, type GetClaimableResponse, type HonoPaymentConfig, type HonoRefundConfig, NETWORKS, NetworkError, type NetworkInfo, type NetworkType, OpenFacilitator, type PaymentAuthorization, type PaymentContext, type PaymentKind, type PaymentMiddlewareConfig, type PaymentPayload, type PaymentRequest, type PaymentRequirements, type RefundProtectionConfig, type ReportFailureParams, type ReportFailureResponse, type SettleResponse, SettlementError, type SupportedResponse, VerificationError, type VerifyResponse, createDefaultFacilitator, createPaymentContext, createPaymentMiddleware, createRefundMiddleware, executeClaim, getClaimHistory, getClaimable, getMainnets, getNetwork, getNetworkType, getTestnets, honoPaymentMiddleware, honoRefundMiddleware, isPaymentPayload, isValidNetwork, reportFailure, toV1NetworkId, toV2NetworkId, withRefundProtection };
|
|
763
|
+
export { type ClaimHistoryItem, type ClaimableItem, ConfigurationError, type ExecuteClaimParams, type ExecuteClaimResponse, type FacilitatorConfig, FacilitatorError, type GetClaimHistoryResponse, type GetClaimableParams, type GetClaimableResponse, type HonoPaymentConfig, type HonoRefundConfig, type MiddlewareRefundConfig, NETWORKS, NetworkError, type NetworkInfo, type NetworkType, OpenFacilitator, type PaymentAuthorization, type PaymentContext, type PaymentKind, type PaymentMiddlewareConfig, type PaymentPayload, type PaymentPayloadV1, type PaymentPayloadV2, type PaymentRequest, type PaymentRequirements, type PaymentRequirementsV1, type PaymentRequirementsV2, type RefundProtectionConfig, type ReportFailureParams, type ReportFailureResponse, type SettleResponse, SettlementError, type SupportedResponse, VerificationError, type VerifyResponse, assertNever, createDefaultFacilitator, createPaymentContext, createPaymentMiddleware, createRefundMiddleware, executeClaim, getClaimHistory, getClaimable, getMainnets, getNetwork, getNetworkType, getSchemeNetwork, getTestnets, getVersion, getVersionSafe, honoPaymentMiddleware, honoRefundMiddleware, isPaymentPayload, isPaymentPayloadV1, isPaymentPayloadV2, isPaymentRequirementsV1, isPaymentRequirementsV2, isValidNetwork, reportFailure, toV1NetworkId, toV2NetworkId, withRefundProtection };
|
package/dist/index.js
CHANGED
|
@@ -27,6 +27,7 @@ __export(index_exports, {
|
|
|
27
27
|
OpenFacilitator: () => OpenFacilitator,
|
|
28
28
|
SettlementError: () => SettlementError,
|
|
29
29
|
VerificationError: () => VerificationError,
|
|
30
|
+
assertNever: () => assertNever,
|
|
30
31
|
createDefaultFacilitator: () => createDefaultFacilitator,
|
|
31
32
|
createPaymentContext: () => createPaymentContext,
|
|
32
33
|
createPaymentMiddleware: () => createPaymentMiddleware,
|
|
@@ -37,10 +38,17 @@ __export(index_exports, {
|
|
|
37
38
|
getMainnets: () => getMainnets,
|
|
38
39
|
getNetwork: () => getNetwork,
|
|
39
40
|
getNetworkType: () => getNetworkType,
|
|
41
|
+
getSchemeNetwork: () => getSchemeNetwork,
|
|
40
42
|
getTestnets: () => getTestnets,
|
|
43
|
+
getVersion: () => getVersion,
|
|
44
|
+
getVersionSafe: () => getVersionSafe,
|
|
41
45
|
honoPaymentMiddleware: () => honoPaymentMiddleware,
|
|
42
46
|
honoRefundMiddleware: () => honoRefundMiddleware,
|
|
43
47
|
isPaymentPayload: () => isPaymentPayload,
|
|
48
|
+
isPaymentPayloadV1: () => isPaymentPayloadV1,
|
|
49
|
+
isPaymentPayloadV2: () => isPaymentPayloadV2,
|
|
50
|
+
isPaymentRequirementsV1: () => isPaymentRequirementsV1,
|
|
51
|
+
isPaymentRequirementsV2: () => isPaymentRequirementsV2,
|
|
44
52
|
isValidNetwork: () => isValidNetwork,
|
|
45
53
|
reportFailure: () => reportFailure,
|
|
46
54
|
toV1NetworkId: () => toV1NetworkId,
|
|
@@ -94,7 +102,66 @@ function buildUrl(baseUrl, path) {
|
|
|
94
102
|
function isPaymentPayload(value) {
|
|
95
103
|
if (!value || typeof value !== "object") return false;
|
|
96
104
|
const obj = value;
|
|
97
|
-
|
|
105
|
+
if (obj.x402Version === 1) {
|
|
106
|
+
return typeof obj.scheme === "string" && typeof obj.network === "string" && obj.payload !== void 0;
|
|
107
|
+
} else if (obj.x402Version === 2) {
|
|
108
|
+
const accepted = obj.accepted;
|
|
109
|
+
return accepted !== void 0 && typeof accepted === "object" && typeof accepted.scheme === "string" && typeof accepted.network === "string" && obj.payload !== void 0;
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
function isPaymentPayloadV1(value) {
|
|
114
|
+
if (!value || typeof value !== "object") return false;
|
|
115
|
+
const obj = value;
|
|
116
|
+
return obj.x402Version === 1 && typeof obj.scheme === "string" && typeof obj.network === "string" && obj.payload !== void 0 && typeof obj.payload === "object";
|
|
117
|
+
}
|
|
118
|
+
function isPaymentPayloadV2(value) {
|
|
119
|
+
if (!value || typeof value !== "object") return false;
|
|
120
|
+
const obj = value;
|
|
121
|
+
if (obj.x402Version !== 2) return false;
|
|
122
|
+
const accepted = obj.accepted;
|
|
123
|
+
return accepted !== void 0 && typeof accepted === "object" && typeof accepted.scheme === "string" && typeof accepted.network === "string" && typeof accepted.asset === "string" && typeof accepted.amount === "string" && typeof accepted.payTo === "string" && typeof accepted.maxTimeoutSeconds === "number" && obj.payload !== void 0;
|
|
124
|
+
}
|
|
125
|
+
function isPaymentRequirementsV1(value) {
|
|
126
|
+
if (!value || typeof value !== "object") return false;
|
|
127
|
+
return "maxAmountRequired" in value;
|
|
128
|
+
}
|
|
129
|
+
function isPaymentRequirementsV2(value) {
|
|
130
|
+
if (!value || typeof value !== "object") return false;
|
|
131
|
+
return "amount" in value && !("maxAmountRequired" in value);
|
|
132
|
+
}
|
|
133
|
+
function getSchemeNetwork(payload) {
|
|
134
|
+
if (payload.x402Version === 1) {
|
|
135
|
+
return {
|
|
136
|
+
scheme: payload.scheme,
|
|
137
|
+
network: payload.network
|
|
138
|
+
};
|
|
139
|
+
} else {
|
|
140
|
+
return {
|
|
141
|
+
scheme: payload.accepted.scheme,
|
|
142
|
+
network: payload.accepted.network
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function getVersion(payload) {
|
|
147
|
+
return payload.x402Version;
|
|
148
|
+
}
|
|
149
|
+
function getVersionSafe(payment) {
|
|
150
|
+
if (!payment || typeof payment !== "object") {
|
|
151
|
+
return 1;
|
|
152
|
+
}
|
|
153
|
+
const obj = payment;
|
|
154
|
+
const version = obj.x402Version;
|
|
155
|
+
if (version === void 0) return 1;
|
|
156
|
+
if (version === 1 || version === 2) return version;
|
|
157
|
+
throw new Error(
|
|
158
|
+
`Unsupported x402 version: ${version}. SDK supports versions 1 and 2.`
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
function assertNever(value, message) {
|
|
162
|
+
throw new Error(
|
|
163
|
+
message ?? `Unhandled discriminated union member: ${JSON.stringify(value)}`
|
|
164
|
+
);
|
|
98
165
|
}
|
|
99
166
|
|
|
100
167
|
// src/client.ts
|
|
@@ -124,8 +191,9 @@ var OpenFacilitator = class {
|
|
|
124
191
|
*/
|
|
125
192
|
async verify(payment, requirements) {
|
|
126
193
|
try {
|
|
194
|
+
const version = getVersionSafe(payment);
|
|
127
195
|
const body = {
|
|
128
|
-
x402Version:
|
|
196
|
+
x402Version: version,
|
|
129
197
|
paymentPayload: payment,
|
|
130
198
|
paymentRequirements: requirements
|
|
131
199
|
};
|
|
@@ -149,8 +217,9 @@ var OpenFacilitator = class {
|
|
|
149
217
|
*/
|
|
150
218
|
async settle(payment, requirements) {
|
|
151
219
|
try {
|
|
220
|
+
const version = getVersionSafe(payment);
|
|
152
221
|
const body = {
|
|
153
|
-
x402Version:
|
|
222
|
+
x402Version: version,
|
|
154
223
|
paymentPayload: payment,
|
|
155
224
|
paymentRequirements: requirements
|
|
156
225
|
};
|
|
@@ -562,7 +631,7 @@ function honoRefundMiddleware(config) {
|
|
|
562
631
|
function createPaymentContext(settleResponse, paymentPayload, requirements) {
|
|
563
632
|
const payload = paymentPayload.payload;
|
|
564
633
|
const authorization = payload?.authorization;
|
|
565
|
-
const amount = authorization?.amount || payload?.amount || requirements?.maxAmountRequired || "0";
|
|
634
|
+
const amount = authorization?.amount || payload?.amount || requirements?.amount || requirements?.maxAmountRequired || "0";
|
|
566
635
|
const asset = authorization?.asset || payload?.asset || requirements?.asset || "";
|
|
567
636
|
return {
|
|
568
637
|
transactionHash: settleResponse.transaction,
|
|
@@ -591,16 +660,28 @@ function createPaymentMiddleware(config) {
|
|
|
591
660
|
if (config.refundProtection) {
|
|
592
661
|
extra.supportsRefunds = true;
|
|
593
662
|
}
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
663
|
+
if ("maxAmountRequired" in requirements2) {
|
|
664
|
+
return {
|
|
665
|
+
scheme: requirements2.scheme,
|
|
666
|
+
network: requirements2.network,
|
|
667
|
+
maxAmountRequired: requirements2.maxAmountRequired,
|
|
668
|
+
asset: requirements2.asset,
|
|
669
|
+
payTo: requirements2.payTo,
|
|
670
|
+
resource: requirements2.resource || req.url,
|
|
671
|
+
description: requirements2.description,
|
|
672
|
+
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
673
|
+
};
|
|
674
|
+
} else {
|
|
675
|
+
return {
|
|
676
|
+
scheme: requirements2.scheme,
|
|
677
|
+
network: requirements2.network,
|
|
678
|
+
amount: requirements2.amount,
|
|
679
|
+
asset: requirements2.asset,
|
|
680
|
+
payTo: requirements2.payTo,
|
|
681
|
+
maxTimeoutSeconds: requirements2.maxTimeoutSeconds,
|
|
682
|
+
extra: { ...extra, ...requirements2.extra }
|
|
683
|
+
};
|
|
684
|
+
}
|
|
604
685
|
});
|
|
605
686
|
res.status(402).json({
|
|
606
687
|
x402Version: 2,
|
|
@@ -647,13 +728,14 @@ function createPaymentMiddleware(config) {
|
|
|
647
728
|
if (config.refundProtection) {
|
|
648
729
|
const originalNext = next;
|
|
649
730
|
const refundConfig = config.refundProtection;
|
|
731
|
+
const refundFacilitatorUrl = refundConfig.facilitatorUrl || facilitator.url;
|
|
650
732
|
next = async (error) => {
|
|
651
733
|
if (error) {
|
|
652
734
|
const err = error instanceof Error ? error : new Error(String(error));
|
|
653
735
|
if (!refundConfig.shouldReport || refundConfig.shouldReport(err)) {
|
|
654
736
|
try {
|
|
655
737
|
const result = await reportFailure({
|
|
656
|
-
facilitatorUrl:
|
|
738
|
+
facilitatorUrl: refundFacilitatorUrl,
|
|
657
739
|
apiKey: refundConfig.apiKey,
|
|
658
740
|
originalTxHash: paymentContext.transactionHash,
|
|
659
741
|
userWallet: paymentContext.userWallet,
|
|
@@ -698,16 +780,28 @@ function honoPaymentMiddleware(config) {
|
|
|
698
780
|
if (config.refundProtection) {
|
|
699
781
|
extra.supportsRefunds = true;
|
|
700
782
|
}
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
783
|
+
if ("maxAmountRequired" in requirements2) {
|
|
784
|
+
return {
|
|
785
|
+
scheme: requirements2.scheme,
|
|
786
|
+
network: requirements2.network,
|
|
787
|
+
maxAmountRequired: requirements2.maxAmountRequired,
|
|
788
|
+
asset: requirements2.asset,
|
|
789
|
+
payTo: requirements2.payTo,
|
|
790
|
+
resource: requirements2.resource || c.req.url,
|
|
791
|
+
description: requirements2.description,
|
|
792
|
+
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
793
|
+
};
|
|
794
|
+
} else {
|
|
795
|
+
return {
|
|
796
|
+
scheme: requirements2.scheme,
|
|
797
|
+
network: requirements2.network,
|
|
798
|
+
amount: requirements2.amount,
|
|
799
|
+
asset: requirements2.asset,
|
|
800
|
+
payTo: requirements2.payTo,
|
|
801
|
+
maxTimeoutSeconds: requirements2.maxTimeoutSeconds,
|
|
802
|
+
extra: { ...extra, ...requirements2.extra }
|
|
803
|
+
};
|
|
804
|
+
}
|
|
711
805
|
});
|
|
712
806
|
return c.json({
|
|
713
807
|
x402Version: 2,
|
|
@@ -745,6 +839,7 @@ function honoPaymentMiddleware(config) {
|
|
|
745
839
|
c.set("paymentContext", paymentContext);
|
|
746
840
|
if (config.refundProtection) {
|
|
747
841
|
const refundConfig = config.refundProtection;
|
|
842
|
+
const refundFacilitatorUrl = refundConfig.facilitatorUrl || facilitator.url;
|
|
748
843
|
try {
|
|
749
844
|
await next();
|
|
750
845
|
} catch (error) {
|
|
@@ -752,7 +847,7 @@ function honoPaymentMiddleware(config) {
|
|
|
752
847
|
if (!refundConfig.shouldReport || refundConfig.shouldReport(err)) {
|
|
753
848
|
try {
|
|
754
849
|
const result = await reportFailure({
|
|
755
|
-
facilitatorUrl:
|
|
850
|
+
facilitatorUrl: refundFacilitatorUrl,
|
|
756
851
|
apiKey: refundConfig.apiKey,
|
|
757
852
|
originalTxHash: paymentContext.transactionHash,
|
|
758
853
|
userWallet: paymentContext.userWallet,
|
|
@@ -789,6 +884,7 @@ function honoPaymentMiddleware(config) {
|
|
|
789
884
|
OpenFacilitator,
|
|
790
885
|
SettlementError,
|
|
791
886
|
VerificationError,
|
|
887
|
+
assertNever,
|
|
792
888
|
createDefaultFacilitator,
|
|
793
889
|
createPaymentContext,
|
|
794
890
|
createPaymentMiddleware,
|
|
@@ -799,10 +895,17 @@ function honoPaymentMiddleware(config) {
|
|
|
799
895
|
getMainnets,
|
|
800
896
|
getNetwork,
|
|
801
897
|
getNetworkType,
|
|
898
|
+
getSchemeNetwork,
|
|
802
899
|
getTestnets,
|
|
900
|
+
getVersion,
|
|
901
|
+
getVersionSafe,
|
|
803
902
|
honoPaymentMiddleware,
|
|
804
903
|
honoRefundMiddleware,
|
|
805
904
|
isPaymentPayload,
|
|
905
|
+
isPaymentPayloadV1,
|
|
906
|
+
isPaymentPayloadV2,
|
|
907
|
+
isPaymentRequirementsV1,
|
|
908
|
+
isPaymentRequirementsV2,
|
|
806
909
|
isValidNetwork,
|
|
807
910
|
reportFailure,
|
|
808
911
|
toV1NetworkId,
|
package/dist/index.mjs
CHANGED
|
@@ -43,7 +43,66 @@ function buildUrl(baseUrl, path) {
|
|
|
43
43
|
function isPaymentPayload(value) {
|
|
44
44
|
if (!value || typeof value !== "object") return false;
|
|
45
45
|
const obj = value;
|
|
46
|
-
|
|
46
|
+
if (obj.x402Version === 1) {
|
|
47
|
+
return typeof obj.scheme === "string" && typeof obj.network === "string" && obj.payload !== void 0;
|
|
48
|
+
} else if (obj.x402Version === 2) {
|
|
49
|
+
const accepted = obj.accepted;
|
|
50
|
+
return accepted !== void 0 && typeof accepted === "object" && typeof accepted.scheme === "string" && typeof accepted.network === "string" && obj.payload !== void 0;
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
function isPaymentPayloadV1(value) {
|
|
55
|
+
if (!value || typeof value !== "object") return false;
|
|
56
|
+
const obj = value;
|
|
57
|
+
return obj.x402Version === 1 && typeof obj.scheme === "string" && typeof obj.network === "string" && obj.payload !== void 0 && typeof obj.payload === "object";
|
|
58
|
+
}
|
|
59
|
+
function isPaymentPayloadV2(value) {
|
|
60
|
+
if (!value || typeof value !== "object") return false;
|
|
61
|
+
const obj = value;
|
|
62
|
+
if (obj.x402Version !== 2) return false;
|
|
63
|
+
const accepted = obj.accepted;
|
|
64
|
+
return accepted !== void 0 && typeof accepted === "object" && typeof accepted.scheme === "string" && typeof accepted.network === "string" && typeof accepted.asset === "string" && typeof accepted.amount === "string" && typeof accepted.payTo === "string" && typeof accepted.maxTimeoutSeconds === "number" && obj.payload !== void 0;
|
|
65
|
+
}
|
|
66
|
+
function isPaymentRequirementsV1(value) {
|
|
67
|
+
if (!value || typeof value !== "object") return false;
|
|
68
|
+
return "maxAmountRequired" in value;
|
|
69
|
+
}
|
|
70
|
+
function isPaymentRequirementsV2(value) {
|
|
71
|
+
if (!value || typeof value !== "object") return false;
|
|
72
|
+
return "amount" in value && !("maxAmountRequired" in value);
|
|
73
|
+
}
|
|
74
|
+
function getSchemeNetwork(payload) {
|
|
75
|
+
if (payload.x402Version === 1) {
|
|
76
|
+
return {
|
|
77
|
+
scheme: payload.scheme,
|
|
78
|
+
network: payload.network
|
|
79
|
+
};
|
|
80
|
+
} else {
|
|
81
|
+
return {
|
|
82
|
+
scheme: payload.accepted.scheme,
|
|
83
|
+
network: payload.accepted.network
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function getVersion(payload) {
|
|
88
|
+
return payload.x402Version;
|
|
89
|
+
}
|
|
90
|
+
function getVersionSafe(payment) {
|
|
91
|
+
if (!payment || typeof payment !== "object") {
|
|
92
|
+
return 1;
|
|
93
|
+
}
|
|
94
|
+
const obj = payment;
|
|
95
|
+
const version = obj.x402Version;
|
|
96
|
+
if (version === void 0) return 1;
|
|
97
|
+
if (version === 1 || version === 2) return version;
|
|
98
|
+
throw new Error(
|
|
99
|
+
`Unsupported x402 version: ${version}. SDK supports versions 1 and 2.`
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
function assertNever(value, message) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
message ?? `Unhandled discriminated union member: ${JSON.stringify(value)}`
|
|
105
|
+
);
|
|
47
106
|
}
|
|
48
107
|
|
|
49
108
|
// src/client.ts
|
|
@@ -73,8 +132,9 @@ var OpenFacilitator = class {
|
|
|
73
132
|
*/
|
|
74
133
|
async verify(payment, requirements) {
|
|
75
134
|
try {
|
|
135
|
+
const version = getVersionSafe(payment);
|
|
76
136
|
const body = {
|
|
77
|
-
x402Version:
|
|
137
|
+
x402Version: version,
|
|
78
138
|
paymentPayload: payment,
|
|
79
139
|
paymentRequirements: requirements
|
|
80
140
|
};
|
|
@@ -98,8 +158,9 @@ var OpenFacilitator = class {
|
|
|
98
158
|
*/
|
|
99
159
|
async settle(payment, requirements) {
|
|
100
160
|
try {
|
|
161
|
+
const version = getVersionSafe(payment);
|
|
101
162
|
const body = {
|
|
102
|
-
x402Version:
|
|
163
|
+
x402Version: version,
|
|
103
164
|
paymentPayload: payment,
|
|
104
165
|
paymentRequirements: requirements
|
|
105
166
|
};
|
|
@@ -511,7 +572,7 @@ function honoRefundMiddleware(config) {
|
|
|
511
572
|
function createPaymentContext(settleResponse, paymentPayload, requirements) {
|
|
512
573
|
const payload = paymentPayload.payload;
|
|
513
574
|
const authorization = payload?.authorization;
|
|
514
|
-
const amount = authorization?.amount || payload?.amount || requirements?.maxAmountRequired || "0";
|
|
575
|
+
const amount = authorization?.amount || payload?.amount || requirements?.amount || requirements?.maxAmountRequired || "0";
|
|
515
576
|
const asset = authorization?.asset || payload?.asset || requirements?.asset || "";
|
|
516
577
|
return {
|
|
517
578
|
transactionHash: settleResponse.transaction,
|
|
@@ -540,16 +601,28 @@ function createPaymentMiddleware(config) {
|
|
|
540
601
|
if (config.refundProtection) {
|
|
541
602
|
extra.supportsRefunds = true;
|
|
542
603
|
}
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
604
|
+
if ("maxAmountRequired" in requirements2) {
|
|
605
|
+
return {
|
|
606
|
+
scheme: requirements2.scheme,
|
|
607
|
+
network: requirements2.network,
|
|
608
|
+
maxAmountRequired: requirements2.maxAmountRequired,
|
|
609
|
+
asset: requirements2.asset,
|
|
610
|
+
payTo: requirements2.payTo,
|
|
611
|
+
resource: requirements2.resource || req.url,
|
|
612
|
+
description: requirements2.description,
|
|
613
|
+
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
614
|
+
};
|
|
615
|
+
} else {
|
|
616
|
+
return {
|
|
617
|
+
scheme: requirements2.scheme,
|
|
618
|
+
network: requirements2.network,
|
|
619
|
+
amount: requirements2.amount,
|
|
620
|
+
asset: requirements2.asset,
|
|
621
|
+
payTo: requirements2.payTo,
|
|
622
|
+
maxTimeoutSeconds: requirements2.maxTimeoutSeconds,
|
|
623
|
+
extra: { ...extra, ...requirements2.extra }
|
|
624
|
+
};
|
|
625
|
+
}
|
|
553
626
|
});
|
|
554
627
|
res.status(402).json({
|
|
555
628
|
x402Version: 2,
|
|
@@ -596,13 +669,14 @@ function createPaymentMiddleware(config) {
|
|
|
596
669
|
if (config.refundProtection) {
|
|
597
670
|
const originalNext = next;
|
|
598
671
|
const refundConfig = config.refundProtection;
|
|
672
|
+
const refundFacilitatorUrl = refundConfig.facilitatorUrl || facilitator.url;
|
|
599
673
|
next = async (error) => {
|
|
600
674
|
if (error) {
|
|
601
675
|
const err = error instanceof Error ? error : new Error(String(error));
|
|
602
676
|
if (!refundConfig.shouldReport || refundConfig.shouldReport(err)) {
|
|
603
677
|
try {
|
|
604
678
|
const result = await reportFailure({
|
|
605
|
-
facilitatorUrl:
|
|
679
|
+
facilitatorUrl: refundFacilitatorUrl,
|
|
606
680
|
apiKey: refundConfig.apiKey,
|
|
607
681
|
originalTxHash: paymentContext.transactionHash,
|
|
608
682
|
userWallet: paymentContext.userWallet,
|
|
@@ -647,16 +721,28 @@ function honoPaymentMiddleware(config) {
|
|
|
647
721
|
if (config.refundProtection) {
|
|
648
722
|
extra.supportsRefunds = true;
|
|
649
723
|
}
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
724
|
+
if ("maxAmountRequired" in requirements2) {
|
|
725
|
+
return {
|
|
726
|
+
scheme: requirements2.scheme,
|
|
727
|
+
network: requirements2.network,
|
|
728
|
+
maxAmountRequired: requirements2.maxAmountRequired,
|
|
729
|
+
asset: requirements2.asset,
|
|
730
|
+
payTo: requirements2.payTo,
|
|
731
|
+
resource: requirements2.resource || c.req.url,
|
|
732
|
+
description: requirements2.description,
|
|
733
|
+
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
734
|
+
};
|
|
735
|
+
} else {
|
|
736
|
+
return {
|
|
737
|
+
scheme: requirements2.scheme,
|
|
738
|
+
network: requirements2.network,
|
|
739
|
+
amount: requirements2.amount,
|
|
740
|
+
asset: requirements2.asset,
|
|
741
|
+
payTo: requirements2.payTo,
|
|
742
|
+
maxTimeoutSeconds: requirements2.maxTimeoutSeconds,
|
|
743
|
+
extra: { ...extra, ...requirements2.extra }
|
|
744
|
+
};
|
|
745
|
+
}
|
|
660
746
|
});
|
|
661
747
|
return c.json({
|
|
662
748
|
x402Version: 2,
|
|
@@ -694,6 +780,7 @@ function honoPaymentMiddleware(config) {
|
|
|
694
780
|
c.set("paymentContext", paymentContext);
|
|
695
781
|
if (config.refundProtection) {
|
|
696
782
|
const refundConfig = config.refundProtection;
|
|
783
|
+
const refundFacilitatorUrl = refundConfig.facilitatorUrl || facilitator.url;
|
|
697
784
|
try {
|
|
698
785
|
await next();
|
|
699
786
|
} catch (error) {
|
|
@@ -701,7 +788,7 @@ function honoPaymentMiddleware(config) {
|
|
|
701
788
|
if (!refundConfig.shouldReport || refundConfig.shouldReport(err)) {
|
|
702
789
|
try {
|
|
703
790
|
const result = await reportFailure({
|
|
704
|
-
facilitatorUrl:
|
|
791
|
+
facilitatorUrl: refundFacilitatorUrl,
|
|
705
792
|
apiKey: refundConfig.apiKey,
|
|
706
793
|
originalTxHash: paymentContext.transactionHash,
|
|
707
794
|
userWallet: paymentContext.userWallet,
|
|
@@ -737,6 +824,7 @@ export {
|
|
|
737
824
|
OpenFacilitator,
|
|
738
825
|
SettlementError,
|
|
739
826
|
VerificationError,
|
|
827
|
+
assertNever,
|
|
740
828
|
createDefaultFacilitator,
|
|
741
829
|
createPaymentContext,
|
|
742
830
|
createPaymentMiddleware,
|
|
@@ -747,10 +835,17 @@ export {
|
|
|
747
835
|
getMainnets,
|
|
748
836
|
getNetwork,
|
|
749
837
|
getNetworkType,
|
|
838
|
+
getSchemeNetwork,
|
|
750
839
|
getTestnets,
|
|
840
|
+
getVersion,
|
|
841
|
+
getVersionSafe,
|
|
751
842
|
honoPaymentMiddleware,
|
|
752
843
|
honoRefundMiddleware,
|
|
753
844
|
isPaymentPayload,
|
|
845
|
+
isPaymentPayloadV1,
|
|
846
|
+
isPaymentPayloadV2,
|
|
847
|
+
isPaymentRequirementsV1,
|
|
848
|
+
isPaymentRequirementsV2,
|
|
754
849
|
isValidNetwork,
|
|
755
850
|
reportFailure,
|
|
756
851
|
toV1NetworkId,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openfacilitator/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "TypeScript SDK for x402 payment facilitation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
13
13
|
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
14
14
|
"test": "vitest",
|
|
15
|
-
"lint": "
|
|
15
|
+
"lint": "tsc --noEmit",
|
|
16
16
|
"clean": "rm -rf dist"
|
|
17
17
|
},
|
|
18
18
|
"keywords": [
|