@openfacilitator/sdk 0.6.2 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +191 -0
- package/dist/index.d.mts +138 -17
- package/dist/index.d.ts +138 -17
- package/dist/index.js +133 -28
- package/dist/index.mjs +125 -28
- package/package.json +9 -9
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2024 OpenFacilitator Contributors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
|
191
|
+
|
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
|
|
@@ -516,13 +640,10 @@ declare function createPaymentContext(settleResponse: {
|
|
|
516
640
|
transaction: string;
|
|
517
641
|
payer: string;
|
|
518
642
|
network: string;
|
|
519
|
-
}, paymentPayload: {
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
asset: string;
|
|
524
|
-
};
|
|
525
|
-
};
|
|
643
|
+
}, paymentPayload: Record<string, unknown>, requirements?: {
|
|
644
|
+
maxAmountRequired?: string;
|
|
645
|
+
amount?: string;
|
|
646
|
+
asset?: string;
|
|
526
647
|
}): PaymentContext;
|
|
527
648
|
interface PaymentMiddlewareConfig {
|
|
528
649
|
/** Facilitator instance or URL */
|
|
@@ -623,4 +744,4 @@ declare function honoPaymentMiddleware(config: HonoPaymentConfig): (c: HonoConte
|
|
|
623
744
|
json: (body: unknown, status?: number) => Response;
|
|
624
745
|
}, next: () => Promise<void>) => Promise<Response | undefined>;
|
|
625
746
|
|
|
626
|
-
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 };
|
|
747
|
+
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 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
|
|
@@ -516,13 +640,10 @@ declare function createPaymentContext(settleResponse: {
|
|
|
516
640
|
transaction: string;
|
|
517
641
|
payer: string;
|
|
518
642
|
network: string;
|
|
519
|
-
}, paymentPayload: {
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
asset: string;
|
|
524
|
-
};
|
|
525
|
-
};
|
|
643
|
+
}, paymentPayload: Record<string, unknown>, requirements?: {
|
|
644
|
+
maxAmountRequired?: string;
|
|
645
|
+
amount?: string;
|
|
646
|
+
asset?: string;
|
|
526
647
|
}): PaymentContext;
|
|
527
648
|
interface PaymentMiddlewareConfig {
|
|
528
649
|
/** Facilitator instance or URL */
|
|
@@ -623,4 +744,4 @@ declare function honoPaymentMiddleware(config: HonoPaymentConfig): (c: HonoConte
|
|
|
623
744
|
json: (body: unknown, status?: number) => Response;
|
|
624
745
|
}, next: () => Promise<void>) => Promise<Response | undefined>;
|
|
625
746
|
|
|
626
|
-
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 };
|
|
747
|
+
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 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
|
};
|
|
@@ -559,12 +628,16 @@ function honoRefundMiddleware(config) {
|
|
|
559
628
|
}
|
|
560
629
|
};
|
|
561
630
|
}
|
|
562
|
-
function createPaymentContext(settleResponse, paymentPayload) {
|
|
631
|
+
function createPaymentContext(settleResponse, paymentPayload, requirements) {
|
|
632
|
+
const payload = paymentPayload.payload;
|
|
633
|
+
const authorization = payload?.authorization;
|
|
634
|
+
const amount = authorization?.amount || payload?.amount || requirements?.amount || requirements?.maxAmountRequired || "0";
|
|
635
|
+
const asset = authorization?.asset || payload?.asset || requirements?.asset || "";
|
|
563
636
|
return {
|
|
564
637
|
transactionHash: settleResponse.transaction,
|
|
565
638
|
userWallet: settleResponse.payer,
|
|
566
|
-
amount
|
|
567
|
-
asset
|
|
639
|
+
amount,
|
|
640
|
+
asset,
|
|
568
641
|
network: settleResponse.network
|
|
569
642
|
};
|
|
570
643
|
}
|
|
@@ -587,16 +660,28 @@ function createPaymentMiddleware(config) {
|
|
|
587
660
|
if (config.refundProtection) {
|
|
588
661
|
extra.supportsRefunds = true;
|
|
589
662
|
}
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
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
|
+
}
|
|
600
685
|
});
|
|
601
686
|
res.status(402).json({
|
|
602
687
|
x402Version: 2,
|
|
@@ -635,7 +720,7 @@ function createPaymentMiddleware(config) {
|
|
|
635
720
|
});
|
|
636
721
|
return;
|
|
637
722
|
}
|
|
638
|
-
const paymentContext = createPaymentContext(settleResult, paymentPayload);
|
|
723
|
+
const paymentContext = createPaymentContext(settleResult, paymentPayload, requirements);
|
|
639
724
|
req.paymentContext = paymentContext;
|
|
640
725
|
if (res.locals) {
|
|
641
726
|
res.locals.paymentContext = paymentContext;
|
|
@@ -694,16 +779,28 @@ function honoPaymentMiddleware(config) {
|
|
|
694
779
|
if (config.refundProtection) {
|
|
695
780
|
extra.supportsRefunds = true;
|
|
696
781
|
}
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
782
|
+
if ("maxAmountRequired" in requirements2) {
|
|
783
|
+
return {
|
|
784
|
+
scheme: requirements2.scheme,
|
|
785
|
+
network: requirements2.network,
|
|
786
|
+
maxAmountRequired: requirements2.maxAmountRequired,
|
|
787
|
+
asset: requirements2.asset,
|
|
788
|
+
payTo: requirements2.payTo,
|
|
789
|
+
resource: requirements2.resource || c.req.url,
|
|
790
|
+
description: requirements2.description,
|
|
791
|
+
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
792
|
+
};
|
|
793
|
+
} else {
|
|
794
|
+
return {
|
|
795
|
+
scheme: requirements2.scheme,
|
|
796
|
+
network: requirements2.network,
|
|
797
|
+
amount: requirements2.amount,
|
|
798
|
+
asset: requirements2.asset,
|
|
799
|
+
payTo: requirements2.payTo,
|
|
800
|
+
maxTimeoutSeconds: requirements2.maxTimeoutSeconds,
|
|
801
|
+
extra: { ...extra, ...requirements2.extra }
|
|
802
|
+
};
|
|
803
|
+
}
|
|
707
804
|
});
|
|
708
805
|
return c.json({
|
|
709
806
|
x402Version: 2,
|
|
@@ -737,7 +834,7 @@ function honoPaymentMiddleware(config) {
|
|
|
737
834
|
reason: settleResult.errorReason
|
|
738
835
|
}, 402);
|
|
739
836
|
}
|
|
740
|
-
const paymentContext = createPaymentContext(settleResult, paymentPayload);
|
|
837
|
+
const paymentContext = createPaymentContext(settleResult, paymentPayload, requirements);
|
|
741
838
|
c.set("paymentContext", paymentContext);
|
|
742
839
|
if (config.refundProtection) {
|
|
743
840
|
const refundConfig = config.refundProtection;
|
|
@@ -785,6 +882,7 @@ function honoPaymentMiddleware(config) {
|
|
|
785
882
|
OpenFacilitator,
|
|
786
883
|
SettlementError,
|
|
787
884
|
VerificationError,
|
|
885
|
+
assertNever,
|
|
788
886
|
createDefaultFacilitator,
|
|
789
887
|
createPaymentContext,
|
|
790
888
|
createPaymentMiddleware,
|
|
@@ -795,10 +893,17 @@ function honoPaymentMiddleware(config) {
|
|
|
795
893
|
getMainnets,
|
|
796
894
|
getNetwork,
|
|
797
895
|
getNetworkType,
|
|
896
|
+
getSchemeNetwork,
|
|
798
897
|
getTestnets,
|
|
898
|
+
getVersion,
|
|
899
|
+
getVersionSafe,
|
|
799
900
|
honoPaymentMiddleware,
|
|
800
901
|
honoRefundMiddleware,
|
|
801
902
|
isPaymentPayload,
|
|
903
|
+
isPaymentPayloadV1,
|
|
904
|
+
isPaymentPayloadV2,
|
|
905
|
+
isPaymentRequirementsV1,
|
|
906
|
+
isPaymentRequirementsV2,
|
|
802
907
|
isValidNetwork,
|
|
803
908
|
reportFailure,
|
|
804
909
|
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
|
};
|
|
@@ -508,12 +569,16 @@ function honoRefundMiddleware(config) {
|
|
|
508
569
|
}
|
|
509
570
|
};
|
|
510
571
|
}
|
|
511
|
-
function createPaymentContext(settleResponse, paymentPayload) {
|
|
572
|
+
function createPaymentContext(settleResponse, paymentPayload, requirements) {
|
|
573
|
+
const payload = paymentPayload.payload;
|
|
574
|
+
const authorization = payload?.authorization;
|
|
575
|
+
const amount = authorization?.amount || payload?.amount || requirements?.amount || requirements?.maxAmountRequired || "0";
|
|
576
|
+
const asset = authorization?.asset || payload?.asset || requirements?.asset || "";
|
|
512
577
|
return {
|
|
513
578
|
transactionHash: settleResponse.transaction,
|
|
514
579
|
userWallet: settleResponse.payer,
|
|
515
|
-
amount
|
|
516
|
-
asset
|
|
580
|
+
amount,
|
|
581
|
+
asset,
|
|
517
582
|
network: settleResponse.network
|
|
518
583
|
};
|
|
519
584
|
}
|
|
@@ -536,16 +601,28 @@ function createPaymentMiddleware(config) {
|
|
|
536
601
|
if (config.refundProtection) {
|
|
537
602
|
extra.supportsRefunds = true;
|
|
538
603
|
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
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
|
+
}
|
|
549
626
|
});
|
|
550
627
|
res.status(402).json({
|
|
551
628
|
x402Version: 2,
|
|
@@ -584,7 +661,7 @@ function createPaymentMiddleware(config) {
|
|
|
584
661
|
});
|
|
585
662
|
return;
|
|
586
663
|
}
|
|
587
|
-
const paymentContext = createPaymentContext(settleResult, paymentPayload);
|
|
664
|
+
const paymentContext = createPaymentContext(settleResult, paymentPayload, requirements);
|
|
588
665
|
req.paymentContext = paymentContext;
|
|
589
666
|
if (res.locals) {
|
|
590
667
|
res.locals.paymentContext = paymentContext;
|
|
@@ -643,16 +720,28 @@ function honoPaymentMiddleware(config) {
|
|
|
643
720
|
if (config.refundProtection) {
|
|
644
721
|
extra.supportsRefunds = true;
|
|
645
722
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
723
|
+
if ("maxAmountRequired" in requirements2) {
|
|
724
|
+
return {
|
|
725
|
+
scheme: requirements2.scheme,
|
|
726
|
+
network: requirements2.network,
|
|
727
|
+
maxAmountRequired: requirements2.maxAmountRequired,
|
|
728
|
+
asset: requirements2.asset,
|
|
729
|
+
payTo: requirements2.payTo,
|
|
730
|
+
resource: requirements2.resource || c.req.url,
|
|
731
|
+
description: requirements2.description,
|
|
732
|
+
...Object.keys(extra).length > 0 ? { extra } : {}
|
|
733
|
+
};
|
|
734
|
+
} else {
|
|
735
|
+
return {
|
|
736
|
+
scheme: requirements2.scheme,
|
|
737
|
+
network: requirements2.network,
|
|
738
|
+
amount: requirements2.amount,
|
|
739
|
+
asset: requirements2.asset,
|
|
740
|
+
payTo: requirements2.payTo,
|
|
741
|
+
maxTimeoutSeconds: requirements2.maxTimeoutSeconds,
|
|
742
|
+
extra: { ...extra, ...requirements2.extra }
|
|
743
|
+
};
|
|
744
|
+
}
|
|
656
745
|
});
|
|
657
746
|
return c.json({
|
|
658
747
|
x402Version: 2,
|
|
@@ -686,7 +775,7 @@ function honoPaymentMiddleware(config) {
|
|
|
686
775
|
reason: settleResult.errorReason
|
|
687
776
|
}, 402);
|
|
688
777
|
}
|
|
689
|
-
const paymentContext = createPaymentContext(settleResult, paymentPayload);
|
|
778
|
+
const paymentContext = createPaymentContext(settleResult, paymentPayload, requirements);
|
|
690
779
|
c.set("paymentContext", paymentContext);
|
|
691
780
|
if (config.refundProtection) {
|
|
692
781
|
const refundConfig = config.refundProtection;
|
|
@@ -733,6 +822,7 @@ export {
|
|
|
733
822
|
OpenFacilitator,
|
|
734
823
|
SettlementError,
|
|
735
824
|
VerificationError,
|
|
825
|
+
assertNever,
|
|
736
826
|
createDefaultFacilitator,
|
|
737
827
|
createPaymentContext,
|
|
738
828
|
createPaymentMiddleware,
|
|
@@ -743,10 +833,17 @@ export {
|
|
|
743
833
|
getMainnets,
|
|
744
834
|
getNetwork,
|
|
745
835
|
getNetworkType,
|
|
836
|
+
getSchemeNetwork,
|
|
746
837
|
getTestnets,
|
|
838
|
+
getVersion,
|
|
839
|
+
getVersionSafe,
|
|
747
840
|
honoPaymentMiddleware,
|
|
748
841
|
honoRefundMiddleware,
|
|
749
842
|
isPaymentPayload,
|
|
843
|
+
isPaymentPayloadV1,
|
|
844
|
+
isPaymentPayloadV2,
|
|
845
|
+
isPaymentRequirementsV1,
|
|
846
|
+
isPaymentRequirementsV2,
|
|
750
847
|
isValidNetwork,
|
|
751
848
|
reportFailure,
|
|
752
849
|
toV1NetworkId,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openfacilitator/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "TypeScript SDK for x402 payment facilitation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -8,13 +8,6 @@
|
|
|
8
8
|
"files": [
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
|
-
"scripts": {
|
|
12
|
-
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
13
|
-
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
14
|
-
"test": "vitest",
|
|
15
|
-
"lint": "eslint src/",
|
|
16
|
-
"clean": "rm -rf dist"
|
|
17
|
-
},
|
|
18
11
|
"keywords": [
|
|
19
12
|
"x402",
|
|
20
13
|
"payments",
|
|
@@ -37,5 +30,12 @@
|
|
|
37
30
|
},
|
|
38
31
|
"engines": {
|
|
39
32
|
"node": ">=18"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
36
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
37
|
+
"test": "vitest",
|
|
38
|
+
"lint": "tsc --noEmit",
|
|
39
|
+
"clean": "rm -rf dist"
|
|
40
40
|
}
|
|
41
|
-
}
|
|
41
|
+
}
|