@datumesh/verify 0.0.0-stage → 0.1.1
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 +202 -0
- package/README.md +54 -2
- package/dist/cjs/ed25519.d.ts +2 -0
- package/dist/cjs/ed25519.js +82 -0
- package/dist/cjs/errors.d.ts +14 -0
- package/dist/cjs/errors.js +61 -0
- package/dist/cjs/hash.d.ts +6 -0
- package/dist/cjs/hash.js +27 -0
- package/dist/cjs/index.d.ts +27 -0
- package/dist/cjs/index.js +83 -0
- package/dist/cjs/jcs.d.ts +5 -0
- package/dist/cjs/jcs.js +25 -0
- package/dist/cjs/json.d.ts +29 -0
- package/dist/cjs/json.js +321 -0
- package/dist/cjs/keys.d.ts +73 -0
- package/dist/cjs/keys.js +257 -0
- package/dist/cjs/lexical.d.ts +59 -0
- package/dist/cjs/lexical.js +207 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/unicode-data.d.ts +8 -0
- package/dist/cjs/unicode-data.js +14 -0
- package/dist/cjs/unicode.d.ts +12 -0
- package/dist/cjs/unicode.js +192 -0
- package/dist/cjs/uri.d.ts +31 -0
- package/dist/cjs/uri.js +201 -0
- package/dist/cjs/utf8.d.ts +10 -0
- package/dist/cjs/utf8.js +101 -0
- package/dist/cjs/validate.d.ts +17 -0
- package/dist/cjs/validate.js +349 -0
- package/dist/cjs/verify.d.ts +38 -0
- package/dist/cjs/verify.js +192 -0
- package/dist/esm/ed25519.d.ts +2 -0
- package/dist/esm/ed25519.js +79 -0
- package/dist/esm/errors.d.ts +14 -0
- package/dist/esm/errors.js +56 -0
- package/dist/esm/hash.d.ts +6 -0
- package/dist/esm/hash.js +21 -0
- package/dist/esm/index.d.ts +27 -0
- package/dist/esm/index.js +63 -0
- package/dist/esm/jcs.d.ts +5 -0
- package/dist/esm/jcs.js +18 -0
- package/dist/esm/json.d.ts +29 -0
- package/dist/esm/json.js +309 -0
- package/dist/esm/keys.d.ts +73 -0
- package/dist/esm/keys.js +249 -0
- package/dist/esm/lexical.d.ts +59 -0
- package/dist/esm/lexical.js +187 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/unicode-data.d.ts +8 -0
- package/dist/esm/unicode-data.js +11 -0
- package/dist/esm/unicode.d.ts +12 -0
- package/dist/esm/unicode.js +184 -0
- package/dist/esm/uri.d.ts +31 -0
- package/dist/esm/uri.js +196 -0
- package/dist/esm/utf8.d.ts +10 -0
- package/dist/esm/utf8.js +96 -0
- package/dist/esm/validate.d.ts +17 -0
- package/dist/esm/validate.js +340 -0
- package/dist/esm/verify.d.ts +38 -0
- package/dist/esm/verify.js +189 -0
- package/package.json +54 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @datumesh/verify
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Verify [Datumesh](https://datumesh.com) receipts in JavaScript: signed answers for addressable data cells, checked offline against strict RFC 8785 canonical JSON and Ed25519, following the [Datumesh specification](https://github.com/datumesh-com/datumesh-spec) revision `spec-0.1.1` and passing all of its test vectors.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @datumesh/verify
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import { readFileSync } from 'node:fs';
|
|
11
|
+
import { verify, keysFromResponse } from '@datumesh/verify';
|
|
12
|
+
|
|
13
|
+
// vectors/receipt/stored-number.json from https://github.com/datumesh-com/datumesh-spec
|
|
14
|
+
const vector = JSON.parse(readFileSync('stored-number.json', 'utf8'));
|
|
15
|
+
const keys = keysFromResponse('rates.example.org', 200, vector.keys['rates.example.org'].https.body);
|
|
16
|
+
const result = verify(vector.input, keys);
|
|
17
|
+
console.log(result.ok, result.code, result.level, result.hash);
|
|
18
|
+
// true OK signed 501f744d910c585859a5be4f3b62385ed0d6803db53dbbf95dff5097a68bee0d
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The same with CommonJS:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
const { readFileSync } = require('node:fs');
|
|
25
|
+
const { verify, keysFromResponse } = require('@datumesh/verify');
|
|
26
|
+
|
|
27
|
+
const vector = JSON.parse(readFileSync('stored-number.json', 'utf8'));
|
|
28
|
+
const keys = keysFromResponse('rates.example.org', 200, vector.keys['rates.example.org'].https.body);
|
|
29
|
+
console.log(verify(vector.input, keys).code); // OK
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## What it does
|
|
33
|
+
|
|
34
|
+
- `verify(receipt, keys, recompute?)` runs the verification procedure of SPEC.md 10.1 and returns `{ ok, code, level, hash, key, detail, path }`. It is synchronous, never touches the network and never throws for a bad receipt or bad key material; failures come back as the spec's error codes (`E_SIGNATURE`, `E_KEY_NOT_VALID`, ...). `level` is `"signed"`, or `"recomputed"` when you pass the descriptor, the input receipts and the value you obtained by evaluating the function (the library checks lineage and compares; it does not evaluate functions).
|
|
35
|
+
- `keys` is a key document, the result of `keysFromResponse` / `fetchKeys`, a `{ keyId: publicKeyHex }` map, or an array of these (one per host for lineage).
|
|
36
|
+
- `fetchKeys(host)` is the only function that uses the network: one `GET https://<host>/.well-known/datumesh.json`, no redirects, 10 s timeout, using the platform's `fetch` and TLS verification.
|
|
37
|
+
- `canonicalize(obj, kind)`, `cellHash`, `receiptHash`, `functionHash`, `parseUri(uri, signature)`, `formatUri(ref)` and `keyId` expose canonical forms, hashes and cell URIs. They throw `DatumeshError` (with `.code` and `.path`).
|
|
38
|
+
|
|
39
|
+
Strings are checked against Unicode 15.1 tables shipped with the package, so results do not depend on the JavaScript engine's Unicode version. Ed25519 verification is libsodium-strict, as the spec requires. ESM and CommonJS builds with TypeScript types, for Node 18+ and for browsers through a bundler (the core uses no Node built-ins). Where the specification left a question open, the choices made are listed in [DEFECTS.md](https://github.com/datumesh-com/datumesh-verify-js/blob/main/DEFECTS.md).
|
|
40
|
+
|
|
41
|
+
A valid signature means only that the holder of the key named in `computed_by` stated the value. Whom to trust is your policy.
|
|
42
|
+
|
|
43
|
+
## Development
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
git clone --recurse-submodules https://github.com/datumesh-com/datumesh-verify-js && cd datumesh-verify-js
|
|
47
|
+
npm ci && npm test # all tests; scripts/browser-test.sh and scripts/package-test.sh need Docker
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Links
|
|
51
|
+
|
|
52
|
+
- Datumesh: https://datumesh.com
|
|
53
|
+
- Specification and test vectors: https://github.com/datumesh-com/datumesh-spec
|
|
54
|
+
|
|
55
|
+
Licensed under the Apache License 2.0.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.verifyEd25519Strict = verifyEd25519Strict;
|
|
4
|
+
// Ed25519 verification as SPEC.md 10.1 step 6 requires (libsodium-strict, RFC 8032 cofactorless):
|
|
5
|
+
// - S < L;
|
|
6
|
+
// - A and R canonically encoded (y < p; x = 0 with the sign bit set is rejected);
|
|
7
|
+
// - A and R not of small order;
|
|
8
|
+
// - [S]B - [k]A, encoded, equals R byte for byte, with k = SHA-512(R || A || M) mod L.
|
|
9
|
+
//
|
|
10
|
+
// @noble/curves supplies the group arithmetic. Its own verify() with zip215:false enforces
|
|
11
|
+
// S < L, canonical A and R, and rejects small-order A, but it accepts small-order R and checks
|
|
12
|
+
// the cofactored equation [8]([S]B - R - [k]A) = 0, which accepts some signatures the
|
|
13
|
+
// cofactorless rule rejects. So the checks are done here, and noble's verify() is additionally
|
|
14
|
+
// required to succeed (it is implied by the cofactorless check; it is a second opinion only).
|
|
15
|
+
const ed25519_1 = require("@noble/curves/ed25519");
|
|
16
|
+
const sha2_1 = require("@noble/hashes/sha2");
|
|
17
|
+
const Point = ed25519_1.ed25519.Point;
|
|
18
|
+
const L = 2n ** 252n + 27742317777372353535851937790883648493n;
|
|
19
|
+
const P = 2n ** 255n - 19n;
|
|
20
|
+
function le(b) {
|
|
21
|
+
let n = 0n;
|
|
22
|
+
for (let i = b.length - 1; i >= 0; i--)
|
|
23
|
+
n = (n << 8n) | BigInt(b[i]);
|
|
24
|
+
return n;
|
|
25
|
+
}
|
|
26
|
+
function decodeStrict(b) {
|
|
27
|
+
const y = le(b) & ((1n << 255n) - 1n);
|
|
28
|
+
if (y >= P)
|
|
29
|
+
return null; // non-canonical encoding
|
|
30
|
+
try {
|
|
31
|
+
return Point.fromBytes(b, false); // also rejects x = 0 with the sign bit set
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function equalBytes(a, b) {
|
|
38
|
+
if (a.length !== b.length)
|
|
39
|
+
return false;
|
|
40
|
+
let d = 0;
|
|
41
|
+
for (let i = 0; i < a.length; i++)
|
|
42
|
+
d |= a[i] ^ b[i];
|
|
43
|
+
return d === 0;
|
|
44
|
+
}
|
|
45
|
+
/** Strict Ed25519 verification of `sig` (64 bytes) over `msg` with public key `pub` (32 bytes). */
|
|
46
|
+
function verifyEd25519Strict(sig, msg, pub) {
|
|
47
|
+
if (sig.length !== 64 || pub.length !== 32)
|
|
48
|
+
return false;
|
|
49
|
+
const rBytes = sig.subarray(0, 32);
|
|
50
|
+
const s = le(sig.subarray(32, 64));
|
|
51
|
+
if (s >= L)
|
|
52
|
+
return false;
|
|
53
|
+
const A = decodeStrict(pub);
|
|
54
|
+
const R = decodeStrict(rBytes);
|
|
55
|
+
if (!A || !R)
|
|
56
|
+
return false;
|
|
57
|
+
if (A.isSmallOrder() || R.isSmallOrder())
|
|
58
|
+
return false;
|
|
59
|
+
const h = (0, sha2_1.sha512)(concat(rBytes, pub, msg));
|
|
60
|
+
const k = le(h) % L;
|
|
61
|
+
// R' = [S]B - [k]A (multiplyUnsafe accepts 0; inputs are public, so variable time is fine).
|
|
62
|
+
const SB = s === 0n ? Point.ZERO : Point.BASE.multiplyUnsafe(s);
|
|
63
|
+
const kA = k === 0n ? Point.ZERO : A.multiplyUnsafe(k);
|
|
64
|
+
const rCheck = SB.subtract(kA).toBytes();
|
|
65
|
+
if (!equalBytes(rCheck, rBytes))
|
|
66
|
+
return false;
|
|
67
|
+
try {
|
|
68
|
+
return ed25519_1.ed25519.verify(sig, msg, pub, { zip215: false });
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function concat(...parts) {
|
|
75
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
76
|
+
let o = 0;
|
|
77
|
+
for (const p of parts) {
|
|
78
|
+
out.set(p, o);
|
|
79
|
+
o += p.length;
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** The spec revision this library conforms to. */
|
|
2
|
+
export declare const SPEC_VERSION = "spec-0.1.1";
|
|
3
|
+
/** Every error code defined by SPEC.md 10.2, in the order of that table. */
|
|
4
|
+
export declare const ERROR_CODES: readonly ["E_PARSE_JSON", "E_PARSE_DUPLICATE_KEY", "E_PARSE_URI", "E_SCHEMA", "E_CANON_NAME", "E_CANON_ARG_NAME", "E_CANON_ARG_DUPLICATE", "E_CANON_ARG_ORDER", "E_CANON_PERCENT", "E_CANON_TYPE", "E_CANON_NUMBER", "E_CANON_DATE", "E_CANON_INSTANT", "E_CANON_STRING", "E_CANON_HEX", "E_CANON_LICENCE", "E_CANON_SET", "E_CANON_LIMIT", "E_SLICE", "E_PUBLISHER_MISMATCH", "E_VALID_MISMATCH", "E_TIME_ORDER", "E_ARG_UNKNOWN", "E_ARG_MISSING", "E_KEY_UNAVAILABLE", "E_KEY_DOC", "E_KEY_UNKNOWN", "E_KEY_NOT_VALID", "E_SIGNATURE", "E_FN_UNAVAILABLE", "E_FN_HASH_MISMATCH", "E_ARG_TYPE", "E_RETURN_TYPE", "E_INPUT_UNAVAILABLE", "E_INPUT_INVALID", "E_INPUT_UNDECLARED", "E_VALUE_MISMATCH"];
|
|
5
|
+
export type ErrorCode = (typeof ERROR_CODES)[number];
|
|
6
|
+
/** A Datumesh rule was broken. `code` is a SPEC.md 10.2 code; `path` locates the failure. */
|
|
7
|
+
export declare class DatumeshError extends Error {
|
|
8
|
+
readonly code: ErrorCode;
|
|
9
|
+
readonly path: string | null;
|
|
10
|
+
readonly detail: string;
|
|
11
|
+
constructor(code: ErrorCode, detail: string, path?: string | null);
|
|
12
|
+
}
|
|
13
|
+
/** Shorthand used throughout: throw a DatumeshError. */
|
|
14
|
+
export declare function fail(code: ErrorCode, detail: string, path?: string | null): never;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DatumeshError = exports.ERROR_CODES = exports.SPEC_VERSION = void 0;
|
|
4
|
+
exports.fail = fail;
|
|
5
|
+
/** The spec revision this library conforms to. */
|
|
6
|
+
exports.SPEC_VERSION = 'spec-0.1.1';
|
|
7
|
+
/** Every error code defined by SPEC.md 10.2, in the order of that table. */
|
|
8
|
+
exports.ERROR_CODES = [
|
|
9
|
+
'E_PARSE_JSON',
|
|
10
|
+
'E_PARSE_DUPLICATE_KEY',
|
|
11
|
+
'E_PARSE_URI',
|
|
12
|
+
'E_SCHEMA',
|
|
13
|
+
'E_CANON_NAME',
|
|
14
|
+
'E_CANON_ARG_NAME',
|
|
15
|
+
'E_CANON_ARG_DUPLICATE',
|
|
16
|
+
'E_CANON_ARG_ORDER',
|
|
17
|
+
'E_CANON_PERCENT',
|
|
18
|
+
'E_CANON_TYPE',
|
|
19
|
+
'E_CANON_NUMBER',
|
|
20
|
+
'E_CANON_DATE',
|
|
21
|
+
'E_CANON_INSTANT',
|
|
22
|
+
'E_CANON_STRING',
|
|
23
|
+
'E_CANON_HEX',
|
|
24
|
+
'E_CANON_LICENCE',
|
|
25
|
+
'E_CANON_SET',
|
|
26
|
+
'E_CANON_LIMIT',
|
|
27
|
+
'E_SLICE',
|
|
28
|
+
'E_PUBLISHER_MISMATCH',
|
|
29
|
+
'E_VALID_MISMATCH',
|
|
30
|
+
'E_TIME_ORDER',
|
|
31
|
+
'E_ARG_UNKNOWN',
|
|
32
|
+
'E_ARG_MISSING',
|
|
33
|
+
'E_KEY_UNAVAILABLE',
|
|
34
|
+
'E_KEY_DOC',
|
|
35
|
+
'E_KEY_UNKNOWN',
|
|
36
|
+
'E_KEY_NOT_VALID',
|
|
37
|
+
'E_SIGNATURE',
|
|
38
|
+
'E_FN_UNAVAILABLE',
|
|
39
|
+
'E_FN_HASH_MISMATCH',
|
|
40
|
+
'E_ARG_TYPE',
|
|
41
|
+
'E_RETURN_TYPE',
|
|
42
|
+
'E_INPUT_UNAVAILABLE',
|
|
43
|
+
'E_INPUT_INVALID',
|
|
44
|
+
'E_INPUT_UNDECLARED',
|
|
45
|
+
'E_VALUE_MISMATCH',
|
|
46
|
+
];
|
|
47
|
+
/** A Datumesh rule was broken. `code` is a SPEC.md 10.2 code; `path` locates the failure. */
|
|
48
|
+
class DatumeshError extends Error {
|
|
49
|
+
constructor(code, detail, path = null) {
|
|
50
|
+
super(`${code}${path ? ` at ${path}` : ''}: ${detail}`);
|
|
51
|
+
this.name = 'DatumeshError';
|
|
52
|
+
this.code = code;
|
|
53
|
+
this.path = path;
|
|
54
|
+
this.detail = detail;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.DatumeshError = DatumeshError;
|
|
58
|
+
/** Shorthand used throughout: throw a DatumeshError. */
|
|
59
|
+
function fail(code, detail, path = null) {
|
|
60
|
+
throw new DatumeshError(code, detail, path);
|
|
61
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function toHex(b: Uint8Array): string;
|
|
2
|
+
export declare function fromHex(s: string): Uint8Array;
|
|
3
|
+
/** SHA-256 in lowercase hex. */
|
|
4
|
+
export declare function sha256Hex(b: Uint8Array): string;
|
|
5
|
+
/** SPEC 9.1: first 16 hex digits of SHA-256 over the raw 32-byte public key. */
|
|
6
|
+
export declare function keyIdOf(publicKey: Uint8Array): string;
|
package/dist/cjs/hash.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toHex = toHex;
|
|
4
|
+
exports.fromHex = fromHex;
|
|
5
|
+
exports.sha256Hex = sha256Hex;
|
|
6
|
+
exports.keyIdOf = keyIdOf;
|
|
7
|
+
const sha2_1 = require("@noble/hashes/sha2");
|
|
8
|
+
function toHex(b) {
|
|
9
|
+
let s = '';
|
|
10
|
+
for (let i = 0; i < b.length; i++)
|
|
11
|
+
s += b[i].toString(16).padStart(2, '0');
|
|
12
|
+
return s;
|
|
13
|
+
}
|
|
14
|
+
function fromHex(s) {
|
|
15
|
+
const out = new Uint8Array(s.length / 2);
|
|
16
|
+
for (let i = 0; i < out.length; i++)
|
|
17
|
+
out[i] = parseInt(s.slice(2 * i, 2 * i + 2), 16);
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
/** SHA-256 in lowercase hex. */
|
|
21
|
+
function sha256Hex(b) {
|
|
22
|
+
return toHex((0, sha2_1.sha256)(b));
|
|
23
|
+
}
|
|
24
|
+
/** SPEC 9.1: first 16 hex digits of SHA-256 over the raw 32-byte public key. */
|
|
25
|
+
function keyIdOf(publicKey) {
|
|
26
|
+
return sha256Hex(publicKey).slice(0, 16);
|
|
27
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Kind } from './validate.js';
|
|
2
|
+
export { DatumeshError, ERROR_CODES, SPEC_VERSION, type ErrorCode } from './errors.js';
|
|
3
|
+
export { verify, type VerifyResult, type Recompute } from './verify.js';
|
|
4
|
+
export { parseUri, formatUri, httpsUrl, type CellRef, type TypedScalar, type Signature } from './uri.js';
|
|
5
|
+
export { keysFromResponse, keysFromDocument, fetchKeys, FETCH_TIMEOUT_MS, type KeysResult, type KeyEntry, type KeyMap, type KeySource } from './keys.js';
|
|
6
|
+
export type { Kind } from './validate.js';
|
|
7
|
+
export type { JsonValue } from './json.js';
|
|
8
|
+
/** Library version. */
|
|
9
|
+
export declare const VERSION = "0.1.1";
|
|
10
|
+
/**
|
|
11
|
+
* Validate `input` as `kind` and return its RFC 8785 canonical bytes. `input` is JSON text,
|
|
12
|
+
* UTF-8 bytes, or an already-parsed value. `kind` is "scalar", "cell", "ref", "function" or
|
|
13
|
+
* "receipt" (inferred from the members when omitted), or "jcs" for plain RFC 8785 with only the
|
|
14
|
+
* strict-parsing rules of SPEC 6.1. A receipt is returned whole, `sig` included, and is checked
|
|
15
|
+
* through SPEC 10.1 step 4. Throws DatumeshError.
|
|
16
|
+
*/
|
|
17
|
+
export declare function canonicalize(input: unknown, kind?: Kind | null): Uint8Array;
|
|
18
|
+
/** Cell id (SPEC 4.4): SHA-256 of the cell's canonical bytes, lowercase hex. Throws DatumeshError. */
|
|
19
|
+
export declare function cellHash(cell: unknown): string;
|
|
20
|
+
/** Function hash (SPEC 7): SHA-256 of the descriptor's canonical bytes. Throws DatumeshError. */
|
|
21
|
+
export declare function functionHash(descriptor: unknown): string;
|
|
22
|
+
/** Receipt hash (SPEC 8.3): SHA-256 of the canonical receipt without `sig`. Throws DatumeshError. */
|
|
23
|
+
export declare function receiptHash(receipt: unknown): string;
|
|
24
|
+
/** The signed payload of a receipt (SPEC 8.3): canonical bytes without `sig`. Throws DatumeshError. */
|
|
25
|
+
export declare function receiptPayload(receipt: unknown): Uint8Array;
|
|
26
|
+
/** Key id of an Ed25519 public key given as 64 lowercase hex digits (SPEC 9.1). Throws DatumeshError. */
|
|
27
|
+
export declare function keyId(publicKeyHex: string): string;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VERSION = exports.FETCH_TIMEOUT_MS = exports.fetchKeys = exports.keysFromDocument = exports.keysFromResponse = exports.httpsUrl = exports.formatUri = exports.parseUri = exports.verify = exports.SPEC_VERSION = exports.ERROR_CODES = exports.DatumeshError = void 0;
|
|
4
|
+
exports.canonicalize = canonicalize;
|
|
5
|
+
exports.cellHash = cellHash;
|
|
6
|
+
exports.functionHash = functionHash;
|
|
7
|
+
exports.receiptHash = receiptHash;
|
|
8
|
+
exports.receiptPayload = receiptPayload;
|
|
9
|
+
exports.keyId = keyId;
|
|
10
|
+
/**
|
|
11
|
+
* @datumesh/verify: verify Datumesh receipts (SPEC.md, revision spec-0.1.1).
|
|
12
|
+
*
|
|
13
|
+
* Everything here is synchronous and offline except fetchKeys, the only function that uses the
|
|
14
|
+
* network.
|
|
15
|
+
*/
|
|
16
|
+
const errors_js_1 = require("./errors.js");
|
|
17
|
+
const hash_js_1 = require("./hash.js");
|
|
18
|
+
const json_js_1 = require("./json.js");
|
|
19
|
+
const jcs_js_1 = require("./jcs.js");
|
|
20
|
+
const lexical_js_1 = require("./lexical.js");
|
|
21
|
+
const validate_js_1 = require("./validate.js");
|
|
22
|
+
var errors_js_2 = require("./errors.js");
|
|
23
|
+
Object.defineProperty(exports, "DatumeshError", { enumerable: true, get: function () { return errors_js_2.DatumeshError; } });
|
|
24
|
+
Object.defineProperty(exports, "ERROR_CODES", { enumerable: true, get: function () { return errors_js_2.ERROR_CODES; } });
|
|
25
|
+
Object.defineProperty(exports, "SPEC_VERSION", { enumerable: true, get: function () { return errors_js_2.SPEC_VERSION; } });
|
|
26
|
+
var verify_js_1 = require("./verify.js");
|
|
27
|
+
Object.defineProperty(exports, "verify", { enumerable: true, get: function () { return verify_js_1.verify; } });
|
|
28
|
+
var uri_js_1 = require("./uri.js");
|
|
29
|
+
Object.defineProperty(exports, "parseUri", { enumerable: true, get: function () { return uri_js_1.parseUri; } });
|
|
30
|
+
Object.defineProperty(exports, "formatUri", { enumerable: true, get: function () { return uri_js_1.formatUri; } });
|
|
31
|
+
Object.defineProperty(exports, "httpsUrl", { enumerable: true, get: function () { return uri_js_1.httpsUrl; } });
|
|
32
|
+
var keys_js_1 = require("./keys.js");
|
|
33
|
+
Object.defineProperty(exports, "keysFromResponse", { enumerable: true, get: function () { return keys_js_1.keysFromResponse; } });
|
|
34
|
+
Object.defineProperty(exports, "keysFromDocument", { enumerable: true, get: function () { return keys_js_1.keysFromDocument; } });
|
|
35
|
+
Object.defineProperty(exports, "fetchKeys", { enumerable: true, get: function () { return keys_js_1.fetchKeys; } });
|
|
36
|
+
Object.defineProperty(exports, "FETCH_TIMEOUT_MS", { enumerable: true, get: function () { return keys_js_1.FETCH_TIMEOUT_MS; } });
|
|
37
|
+
/** Library version. */
|
|
38
|
+
exports.VERSION = '0.1.1';
|
|
39
|
+
function parseAndValidate(input, kind) {
|
|
40
|
+
if (kind !== undefined && kind !== null && !validate_js_1.KINDS.includes(kind))
|
|
41
|
+
throw new TypeError(`unknown kind ${JSON.stringify(kind)}; expected one of ${validate_js_1.KINDS.join(', ')}`);
|
|
42
|
+
const value = (0, json_js_1.toJson)(input);
|
|
43
|
+
const k = kind ?? (0, validate_js_1.inferKind)(value);
|
|
44
|
+
(0, validate_js_1.validate)(value, k);
|
|
45
|
+
return { value, kind: k };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Validate `input` as `kind` and return its RFC 8785 canonical bytes. `input` is JSON text,
|
|
49
|
+
* UTF-8 bytes, or an already-parsed value. `kind` is "scalar", "cell", "ref", "function" or
|
|
50
|
+
* "receipt" (inferred from the members when omitted), or "jcs" for plain RFC 8785 with only the
|
|
51
|
+
* strict-parsing rules of SPEC 6.1. A receipt is returned whole, `sig` included, and is checked
|
|
52
|
+
* through SPEC 10.1 step 4. Throws DatumeshError.
|
|
53
|
+
*/
|
|
54
|
+
function canonicalize(input, kind) {
|
|
55
|
+
return (0, jcs_js_1.jcsBytes)(parseAndValidate(input, kind).value);
|
|
56
|
+
}
|
|
57
|
+
/** Cell id (SPEC 4.4): SHA-256 of the cell's canonical bytes, lowercase hex. Throws DatumeshError. */
|
|
58
|
+
function cellHash(cell) {
|
|
59
|
+
return (0, hash_js_1.sha256Hex)((0, jcs_js_1.jcsBytes)(parseAndValidate(cell, 'cell').value));
|
|
60
|
+
}
|
|
61
|
+
/** Function hash (SPEC 7): SHA-256 of the descriptor's canonical bytes. Throws DatumeshError. */
|
|
62
|
+
function functionHash(descriptor) {
|
|
63
|
+
return (0, hash_js_1.sha256Hex)((0, jcs_js_1.jcsBytes)(parseAndValidate(descriptor, 'function').value));
|
|
64
|
+
}
|
|
65
|
+
/** Receipt hash (SPEC 8.3): SHA-256 of the canonical receipt without `sig`. Throws DatumeshError. */
|
|
66
|
+
function receiptHash(receipt) {
|
|
67
|
+
return (0, hash_js_1.sha256Hex)(receiptPayload(receipt));
|
|
68
|
+
}
|
|
69
|
+
/** The signed payload of a receipt (SPEC 8.3): canonical bytes without `sig`. Throws DatumeshError. */
|
|
70
|
+
function receiptPayload(receipt) {
|
|
71
|
+
const r = parseAndValidate(receipt, 'receipt').value;
|
|
72
|
+
const o = (0, json_js_1.newObject)();
|
|
73
|
+
for (const k of Object.keys(r))
|
|
74
|
+
if (k !== 'sig')
|
|
75
|
+
o[k] = r[k];
|
|
76
|
+
return (0, jcs_js_1.jcsBytes)(o);
|
|
77
|
+
}
|
|
78
|
+
/** Key id of an Ed25519 public key given as 64 lowercase hex digits (SPEC 9.1). Throws DatumeshError. */
|
|
79
|
+
function keyId(publicKeyHex) {
|
|
80
|
+
if (typeof publicKeyHex !== 'string' || !(0, lexical_js_1.isHex)(publicKeyHex, 64))
|
|
81
|
+
(0, errors_js_1.fail)('E_CANON_HEX', 'expected 64 lowercase hex digits', '$');
|
|
82
|
+
return (0, hash_js_1.keyIdOf)((0, hash_js_1.fromHex)(publicKeyHex));
|
|
83
|
+
}
|
package/dist/cjs/jcs.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.jcsText = jcsText;
|
|
7
|
+
exports.jcsBytes = jcsBytes;
|
|
8
|
+
// RFC 8785 JSON Canonicalization Scheme. Serialisation is delegated to the `canonicalize`
|
|
9
|
+
// package (the RFC authors' implementation), which relies on ECMAScript JSON.stringify for
|
|
10
|
+
// strings and numbers and on the default (UTF-16 code unit) sort for member names, exactly as
|
|
11
|
+
// RFC 8785 specifies. It is only ever given values produced by ./json.ts (plain data, finite
|
|
12
|
+
// numbers, well-formed strings), so its toJSON/undefined handling never comes into play.
|
|
13
|
+
const canonicalize_1 = __importDefault(require("canonicalize"));
|
|
14
|
+
const utf8_js_1 = require("./utf8.js");
|
|
15
|
+
/** RFC 8785 canonical text of a JSON value. */
|
|
16
|
+
function jcsText(v) {
|
|
17
|
+
const s = (0, canonicalize_1.default)(v);
|
|
18
|
+
if (typeof s !== 'string')
|
|
19
|
+
throw new Error('canonicalize returned no text');
|
|
20
|
+
return s;
|
|
21
|
+
}
|
|
22
|
+
/** RFC 8785 canonical bytes (UTF-8) of a JSON value. */
|
|
23
|
+
function jcsBytes(v) {
|
|
24
|
+
return (0, utf8_js_1.utf8Encode)(jcsText(v));
|
|
25
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type JsonValue = null | boolean | number | string | JsonValue[] | JsonObject;
|
|
2
|
+
export interface JsonObject {
|
|
3
|
+
[key: string]: JsonValue;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Nesting deeper than this is rejected with E_PARSE_JSON. SPEC.md sets no depth limit; this is
|
|
7
|
+
* an implementation limit (Datumesh objects nest at most five levels) that keeps parsing and
|
|
8
|
+
* canonicalisation from exhausting the call stack.
|
|
9
|
+
*/
|
|
10
|
+
export declare const MAX_DEPTH = 1000;
|
|
11
|
+
export declare function isObject(v: unknown): v is JsonObject;
|
|
12
|
+
export declare function newObject(): JsonObject;
|
|
13
|
+
export declare function has(o: JsonObject, k: string): boolean;
|
|
14
|
+
/** Member names in JCS order (UTF-16 code units, which is JavaScript's default sort). */
|
|
15
|
+
export declare function sortedKeys(o: JsonObject): string[];
|
|
16
|
+
export declare function memberPath(path: string, name: string): string;
|
|
17
|
+
/** Parse JSON text strictly. */
|
|
18
|
+
export declare function parseJsonText(text: string): JsonValue;
|
|
19
|
+
/** Parse UTF-8 JSON bytes strictly: no BOM, no invalid UTF-8. */
|
|
20
|
+
export declare function parseJsonBytes(bytes: Uint8Array): JsonValue;
|
|
21
|
+
/**
|
|
22
|
+
* Check an already-parsed JavaScript value against the JSON data model and return a private
|
|
23
|
+
* deep copy (plain data with null-prototype objects), so later checks cannot be affected by
|
|
24
|
+
* getters, prototypes or mutation. Duplicate members cannot be detected here: pass JSON text
|
|
25
|
+
* or bytes for that.
|
|
26
|
+
*/
|
|
27
|
+
export declare function normalizeValue(v: unknown, path?: string, depth?: number): JsonValue;
|
|
28
|
+
/** JSON text (string), UTF-8 bytes (Uint8Array) or an already-parsed value. */
|
|
29
|
+
export declare function toJson(input: unknown): JsonValue;
|