@vauban-pay/substrate 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 +179 -0
- package/README.md +52 -0
- package/dist/index.cjs +93 -0
- package/dist/index.d.cts +51 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.js +52 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
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 made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other transformations
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and its Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of tracking such
|
|
55
|
+
submissions, but excluding communication that is conspicuously marked
|
|
56
|
+
or designated in writing by the copyright owner as "Not a Contribution."
|
|
57
|
+
|
|
58
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
59
|
+
whom a Contribution has been submitted to the Licensor.
|
|
60
|
+
|
|
61
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
65
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
66
|
+
Work and such Derivative Works in Source or Object form.
|
|
67
|
+
|
|
68
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
69
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
70
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
71
|
+
(except as stated in this section) patent license to make, have made,
|
|
72
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
73
|
+
where such license applies only to those patent contributions
|
|
74
|
+
Licensable by such Contributor that are necessarily infringed by their
|
|
75
|
+
Contributor contribution(s) alone or by the combination of their
|
|
76
|
+
Contribution(s) with the Work to which such Contribution(s) was submitted.
|
|
77
|
+
If You institute patent litigation against any entity (including a
|
|
78
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
|
79
|
+
Contribution incorporated within the Work constitutes direct or
|
|
80
|
+
contributory patent infringement, then any patent licenses granted to
|
|
81
|
+
You under this License for that Work shall terminate as of the date such
|
|
82
|
+
litigation is filed.
|
|
83
|
+
|
|
84
|
+
4. Redistribution. You may reproduce and distribute copies of the Work
|
|
85
|
+
or Derivative Works thereof in any medium, with or without
|
|
86
|
+
modifications, and in Source or Object form, provided that You meet
|
|
87
|
+
the following conditions:
|
|
88
|
+
|
|
89
|
+
(a) You must give any other recipients of the Work or Derivative Works
|
|
90
|
+
a copy of this License; and
|
|
91
|
+
|
|
92
|
+
(b) You must cause any modified files to carry prominent notices stating
|
|
93
|
+
that You changed the files; and
|
|
94
|
+
|
|
95
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
96
|
+
distribute, all copyright, patent, trademark, and attribution notices
|
|
97
|
+
from the Source form of the Work, excluding those notices that do not
|
|
98
|
+
pertain to any part of the Derivative Works; and
|
|
99
|
+
|
|
100
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
101
|
+
distribution, You must include a readable copy of the attribution
|
|
102
|
+
notices contained within such NOTICE file, in at least one of the
|
|
103
|
+
following places: within a NOTICE text file distributed as part of
|
|
104
|
+
the Derivative Works; within the Source form or documentation, if
|
|
105
|
+
provided along with the Derivative Works; or, within a display
|
|
106
|
+
generated by the Derivative Works, if and wherever such third-party
|
|
107
|
+
notices normally appear. The contents of the NOTICE file are for
|
|
108
|
+
informational purposes only and do not modify the License. You may
|
|
109
|
+
add Your own attribution notices within Derivative Works that You
|
|
110
|
+
distribute, alongside or in addition to the NOTICE text from the
|
|
111
|
+
Work, provided that such additional attribution notices cannot be
|
|
112
|
+
construed as modifying the License.
|
|
113
|
+
|
|
114
|
+
You may add Your own license statement for Your modifications and may
|
|
115
|
+
provide additional grant of rights to use, reproduce, modify, and
|
|
116
|
+
distribute those modifications as a Derivative Work, either on the terms
|
|
117
|
+
of this License or with separate terms and conditions covering those
|
|
118
|
+
modifications.
|
|
119
|
+
|
|
120
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
121
|
+
Contribution intentionally submitted for inclusion in the Work by You to
|
|
122
|
+
the Licensor shall be under the terms and conditions of this License,
|
|
123
|
+
without any additional terms or conditions. Notwithstanding the above,
|
|
124
|
+
nothing herein shall supersede or modify the terms of any separate
|
|
125
|
+
license agreement you may have executed with Licensor regarding such
|
|
126
|
+
Contributions.
|
|
127
|
+
|
|
128
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
129
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
130
|
+
except as required for reasonable and customary use in describing the
|
|
131
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
132
|
+
|
|
133
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed
|
|
134
|
+
to in writing, Licensor provides the Work (and each Contributor
|
|
135
|
+
provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES
|
|
136
|
+
OR CONDITIONS OF ANY KIND, either express or implied, including,
|
|
137
|
+
without limitation, any warranties or conditions of TITLE,
|
|
138
|
+
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR
|
|
139
|
+
PURPOSE. You are solely responsible for determining the appropriateness
|
|
140
|
+
of using or reproducing the Work and assume any risks associated with
|
|
141
|
+
Your exercise of permissions under this License.
|
|
142
|
+
|
|
143
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
144
|
+
whether in tort (including negligence), contract, or otherwise, unless
|
|
145
|
+
required by applicable law (such as deliberate and grossly negligent
|
|
146
|
+
acts) or agreed to in writing, shall any Contributor be liable to You
|
|
147
|
+
for damages, including any direct, indirect, special, incidental, or
|
|
148
|
+
exemplary damages of any character arising as a result of this License
|
|
149
|
+
or out of the use or inability to use the Work (including but not limited
|
|
150
|
+
to damages for loss of goodwill, work stoppage, computer failure or
|
|
151
|
+
malfunction, or all other commercial damages or losses), even if such
|
|
152
|
+
Contributor has been advised of the possibility of such damages.
|
|
153
|
+
|
|
154
|
+
9. Accepting Warranty or Additional Liability. While redistributing the
|
|
155
|
+
Work or Derivative Works thereof, You may choose to offer, and charge a
|
|
156
|
+
fee for, acceptance of support, warranty, indemnity, or other liability
|
|
157
|
+
obligations and/or rights consistent with this License. However, in
|
|
158
|
+
accepting such obligations, You may offer such conditions only on Your
|
|
159
|
+
own behalf and on Your sole responsibility, not on behalf of any other
|
|
160
|
+
Contributor, and only if You agree to indemnify, defend, and hold each
|
|
161
|
+
Contributor harmless for any liability incurred by, or claims asserted
|
|
162
|
+
against, such Contributor by reason of your accepting any such warranty
|
|
163
|
+
or additional liability.
|
|
164
|
+
|
|
165
|
+
END OF TERMS AND CONDITIONS
|
|
166
|
+
|
|
167
|
+
Copyright 2026 Vauban Research <research@vauban.tech>
|
|
168
|
+
|
|
169
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
170
|
+
you may not use this file except in compliance with the License.
|
|
171
|
+
You may obtain a copy of the License at
|
|
172
|
+
|
|
173
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
174
|
+
|
|
175
|
+
Unless required by applicable law or agreed to in writing, software
|
|
176
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
177
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
178
|
+
See the License for the specific language governing permissions and
|
|
179
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# @vauban-pay/substrate
|
|
2
|
+
|
|
3
|
+
TypeScript reference runner for JCS (RFC 8785) preimage discipline in the
|
|
4
|
+
x402 STARK Receipt Format Extension
|
|
5
|
+
([draft-vauban-x402-stark-receipts](https://datatracker.ietf.org/doc/draft-vauban-x402-stark-receipts/)).
|
|
6
|
+
|
|
7
|
+
Sister implementations:
|
|
8
|
+
|
|
9
|
+
- Rust: [`vauban-x402-jcs-conformance`](https://crates.io/crates/vauban-x402-jcs-conformance) v0.1.0 (live on crates.io)
|
|
10
|
+
- Python: [`vauban-x402-stark-receipt`](https://pypi.org/project/vauban-x402-stark-receipt/)
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pnpm add @vauban-pay/substrate
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## API
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { jcsCanonicalBytes, jcsSha256, jcsHash, validatePairInvariant } from "@vauban-pay/substrate";
|
|
22
|
+
|
|
23
|
+
// Canonical UTF-8 bytes (RFC 8785 key sort, no whitespace)
|
|
24
|
+
const bytes: Uint8Array = jcsCanonicalBytes({ amount: 100, currency: "STRK" });
|
|
25
|
+
|
|
26
|
+
// SHA-256 over canonical bytes, prefixed "sha256:<hex64>"
|
|
27
|
+
const digest: string = jcsSha256({ amount: 100, currency: "STRK" });
|
|
28
|
+
// => "sha256:a3f9..."
|
|
29
|
+
|
|
30
|
+
// Both in one call (camelCase TS; Rust struct uses snake_case)
|
|
31
|
+
const { base64Canonical, sha256 } = jcsHash({ amount: 100, currency: "STRK" });
|
|
32
|
+
|
|
33
|
+
// Pair invariant: key order is irrelevant, array order is significant
|
|
34
|
+
const ok: boolean = validatePairInvariant({ a: 1, b: 2 }, { b: 2, a: 1 }); // true
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
**Rust/TS name mapping** (snake_case Rust; camelCase TS):
|
|
38
|
+
`jcs_canonical_bytes` ; `jcsCanonicalBytes` |
|
|
39
|
+
`jcs_sha256` ; `jcsSha256` |
|
|
40
|
+
`jcs_hash` ; `jcsHash` |
|
|
41
|
+
`validate_pair_invariant` ; `validatePairInvariant`
|
|
42
|
+
|
|
43
|
+
Errors surface as `CanonicalisationError` (subclass of `Error`).
|
|
44
|
+
|
|
45
|
+
## Spec reference
|
|
46
|
+
|
|
47
|
+
IETF I-D: [draft-vauban-x402-stark-receipts-00](https://datatracker.ietf.org/doc/draft-vauban-x402-stark-receipts/)
|
|
48
|
+
RFC 8785 (JCS): https://www.rfc-editor.org/rfc/rfc8785
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
Apache-2.0 - Vauban Research <research@vauban.tech> - https://pay.vauban.tech
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
CanonicalisationError: () => CanonicalisationError,
|
|
34
|
+
jcsCanonicalBytes: () => jcsCanonicalBytes,
|
|
35
|
+
jcsHash: () => jcsHash,
|
|
36
|
+
jcsSha256: () => jcsSha256,
|
|
37
|
+
validatePairInvariant: () => validatePairInvariant
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(index_exports);
|
|
40
|
+
|
|
41
|
+
// src/jcs.ts
|
|
42
|
+
var import_node_crypto = require("crypto");
|
|
43
|
+
var import_canonicalize = __toESM(require("canonicalize"), 1);
|
|
44
|
+
var CanonicalisationError = class extends Error {
|
|
45
|
+
constructor(message) {
|
|
46
|
+
super(message);
|
|
47
|
+
this.name = "CanonicalisationError";
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
function jcsCanonicalBytes(body) {
|
|
51
|
+
const serialised = (0, import_canonicalize.default)(body);
|
|
52
|
+
if (serialised === void 0) {
|
|
53
|
+
throw new CanonicalisationError(
|
|
54
|
+
"canonicalize returned undefined; value may contain undefined, Infinity, NaN, or a function"
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
return new TextEncoder().encode(serialised);
|
|
58
|
+
}
|
|
59
|
+
function jcsSha256(body) {
|
|
60
|
+
const bytes = jcsCanonicalBytes(body);
|
|
61
|
+
const digest = (0, import_node_crypto.createHash)("sha256").update(bytes).digest("hex");
|
|
62
|
+
return `sha256:${digest}`;
|
|
63
|
+
}
|
|
64
|
+
function jcsHash(body) {
|
|
65
|
+
const bytes = jcsCanonicalBytes(body);
|
|
66
|
+
const base64Canonical = Buffer.from(bytes).toString("base64");
|
|
67
|
+
const digest = (0, import_node_crypto.createHash)("sha256").update(bytes).digest("hex");
|
|
68
|
+
return {
|
|
69
|
+
base64Canonical,
|
|
70
|
+
sha256: `sha256:${digest}`
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function validatePairInvariant(left, right) {
|
|
74
|
+
try {
|
|
75
|
+
const leftBytes = jcsCanonicalBytes(left);
|
|
76
|
+
const rightBytes = jcsCanonicalBytes(right);
|
|
77
|
+
if (leftBytes.length !== rightBytes.length) return false;
|
|
78
|
+
for (let i = 0; i < leftBytes.length; i++) {
|
|
79
|
+
if (leftBytes[i] !== rightBytes[i]) return false;
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
} catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
87
|
+
0 && (module.exports = {
|
|
88
|
+
CanonicalisationError,
|
|
89
|
+
jcsCanonicalBytes,
|
|
90
|
+
jcsHash,
|
|
91
|
+
jcsSha256,
|
|
92
|
+
validatePairInvariant
|
|
93
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when canonicalize returns undefined (e.g., undefined input, or
|
|
3
|
+
* a value that RFC 8785 cannot represent such as a bare function).
|
|
4
|
+
*/
|
|
5
|
+
declare class CanonicalisationError extends Error {
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Produce the JCS (RFC 8785) canonical UTF-8 byte representation of `body`.
|
|
10
|
+
*
|
|
11
|
+
* Mirrors Rust: `jcs_canonical_bytes(value: &impl Serialize) -> Vec<u8>`
|
|
12
|
+
*
|
|
13
|
+
* Throws `CanonicalisationError` if the value cannot be serialised
|
|
14
|
+
* (e.g., `undefined`, `Infinity`, `NaN`, or bare functions).
|
|
15
|
+
*/
|
|
16
|
+
declare function jcsCanonicalBytes(body: unknown): Uint8Array;
|
|
17
|
+
/**
|
|
18
|
+
* Compute SHA-256 over the JCS canonical bytes of `body`.
|
|
19
|
+
* Returns the digest in hex, prefixed with "sha256:".
|
|
20
|
+
*
|
|
21
|
+
* Mirrors Rust: `jcs_sha256(value: &impl Serialize) -> String`
|
|
22
|
+
* The "sha256:<hex>" convention is the canonical Vauban preimage tag.
|
|
23
|
+
*/
|
|
24
|
+
declare function jcsSha256(body: unknown): string;
|
|
25
|
+
/**
|
|
26
|
+
* Compute JCS canonical bytes + SHA-256 digest in a single pass.
|
|
27
|
+
* Returns both the base64-encoded canonical JSON and the "sha256:<hex>" tag.
|
|
28
|
+
*
|
|
29
|
+
* Mirrors Rust: `jcs_hash(value: &impl Serialize) -> JcsHash`
|
|
30
|
+
* where JcsHash { base64_canonical, sha256 }.
|
|
31
|
+
*
|
|
32
|
+
* TypeScript field names are camelCase per TS convention;
|
|
33
|
+
* Rust struct uses snake_case (base64_canonical / sha256).
|
|
34
|
+
*/
|
|
35
|
+
declare function jcsHash(body: unknown): {
|
|
36
|
+
base64Canonical: string;
|
|
37
|
+
sha256: string;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Verify that two independently-serialised values produce identical JCS canonical bytes.
|
|
41
|
+
* Used to validate the pair invariant: canonical(A) == canonical(B) iff A and B are
|
|
42
|
+
* semantically equivalent under RFC 8785 (same keys, same values, key order irrelevant).
|
|
43
|
+
*
|
|
44
|
+
* Mirrors Rust: `validate_pair_invariant(left: &impl Serialize, right: &impl Serialize) -> bool`
|
|
45
|
+
*
|
|
46
|
+
* Returns `false` (rather than throwing) when either value cannot be canonicalised,
|
|
47
|
+
* matching the Rust `Result::is_ok()` -> bool semantic.
|
|
48
|
+
*/
|
|
49
|
+
declare function validatePairInvariant(left: unknown, right: unknown): boolean;
|
|
50
|
+
|
|
51
|
+
export { CanonicalisationError, jcsCanonicalBytes, jcsHash, jcsSha256, validatePairInvariant };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when canonicalize returns undefined (e.g., undefined input, or
|
|
3
|
+
* a value that RFC 8785 cannot represent such as a bare function).
|
|
4
|
+
*/
|
|
5
|
+
declare class CanonicalisationError extends Error {
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Produce the JCS (RFC 8785) canonical UTF-8 byte representation of `body`.
|
|
10
|
+
*
|
|
11
|
+
* Mirrors Rust: `jcs_canonical_bytes(value: &impl Serialize) -> Vec<u8>`
|
|
12
|
+
*
|
|
13
|
+
* Throws `CanonicalisationError` if the value cannot be serialised
|
|
14
|
+
* (e.g., `undefined`, `Infinity`, `NaN`, or bare functions).
|
|
15
|
+
*/
|
|
16
|
+
declare function jcsCanonicalBytes(body: unknown): Uint8Array;
|
|
17
|
+
/**
|
|
18
|
+
* Compute SHA-256 over the JCS canonical bytes of `body`.
|
|
19
|
+
* Returns the digest in hex, prefixed with "sha256:".
|
|
20
|
+
*
|
|
21
|
+
* Mirrors Rust: `jcs_sha256(value: &impl Serialize) -> String`
|
|
22
|
+
* The "sha256:<hex>" convention is the canonical Vauban preimage tag.
|
|
23
|
+
*/
|
|
24
|
+
declare function jcsSha256(body: unknown): string;
|
|
25
|
+
/**
|
|
26
|
+
* Compute JCS canonical bytes + SHA-256 digest in a single pass.
|
|
27
|
+
* Returns both the base64-encoded canonical JSON and the "sha256:<hex>" tag.
|
|
28
|
+
*
|
|
29
|
+
* Mirrors Rust: `jcs_hash(value: &impl Serialize) -> JcsHash`
|
|
30
|
+
* where JcsHash { base64_canonical, sha256 }.
|
|
31
|
+
*
|
|
32
|
+
* TypeScript field names are camelCase per TS convention;
|
|
33
|
+
* Rust struct uses snake_case (base64_canonical / sha256).
|
|
34
|
+
*/
|
|
35
|
+
declare function jcsHash(body: unknown): {
|
|
36
|
+
base64Canonical: string;
|
|
37
|
+
sha256: string;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Verify that two independently-serialised values produce identical JCS canonical bytes.
|
|
41
|
+
* Used to validate the pair invariant: canonical(A) == canonical(B) iff A and B are
|
|
42
|
+
* semantically equivalent under RFC 8785 (same keys, same values, key order irrelevant).
|
|
43
|
+
*
|
|
44
|
+
* Mirrors Rust: `validate_pair_invariant(left: &impl Serialize, right: &impl Serialize) -> bool`
|
|
45
|
+
*
|
|
46
|
+
* Returns `false` (rather than throwing) when either value cannot be canonicalised,
|
|
47
|
+
* matching the Rust `Result::is_ok()` -> bool semantic.
|
|
48
|
+
*/
|
|
49
|
+
declare function validatePairInvariant(left: unknown, right: unknown): boolean;
|
|
50
|
+
|
|
51
|
+
export { CanonicalisationError, jcsCanonicalBytes, jcsHash, jcsSha256, validatePairInvariant };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// src/jcs.ts
|
|
2
|
+
import { createHash } from "crypto";
|
|
3
|
+
import canonicalize from "canonicalize";
|
|
4
|
+
var CanonicalisationError = class extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "CanonicalisationError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
function jcsCanonicalBytes(body) {
|
|
11
|
+
const serialised = canonicalize(body);
|
|
12
|
+
if (serialised === void 0) {
|
|
13
|
+
throw new CanonicalisationError(
|
|
14
|
+
"canonicalize returned undefined; value may contain undefined, Infinity, NaN, or a function"
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
return new TextEncoder().encode(serialised);
|
|
18
|
+
}
|
|
19
|
+
function jcsSha256(body) {
|
|
20
|
+
const bytes = jcsCanonicalBytes(body);
|
|
21
|
+
const digest = createHash("sha256").update(bytes).digest("hex");
|
|
22
|
+
return `sha256:${digest}`;
|
|
23
|
+
}
|
|
24
|
+
function jcsHash(body) {
|
|
25
|
+
const bytes = jcsCanonicalBytes(body);
|
|
26
|
+
const base64Canonical = Buffer.from(bytes).toString("base64");
|
|
27
|
+
const digest = createHash("sha256").update(bytes).digest("hex");
|
|
28
|
+
return {
|
|
29
|
+
base64Canonical,
|
|
30
|
+
sha256: `sha256:${digest}`
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function validatePairInvariant(left, right) {
|
|
34
|
+
try {
|
|
35
|
+
const leftBytes = jcsCanonicalBytes(left);
|
|
36
|
+
const rightBytes = jcsCanonicalBytes(right);
|
|
37
|
+
if (leftBytes.length !== rightBytes.length) return false;
|
|
38
|
+
for (let i = 0; i < leftBytes.length; i++) {
|
|
39
|
+
if (leftBytes[i] !== rightBytes[i]) return false;
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
} catch {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
CanonicalisationError,
|
|
48
|
+
jcsCanonicalBytes,
|
|
49
|
+
jcsHash,
|
|
50
|
+
jcsSha256,
|
|
51
|
+
validatePairInvariant
|
|
52
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vauban-pay/substrate",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "TypeScript reference runner for JCS (RFC 8785) preimage discipline in x402 STARK Receipt Format Extension (draft-vauban-x402-stark-receipts).",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "Vauban Research <research@vauban.tech>",
|
|
7
|
+
"homepage": "https://pay.vauban.tech",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/vauban-org/vauban-zkpay"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/vauban-org/vauban-zkpay/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"x402",
|
|
17
|
+
"jcs",
|
|
18
|
+
"stark",
|
|
19
|
+
"receipt",
|
|
20
|
+
"canonical",
|
|
21
|
+
"preimage",
|
|
22
|
+
"ietf"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.cjs",
|
|
26
|
+
"module": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"require": "./dist/index.cjs"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"canonicalize": "^3.0.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"typescript": "^5.4.0",
|
|
45
|
+
"tsup": "^8.0.0",
|
|
46
|
+
"vitest": "^1.0.0",
|
|
47
|
+
"eslint": "^9.0.0",
|
|
48
|
+
"@types/node": "^20.0.0"
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=18.0.0",
|
|
52
|
+
"pnpm": ">=8.0.0"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
59
|
+
"test": "vitest run",
|
|
60
|
+
"lint": "eslint src --max-warnings 0"
|
|
61
|
+
}
|
|
62
|
+
}
|