@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/src/chain.ts CHANGED
@@ -41,6 +41,7 @@ import { bytesToHex } from '@noble/hashes/utils';
41
41
  import { canonicalize } from './canonical';
42
42
  import { signReceipt, verifyReceipt } from './sign';
43
43
  import type { ReceiptPayload, ReceiptSignerConfig, SignedReceipt } from './types';
44
+ import type { ApprovalReceiptPayload, ExecutionReceiptPayload } from './approval';
44
45
 
45
46
  /** Sentinel prevReceiptHash for the first receipt in a log: 32 zero bytes. */
46
47
  export const GENESIS_PREV_RECEIPT_HASH = '0x' + '0'.repeat(64);
@@ -50,7 +51,15 @@ export const GENESIS_PREV_RECEIPT_HASH = '0x' + '0'.repeat(64);
50
51
  * canonicalize({ payload, signature }). Excludes `id` (derivable from
51
52
  * signature.payloadHash) and `receiptHash` itself (to avoid self-reference).
52
53
  */
53
- export function computeReceiptHash(receipt: SignedReceipt): string {
54
+ export function computeReceiptHash(receipt: SignedReceipt): string;
55
+ export function computeReceiptHash(
56
+ receipt: SignedReceipt<ApprovalReceiptPayload | ExecutionReceiptPayload>,
57
+ ): string;
58
+ // Legacy signature repeated last: ReturnType/Parameters read the last overload.
59
+ export function computeReceiptHash(receipt: SignedReceipt): string;
60
+ export function computeReceiptHash(
61
+ receipt: SignedReceipt<ReceiptPayload | ApprovalReceiptPayload | ExecutionReceiptPayload>,
62
+ ): string {
54
63
  const canonical = canonicalize({ payload: receipt.payload, signature: receipt.signature });
55
64
  return '0x' + bytesToHex(keccak_256(new TextEncoder().encode(canonical)));
56
65
  }
package/src/index.ts CHANGED
@@ -43,3 +43,42 @@ export {
43
43
  type SignerDiscoveryDocument,
44
44
  type DiscoveredSigner,
45
45
  } from './signer-discovery';
46
+ export {
47
+ ARGUMENTS_DIGEST_DST,
48
+ ARGUMENTS_MAX_BYTES,
49
+ ARGUMENTS_MAX_DEPTH,
50
+ LINK_VERIFICATION_STATEMENT,
51
+ ApprovedExecutionError,
52
+ computeArgumentsDigest,
53
+ createApprovalReceipt,
54
+ executeApproved,
55
+ validateArgumentsJson,
56
+ verifyApprovalExecutionLink,
57
+ } from './approval';
58
+ export type {
59
+ ApprovalAssertion,
60
+ ApprovalBinding,
61
+ ApprovalContext,
62
+ ApprovalDecision,
63
+ ApprovalReceiptInput,
64
+ ApprovalReceiptPayload,
65
+ ApprovalValidity,
66
+ ApprovedExecution,
67
+ ArgumentsValidation,
68
+ BlockedExecution,
69
+ BlockedExecutionCode,
70
+ ConsumeClaim,
71
+ ConsumeFn,
72
+ DispatchFn,
73
+ ExecuteApprovedParams,
74
+ ExecutionFields,
75
+ ExecutionReceiptPayload,
76
+ JsonObject,
77
+ JsonPrimitive,
78
+ JsonValue,
79
+ LinkCheck,
80
+ LinkCheckId,
81
+ LinkVerificationResult,
82
+ ReturnedExecution,
83
+ VerifyLinkParams,
84
+ } from './approval';
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/sign.ts CHANGED
@@ -5,6 +5,18 @@ import { keccak_256 } from '@noble/hashes/sha3';
5
5
  import { bytesToHex } from '@noble/hashes/utils';
6
6
  import { canonicalize } from './canonical';
7
7
  import type { ReceiptPayload, ReceiptSignerConfig, SignedReceipt } from './types';
8
+ import type { ApprovalReceiptPayload, ExecutionReceiptPayload } from './approval';
9
+
10
+ /**
11
+ * Payload kinds this module signs. Legacy-first overloads below keep every
12
+ * existing ReceiptPayload call site typed exactly as before; the second
13
+ * overload admits the approval-binding kinds. Runtime behavior, hashes and
14
+ * signature bytes are identical for all of them.
15
+ */
16
+ type AnyReceiptPayload = ReceiptPayload | ApprovalReceiptPayload | ExecutionReceiptPayload;
17
+ // Each legacy signature appears FIRST (call resolution) and again LAST:
18
+ // conditional types such as ReturnType<typeof signReceipt> read the last
19
+ // overload, and existing code relies on that resolving to the legacy shape.
8
20
 
9
21
  // @noble/secp256k1 v2 requires hmacSha256Sync to be set for synchronous signing.
10
22
  secp256k1.etc.hmacSha256Sync = (k: Uint8Array, ...m: Uint8Array[]) => {
@@ -36,7 +48,10 @@ function deriveAddress(publicKeyBytes: Uint8Array): string {
36
48
  return '0x' + bytesToHex(hash.slice(hash.length - 20));
37
49
  }
38
50
 
39
- export function hashPayload(payload: ReceiptPayload): string {
51
+ export function hashPayload(payload: ReceiptPayload): string;
52
+ export function hashPayload(payload: ApprovalReceiptPayload | ExecutionReceiptPayload): string;
53
+ export function hashPayload(payload: ReceiptPayload): string;
54
+ export function hashPayload(payload: AnyReceiptPayload): string {
40
55
  const canonical = canonicalize(payload);
41
56
  const hash = keccak_256(utf8ToBytes(canonical));
42
57
  return '0x' + bytesToHex(hash);
@@ -45,7 +60,19 @@ export function hashPayload(payload: ReceiptPayload): string {
45
60
  export function signReceipt(
46
61
  payload: ReceiptPayload,
47
62
  config: ReceiptSignerConfig,
48
- ): SignedReceipt {
63
+ ): SignedReceipt;
64
+ export function signReceipt<T extends ApprovalReceiptPayload | ExecutionReceiptPayload>(
65
+ payload: T,
66
+ config: ReceiptSignerConfig,
67
+ ): SignedReceipt<T>;
68
+ export function signReceipt(
69
+ payload: ReceiptPayload,
70
+ config: ReceiptSignerConfig,
71
+ ): SignedReceipt;
72
+ export function signReceipt(
73
+ payload: AnyReceiptPayload,
74
+ config: ReceiptSignerConfig,
75
+ ): SignedReceipt<AnyReceiptPayload> {
49
76
  const canonical = canonicalize(payload);
50
77
  const hash = keccak_256(utf8ToBytes(canonical));
51
78
  const privateKeyBytes = hexToBytes(config.privateKey);
@@ -78,6 +105,18 @@ export function signReceipt(
78
105
  export function verifyReceipt(
79
106
  receipt: SignedReceipt,
80
107
  expectedSigner?: string,
108
+ ): boolean;
109
+ export function verifyReceipt(
110
+ receipt: SignedReceipt<ApprovalReceiptPayload | ExecutionReceiptPayload>,
111
+ expectedSigner?: string,
112
+ ): boolean;
113
+ export function verifyReceipt(
114
+ receipt: SignedReceipt,
115
+ expectedSigner?: string,
116
+ ): boolean;
117
+ export function verifyReceipt(
118
+ receipt: SignedReceipt<AnyReceiptPayload>,
119
+ expectedSigner?: string,
81
120
  ): boolean {
82
121
  try {
83
122
  // Recompute hash from payload
package/src/types.ts CHANGED
@@ -75,10 +75,15 @@ export interface ReceiptPayload {
75
75
  chain?: ReceiptChainFields;
76
76
  }
77
77
 
78
- export interface SignedReceipt {
78
+ /**
79
+ * Signed envelope. Generic over the payload so the approval-binding module can
80
+ * sign its own payload kinds; the default keeps every legacy call site typed
81
+ * exactly as before (`SignedReceipt` === `SignedReceipt<ReceiptPayload>`).
82
+ */
83
+ export interface SignedReceipt<T = ReceiptPayload> {
79
84
  /** First 16 hex chars of payloadHash. */
80
85
  id: string;
81
- payload: ReceiptPayload;
86
+ payload: T;
82
87
  signature: {
83
88
  alg: 'ES256K';
84
89
  keyId: string;
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 ---
@@ -233,6 +234,21 @@ function validateSignature(): void {
233
234
  pass(`Signature valid (signer: ${truncate(receipt.signature.signer)})`);
234
235
  }
235
236
 
237
+ // 6. Instance binding (spec/receipt-instance-binding-v1.md §4): signature
238
+ // validity and instance-binding validity are different claims — a
239
+ // signer-issued receipt with a forged instance.ref passes verifyReceipt()
240
+ // in check 5 and MUST still fail here.
241
+ function validateInstance(): void {
242
+ const result = verifyInstanceBinding(receipt);
243
+ if (!result.ok) {
244
+ failCheck(`Instance binding invalid [${result.code}] ${result.detail}`);
245
+ return;
246
+ }
247
+ if (result.present) {
248
+ pass(`Instance binding valid (${receipt.payload.instance!.ref})`);
249
+ }
250
+ }
251
+
236
252
  // --- Run checks ---
237
253
 
238
254
  if (validateSchema()) {
@@ -240,6 +256,7 @@ if (validateSchema()) {
240
256
  if (validateHash()) {
241
257
  validateId();
242
258
  validateSignature();
259
+ validateInstance();
243
260
  }
244
261
  }
245
262