@bolyra/receipts 0.10.0 → 0.12.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/dist/chain.d.ts CHANGED
@@ -36,6 +36,7 @@
36
36
  * (enterprise-configurable), not part of this library.
37
37
  */
38
38
  import type { ReceiptPayload, ReceiptSignerConfig, SignedReceipt } from './types';
39
+ import type { ApprovalReceiptPayload, ExecutionReceiptPayload } from './approval';
39
40
  /** Sentinel prevReceiptHash for the first receipt in a log: 32 zero bytes. */
40
41
  export declare const GENESIS_PREV_RECEIPT_HASH: string;
41
42
  /**
@@ -44,6 +45,8 @@ export declare const GENESIS_PREV_RECEIPT_HASH: string;
44
45
  * signature.payloadHash) and `receiptHash` itself (to avoid self-reference).
45
46
  */
46
47
  export declare function computeReceiptHash(receipt: SignedReceipt): string;
48
+ export declare function computeReceiptHash(receipt: SignedReceipt<ApprovalReceiptPayload | ExecutionReceiptPayload>): string;
49
+ export declare function computeReceiptHash(receipt: SignedReceipt): string;
47
50
  /**
48
51
  * Stateful writer-side chain: assigns { seq, prevReceiptHash } to each payload
49
52
  * before signing and attaches the resulting receiptHash to the envelope.
package/dist/chain.js CHANGED
@@ -46,11 +46,6 @@ const canonical_1 = require("./canonical");
46
46
  const sign_1 = require("./sign");
47
47
  /** Sentinel prevReceiptHash for the first receipt in a log: 32 zero bytes. */
48
48
  exports.GENESIS_PREV_RECEIPT_HASH = '0x' + '0'.repeat(64);
