@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/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { ChainReceiptClient, ChainReceiptError, receiptFromResponse, receiptFromSseEvent, type CancelSlipOptions, type ChainReceiptClientOptions, type ChatCompletionsOptions, type ChatCompletionsResult, type CreateDirectReceiptOptions, 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, CancelSlipResult, DirectReceiptResult, PrivacyMode, PublicKeyEntry, Receipt, ReceiptEnvelope, ReceiptListItem, ReceiptListPage, ReceiptRequest, ReceiptResponse, RecordKind, Salts, ServerVerifyResult, SlipCancellation, SlipStatus, SlipStatusView, 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,iBAAiB,EACtB,KAAK,yBAAyB,EAC9B,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,0BAA0B,EAC/B,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,gBAAgB,EAChB,mBAAmB,EACnB,WAAW,EACX,cAAc,EACd,OAAO,EACP,eAAe,EACf,eAAe,EACf,eAAe,EACf,cAAc,EACd,eAAe,EACf,UAAU,EACV,KAAK,EACL,kBAAkB,EAClB,gBAAgB,EAChB,UAAU,EACV,cAAc,EACd,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,GAOpB,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"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
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" or "air/2" (AUD-28: air/2
|
|
25
|
+
* binds prev_receipt_hash into the signed object). receipt_hash =
|
|
26
|
+
* sha256(JCS(receipt)); the Ed25519 signature covers those 32 raw
|
|
27
|
+
* bytes. Salts are returned to the customer once and never stored. */
|
|
28
|
+
export interface Receipt {
|
|
29
|
+
schema: "air/1" | "air/2";
|
|
30
|
+
receipt_id: string;
|
|
31
|
+
issuer: string;
|
|
32
|
+
org: string;
|
|
33
|
+
created_at: string;
|
|
34
|
+
request: ReceiptRequest;
|
|
35
|
+
response: ReceiptResponse;
|
|
36
|
+
privacy: {
|
|
37
|
+
mode: PrivacyMode;
|
|
38
|
+
salted: boolean;
|
|
39
|
+
};
|
|
40
|
+
customer_ref?: string;
|
|
41
|
+
prev_receipt_hash?: string;
|
|
42
|
+
}
|
|
43
|
+
/** One salt per commitment (base64, 32 random bytes each). */
|
|
44
|
+
export interface Salts {
|
|
45
|
+
prompt: string;
|
|
46
|
+
response: string;
|
|
47
|
+
}
|
|
48
|
+
export type AnchorStatus = "pending" | "submitted" | "confirmed" | "failed";
|
|
49
|
+
export interface AnchorInfo {
|
|
50
|
+
status: AnchorStatus;
|
|
51
|
+
batchId?: string;
|
|
52
|
+
merkleRoot?: string;
|
|
53
|
+
merkleProof?: {
|
|
54
|
+
side: "L" | "R";
|
|
55
|
+
hash: string;
|
|
56
|
+
}[];
|
|
57
|
+
otsUrl?: string;
|
|
58
|
+
bitcoin?: {
|
|
59
|
+
block_height: number;
|
|
60
|
+
block_hash: string;
|
|
61
|
+
block_time: string;
|
|
62
|
+
};
|
|
63
|
+
hedera?: {
|
|
64
|
+
topicId: string;
|
|
65
|
+
sequence: number;
|
|
66
|
+
consensusTimestamp: string;
|
|
67
|
+
txId: string;
|
|
68
|
+
merkleRoot: string;
|
|
69
|
+
merkleProof: {
|
|
70
|
+
side: "L" | "R";
|
|
71
|
+
hash: string;
|
|
72
|
+
}[];
|
|
73
|
+
hashscanUrl: string;
|
|
74
|
+
mirrorUrl: string;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** The full air/1 envelope, as returned by GET /v1/receipts/:id and
|
|
78
|
+
* the public bundle download. */
|
|
79
|
+
export interface ReceiptEnvelope {
|
|
80
|
+
receipt: Receipt;
|
|
81
|
+
receipt_hash: string;
|
|
82
|
+
signature: {
|
|
83
|
+
alg: "ed25519";
|
|
84
|
+
key_id: string;
|
|
85
|
+
value: string;
|
|
86
|
+
};
|
|
87
|
+
anchor: AnchorInfo;
|
|
88
|
+
anchors: unknown[];
|
|
89
|
+
verify_url: string;
|
|
90
|
+
modelRan: string | null;
|
|
91
|
+
hederaAnchorMissed: boolean;
|
|
92
|
+
}
|
|
93
|
+
/** The `ai_receipt` block attached to a proxy chat-completion
|
|
94
|
+
* response (non-streaming) or SSE event (streaming). */
|
|
95
|
+
export interface AiReceiptSummary {
|
|
96
|
+
receipt_id: string;
|
|
97
|
+
verify_url: string;
|
|
98
|
+
receipt_hash: string;
|
|
99
|
+
/** base64 Ed25519 signature over the receipt hash. */
|
|
100
|
+
signature: string;
|
|
101
|
+
key_id: string;
|
|
102
|
+
anchor_status: "pending";
|
|
103
|
+
salts: Salts;
|
|
104
|
+
/** false when the receipt insert failed and it went to the retry
|
|
105
|
+
* table instead — the receipt is still signed and verifiable. */
|
|
106
|
+
stored: boolean;
|
|
107
|
+
}
|
|
108
|
+
/** One row of GET /v1/receipts. */
|
|
109
|
+
export interface ReceiptListItem {
|
|
110
|
+
receipt_id: string;
|
|
111
|
+
provider: string;
|
|
112
|
+
model_requested: string;
|
|
113
|
+
status: "complete" | "incomplete" | "upstream_error";
|
|
114
|
+
created_at: string;
|
|
115
|
+
completed_at: string;
|
|
116
|
+
receipt_hash: string;
|
|
117
|
+
anchor_status: AnchorStatus;
|
|
118
|
+
verify_url: string;
|
|
119
|
+
}
|
|
120
|
+
export interface ReceiptListPage {
|
|
121
|
+
object: "list";
|
|
122
|
+
data: ReceiptListItem[];
|
|
123
|
+
has_more: boolean;
|
|
124
|
+
next_cursor?: string;
|
|
125
|
+
}
|
|
126
|
+
/** One entry of GET /.well-known/receipt-keys.json. */
|
|
127
|
+
export interface PublicKeyEntry {
|
|
128
|
+
key_id: string;
|
|
129
|
+
algorithm: string;
|
|
130
|
+
/** base64 Ed25519 public key (32 bytes). */
|
|
131
|
+
public_key: string;
|
|
132
|
+
status: string;
|
|
133
|
+
activated_at: string;
|
|
134
|
+
retired_at: string | null;
|
|
135
|
+
revoked_at: string | null;
|
|
136
|
+
revocation_reason: string | null;
|
|
137
|
+
}
|
|
138
|
+
/** The record kind for POST /v1/receipts. "generic" receipts any
|
|
139
|
+
* JSON as sent; "permission_slip" issues a Permission Slip from
|
|
140
|
+
* {checkout_mandate, payment_mandate} (a paid add-on — Developer
|
|
141
|
+
* +$15/month, Team +$49/month; included on Business/Enterprise). */
|
|
142
|
+
export type RecordKind = "generic" | "permission_slip";
|
|
143
|
+
/** The public status of a Permission Slip (GET /v1/slips/:id). It
|
|
144
|
+
* carries no mandate content — a merchant already holds the
|
|
145
|
+
* mandate from the agent; this only confirms the slip is still
|
|
146
|
+
* good. */
|
|
147
|
+
export type SlipStatus = "active" | "cancelled" | "expired";
|
|
148
|
+
export interface SlipStatusView {
|
|
149
|
+
slip_id: string;
|
|
150
|
+
status: SlipStatus;
|
|
151
|
+
slip_hash: string;
|
|
152
|
+
receipt_id: string;
|
|
153
|
+
verify_url: string;
|
|
154
|
+
expires_at: string;
|
|
155
|
+
cancelled_at: string | null;
|
|
156
|
+
cancellation_receipt_id: string | null;
|
|
157
|
+
}
|
|
158
|
+
/** Result of POST /v1/receipts (201, or 200 for a duplicate slip).
|
|
159
|
+
* `receipt` is the signed summary (same shape as the `ai_receipt`
|
|
160
|
+
* block) — absent for a duplicate slip, which created no new
|
|
161
|
+
* receipt. `warning` appears when the receipt was signed but not
|
|
162
|
+
* stored (retry the call). */
|
|
163
|
+
export interface DirectReceiptResult {
|
|
164
|
+
record_kind: RecordKind;
|
|
165
|
+
receipt?: AiReceiptSummary;
|
|
166
|
+
slip?: SlipStatusView;
|
|
167
|
+
warning?: string;
|
|
168
|
+
/** True when the slip already existed (permission_slip only). */
|
|
169
|
+
duplicate?: boolean;
|
|
170
|
+
receipt_id?: string;
|
|
171
|
+
verify_url?: string;
|
|
172
|
+
}
|
|
173
|
+
export interface SlipCancellation {
|
|
174
|
+
cancellation_receipt_id: string;
|
|
175
|
+
verify_url: string;
|
|
176
|
+
cancelled_by: string;
|
|
177
|
+
cancelled_at: string;
|
|
178
|
+
}
|
|
179
|
+
/** Result of POST /v1/slips/:id/cancel (201, or 200 when the slip
|
|
180
|
+
* was already cancelled — idempotent). */
|
|
181
|
+
export interface CancelSlipResult {
|
|
182
|
+
duplicate: boolean;
|
|
183
|
+
slip: SlipStatusView;
|
|
184
|
+
cancellation: SlipCancellation;
|
|
185
|
+
}
|
|
186
|
+
/** Result of the offline verifyReceipt. `valid` is true only when
|
|
187
|
+
* the hash recomputes AND the signature verifies. Nothing here
|
|
188
|
+
* touches the network. AUD-04: `keyStatus` is the signing key's
|
|
189
|
+
* status verdict for this receipt — a revoked-after receipt is
|
|
190
|
+
* invalid (forgery window) even with good crypto; a revoked-before
|
|
191
|
+
* receipt stays valid with `keyWarning` set. "unknown" when the key
|
|
192
|
+
* was passed directly (no status to assess) or could not be
|
|
193
|
+
* resolved. */
|
|
194
|
+
export interface VerifyResult {
|
|
195
|
+
valid: boolean;
|
|
196
|
+
hashValid: boolean;
|
|
197
|
+
signatureValid: boolean;
|
|
198
|
+
/** The claimed receipt hash ("" when the envelope had none). */
|
|
199
|
+
receiptHash: string;
|
|
200
|
+
/** The recomputed hash — differs from receiptHash when tampered. */
|
|
201
|
+
recomputedHash: string;
|
|
202
|
+
keyId: string | null;
|
|
203
|
+
reasons: string[];
|
|
204
|
+
/** Signing-key status verdict: "ok" | "revoked-before" |
|
|
205
|
+
* "revoked-after" | "unknown". */
|
|
206
|
+
keyStatus?: "ok" | "revoked-before" | "revoked-after" | "unknown";
|
|
207
|
+
/** Human-readable key warning, if any (revoked-before/after). */
|
|
208
|
+
keyWarning?: string | null;
|
|
209
|
+
}
|
|
210
|
+
/** Result of POST /v1/verify (server-side checks). */
|
|
211
|
+
export interface ServerVerifyResult {
|
|
212
|
+
valid: boolean;
|
|
213
|
+
receipt_id: string;
|
|
214
|
+
checks: {
|
|
215
|
+
hash: boolean | null;
|
|
216
|
+
signature: boolean | null;
|
|
217
|
+
content: boolean | null;
|
|
218
|
+
merkle: boolean | null;
|
|
219
|
+
bitcoin: boolean | null;
|
|
220
|
+
};
|
|
221
|
+
/** AUD-04: the signing key's status verdict for this receipt. */
|
|
222
|
+
key_status?: "ok" | "revoked-before" | "revoked-after" | "unknown";
|
|
223
|
+
key_warning?: string | null;
|
|
224
|
+
anchor: {
|
|
225
|
+
status: AnchorStatus | "unknown";
|
|
226
|
+
batchId?: string;
|
|
227
|
+
};
|
|
228
|
+
receipt?: Receipt;
|
|
229
|
+
error?: string;
|
|
230
|
+
}
|
|
231
|
+
//# 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,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,eAAe;IAC9B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,KAAK,EAAE;QACL,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,gBAAgB,CAAC,EAAE,MAAM,CAAC;QAC1B,SAAS,EAAE,OAAO,CAAC;KACpB,CAAC;IACF,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,UAAU,GAAG,YAAY,GAAG,gBAAgB,CAAC;CACtD;AAED;;;uEAGuE;AACvE,MAAM,WAAW,OAAO;IACtB,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC;IAC1B,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,MAAM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;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"}
|
package/dist/verify.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
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
|
+
export type KeyStatusKind = "ok" | "revoked-before" | "revoked-after" | "unknown";
|
|
8
|
+
export type KeyStatusAssessment = {
|
|
9
|
+
kind: "ok";
|
|
10
|
+
status: "active" | "retired";
|
|
11
|
+
} | {
|
|
12
|
+
kind: "revoked-before";
|
|
13
|
+
revokedAt: string;
|
|
14
|
+
} | {
|
|
15
|
+
kind: "revoked-after";
|
|
16
|
+
revokedAt: string;
|
|
17
|
+
} | {
|
|
18
|
+
kind: "unknown";
|
|
19
|
+
};
|
|
20
|
+
export declare function assessSigningKey(key: {
|
|
21
|
+
status: string;
|
|
22
|
+
revokedAt: string | null;
|
|
23
|
+
} | null | undefined, receiptCreatedAt: string): KeyStatusAssessment;
|
|
24
|
+
export declare function keyStatusWarning(assessment: KeyStatusAssessment): string | null;
|
|
25
|
+
/** Accept a public key as a 64-char hex string, a base64 string
|
|
26
|
+
* (44 chars, the form in /.well-known/receipt-keys.json), or raw
|
|
27
|
+
* 32 bytes. Throws on anything else. */
|
|
28
|
+
export declare function parsePublicKey(input: string | Uint8Array): Uint8Array;
|
|
29
|
+
/** Fetch the public signing-key directory and return the entry for
|
|
30
|
+
* `keyId`. Throws when the key is not listed. The directory is
|
|
31
|
+
* public and needs no auth. */
|
|
32
|
+
export declare function fetchPublicKey(keyId: string, baseUrl?: string, fetchFn?: typeof fetch): Promise<PublicKeyEntry>;
|
|
33
|
+
/** Verify a receipt envelope offline: recompute the hash from the
|
|
34
|
+
* receipt object and verify the Ed25519 signature against the
|
|
35
|
+
* public key. Returns a result object — never throws on a bad
|
|
36
|
+
* receipt, a bad signature, or a bad key (those are `valid: false`
|
|
37
|
+
* with reasons). Throws only when the envelope is not an object at
|
|
38
|
+
* all.
|
|
39
|
+
*
|
|
40
|
+
* The public key can be passed directly, or resolved by key_id
|
|
41
|
+
* from the public directory when `baseUrl` is given and no key is
|
|
42
|
+
* passed — that second form needs one network call to fetch the
|
|
43
|
+
* directory; the crypto itself still runs locally. */
|
|
44
|
+
export declare function verifyReceipt(envelope: ReceiptEnvelope, publicKey: string | Uint8Array): VerifyResult;
|
|
45
|
+
export declare function verifyReceipt(envelope: ReceiptEnvelope, publicKey?: string | Uint8Array | {
|
|
46
|
+
fetchFrom: string;
|
|
47
|
+
fetchFn?: typeof fetch;
|
|
48
|
+
}): Promise<VerifyResult>;
|
|
49
|
+
/** Recompute the content hashes from the original prompt/response +
|
|
50
|
+
* salts and compare them to the receipt's commitments. The text
|
|
51
|
+
* never leaves the caller's machine. `promptBody` is the exact
|
|
52
|
+
* object the prompt commitment hashes: {messages, tools?,
|
|
53
|
+
* response_format?} — only the keys present in the request. */
|
|
54
|
+
export declare function verifyReceiptContent(receipt: Receipt, salts: Salts, promptBody: Record<string, unknown>, responseJson: unknown): boolean;
|
|
55
|
+
/** Verify a Merkle inclusion proof from an envelope's anchor block.
|
|
56
|
+
* `proof` steps carry "sha256:<hex>" hashes (the envelope form);
|
|
57
|
+
* `leafHash` and `root` accept the same tag form. */
|
|
58
|
+
export declare function verifyAnchorProof(leafHash: string, proof: {
|
|
59
|
+
side: "L" | "R";
|
|
60
|
+
hash: string;
|
|
61
|
+
}[], root: string): boolean;
|
|
62
|
+
/** The base64 form of a public key (handy for logging/comparing). */
|
|
63
|
+
export declare function publicKeyToBase64(key: Uint8Array): string;
|
|
64
|
+
//# sourceMappingURL=verify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../src/verify.ts"],"names":[],"mappings":"AASA,OAAO,EASL,KAAK,eAAe,EACrB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EACV,cAAc,EACd,OAAO,EACP,eAAe,EACf,KAAK,EACL,YAAY,EACb,MAAM,YAAY,CAAC;AAEpB,YAAY,EAAE,eAAe,EAAE,CAAC;AAEhC,eAAO,MAAM,gBAAgB,8BAA8B,CAAC;AAE5D,gDAAgD;AAChD,wBAAgB,YAAY,CAC1B,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,MAAyB,GACjC,MAAM,CAER;AASD,MAAM,MAAM,aAAa,GACrB,IAAI,GACJ,gBAAgB,GAChB,eAAe,GACf,SAAS,CAAC;AAEd,MAAM,MAAM,mBAAmB,GAC3B;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,MAAM,EAAE,QAAQ,GAAG,SAAS,CAAA;CAAE,GAC5C;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAC7C;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAC5C;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,CAAC;AAExB,wBAAgB,gBAAgB,CAC9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;AACrE,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,UAAU,GAAG,MAAM,CAEzD"}
|
package/dist/verify.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/* Offline receipt verification (plan step 25).
|
|
2
|
+
verifyReceipt runs entirely locally: it recomputes the receipt
|
|
3
|
+
hash from the receipt object (RFC 8785 JCS + SHA-256) and checks
|
|
4
|
+
the Ed25519 signature against a public key — no network call, so
|
|
5
|
+
it works in a browser, in a test, or on a downloaded bundle.
|
|
6
|
+
The public key comes from the caller (hex, base64, or raw bytes)
|
|
7
|
+
or from the public key directory at /.well-known/receipt-keys.json
|
|
8
|
+
via fetchPublicKey — retired keys stay listed there so old
|
|
9
|
+
receipts keep verifying. */
|
|
10
|
+
import { base64ToBytes, bytesToBase64, hashBytesFromTag, hexToBytes, receiptHash, verifyMerkleProof as verifyMerkleProofCrypto, verifyReceiptContent as verifyContentCrypto, verifyReceiptSignature, } from "./crypto.js";
|
|
11
|
+
export const DEFAULT_BASE_URL = "https://chainreceipt.link";
|
|
12
|
+
/** The public verify-page URL for a receipt. */
|
|
13
|
+
export function getVerifyUrl(receiptId, baseUrl = DEFAULT_BASE_URL) {
|
|
14
|
+
return `${baseUrl.replace(/\/$/, "")}/verify/${receiptId}`;
|
|
15
|
+
}
|
|
16
|
+
export function assessSigningKey(key, receiptCreatedAt) {
|
|
17
|
+
if (!key)
|
|
18
|
+
return { kind: "unknown" };
|
|
19
|
+
if (key.status === "revoked") {
|
|
20
|
+
if (!key.revokedAt)
|
|
21
|
+
return { kind: "revoked-after", revokedAt: "" };
|
|
22
|
+
const created = Date.parse(receiptCreatedAt);
|
|
23
|
+
const revoked = Date.parse(key.revokedAt);
|
|
24
|
+
if (!Number.isNaN(created) &&
|
|
25
|
+
!Number.isNaN(revoked) &&
|
|
26
|
+
created < revoked) {
|
|
27
|
+
return { kind: "revoked-before", revokedAt: key.revokedAt };
|
|
28
|
+
}
|
|
29
|
+
return { kind: "revoked-after", revokedAt: key.revokedAt };
|
|
30
|
+
}
|
|
31
|
+
if (key.status === "retired")
|
|
32
|
+
return { kind: "ok", status: "retired" };
|
|
33
|
+
return { kind: "ok", status: "active" };
|
|
34
|
+
}
|
|
35
|
+
export function keyStatusWarning(assessment) {
|
|
36
|
+
if (assessment.kind === "revoked-before") {
|
|
37
|
+
return (`This signing key was later revoked on ${assessment.revokedAt}. ` +
|
|
38
|
+
`This receipt was created before the revocation, so it still ` +
|
|
39
|
+
`verifies — but the key must not be trusted for anything signed ` +
|
|
40
|
+
`after that date.`);
|
|
41
|
+
}
|
|
42
|
+
if (assessment.kind === "revoked-after") {
|
|
43
|
+
return (`This signing key was revoked on ${assessment.revokedAt || "an unknown date"}. ` +
|
|
44
|
+
`This receipt was created after the revocation, so it is INVALID — ` +
|
|
45
|
+
`it may be a forgery signed with the compromised key.`);
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
/** Accept a public key as a 64-char hex string, a base64 string
|
|
50
|
+
* (44 chars, the form in /.well-known/receipt-keys.json), or raw
|
|
51
|
+
* 32 bytes. Throws on anything else. */
|
|
52
|
+
export function parsePublicKey(input) {
|
|
53
|
+
if (input instanceof Uint8Array) {
|
|
54
|
+
if (input.length !== 32) {
|
|
55
|
+
throw new Error(`public key must be 32 bytes, got ${input.length}`);
|
|
56
|
+
}
|
|
57
|
+
return input;
|
|
58
|
+
}
|
|
59
|
+
if (/^[0-9a-fA-F]{64}$/.test(input))
|
|
60
|
+
return hexToBytes(input);
|
|
61
|
+
const bytes = base64ToBytes(input);
|
|
62
|
+
if (bytes.length !== 32) {
|
|
63
|
+
throw new Error(`public key must decode to 32 bytes, got ${bytes.length}`);
|
|
64
|
+
}
|
|
65
|
+
return bytes;
|
|
66
|
+
}
|
|
67
|
+
/** Fetch the public signing-key directory and return the entry for
|
|
68
|
+
* `keyId`. Throws when the key is not listed. The directory is
|
|
69
|
+
* public and needs no auth. */
|
|
70
|
+
export async function fetchPublicKey(keyId, baseUrl = DEFAULT_BASE_URL, fetchFn = fetch) {
|
|
71
|
+
const url = `${baseUrl.replace(/\/$/, "")}/.well-known/receipt-keys.json`;
|
|
72
|
+
const res = await fetchFn(url, {
|
|
73
|
+
headers: { Accept: "application/json" },
|
|
74
|
+
});
|
|
75
|
+
if (!res.ok) {
|
|
76
|
+
throw new Error(`failed to fetch signing keys (${res.status} ${url})`);
|
|
77
|
+
}
|
|
78
|
+
const doc = (await res.json());
|
|
79
|
+
const entry = (doc.keys ?? []).find((k) => k.key_id === keyId);
|
|
80
|
+
if (!entry) {
|
|
81
|
+
throw new Error(`signing key ${keyId} is not published`);
|
|
82
|
+
}
|
|
83
|
+
return entry;
|
|
84
|
+
}
|
|
85
|
+
export function verifyReceipt(envelope, publicKey) {
|
|
86
|
+
if (typeof envelope !== "object" ||
|
|
87
|
+
envelope === null ||
|
|
88
|
+
typeof envelope.receipt !== "object" ||
|
|
89
|
+
envelope.receipt === null) {
|
|
90
|
+
throw new Error("verifyReceipt: envelope.receipt is missing");
|
|
91
|
+
}
|
|
92
|
+
const env = envelope;
|
|
93
|
+
const keyId = env.signature?.key_id ?? null;
|
|
94
|
+
const check = (key, keyEntry = null) => {
|
|
95
|
+
const reasons = [];
|
|
96
|
+
let hashValid = false;
|
|
97
|
+
let recomputedHash = "";
|
|
98
|
+
try {
|
|
99
|
+
recomputedHash = receiptHash(env.receipt);
|
|
100
|
+
hashValid = recomputedHash === env.receipt_hash;
|
|
101
|
+
if (!hashValid)
|
|
102
|
+
reasons.push("receipt_hash does not recompute");
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
reasons.push(`receipt hash failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
106
|
+
}
|
|
107
|
+
let signatureValid = false;
|
|
108
|
+
try {
|
|
109
|
+
const sigValue = env.signature?.value;
|
|
110
|
+
if (typeof sigValue !== "string" || sigValue.length === 0) {
|
|
111
|
+
reasons.push("envelope has no signature value");
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
signatureValid = verifyReceiptSignature(env.receipt, sigValue, key);
|
|
115
|
+
if (!signatureValid)
|
|
116
|
+
reasons.push("signature does not verify");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
reasons.push("signature check failed");
|
|
121
|
+
}
|
|
122
|
+
// AUD-04: the key's status verdict for this receipt. A direct
|
|
123
|
+
// key carries no status, so it is unassessable ("unknown"); a key
|
|
124
|
+
// resolved from the public directory carries status + revoked_at.
|
|
125
|
+
const keyAssessment = assessSigningKey(keyEntry, typeof env.receipt.created_at === "string"
|
|
126
|
+
? env.receipt.created_at
|
|
127
|
+
: "");
|
|
128
|
+
const keyWarning = keyStatusWarning(keyAssessment);
|
|
129
|
+
const keyInvalidates = keyAssessment.kind === "revoked-after";
|
|
130
|
+
if (keyInvalidates) {
|
|
131
|
+
reasons.push("signing key was revoked before this receipt was created");
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
valid: hashValid && signatureValid && !keyInvalidates,
|
|
135
|
+
hashValid,
|
|
136
|
+
signatureValid,
|
|
137
|
+
receiptHash: typeof env.receipt_hash === "string" ? env.receipt_hash : "",
|
|
138
|
+
recomputedHash,
|
|
139
|
+
keyId,
|
|
140
|
+
reasons,
|
|
141
|
+
keyStatus: keyAssessment.kind,
|
|
142
|
+
keyWarning,
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
if (typeof publicKey === "string" || publicKey instanceof Uint8Array) {
|
|
146
|
+
let key;
|
|
147
|
+
try {
|
|
148
|
+
key = parsePublicKey(publicKey);
|
|
149
|
+
}
|
|
150
|
+
catch (err) {
|
|
151
|
+
return {
|
|
152
|
+
valid: false,
|
|
153
|
+
hashValid: false,
|
|
154
|
+
signatureValid: false,
|
|
155
|
+
receiptHash: typeof env.receipt_hash === "string" ? env.receipt_hash : "",
|
|
156
|
+
recomputedHash: "",
|
|
157
|
+
keyId,
|
|
158
|
+
reasons: [
|
|
159
|
+
`unusable public key: ${err instanceof Error ? err.message : String(err)}`,
|
|
160
|
+
],
|
|
161
|
+
keyStatus: "unknown",
|
|
162
|
+
keyWarning: null,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
return check(key);
|
|
166
|
+
}
|
|
167
|
+
// No key passed: resolve it from the public directory by key_id.
|
|
168
|
+
const baseUrl = publicKey?.fetchFrom ?? DEFAULT_BASE_URL;
|
|
169
|
+
const fetchFn = publicKey?.fetchFn ?? fetch;
|
|
170
|
+
if (!keyId) {
|
|
171
|
+
return Promise.resolve({
|
|
172
|
+
valid: false,
|
|
173
|
+
hashValid: false,
|
|
174
|
+
signatureValid: false,
|
|
175
|
+
receiptHash: typeof env.receipt_hash === "string" ? env.receipt_hash : "",
|
|
176
|
+
recomputedHash: "",
|
|
177
|
+
keyId,
|
|
178
|
+
reasons: ["envelope has no signature key_id to resolve"],
|
|
179
|
+
keyStatus: "unknown",
|
|
180
|
+
keyWarning: null,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
return fetchPublicKey(keyId, baseUrl, fetchFn).then((entry) => check(parsePublicKey(entry.public_key), {
|
|
184
|
+
status: entry.status,
|
|
185
|
+
revokedAt: entry.revoked_at,
|
|
186
|
+
}), (err) => ({
|
|
187
|
+
valid: false,
|
|
188
|
+
hashValid: false,
|
|
189
|
+
signatureValid: false,
|
|
190
|
+
receiptHash: typeof env.receipt_hash === "string" ? env.receipt_hash : "",
|
|
191
|
+
recomputedHash: "",
|
|
192
|
+
keyId,
|
|
193
|
+
reasons: [
|
|
194
|
+
`could not fetch public key ${keyId}: ${err instanceof Error ? err.message : String(err)}`,
|
|
195
|
+
],
|
|
196
|
+
keyStatus: "unknown",
|
|
197
|
+
keyWarning: null,
|
|
198
|
+
}));
|
|
199
|
+
}
|
|
200
|
+
/** Recompute the content hashes from the original prompt/response +
|
|
201
|
+
* salts and compare them to the receipt's commitments. The text
|
|
202
|
+
* never leaves the caller's machine. `promptBody` is the exact
|
|
203
|
+
* object the prompt commitment hashes: {messages, tools?,
|
|
204
|
+
* response_format?} — only the keys present in the request. */
|
|
205
|
+
export function verifyReceiptContent(receipt, salts, promptBody, responseJson) {
|
|
206
|
+
try {
|
|
207
|
+
return verifyContentCrypto(receipt, salts, promptBody, responseJson);
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/** Verify a Merkle inclusion proof from an envelope's anchor block.
|
|
214
|
+
* `proof` steps carry "sha256:<hex>" hashes (the envelope form);
|
|
215
|
+
* `leafHash` and `root` accept the same tag form. */
|
|
216
|
+
export function verifyAnchorProof(leafHash, proof, root) {
|
|
217
|
+
try {
|
|
218
|
+
const steps = proof.map((step) => ({
|
|
219
|
+
side: step.side,
|
|
220
|
+
hash: hashBytesFromTag(step.hash),
|
|
221
|
+
}));
|
|
222
|
+
return verifyMerkleProofCrypto(hashBytesFromTag(leafHash), steps, hashBytesFromTag(root));
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/** The base64 form of a public key (handy for logging/comparing). */
|
|
229
|
+
export function publicKeyToBase64(key) {
|
|
230
|
+
return bytesToBase64(key);
|
|
231
|
+
}
|
|
232
|
+
//# sourceMappingURL=verify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify.js","sourceRoot":"","sources":["../src/verify.ts"],"names":[],"mappings":"AAAA;;;;;;;;8BAQ8B;AAC9B,OAAO,EACL,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,UAAU,EACV,WAAW,EACX,iBAAiB,IAAI,uBAAuB,EAC5C,oBAAoB,IAAI,mBAAmB,EAC3C,sBAAsB,GAEvB,MAAM,aAAa,CAAC;AAWrB,MAAM,CAAC,MAAM,gBAAgB,GAAG,2BAA2B,CAAC;AAE5D,gDAAgD;AAChD,MAAM,UAAU,YAAY,CAC1B,SAAiB,EACjB,UAAkB,gB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