@bolyra/receipts 0.10.1 → 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.
@@ -29,7 +29,11 @@ export interface InstancePreimage {
29
29
  audience: string;
30
30
  /** Binding program discriminator (e.g. "x402", "mpp"). */
31
31
  program: string;
32
- /** Granted capability tokens, order as granted (order is significant). */
32
+ /**
33
+ * Capability tokens the decision was evaluated over, order as presented
34
+ * (order is significant): the granted set on allow; on deny, the tokens
35
+ * the request was evaluated against.
36
+ */
33
37
  capabilities: string[];
34
38
  /** Decimal string, exactly as decided. */
35
39
  amountUsd: string;
@@ -45,7 +49,7 @@ export interface ReceiptInstanceFields {
45
49
  ref: string;
46
50
  preimage: InstancePreimage;
47
51
  }
48
- export type InstanceBindingCode = 'absent' | 'ok' | 'wrong_kind' | 'malformed_ref' | 'out_of_domain' | 'ref_mismatch';
52
+ export type InstanceBindingCode = 'absent' | 'ok' | 'malformed_receipt' | 'wrong_kind' | 'malformed_ref' | 'out_of_domain' | 'ref_mismatch';
49
53
  export type InstanceBindingResult = {
50
54
  ok: true;
51
55
  present: false;
@@ -54,10 +58,15 @@ export type InstanceBindingResult = {
54
58
  ok: true;
55
59
  present: true;
56
60
  code: 'ok';
61
+ } | {
62
+ ok: false;
63
+ present: false;
64
+ code: 'malformed_receipt';
65
+ detail: string;
57
66
  } | {
58
67
  ok: false;
59
68
  present: true;
60
- code: Exclude<InstanceBindingCode, 'absent' | 'ok'>;
69
+ code: Exclude<InstanceBindingCode, 'absent' | 'ok' | 'malformed_receipt'>;
61
70
  detail: string;
62
71
  };
63
72
  export type PreimageValidation = {
package/dist/instance.js CHANGED
@@ -126,6 +126,17 @@ function computeInstanceRef(preimage) {
126
126
  * receipt carries no instance block (the block is optional).
127
127
  */
128
128
  function verifyInstanceBinding(receipt) {
129
+ // Parsed JSON can hand this function anything — a log line that is not a
130
+ // receipt at all must fail deterministically, never throw.
131
+ const payload = receipt?.payload;
132
+ if (typeof payload !== 'object' || payload === null || Array.isArray(payload)) {
133
+ return {
134
+ ok: false,
135
+ present: false,
136
+ code: 'malformed_receipt',
137
+ detail: 'input has no receipt payload object',
138
+ };
139
+ }
129
140
  const instance = receipt.payload.instance;
130
141
  if (instance === undefined) {
131
142
  return { ok: true, present: false, code: 'absent' };
@@ -37,6 +37,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
37
37
  const fs = __importStar(require("fs"));
38
38
  const process = __importStar(require("process"));
39
39
  const sign_1 = require("./sign");
40
+ const instance_1 = require("./instance");
40
41
  // --- Helpers ---
41
42
  function usage() {
42
43
  return `Usage: bolyra-receipt-verify <receipt.json> [options]
@@ -297,6 +298,20 @@ function validateSignature() {
297
298
  }
298
299
  pass(`Signature valid (signer: ${truncate(receipt.signature.signer)})`);
299
300
  }
301
+ // 6. Instance binding (spec/receipt-instance-binding-v1.md §4): signature
302
+ // validity and instance-binding validity are different claims — a
303
+ // signer-issued receipt with a forged instance.ref passes verifyReceipt()
304
+ // in check 5 and MUST still fail here.
305
+ function validateInstance() {
306
+ const result = (0, instance_1.verifyInstanceBinding)(receipt);
307
+ if (!result.ok) {
308
+ failCheck(`Instance binding invalid [${result.code}] ${result.detail}`);
309
+ return;
310
+ }
311
+ if (result.present) {
312
+ pass(`Instance binding valid (${receipt.payload.instance.ref})`);
313
+ }
314
+ }
300
315
  // --- Run checks ---
301
316
  // A receipt is untrusted input. Whatever shape it has, the outcome is a list
302
317
  // of checks and an exit code, never an uncaught error.
@@ -306,6 +321,7 @@ try {
306
321
  if (validateHash()) {
307
322
  validateId();
308
323
  validateSignature();
324
+ validateInstance();
309
325
  }
310
326
  }
311
327
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bolyra/receipts",
3
- "version": "0.10.1",
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",
@@ -18,12 +18,12 @@
18
18
  "@noble/hashes": "^1.7.0"
19
19
  },
20
20
  "devDependencies": {
21
- "typescript": "^5.5.0",
21
+ "@types/jest": "^30.0.0",
22
22
  "@types/node": "^25.9.1",
23
23
  "jest": "^30.4.2",
24
24
  "ts-jest": "^29.2.0",
25
25
  "tsx": "^4.20.0",
26
- "@types/jest": "^30.0.0"
26
+ "typescript": "^5.5.0"
27
27
  },
28
28
  "files": [
29
29
  "dist/",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "publishConfig": {
39
39
  "access": "public",
40
- "tag": "release-0.10"
40
+ "tag": "release-0.11"
41
41
  },
42
42
  "keywords": [
43
43
  "bolyra",
package/src/instance.ts CHANGED
@@ -48,7 +48,11 @@ export interface InstancePreimage {
48
48
  audience: string;
49
49
  /** Binding program discriminator (e.g. "x402", "mpp"). */
50
50
  program: string;
51
- /** Granted capability tokens, order as granted (order is significant). */
51
+ /**
52
+ * Capability tokens the decision was evaluated over, order as presented
53
+ * (order is significant): the granted set on allow; on deny, the tokens
54
+ * the request was evaluated against.
55
+ */
52
56
  capabilities: string[];
53
57
  /** Decimal string, exactly as decided. */
54
58
  amountUsd: string;
@@ -69,6 +73,7 @@ export interface ReceiptInstanceFields {
69
73
  export type InstanceBindingCode =
70
74
  | 'absent'
71
75
  | 'ok'
76
+ | 'malformed_receipt'
72
77
  | 'wrong_kind'
73
78
  | 'malformed_ref'
74
79
  | 'out_of_domain'
@@ -77,7 +82,13 @@ export type InstanceBindingCode =
77
82
  export type InstanceBindingResult =
78
83
  | { ok: true; present: false; code: 'absent' }
79
84
  | { ok: true; present: true; code: 'ok' }
80
- | { ok: false; present: true; code: Exclude<InstanceBindingCode, 'absent' | 'ok'>; detail: string };
85
+ | { ok: false; present: false; code: 'malformed_receipt'; detail: string }
86
+ | {
87
+ ok: false;
88
+ present: true;
89
+ code: Exclude<InstanceBindingCode, 'absent' | 'ok' | 'malformed_receipt'>;
90
+ detail: string;
91
+ };
81
92
 
82
93
  export type PreimageValidation = { ok: true } | { ok: false; detail: string };
83
94
 
@@ -175,6 +186,17 @@ export function computeInstanceRef(preimage: InstancePreimage): string {
175
186
  * receipt carries no instance block (the block is optional).
176
187
  */
177
188
  export function verifyInstanceBinding(receipt: SignedReceipt): InstanceBindingResult {
189
+ // Parsed JSON can hand this function anything — a log line that is not a
190
+ // receipt at all must fail deterministically, never throw.
191
+ const payload = (receipt as { payload?: unknown } | null | undefined)?.payload;
192
+ if (typeof payload !== 'object' || payload === null || Array.isArray(payload)) {
193
+ return {
194
+ ok: false,
195
+ present: false,
196
+ code: 'malformed_receipt',
197
+ detail: 'input has no receipt payload object',
198
+ };
199
+ }
178
200
  const instance = receipt.payload.instance;
179
201
  if (instance === undefined) {
180
202
  return { ok: true, present: false, code: 'absent' };
package/src/verify-cli.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  import * as fs from 'fs';
4
4
  import * as process from 'process';
5
5
  import { verifyReceipt, hashPayload } from './sign';
6
+ import { verifyInstanceBinding } from './instance';
6
7
  import type { SignedReceipt } from './types';
7
8
 
8
9
  // --- Helpers ---
@@ -255,6 +256,21 @@ function validateSignature(): void {
255
256
  pass(`Signature valid (signer: ${truncate(receipt.signature.signer)})`);
256
257
  }
257
258
 
259
+ // 6. Instance binding (spec/receipt-instance-binding-v1.md §4): signature
260
+ // validity and instance-binding validity are different claims — a
261
+ // signer-issued receipt with a forged instance.ref passes verifyReceipt()
262
+ // in check 5 and MUST still fail here.
263
+ function validateInstance(): void {
264
+ const result = verifyInstanceBinding(receipt);
265
+ if (!result.ok) {
266
+ failCheck(`Instance binding invalid [${result.code}] ${result.detail}`);
267
+ return;
268
+ }
269
+ if (result.present) {
270
+ pass(`Instance binding valid (${receipt.payload.instance!.ref})`);
271
+ }
272
+ }
273
+
258
274
  // --- Run checks ---
259
275
 
260
276
  // A receipt is untrusted input. Whatever shape it has, the outcome is a list
@@ -265,6 +281,7 @@ try {
265
281
  if (validateHash()) {
266
282
  validateId();
267
283
  validateSignature();
284
+ validateInstance();
268
285
  }
269
286
  }
270
287
  } catch {