49
- /**
50
- * Canonical hash of a signed receipt: keccak256 over
51
- * canonicalize({ payload, signature }). Excludes `id` (derivable from
52
- * signature.payloadHash) and `receiptHash` itself (to avoid self-reference).
53
- */
54
49
  function computeReceiptHash(receipt) {
55
50
  const canonical = (0, canonical_1.canonicalize)({ payload: receipt.payload, signature: receipt.signature });
56
51
  return '0x' + (0, utils_1.bytesToHex)((0, sha3_1.keccak_256)(new TextEncoder().encode(canonical)));
package/dist/index.d.ts CHANGED
@@ -7,3 +7,5 @@ export type { ReceiptPayload, ReceiptChainFields, SignedReceipt, ReceiptSignerCo
7
7
  export { INSTANCE_BINDING_DST, INSTANCE_REF_PREFIX, computeInstanceRef, validateInstancePreimage, verifyInstanceBinding, } from './instance';
8
8
  export type { InstancePreimage, ReceiptInstanceFields, InstanceBindingCode, InstanceBindingResult, PreimageValidation, } from './instance';
9
9
  export { parseSignerDiscovery, acceptedSigners, SignerDiscoveryError, type SignerDiscoveryDocument, type DiscoveredSigner, } from './signer-discovery';
10
+ export { ARGUMENTS_DIGEST_DST, ARGUMENTS_MAX_BYTES, ARGUMENTS_MAX_DEPTH, LINK_VERIFICATION_STATEMENT, ApprovedExecutionError, computeArgumentsDigest, createApprovalReceipt, executeApproved, validateArgumentsJson, verifyApprovalExecutionLink, } from './approval';
11
+ export type { ApprovalAssertion, ApprovalBinding, ApprovalContext, ApprovalDecision, ApprovalReceiptInput, ApprovalReceiptPayload, ApprovalValidity, ApprovedExecution, ArgumentsValidation, BlockedExecution, BlockedExecutionCode, ConsumeClaim, ConsumeFn, DispatchFn, ExecuteApprovedParams, ExecutionFields, ExecutionReceiptPayload, JsonObject, JsonPrimitive, JsonValue, LinkCheck, LinkCheckId, LinkVerificationResult, ReturnedExecution, VerifyLinkParams, } from './approval';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SignerDiscoveryError = exports.acceptedSigners = exports.parseSignerDiscovery = exports.verifyInstanceBinding = exports.validateInstancePreimage = exports.computeInstanceRef = exports.INSTANCE_REF_PREFIX = exports.INSTANCE_BINDING_DST = exports.verifyReceiptChain = exports.computeReceiptHash = exports.ReceiptChain = exports.GENESIS_PREV_RECEIPT_HASH = exports.hashPayload = exports.verifyReceipt = exports.signReceipt = exports.createCommerceReceipt = exports.createAuthReceipt = exports.canonicalize = void 0;
3
+ exports.verifyApprovalExecutionLink = exports.validateArgumentsJson = exports.executeApproved = exports.createApprovalReceipt = exports.computeArgumentsDigest = exports.ApprovedExecutionError = exports.LINK_VERIFICATION_STATEMENT = exports.ARGUMENTS_MAX_DEPTH = exports.ARGUMENTS_MAX_BYTES = exports.ARGUMENTS_DIGEST_DST = exports.SignerDiscoveryError = exports.acceptedSigners = exports.parseSignerDiscovery = exports.verifyInstanceBinding = exports.validateInstancePreimage = exports.computeInstanceRef = exports.INSTANCE_REF_PREFIX = exports.INSTANCE_BINDING_DST = exports.verifyReceiptChain = exports.computeReceiptHash = exports.ReceiptChain = exports.GENESIS_PREV_RECEIPT_HASH = exports.hashPayload = exports.verifyReceipt = exports.signReceipt = exports.createCommerceReceipt = exports.createAuthReceipt = exports.canonicalize = void 0;
4
4
  var canonical_1 = require("./canonical");
5
5
  Object.defineProperty(exports, "canonicalize", { enumerable: true, get: function () { return canonical_1.canonicalize; } });
6
6
  var receipt_1 = require("./receipt");
@@ -25,3 +25,14 @@ var signer_discovery_1 = require("./signer-discovery");
25
25
  Object.defineProperty(exports, "parseSignerDiscovery", { enumerable: true, get: function () { return signer_discovery_1.parseSignerDiscovery; } });
26
26
  Object.defineProperty(exports, "acceptedSigners", { enumerable: true, get: function () { return signer_discovery_1.acceptedSigners; } });
27
27
  Object.defineProperty(exports, "SignerDiscoveryError", { enumerable: true, get: function () { return signer_discovery_1.SignerDiscoveryError; } });
28
+ var approval_1 = require("./approval");
29
+ Object.defineProperty(exports, "ARGUMENTS_DIGEST_DST", { enumerable: true, get: function () { return approval_1.ARGUMENTS_DIGEST_DST; } });
30
+ Object.defineProperty(exports, "ARGUMENTS_MAX_BYTES", { enumerable: true, get: function () { return approval_1.ARGUMENTS_MAX_BYTES; } });
31
+ Object.defineProperty(exports, "ARGUMENTS_MAX_DEPTH", { enumerable: true, get: function () { return approval_1.ARGUMENTS_MAX_DEPTH; } });
32
+ Object.defineProperty(exports, "LINK_VERIFICATION_STATEMENT", { enumerable: true, get: function () { return approval_1.LINK_VERIFICATION_STATEMENT; } });
33
+ Object.defineProperty(exports, "ApprovedExecutionError", { enumerable: true, get: function () { return approval_1.ApprovedExecutionError; } });
34
+ Object.defineProperty(exports, "computeArgumentsDigest", { enumerable: true, get: function () { return approval_1.computeArgumentsDigest; } });
35
+ Object.defineProperty(exports, "createApprovalReceipt", { enumerable: true, get: function () { return approval_1.createApprovalReceipt; } });
36
+ Object.defineProperty(exports, "executeApproved", { enumerable: true, get: function () { return approval_1.executeApproved; } });
37
+ Object.defineProperty(exports, "validateArgumentsJson", { enumerable: true, get: function () { return approval_1.validateArgumentsJson; } });
38
+ Object.defineProperty(exports, "verifyApprovalExecutionLink", { enumerable: true, get: function () { return approval_1.verifyApprovalExecutionLink; } });
@@ -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' };
package/dist/sign.d.ts CHANGED
@@ -1,4 +1,11 @@
1
1
  import type { ReceiptPayload, ReceiptSignerConfig, SignedReceipt } from './types';
2
+ import type { ApprovalReceiptPayload, ExecutionReceiptPayload } from './approval';
2
3
  export declare function hashPayload(payload: ReceiptPayload): string;
4
+ export declare function hashPayload(payload: ApprovalReceiptPayload | ExecutionReceiptPayload): string;
5
+ export declare function hashPayload(payload: ReceiptPayload): string;
6
+ export declare function signReceipt(payload: ReceiptPayload, config: ReceiptSignerConfig): SignedReceipt;
7
+ export declare function signReceipt<T extends ApprovalReceiptPayload | ExecutionReceiptPayload>(payload: T, config: ReceiptSignerConfig): SignedReceipt<T>;
3
8
  export declare function signReceipt(payload: ReceiptPayload, config: ReceiptSignerConfig): SignedReceipt;
4
9
  export declare function verifyReceipt(receipt: SignedReceipt, expectedSigner?: string): boolean;
10
+ export declare function verifyReceipt(receipt: SignedReceipt<ApprovalReceiptPayload | ExecutionReceiptPayload>, expectedSigner?: string): boolean;
11
+ export declare function verifyReceipt(receipt: SignedReceipt, expectedSigner?: string): boolean;
package/dist/sign.js CHANGED
@@ -42,6 +42,9 @@ const sha256_1 = require("@noble/hashes/sha256");
42
42
  const sha3_1 = require("@noble/hashes/sha3");
43
43
  const utils_1 = require("@noble/hashes/utils");
44
44
  const canonical_1 = require("./canonical");
45
+ // Each legacy signature appears FIRST (call resolution) and again LAST:
46
+ // conditional types such as ReturnType<typeof signReceipt> read the last
47
+ // overload, and existing code relies on that resolving to the legacy shape.
45
48
  // @noble/secp256k1 v2 requires hmacSha256Sync to be set for synchronous signing.
46
49
  secp256k1.etc.hmacSha256Sync = (k, ...m) => {
47
50
  const h = hmac_1.hmac.create(sha256_1.sha256, k);
package/dist/types.d.ts CHANGED
@@ -66,10 +66,15 @@ export interface ReceiptPayload {
66
66
  /** Present only on hash-chained receipts (written via ReceiptChain). */
67
67
  chain?: ReceiptChainFields;
68
68
  }
69
- export interface SignedReceipt {
69
+ /**
70
+ * Signed envelope. Generic over the payload so the approval-binding module can
71
+ * sign its own payload kinds; the default keeps every legacy call site typed
72
+ * exactly as before (`SignedReceipt` === `SignedReceipt<ReceiptPayload>`).
73
+ */
74
+ export interface SignedReceipt<T = ReceiptPayload> {
70
75
  /** First 16 hex chars of payloadHash. */
71
76
  id: string;
72
- payload: ReceiptPayload;
77
+ payload: T;
73
78
  signature: {
74
79
  alg: 'ES256K';
75
80
  keyId: string;
@@ -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]
@@ -273,12 +274,27 @@ function validateSignature() {
273
274
  }
274
275
  pass(`Signature valid (signer: ${truncate(receipt.signature.signer)})`);
275
276
  }
277
+ // 6. Instance binding (spec/receipt-instance-binding-v1.md §4): signature
278
+ // validity and instance-binding validity are different claims — a
279
+ // signer-issued receipt with a forged instance.ref passes verifyReceipt()
280
+ // in check 5 and MUST still fail here.
281
+ function validateInstance() {
282
+ const result = (0, instance_1.verifyInstanceBinding)(receipt);
283
+ if (!result.ok) {
284
+ failCheck(`Instance binding invalid [${result.code}] ${result.detail}`);
285
+ return;
286
+ }
287
+ if (result.present) {
288
+ pass(`Instance binding valid (${receipt.payload.instance.ref})`);
289
+ }
290
+ }
276
291
  // --- Run checks ---
277
292
  if (validateSchema()) {
278
293
  validateTimestamp();
279
294
  if (validateHash()) {
280
295
  validateId();
281
296
  validateSignature();
297
+ validateInstance();
282
298
  }
283
299
  }
284
300
  // --- Output ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bolyra/receipts",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
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",
@@ -11,18 +11,19 @@
11
11
  "build": "tsc",
12
12
  "prepare": "tsc",
13
13
  "test": "jest",
14
- "typecheck": "tsc --noEmit"
14
+ "typecheck": "tsc --noEmit && tsc -p tsconfig.test.json"
15
15
  },
16
16
  "dependencies": {
17
17
  "@noble/secp256k1": "^2.2.0",
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
- "@types/jest": "^30.0.0"
25
+ "tsx": "^4.20.0",
26
+ "typescript": "^5.5.0"
26
27
  },
27
28
  "files": [
28
29
  "dist/",