@bolyra/receipts 0.11.0 → 0.11.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/dist/canonical.d.ts +21 -0
- package/dist/canonical.js +28 -0
- package/dist/chain.js +16 -1
- package/dist/verify-cli.js +42 -11
- package/package.json +3 -2
- package/src/canonical.ts +29 -0
- package/src/chain.ts +15 -1
- package/src/verify-cli.ts +39 -11
package/dist/canonical.d.ts
CHANGED
|
@@ -1 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic JSON: object keys sorted, arrays in order.
|
|
3
|
+
*
|
|
4
|
+
* Throws a TypeError when an object that JSON.stringify visits as a member of
|
|
5
|
+
* the value (at any depth, including objects inside arrays) has an OWN key
|
|
6
|
+
* named "__proto__" (JSON.parse creates one from the text `{"__proto__": ...}`).
|
|
7
|
+
* The guard covers plain JSON data. It does not see non-index properties set
|
|
8
|
+
* on an array, or the original object when a `toJSON` method replaces it.
|
|
9
|
+
* The sorted copy below is built by assignment, and assigning to that name
|
|
10
|
+
* sets the copy's prototype instead of creating a key, so the member would be
|
|
11
|
+
* absent from the output. A value with such a member therefore has no exact
|
|
12
|
+
* canonical form here, and it is refused rather than serialized without it.
|
|
13
|
+
*
|
|
14
|
+
* Also throws a TypeError for a number that is not finite, or is negative
|
|
15
|
+
* zero. JSON.stringify writes those as `null` and `0`, so each would share a
|
|
16
|
+
* canonical form with a different value. JSON.parse yields them from ordinary
|
|
17
|
+
* text such as `1e400` and `-0`.
|
|
18
|
+
*
|
|
19
|
+
* Callers that verify (verifyReceipt, verifyReceiptChain) treat the throw as
|
|
20
|
+
* a failed verification; callers that sign or hash propagate it.
|
|
21
|
+
*/
|
|
1
22
|
export declare function canonicalize(obj: unknown): string;
|
package/dist/canonical.js
CHANGED
|
@@ -1,14 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.canonicalize = canonicalize;
|
|
4
|
+
const UNREPRESENTABLE_KEY = '__proto__';
|
|
5
|
+
/**
|
|
6
|
+
* Deterministic JSON: object keys sorted, arrays in order.
|
|
7
|
+
*
|
|
8
|
+
* Throws a TypeError when an object that JSON.stringify visits as a member of
|
|
9
|
+
* the value (at any depth, including objects inside arrays) has an OWN key
|
|
10
|
+
* named "__proto__" (JSON.parse creates one from the text `{"__proto__": ...}`).
|
|
11
|
+
* The guard covers plain JSON data. It does not see non-index properties set
|
|
12
|
+
* on an array, or the original object when a `toJSON` method replaces it.
|
|
13
|
+
* The sorted copy below is built by assignment, and assigning to that name
|
|
14
|
+
* sets the copy's prototype instead of creating a key, so the member would be
|
|
15
|
+
* absent from the output. A value with such a member therefore has no exact
|
|
16
|
+
* canonical form here, and it is refused rather than serialized without it.
|
|
17
|
+
*
|
|
18
|
+
* Also throws a TypeError for a number that is not finite, or is negative
|
|
19
|
+
* zero. JSON.stringify writes those as `null` and `0`, so each would share a
|
|
20
|
+
* canonical form with a different value. JSON.parse yields them from ordinary
|
|
21
|
+
* text such as `1e400` and `-0`.
|
|
22
|
+
*
|
|
23
|
+
* Callers that verify (verifyReceipt, verifyReceiptChain) treat the throw as
|
|
24
|
+
* a failed verification; callers that sign or hash propagate it.
|
|
25
|
+
*/
|
|
4
26
|
function canonicalize(obj) {
|
|
5
27
|
return JSON.stringify(obj, (_key, value) => {
|
|
6
28
|
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
29
|
+
if (Object.prototype.hasOwnProperty.call(value, UNREPRESENTABLE_KEY)) {
|
|
30
|
+
throw new TypeError('canonicalize: an object has an own "__proto__" key, which has no canonical form');
|
|
31
|
+
}
|
|
7
32
|
return Object.keys(value).sort().reduce((sorted, k) => {
|
|
8
33
|
sorted[k] = value[k];
|
|
9
34
|
return sorted;
|
|
10
35
|
}, {});
|
|
11
36
|
}
|
|
37
|
+
if (typeof value === 'number' && (!Number.isFinite(value) || Object.is(value, -0))) {
|
|
38
|
+
throw new TypeError('canonicalize: a number is non-finite or negative zero, which has no canonical form');
|
|
39
|
+
}
|
|
12
40
|
return value;
|
|
13
41
|
});
|
|
14
42
|
}
|
package/dist/chain.js
CHANGED
|
@@ -153,7 +153,22 @@ function verifyReceiptChain(receipts, options = {}) {
|
|
|
153
153
|
}
|
|
154
154
|
chained += 1;
|
|
155
155
|
// 3. Link check against the previous CHAINED receipt.
|
|
156
|
-
|
|
156
|
+
// A receipt whose content has no canonical form cannot be hashed. It is
|
|
157
|
+
// reported and skipped: `prev` and the head stay at the last hashable
|
|
158
|
+
// receipt, so the following link fails too. This function never throws.
|
|
159
|
+
let actualHash;
|
|
160
|
+
try {
|
|
161
|
+
actualHash = computeReceiptHash(receipt);
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
issues.push({
|
|
165
|
+
index,
|
|
166
|
+
receiptId: typeof id === 'string' ? id : undefined,
|
|
167
|
+
code: 'malformed-receipt',
|
|
168
|
+
message: `entry #${index} cannot be hashed (its content has no canonical form)`,
|
|
169
|
+
});
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
157
172
|
if (prev === undefined) {
|
|
158
173
|
if (chain.seq !== 0 || chain.prevReceiptHash !== exports.GENESIS_PREV_RECEIPT_HASH) {
|
|
159
174
|
issues.push({
|
package/dist/verify-cli.js
CHANGED
|
@@ -129,6 +129,10 @@ function failCheck(msg) {
|
|
|
129
129
|
}
|
|
130
130
|
// 1. Schema validation
|
|
131
131
|
function validateSchema() {
|
|
132
|
+
if (receipt === null || typeof receipt !== 'object') {
|
|
133
|
+
failCheck('Schema: receipt is not an object');
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
132
136
|
const p = receipt.payload;
|
|
133
137
|
const s = receipt.signature;
|
|
134
138
|
const errors = [];
|
|
@@ -182,6 +186,9 @@ function validateSchema() {
|
|
|
182
186
|
if (!p.proof) {
|
|
183
187
|
errors.push('payload.proof missing');
|
|
184
188
|
}
|
|
189
|
+
else if (typeof p.proof !== 'object') {
|
|
190
|
+
errors.push('payload.proof not an object');
|
|
191
|
+
}
|
|
185
192
|
else {
|
|
186
193
|
for (const k of ['bundleVersion', 'nonce', 'humanProofHash', 'agentProofHash', 'publicSignalsHash']) {
|
|
187
194
|
if (!(k in p.proof))
|
|
@@ -216,11 +223,11 @@ function validateSchema() {
|
|
|
216
223
|
errors.push('signature.alg !== "ES256K"');
|
|
217
224
|
if (s.keyId !== p.keyId)
|
|
218
225
|
errors.push('signature.keyId does not match payload.keyId');
|
|
219
|
-
if (!/^0x[0-9a-fA-F]{40}$/.test(s.signer
|
|
226
|
+
if (typeof s.signer !== 'string' || !/^0x[0-9a-fA-F]{40}$/.test(s.signer))
|
|
220
227
|
errors.push('signature.signer invalid format');
|
|
221
|
-
if (!/^0x[0-9a-fA-F]{64}$/.test(s.payloadHash
|
|
228
|
+
if (typeof s.payloadHash !== 'string' || !/^0x[0-9a-fA-F]{64}$/.test(s.payloadHash))
|
|
222
229
|
errors.push('signature.payloadHash invalid format');
|
|
223
|
-
if (!/^0x[0-9a-fA-F]{130}$/.test(s.value
|
|
230
|
+
if (typeof s.value !== 'string' || !/^0x[0-9a-fA-F]{130}$/.test(s.value))
|
|
224
231
|
errors.push('signature.value invalid format');
|
|
225
232
|
if (errors.length) {
|
|
226
233
|
failCheck(`Schema: ${errors.join('; ')}`);
|
|
@@ -235,7 +242,12 @@ function validateTimestamp() {
|
|
|
235
242
|
const issuedAt = receipt.payload.issuedAt;
|
|
236
243
|
const allowFutureSeconds = 300;
|
|
237
244
|
const ageSec = nowSec - issuedAt;
|
|
238
|
-
const
|
|
245
|
+
const date = new Date(issuedAt * 1000);
|
|
246
|
+
if (!Number.isFinite(date.getTime())) {
|
|
247
|
+
failCheck('Timestamp is outside the supported date range');
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const isoStr = date.toISOString();
|
|
239
251
|
if (issuedAt > nowSec + allowFutureSeconds) {
|
|
240
252
|
failCheck(`Timestamp: ${isoStr} is ${issuedAt - nowSec}s in the future (max skew: ${allowFutureSeconds}s)`);
|
|
241
253
|
return;
|
|
@@ -248,7 +260,15 @@ function validateTimestamp() {
|
|
|
248
260
|
}
|
|
249
261
|
// 3. Hash check
|
|
250
262
|
function validateHash() {
|
|
251
|
-
|
|
263
|
+
let computed;
|
|
264
|
+
try {
|
|
265
|
+
computed = (0, sign_1.hashPayload)(receipt.payload);
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
// The payload holds a value with no canonical form, so it has no hash.
|
|
269
|
+
failCheck('Payload cannot be canonicalized');
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
252
272
|
if (computed !== receipt.signature.payloadHash) {
|
|
253
273
|
failCheck(`Payload hash mismatch (expected ${truncate(receipt.signature.payloadHash)}, got ${truncate(computed)})`);
|
|
254
274
|
return false;
|
|
@@ -259,6 +279,10 @@ function validateHash() {
|
|
|
259
279
|
// 4. ID check
|
|
260
280
|
function validateId() {
|
|
261
281
|
const expected = '0x' + receipt.signature.payloadHash.slice(2, 18);
|
|
282
|
+
if (typeof receipt.id !== 'string') {
|
|
283
|
+
failCheck(`Receipt ID is not a string (expected ${expected})`);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
262
286
|
if (receipt.id !== expected) {
|
|
263
287
|
failCheck(`Receipt ID mismatch (expected ${expected}, got ${receipt.id})`);
|
|
264
288
|
return;
|
|
@@ -289,14 +313,21 @@ function validateInstance() {
|
|
|
289
313
|
}
|
|
290
314
|
}
|
|
291
315
|
// --- Run checks ---
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
316
|
+
// A receipt is untrusted input. Whatever shape it has, the outcome is a list
|
|
317
|
+
// of checks and an exit code, never an uncaught error.
|
|
318
|
+
try {
|
|
319
|
+
if (validateSchema()) {
|
|
320
|
+
validateTimestamp();
|
|
321
|
+
if (validateHash()) {
|
|
322
|
+
validateId();
|
|
323
|
+
validateSignature();
|
|
324
|
+
validateInstance();
|
|
325
|
+
}
|
|
298
326
|
}
|
|
299
327
|
}
|
|
328
|
+
catch {
|
|
329
|
+
failCheck('Receipt could not be checked');
|
|
330
|
+
}
|
|
300
331
|
// --- Output ---
|
|
301
332
|
for (const line of checks) {
|
|
302
333
|
console.log(line);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bolyra/receipts",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.1",
|
|
4
4
|
"description": "Signed auth receipts for Bolyra ZKP verification decisions — canonical JSON, secp256k1 sign/verify, EVM-compatible r||s||v signatures.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -36,7 +36,8 @@
|
|
|
36
36
|
"url": "https://github.com/bolyra/bolyra"
|
|
37
37
|
},
|
|
38
38
|
"publishConfig": {
|
|
39
|
-
"access": "public"
|
|
39
|
+
"access": "public",
|
|
40
|
+
"tag": "release-0.11"
|
|
40
41
|
},
|
|
41
42
|
"keywords": [
|
|
42
43
|
"bolyra",
|
package/src/canonical.ts
CHANGED
|
@@ -1,11 +1,40 @@
|
|
|
1
|
+
const UNREPRESENTABLE_KEY = '__proto__';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Deterministic JSON: object keys sorted, arrays in order.
|
|
5
|
+
*
|
|
6
|
+
* Throws a TypeError when an object that JSON.stringify visits as a member of
|
|
7
|
+
* the value (at any depth, including objects inside arrays) has an OWN key
|
|
8
|
+
* named "__proto__" (JSON.parse creates one from the text `{"__proto__": ...}`).
|
|
9
|
+
* The guard covers plain JSON data. It does not see non-index properties set
|
|
10
|
+
* on an array, or the original object when a `toJSON` method replaces it.
|
|
11
|
+
* The sorted copy below is built by assignment, and assigning to that name
|
|
12
|
+
* sets the copy's prototype instead of creating a key, so the member would be
|
|
13
|
+
* absent from the output. A value with such a member therefore has no exact
|
|
14
|
+
* canonical form here, and it is refused rather than serialized without it.
|
|
15
|
+
*
|
|
16
|
+
* Also throws a TypeError for a number that is not finite, or is negative
|
|
17
|
+
* zero. JSON.stringify writes those as `null` and `0`, so each would share a
|
|
18
|
+
* canonical form with a different value. JSON.parse yields them from ordinary
|
|
19
|
+
* text such as `1e400` and `-0`.
|
|
20
|
+
*
|
|
21
|
+
* Callers that verify (verifyReceipt, verifyReceiptChain) treat the throw as
|
|
22
|
+
* a failed verification; callers that sign or hash propagate it.
|
|
23
|
+
*/
|
|
1
24
|
export function canonicalize(obj: unknown): string {
|
|
2
25
|
return JSON.stringify(obj, (_key, value) => {
|
|
3
26
|
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
27
|
+
if (Object.prototype.hasOwnProperty.call(value, UNREPRESENTABLE_KEY)) {
|
|
28
|
+
throw new TypeError('canonicalize: an object has an own "__proto__" key, which has no canonical form');
|
|
29
|
+
}
|
|
4
30
|
return Object.keys(value).sort().reduce((sorted, k) => {
|
|
5
31
|
sorted[k] = (value as Record<string, unknown>)[k];
|
|
6
32
|
return sorted;
|
|
7
33
|
}, {} as Record<string, unknown>);
|
|
8
34
|
}
|
|
35
|
+
if (typeof value === 'number' && (!Number.isFinite(value) || Object.is(value, -0))) {
|
|
36
|
+
throw new TypeError('canonicalize: a number is non-finite or negative zero, which has no canonical form');
|
|
37
|
+
}
|
|
9
38
|
return value;
|
|
10
39
|
});
|
|
11
40
|
}
|
package/src/chain.ts
CHANGED
|
@@ -216,7 +216,21 @@ export function verifyReceiptChain(
|
|
|
216
216
|
chained += 1;
|
|
217
217
|
|
|
218
218
|
// 3. Link check against the previous CHAINED receipt.
|
|
219
|
-
|
|
219
|
+
// A receipt whose content has no canonical form cannot be hashed. It is
|
|
220
|
+
// reported and skipped: `prev` and the head stay at the last hashable
|
|
221
|
+
// receipt, so the following link fails too. This function never throws.
|
|
222
|
+
let actualHash: string;
|
|
223
|
+
try {
|
|
224
|
+
actualHash = computeReceiptHash(receipt);
|
|
225
|
+
} catch {
|
|
226
|
+
issues.push({
|
|
227
|
+
index,
|
|
228
|
+
receiptId: typeof id === 'string' ? id : undefined,
|
|
229
|
+
code: 'malformed-receipt',
|
|
230
|
+
message: `entry #${index} cannot be hashed (its content has no canonical form)`,
|
|
231
|
+
});
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
220
234
|
if (prev === undefined) {
|
|
221
235
|
if (chain.seq !== 0 || chain.prevReceiptHash !== GENESIS_PREV_RECEIPT_HASH) {
|
|
222
236
|
issues.push({
|
package/src/verify-cli.ts
CHANGED
|
@@ -104,6 +104,10 @@ function failCheck(msg: string): void {
|
|
|
104
104
|
|
|
105
105
|
// 1. Schema validation
|
|
106
106
|
function validateSchema(): boolean {
|
|
107
|
+
if (receipt === null || typeof receipt !== 'object') {
|
|
108
|
+
failCheck('Schema: receipt is not an object');
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
107
111
|
const p = receipt.payload;
|
|
108
112
|
const s = receipt.signature;
|
|
109
113
|
const errors: string[] = [];
|
|
@@ -144,6 +148,8 @@ function validateSchema(): boolean {
|
|
|
144
148
|
// proof
|
|
145
149
|
if (!p.proof) {
|
|
146
150
|
errors.push('payload.proof missing');
|
|
151
|
+
} else if (typeof p.proof !== 'object') {
|
|
152
|
+
errors.push('payload.proof not an object');
|
|
147
153
|
} else {
|
|
148
154
|
for (const k of ['bundleVersion', 'nonce', 'humanProofHash', 'agentProofHash', 'publicSignalsHash'] as const) {
|
|
149
155
|
if (!(k in p.proof)) errors.push(`proof.${k} missing`);
|
|
@@ -172,9 +178,9 @@ function validateSchema(): boolean {
|
|
|
172
178
|
// signature fields
|
|
173
179
|
if (s.alg !== 'ES256K') errors.push('signature.alg !== "ES256K"');
|
|
174
180
|
if (s.keyId !== p.keyId) errors.push('signature.keyId does not match payload.keyId');
|
|
175
|
-
if (!/^0x[0-9a-fA-F]{40}$/.test(s.signer
|
|
176
|
-
if (!/^0x[0-9a-fA-F]{64}$/.test(s.payloadHash
|
|
177
|
-
if (!/^0x[0-9a-fA-F]{130}$/.test(s.value
|
|
181
|
+
if (typeof s.signer !== 'string' || !/^0x[0-9a-fA-F]{40}$/.test(s.signer)) errors.push('signature.signer invalid format');
|
|
182
|
+
if (typeof s.payloadHash !== 'string' || !/^0x[0-9a-fA-F]{64}$/.test(s.payloadHash)) errors.push('signature.payloadHash invalid format');
|
|
183
|
+
if (typeof s.value !== 'string' || !/^0x[0-9a-fA-F]{130}$/.test(s.value)) errors.push('signature.value invalid format');
|
|
178
184
|
|
|
179
185
|
if (errors.length) {
|
|
180
186
|
failCheck(`Schema: ${errors.join('; ')}`);
|
|
@@ -190,7 +196,12 @@ function validateTimestamp(): void {
|
|
|
190
196
|
const issuedAt = receipt.payload.issuedAt;
|
|
191
197
|
const allowFutureSeconds = 300;
|
|
192
198
|
const ageSec = nowSec - issuedAt;
|
|
193
|
-
const
|
|
199
|
+
const date = new Date(issuedAt * 1000);
|
|
200
|
+
if (!Number.isFinite(date.getTime())) {
|
|
201
|
+
failCheck('Timestamp is outside the supported date range');
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const isoStr = date.toISOString();
|
|
194
205
|
|
|
195
206
|
if (issuedAt > nowSec + allowFutureSeconds) {
|
|
196
207
|
failCheck(`Timestamp: ${isoStr} is ${issuedAt - nowSec}s in the future (max skew: ${allowFutureSeconds}s)`);
|
|
@@ -205,7 +216,14 @@ function validateTimestamp(): void {
|
|
|
205
216
|
|
|
206
217
|
// 3. Hash check
|
|
207
218
|
function validateHash(): boolean {
|
|
208
|
-
|
|
219
|
+
let computed: string;
|
|
220
|
+
try {
|
|
221
|
+
computed = hashPayload(receipt.payload);
|
|
222
|
+
} catch {
|
|
223
|
+
// The payload holds a value with no canonical form, so it has no hash.
|
|
224
|
+
failCheck('Payload cannot be canonicalized');
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
209
227
|
if (computed !== receipt.signature.payloadHash) {
|
|
210
228
|
failCheck(`Payload hash mismatch (expected ${truncate(receipt.signature.payloadHash)}, got ${truncate(computed)})`);
|
|
211
229
|
return false;
|
|
@@ -217,6 +235,10 @@ function validateHash(): boolean {
|
|
|
217
235
|
// 4. ID check
|
|
218
236
|
function validateId(): void {
|
|
219
237
|
const expected = '0x' + receipt.signature.payloadHash.slice(2, 18);
|
|
238
|
+
if (typeof receipt.id !== 'string') {
|
|
239
|
+
failCheck(`Receipt ID is not a string (expected ${expected})`);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
220
242
|
if (receipt.id !== expected) {
|
|
221
243
|
failCheck(`Receipt ID mismatch (expected ${expected}, got ${receipt.id})`);
|
|
222
244
|
return;
|
|
@@ -251,13 +273,19 @@ function validateInstance(): void {
|
|
|
251
273
|
|
|
252
274
|
// --- Run checks ---
|
|
253
275
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
276
|
+
// A receipt is untrusted input. Whatever shape it has, the outcome is a list
|
|
277
|
+
// of checks and an exit code, never an uncaught error.
|
|
278
|
+
try {
|
|
279
|
+
if (validateSchema()) {
|
|
280
|
+
validateTimestamp();
|
|
281
|
+
if (validateHash()) {
|
|
282
|
+
validateId();
|
|
283
|
+
validateSignature();
|
|
284
|
+
validateInstance();
|
|
285
|
+
}
|
|
260
286
|
}
|
|
287
|
+
} catch {
|
|
288
|
+
failCheck('Receipt could not be checked');
|
|
261
289
|
}
|
|
262
290
|
|
|
263
291
|
// --- Output ---
|