@chain-receipt/sdk 0.0.0-stage → 0.1.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/crypto.js ADDED
@@ -0,0 +1,286 @@
1
+ /* @chain-receipt/sdk — crypto primitives (plan step 25).
2
+ The same constructions as @chain-receipt/core (RFC 8785 JCS,
3
+ SHA-256, Ed25519, Merkle leaf 0x00 / node 0x01), implemented here
4
+ against the same libraries (@noble/ed25519, @noble/hashes,
5
+ canonicalize) instead of importing core — core is private and
6
+ unpublished, and its index pulls node:crypto, so a workspace
7
+ dependency would make this published package uninstallable and
8
+ break in browsers. This file is deliberately dependency-light and
9
+ Buffer-free so the offline verify path runs in Node and browsers.
10
+ Every function here is proven against the shared golden vectors in
11
+ packages/core/test-vectors/golden.json (see crypto.test.ts). */
12
+ import * as ed from "@noble/ed25519";
13
+ import { sha256 } from "@noble/hashes/sha2";
14
+ import { sha512 } from "@noble/hashes/sha512";
15
+ import canonicalize from "canonicalize";
16
+ ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));
17
+ ed.etc.sha512Async = (...m) => Promise.resolve(ed.etc.sha512Sync(...m));
18
+ /* --- Browser-safe hex / base64 (no Buffer) --- */
19
+ const HEX = "0123456789abcdef";
20
+ export function bytesToHex(bytes) {
21
+ let out = "";
22
+ for (const b of bytes)
23
+ out += HEX[b >> 4] + HEX[b & 0x0f];
24
+ return out;
25
+ }
26
+ export function hexToBytes(hex) {
27
+ if (hex.length % 2 !== 0 || !/^[0-9a-fA-F]*$/.test(hex)) {
28
+ throw new Error(`invalid hex: ${hex.slice(0, 32)}`);
29
+ }
30
+ const out = new Uint8Array(hex.length / 2);
31
+ for (let i = 0; i < out.length; i++) {
32
+ out[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
33
+ }
34
+ return out;
35
+ }
36
+ const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
37
+ export function bytesToBase64(bytes) {
38
+ let out = "";
39
+ for (let i = 0; i < bytes.length; i += 3) {
40
+ const b0 = bytes[i];
41
+ const b1 = bytes[i + 1];
42
+ const b2 = bytes[i + 2];
43
+ out += B64[b0 >> 2];
44
+ out += B64[((b0 & 0x03) << 4) | ((b1 ?? 0) >> 4)];
45
+ out += b1 === undefined ? "=" : B64[((b1 & 0x0f) << 2) | ((b2 ?? 0) >> 6)];
46
+ out += b2 === undefined ? "=" : B64[b2 & 0x3f];
47
+ }
48
+ return out;
49
+ }
50
+ export function base64ToBytes(b64) {
51
+ const clean = b64.replace(/\s/g, "");
52
+ if (!/^[A-Za-z0-9+/]*={0,2}$/.test(clean) || clean.length % 4 !== 0) {
53
+ throw new Error("invalid base64");
54
+ }
55
+ const lookup = new Map();
56
+ for (let i = 0; i < B64.length; i++)
57
+ lookup.set(B64[i], i);
58
+ const out = [];
59
+ for (let i = 0; i < clean.length; i += 4) {
60
+ const c0 = lookup.get(clean[i]) ?? 0;
61
+ const c1 = lookup.get(clean[i + 1]) ?? 0;
62
+ const c2 = clean[i + 2] === "=" ? 0 : (lookup.get(clean[i + 2]) ?? 0);
63
+ const c3 = clean[i + 3] === "=" ? 0 : (lookup.get(clean[i + 3]) ?? 0);
64
+ out.push((c0 << 2) | (c1 >> 4));
65
+ if (clean[i + 2] !== "=")
66
+ out.push(((c1 & 0x0f) << 4) | (c2 >> 2));
67
+ if (clean[i + 3] !== "=")
68
+ out.push(((c2 & 0x03) << 6) | c3);
69
+ }
70
+ return Uint8Array.from(out);
71
+ }
72
+ /* --- Canonical JSON (RFC 8785 / JCS) --- */
73
+ /** JCS refuses these anywhere in the structure — the `canonicalize`
74
+ * library silently drops undefined object properties, so validate
75
+ * first: a receipt containing undefined must never hash as if the
76
+ * field were absent. (Same guard as core.) */
77
+ function assertJsonValue(value) {
78
+ if (value === undefined)
79
+ throw new Error("canonicalJson: undefined value");
80
+ if (typeof value === "function" || typeof value === "symbol") {
81
+ throw new Error(`canonicalJson: ${typeof value} value`);
82
+ }
83
+ if (typeof value === "bigint") {
84
+ throw new Error("canonicalJson: bigint value");
85
+ }
86
+ if (typeof value === "number" && !Number.isFinite(value)) {
87
+ throw new Error("canonicalJson: non-finite number");
88
+ }
89
+ if (Array.isArray(value)) {
90
+ for (const item of value)
91
+ assertJsonValue(item);
92
+ }
93
+ else if (value !== null && typeof value === "object") {
94
+ for (const item of Object.values(value))
95
+ assertJsonValue(item);
96
+ }
97
+ }
98
+ /** Canonicalize a JSON value to its RFC 8785 string form. */
99
+ export function canonicalJson(value) {
100
+ assertJsonValue(value);
101
+ const out = canonicalize(value);
102
+ if (typeof out !== "string") {
103
+ throw new Error("canonicalJson: value is not JSON-serializable");
104
+ }
105
+ return out;
106
+ }
107
+ /** Canonical JSON as UTF-8 bytes, ready for hashing. */
108
+ export function canonicalJsonBytes(value) {
109
+ return new TextEncoder().encode(canonicalJson(value));
110
+ }
111
+ /* --- SHA-256 ("sha256:<hex>" tags, per plan section 8) --- */
112
+ export function sha256Bytes(data) {
113
+ return sha256(data);
114
+ }
115
+ export function sha256Hex(data) {
116
+ return bytesToHex(sha256(data));
117
+ }
118
+ /** "sha256:<hex>" for raw bytes. */
119
+ export function sha256Tagged(data) {
120
+ return `sha256:${sha256Hex(data)}`;
121
+ }
122
+ /** "sha256:<hex>" for bytes that are ALREADY a digest — tags without
123
+ * re-hashing. */
124
+ export function tagHashBytes(digest) {
125
+ return `sha256:${bytesToHex(digest)}`;
126
+ }
127
+ /** Raw 32 bytes of the hash behind a "sha256:<hex>" tag. */
128
+ export function hashBytesFromTag(tag) {
129
+ if (!tag.startsWith("sha256:") || tag.length !== 7 + 64) {
130
+ throw new Error(`invalid sha256 tag: ${tag}`);
131
+ }
132
+ return hexToBytes(tag.slice(7));
133
+ }
134
+ /* --- Ed25519 (RFC 8032) --- */
135
+ /** 32-byte public key for a 32-byte private seed. */
136
+ export function getPublicKey(privateKey) {
137
+ return ed.getPublicKey(privateKey);
138
+ }
139
+ /** Sign a message (NOT a hash) with a 32-byte private seed. */
140
+ export function signMessage(message, privateKey) {
141
+ return ed.sign(message, privateKey);
142
+ }
143
+ /** Verify a signature over a message with a 32-byte public key. */
144
+ export function verifyMessage(signature, message, publicKey) {
145
+ try {
146
+ return ed.verify(signature, message, publicKey);
147
+ }
148
+ catch {
149
+ return false;
150
+ }
151
+ }
152
+ /** A signing keypair from a 32-byte seed. */
153
+ export function keypairFromSeed(seed) {
154
+ if (seed.length !== 32) {
155
+ throw new Error("Ed25519 seed must be 32 bytes");
156
+ }
157
+ return { privateKey: seed, publicKey: getPublicKey(seed) };
158
+ }
159
+ /* --- Receipt hashing / signing (schema "air/1") --- */
160
+ /** receipt_hash = sha256(JCS(receipt)). */
161
+ export function receiptHash(receipt) {
162
+ return sha256Tagged(canonicalJsonBytes(receipt));
163
+ }
164
+ /** sha256 of the canonical receipt JSON, as raw bytes (for Merkle
165
+ * leaves — the leaf hash is sha256(0x00 || this)). */
166
+ export function receiptHashBytes(receipt) {
167
+ return sha256Bytes(canonicalJsonBytes(receipt));
168
+ }
169
+ /** Sign a receipt: Ed25519 over the 32 raw bytes of its receipt_hash. */
170
+ export function signReceipt(receipt, privateKey) {
171
+ const hashBytes = hashBytesFromTag(receiptHash(receipt));
172
+ return bytesToBase64(signMessage(hashBytes, privateKey));
173
+ }
174
+ /** Verify a receipt's signature and that its hash matches its content.
175
+ * Returns false (never throws) on any mismatch. */
176
+ export function verifyReceiptSignature(receipt, signatureB64, publicKey) {
177
+ try {
178
+ const hashBytes = hashBytesFromTag(receiptHash(receipt));
179
+ const signature = base64ToBytes(signatureB64);
180
+ return verifyMessage(signature, hashBytes, publicKey);
181
+ }
182
+ catch {
183
+ return false;
184
+ }
185
+ }
186
+ /** Salted prompt commitment: sha256(salt || JCS(messages + tools +
187
+ * response_format)). */
188
+ export function saltedPromptHash(saltB64, requestBody) {
189
+ const salt = base64ToBytes(saltB64);
190
+ const body = canonicalJsonBytes(requestBody);
191
+ const input = new Uint8Array(salt.length + body.length);
192
+ input.set(salt, 0);
193
+ input.set(body, salt.length);
194
+ return sha256Tagged(input);
195
+ }
196
+ /** Salted response commitment: sha256(salt || JCS(provider response)). */
197
+ export function saltedResponseHash(saltB64, responseJson) {
198
+ const salt = base64ToBytes(saltB64);
199
+ const body = canonicalJsonBytes(responseJson);
200
+ const input = new Uint8Array(salt.length + body.length);
201
+ input.set(salt, 0);
202
+ input.set(body, salt.length);
203
+ return sha256Tagged(input);
204
+ }
205
+ /** Recompute the content hashes from the original text + salts and
206
+ * compare them to the receipt. This is what the public verify page
207
+ * runs in the browser: the text never leaves the customer's machine. */
208
+ export function verifyReceiptContent(receipt, salts, promptBody, responseJson) {
209
+ return (saltedPromptHash(salts.prompt, promptBody) ===
210
+ receipt.request.promptHash &&
211
+ saltedResponseHash(salts.response, responseJson) ===
212
+ receipt.response.responseHash);
213
+ }
214
+ function leafHash(hash) {
215
+ const input = new Uint8Array(33);
216
+ input[0] = 0x00;
217
+ input.set(hash, 1);
218
+ return sha256Bytes(input);
219
+ }
220
+ function nodeHash(left, right) {
221
+ const input = new Uint8Array(65);
222
+ input[0] = 0x01;
223
+ input.set(left, 1);
224
+ input.set(right, 33);
225
+ return sha256Bytes(input);
226
+ }
227
+ /** Merkle root of an ordered list of receipt-hash digests. */
228
+ export function merkleRoot(hashes) {
229
+ if (hashes.length === 0) {
230
+ throw new Error("merkleRoot: empty leaf list");
231
+ }
232
+ let level = hashes.map(leafHash);
233
+ while (level.length > 1) {
234
+ const next = [];
235
+ for (let i = 0; i < level.length; i += 2) {
236
+ const left = level[i];
237
+ const right = level[i + 1] ?? left; // duplicate the last node
238
+ next.push(nodeHash(left, right));
239
+ }
240
+ level = next;
241
+ }
242
+ return level[0];
243
+ }
244
+ /** Merkle proof for the leaf at `index`. Empty proof for a
245
+ * single-leaf tree. */
246
+ export function merkleProof(hashes, index) {
247
+ if (index < 0 || index >= hashes.length) {
248
+ throw new Error(`merkleProof: index ${index} out of range`);
249
+ }
250
+ const proof = [];
251
+ let level = hashes.map(leafHash);
252
+ let idx = index;
253
+ while (level.length > 1) {
254
+ const isRight = idx % 2 === 1;
255
+ const siblingIdx = isRight ? idx - 1 : idx + 1;
256
+ const sibling = siblingIdx < level.length ? level[siblingIdx] : level[idx];
257
+ proof.push({ side: isRight ? "L" : "R", hash: sibling });
258
+ const next = [];
259
+ for (let i = 0; i < level.length; i += 2) {
260
+ const left = level[i];
261
+ const right = level[i + 1] ?? left;
262
+ next.push(nodeHash(left, right));
263
+ }
264
+ level = next;
265
+ idx = Math.floor(idx / 2);
266
+ }
267
+ return proof;
268
+ }
269
+ /** Recompute the root from a leaf hash and its proof. */
270
+ export function verifyMerkleProof(leafHashBytes, proof, expectedRoot) {
271
+ let current = leafHash(leafHashBytes);
272
+ for (const step of proof) {
273
+ current =
274
+ step.side === "L"
275
+ ? nodeHash(step.hash, current)
276
+ : nodeHash(current, step.hash);
277
+ }
278
+ if (current.length !== expectedRoot.length)
279
+ return false;
280
+ for (let i = 0; i < current.length; i++) {
281
+ if (current[i] !== expectedRoot[i])
282
+ return false;
283
+ }
284
+ return true;
285
+ }
286
+ //# sourceMappingURL=crypto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;kEAUkE;AAClE,OAAO,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,YAAY,MAAM,cAAc,CAAC;AAGxC,EAAE,CAAC,GAAG,CAAC,UAAU,GAAG,CAAC,GAAG,CAAe,EAAE,EAAE,CACzC,MAAM,CAAC,EAAE,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACnC,EAAE,CAAC,GAAG,CAAC,WAAW,GAAG,CAAC,GAAG,CA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C,CAAC;YAAE,OAAO,KAAK,CAAC;IACnD,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { ChainReceiptClient, ChainReceiptError, receiptFromResponse, receiptFromSseEvent, type ChainReceiptClientOptions, type ChatCompletionsOptions, type ChatCompletionsResult, type ListReceiptsParams, } from "./client.js";
2
+ export { DEFAULT_BASE_URL, fetchPublicKey, getVerifyUrl, parsePublicKey, publicKeyToBase64, verifyAnchorProof, verifyReceipt, verifyReceiptContent, } from "./verify.js";
3
+ export { base64ToBytes, bytesToBase64, bytesToHex, canonicalJson, getPublicKey, hexToBytes, keypairFromSeed, merkleProof, merkleRoot, receiptHash, receiptHashBytes, saltedPromptHash, saltedResponseHash, sha256Hex, sha256Tagged, signMessage, signReceipt, verifyMerkleProof, verifyMessage, verifyReceiptSignature, type MerkleProofStep, } from "./crypto.js";
4
+ export type { AiReceiptSummary, AnchorInfo, AnchorStatus, PrivacyMode, PublicKeyEntry, Receipt, ReceiptEnvelope, ReceiptListItem, ReceiptListPage, ReceiptRequest, ReceiptResponse, Salts, ServerVerifyResult, VerifyResult, } from "./types.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACnB,KAAK,yBAAyB,EAC9B,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,kBAAkB,GACxB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,cAAc,EACd,iBAAiB,EACjB,iBAAiB,EACjB,aAAa,EACb,oBAAoB,GACrB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,aAAa,EACb,aAAa,EACb,UAAU,EACV,aAAa,EACb,YAAY,EACZ,UAAU,EACV,eAAe,EACf,WAAW,EACX,UAAU,EACV,WAAW,EACX,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,SAAS,EACT,YAAY,EACZ,WAAW,EACX,WAAW,EACX,iBAAiB,EACjB,aAAa,EACb,sBAAsB,EACtB,KAAK,eAAe,GACrB,MAAM,aAAa,CAAC;AACrB,YAAY,EACV,gBAAgB,EAChB,UAAU,EACV,YAAY,EACZ,WAAW,EACX,cAAc,EACd,OAAO,EACP,eAAe,EACf,eAAe,EACf,eAAe,EACf,cAAc,EACd,eAAe,EACf,KAAK,EACL,kBAAkB,EAClB,YAAY,GACb,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ /* @chain-receipt/sdk — JS client (plan step 25, Phase 2).
2
+ getReceipt, listReceipts, downloadBundle, verifyReceipt (offline),
3
+ receiptFromResponse, <ReceiptBadge> (from "@chain-receipt/sdk/badge").
4
+ The crypto in ./crypto.ts is proven against the shared golden
5
+ vectors (packages/core/test-vectors/golden.json); see crypto.test.ts. */
6
+ export { ChainReceiptClient, ChainReceiptError, receiptFromResponse, receiptFromSseEvent, } from "./client.js";
7
+ export { DEFAULT_BASE_URL, fetchPublicKey, getVerifyUrl, parsePublicKey, publicKeyToBase64, verifyAnchorProof, verifyReceipt, verifyReceiptContent, } from "./verify.js";
8
+ export { base64ToBytes, bytesToBase64, bytesToHex, canonicalJson, getPublicKey, hexToBytes, keypairFromSeed, merkleProof, merkleRoot, receiptHash, receiptHashBytes, saltedPromptHash, saltedResponseHash, sha256Hex, sha256Tagged, signMessage, signReceipt, verifyMerkleProof, verifyMessage, verifyReceiptSignature, } from "./crypto.js";
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;2EAI2E;AAC3E,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,GAKpB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,cAAc,EACd,iBAAiB,EACjB,iBAAiB,EACjB,aAAa,EACb,oBAAoB,GACrB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,aAAa,EACb,aAAa,EACb,UAAU,EACV,aAAa,EACb,YAAY,EACZ,UAAU,EACV,eAAe,EACf,WAAW,EACX,UAAU,EACV,WAAW,EACX,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,SAAS,EACT,YAAY,EACZ,WAAW,EACX,WAAW,EACX,iBAAiB,EACjB,aAAa,EACb,sBAAsB,GAEvB,MAAM,aAAa,CAAC"}
@@ -0,0 +1,169 @@
1
+ export type PrivacyMode = "hash_only" | "encrypted_full";
2
+ export interface ReceiptRequest {
3
+ provider: "openai" | "anthropic" | "google" | "xai";
4
+ endpoint: string;
5
+ modelRequested: string;
6
+ paramsHash: string;
7
+ promptHash: string;
8
+ stream: boolean;
9
+ }
10
+ export interface ReceiptResponse {
11
+ modelReported?: string;
12
+ upstreamId?: string;
13
+ finishReason?: string;
14
+ responseHash: string;
15
+ rawStreamHash?: string;
16
+ usage: {
17
+ promptTokens?: number;
18
+ completionTokens?: number;
19
+ estimated: boolean;
20
+ };
21
+ completedAt: string;
22
+ status: "complete" | "incomplete" | "upstream_error";
23
+ }
24
+ /** One proxied AI call, schema "air/1". receipt_hash =
25
+ * sha256(JCS(receipt)); the Ed25519 signature covers those 32 raw
26
+ * bytes. Salts are returned to the customer once and never stored. */
27
+ export interface Receipt {
28
+ schema: "air/1";
29
+ receipt_id: string;
30
+ issuer: string;
31
+ org: string;
32
+ created_at: string;
33
+ request: ReceiptRequest;
34
+ response: ReceiptResponse;
35
+ privacy: {
36
+ mode: PrivacyMode;
37
+ salted: boolean;
38
+ };
39
+ customer_ref?: string;
40
+ prev_receipt_hash?: string;
41
+ }
42
+ /** One salt per commitment (base64, 32 random bytes each). */
43
+ export interface Salts {
44
+ prompt: string;
45
+ response: string;
46
+ }
47
+ export type AnchorStatus = "pending" | "submitted" | "confirmed" | "failed";
48
+ export interface AnchorInfo {
49
+ status: AnchorStatus;
50
+ batchId?: string;
51
+ merkleRoot?: string;
52
+ merkleProof?: {
53
+ side: "L" | "R";
54
+ hash: string;
55
+ }[];
56
+ otsUrl?: string;
57
+ bitcoin?: {
58
+ block_height: number;
59
+ block_hash: string;
60
+ block_time: string;
61
+ };
62
+ hedera?: {
63
+ topicId: string;
64
+ sequence: number;
65
+ consensusTimestamp: string;
66
+ txId: string;
67
+ merkleRoot: string;
68
+ merkleProof: {
69
+ side: "L" | "R";
70
+ hash: string;
71
+ }[];
72
+ hashscanUrl: string;
73
+ mirrorUrl: string;
74
+ };
75
+ }
76
+ /** The full air/1 envelope, as returned by GET /v1/receipts/:id and
77
+ * the public bundle download. */
78
+ export interface ReceiptEnvelope {
79
+ receipt: Receipt;
80
+ receipt_hash: string;
81
+ signature: {
82
+ alg: "ed25519";
83
+ key_id: string;
84
+ value: string;
85
+ };
86
+ anchor: AnchorInfo;
87
+ anchors: unknown[];
88
+ verify_url: string;
89
+ modelRan: string | null;
90
+ hederaAnchorMissed: boolean;
91
+ }
92
+ /** The `ai_receipt` block attached to a proxy chat-completion
93
+ * response (non-streaming) or SSE event (streaming). */
94
+ export interface AiReceiptSummary {
95
+ receipt_id: string;
96
+ verify_url: string;
97
+ receipt_hash: string;
98
+ /** base64 Ed25519 signature over the receipt hash. */
99
+ signature: string;
100
+ key_id: string;
101
+ anchor_status: "pending";
102
+ salts: Salts;
103
+ /** false when the receipt insert failed and it went to the retry
104
+ * table instead — the receipt is still signed and verifiable. */
105
+ stored: boolean;
106
+ }
107
+ /** One row of GET /v1/receipts. */
108
+ export interface ReceiptListItem {
109
+ receipt_id: string;
110
+ provider: string;
111
+ model_requested: string;
112
+ status: "complete" | "incomplete" | "upstream_error";
113
+ created_at: string;
114
+ completed_at: string;
115
+ receipt_hash: string;
116
+ anchor_status: AnchorStatus;
117
+ verify_url: string;
118
+ }
119
+ export interface ReceiptListPage {
120
+ object: "list";
121
+ data: ReceiptListItem[];
122
+ has_more: boolean;
123
+ next_cursor?: string;
124
+ }
125
+ /** One entry of GET /.well-known/receipt-keys.json. */
126
+ export interface PublicKeyEntry {
127
+ key_id: string;
128
+ algorithm: string;
129
+ /** base64 Ed25519 public key (32 bytes). */
130
+ public_key: string;
131
+ status: string;
132
+ activated_at: string;
133
+ retired_at: string | null;
134
+ revoked_at: string | null;
135
+ revocation_reason: string | null;
136
+ }
137
+ /** Result of the offline verifyReceipt. `valid` is true only when
138
+ * the hash recomputes AND the signature verifies. Nothing here
139
+ * touches the network. */
140
+ export interface VerifyResult {
141
+ valid: boolean;
142
+ hashValid: boolean;
143
+ signatureValid: boolean;
144
+ /** The claimed receipt hash ("" when the envelope had none). */
145
+ receiptHash: string;
146
+ /** The recomputed hash — differs from receiptHash when tampered. */
147
+ recomputedHash: string;
148
+ keyId: string | null;
149
+ reasons: string[];
150
+ }
151
+ /** Result of POST /v1/verify (server-side checks). */
152
+ export interface ServerVerifyResult {
153
+ valid: boolean;
154
+ receipt_id: string;
155
+ checks: {
156
+ hash: boolean | null;
157
+ signature: boolean | null;
158
+ content: boolean | null;
159
+ merkle: boolean | null;
160
+ bitcoin: boolean | null;
161
+ };
162
+ anchor: {
163
+ status: AnchorStatus | "unknown";
164
+ batchId?: string;
165
+ };
166
+ receipt?: Receipt;
167
+ error?: string;
168
+ }
169
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,WAAW,GAAG,WAAW,GAAG,gBAAgB,CAAC;AAEzD,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,QAAQ,GAAG,WAAW,GAAG,QAAQ,GAAG,KAAK,CAAC;IACpD,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,M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AO,EAAE,MAAM,EAAE,CAAC;CACnB;AAED,sDAAsD;AACtD,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,OAAO,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE;QACN,IAAI,EAAE,OAAO,GAAG,IAAI,CAAC;QACrB,SAAS,EAAE,OAAO,GAAG,IAAI,CAAC;QAC1B,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;QACxB,MAAM,EAAE,OAAO,GAAG,IAAI,CAAC;QACvB,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;KACzB,CAAC;IACF,MAAM,EAAE;QAAE,MAAM,EAAE,YAAY,GAAG,SAAS,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC/D,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB"}
package/dist/types.js ADDED
@@ -0,0 +1,8 @@
1
+ /* @chain-receipt/sdk — shared types (plan step 25).
2
+ These mirror the server's air/1 envelope and v1 API shapes
3
+ (apps/web/lib/receipts/envelope.ts, apps/web/lib/proxy/receipts.ts).
4
+ They are defined here — not imported from @chain-receipt/core —
5
+ because core is private and unpublished: a published SDK cannot
6
+ have its public types resolve through an unpublished package. */
7
+ export {};
8
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;mEAKmE"}
@@ -0,0 +1,46 @@
1
+ import { type MerkleProofStep } from "./crypto.js";
2
+ import type { PublicKeyEntry, Receipt, ReceiptEnvelope, Salts, VerifyResult } from "./types.js";
3
+ export type { MerkleProofStep };
4
+ export declare const DEFAULT_BASE_URL = "https://chainreceipt.link";
5
+ /** The public verify-page URL for a receipt. */
6
+ export declare function getVerifyUrl(receiptId: string, baseUrl?: string): string;
7
+ /** Accept a public key as a 64-char hex string, a base64 string
8
+ * (44 chars, the form in /.well-known/receipt-keys.json), or raw
9
+ * 32 bytes. Throws on anything else. */
10
+ export declare function parsePublicKey(input: string | Uint8Array): Uint8Array;
11
+ /** Fetch the public signing-key directory and return the entry for
12
+ * `keyId`. Throws when the key is not listed. The directory is
13
+ * public and needs no auth. */
14
+ export declare function fetchPublicKey(keyId: string, baseUrl?: string, fetchFn?: typeof fetch): Promise<PublicKeyEntry>;
15
+ /** Verify a receipt envelope offline: recompute the hash from the
16
+ * receipt object and verify the Ed25519 signature against the
17
+ * public key. Returns a result object — never throws on a bad
18
+ * receipt, a bad signature, or a bad key (those are `valid: false`
19
+ * with reasons). Throws only when the envelope is not an object at
20
+ * all.
21
+ *
22
+ * The public key can be passed directly, or resolved by key_id
23
+ * from the public directory when `baseUrl` is given and no key is
24
+ * passed — that second form needs one network call to fetch the
25
+ * directory; the crypto itself still runs locally. */
26
+ export declare function verifyReceipt(envelope: ReceiptEnvelope, publicKey: string | Uint8Array): VerifyResult;
27
+ export declare function verifyReceipt(envelope: ReceiptEnvelope, publicKey?: string | Uint8Array | {
28
+ fetchFrom: string;
29
+ fetchFn?: typeof fetch;
30
+ }): Promise<VerifyResult>;
31
+ /** Recompute the content hashes from the original prompt/response +
32
+ * salts and compare them to the receipt's commitments. The text
33
+ * never leaves the caller's machine. `promptBody` is the exact
34
+ * object the prompt commitment hashes: {messages, tools?,
35
+ * response_format?} — only the keys present in the request. */
36
+ export declare function verifyReceiptContent(receipt: Receipt, salts: Salts, promptBody: Record<string, unknown>, responseJson: unknown): boolean;
37
+ /** Verify a Merkle inclusion proof from an envelope's anchor block.
38
+ * `proof` steps carry "sha256:<hex>" hashes (the envelope form);
39
+ * `leafHash` and `root` accept the same tag form. */
40
+ export declare function verifyAnchorProof(leafHash: string, proof: {
41
+ side: "L" | "R";
42
+ hash: string;
43
+ }[], root: string): boolean;
44
+ /** The base64 form of a public key (handy for logging/comparing). */
45
+ export declare function publicKeyToBase64(key: Uint8Array): string;
46
+ //# sourceMappingURL=verify.d.ts.map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