@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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Cinian Studios Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,185 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @chain-receipt/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
JavaScript / TypeScript client for [Chain Receipt](https://chainreceipt.link) — verified AI output receipts. Every AI call through the Chain Receipt proxy returns a signed receipt; this SDK fetches receipts, parses them out of proxy responses, and verifies them **offline** (hash recompute + Ed25519 signature, no network).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @chain-receipt/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires Node 18+ (global `fetch`) or any modern browser. No runtime dependencies beyond `@noble/ed25519`, `@noble/hashes`, and `canonicalize` — no `Buffer`, no `node:crypto`, so the offline verify path runs in browsers as well as Node.
|
|
12
|
+
|
|
13
|
+
## Quickstart
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { ChainReceiptClient } from "@chain-receipt/sdk";
|
|
17
|
+
|
|
18
|
+
const client = new ChainReceiptClient({ apiKey: "crk_test_..." });
|
|
19
|
+
|
|
20
|
+
// 1. Call the proxy — the receipt comes back attached to the answer.
|
|
21
|
+
const { data, receipt } = await client.chatCompletions({
|
|
22
|
+
model: "openai/gpt-6-luna",
|
|
23
|
+
messages: [{ role: "user", content: "Say the word receipt." }],
|
|
24
|
+
max_tokens: 20,
|
|
25
|
+
});
|
|
26
|
+
// data.choices[0].message.content === "receipt"
|
|
27
|
+
// receipt.receipt_id, receipt.receipt_hash, receipt.salts, ...
|
|
28
|
+
|
|
29
|
+
// 2. Fetch the full envelope later.
|
|
30
|
+
const envelope = await client.getReceipt(receipt.receipt_id);
|
|
31
|
+
|
|
32
|
+
// 3. Verify it offline. The public key is fetched once from the
|
|
33
|
+
// public key directory; the crypto itself runs locally.
|
|
34
|
+
const result = await client.verifyReceipt(envelope);
|
|
35
|
+
console.log(result.valid); // true
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
If you already call the proxy with your own `fetch` (or with the OpenAI client pointed at the Chain Receipt base URL), pull the receipt out of the response yourself:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { receiptFromResponse, receiptFromSseEvent } from "@chain-receipt/sdk";
|
|
42
|
+
|
|
43
|
+
// Non-streaming: the response JSON carries ai_receipt at the top level.
|
|
44
|
+
const receipt = receiptFromResponse(await res.json());
|
|
45
|
+
|
|
46
|
+
// Streaming: each SSE `data:` line — the ai_receipt event arrives
|
|
47
|
+
// just before [DONE].
|
|
48
|
+
for (const line of sseLines) {
|
|
49
|
+
const receipt = receiptFromSseEvent(line);
|
|
50
|
+
if (receipt) console.log("receipt:", receipt.receipt_id);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Verify offline, with a key you already hold
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { verifyReceipt } from "@chain-receipt/sdk";
|
|
58
|
+
|
|
59
|
+
// Public key as base64 (the form in /.well-known/receipt-keys.json),
|
|
60
|
+
// 64-char hex, or raw 32 bytes.
|
|
61
|
+
const result = verifyReceipt(envelope, "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a");
|
|
62
|
+
// { valid: true, hashValid: true, signatureValid: true,
|
|
63
|
+
// receiptHash, recomputedHash, keyId, reasons: [] }
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`verifyReceipt` never throws on a bad receipt, a bad signature, or a bad key — those come back as `valid: false` with `reasons`. It throws only when the envelope has no receipt object at all.
|
|
67
|
+
|
|
68
|
+
## Prove the content, not just the signature
|
|
69
|
+
|
|
70
|
+
The receipt holds salted hashes of your prompt and the provider's response, not the text itself. Recompute them locally — the text never leaves your machine:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { verifyReceiptContent } from "@chain-receipt/sdk";
|
|
74
|
+
|
|
75
|
+
// salts came back once in ai_receipt.salts — keep them with the receipt.
|
|
76
|
+
const ok = verifyReceiptContent(
|
|
77
|
+
envelope.receipt,
|
|
78
|
+
receipt.salts,
|
|
79
|
+
{ messages: [{ role: "user", content: "Say the word receipt." }] },
|
|
80
|
+
providerResponseJson,
|
|
81
|
+
);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
The prompt body is the exact object the commitment hashes: `{messages, tools?, response_format?}` — only the keys present in the request.
|
|
85
|
+
|
|
86
|
+
## List receipts
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
const page = await client.listReceipts({ status: "complete", limit: 50 });
|
|
90
|
+
for (const row of page.data) {
|
|
91
|
+
console.log(row.receipt_id, row.anchor_status, row.verify_url);
|
|
92
|
+
}
|
|
93
|
+
if (page.has_more) {
|
|
94
|
+
const next = await client.listReceipts({ cursor: page.next_cursor });
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Download a public bundle
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
// Public receipts only — no API key needed. A private receipt
|
|
102
|
+
// answers 404 here; use client.getReceipt for your own receipts.
|
|
103
|
+
const envelope = await client.downloadBundle("rcpt_...");
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Receipt any JSON directly (no AI call)
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
// The commitment hashes the JSON as sent; a non-object is wrapped
|
|
110
|
+
// as { value: data }.
|
|
111
|
+
const { receipt } = await client.createDirectReceipt(
|
|
112
|
+
{ order: "ORD-1", total: 42.5, currency: "CAD" },
|
|
113
|
+
{ customerRef: "ORD-1" },
|
|
114
|
+
);
|
|
115
|
+
// receipt.receipt_id, receipt.verify_url, receipt.receipt_hash
|
|
116
|
+
|
|
117
|
+
// Permission Slips — data is { checkout_mandate, payment_mandate }.
|
|
118
|
+
// A paid add-on (Developer +$15/month, Team +$49/month; included
|
|
119
|
+
// on Business/Enterprise). A duplicate slip answers 200 with
|
|
120
|
+
// duplicate: true and no new receipt.
|
|
121
|
+
const slipResult = await client.createDirectReceipt(
|
|
122
|
+
{ checkout_mandate: {...}, payment_mandate: {...} },
|
|
123
|
+
{ recordKind: "permission_slip" },
|
|
124
|
+
);
|
|
125
|
+
const slip = slipResult.slip; // { slip_id, status: "active", ... }
|
|
126
|
+
|
|
127
|
+
// A slip's public status — no API key is sent. Status only, never
|
|
128
|
+
// the mandate content.
|
|
129
|
+
const status = await client.getSlip("slip_...");
|
|
130
|
+
console.log(status.status); // "active" | "cancelled" | "expired"
|
|
131
|
+
|
|
132
|
+
// Revoke a slip. Owner: the org's API key (default). Delegate: the
|
|
133
|
+
// agent whose key the slip's cnf binds — pass its Ed25519 signature
|
|
134
|
+
// (base64, over the canonical JSON of { slip_id, slip_hash,
|
|
135
|
+
// cancelled_at }) with cancelledAt; no API key is sent.
|
|
136
|
+
await client.cancelSlip("slip_...", { cancelledAt: "2026-11-26T12:00:00.000Z" });
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Server-side verification (Merkle + Bitcoin)
|
|
140
|
+
|
|
141
|
+
The offline check covers hash + signature. The server also checks the Merkle inclusion proof and the Bitcoin anchor state:
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
const server = await client.verifyOnline({ envelope });
|
|
145
|
+
// { valid, checks: { hash, signature, content, merkle, bitcoin },
|
|
146
|
+
// anchor: { status: "confirmed" | "pending" | ... } }
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## React badge
|
|
150
|
+
|
|
151
|
+
```tsx
|
|
152
|
+
import { ReceiptBadge } from "@chain-receipt/sdk/badge";
|
|
153
|
+
|
|
154
|
+
<ReceiptBadge receiptId="rcpt_..." />
|
|
155
|
+
// <a href="https://chainreceipt.link/verify/rcpt_...">✓ Verified by Chain Receipt</a>
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
React is an optional peer dependency (`>=16.8`) — importing the client alone never pulls React in. The badge renders a plain link with no imposed styles; pass `className`, `label`, or `verifyUrl` to control it.
|
|
159
|
+
|
|
160
|
+
## API
|
|
161
|
+
|
|
162
|
+
| Export | What it does |
|
|
163
|
+
| --- | --- |
|
|
164
|
+
| `ChainReceiptClient` | `getReceipt`, `listReceipts`, `downloadBundle`, `chatCompletions`, `verifyReceipt`, `verifyOnline`, `getPublicKey`, `getVerifyUrl`, `createDirectReceipt`, `getSlip`, `cancelSlip` |
|
|
165
|
+
| `receiptFromResponse` | `ai_receipt` block out of a proxy response JSON (or SSE event object) |
|
|
166
|
+
| `receiptFromSseEvent` | Receipt out of one SSE `data:` line |
|
|
167
|
+
| `verifyReceipt` | Offline hash + Ed25519 check (sync with a key, async resolving the key) |
|
|
168
|
+
| `verifyReceiptContent` | Salted prompt/response re-hash check |
|
|
169
|
+
| `verifyAnchorProof` | Merkle inclusion proof from an envelope's anchor block |
|
|
170
|
+
| `fetchPublicKey` | One entry of `/.well-known/receipt-keys.json` |
|
|
171
|
+
| `getVerifyUrl` | Public verify-page URL for a receipt |
|
|
172
|
+
| `ReceiptBadge` | React link to the verify page (`@chain-receipt/sdk/badge`) |
|
|
173
|
+
| Crypto utilities | `canonicalJson`, `receiptHash`, `signReceipt`, `verifyReceiptSignature`, `merkleRoot`, `merkleProof`, `verifyMerkleProof`, `saltedPromptHash`, `saltedResponseHash`, hex/base64 helpers |
|
|
174
|
+
|
|
175
|
+
Errors are `ChainReceiptError` with `.status` (HTTP status) and `.code` (API error code) when available.
|
|
176
|
+
|
|
177
|
+
## Correctness
|
|
178
|
+
|
|
179
|
+
The SDK's crypto is proven against the shared golden vectors in `packages/core/test-vectors/golden.json`: JCS (RFC 8785 §3.2.3), Ed25519 (RFC 8032 test 1), SHA-256 (FIPS 180-4), and the locked Merkle root (leaf `sha256(0x00||hash)`, node `sha256(0x01||L||R)`). Run them with `pnpm test`. Tamper tests flip one byte in the receipt, the signature, the prompt, and the Merkle proof — every one fails verification.
|
|
180
|
+
|
|
181
|
+
## Notes
|
|
182
|
+
|
|
183
|
+
- The SDK never stores or sends provider keys. A provider key is passed per chat call (`X-Provider-Key`) or saved on the portal Keys page.
|
|
184
|
+
- Receipts are hash-only by default: the envelope contains hashes, never your prompt or the answer. Keep the `salts` from `ai_receipt` — without them the content check cannot run, and they are returned exactly once.
|
|
185
|
+
- A genuine receipt is valid before its batch confirms on Bitcoin; `anchor.status` (`pending` → `submitted` → `confirmed`) is reported separately from `valid`.
|
package/dist/badge.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
export interface ReceiptBadgeProps {
|
|
3
|
+
/** The receipt id (rcpt_...). */
|
|
4
|
+
receiptId: string;
|
|
5
|
+
/** Full verify URL. Defaults to {baseUrl}/verify/{receiptId}. */
|
|
6
|
+
verifyUrl?: string;
|
|
7
|
+
/** Site base URL. Defaults to https://chainreceipt.link. */
|
|
8
|
+
baseUrl?: string;
|
|
9
|
+
/** Link text. Defaults to "Verified by Chain Receipt". */
|
|
10
|
+
label?: ReactNode;
|
|
11
|
+
/** CSS class for the <a>. */
|
|
12
|
+
className?: string;
|
|
13
|
+
/** Where the link opens. Defaults to a new tab. */
|
|
14
|
+
target?: "_blank" | "_self";
|
|
15
|
+
}
|
|
16
|
+
export declare function ReceiptBadge({ receiptId, verifyUrl, baseUrl, label, className, target, }: ReceiptBadgeProps): import("react").JSX.Element;
|
|
17
|
+
//# sourceMappingURL=badge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"badge.d.ts","sourceRoot":"","sources":["../src/badge.tsx"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAEvC,MAAM,WAAW,iBAAiB;IAChC,iCAAiC;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,iEAAiE;IACjE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,4DAA4D;IAC5D,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,0DAA0D;IAC1D,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,6BAA6B;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,mDAAmD;IACnD,MAAM,CAAC,EAAE,QAAQ,GAAG,OAAO,CAAC;CAC7B;AAED,wBAAgB,YAAY,CAAC,EAC3B,SAAS,EACT,SAAS,EACT,OAAqC,EACrC,KAAmC,EACnC,SAAS,EACT,MAAiB,GAClB,EAAE,iBAAiB,+BAcnB"}
|
package/dist/badge.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
export function ReceiptBadge({ receiptId, verifyUrl, baseUrl = "https://chainreceipt.link", label = "Verified by Chain Receipt", className, target = "_blank", }) {
|
|
3
|
+
const href = verifyUrl ?? `${baseUrl.replace(/\/$/, "")}/verify/${receiptId}`;
|
|
4
|
+
return (_jsxs("a", { href: href, className: className, target: target, rel: target === "_blank" ? "noopener noreferrer" : undefined, "data-chain-receipt-badge": receiptId, children: ["\u2713 ", label] }));
|
|
5
|
+
}
|
|
6
|
+
//# sourceMappingURL=badge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"badge.js","sourceRoot":"","sources":["../src/badge.tsx"],"names":[],"mappings":";AA2BA,MAAM,UAAU,YAAY,CAAC,EAC3B,SAAS,EACT,SAAS,EACT,OAAO,GAAG,2BAA2B,EACrC,KAAK,GAAG,2BAA2B,EACnC,SAAS,EACT,MAAM,GAAG,QAAQ,GACC;IAClB,MAAM,IAAI,GACR,SAAS,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,WAAW,SAAS,EAAE,CAAC;IACnE,OAAO,CACL,aACE,IAAI,EAAE,IAAI,EACV,SAAS,EAAE,SAAS,EACpB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,SAAS,8BAClC,SAAS,wBAEhC,KAAK,IACN,CACL,CAAC;AACJ,CAAC"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { fetchPublicKey, getVerifyUrl, verifyReceipt } from "./verify.js";
|
|
2
|
+
import type { AiReceiptSummary, CancelSlipResult, DirectReceiptResult, PublicKeyEntry, ReceiptEnvelope, ReceiptListPage, RecordKind, ServerVerifyResult, SlipStatusView, VerifyResult } from "./types.js";
|
|
3
|
+
export { fetchPublicKey, getVerifyUrl, verifyReceipt };
|
|
4
|
+
/** Error from the Chain Receipt API (or from the network layer).
|
|
5
|
+
* `status` is the HTTP status when one was received; `code` is the
|
|
6
|
+
* API's error code when the body carried one. */
|
|
7
|
+
export declare class ChainReceiptError extends Error {
|
|
8
|
+
status: number | null;
|
|
9
|
+
code: string | null;
|
|
10
|
+
constructor(message: string, status?: number | null, code?: string | null);
|
|
11
|
+
/** Build an error from a failed fetch Response, reading the
|
|
12
|
+
* OpenAI-shaped {error: {message, code}} body when present. */
|
|
13
|
+
static fromResponse(res: Response, path: string): Promise<ChainReceiptError>;
|
|
14
|
+
}
|
|
15
|
+
export interface ChainReceiptClientOptions {
|
|
16
|
+
/** Chain Receipt API key (crk_live_... or crk_test_...). */
|
|
17
|
+
apiKey: string;
|
|
18
|
+
/** API base URL. Defaults to https://chainreceipt.link. */
|
|
19
|
+
baseUrl?: string;
|
|
20
|
+
/** fetch implementation (defaults to the global fetch). Inject a
|
|
21
|
+
* mock in tests or a custom implementation in non-DOM runtimes. */
|
|
22
|
+
fetch?: typeof fetch;
|
|
23
|
+
}
|
|
24
|
+
export interface ListReceiptsParams {
|
|
25
|
+
/** ISO dates. */
|
|
26
|
+
from?: string;
|
|
27
|
+
to?: string;
|
|
28
|
+
/** API key id (key_...) to filter by. */
|
|
29
|
+
apiKeyId?: string;
|
|
30
|
+
status?: "complete" | "incomplete" | "upstream_error";
|
|
31
|
+
/** customer_ref to filter by. */
|
|
32
|
+
ref?: string;
|
|
33
|
+
/** 1–100, default 50. */
|
|
34
|
+
limit?: number;
|
|
35
|
+
/** The seq of the last row seen (from a previous page's
|
|
36
|
+
* next_cursor). */
|
|
37
|
+
cursor?: string;
|
|
38
|
+
}
|
|
39
|
+
export interface ChatCompletionsOptions {
|
|
40
|
+
/** Provider key for this one call (X-Provider-Key). Omit when the
|
|
41
|
+
* org has a provider key saved on the portal Keys page. */
|
|
42
|
+
providerKey?: string;
|
|
43
|
+
signal?: AbortSignal;
|
|
44
|
+
}
|
|
45
|
+
export interface ChatCompletionsResult {
|
|
46
|
+
/** The provider's response JSON, exactly as returned. */
|
|
47
|
+
data: Record<string, unknown>;
|
|
48
|
+
/** The attached receipt summary, or null when the proxy could not
|
|
49
|
+
* sign/store one (the answer is still returned in `data`). */
|
|
50
|
+
receipt: AiReceiptSummary | null;
|
|
51
|
+
}
|
|
52
|
+
export interface CreateDirectReceiptOptions {
|
|
53
|
+
/** "generic" (default) receipts any JSON as sent;
|
|
54
|
+
* "permission_slip" issues a Permission Slip — `data` must then
|
|
55
|
+
* be {checkout_mandate, payment_mandate}. */
|
|
56
|
+
recordKind?: RecordKind;
|
|
57
|
+
/** Your own reference, returned on the receipt (max 128 chars). */
|
|
58
|
+
customerRef?: string;
|
|
59
|
+
}
|
|
60
|
+
export interface CancelSlipOptions {
|
|
61
|
+
/** ISO 8601 cancellation time (defaults to now server-side).
|
|
62
|
+
* Required on the delegate path (with `signature`) — it is part
|
|
63
|
+
* of the signed payload. Must not be more than 5 minutes in the
|
|
64
|
+
* future. */
|
|
65
|
+
cancelledAt?: string;
|
|
66
|
+
/** The agent's Ed25519 signature (base64) over the canonical JSON
|
|
67
|
+
* of {slip_id, slip_hash, cancelled_at} — the delegate path, for
|
|
68
|
+
* the agent whose key the slip's cnf binds. When set, no API key
|
|
69
|
+
* is sent: the cancel is authenticated by the signature alone.
|
|
70
|
+
* Omit it for the owner path (the org's API key). */
|
|
71
|
+
signature?: string;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Parse the `ai_receipt` block out of a proxy chat-completion
|
|
75
|
+
* response. Handles both shapes:
|
|
76
|
+
* - non-streaming: the response JSON carries `ai_receipt` at the top
|
|
77
|
+
* level;
|
|
78
|
+
* - streaming: the `ai_receipt` SSE event, parsed to
|
|
79
|
+
* `{object: "ai_receipt", ai_receipt: {...}}`.
|
|
80
|
+
* Returns null when the response carries no receipt. */
|
|
81
|
+
export declare function receiptFromResponse(response: unknown): AiReceiptSummary | null;
|
|
82
|
+
/** Parse one SSE `data:` line from a streamed proxy response and
|
|
83
|
+
* return its receipt when it is the `ai_receipt` event. Returns
|
|
84
|
+
* null for token events, `[DONE]`, and anything unparseable. */
|
|
85
|
+
export declare function receiptFromSseEvent(dataLine: string): AiReceiptSummary | null;
|
|
86
|
+
export declare class ChainReceiptClient {
|
|
87
|
+
readonly apiKey: string;
|
|
88
|
+
readonly baseUrl: string;
|
|
89
|
+
private readonly fetchFn;
|
|
90
|
+
constructor(options: ChainReceiptClientOptions);
|
|
91
|
+
private request;
|
|
92
|
+
/** The public verify-page URL for a receipt. */
|
|
93
|
+
getVerifyUrl(receiptId: string): string;
|
|
94
|
+
/** Full air/1 envelope for one receipt (GET /v1/receipts/:id).
|
|
95
|
+
* A receipt from another org answers the same 404 as a missing
|
|
96
|
+
* one. */
|
|
97
|
+
getReceipt(receiptId: string): Promise<ReceiptEnvelope>;
|
|
98
|
+
/** The org's receipts, newest first (GET /v1/receipts). */
|
|
99
|
+
listReceipts(params?: ListReceiptsParams): Promise<ReceiptListPage>;
|
|
100
|
+
/** Download a receipt's public bundle (GET /verify/:id/download).
|
|
101
|
+
* No API key needed — but only public receipts can be downloaded
|
|
102
|
+
* this way; a private receipt answers 404, so use getReceipt for
|
|
103
|
+
* your own receipts instead. */
|
|
104
|
+
downloadBundle(receiptId: string): Promise<ReceiptEnvelope>;
|
|
105
|
+
/** The published signing key for a key_id
|
|
106
|
+
* (GET /.well-known/receipt-keys.json). Public, no auth. */
|
|
107
|
+
getPublicKey(keyId: string): Promise<PublicKeyEntry>;
|
|
108
|
+
/** Verify a receipt envelope offline — hash recompute + Ed25519
|
|
109
|
+
* signature, no network when the public key is passed directly.
|
|
110
|
+
* Pass the key (hex/base64/bytes), or omit it to resolve it by
|
|
111
|
+
* the envelope's key_id from the public key directory (one fetch).
|
|
112
|
+
* Accepts an envelope from getReceipt / downloadBundle, or the
|
|
113
|
+
* receipt + hash + signature rebuilt from an ai_receipt summary. */
|
|
114
|
+
verifyReceipt(envelope: ReceiptEnvelope, publicKey?: string | Uint8Array): VerifyResult | Promise<VerifyResult>;
|
|
115
|
+
/** Server-side verification (POST /v1/verify). Public — no API
|
|
116
|
+
* key is sent. Accepts either a full envelope or a receipt id
|
|
117
|
+
* (plus optional prompt/response + salts for the content check).
|
|
118
|
+
* The server also reports the Merkle + Bitcoin anchor checks,
|
|
119
|
+
* which the offline path cannot. */
|
|
120
|
+
verifyOnline(input: {
|
|
121
|
+
envelope: ReceiptEnvelope;
|
|
122
|
+
} | {
|
|
123
|
+
receiptId: string;
|
|
124
|
+
prompt?: unknown;
|
|
125
|
+
response?: unknown;
|
|
126
|
+
salts?: {
|
|
127
|
+
prompt: string;
|
|
128
|
+
response: string;
|
|
129
|
+
};
|
|
130
|
+
}): Promise<ServerVerifyResult>;
|
|
131
|
+
/** Receipt any JSON directly, without calling an AI provider
|
|
132
|
+
* (POST /v1/receipts). The commitment hashes `data` as sent; a
|
|
133
|
+
* non-object is wrapped as {value: data}. Returns 201 with the
|
|
134
|
+
* signed receipt summary (and the slip, for
|
|
135
|
+
* recordKind "permission_slip" — data must then be
|
|
136
|
+
* {checkout_mandate, payment_mandate}). A duplicate slip answers
|
|
137
|
+
* 200 with `duplicate: true` and no new receipt. */
|
|
138
|
+
createDirectReceipt(data: unknown, options?: CreateDirectReceiptOptions): Promise<DirectReceiptResult>;
|
|
139
|
+
/** A slip's public status (GET /v1/slips/:id). Public — no API
|
|
140
|
+
* key is sent. Status only (active / cancelled / expired), never
|
|
141
|
+
* the mandate content. */
|
|
142
|
+
getSlip(slipId: string): Promise<SlipStatusView>;
|
|
143
|
+
/** Revoke a slip (POST /v1/slips/:id/cancel). Two callers:
|
|
144
|
+
* - Owner (default): the org's API key authenticates; pass
|
|
145
|
+
* `cancelledAt` to backdate the cancellation (max 5 minutes
|
|
146
|
+
* into the future), or omit it for now.
|
|
147
|
+
* - Delegate: pass `signature` (base64 Ed25519 over the canonical
|
|
148
|
+
* JSON of {slip_id, slip_hash, cancelled_at}) with
|
|
149
|
+
* `cancelledAt` required — no API key is sent. Revoking an
|
|
150
|
+
* already-cancelled slip returns the existing cancellation
|
|
151
|
+
* (idempotent, 200). */
|
|
152
|
+
cancelSlip(slipId: string, options?: CancelSlipOptions): Promise<CancelSlipResult>;
|
|
153
|
+
/** Call the OpenAI-compatible proxy (POST
|
|
154
|
+
* /api/v1/chat/completions). The request body is OpenAI-shaped
|
|
155
|
+
* ({model: "openai/gpt-...", messages, ...}); the response is the
|
|
156
|
+
* provider's JSON plus the attached receipt, parsed out for you.
|
|
157
|
+
* Streaming is not handled here — pass stream: true and this
|
|
158
|
+
* throws; stream with your own fetch and pull the receipt from
|
|
159
|
+
* the `ai_receipt` SSE event with receiptFromSseEvent. */
|
|
160
|
+
chatCompletions(body: Record<string, unknown>, options?: ChatCompletionsOptions): Promise<ChatCompletionsResult>;
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file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