s402 0.1.1 → 0.1.3
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/CHANGELOG.md +15 -0
- package/LICENSE +191 -21
- package/README.md +4 -1
- package/SECURITY.md +3 -3
- package/dist/http.d.mts +15 -2
- package/dist/http.mjs +64 -4
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +37 -7
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.1.1] - 2026-02-16
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- Facilitator `verify()` and `settle()` now have the same defense-in-depth guards as `process()`:
|
|
13
|
+
- Reject non-number `expiresAt` values (prevents silent bypass with string types)
|
|
14
|
+
- Reject payload schemes not in `requirements.accepts` (scheme-mismatch guard)
|
|
15
|
+
- `protocolFeeBps` validation now requires an integer (rejects `50.5`)
|
|
16
|
+
- Sub-object fields (stream, escrow, unlock, prepaid, mandate) are now stripped of unknown keys at the trust boundary, matching the top-level field stripping behavior
|
|
17
|
+
- `process()` now catches exceptions thrown by `scheme.settle()` and returns them as error results instead of propagating unhandled
|
|
18
|
+
|
|
19
|
+
### Added
|
|
20
|
+
|
|
21
|
+
- `isValidU64Amount()` — validates amount strings fit in a Sui u64 (format + magnitude check). The existing `isValidAmount()` remains format-only for chain-agnostic use.
|
|
22
|
+
|
|
8
23
|
## [0.1.0] - 2026-02-15
|
|
9
24
|
|
|
10
25
|
### Added
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,191 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2026 Swee Group LLC
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# s402
|
|
2
2
|
|
|
3
|
+
[](https://github.com/s402-protocol/core/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/s402)
|
|
5
|
+
|
|
3
6
|
**Sui-native HTTP 402 protocol.** Atomic settlement via Sui's Programmable Transaction Blocks (PTBs). Includes an optional compat layer (`s402/compat`) for normalizing x402 input.
|
|
4
7
|
|
|
5
8
|
```bash
|
|
@@ -319,4 +322,4 @@ const requirements: s402PaymentRequirements = {
|
|
|
319
322
|
|
|
320
323
|
## License
|
|
321
324
|
|
|
322
|
-
|
|
325
|
+
Apache-2.0 — see [LICENSE](./LICENSE) for details.
|
package/SECURITY.md
CHANGED
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
|
|
5
5
|
**Please do not open public issues for security vulnerabilities.**
|
|
6
6
|
|
|
7
|
-
If you discover a security issue in s402, please report it privately:
|
|
7
|
+
If you discover a security issue in s402, please report it privately via either method:
|
|
8
8
|
|
|
9
|
-
- **Email:** dannydevs@proton.me
|
|
10
|
-
- **
|
|
9
|
+
- **Email:** dannydevs@proton.me (subject: `[s402 security] <brief description>`)
|
|
10
|
+
- **GitHub:** [Open a private security advisory](https://github.com/s402-protocol/core/security/advisories/new)
|
|
11
11
|
|
|
12
12
|
You will receive an acknowledgment within 48 hours. We aim to provide a fix or mitigation plan within 7 days of confirmation.
|
|
13
13
|
|
package/dist/http.d.mts
CHANGED
|
@@ -20,10 +20,15 @@ declare function pickSettleResponseFields(obj: Record<string, unknown>): s402Set
|
|
|
20
20
|
/** Decode settlement response from the `payment-response` header */
|
|
21
21
|
declare function decodeSettleResponse(header: string): s402SettleResponse;
|
|
22
22
|
/**
|
|
23
|
-
* Check that a string represents a canonical non-negative integer
|
|
23
|
+
* Check that a string represents a canonical non-negative integer.
|
|
24
24
|
* Rejects leading zeros ("007"), empty strings, negatives, decimals.
|
|
25
25
|
* Accepts "0" as the only zero representation.
|
|
26
26
|
*
|
|
27
|
+
* NOTE: This is a **format-only** check — it validates the string is a well-formed
|
|
28
|
+
* non-negative integer but does NOT enforce magnitude bounds. Arbitrarily large
|
|
29
|
+
* integers (e.g. 100+ digits) pass this check. For Sui-specific u64 validation,
|
|
30
|
+
* use `isValidU64Amount()` which also checks that the value fits in a u64.
|
|
31
|
+
*
|
|
27
32
|
* A-13 (Semantic gap): "0" passes validation because it's a valid u64 on-chain.
|
|
28
33
|
* However, amount="0" in payment requirements is semantically ambiguous — it could
|
|
29
34
|
* mean "free" or be a misconfiguration. The s402 wire format intentionally allows it
|
|
@@ -32,6 +37,14 @@ declare function decodeSettleResponse(header: string): s402SettleResponse;
|
|
|
32
37
|
* not at the protocol level.
|
|
33
38
|
*/
|
|
34
39
|
declare function isValidAmount(s: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Check that a string represents a valid Sui u64 amount.
|
|
42
|
+
* Like `isValidAmount` but also rejects values exceeding u64 max (2^64 - 1).
|
|
43
|
+
*
|
|
44
|
+
* Use this in scheme implementations that target Sui's u64 amounts (MIST, etc.).
|
|
45
|
+
* The wire-format validator uses `isValidAmount` (format-only) to stay chain-agnostic.
|
|
46
|
+
*/
|
|
47
|
+
declare function isValidU64Amount(s: string): boolean;
|
|
35
48
|
/**
|
|
36
49
|
* Validate mandate requirements sub-object.
|
|
37
50
|
* Mandate is protocol-level (used for authorization decisions), so we validate fully.
|
|
@@ -75,4 +88,4 @@ declare function detectProtocol(headers: Headers): 's402' | 'x402' | 'unknown';
|
|
|
75
88
|
*/
|
|
76
89
|
declare function extractRequirementsFromResponse(response: Response): s402PaymentRequirements | null;
|
|
77
90
|
//#endregion
|
|
78
|
-
export { decodePaymentPayload, decodePaymentRequired, decodeSettleResponse, detectProtocol, encodePaymentPayload, encodePaymentRequired, encodeSettleResponse, extractRequirementsFromResponse, isValidAmount, pickPayloadFields, pickRequirementsFields, pickSettleResponseFields, validateEscrowShape, validateMandateShape, validatePrepaidShape, validateRequirementsShape, validateStreamShape, validateSubObjects, validateUnlockShape };
|
|
91
|
+
export { decodePaymentPayload, decodePaymentRequired, decodeSettleResponse, detectProtocol, encodePaymentPayload, encodePaymentRequired, encodeSettleResponse, extractRequirementsFromResponse, isValidAmount, isValidU64Amount, pickPayloadFields, pickRequirementsFields, pickSettleResponseFields, validateEscrowShape, validateMandateShape, validatePrepaidShape, validateRequirementsShape, validateStreamShape, validateSubObjects, validateUnlockShape };
|
package/dist/http.mjs
CHANGED
|
@@ -56,10 +56,50 @@ const S402_REQUIREMENTS_KEYS = new Set([
|
|
|
56
56
|
"prepaid",
|
|
57
57
|
"extensions"
|
|
58
58
|
]);
|
|
59
|
+
/** Known keys for each sub-object type — used to strip extra keys at the trust boundary. */
|
|
60
|
+
const S402_SUB_OBJECT_KEYS = {
|
|
61
|
+
mandate: new Set([
|
|
62
|
+
"required",
|
|
63
|
+
"minPerTx",
|
|
64
|
+
"coinType"
|
|
65
|
+
]),
|
|
66
|
+
stream: new Set([
|
|
67
|
+
"ratePerSecond",
|
|
68
|
+
"budgetCap",
|
|
69
|
+
"minDeposit",
|
|
70
|
+
"streamSetupUrl"
|
|
71
|
+
]),
|
|
72
|
+
escrow: new Set([
|
|
73
|
+
"seller",
|
|
74
|
+
"arbiter",
|
|
75
|
+
"deadlineMs"
|
|
76
|
+
]),
|
|
77
|
+
unlock: new Set([
|
|
78
|
+
"encryptionId",
|
|
79
|
+
"walrusBlobId",
|
|
80
|
+
"encryptionPackageId"
|
|
81
|
+
]),
|
|
82
|
+
prepaid: new Set([
|
|
83
|
+
"ratePerCall",
|
|
84
|
+
"maxCalls",
|
|
85
|
+
"minDeposit",
|
|
86
|
+
"withdrawalDelayMs"
|
|
87
|
+
])
|
|
88
|
+
};
|
|
89
|
+
/** Strip unknown keys from a sub-object, returning a clean copy. */
|
|
90
|
+
function pickSubObjectFields(key, value) {
|
|
91
|
+
const allowedKeys = S402_SUB_OBJECT_KEYS[key];
|
|
92
|
+
if (!allowedKeys || value == null || typeof value !== "object" || Array.isArray(value)) return value;
|
|
93
|
+
const obj = value;
|
|
94
|
+
const clean = {};
|
|
95
|
+
for (const k of allowedKeys) if (k in obj) clean[k] = obj[k];
|
|
96
|
+
return clean;
|
|
97
|
+
}
|
|
59
98
|
/** Return a clean object with only known s402 requirement fields. */
|
|
60
99
|
function pickRequirementsFields(obj) {
|
|
61
100
|
const result = {};
|
|
62
|
-
for (const key of S402_REQUIREMENTS_KEYS) if (key in obj) result[key] = obj[key];
|
|
101
|
+
for (const key of S402_REQUIREMENTS_KEYS) if (key in obj) if (key in S402_SUB_OBJECT_KEYS) result[key] = pickSubObjectFields(key, obj[key]);
|
|
102
|
+
else result[key] = obj[key];
|
|
63
103
|
return result;
|
|
64
104
|
}
|
|
65
105
|
/** Decode payment requirements from the `payment-required` header */
|
|
@@ -172,10 +212,15 @@ const VALID_SCHEMES = new Set([
|
|
|
172
212
|
"prepaid"
|
|
173
213
|
]);
|
|
174
214
|
/**
|
|
175
|
-
* Check that a string represents a canonical non-negative integer
|
|
215
|
+
* Check that a string represents a canonical non-negative integer.
|
|
176
216
|
* Rejects leading zeros ("007"), empty strings, negatives, decimals.
|
|
177
217
|
* Accepts "0" as the only zero representation.
|
|
178
218
|
*
|
|
219
|
+
* NOTE: This is a **format-only** check — it validates the string is a well-formed
|
|
220
|
+
* non-negative integer but does NOT enforce magnitude bounds. Arbitrarily large
|
|
221
|
+
* integers (e.g. 100+ digits) pass this check. For Sui-specific u64 validation,
|
|
222
|
+
* use `isValidU64Amount()` which also checks that the value fits in a u64.
|
|
223
|
+
*
|
|
179
224
|
* A-13 (Semantic gap): "0" passes validation because it's a valid u64 on-chain.
|
|
180
225
|
* However, amount="0" in payment requirements is semantically ambiguous — it could
|
|
181
226
|
* mean "free" or be a misconfiguration. The s402 wire format intentionally allows it
|
|
@@ -186,6 +231,21 @@ const VALID_SCHEMES = new Set([
|
|
|
186
231
|
function isValidAmount(s) {
|
|
187
232
|
return /^(0|[1-9][0-9]*)$/.test(s);
|
|
188
233
|
}
|
|
234
|
+
/** Maximum value representable as a Sui u64: 2^64 - 1 */
|
|
235
|
+
const U64_MAX = "18446744073709551615";
|
|
236
|
+
/**
|
|
237
|
+
* Check that a string represents a valid Sui u64 amount.
|
|
238
|
+
* Like `isValidAmount` but also rejects values exceeding u64 max (2^64 - 1).
|
|
239
|
+
*
|
|
240
|
+
* Use this in scheme implementations that target Sui's u64 amounts (MIST, etc.).
|
|
241
|
+
* The wire-format validator uses `isValidAmount` (format-only) to stay chain-agnostic.
|
|
242
|
+
*/
|
|
243
|
+
function isValidU64Amount(s) {
|
|
244
|
+
if (!isValidAmount(s)) return false;
|
|
245
|
+
if (s.length > 20) return false;
|
|
246
|
+
if (s.length < 20) return true;
|
|
247
|
+
return s <= U64_MAX;
|
|
248
|
+
}
|
|
189
249
|
/** Helper: assert obj is a plain object (not null/array/primitive). */
|
|
190
250
|
function assertPlainObject(value, label) {
|
|
191
251
|
if (value == null || typeof value !== "object" || Array.isArray(value)) throw new s402Error("INVALID_PAYLOAD", `${label} must be a plain object, got ${Array.isArray(value) ? "array" : typeof value}`);
|
|
@@ -289,7 +349,7 @@ function validateRequirementsShape(obj) {
|
|
|
289
349
|
const accepts = record.accepts;
|
|
290
350
|
for (const scheme of accepts) if (typeof scheme !== "string") throw new s402Error("INVALID_PAYLOAD", `Invalid entry in accepts array: expected string, got ${typeof scheme}`);
|
|
291
351
|
if (record.protocolFeeBps !== void 0) {
|
|
292
|
-
if (typeof record.protocolFeeBps !== "number" || !Number.isFinite(record.protocolFeeBps) || record.protocolFeeBps < 0 || record.protocolFeeBps > 1e4) throw new s402Error("INVALID_PAYLOAD", `protocolFeeBps must be
|
|
352
|
+
if (typeof record.protocolFeeBps !== "number" || !Number.isFinite(record.protocolFeeBps) || !Number.isInteger(record.protocolFeeBps) || record.protocolFeeBps < 0 || record.protocolFeeBps > 1e4) throw new s402Error("INVALID_PAYLOAD", `protocolFeeBps must be an integer between 0 and 10000, got ${record.protocolFeeBps}`);
|
|
293
353
|
}
|
|
294
354
|
if (record.expiresAt !== void 0) {
|
|
295
355
|
if (typeof record.expiresAt !== "number" || !Number.isFinite(record.expiresAt)) throw new s402Error("INVALID_PAYLOAD", `expiresAt must be a finite number (Unix timestamp ms), got ${typeof record.expiresAt}`);
|
|
@@ -352,4 +412,4 @@ function extractRequirementsFromResponse(response) {
|
|
|
352
412
|
}
|
|
353
413
|
|
|
354
414
|
//#endregion
|
|
355
|
-
export { decodePaymentPayload, decodePaymentRequired, decodeSettleResponse, detectProtocol, encodePaymentPayload, encodePaymentRequired, encodeSettleResponse, extractRequirementsFromResponse, isValidAmount, pickPayloadFields, pickRequirementsFields, pickSettleResponseFields, validateEscrowShape, validateMandateShape, validatePrepaidShape, validateRequirementsShape, validateStreamShape, validateSubObjects, validateUnlockShape };
|
|
415
|
+
export { decodePaymentPayload, decodePaymentRequired, decodeSettleResponse, detectProtocol, encodePaymentPayload, encodePaymentRequired, encodeSettleResponse, extractRequirementsFromResponse, isValidAmount, isValidU64Amount, pickPayloadFields, pickRequirementsFields, pickSettleResponseFields, validateEscrowShape, validateMandateShape, validatePrepaidShape, validateRequirementsShape, validateStreamShape, validateSubObjects, validateUnlockShape };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createS402Error, s402Error, s402ErrorCode, s402ErrorCodeType, s402ErrorInfo } from "./errors.mjs";
|
|
2
2
|
import { S402_HEADERS, S402_VERSION, s402Discovery, s402EscrowExtra, s402EscrowPayload, s402ExactPayload, s402Mandate, s402MandateRequirements, s402PaymentPayload, s402PaymentPayloadBase, s402PaymentRequirements, s402PaymentSession, s402PrepaidExtra, s402PrepaidPayload, s402RegistryQuery, s402Scheme, s402ServiceEntry, s402SettleResponse, s402SettlementMode, s402StreamExtra, s402StreamPayload, s402UnlockExtra, s402UnlockPayload, s402VerifyResponse } from "./types.mjs";
|
|
3
|
-
import { decodePaymentPayload, decodePaymentRequired, decodeSettleResponse, detectProtocol, encodePaymentPayload, encodePaymentRequired, encodeSettleResponse, extractRequirementsFromResponse, isValidAmount, validateRequirementsShape } from "./http.mjs";
|
|
3
|
+
import { decodePaymentPayload, decodePaymentRequired, decodeSettleResponse, detectProtocol, encodePaymentPayload, encodePaymentRequired, encodeSettleResponse, extractRequirementsFromResponse, isValidAmount, isValidU64Amount, validateRequirementsShape } from "./http.mjs";
|
|
4
4
|
|
|
5
5
|
//#region src/scheme.d.ts
|
|
6
6
|
/** Implemented by each scheme on the client side */
|
|
@@ -128,12 +128,12 @@ declare class s402Facilitator {
|
|
|
128
128
|
register(network: string, scheme: s402FacilitatorScheme): this;
|
|
129
129
|
/**
|
|
130
130
|
* Verify a payment payload by dispatching to the correct scheme.
|
|
131
|
-
* Includes expiration guard
|
|
131
|
+
* Includes expiration guard and scheme-mismatch check.
|
|
132
132
|
*/
|
|
133
133
|
verify(payload: s402PaymentPayload, requirements: s402PaymentRequirements): Promise<s402VerifyResponse>;
|
|
134
134
|
/**
|
|
135
135
|
* Settle a payment by dispatching to the correct scheme.
|
|
136
|
-
* Includes expiration guard
|
|
136
|
+
* Includes expiration guard and scheme-mismatch check.
|
|
137
137
|
*/
|
|
138
138
|
settle(payload: s402PaymentPayload, requirements: s402PaymentRequirements): Promise<s402SettleResponse>;
|
|
139
139
|
/**
|
|
@@ -191,4 +191,4 @@ declare class s402ResourceServer {
|
|
|
191
191
|
process(payload: s402PaymentPayload, requirements: s402PaymentRequirements): Promise<s402SettleResponse>;
|
|
192
192
|
}
|
|
193
193
|
//#endregion
|
|
194
|
-
export { S402_HEADERS, S402_VERSION, createS402Error, decodePaymentPayload, decodePaymentRequired, decodeSettleResponse, detectProtocol, encodePaymentPayload, encodePaymentRequired, encodeSettleResponse, extractRequirementsFromResponse, isValidAmount, s402Client, type s402ClientScheme, type s402DirectScheme, type s402Discovery, s402Error, s402ErrorCode, type s402ErrorCodeType, type s402ErrorInfo, type s402EscrowExtra, type s402EscrowPayload, type s402ExactPayload, s402Facilitator, type s402FacilitatorScheme, type s402Mandate, type s402MandateRequirements, type s402PaymentPayload, type s402PaymentPayloadBase, type s402PaymentRequirements, type s402PaymentSession, type s402PrepaidExtra, type s402PrepaidPayload, type s402RegistryQuery, s402ResourceServer, type s402RouteConfig, type s402Scheme, type s402ServerScheme, type s402ServiceEntry, type s402SettleResponse, type s402SettlementMode, type s402StreamExtra, type s402StreamPayload, type s402UnlockExtra, type s402UnlockPayload, type s402VerifyResponse, validateRequirementsShape };
|
|
194
|
+
export { S402_HEADERS, S402_VERSION, createS402Error, decodePaymentPayload, decodePaymentRequired, decodeSettleResponse, detectProtocol, encodePaymentPayload, encodePaymentRequired, encodeSettleResponse, extractRequirementsFromResponse, isValidAmount, isValidU64Amount, s402Client, type s402ClientScheme, type s402DirectScheme, type s402Discovery, s402Error, s402ErrorCode, type s402ErrorCodeType, type s402ErrorInfo, type s402EscrowExtra, type s402EscrowPayload, type s402ExactPayload, s402Facilitator, type s402FacilitatorScheme, type s402Mandate, type s402MandateRequirements, type s402PaymentPayload, type s402PaymentPayloadBase, type s402PaymentRequirements, type s402PaymentSession, type s402PrepaidExtra, type s402PrepaidPayload, type s402RegistryQuery, s402ResourceServer, type s402RouteConfig, type s402Scheme, type s402ServerScheme, type s402ServiceEntry, type s402SettleResponse, type s402SettlementMode, type s402StreamExtra, type s402StreamPayload, type s402UnlockExtra, type s402UnlockPayload, type s402VerifyResponse, validateRequirementsShape };
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { S402_HEADERS, S402_VERSION } from "./types.mjs";
|
|
2
2
|
import { createS402Error, s402Error, s402ErrorCode } from "./errors.mjs";
|
|
3
|
-
import { decodePaymentPayload, decodePaymentRequired, decodeSettleResponse, detectProtocol, encodePaymentPayload, encodePaymentRequired, encodeSettleResponse, extractRequirementsFromResponse, isValidAmount, validateRequirementsShape } from "./http.mjs";
|
|
3
|
+
import { decodePaymentPayload, decodePaymentRequired, decodeSettleResponse, detectProtocol, encodePaymentPayload, encodePaymentRequired, encodeSettleResponse, extractRequirementsFromResponse, isValidAmount, isValidU64Amount, validateRequirementsShape } from "./http.mjs";
|
|
4
4
|
|
|
5
5
|
//#region src/client.ts
|
|
6
6
|
var s402Client = class {
|
|
@@ -138,29 +138,51 @@ var s402Facilitator = class {
|
|
|
138
138
|
}
|
|
139
139
|
/**
|
|
140
140
|
* Verify a payment payload by dispatching to the correct scheme.
|
|
141
|
-
* Includes expiration guard
|
|
141
|
+
* Includes expiration guard and scheme-mismatch check.
|
|
142
142
|
*/
|
|
143
143
|
async verify(payload, requirements) {
|
|
144
|
-
if (
|
|
144
|
+
if (requirements.expiresAt != null) {
|
|
145
|
+
if (typeof requirements.expiresAt !== "number" || !Number.isFinite(requirements.expiresAt)) return {
|
|
146
|
+
valid: false,
|
|
147
|
+
invalidReason: `Invalid expiresAt value: expected finite number, got ${typeof requirements.expiresAt}`
|
|
148
|
+
};
|
|
145
149
|
if (Date.now() > requirements.expiresAt) return {
|
|
146
150
|
valid: false,
|
|
147
151
|
invalidReason: `Payment requirements expired at ${new Date(requirements.expiresAt).toISOString()}`
|
|
148
152
|
};
|
|
149
153
|
}
|
|
154
|
+
if (requirements.accepts && requirements.accepts.length > 0) {
|
|
155
|
+
if (!requirements.accepts.includes(payload.scheme)) return {
|
|
156
|
+
valid: false,
|
|
157
|
+
invalidReason: `Scheme "${payload.scheme}" is not accepted by these requirements. Accepted: [${requirements.accepts.join(", ")}]`
|
|
158
|
+
};
|
|
159
|
+
}
|
|
150
160
|
return this.resolveScheme(payload.scheme, requirements.network).verify(payload, requirements);
|
|
151
161
|
}
|
|
152
162
|
/**
|
|
153
163
|
* Settle a payment by dispatching to the correct scheme.
|
|
154
|
-
* Includes expiration guard
|
|
164
|
+
* Includes expiration guard and scheme-mismatch check.
|
|
155
165
|
*/
|
|
156
166
|
async settle(payload, requirements) {
|
|
157
|
-
if (
|
|
167
|
+
if (requirements.expiresAt != null) {
|
|
168
|
+
if (typeof requirements.expiresAt !== "number" || !Number.isFinite(requirements.expiresAt)) return {
|
|
169
|
+
success: false,
|
|
170
|
+
error: `Invalid expiresAt value: expected finite number, got ${typeof requirements.expiresAt}`,
|
|
171
|
+
errorCode: "INVALID_PAYLOAD"
|
|
172
|
+
};
|
|
158
173
|
if (Date.now() > requirements.expiresAt) return {
|
|
159
174
|
success: false,
|
|
160
175
|
error: `Payment requirements expired at ${new Date(requirements.expiresAt).toISOString()}`,
|
|
161
176
|
errorCode: "REQUIREMENTS_EXPIRED"
|
|
162
177
|
};
|
|
163
178
|
}
|
|
179
|
+
if (requirements.accepts && requirements.accepts.length > 0) {
|
|
180
|
+
if (!requirements.accepts.includes(payload.scheme)) return {
|
|
181
|
+
success: false,
|
|
182
|
+
error: `Scheme "${payload.scheme}" is not accepted by these requirements. Accepted: [${requirements.accepts.join(", ")}]`,
|
|
183
|
+
errorCode: "SCHEME_NOT_SUPPORTED"
|
|
184
|
+
};
|
|
185
|
+
}
|
|
164
186
|
return this.resolveScheme(payload.scheme, requirements.network).settle(payload, requirements);
|
|
165
187
|
}
|
|
166
188
|
/**
|
|
@@ -203,7 +225,15 @@ var s402Facilitator = class {
|
|
|
203
225
|
error: `Payment requirements expired during verification at ${new Date(requirements.expiresAt).toISOString()}`,
|
|
204
226
|
errorCode: "REQUIREMENTS_EXPIRED"
|
|
205
227
|
};
|
|
206
|
-
|
|
228
|
+
try {
|
|
229
|
+
return await scheme.settle(payload, requirements);
|
|
230
|
+
} catch (e) {
|
|
231
|
+
return {
|
|
232
|
+
success: false,
|
|
233
|
+
error: e instanceof Error ? e.message : "Settlement failed with an unexpected error",
|
|
234
|
+
errorCode: "VERIFICATION_FAILED"
|
|
235
|
+
};
|
|
236
|
+
}
|
|
207
237
|
}
|
|
208
238
|
/**
|
|
209
239
|
* Check if a scheme is supported for a network.
|
|
@@ -228,4 +258,4 @@ var s402Facilitator = class {
|
|
|
228
258
|
};
|
|
229
259
|
|
|
230
260
|
//#endregion
|
|
231
|
-
export { S402_HEADERS, S402_VERSION, createS402Error, decodePaymentPayload, decodePaymentRequired, decodeSettleResponse, detectProtocol, encodePaymentPayload, encodePaymentRequired, encodeSettleResponse, extractRequirementsFromResponse, isValidAmount, s402Client, s402Error, s402ErrorCode, s402Facilitator, s402ResourceServer, validateRequirementsShape };
|
|
261
|
+
export { S402_HEADERS, S402_VERSION, createS402Error, decodePaymentPayload, decodePaymentRequired, decodeSettleResponse, detectProtocol, encodePaymentPayload, encodePaymentRequired, encodeSettleResponse, extractRequirementsFromResponse, isValidAmount, isValidU64Amount, s402Client, s402Error, s402ErrorCode, s402Facilitator, s402ResourceServer, validateRequirementsShape };
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "s402",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "s402 — Sui-native HTTP 402 wire format. Types, HTTP encoding, and scheme registry for five payment schemes. Wire-compatible with x402. Zero runtime dependencies.",
|
|
6
|
-
"license": "
|
|
7
|
-
"author": "
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"author": "Swee Group LLC <daniel@sweeinc.com> (https://s402-protocol.org)",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "https://github.com/s402-protocol/core.git"
|