@ftptech/x402-canton-core 0.1.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 +201 -0
- package/README.md +34 -0
- package/dist/caip2.d.ts +17 -0
- package/dist/caip2.d.ts.map +1 -0
- package/dist/caip2.js +27 -0
- package/dist/caip2.js.map +1 -0
- package/dist/encoding.d.ts +18 -0
- package/dist/encoding.d.ts.map +1 -0
- package/dist/encoding.js +29 -0
- package/dist/encoding.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +167 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +93 -0
- package/dist/types.js.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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
|
+
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 describing the origin of the Work and
|
|
141
|
+
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 Support. 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 support.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 FTP Team
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# @ftptech/x402-canton-core
|
|
2
|
+
|
|
3
|
+
Shared TypeScript types and helpers for the Canton x402 protocol
|
|
4
|
+
stack. Used by `@ftptech/x402-canton-facilitator`,
|
|
5
|
+
`@ftptech/x402-canton-client`, `@ftptech/x402-canton-express`, and
|
|
6
|
+
`@ftptech/x402-canton-next`.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm i @ftptech/x402-canton-core
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
> Note: the `@ftp` npm scope is not final — it may change before the
|
|
15
|
+
> first public release (see [`docs/PUBLISHING.md`](https://github.com/sunstrike228/canton-x402/blob/main/docs/PUBLISHING.md)).
|
|
16
|
+
> Pin the version you install and check the README for the current
|
|
17
|
+
> package name.
|
|
18
|
+
|
|
19
|
+
## What's in here
|
|
20
|
+
|
|
21
|
+
- `X402ResourceInfo`, `PaymentRequirements`, `CantonPaymentPayload`,
|
|
22
|
+
`FacilitatorRequest`, `VerifyResponse`, `SettleResponse`,
|
|
23
|
+
`SupportedResponse`, `CantonErrorCode` types per x402 v2.
|
|
24
|
+
- `CantonNetwork` CAIP-2-style identifiers (`canton:devnet`,
|
|
25
|
+
`canton:mainnet`, `canton:<global-synchronizer-id>`).
|
|
26
|
+
- `encodeBase64Json` / `decodeBase64Json` + v1/v2 header-name
|
|
27
|
+
constants (`PAYMENT-REQUIRED`, `PAYMENT-SIGNATURE`,
|
|
28
|
+
`PAYMENT-RESPONSE`, `X-PAYMENT`, `X-PAYMENT-RESPONSE`).
|
|
29
|
+
|
|
30
|
+
## Project
|
|
31
|
+
|
|
32
|
+
See [github.com/sunstrike228/canton-x402](https://github.com/sunstrike228/canton-x402)
|
|
33
|
+
and the scheme spec at
|
|
34
|
+
[`specs/scheme_exact_canton.md`](https://github.com/sunstrike228/canton-x402/blob/main/specs/scheme_exact_canton.md).
|
package/dist/caip2.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CAIP-2-style network identifiers for Canton.
|
|
3
|
+
*
|
|
4
|
+
* x402 v2 uses CAIP-2 (`<namespace>:<reference>`) for networks: `eip155:8453`
|
|
5
|
+
* for Base mainnet, `solana:<genesis-hash>` for Solana, etc. Canton's
|
|
6
|
+
* canonical reference is the Global Synchronizer ID. For convenience we
|
|
7
|
+
* also accept the short forms `canton:devnet`, `canton:testnet`, and `canton:mainnet`.
|
|
8
|
+
*/
|
|
9
|
+
import type { CantonNetwork } from "./types.js";
|
|
10
|
+
export declare const CANTON_NAMESPACE: "canton";
|
|
11
|
+
export declare const CANTON_DEVNET: CantonNetwork;
|
|
12
|
+
export declare const CANTON_TESTNET: CantonNetwork;
|
|
13
|
+
export declare const CANTON_MAINNET: CantonNetwork;
|
|
14
|
+
export declare function buildNetworkId(synchronizerId: string): CantonNetwork;
|
|
15
|
+
export declare function isCantonNetwork(value: string): value is CantonNetwork;
|
|
16
|
+
export declare function parseNetworkReference(network: CantonNetwork): string;
|
|
17
|
+
//# sourceMappingURL=caip2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"caip2.d.ts","sourceRoot":"","sources":["../src/caip2.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD,eAAO,MAAM,gBAAgB,EAAG,QAAiB,CAAC;AAElD,eAAO,MAAM,aAAa,EAAE,aAA+B,CAAC;AAC5D,eAAO,MAAM,cAAc,EAAE,aAAgC,CAAC;AAC9D,eAAO,MAAM,cAAc,EAAE,aAAgC,CAAC;AAE9D,wBAAgB,cAAc,CAAC,cAAc,EAAE,MAAM,GAAG,aAAa,CAEpE;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,aAAa,CAErE;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,aAAa,GAAG,MAAM,CAmBpE"}
|
package/dist/caip2.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export const CANTON_NAMESPACE = "canton";
|
|
2
|
+
export const CANTON_DEVNET = "canton:devnet";
|
|
3
|
+
export const CANTON_TESTNET = "canton:testnet";
|
|
4
|
+
export const CANTON_MAINNET = "canton:mainnet";
|
|
5
|
+
export function buildNetworkId(synchronizerId) {
|
|
6
|
+
return `canton:${synchronizerId}`;
|
|
7
|
+
}
|
|
8
|
+
export function isCantonNetwork(value) {
|
|
9
|
+
return value.startsWith(`${CANTON_NAMESPACE}:`);
|
|
10
|
+
}
|
|
11
|
+
export function parseNetworkReference(network) {
|
|
12
|
+
// Validate the `canton:` prefix explicitly (defense-in-depth: the type says
|
|
13
|
+
// CantonNetwork, but a runtime caller could hand us a raw string). Then take
|
|
14
|
+
// EVERYTHING after the prefix — a Global Synchronizer ID like
|
|
15
|
+
// `canton:global-domain::1220abc` must survive intact, so we slice on the
|
|
16
|
+
// prefix length rather than split(":") which would truncate the embedded `::`.
|
|
17
|
+
const prefix = `${CANTON_NAMESPACE}:`;
|
|
18
|
+
if (!network.startsWith(prefix)) {
|
|
19
|
+
throw new Error(`malformed Canton network id (missing "${prefix}" prefix): ${network}`);
|
|
20
|
+
}
|
|
21
|
+
const ref = network.slice(prefix.length);
|
|
22
|
+
if (!ref) {
|
|
23
|
+
throw new Error(`malformed Canton network id (empty reference): ${network}`);
|
|
24
|
+
}
|
|
25
|
+
return ref;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=caip2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"caip2.js","sourceRoot":"","sources":["../src/caip2.ts"],"names":[],"mappings":"AAUA,MAAM,CAAC,MAAM,gBAAgB,GAAG,QAAiB,CAAC;AAElD,MAAM,CAAC,MAAM,aAAa,GAAkB,eAAe,CAAC;AAC5D,MAAM,CAAC,MAAM,cAAc,GAAkB,gBAAgB,CAAC;AAC9D,MAAM,CAAC,MAAM,cAAc,GAAkB,gBAAgB,CAAC;AAE9D,MAAM,UAAU,cAAc,CAAC,cAAsB;IACnD,OAAO,UAAU,cAAc,EAAmB,CAAC;AACrD,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,KAAa;IAC3C,OAAO,KAAK,CAAC,UAAU,CAAC,GAAG,gBAAgB,GAAG,CAAC,CAAC;AAClD,CAAC;AAED,MAAM,UAAU,qBAAqB,CAAC,OAAsB;IAC1D,4EAA4E;IAC5E,6EAA6E;IAC7E,8DAA8D;IAC9D,0EAA0E;IAC1E,+EAA+E;IAC/E,MAAM,MAAM,GAAG,GAAG,gBAAgB,GAAG,CAAC;IACtC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CACb,yCAAyC,MAAM,cAAc,OAAO,EAAE,CACvE,CAAC;IACJ,CAAC;IACD,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACzC,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,MAAM,IAAI,KAAK,CACb,kDAAkD,OAAO,EAAE,CAC5D,CAAC;IACJ,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* x402 wire-format encoding helpers.
|
|
3
|
+
*
|
|
4
|
+
* The PAYMENT-REQUIRED, PAYMENT-SIGNATURE, and PAYMENT-RESPONSE headers
|
|
5
|
+
* are all base64-encoded JSON (x402 v2 wire format).
|
|
6
|
+
*/
|
|
7
|
+
export declare const HEADER_PAYMENT_REQUIRED_V2 = "PAYMENT-REQUIRED";
|
|
8
|
+
export declare const HEADER_PAYMENT_SIGNATURE_V2 = "PAYMENT-SIGNATURE";
|
|
9
|
+
export declare const HEADER_PAYMENT_RESPONSE_V2 = "PAYMENT-RESPONSE";
|
|
10
|
+
export declare function encodeBase64Json(value: unknown): string;
|
|
11
|
+
/** Max accepted size of a base64 envelope before decode (defense vs.
|
|
12
|
+
* attacker-supplied PAYMENT-SIGNATURE / X-PAYMENT headers, which arrive
|
|
13
|
+
* OUTSIDE the server's body-size limit). x402 envelopes are a few KiB;
|
|
14
|
+
* 128 KiB is generous. Reject larger input before spending CPU/memory on
|
|
15
|
+
* base64-decode + JSON.parse. */
|
|
16
|
+
export declare const MAX_BASE64_JSON_INPUT: number;
|
|
17
|
+
export declare function decodeBase64Json<T>(encoded: string): T;
|
|
18
|
+
//# sourceMappingURL=encoding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,eAAO,MAAM,0BAA0B,qBAAqB,CAAC;AAC7D,eAAO,MAAM,2BAA2B,sBAAsB,CAAC;AAC/D,eAAO,MAAM,0BAA0B,qBAAqB,CAAC;AAE7D,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAEvD;AAED;;;;kCAIkC;AAClC,eAAO,MAAM,qBAAqB,QAAa,CAAC;AAEhD,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,GAAG,CAAC,CAWtD"}
|
package/dist/encoding.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* x402 wire-format encoding helpers.
|
|
3
|
+
*
|
|
4
|
+
* The PAYMENT-REQUIRED, PAYMENT-SIGNATURE, and PAYMENT-RESPONSE headers
|
|
5
|
+
* are all base64-encoded JSON (x402 v2 wire format).
|
|
6
|
+
*/
|
|
7
|
+
export const HEADER_PAYMENT_REQUIRED_V2 = "PAYMENT-REQUIRED";
|
|
8
|
+
export const HEADER_PAYMENT_SIGNATURE_V2 = "PAYMENT-SIGNATURE";
|
|
9
|
+
export const HEADER_PAYMENT_RESPONSE_V2 = "PAYMENT-RESPONSE";
|
|
10
|
+
export function encodeBase64Json(value) {
|
|
11
|
+
return Buffer.from(JSON.stringify(value), "utf8").toString("base64");
|
|
12
|
+
}
|
|
13
|
+
/** Max accepted size of a base64 envelope before decode (defense vs.
|
|
14
|
+
* attacker-supplied PAYMENT-SIGNATURE / X-PAYMENT headers, which arrive
|
|
15
|
+
* OUTSIDE the server's body-size limit). x402 envelopes are a few KiB;
|
|
16
|
+
* 128 KiB is generous. Reject larger input before spending CPU/memory on
|
|
17
|
+
* base64-decode + JSON.parse. */
|
|
18
|
+
export const MAX_BASE64_JSON_INPUT = 128 * 1024;
|
|
19
|
+
export function decodeBase64Json(encoded) {
|
|
20
|
+
if (typeof encoded !== "string") {
|
|
21
|
+
throw new Error("decodeBase64Json: input is not a string");
|
|
22
|
+
}
|
|
23
|
+
if (encoded.length > MAX_BASE64_JSON_INPUT) {
|
|
24
|
+
throw new Error(`decodeBase64Json: input too large (${encoded.length} > ${MAX_BASE64_JSON_INPUT})`);
|
|
25
|
+
}
|
|
26
|
+
const json = Buffer.from(encoded, "base64").toString("utf8");
|
|
27
|
+
return JSON.parse(json);
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=encoding.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encoding.js","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,0BAA0B,GAAG,kBAAkB,CAAC;AAC7D,MAAM,CAAC,MAAM,2BAA2B,GAAG,mBAAmB,CAAC;AAC/D,MAAM,CAAC,MAAM,0BAA0B,GAAG,kBAAkB,CAAC;AAE7D,MAAM,UAAU,gBAAgB,CAAC,KAAc;IAC7C,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AACvE,CAAC;AAED;;;;kCAIkC;AAClC,MAAM,CAAC,MAAM,qBAAqB,GAAG,GAAG,GAAG,IAAI,CAAC;AAEhD,MAAM,UAAU,gBAAgB,CAAI,OAAe;IACjD,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;IAC7D,CAAC;IACD,IAAI,OAAO,CAAC,MAAM,GAAG,qBAAqB,EAAE,CAAC;QAC3C,MAAM,IAAI,KAAK,CACb,sCAAsC,OAAO,CAAC,MAAM,MAAM,qBAAqB,GAAG,CACnF,CAAC;IACJ,CAAC;IACD,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC7D,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAM,CAAC;AAC/B,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canton x402 type definitions.
|
|
3
|
+
*
|
|
4
|
+
* These mirror the v2 wire format from x402-foundation/x402, specialized
|
|
5
|
+
* for the `exact-canton` scheme. The Canton-specific bits live in
|
|
6
|
+
* `extra` (server side) and `payload` (client side); the envelope is
|
|
7
|
+
* standard x402 v2.
|
|
8
|
+
*/
|
|
9
|
+
/** CAIP-2-style Canton network identifier. */
|
|
10
|
+
export type CantonNetwork = `canton:devnet` | `canton:mainnet` | `canton:${string}`;
|
|
11
|
+
/** Which on-ledger primitive carries the actual CC movement.
|
|
12
|
+
* external-party-amulet-rules (v1): the facilitator submits
|
|
13
|
+
* TransferCommand_Send and pays the Global Synchronizer traffic fee.
|
|
14
|
+
* cip56-transfer-factory: the payer submits TransferFactory_Transfer and
|
|
15
|
+
* pays; the facilitator only verifies. A deploy advertises one via
|
|
16
|
+
* CANTON_X402_PRIMARY_METHOD; both are handled at verify/settle. */
|
|
17
|
+
export type CantonTransferMethod = "external-party-amulet-rules" | "cip56-transfer-factory";
|
|
18
|
+
/** Canton-specific `extra` block in 402 PaymentRequirements. */
|
|
19
|
+
export type CantonPaymentRequirementsExtra = {
|
|
20
|
+
transferMethod: "external-party-amulet-rules";
|
|
21
|
+
facilitatorParty: string;
|
|
22
|
+
synchronizerId: string;
|
|
23
|
+
merchantContractCid?: string;
|
|
24
|
+
memo?: string;
|
|
25
|
+
} | {
|
|
26
|
+
transferMethod: "cip56-transfer-factory";
|
|
27
|
+
facilitatorParty: string;
|
|
28
|
+
synchronizerId: string;
|
|
29
|
+
transferFactoryCid: string;
|
|
30
|
+
/** Hash-prefixed templateId of the factory contract, same form
|
|
31
|
+
* Canton emits on createdEvent.templateId. Lets the client submit
|
|
32
|
+
* TransferFactory_Transfer without its own Scan registry lookup. */
|
|
33
|
+
transferFactoryTemplateId: string;
|
|
34
|
+
instrumentId: {
|
|
35
|
+
admin: string;
|
|
36
|
+
id: string;
|
|
37
|
+
};
|
|
38
|
+
memo?: string;
|
|
39
|
+
};
|
|
40
|
+
/** Canton-specific `payload` block in PaymentPayload. */
|
|
41
|
+
export type CantonPaymentPayload = {
|
|
42
|
+
transferMethod: "external-party-amulet-rules";
|
|
43
|
+
transferCommandCid: string;
|
|
44
|
+
payerParty: string;
|
|
45
|
+
nonce: number;
|
|
46
|
+
signatureProof?: string;
|
|
47
|
+
/** updateId of the TransferCommand_Create transaction (relay path only).
|
|
48
|
+
* When present, the facilitator records it for traffic-burn attribution
|
|
49
|
+
* alongside the Send updateId. Optional: absent for direct-key signers. */
|
|
50
|
+
createUpdateId?: string;
|
|
51
|
+
} | {
|
|
52
|
+
transferMethod: "cip56-transfer-factory";
|
|
53
|
+
payerParty: string;
|
|
54
|
+
/** Canton ledger updateId of the TransferFactory_Transfer exercise.
|
|
55
|
+
* Required when the registry returned
|
|
56
|
+
* TransferInstructionResult_Completed (settlement already on-ledger;
|
|
57
|
+
* the updateId is the proof). */
|
|
58
|
+
updateId?: string;
|
|
59
|
+
/** Contract id of a TransferInstruction returned when the result is
|
|
60
|
+
* TransferInstructionResult_Pending; /settle waits for it. */
|
|
61
|
+
transferInstructionCid?: string;
|
|
62
|
+
signatureProof?: string;
|
|
63
|
+
};
|
|
64
|
+
/** Resource being paid for. Echoed from the server's 402 PAYMENT-REQUIRED
|
|
65
|
+
* header into every PaymentPayload per x402 v2. */
|
|
66
|
+
export interface X402ResourceInfo {
|
|
67
|
+
url: string;
|
|
68
|
+
description?: string;
|
|
69
|
+
mimeType?: string;
|
|
70
|
+
}
|
|
71
|
+
/** /verify and /settle request body (x402 v2). */
|
|
72
|
+
export type FacilitatorRequest = {
|
|
73
|
+
x402Version: 2;
|
|
74
|
+
paymentPayload: {
|
|
75
|
+
x402Version: 2;
|
|
76
|
+
scheme: "exact-canton";
|
|
77
|
+
network: CantonNetwork;
|
|
78
|
+
resource: X402ResourceInfo;
|
|
79
|
+
accepted: PaymentRequirements;
|
|
80
|
+
payload: CantonPaymentPayload;
|
|
81
|
+
extensions?: Record<string, unknown>;
|
|
82
|
+
};
|
|
83
|
+
paymentRequirements: PaymentRequirements;
|
|
84
|
+
};
|
|
85
|
+
/** /verify response (200 regardless of validity; success carried in `isValid`). */
|
|
86
|
+
export type VerifyResponse = {
|
|
87
|
+
isValid: true;
|
|
88
|
+
payer: string;
|
|
89
|
+
} | {
|
|
90
|
+
isValid: false;
|
|
91
|
+
invalidReason: CantonErrorCode;
|
|
92
|
+
payer?: string;
|
|
93
|
+
};
|
|
94
|
+
/** /settle response. */
|
|
95
|
+
export type SettleResponse = {
|
|
96
|
+
success: true;
|
|
97
|
+
payer: string;
|
|
98
|
+
transaction: string;
|
|
99
|
+
network: CantonNetwork;
|
|
100
|
+
amount?: string;
|
|
101
|
+
extensions?: Record<string, unknown>;
|
|
102
|
+
} | {
|
|
103
|
+
success: false;
|
|
104
|
+
errorReason: CantonErrorCode;
|
|
105
|
+
transaction: "";
|
|
106
|
+
};
|
|
107
|
+
/** /supported response. */
|
|
108
|
+
export type SupportedResponse = {
|
|
109
|
+
kinds: Array<{
|
|
110
|
+
x402Version: 1 | 2;
|
|
111
|
+
scheme: "exact-canton";
|
|
112
|
+
network: CantonNetwork;
|
|
113
|
+
extra?: {
|
|
114
|
+
transferMethods: CantonTransferMethod[];
|
|
115
|
+
};
|
|
116
|
+
}>;
|
|
117
|
+
extensions: string[];
|
|
118
|
+
signers: Record<string, string[]>;
|
|
119
|
+
};
|
|
120
|
+
/** PaymentRequirements entry as advertised in an `accepts[]` array. */
|
|
121
|
+
export type PaymentRequirements = {
|
|
122
|
+
scheme: "exact-canton";
|
|
123
|
+
network: CantonNetwork;
|
|
124
|
+
amount: string;
|
|
125
|
+
asset: string;
|
|
126
|
+
payTo: string;
|
|
127
|
+
maxTimeoutSeconds: number;
|
|
128
|
+
extra: CantonPaymentRequirementsExtra;
|
|
129
|
+
};
|
|
130
|
+
/** Canton-specific x402 error codes. Prefix `invalid_exact_canton_*`. */
|
|
131
|
+
export type CantonErrorCode = "invalid_exact_canton_transfer_command_not_found" | "invalid_exact_canton_amount_mismatch" | "invalid_exact_canton_asset_mismatch" | "invalid_exact_canton_expired" | "invalid_exact_canton_nonce_reuse" | "invalid_exact_canton_payer_mismatch" | "invalid_exact_canton_merchant_mismatch" | "invalid_exact_canton_signature" | "invalid_exact_canton_resource_url_mismatch" | "invalid_exact_canton_merchant_not_registered" | "invalid_exact_canton_counter_not_ready" | "invalid_exact_canton_transfer_instruction_not_found" | "invalid_exact_canton_transfer_completed_not_visible" | "invalid_exact_canton_transfer_instruction_pending" | "invalid_exact_canton_instrument_id_mismatch" | "invalid_exact_canton_transfer_factory_not_found" | "invalid_exact_canton_missing_proof" | "invalid_exact_canton_holding_locked" | "invalid_exact_canton_payment_already_settled" | "unexpected_canton_ledger_error";
|
|
132
|
+
/**
|
|
133
|
+
* Pick the server's OWN PaymentRequirements entry that a client claims to
|
|
134
|
+
* be paying against — defeating client tampering of price / recipient.
|
|
135
|
+
*
|
|
136
|
+
* SECURITY (audit SEC-1): the facilitator is a generic relay that
|
|
137
|
+
* validates an on-ledger transfer against whatever `paymentRequirements`
|
|
138
|
+
* it is handed. The resource-server middleware receives the client's
|
|
139
|
+
* claimed `accepted` block from INSIDE the client-controlled
|
|
140
|
+
* PAYMENT-SIGNATURE envelope. If the middleware forwards that claimed
|
|
141
|
+
* block to the facilitator unchecked, an attacker can set `amount: "1"`
|
|
142
|
+
* (or `payTo: <self>`), submit a matching tiny on-ledger transfer, and
|
|
143
|
+
* still unlock the gated resource. The middleware MUST instead pin the
|
|
144
|
+
* requirements to its own configured `accepts` list.
|
|
145
|
+
*
|
|
146
|
+
* Returns the matching SERVER entry (authoritative on every field), or
|
|
147
|
+
* `null` if the client's claim does not correspond to any configured
|
|
148
|
+
* entry — in which case the middleware must respond 402 and never call
|
|
149
|
+
* the facilitator with the client's numbers.
|
|
150
|
+
*
|
|
151
|
+
* Matching is on the money-critical fields only: scheme, network, amount,
|
|
152
|
+
* asset, payTo, and extra.{transferMethod, facilitatorParty,
|
|
153
|
+
* synchronizerId, instrumentId}. `maxTimeoutSeconds` / `memo` / discovery
|
|
154
|
+
* cids are not part of the price contract.
|
|
155
|
+
*/
|
|
156
|
+
export declare function selectServerRequirements(accepts: PaymentRequirements[], clientAccepted: unknown): PaymentRequirements | null;
|
|
157
|
+
/**
|
|
158
|
+
* Config-time consistency guard (audit L1). On the CIP-56 path the facilitator
|
|
159
|
+
* validates against `extra.instrumentId` and ignores `asset`, so if an operator
|
|
160
|
+
* configures an `asset` of the structured `<admin>::<id>` form that disagrees
|
|
161
|
+
* with `extra.instrumentId`, the mismatch is silent and the instrumentId wins.
|
|
162
|
+
* Catch it at middleware setup instead. `asset` may also be a symbolic value
|
|
163
|
+
* such as "canton-coin" (see PaymentRequirements.asset) — only the `::` form is
|
|
164
|
+
* cross-checked. Throws on a mismatch; no-op when consistent or not applicable.
|
|
165
|
+
*/
|
|
166
|
+
export declare function assertAssetInstrumentConsistency(req: PaymentRequirements): void;
|
|
167
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"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,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canton x402 type definitions.
|
|
3
|
+
*
|
|
4
|
+
* These mirror the v2 wire format from x402-foundation/x402, specialized
|
|
5
|
+
* for the `exact-canton` scheme. The Canton-specific bits live in
|
|
6
|
+
* `extra` (server side) and `payload` (client side); the envelope is
|
|
7
|
+
* standard x402 v2.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Pick the server's OWN PaymentRequirements entry that a client claims to
|
|
11
|
+
* be paying against — defeating client tampering of price / recipient.
|
|
12
|
+
*
|
|
13
|
+
* SECURITY (audit SEC-1): the facilitator is a generic relay that
|
|
14
|
+
* validates an on-ledger transfer against whatever `paymentRequirements`
|
|
15
|
+
* it is handed. The resource-server middleware receives the client's
|
|
16
|
+
* claimed `accepted` block from INSIDE the client-controlled
|
|
17
|
+
* PAYMENT-SIGNATURE envelope. If the middleware forwards that claimed
|
|
18
|
+
* block to the facilitator unchecked, an attacker can set `amount: "1"`
|
|
19
|
+
* (or `payTo: <self>`), submit a matching tiny on-ledger transfer, and
|
|
20
|
+
* still unlock the gated resource. The middleware MUST instead pin the
|
|
21
|
+
* requirements to its own configured `accepts` list.
|
|
22
|
+
*
|
|
23
|
+
* Returns the matching SERVER entry (authoritative on every field), or
|
|
24
|
+
* `null` if the client's claim does not correspond to any configured
|
|
25
|
+
* entry — in which case the middleware must respond 402 and never call
|
|
26
|
+
* the facilitator with the client's numbers.
|
|
27
|
+
*
|
|
28
|
+
* Matching is on the money-critical fields only: scheme, network, amount,
|
|
29
|
+
* asset, payTo, and extra.{transferMethod, facilitatorParty,
|
|
30
|
+
* synchronizerId, instrumentId}. `maxTimeoutSeconds` / `memo` / discovery
|
|
31
|
+
* cids are not part of the price contract.
|
|
32
|
+
*/
|
|
33
|
+
export function selectServerRequirements(accepts, clientAccepted) {
|
|
34
|
+
if (typeof clientAccepted !== "object" || clientAccepted === null) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const c = clientAccepted;
|
|
38
|
+
const cExtra = (c.extra ?? {});
|
|
39
|
+
for (const r of accepts) {
|
|
40
|
+
const rExtra = r.extra;
|
|
41
|
+
if (r.scheme !== c.scheme)
|
|
42
|
+
continue;
|
|
43
|
+
if (r.network !== c.network)
|
|
44
|
+
continue;
|
|
45
|
+
if (r.amount !== c.amount)
|
|
46
|
+
continue;
|
|
47
|
+
if (r.asset !== c.asset)
|
|
48
|
+
continue;
|
|
49
|
+
if (r.payTo !== c.payTo)
|
|
50
|
+
continue;
|
|
51
|
+
if (rExtra.transferMethod !== cExtra.transferMethod)
|
|
52
|
+
continue;
|
|
53
|
+
if (rExtra.facilitatorParty !== cExtra.facilitatorParty)
|
|
54
|
+
continue;
|
|
55
|
+
if (rExtra.synchronizerId !== cExtra.synchronizerId)
|
|
56
|
+
continue;
|
|
57
|
+
// instrumentId (CIP-56): if either side carries it, both must match.
|
|
58
|
+
const rInst = rExtra.instrumentId;
|
|
59
|
+
const cInst = cExtra.instrumentId;
|
|
60
|
+
if (rInst || cInst) {
|
|
61
|
+
if (!rInst || !cInst)
|
|
62
|
+
continue;
|
|
63
|
+
if (rInst.admin !== cInst.admin || rInst.id !== cInst.id)
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
return r;
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Config-time consistency guard (audit L1). On the CIP-56 path the facilitator
|
|
72
|
+
* validates against `extra.instrumentId` and ignores `asset`, so if an operator
|
|
73
|
+
* configures an `asset` of the structured `<admin>::<id>` form that disagrees
|
|
74
|
+
* with `extra.instrumentId`, the mismatch is silent and the instrumentId wins.
|
|
75
|
+
* Catch it at middleware setup instead. `asset` may also be a symbolic value
|
|
76
|
+
* such as "canton-coin" (see PaymentRequirements.asset) — only the `::` form is
|
|
77
|
+
* cross-checked. Throws on a mismatch; no-op when consistent or not applicable.
|
|
78
|
+
*/
|
|
79
|
+
export function assertAssetInstrumentConsistency(req) {
|
|
80
|
+
const inst = req.extra.instrumentId;
|
|
81
|
+
if (!inst)
|
|
82
|
+
return;
|
|
83
|
+
if (!req.asset.includes("::"))
|
|
84
|
+
return; // symbolic asset (e.g. "canton-coin")
|
|
85
|
+
const expected = `${inst.admin}::${inst.id}`;
|
|
86
|
+
if (req.asset !== expected) {
|
|
87
|
+
throw new Error(`payment requirements asset "${req.asset}" disagrees with ` +
|
|
88
|
+
`extra.instrumentId ("${expected}"). On the CIP-56 path instrumentId ` +
|
|
89
|
+
`is authoritative, so a mismatched asset is an operator misconfig — ` +
|
|
90
|
+
`make asset and extra.instrumentId.{admin,id} consistent.`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AA4KH;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,wBAAwB,CACtC,OAA8B,EAC9B,cAAuB;IAEvB,IAAI,OAAO,cAAc,KAAK,QAAQ,IAAI,cAAc,KAAK,IAAI,EAAE,CAAC;QAClE,OAAO,IAAI,CAAC;IACd,CAAC;IACD,MAAM,CAAC,GAAG,cAA8C,CAAC;IACzD,MAAM,MAAM,GAAG,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE,CAK3B,CAAC;IACH,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;QACxB,MAAM,MAAM,GAAG,CAAC,CAAC,KAKhB,CAAC;QACF,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;YAAE,SAAS;QACpC,IAAI,CAAC,CAAC,OAAO,KAAK,CAAC,CAAC,OAAO;YAAE,SAAS;QACtC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;YAAE,SAAS;QACpC,IAAI,CAAC,CAAC,KAAK,KAAK,CAAC,C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AAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ftptech/x402-canton-core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared types and helpers for the Canton x402 stack",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "FTP team",
|
|
7
|
+
"homepage": "https://github.com/sunstrike228/canton-x402#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/sunstrike228/canton-x402.git",
|
|
11
|
+
"directory": "packages/core"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/sunstrike228/canton-x402/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"x402",
|
|
18
|
+
"canton",
|
|
19
|
+
"canton-network",
|
|
20
|
+
"payment",
|
|
21
|
+
"http",
|
|
22
|
+
"402"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist",
|
|
35
|
+
"README.md",
|
|
36
|
+
"LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsc -p tsconfig.json",
|
|
43
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
44
|
+
"test": "vitest run --passWithNoTests",
|
|
45
|
+
"lint": "eslint src --max-warnings 0"
|
|
46
|
+
}
|
|
47
|
+
}
|