@odatano/x402 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env +0 -0
- package/.github/workflows/test.yaml +10 -1
- package/CHANGELOG.md +27 -2
- package/LICENSE +169 -21
- package/README.md +11 -8
- package/datano-x402.png +0 -0
- package/package.json +5 -4
- package/release-0.4.0.md +58 -0
- package/srv/bridge.d.ts +85 -1
- package/srv/bridge.js +34 -8
- package/srv/client/axios.js +1 -3
- package/srv/client/errors.d.ts +0 -21
- package/srv/client/errors.js +0 -37
- package/srv/client/fetch.js +2 -7
- package/srv/core/decode.js +48 -137
- package/srv/helpers/address.d.ts +31 -0
- package/srv/helpers/address.js +57 -0
- package/srv/helpers/build-unsigned-tx.d.ts +24 -23
- package/srv/helpers/build-unsigned-tx.js +62 -147
- package/srv/middleware/cap.js +27 -2
package/.env
ADDED
|
File without changes
|
|
@@ -46,4 +46,13 @@ jobs:
|
|
|
46
46
|
run: npm run build
|
|
47
47
|
|
|
48
48
|
- name: Run tests
|
|
49
|
-
run: npm test
|
|
49
|
+
run: npm run test:coverage
|
|
50
|
+
|
|
51
|
+
- name: Upload coverage to Codecov
|
|
52
|
+
# Only upload once per CI run — pick a single Node version.
|
|
53
|
+
if: matrix.node == '22.x'
|
|
54
|
+
uses: codecov/codecov-action@v4
|
|
55
|
+
with:
|
|
56
|
+
files: ./coverage/lcov.info
|
|
57
|
+
token: ${{ secrets.CODECOV_TOKEN }}
|
|
58
|
+
fail_ci_if_error: false
|
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,31 @@ All notable changes to `@odatano/x402` are documented here. The format follows [
|
|
|
4
4
|
|
|
5
5
|
**Pre-1.0 caveat:** minor versions may include breaking changes until `1.0.0`.
|
|
6
6
|
|
|
7
|
+
## [0.4.0] - 2026-06-19
|
|
8
|
+
|
|
9
|
+
### Changed (breaking)
|
|
10
|
+
- **Dropped the direct `@emurgo/cardano-serialization-lib-nodejs` (CSL) dependency.** x402 no longer carries a CBOR/transaction library of its own; all tx parsing and building now go through `@odatano/core`'s Buildooor stack (CSL-free since core `1.8.0`). **The `@odatano/core` peer requirement is now `>=1.9.1`** (was `>=1.7.8`). Upgrade `@odatano/core` in the same step.
|
|
11
|
+
- **`buildUnsignedPaymentTx` now delegates to core's tx-builder.** UTxO selection, change, min-ADA and fee are handled by core (Buildooor `keepRelevant`) instead of x402's own CSL coin-selection. Consequences: the v2 `nonceRef` is now the built tx's first input (was x402's largest-UTxO pick); the validity-range upper bound is derived from a POSIX deadline and read back from the built tx, so `ttlSlot` reflects what the builder set (and may be `null` if unset) rather than a value x402 computed. The result shape (`unsignedTxCborHex`, `txHashHex`, `requiredSignerHex`, `nonceRef`, `inputs`, `ttlSlot`) and the `BuildUnsignedTxArgs` (`buyerBech32`, `requirements`, `ttlSlotsFromNow`) are unchanged. This helper is browser-buyer convenience only; it is not on the facilitator/validation path.
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
- `bech32` runtime dependency, for CSL-free Shelley address introspection (`srv/helpers/address.ts`) used to derive `requiredSignerHex` and validate Base/Enterprise key-cred addresses.
|
|
15
|
+
|
|
16
|
+
### Internal
|
|
17
|
+
- `srv/core/decode.ts` parses via core's pure `parseTransaction`; `srv/bridge.ts` gained typed `parseTransaction` and `buildUnsignedTransfer` wrappers (single coupling point preserved).
|
|
18
|
+
- Test fixtures (`test/fixtures/{constants,build-tx}.ts`) rebuilt on `@harmoniclabs/buildooor` (dev-only); shared `core-parse-mock` stubs the core barrel down to its pure parser so decode-exercising suites don't load uncompiled `@cds-models` sources. Suite: 317 tests across 25 suites, all green.
|
|
19
|
+
|
|
20
|
+
## [0.3.1] - 2026-05-15
|
|
21
|
+
|
|
22
|
+
### Fixed
|
|
23
|
+
- **`gateService` now emits the canonical x402 v2 body on the wire** instead of CAP's OData-wrapped shape. The gate writes `httpRes.status(402).json(body)` directly when an Express response is reachable, then calls `req.reject(402, ...)` as the chain-terminator: the synchronous throw stops CAP's handler pipeline so the gated `on` handler never runs, and CAP's render attempt no-ops on `headersSent`. Non-HTTP transports (event invocations, `$batch` reuse) fall back to plain `req.reject`. Validated against `@sap/cds ^9`. Third-party x402 clients now interop with CAP-gated services without any unwrap shim. Closes the "Known issues" item from 0.3.0.
|
|
24
|
+
|
|
25
|
+
### Removed (breaking)
|
|
26
|
+
- **`unwrapCapEnvelope`** helper and its calls from `x402Fetch` / `x402Axios`. With the server fix above, the v2 body lands at the top level on the wire and the defensive unwrap is dead code. The export is gone; consumers who pulled it in (e.g. for wrapping the wrappers) should remove the import. **Pair the upgrade**: if you upgrade the `@odatano/x402` client to 0.3.1, upgrade the server in the same step, since 0.3.1 clients no longer unwrap a 0.3.0-style wrapped body.
|
|
27
|
+
|
|
28
|
+
### Changed
|
|
29
|
+
- `srv/middleware/cap.ts` , new `send402` helper and `getHttpRes` accessor; the one 402 emit site routes through `send402`. The two 500 `req.reject` paths (pricing-resolver throw, facilitator throw) are unchanged.
|
|
30
|
+
- Test suite: 232 tests across 21 suites. CAP middleware tests gained 3 cases (canonical-wire-shape regression, `headersSent` defensive fallback, no-`http.res` transport fallback). Client tests dropped 7 cases tied to `unwrapCapEnvelope`.
|
|
31
|
+
|
|
7
32
|
## [0.3.0] - 2026-05-15
|
|
8
33
|
|
|
9
34
|
### Added
|
|
@@ -16,14 +41,14 @@ All notable changes to `@odatano/x402` are documented here. The format follows [
|
|
|
16
41
|
- **Browser-buyer example** , `examples/browser-buyer/` Vite scaffold showing CIP-30 wallet + `x402Fetch` wiring. Documents the typical "unsigned-from-server, signed-by-wallet" architecture (server exposes `POST /pay/intent` via `buildUnsignedPaymentTx`; browser signs via CIP-30). Includes CORS notes for cross-origin deployments.
|
|
17
42
|
|
|
18
43
|
### Fixed
|
|
19
|
-
- **`x402Fetch` / `x402Axios` now interop with `gateService`** out of the box. CAP's `req.reject(402, body)` wraps the canonical v2 body inside its standard OData error envelope (`{ error: { message: "<json>", code: "402", ... } }`), so previous client wrappers saw `body.x402Version === undefined` and bailed without retrying. Both clients now defensively unwrap the OData envelope before validating shape.
|
|
44
|
+
- **`x402Fetch` / `x402Axios` now interop with `gateService`** out of the box. CAP's `req.reject(402, body)` wraps the canonical v2 body inside its standard OData error envelope (`{ error: { message: "<json>", code: "402", ... } }`), so previous client wrappers saw `body.x402Version === undefined` and bailed without retrying. Both clients now defensively unwrap the OData envelope before validating shape.
|
|
20
45
|
|
|
21
46
|
### Known issues
|
|
22
47
|
- The CAP `gateService` still emits 402 responses wrapped in CAP's OData error envelope on the wire (because `req.reject` is the only documented abort path). Third-party x402 clients hitting a CAP-gated server will see the wrapped shape; only `@odatano/x402`'s own clients unwrap it. Direct-write-to-`req.http.res` is the planned symmetric fix but needs validation against `@sap/cds` ^9 internals; tracked for v0.3.1.
|
|
23
48
|
- `PaymentClaim.payTo` , verified recipient address now populated on the claim (was previously only on the requirements entry). Useful for `onAccepted` audit and receipts.
|
|
24
49
|
|
|
25
50
|
### Changed
|
|
26
|
-
- Test count: 177 →
|
|
51
|
+
- Test count: 177 → 236 (HTTP-server round-trips, multi-accept + dynamic-pricing across `requirements`/`validate`/`verify`/`cap`/`express`, 4 receipts cases, 5 grants cases, 13 client-error cases).
|
|
27
52
|
- `srv/middleware/{cap,express}.ts` now emit `accepts[]` via `buildPaymentRequirementsMulti()`; single-entry callers are unaffected (one-entry array produces a body byte-identical to v0.2).
|
|
28
53
|
- `srv/facilitator/verify.ts` decodes the envelope BEFORE selecting a requirements entry; multi-accept depends on knowing which `(payTo, asset)` the tx actually credited.
|
|
29
54
|
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,169 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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 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
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
124
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
125
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
126
|
+
this License, without any additional terms or conditions.
|
|
127
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
128
|
+
the terms of any separate license agreement you may have executed
|
|
129
|
+
with Licensor regarding such Contributions.
|
|
130
|
+
|
|
131
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
132
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
133
|
+
except as required for reasonable and customary use in describing the
|
|
134
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
135
|
+
|
|
136
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
137
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
138
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
139
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
140
|
+
implied, including, without limitation, any warranties or conditions
|
|
141
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
142
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
143
|
+
appropriateness of using or redistributing the Work and assume any
|
|
144
|
+
risks associated with Your exercise of permissions under this License.
|
|
145
|
+
|
|
146
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
147
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
148
|
+
unless required by applicable law (such as deliberate and grossly
|
|
149
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
150
|
+
liable to You for damages, including any direct, indirect, special,
|
|
151
|
+
incidental, or consequential damages of any character arising as a
|
|
152
|
+
result of this License or out of the use or inability to use the
|
|
153
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
154
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
155
|
+
other commercial damages or losses), even if such Contributor
|
|
156
|
+
has been advised of the possibility of such damages.
|
|
157
|
+
|
|
158
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
159
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
160
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
161
|
+
or other liability obligations and/or rights consistent with this
|
|
162
|
+
License. However, in accepting such obligations, You may act only
|
|
163
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
164
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
165
|
+
defend, and hold each Contributor harmless for any liability
|
|
166
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
167
|
+
of your accepting any such warranty or additional liability.
|
|
168
|
+
|
|
169
|
+
Copyright [16.05.2026] [Maximilian Weber]
|
package/README.md
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+

|
|
1
2
|
# @odatano/x402
|
|
2
3
|
|
|
3
|
-
[](https://github.com/ODATANO/x402/actions/workflows/test.yaml)
|
|
5
|
+
[](https://codecov.io/gh/ODATANO/x402)
|
|
6
|
+
[](https://www.npmjs.com/package/@odatano/core)
|
|
7
|
+
[](https://www.npmjs.com/package/@odatano/x402)
|
|
8
|
+
[](https://www.npmjs.com/package/@odatano/x402)
|
|
9
|
+
[](LICENSE)
|
|
7
10
|
|
|
8
11
|
x402 payment gating for SAP CAP applications, backed by Cardano.
|
|
9
12
|
|
|
@@ -17,7 +20,7 @@ Implements the **Cardano-x402-v2** spec on top of [`@odatano/core`](https://www.
|
|
|
17
20
|
npm install @odatano/x402 @odatano/core
|
|
18
21
|
```
|
|
19
22
|
|
|
20
|
-
`@odatano/core` (the Cardano bridge) is a peer dependency. Install whichever version meets `>=1.
|
|
23
|
+
`@odatano/core` (the Cardano bridge) is a peer dependency. Install whichever version meets `>=1.9.1`.
|
|
21
24
|
|
|
22
25
|
## Quick Start
|
|
23
26
|
|
|
@@ -81,7 +84,7 @@ Configure the Cardano backend in `package.json`:
|
|
|
81
84
|
|
|
82
85
|
- Node.js 22+
|
|
83
86
|
- `@sap/cds >= 9` (peer)
|
|
84
|
-
- `@odatano/core >= 1.
|
|
87
|
+
- `@odatano/core >= 1.9.1` (peer)
|
|
85
88
|
- `express ^4` (peer), only if you use `x402Middleware`
|
|
86
89
|
- A Cardano backend reachable via `@odatano/core` (Blockfrost / Koios / Ogmios)
|
|
87
90
|
|
|
@@ -90,9 +93,9 @@ Configure the Cardano backend in `package.json`:
|
|
|
90
93
|
```bash
|
|
91
94
|
npm install # Workspace install: covers root + examples/*
|
|
92
95
|
npm run build # tsc, emits .js/.d.ts next to .ts (outDir: .)
|
|
93
|
-
npm test #
|
|
96
|
+
npm test # 232 tests, ~13s
|
|
94
97
|
```
|
|
95
98
|
|
|
96
99
|
## License
|
|
97
100
|
|
|
98
|
-
Apache-2.0
|
|
101
|
+
Apache-2.0 (see [LICENSE](LICENSE))
|
package/datano-x402.png
ADDED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@odatano/x402",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "x402 Cardano-v2 payment library for SAP CAP applications",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "srv/index.js",
|
|
@@ -23,11 +23,11 @@
|
|
|
23
23
|
"test:coverage": "jest --coverage"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"
|
|
26
|
+
"bech32": "^2.0.0"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"@cap-js/cds-types": ">=0.15",
|
|
30
|
-
"@odatano/core": ">=1.
|
|
30
|
+
"@odatano/core": ">=1.9.1",
|
|
31
31
|
"@sap/cds": ">=9",
|
|
32
32
|
"express": "^4"
|
|
33
33
|
},
|
|
@@ -37,10 +37,11 @@
|
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
+
"@harmoniclabs/buildooor": "^0.2.6",
|
|
40
41
|
"@cap-js/cds-typer": "^0.38.0",
|
|
41
42
|
"@cap-js/cds-types": "^0.15.0",
|
|
42
43
|
"@cap-js/sqlite": "^2",
|
|
43
|
-
"@odatano/core": "^1.
|
|
44
|
+
"@odatano/core": "^1.9.1",
|
|
44
45
|
"@odatano/x402": ".",
|
|
45
46
|
"@sap/cds": "^9",
|
|
46
47
|
"@sap/cds-dk": "^9.4.3",
|
package/release-0.4.0.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# @odatano/x402 0.4.0
|
|
2
|
+
|
|
3
|
+
**Drop CSL, route all tx parsing/building through `@odatano/core`.**
|
|
4
|
+
|
|
5
|
+
x402 no longer carries its own CBOR/transaction library. Both former CSL
|
|
6
|
+
call sites now go through `@odatano/core`'s Buildooor stack (CSL-free since
|
|
7
|
+
core `1.8.0`).
|
|
8
|
+
|
|
9
|
+
## Highlights
|
|
10
|
+
|
|
11
|
+
- **`decode.ts`** parses via core's pure `parseTransaction` instead of CSL
|
|
12
|
+
`Transaction`/`FixedTransaction`. The byte-stable tx hash is preserved.
|
|
13
|
+
- **`buildUnsignedPaymentTx`** delegates UTxO selection, change, min-ADA and
|
|
14
|
+
fee to core's tx-builder. The v2 `nonceRef` and `ttlSlot` are read back
|
|
15
|
+
from the built tx rather than computed locally.
|
|
16
|
+
- **New CSL-free Shelley address parser** (`srv/helpers/address.ts`, via
|
|
17
|
+
`bech32`) derives `requiredSignerHex` and validates Base/Enterprise
|
|
18
|
+
key-cred addresses.
|
|
19
|
+
- **`bridge.ts`** gains typed `parseTransaction` + `buildUnsignedTransfer`
|
|
20
|
+
wrappers, keeping the single coupling point to core.
|
|
21
|
+
|
|
22
|
+
## Breaking changes
|
|
23
|
+
|
|
24
|
+
- **`@odatano/core` peer requirement raised `>=1.7.8` → `>=1.9.1`.** Upgrade
|
|
25
|
+
core in the same step.
|
|
26
|
+
- **`buildUnsignedPaymentTx` now uses core's coin-selection.** The nonce is
|
|
27
|
+
the built tx's first input (was x402's largest-UTxO pick), and `ttlSlot`
|
|
28
|
+
reflects what the builder set (may be `null`) instead of a value x402
|
|
29
|
+
computed. The result shape (`unsignedTxCborHex`, `txHashHex`,
|
|
30
|
+
`requiredSignerHex`, `nonceRef`, `inputs`, `ttlSlot`) and the
|
|
31
|
+
`BuildUnsignedTxArgs` (`buyerBech32`, `requirements`, `ttlSlotsFromNow`)
|
|
32
|
+
are unchanged. This helper is browser-buyer convenience only; it is not on
|
|
33
|
+
the facilitator/validation path.
|
|
34
|
+
|
|
35
|
+
## Dependencies
|
|
36
|
+
|
|
37
|
+
- Removed: `@emurgo/cardano-serialization-lib-nodejs` (CSL).
|
|
38
|
+
- Added: `bech32` (runtime), `@harmoniclabs/buildooor` (dev, test fixtures only).
|
|
39
|
+
|
|
40
|
+
## Internal / tests
|
|
41
|
+
|
|
42
|
+
- Test fixtures (`test/fixtures/{constants,build-tx}.ts`) rebuilt on
|
|
43
|
+
`@harmoniclabs/buildooor`. Shared `core-parse-mock` stubs the core barrel
|
|
44
|
+
down to its pure parser so decode-exercising suites don't load uncompiled
|
|
45
|
+
`@cds-models` sources.
|
|
46
|
+
- **317 tests across 25 suites pass; `tsc` and `eslint` clean.**
|
|
47
|
+
|
|
48
|
+
## Upgrade notes
|
|
49
|
+
|
|
50
|
+
Bump both packages together:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
npm install @odatano/x402@^0.4.0 @odatano/core@^1.9.1
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
If you call `buildUnsignedPaymentTx` directly, re-validate the browser-buyer
|
|
57
|
+
signing path on a testnet (preprod): it is the one flow where an external
|
|
58
|
+
wallet re-hashes the tx that core built.
|
package/srv/bridge.d.ts
CHANGED
|
@@ -61,5 +61,89 @@ export declare function getCurrentSlot(): Promise<number>;
|
|
|
61
61
|
* caller's perspective.
|
|
62
62
|
*/
|
|
63
63
|
export declare function isUtxoUnspent(txHash: string, outputIndex: number): Promise<boolean>;
|
|
64
|
-
|
|
64
|
+
/** One output of a parsed tx. `assets[].unit` is `policyId+nameHex`. */
|
|
65
|
+
export interface ParsedTxOutput {
|
|
66
|
+
address: string;
|
|
67
|
+
lovelace: string;
|
|
68
|
+
assets: Array<{
|
|
69
|
+
unit: string;
|
|
70
|
+
quantity: string;
|
|
71
|
+
}>;
|
|
72
|
+
datumHash: string | null;
|
|
73
|
+
inlineDatumHex: string | null;
|
|
74
|
+
referenceScriptHex: string | null;
|
|
75
|
+
}
|
|
76
|
+
/** Structured shape returned by `@odatano/core`'s `parseTransaction`. */
|
|
77
|
+
export interface ParsedTx {
|
|
78
|
+
txHash: string;
|
|
79
|
+
network: 'mainnet' | 'testnet' | null;
|
|
80
|
+
inputs: Array<{
|
|
81
|
+
txHash: string;
|
|
82
|
+
outputIndex: number;
|
|
83
|
+
}>;
|
|
84
|
+
outputs: ParsedTxOutput[];
|
|
85
|
+
/** Validity-range lower bound in slots (decimal string) or null. */
|
|
86
|
+
validityStart: string | null;
|
|
87
|
+
/** Validity-range upper bound (TTL) in slots (decimal string) or null. */
|
|
88
|
+
validityEnd: string | null;
|
|
89
|
+
fee: string;
|
|
90
|
+
mint: Array<{
|
|
91
|
+
unit: string;
|
|
92
|
+
quantity: string;
|
|
93
|
+
}>;
|
|
94
|
+
requiredSigners: string[];
|
|
95
|
+
scriptDataHash: string | null;
|
|
96
|
+
witnesses: {
|
|
97
|
+
vkeyCount: number;
|
|
98
|
+
nativeScripts: number;
|
|
99
|
+
plutusScripts: number;
|
|
100
|
+
plutusData: number;
|
|
101
|
+
redeemers: number;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Parse signed-or-unsigned tx CBOR (hex) into structured fields. Pure,
|
|
106
|
+
* no init / no chain call. Throws `X402Error(INVALID_CBOR)` on malformed
|
|
107
|
+
* input so callers in the decode path get a precise, surfaceable code.
|
|
108
|
+
*/
|
|
109
|
+
export declare function parseTransaction(cborHex: string): ParsedTx;
|
|
110
|
+
/** Request shape accepted by core's `build{Simple,MultiAsset}Transaction`. */
|
|
111
|
+
export interface CoreTransferReq {
|
|
112
|
+
/** Buyer's bech32 — UTxO source and default change address. */
|
|
113
|
+
senderAddress: string;
|
|
114
|
+
/** Bech32 recipient (`payTo`). */
|
|
115
|
+
recipientAddress: string;
|
|
116
|
+
/** Defaults to `senderAddress`. */
|
|
117
|
+
changeAddress?: string;
|
|
118
|
+
/** Output ADA in lovelace (decimal string). For token outputs this is the riding min-ADA. */
|
|
119
|
+
lovelaceAmount: string;
|
|
120
|
+
/** Native assets on the output. `unit` = `policyId+assetNameHex`. Omit/empty for pure ADA. */
|
|
121
|
+
assets?: Array<{
|
|
122
|
+
unit: string;
|
|
123
|
+
quantity: string;
|
|
124
|
+
}>;
|
|
125
|
+
/** Validity-range upper bound as POSIX ms; core converts to a slot. */
|
|
126
|
+
validityEndMs?: number;
|
|
127
|
+
}
|
|
128
|
+
/** Subset of core's `TxBuildResult` that x402 consumes. */
|
|
129
|
+
export interface CoreTxBuildResult {
|
|
130
|
+
unsignedTxCbor: string;
|
|
131
|
+
txBodyHash: string;
|
|
132
|
+
inputs: Array<{
|
|
133
|
+
txHash: string;
|
|
134
|
+
index: number;
|
|
135
|
+
lovelace: string;
|
|
136
|
+
}>;
|
|
137
|
+
outputs: Array<{
|
|
138
|
+
address: string;
|
|
139
|
+
lovelace: string;
|
|
140
|
+
}>;
|
|
141
|
+
feeLovelace: string;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Build an unsigned transfer tx via core's Buildooor builder. Routes to
|
|
145
|
+
* the multi-asset entry point when `assets` is non-empty (it rejects an
|
|
146
|
+
* empty asset list), otherwise the plain-ADA one.
|
|
147
|
+
*/
|
|
148
|
+
export declare function buildUnsignedTransfer(req: CoreTransferReq): Promise<CoreTxBuildResult>;
|
|
65
149
|
//# sourceMappingURL=bridge.d.ts.map
|
package/srv/bridge.js
CHANGED
|
@@ -15,7 +15,6 @@
|
|
|
15
15
|
* Both are called through directly here; no shim layer remains.
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.parseTransaction = void 0;
|
|
19
18
|
exports.init = init;
|
|
20
19
|
exports.shutdown = shutdown;
|
|
21
20
|
exports.getUtxosAtAddress = getUtxosAtAddress;
|
|
@@ -24,6 +23,8 @@ exports.getProtocolParameters = getProtocolParameters;
|
|
|
24
23
|
exports.submitTransaction = submitTransaction;
|
|
25
24
|
exports.getCurrentSlot = getCurrentSlot;
|
|
26
25
|
exports.isUtxoUnspent = isUtxoUnspent;
|
|
26
|
+
exports.parseTransaction = parseTransaction;
|
|
27
|
+
exports.buildUnsignedTransfer = buildUnsignedTransfer;
|
|
27
28
|
const errors_1 = require("./core/errors");
|
|
28
29
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
29
30
|
const od = require('@odatano/core');
|
|
@@ -143,10 +144,35 @@ async function isUtxoUnspent(txHash, outputIndex) {
|
|
|
143
144
|
await ensureInit();
|
|
144
145
|
return od.getCardanoClient().isUtxoUnspent(txHash, outputIndex);
|
|
145
146
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
147
|
+
const odParseTransaction = od.parseTransaction;
|
|
148
|
+
/**
|
|
149
|
+
* Parse signed-or-unsigned tx CBOR (hex) into structured fields. Pure,
|
|
150
|
+
* no init / no chain call. Throws `X402Error(INVALID_CBOR)` on malformed
|
|
151
|
+
* input so callers in the decode path get a precise, surfaceable code.
|
|
152
|
+
*/
|
|
153
|
+
function parseTransaction(cborHex) {
|
|
154
|
+
if (typeof odParseTransaction !== 'function') {
|
|
155
|
+
throw new errors_1.X402Error(errors_1.Codes.BRIDGE_UNAVAILABLE, '@odatano/core does not export parseTransaction (need >= 1.9.1)');
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
return odParseTransaction(cborHex);
|
|
159
|
+
}
|
|
160
|
+
catch (err) {
|
|
161
|
+
throw new errors_1.X402Error(errors_1.Codes.INVALID_CBOR, `transaction CBOR did not decode: ${err?.message ?? err}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Build an unsigned transfer tx via core's Buildooor builder. Routes to
|
|
166
|
+
* the multi-asset entry point when `assets` is non-empty (it rejects an
|
|
167
|
+
* empty asset list), otherwise the plain-ADA one.
|
|
168
|
+
*/
|
|
169
|
+
async function buildUnsignedTransfer(req) {
|
|
170
|
+
await ensureInit();
|
|
171
|
+
const builder = od.getCardanoTxBuilder();
|
|
172
|
+
// Core's builder expects core's own protocol-parameter shape, so source
|
|
173
|
+
// it from the same client rather than re-deriving it here.
|
|
174
|
+
const params = await od.getCardanoClient().getProtocolParameters();
|
|
175
|
+
return req.assets && req.assets.length > 0
|
|
176
|
+
? builder.buildMultiAssetTransaction(req, params)
|
|
177
|
+
: builder.buildSimpleAdaTransaction(req, params);
|
|
178
|
+
}
|
package/srv/client/axios.js
CHANGED
|
@@ -54,9 +54,7 @@ function x402Axios(instance, opts) {
|
|
|
54
54
|
}
|
|
55
55
|
const cfg = error.config;
|
|
56
56
|
const retries = Number(cfg[RETRY_KEY] ?? 0);
|
|
57
|
-
|
|
58
|
-
// Defensively unwrap before validating shape.
|
|
59
|
-
const body = (0, errors_1.unwrapCapEnvelope)(error.response.data);
|
|
57
|
+
const body = error.response.data;
|
|
60
58
|
const status = error.response.status;
|
|
61
59
|
if (retries >= maxRetries) {
|
|
62
60
|
if (opts.errorOnFailure) {
|
package/srv/client/errors.d.ts
CHANGED
|
@@ -60,27 +60,6 @@ export declare class X402PaymentError extends Error {
|
|
|
60
60
|
readonly cause?: unknown;
|
|
61
61
|
constructor(init: X402PaymentErrorInit);
|
|
62
62
|
}
|
|
63
|
-
/**
|
|
64
|
-
* Defensively unwrap a CAP / OData error envelope around a v2 body.
|
|
65
|
-
*
|
|
66
|
-
* Background: `gateService` in this package historically (≤ v0.2)
|
|
67
|
-
* reaches a 402 via `req.reject(402, JSON.stringify(body))`, which
|
|
68
|
-
* CAP wraps into its standard OData error shape, putting the canonical
|
|
69
|
-
* v2 body inside `error.message` as a JSON string:
|
|
70
|
-
*
|
|
71
|
-
* { "error": { "message": "{\"x402Version\":2, ... }", "code": "402", ... } }
|
|
72
|
-
*
|
|
73
|
-
* The Express middleware emits the v2 body at the top level directly.
|
|
74
|
-
* This helper detects the CAP wrap and returns the unwrapped candidate
|
|
75
|
-
* so client wrappers can validate v2 shape uniformly. Non-CAP bodies
|
|
76
|
-
* pass through untouched.
|
|
77
|
-
*
|
|
78
|
-
* Symmetric server-side fix (emit canonical body even through CAP) is
|
|
79
|
-
* a separate concern, deferred until we can validate the CAP-version
|
|
80
|
-
* specific behaviour. The unwrap below keeps `x402Fetch` /
|
|
81
|
-
* `x402Axios` working against both shapes.
|
|
82
|
-
*/
|
|
83
|
-
export declare function unwrapCapEnvelope(parsed: unknown): unknown;
|
|
84
63
|
/**
|
|
85
64
|
* Parse the (server, canonical) error string from a `PaymentRequirementsBody`.
|
|
86
65
|
*
|
package/srv/client/errors.js
CHANGED
|
@@ -31,7 +31,6 @@
|
|
|
31
31
|
*/
|
|
32
32
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
33
|
exports.X402PaymentError = void 0;
|
|
34
|
-
exports.unwrapCapEnvelope = unwrapCapEnvelope;
|
|
35
34
|
exports.parseErrorCode = parseErrorCode;
|
|
36
35
|
exports.paymentErrorFromBody = paymentErrorFromBody;
|
|
37
36
|
/**
|
|
@@ -71,42 +70,6 @@ class X402PaymentError extends Error {
|
|
|
71
70
|
}
|
|
72
71
|
}
|
|
73
72
|
exports.X402PaymentError = X402PaymentError;
|
|
74
|
-
/**
|
|
75
|
-
* Defensively unwrap a CAP / OData error envelope around a v2 body.
|
|
76
|
-
*
|
|
77
|
-
* Background: `gateService` in this package historically (≤ v0.2)
|
|
78
|
-
* reaches a 402 via `req.reject(402, JSON.stringify(body))`, which
|
|
79
|
-
* CAP wraps into its standard OData error shape, putting the canonical
|
|
80
|
-
* v2 body inside `error.message` as a JSON string:
|
|
81
|
-
*
|
|
82
|
-
* { "error": { "message": "{\"x402Version\":2, ... }", "code": "402", ... } }
|
|
83
|
-
*
|
|
84
|
-
* The Express middleware emits the v2 body at the top level directly.
|
|
85
|
-
* This helper detects the CAP wrap and returns the unwrapped candidate
|
|
86
|
-
* so client wrappers can validate v2 shape uniformly. Non-CAP bodies
|
|
87
|
-
* pass through untouched.
|
|
88
|
-
*
|
|
89
|
-
* Symmetric server-side fix (emit canonical body even through CAP) is
|
|
90
|
-
* a separate concern, deferred until we can validate the CAP-version
|
|
91
|
-
* specific behaviour. The unwrap below keeps `x402Fetch` /
|
|
92
|
-
* `x402Axios` working against both shapes.
|
|
93
|
-
*/
|
|
94
|
-
function unwrapCapEnvelope(parsed) {
|
|
95
|
-
if (!parsed || typeof parsed !== 'object')
|
|
96
|
-
return parsed;
|
|
97
|
-
const maybeError = parsed.error;
|
|
98
|
-
if (!maybeError || typeof maybeError !== 'object')
|
|
99
|
-
return parsed;
|
|
100
|
-
const msg = maybeError.message;
|
|
101
|
-
if (typeof msg !== 'string')
|
|
102
|
-
return parsed;
|
|
103
|
-
try {
|
|
104
|
-
return JSON.parse(msg);
|
|
105
|
-
}
|
|
106
|
-
catch {
|
|
107
|
-
return parsed;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
73
|
/**
|
|
111
74
|
* Parse the (server, canonical) error string from a `PaymentRequirementsBody`.
|
|
112
75
|
*
|