@chain-receipt/sdk 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/LICENSE +21 -0
- package/README.md +184 -2
- package/dist/badge.d.ts +17 -0
- package/dist/badge.d.ts.map +1 -0
- package/dist/badge.js +6 -0
- package/dist/badge.js.map +1 -0
- package/dist/client.d.ts +162 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +272 -0
- package/dist/client.js.map +1 -0
- package/dist/crypto.d.ts +60 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +288 -0
- package/dist/crypto.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +231 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/dist/verify.d.ts +64 -0
- package/dist/verify.d.ts.map +1 -0
- package/dist/verify.js +232 -0
- package/dist/verify.js.map +1 -0
- package/package.json +62 -3
package/dist/client.js
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/* @chain-receipt/sdk — HTTP client (plan step 25).
|
|
2
|
+
Thin wrapper over the live v1 API (apps/web/app/api/v1/*) and the
|
|
3
|
+
public verify surface. The API key is a Chain Receipt key
|
|
4
|
+
(crk_live_... / crk_test_...); provider keys are never stored in
|
|
5
|
+
the SDK — they are passed per chat call in X-Provider-Key, or
|
|
6
|
+
saved on the portal Keys page. */
|
|
7
|
+
import { fetchPublicKey, getVerifyUrl, verifyReceipt, } from "./verify.js";
|
|
8
|
+
export { fetchPublicKey, getVerifyUrl, verifyReceipt };
|
|
9
|
+
/** Error from the Chain Receipt API (or from the network layer).
|
|
10
|
+
* `status` is the HTTP status when one was received; `code` is the
|
|
11
|
+
* API's error code when the body carried one. */
|
|
12
|
+
export class ChainReceiptError extends Error {
|
|
13
|
+
status;
|
|
14
|
+
code;
|
|
15
|
+
constructor(message, status = null, code = null) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "ChainReceiptError";
|
|
18
|
+
this.status = status;
|
|
19
|
+
this.code = code;
|
|
20
|
+
}
|
|
21
|
+
/** Build an error from a failed fetch Response, reading the
|
|
22
|
+
* OpenAI-shaped {error: {message, code}} body when present. */
|
|
23
|
+
static async fromResponse(res, path) {
|
|
24
|
+
let message = `Chain Receipt API error: ${res.status} ${path}`;
|
|
25
|
+
let code = null;
|
|
26
|
+
try {
|
|
27
|
+
const body = (await res.json());
|
|
28
|
+
if (typeof body.error?.message === "string") {
|
|
29
|
+
message = body.error.message;
|
|
30
|
+
}
|
|
31
|
+
if (typeof body.error?.code === "string") {
|
|
32
|
+
code = body.error.code;
|
|
33
|
+
}
|
|
34
|
+
else if (typeof body.error?.type === "string") {
|
|
35
|
+
code = body.error.type;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// Non-JSON error body (e.g. the plain-text 404 from the public
|
|
40
|
+
// download route) — keep the status-based message.
|
|
41
|
+
}
|
|
42
|
+
return new ChainReceiptError(message, res.status, code);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Parse the `ai_receipt` block out of a proxy chat-completion
|
|
47
|
+
* response. Handles both shapes:
|
|
48
|
+
* - non-streaming: the response JSON carries `ai_receipt` at the top
|
|
49
|
+
* level;
|
|
50
|
+
* - streaming: the `ai_receipt` SSE event, parsed to
|
|
51
|
+
* `{object: "ai_receipt", ai_receipt: {...}}`.
|
|
52
|
+
* Returns null when the response carries no receipt. */
|
|
53
|
+
export function receiptFromResponse(response) {
|
|
54
|
+
if (typeof response !== "object" || response === null)
|
|
55
|
+
return null;
|
|
56
|
+
const obj = response;
|
|
57
|
+
const block = obj.ai_receipt;
|
|
58
|
+
if (typeof block !== "object" || block === null)
|
|
59
|
+
return null;
|
|
60
|
+
const summary = block;
|
|
61
|
+
if (typeof summary.receipt_id !== "string")
|
|
62
|
+
return null;
|
|
63
|
+
return summary;
|
|
64
|
+
}
|
|
65
|
+
/** Parse one SSE `data:` line from a streamed proxy response and
|
|
66
|
+
* return its receipt when it is the `ai_receipt` event. Returns
|
|
67
|
+
* null for token events, `[DONE]`, and anything unparseable. */
|
|
68
|
+
export function receiptFromSseEvent(dataLine) {
|
|
69
|
+
const line = dataLine.trim();
|
|
70
|
+
if (line === "" || line === "[DONE]")
|
|
71
|
+
return null;
|
|
72
|
+
try {
|
|
73
|
+
return receiptFromResponse(JSON.parse(line));
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export class ChainReceiptClient {
|
|
80
|
+
apiKey;
|
|
81
|
+
baseUrl;
|
|
82
|
+
fetchFn;
|
|
83
|
+
constructor(options) {
|
|
84
|
+
if (!options.apiKey) {
|
|
85
|
+
throw new Error("ChainReceiptClient: apiKey is required");
|
|
86
|
+
}
|
|
87
|
+
this.apiKey = options.apiKey;
|
|
88
|
+
this.baseUrl = (options.baseUrl ?? "https://chainreceipt.link").replace(/\/$/, "");
|
|
89
|
+
this.fetchFn = options.fetch ?? fetch;
|
|
90
|
+
}
|
|
91
|
+
async request(path, init = {}) {
|
|
92
|
+
const { auth = true, ...rest } = init;
|
|
93
|
+
const headers = new Headers(rest.headers);
|
|
94
|
+
headers.set("Accept", "application/json");
|
|
95
|
+
if (auth)
|
|
96
|
+
headers.set("Authorization", `Bearer ${this.apiKey}`);
|
|
97
|
+
let res;
|
|
98
|
+
try {
|
|
99
|
+
res = await this.fetchFn(`${this.baseUrl}${path}`, {
|
|
100
|
+
...rest,
|
|
101
|
+
headers,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
throw new ChainReceiptError(`network error calling ${path}: ${err instanceof Error ? err.message : String(err)}`, null, "network_error");
|
|
106
|
+
}
|
|
107
|
+
if (!res.ok) {
|
|
108
|
+
throw await ChainReceiptError.fromResponse(res, path);
|
|
109
|
+
}
|
|
110
|
+
return (await res.json());
|
|
111
|
+
}
|
|
112
|
+
/** The public verify-page URL for a receipt. */
|
|
113
|
+
getVerifyUrl(receiptId) {
|
|
114
|
+
return getVerifyUrl(receiptId, this.baseUrl);
|
|
115
|
+
}
|
|
116
|
+
/** Full air/1 envelope for one receipt (GET /v1/receipts/:id).
|
|
117
|
+
* A receipt from another org answers the same 404 as a missing
|
|
118
|
+
* one. */
|
|
119
|
+
getReceipt(receiptId) {
|
|
120
|
+
return this.request(`/api/v1/receipts/${encodeURIComponent(receiptId)}`);
|
|
121
|
+
}
|
|
122
|
+
/** The org's receipts, newest first (GET /v1/receipts). */
|
|
123
|
+
listReceipts(params = {}) {
|
|
124
|
+
const qs = new URLSearchParams();
|
|
125
|
+
if (params.from)
|
|
126
|
+
qs.set("from", params.from);
|
|
127
|
+
if (params.to)
|
|
128
|
+
qs.set("to", params.to);
|
|
129
|
+
if (params.apiKeyId)
|
|
130
|
+
qs.set("api_key", params.apiKeyId);
|
|
131
|
+
if (params.status)
|
|
132
|
+
qs.set("status", params.status);
|
|
133
|
+
if (params.ref)
|
|
134
|
+
qs.set("ref", params.ref);
|
|
135
|
+
if (params.limit !== undefined)
|
|
136
|
+
qs.set("limit", String(params.limit));
|
|
137
|
+
if (params.cursor)
|
|
138
|
+
qs.set("cursor", params.cursor);
|
|
139
|
+
const query = qs.toString();
|
|
140
|
+
return this.request(`/api/v1/receipts${query ? `?${query}` : ""}`);
|
|
141
|
+
}
|
|
142
|
+
/** Download a receipt's public bundle (GET /verify/:id/download).
|
|
143
|
+
* No API key needed — but only public receipts can be downloaded
|
|
144
|
+
* this way; a private receipt answers 404, so use getReceipt for
|
|
145
|
+
* your own receipts instead. */
|
|
146
|
+
async downloadBundle(receiptId) {
|
|
147
|
+
const res = await this.fetchFn(`${this.baseUrl}/verify/${encodeURIComponent(receiptId)}/download`, { headers: { Accept: "application/json" } });
|
|
148
|
+
if (!res.ok) {
|
|
149
|
+
throw await ChainReceiptError.fromResponse(res, `/verify/${receiptId}/download`);
|
|
150
|
+
}
|
|
151
|
+
return (await res.json());
|
|
152
|
+
}
|
|
153
|
+
/** The published signing key for a key_id
|
|
154
|
+
* (GET /.well-known/receipt-keys.json). Public, no auth. */
|
|
155
|
+
getPublicKey(keyId) {
|
|
156
|
+
return fetchPublicKey(keyId, this.baseUrl, this.fetchFn);
|
|
157
|
+
}
|
|
158
|
+
/** Verify a receipt envelope offline — hash recompute + Ed25519
|
|
159
|
+
* signature, no network when the public key is passed directly.
|
|
160
|
+
* Pass the key (hex/base64/bytes), or omit it to resolve it by
|
|
161
|
+
* the envelope's key_id from the public key directory (one fetch).
|
|
162
|
+
* Accepts an envelope from getReceipt / downloadBundle, or the
|
|
163
|
+
* receipt + hash + signature rebuilt from an ai_receipt summary. */
|
|
164
|
+
verifyReceipt(envelope, publicKey) {
|
|
165
|
+
if (publicKey !== undefined)
|
|
166
|
+
return verifyReceipt(envelope, publicKey);
|
|
167
|
+
return verifyReceipt(envelope, {
|
|
168
|
+
fetchFrom: this.baseUrl,
|
|
169
|
+
fetchFn: this.fetchFn,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
/** Server-side verification (POST /v1/verify). Public — no API
|
|
173
|
+
* key is sent. Accepts either a full envelope or a receipt id
|
|
174
|
+
* (plus optional prompt/response + salts for the content check).
|
|
175
|
+
* The server also reports the Merkle + Bitcoin anchor checks,
|
|
176
|
+
* which the offline path cannot. */
|
|
177
|
+
verifyOnline(input) {
|
|
178
|
+
const body = "envelope" in input
|
|
179
|
+
? input.envelope
|
|
180
|
+
: {
|
|
181
|
+
receipt_id: input.receiptId,
|
|
182
|
+
...(input.prompt !== undefined ? { prompt: input.prompt } : {}),
|
|
183
|
+
...(input.response !== undefined
|
|
184
|
+
? { response: input.response }
|
|
185
|
+
: {}),
|
|
186
|
+
...(input.salts ? { salts: input.salts } : {}),
|
|
187
|
+
};
|
|
188
|
+
return this.request("/api/v1/verify", {
|
|
189
|
+
method: "POST",
|
|
190
|
+
auth: false,
|
|
191
|
+
headers: { "Content-Type": "application/json" },
|
|
192
|
+
body: JSON.stringify(body),
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
/** Receipt any JSON directly, without calling an AI provider
|
|
196
|
+
* (POST /v1/receipts). The commitment hashes `data` as sent; a
|
|
197
|
+
* non-object is wrapped as {value: data}. Returns 201 with the
|
|
198
|
+
* signed receipt summary (and the slip, for
|
|
199
|
+
* recordKind "permission_slip" — data must then be
|
|
200
|
+
* {checkout_mandate, payment_mandate}). A duplicate slip answers
|
|
201
|
+
* 200 with `duplicate: true` and no new receipt. */
|
|
202
|
+
createDirectReceipt(data, options = {}) {
|
|
203
|
+
return this.request("/api/v1/receipts", {
|
|
204
|
+
method: "POST",
|
|
205
|
+
headers: { "Content-Type": "application/json" },
|
|
206
|
+
body: JSON.stringify({
|
|
207
|
+
record_kind: options.recordKind ?? "generic",
|
|
208
|
+
data,
|
|
209
|
+
...(options.customerRef !== undefined
|
|
210
|
+
? { customer_ref: options.customerRef }
|
|
211
|
+
: {}),
|
|
212
|
+
}),
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
/** A slip's public status (GET /v1/slips/:id). Public — no API
|
|
216
|
+
* key is sent. Status only (active / cancelled / expired), never
|
|
217
|
+
* the mandate content. */
|
|
218
|
+
getSlip(slipId) {
|
|
219
|
+
return this.request(`/api/v1/slips/${encodeURIComponent(slipId)}`, { auth: false });
|
|
220
|
+
}
|
|
221
|
+
/** Revoke a slip (POST /v1/slips/:id/cancel). Two callers:
|
|
222
|
+
* - Owner (default): the org's API key authenticates; pass
|
|
223
|
+
* `cancelledAt` to backdate the cancellation (max 5 minutes
|
|
224
|
+
* into the future), or omit it for now.
|
|
225
|
+
* - Delegate: pass `signature` (base64 Ed25519 over the canonical
|
|
226
|
+
* JSON of {slip_id, slip_hash, cancelled_at}) with
|
|
227
|
+
* `cancelledAt` required — no API key is sent. Revoking an
|
|
228
|
+
* already-cancelled slip returns the existing cancellation
|
|
229
|
+
* (idempotent, 200). */
|
|
230
|
+
cancelSlip(slipId, options = {}) {
|
|
231
|
+
const delegate = options.signature !== undefined;
|
|
232
|
+
return this.request(`/api/v1/slips/${encodeURIComponent(slipId)}/cancel`, {
|
|
233
|
+
method: "POST",
|
|
234
|
+
auth: !delegate,
|
|
235
|
+
headers: { "Content-Type": "application/json" },
|
|
236
|
+
body: JSON.stringify({
|
|
237
|
+
...(options.cancelledAt !== undefined
|
|
238
|
+
? { cancelled_at: options.cancelledAt }
|
|
239
|
+
: {}),
|
|
240
|
+
...(delegate ? { signature: options.signature } : {}),
|
|
241
|
+
}),
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
/** Call the OpenAI-compatible proxy (POST
|
|
245
|
+
* /api/v1/chat/completions). The request body is OpenAI-shaped
|
|
246
|
+
* ({model: "openai/gpt-...", messages, ...}); the response is the
|
|
247
|
+
* provider's JSON plus the attached receipt, parsed out for you.
|
|
248
|
+
* Streaming is not handled here — pass stream: true and this
|
|
249
|
+
* throws; stream with your own fetch and pull the receipt from
|
|
250
|
+
* the `ai_receipt` SSE event with receiptFromSseEvent. */
|
|
251
|
+
async chatCompletions(body, options = {}) {
|
|
252
|
+
if (body.stream === true) {
|
|
253
|
+
throw new Error("chatCompletions: streaming is not supported by this helper — " +
|
|
254
|
+
"stream with your own fetch and use receiptFromSseEvent on " +
|
|
255
|
+
"the ai_receipt SSE event");
|
|
256
|
+
}
|
|
257
|
+
const headers = new Headers({
|
|
258
|
+
"Content-Type": "application/json",
|
|
259
|
+
});
|
|
260
|
+
if (options.providerKey) {
|
|
261
|
+
headers.set("X-Provider-Key", options.providerKey);
|
|
262
|
+
}
|
|
263
|
+
const data = await this.request("/api/v1/chat/completions", {
|
|
264
|
+
method: "POST",
|
|
265
|
+
headers,
|
|
266
|
+
body: JSON.stringify(body),
|
|
267
|
+
...(options.signal ? { signal: options.signal } : {}),
|
|
268
|
+
});
|
|
269
|
+
return { data, receipt: receiptFromResponse(data) };
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;oCAKoC;AACpC,OAAO,EACL,cAAc,EACd,YAAY,EACZ,aAAa,GACd,MAAM,aAAa,CAAC;AAcrB,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,aAAa,EAAE,CAAC;AAEvD;;kDAEkD;AAClD,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAC1C,MAAM,CAAgB;IACtB,IAAI,CAAgB;IAEpB,YACE,OAAe,EACf,SAAwB,IAAI,EAC5B,OAAsB,IAAI;QAE1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED;oEACgE;IAChE,MAAM,CAAC,KAAK,CAAC,YAAY,CACvB,GAAa,EACb,IAAY;QAEZ,IAAI,OAAO,GAAG,4BAA4B,GAAG,CAAC,MAAM,IAAI,IAAI,EAAE,CAAC;QAC/D,IAAI,IAAI,GAAkB,IAAI,CAAC;QAC/B,IAAI,CAAC;YACH,MAAM,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAE7B,CAAC;YACF,IAAI,OAAO,IAAI,CAAC,KAAK,EAAE,OAAO,KAAK,QAAQ,EAAE,CAAC;gBAC5C,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;YAC/B,CAAC;YACD,IAAI,OAAO,IAAI,CAAC,KAAK,EAAE,IAAI,KAAK,QAAQ,EAAE,CAAC;gBACzC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;YACzB,CAA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C;QACD,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,OAAO,CAC7B,0BAA0B,EAC1B;YACE,MAAM,EAAE,MAAM;YACd,OAAO;YACP,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;YAC1B,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACtD,CACF,CAAC;QACF,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,mBAAmB,CAAC,IAAI,CAAC,EAAE,CAAC;IACtD,CAAC;CACF"}
|
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Receipt, Salts } from "./types.js";
|
|
2
|
+
export declare function bytesToHex(bytes: Uint8Array): string;
|
|
3
|
+
export declare function hexToBytes(hex: string): Uint8Array;
|
|
4
|
+
export declare function bytesToBase64(bytes: Uint8Array): string;
|
|
5
|
+
export declare function base64ToBytes(b64: string): Uint8Array;
|
|
6
|
+
/** Canonicalize a JSON value to its RFC 8785 string form. */
|
|
7
|
+
export declare function canonicalJson(value: unknown): string;
|
|
8
|
+
/** Canonical JSON as UTF-8 bytes, ready for hashing. */
|
|
9
|
+
export declare function canonicalJsonBytes(value: unknown): Uint8Array;
|
|
10
|
+
export declare function sha256Bytes(data: Uint8Array): Uint8Array;
|
|
11
|
+
export declare function sha256Hex(data: Uint8Array): string;
|
|
12
|
+
/** "sha256:<hex>" for raw bytes. */
|
|
13
|
+
export declare function sha256Tagged(data: Uint8Array): string;
|
|
14
|
+
/** "sha256:<hex>" for bytes that are ALREADY a digest — tags without
|
|
15
|
+
* re-hashing. */
|
|
16
|
+
export declare function tagHashBytes(digest: Uint8Array): string;
|
|
17
|
+
/** Raw 32 bytes of the hash behind a "sha256:<hex>" tag. */
|
|
18
|
+
export declare function hashBytesFromTag(tag: string): Uint8Array;
|
|
19
|
+
/** 32-byte public key for a 32-byte private seed. */
|
|
20
|
+
export declare function getPublicKey(privateKey: Uint8Array): Uint8Array;
|
|
21
|
+
/** Sign a message (NOT a hash) with a 32-byte private seed. */
|
|
22
|
+
export declare function signMessage(message: Uint8Array, privateKey: Uint8Array): Uint8Array;
|
|
23
|
+
/** Verify a signature over a message with a 32-byte public key. */
|
|
24
|
+
export declare function verifyMessage(signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array): boolean;
|
|
25
|
+
/** A signing keypair from a 32-byte seed. */
|
|
26
|
+
export declare function keypairFromSeed(seed: Uint8Array): {
|
|
27
|
+
privateKey: Uint8Array;
|
|
28
|
+
publicKey: Uint8Array;
|
|
29
|
+
};
|
|
30
|
+
/** receipt_hash = sha256(JCS(receipt)). */
|
|
31
|
+
export declare function receiptHash(receipt: Receipt): string;
|
|
32
|
+
/** sha256 of the canonical receipt JSON, as raw bytes (for Merkle
|
|
33
|
+
* leaves — the leaf hash is sha256(0x00 || this)). */
|
|
34
|
+
export declare function receiptHashBytes(receipt: Receipt): Uint8Array;
|
|
35
|
+
/** Sign a receipt: Ed25519 over the 32 raw bytes of its receipt_hash. */
|
|
36
|
+
export declare function signReceipt(receipt: Receipt, privateKey: Uint8Array): string;
|
|
37
|
+
/** Verify a receipt's signature and that its hash matches its content.
|
|
38
|
+
* Returns false (never throws) on any mismatch. */
|
|
39
|
+
export declare function verifyReceiptSignature(receipt: Receipt, signatureB64: string, publicKey: Uint8Array): boolean;
|
|
40
|
+
/** Salted prompt commitment: sha256(salt || JCS(messages + tools +
|
|
41
|
+
* response_format)). */
|
|
42
|
+
export declare function saltedPromptHash(saltB64: string, requestBody: Record<string, unknown>): string;
|
|
43
|
+
/** Salted response commitment: sha256(salt || JCS(provider response)). */
|
|
44
|
+
export declare function saltedResponseHash(saltB64: string, responseJson: unknown): string;
|
|
45
|
+
/** Recompute the content hashes from the original text + salts and
|
|
46
|
+
* compare them to the receipt. This is what the public verify page
|
|
47
|
+
* runs in the browser: the text never leaves the customer's machine. */
|
|
48
|
+
export declare function verifyReceiptContent(receipt: Receipt, salts: Salts, promptBody: Record<string, unknown>, responseJson: unknown): boolean;
|
|
49
|
+
export type MerkleProofStep = {
|
|
50
|
+
side: "L" | "R";
|
|
51
|
+
hash: Uint8Array;
|
|
52
|
+
};
|
|
53
|
+
/** Merkle root of an ordered list of receipt-hash digests. */
|
|
54
|
+
export declare function merkleRoot(hashes: Uint8Array[]): Uint8Array;
|
|
55
|
+
/** Merkle proof for the leaf at `index`. Empty proof for a
|
|
56
|
+
* single-leaf tree. */
|
|
57
|
+
export declare function merkleProof(hashes: Uint8Array[], index: number): MerkleProofStep[];
|
|
58
|
+
/** Recompute the root from a leaf hash and its proof. */
|
|
59
|
+
export declare function verifyMerkleProof(leafHashBytes: Uint8Array, proof: MerkleProofStep[], expectedRoot: Uint8Array): boolean;
|
|
60
|
+
//# sourceMappingURL=crypto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAWjD,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAIpD;AAED,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CASlD;AAKD,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAYvD;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAkBrD;AA0BD,6DAA6D;AAC7D,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAOpD;AAED,wDAAwD;AACxD,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,CAE7D;AAID,wBAAgB,WAAW,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAExD;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE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,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,OAAO,GACpB,MAAM,CAOR;AAED;;yEAEyE;AACzE,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,KAAK,EACZ,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,YAAY,EAAE,OAAO,GACpB,OAAO,CAOT;AAID,MAAM,MAAM,eAAe,GAAG;IAAE,IAAI,EAAE,GAAG,GAAG,GAAG,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC;AAiBpE,8DAA8D;AAC9D,wBAAgB,UAAU,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,UAAU,CAe3D;AAED;wBACwB;AACxB,wBAAgB,WAAW,CACzB,MAAM,EAAE,UAAU,EAAE,EACpB,KAAK,EAAE,MAAM,GACZ,eAAe,EAAE,CAuBnB;AAED,yDAAyD;AACzD,wBAAgB,iBAAiB,CAC/B,aAAa,EAAE,UAAU,EACzB,KAAK,EAAE,eAAe,EAAE,EACxB,YAAY,EAAE,UAAU,GACvB,OAAO,CAaT"}
|
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
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 (schemas "air/1" and "air/2" —
|
|
160
|
+
the hash covers the receipt object as given, so an air/2 link
|
|
161
|
+
is bound by the same code path) --- */
|
|
162
|
+
/** receipt_hash = sha256(JCS(receipt)). */
|
|
163
|
+
export function receiptHash(receipt) {
|
|
164
|
+
return sha256Tagged(canonicalJsonBytes(receipt));
|
|
165
|
+
}
|
|
166
|
+
/** sha256 of the canonical receipt JSON, as raw bytes (for Merkle
|
|
167
|
+
* leaves — the leaf hash is sha256(0x00 || this)). */
|
|
168
|
+
export function receiptHashBytes(receipt) {
|
|
169
|
+
return sha256Bytes(canonicalJsonBytes(receipt));
|
|
170
|
+
}
|
|
171
|
+
/** Sign a receipt: Ed25519 over the 32 raw bytes of its receipt_hash. */
|
|
172
|
+
export function signReceipt(receipt, privateKey) {
|
|
173
|
+
const hashBytes = hashBytesFromTag(receiptHash(receipt));
|
|
174
|
+
return bytesToBase64(signMessage(hashBytes, privateKey));
|
|
175
|
+
}
|
|
176
|
+
/** Verify a receipt's signature and that its hash matches its content.
|
|
177
|
+
* Returns false (never throws) on any mismatch. */
|
|
178
|
+
export function verifyReceiptSignature(receipt, signatureB64, publicKey) {
|
|
179
|
+
try {
|
|
180
|
+
const hashBytes = hashBytesFromTag(receiptHash(receipt));
|
|
181
|
+
const signature = base64ToBytes(signatureB64);
|
|
182
|
+
return verifyMessage(signature, hashBytes, publicKey);
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/** Salted prompt commitment: sha256(salt || JCS(messages + tools +
|
|
189
|
+
* response_format)). */
|
|
190
|
+
export function saltedPromptHash(saltB64, requestBody) {
|
|
191
|
+
const salt = base64ToBytes(saltB64);
|
|
192
|
+
const body = canonicalJsonBytes(requestBody);
|
|
193
|
+
const input = new Uint8Array(salt.length + body.length);
|
|
194
|
+
input.set(salt, 0);
|
|
195
|
+
input.set(body, salt.length);
|
|
196
|
+
return sha256Tagged(input);
|
|
197
|
+
}
|
|
198
|
+
/** Salted response commitment: sha256(salt || JCS(provider response)). */
|
|
199
|
+
export function saltedResponseHash(saltB64, responseJson) {
|
|
200
|
+
const salt = base64ToBytes(saltB64);
|
|
201
|
+
const body = canonicalJsonBytes(responseJson);
|
|
202
|
+
const input = new Uint8Array(salt.length + body.length);
|
|
203
|
+
input.set(salt, 0);
|
|
204
|
+
input.set(body, salt.length);
|
|
205
|
+
return sha256Tagged(input);
|
|
206
|
+
}
|
|
207
|
+
/** Recompute the content hashes from the original text + salts and
|
|
208
|
+
* compare them to the receipt. This is what the public verify page
|
|
209
|
+
* runs in the browser: the text never leaves the customer's machine. */
|
|
210
|
+
export function verifyReceiptContent(receipt, salts, promptBody, responseJson) {
|
|
211
|
+
return (saltedPromptHash(salts.prompt, promptBody) ===
|
|
212
|
+
receipt.request.promptHash &&
|
|
213
|
+
saltedResponseHash(salts.response, responseJson) ===
|
|
214
|
+
receipt.response.responseHash);
|
|
215
|
+
}
|
|
216
|
+
function leafHash(hash) {
|
|
217
|
+
const input = new Uint8Array(33);
|
|
218
|
+
input[0] = 0x00;
|
|
219
|
+
input.set(hash, 1);
|
|
220
|
+
return sha256Bytes(input);
|
|
221
|
+
}
|
|
222
|
+
function nodeHash(left, right) {
|
|
223
|
+
const input = new Uint8Array(65);
|
|
224
|
+
input[0] = 0x01;
|
|
225
|
+
input.set(left, 1);
|
|
226
|
+
input.set(right, 33);
|
|
227
|
+
return sha256Bytes(input);
|
|
228
|
+
}
|
|
229
|
+
/** Merkle root of an ordered list of receipt-hash digests. */
|
|
230
|
+
export function merkleRoot(hashes) {
|
|
231
|
+
if (hashes.length === 0) {
|
|
232
|
+
throw new Error("merkleRoot: empty leaf list");
|
|
233
|
+
}
|
|
234
|
+
let level = hashes.map(leafHash);
|
|
235
|
+
while (level.length > 1) {
|
|
236
|
+
const next = [];
|
|
237
|
+
for (let i = 0; i < level.length; i += 2) {
|
|
238
|
+
const left = level[i];
|
|
239
|
+
const right = level[i + 1] ?? left; // duplicate the last node
|
|
240
|
+
next.push(nodeHash(left, right));
|
|
241
|
+
}
|
|
242
|
+
level = next;
|
|
243
|
+
}
|
|
244
|
+
return level[0];
|
|
245
|
+
}
|
|
246
|
+
/** Merkle proof for the leaf at `index`. Empty proof for a
|
|
247
|
+
* single-leaf tree. */
|
|
248
|
+
export function merkleProof(hashes, index) {
|
|
249
|
+
if (index < 0 || index >= hashes.length) {
|
|
250
|
+
throw new Error(`merkleProof: index ${index} out of range`);
|
|
251
|
+
}
|
|
252
|
+
const proof = [];
|
|
253
|
+
let level = hashes.map(leafHash);
|
|
254
|
+
let idx = index;
|
|
255
|
+
while (level.length > 1) {
|
|
256
|
+
const isRight = idx % 2 === 1;
|
|
257
|
+
const siblingIdx = isRight ? idx - 1 : idx + 1;
|
|
258
|
+
const sibling = siblingIdx < level.length ? level[siblingIdx] : level[idx];
|
|
259
|
+
proof.push({ side: isRight ? "L" : "R", hash: sibling });
|
|
260
|
+
const next = [];
|
|
261
|
+
for (let i = 0; i < level.length; i += 2) {
|
|
262
|
+
const left = level[i];
|
|
263
|
+
const right = level[i + 1] ?? left;
|
|
264
|
+
next.push(nodeHash(left, right));
|
|
265
|
+
}
|
|
266
|
+
level = next;
|
|
267
|
+
idx = Math.floor(idx / 2);
|
|
268
|
+
}
|
|
269
|
+
return proof;
|
|
270
|
+
}
|
|
271
|
+
/** Recompute the root from a leaf hash and its proof. */
|
|
272
|
+
export function verifyMerkleProof(leafHashBytes, proof, expectedRoot) {
|
|
273
|
+
let current = leafHash(leafHashBytes);
|
|
274
|
+
for (const step of proof) {
|
|
275
|
+
current =
|
|
276
|
+
step.side === "L"
|
|
277
|
+
? nodeHash(step.hash, current)
|
|
278
|
+
: nodeHash(current, step.hash);
|
|
279
|
+
}
|
|
280
|
+
if (current.length !== expectedRoot.length)
|
|
281
|
+
return false;
|
|
282
|
+
for (let i = 0; i < current.length; i++) {
|
|
283
|
+
if (current[i] !== expectedRoot[i])
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
//# 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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