@amanbhawsar/ai-receipt 0.0.0-stage → 0.2.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/package.json CHANGED
@@ -1,6 +1,15 @@
1
1
  {
2
2
  "name": "@amanbhawsar/ai-receipt",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "Zero-dependency cryptographic audit receipts for AI agents & LLM calls. Tamper-evident SHA-256 hash chain, selective disclosure, O(1) fold. Cross-verifiable with the PyPI package.",
5
+ "main": "src/index.js",
6
+ "types": "src/index.d.ts",
7
+ "files": ["src/"],
8
+ "scripts": {
9
+ "test": "node test/test.js"
10
+ },
11
+ "keywords": ["ai", "llm", "audit", "receipt", "tamper-proof", "compliance", "soc2", "hash-chain", "agent", "zero-dependency"],
12
+ "license": "AGPL-3.0-or-later",
13
+ "engines": { "node": ">=14" },
14
+ "repository": { "type": "git", "url": "https://github.com/Amanbhaw/ai-receipt" }
15
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,46 @@
1
+ export interface Receipt {
2
+ spec: string;
3
+ step: number;
4
+ model: string;
5
+ prompt_commit: string;
6
+ response_commit: string;
7
+ tool_commit: string;
8
+ cost: number;
9
+ timestamp: number;
10
+ prev_hash: string;
11
+ hash: string;
12
+ sig?: string;
13
+ }
14
+
15
+ export interface FoldedReceipt {
16
+ spec: string;
17
+ count: number;
18
+ root: string;
19
+ first_step: number | null;
20
+ last_step: number | null;
21
+ }
22
+
23
+ export interface CreateReceiptOptions {
24
+ step: number;
25
+ model: string;
26
+ prompt: unknown;
27
+ response: unknown;
28
+ toolCalls?: unknown;
29
+ cost?: number;
30
+ timestamp?: number;
31
+ prevHash?: string;
32
+ salt?: string;
33
+ secretKey?: string | null;
34
+ }
35
+
36
+ export const VERSION: string;
37
+ export const SPEC: string;
38
+
39
+ export function createReceipt(opts: CreateReceiptOptions): Receipt;
40
+ export function verifyReceipt(r: Receipt, secretKey?: string): boolean;
41
+ export function verifyChain(receipts: Receipt[], secretKey?: string): boolean;
42
+ export function foldReceipts(receipts: Receipt[]): FoldedReceipt;
43
+ export function verifyFold(folded: FoldedReceipt, receipts: Receipt[]): boolean;
44
+ export function openCommitment(commitHex: string, value: unknown, salt: string): boolean;
45
+ export function exportComplianceReport(folded: FoldedReceipt, receipts: Receipt[], fmt?: 'markdown' | 'json', proKey?: string | null): string;
46
+ export function newSalt(): string;
package/src/index.js ADDED
@@ -0,0 +1,185 @@
1
+ 'use strict';
2
+ /**
3
+ * ai-receipt — zero-dependency cryptographic audit receipts for AI agents / LLM calls.
4
+ *
5
+ * Tamper-evident SHA-256 hash chain (same primitive Git and blockchains use). No ZK.
6
+ * Receipts are byte-for-byte cross-verifiable with the Python `ai-receipt` package:
7
+ * the canonical field string and hashing are identical in both.
8
+ *
9
+ * Node >= 14 (uses the built-in `crypto` module; no external runtime dependencies).
10
+ * MIT licensed.
11
+ */
12
+ const crypto = require('crypto');
13
+
14
+ const VERSION = '0.1.0';
15
+ const SPEC = 'ai-receipt/1';
16
+ const US = '\x1f'; // ASCII unit separator
17
+
18
+ function h(str) {
19
+ return crypto.createHash('sha256').update(str, 'utf8').digest('hex');
20
+ }
21
+
22
+ function canonValue(value) {
23
+ if (value === null || value === undefined) return '';
24
+ if (typeof value === 'string') return value;
25
+ if (typeof value === 'boolean') return value ? 'true' : 'false';
26
+ if (typeof value === 'number') {
27
+ if (!Number.isInteger(value)) {
28
+ throw new Error('ai-receipt: use integer values (e.g. micro-dollars) for portable hashes');
29
+ }
30
+ return String(value);
31
+ }
32
+ if (Array.isArray(value)) {
33
+ return '[' + value.map(canonValue).join(',') + ']';
34
+ }
35
+ if (typeof value === 'object') {
36
+ const keys = Object.keys(value).sort();
37
+ return '{' + keys.map((k) => k + ':' + canonValue(value[k])).join(',') + '}';
38
+ }
39
+ return String(value);
40
+ }
41
+
42
+ function commit(value, salt) {
43
+ const text = value === null || value === undefined ? '' : canonValue(value);
44
+ return h(salt + US + text);
45
+ }
46
+
47
+ function newSalt() {
48
+ return crypto.randomBytes(16).toString('hex');
49
+ }
50
+
51
+ function canonicalString(step, model, pc, rc, tc, cost, timestamp, prevHash) {
52
+ return [SPEC, String(step), model, pc, rc, tc, String(cost), String(timestamp), prevHash].join(US);
53
+ }
54
+
55
+ function createReceipt(opts) {
56
+ const {
57
+ step,
58
+ model,
59
+ prompt,
60
+ response,
61
+ toolCalls = null,
62
+ cost = 0,
63
+ prevHash = '',
64
+ secretKey = null,
65
+ } = opts;
66
+ let { timestamp, salt } = opts;
67
+ if (timestamp === undefined || timestamp === null) timestamp = Math.floor(Date.now() / 1000);
68
+ if (salt === undefined || salt === null) salt = newSalt();
69
+
70
+ const promptCommit = commit(prompt, salt);
71
+ const responseCommit = commit(response, salt);
72
+ const toolCommit = commit(toolCalls, salt);
73
+ const hash = h(canonicalString(step, model, promptCommit, responseCommit, toolCommit, cost, timestamp, prevHash));
74
+
75
+ const receipt = {
76
+ spec: SPEC,
77
+ step,
78
+ model,
79
+ prompt_commit: promptCommit,
80
+ response_commit: responseCommit,
81
+ tool_commit: toolCommit,
82
+ cost,
83
+ timestamp,
84
+ prev_hash: prevHash,
85
+ hash,
86
+ };
87
+ if (secretKey !== null && secretKey !== undefined) {
88
+ receipt.sig = crypto.createHmac('sha256', secretKey).update(hash, 'utf8').digest('hex');
89
+ }
90
+ return receipt;
91
+ }
92
+
93
+ function recomputeHash(r) {
94
+ return h(canonicalString(r.step, r.model, r.prompt_commit, r.response_commit, r.tool_commit, r.cost, r.timestamp, r.prev_hash));
95
+ }
96
+
97
+ function timingSafeEqualHex(a, b) {
98
+ if (typeof a !== 'string' || typeof b !== 'string' || a.length !== b.length) return false;
99
+ return crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b));
100
+ }
101
+
102
+ function verifyReceipt(r, secretKey) {
103
+ if (!r || r.spec !== SPEC) return false;
104
+ if (recomputeHash(r) !== r.hash) return false;
105
+ if ('sig' in r) {
106
+ if (secretKey === null || secretKey === undefined) return false;
107
+ const expected = crypto.createHmac('sha256', secretKey).update(r.hash, 'utf8').digest('hex');
108
+ if (!timingSafeEqualHex(expected, r.sig)) return false;
109
+ }
110
+ return true;
111
+ }
112
+
113
+ function openCommitment(commitHex, value, salt) {
114
+ return timingSafeEqualHex(commit(value, salt), commitHex);
115
+ }
116
+
117
+ function foldReceipts(receipts) {
118
+ let root = '';
119
+ for (const r of receipts) root = h(root + r.hash);
120
+ return {
121
+ spec: SPEC,
122
+ count: receipts.length,
123
+ root,
124
+ first_step: receipts.length ? receipts[0].step : null,
125
+ last_step: receipts.length ? receipts[receipts.length - 1].step : null,
126
+ };
127
+ }
128
+
129
+ function verifyChain(receipts, secretKey) {
130
+ let prev = '';
131
+ for (const r of receipts) {
132
+ if (r.prev_hash !== prev) return false;
133
+ if (!verifyReceipt(r, secretKey)) return false;
134
+ prev = r.hash;
135
+ }
136
+ return true;
137
+ }
138
+
139
+ function verifyFold(folded, receipts) {
140
+ const ref = foldReceipts(receipts);
141
+ return folded.root === ref.root && folded.count === ref.count;
142
+ }
143
+
144
+ function proKeyValid(proKey) {
145
+ // TODO (you, when wiring payments): verify against LemonSqueezy license API. Offline here.
146
+ return !!proKey;
147
+ }
148
+
149
+ function exportComplianceReport(folded, receipts, fmt = 'markdown', proKey = null) {
150
+ const pro = proKeyValid(proKey);
151
+ if (fmt === 'json') {
152
+ return JSON.stringify({ report: 'ai-receipt compliance', pro, summary: folded, steps: receipts }, null, 2);
153
+ }
154
+ const lines = ['# ai-receipt — audit report', ''];
155
+ if (pro) lines.push('> **Pro / enterprise report** (SOC2 / EU-AI-Act layout)', '');
156
+ lines.push(
157
+ `- steps: **${folded.count}**`,
158
+ `- root commitment: \`${folded.root}\``,
159
+ `- step range: ${folded.first_step} … ${folded.last_step}`,
160
+ '',
161
+ '| step | model | cost | timestamp | hash (first 16) |',
162
+ '|---|---|---|---|---|'
163
+ );
164
+ for (const r of receipts) {
165
+ lines.push(`| ${r.step} | ${r.model} | ${r.cost} | ${r.timestamp} | \`${r.hash.slice(0, 16)}…\` |`);
166
+ }
167
+ if (!pro) lines.push('', '_Upgrade to Pro for SOC2/EU-AI-Act formatted bundles & signed exports._');
168
+ return lines.join('\n');
169
+ }
170
+
171
+ module.exports = {
172
+ VERSION,
173
+ SPEC,
174
+ createReceipt,
175
+ verifyReceipt,
176
+ verifyChain,
177
+ foldReceipts,
178
+ verifyFold,
179
+ openCommitment,
180
+ exportComplianceReport,
181
+ newSalt,
182
+ // exported for the cross-language test harness:
183
+ _canonValue: canonValue,
184
+ _commit: commit,
185
+ };
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.