@xproof/xproof 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/LICENSE +21 -0
- package/README.md +138 -0
- package/dist/client-DOH3QHZm.d.mts +93 -0
- package/dist/client-DOH3QHZm.d.ts +93 -0
- package/dist/index.d.mts +32 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +350 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +313 -0
- package/dist/index.mjs.map +1 -0
- package/dist/integrations/vercel.d.mts +112 -0
- package/dist/integrations/vercel.d.ts +112 -0
- package/dist/integrations/vercel.js +559 -0
- package/dist/integrations/vercel.js.map +1 -0
- package/dist/integrations/vercel.mjs +534 -0
- package/dist/integrations/vercel.mjs.map +1 -0
- package/package.json +66 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
import { basename } from "path";
|
|
3
|
+
|
|
4
|
+
// src/errors.ts
|
|
5
|
+
var XProofError = class extends Error {
|
|
6
|
+
constructor(message, statusCode = 0, response = null) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "XProofError";
|
|
9
|
+
this.statusCode = statusCode;
|
|
10
|
+
this.response = response;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
var AuthenticationError = class extends XProofError {
|
|
14
|
+
constructor(message = "Invalid or missing API key", response = null) {
|
|
15
|
+
super(message, 401, response);
|
|
16
|
+
this.name = "AuthenticationError";
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var ValidationError = class extends XProofError {
|
|
20
|
+
constructor(message = "Invalid request data", response = null) {
|
|
21
|
+
super(message, 400, response);
|
|
22
|
+
this.name = "ValidationError";
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
var NotFoundError = class extends XProofError {
|
|
26
|
+
constructor(message = "Resource not found", response = null) {
|
|
27
|
+
super(message, 404, response);
|
|
28
|
+
this.name = "NotFoundError";
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
var ConflictError = class extends XProofError {
|
|
32
|
+
constructor(message = "File already certified", certificationId = "", response = null) {
|
|
33
|
+
super(message, 409, response);
|
|
34
|
+
this.name = "ConflictError";
|
|
35
|
+
this.certificationId = certificationId;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var RateLimitError = class extends XProofError {
|
|
39
|
+
constructor(message = "Rate limit exceeded", response = null) {
|
|
40
|
+
super(message, 429, response);
|
|
41
|
+
this.name = "RateLimitError";
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var ServerError = class extends XProofError {
|
|
45
|
+
constructor(message = "Internal server error", statusCode = 500, response = null) {
|
|
46
|
+
super(message, statusCode, response);
|
|
47
|
+
this.name = "ServerError";
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// src/hash.ts
|
|
52
|
+
import { createHash } from "crypto";
|
|
53
|
+
import { readFile } from "fs/promises";
|
|
54
|
+
async function hashFile(path) {
|
|
55
|
+
const data = await readFile(path);
|
|
56
|
+
return createHash("sha256").update(data).digest("hex");
|
|
57
|
+
}
|
|
58
|
+
function hashBuffer(data) {
|
|
59
|
+
return createHash("sha256").update(data).digest("hex");
|
|
60
|
+
}
|
|
61
|
+
function hashString(data) {
|
|
62
|
+
return createHash("sha256").update(data, "utf8").digest("hex");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/parse.ts
|
|
66
|
+
function parseCertification(data) {
|
|
67
|
+
const blockchain = data.blockchain || {};
|
|
68
|
+
return {
|
|
69
|
+
id: data.id || data.proof_id || "",
|
|
70
|
+
fileName: data.fileName || data.filename || data.file_name || "",
|
|
71
|
+
fileHash: data.fileHash || data.file_hash || "",
|
|
72
|
+
transactionHash: data.transactionHash || blockchain.transaction_hash || "",
|
|
73
|
+
transactionUrl: data.transactionUrl || blockchain.explorer_url || "",
|
|
74
|
+
createdAt: data.createdAt || data.created_at || data.timestamp || "",
|
|
75
|
+
authorName: data.authorName || data.author_name || "",
|
|
76
|
+
blockchainStatus: data.blockchainStatus || data.status || "",
|
|
77
|
+
isPublic: data.isPublic !== void 0 ? !!data.isPublic : data.is_public !== void 0 ? !!data.is_public : true,
|
|
78
|
+
certificateUrl: data.certificateUrl || data.certificate_url || "",
|
|
79
|
+
verifyUrl: data.verifyUrl || data.verify_url || ""
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function parseBatchResult(data) {
|
|
83
|
+
const rawResults = data.results || [];
|
|
84
|
+
const results = rawResults.map(parseCertification);
|
|
85
|
+
const total = data.total ?? rawResults.length;
|
|
86
|
+
const created = data.created ?? 0;
|
|
87
|
+
const existing = data.existing ?? 0;
|
|
88
|
+
const summary = {
|
|
89
|
+
total,
|
|
90
|
+
created,
|
|
91
|
+
existing,
|
|
92
|
+
get certified() {
|
|
93
|
+
return created;
|
|
94
|
+
},
|
|
95
|
+
get failed() {
|
|
96
|
+
return total - created - existing;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
return {
|
|
100
|
+
batchId: data.batch_id || "",
|
|
101
|
+
results,
|
|
102
|
+
summary
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function parsePricingInfo(data) {
|
|
106
|
+
const rawTiers = data.tiers || [];
|
|
107
|
+
const tiers = rawTiers.map((t) => ({
|
|
108
|
+
minCertifications: t.min_certifications ?? t.min ?? 0,
|
|
109
|
+
maxCertifications: t.max_certifications ?? t.max ?? null,
|
|
110
|
+
priceUsd: t.price_usd ?? t.price ?? 0
|
|
111
|
+
}));
|
|
112
|
+
return {
|
|
113
|
+
protocol: data.protocol || "",
|
|
114
|
+
version: data.version || "",
|
|
115
|
+
priceUsd: data.price_usd ?? data.current_price ?? 0,
|
|
116
|
+
tiers,
|
|
117
|
+
paymentMethods: data.payment_methods || [],
|
|
118
|
+
raw: data
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function parseRegistrationResult(data) {
|
|
122
|
+
const trialData = data.trial || {};
|
|
123
|
+
const trial = {
|
|
124
|
+
quota: trialData.quota ?? 0,
|
|
125
|
+
used: trialData.used ?? 0,
|
|
126
|
+
remaining: trialData.remaining ?? 0
|
|
127
|
+
};
|
|
128
|
+
return {
|
|
129
|
+
apiKey: data.api_key || "",
|
|
130
|
+
agentName: data.agent_name || "",
|
|
131
|
+
trial,
|
|
132
|
+
endpoints: data.endpoints || {},
|
|
133
|
+
raw: data
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/client.ts
|
|
138
|
+
var VERSION = "0.1.0";
|
|
139
|
+
var DEFAULT_BASE_URL = "https://xproof.app";
|
|
140
|
+
var DEFAULT_TIMEOUT = 3e4;
|
|
141
|
+
var XProofClient = class _XProofClient {
|
|
142
|
+
constructor(options = {}) {
|
|
143
|
+
this.registration = null;
|
|
144
|
+
this.apiKey = options.apiKey ?? "";
|
|
145
|
+
this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
146
|
+
this.timeout = options.timeout ?? DEFAULT_TIMEOUT;
|
|
147
|
+
}
|
|
148
|
+
static async register(agentName, options = {}) {
|
|
149
|
+
const temp = new _XProofClient(options);
|
|
150
|
+
const data = await temp.request("POST", "/api/agent/register", {
|
|
151
|
+
body: { agent_name: agentName },
|
|
152
|
+
authRequired: false
|
|
153
|
+
});
|
|
154
|
+
const result = parseRegistrationResult(data);
|
|
155
|
+
const client = new _XProofClient({
|
|
156
|
+
...options,
|
|
157
|
+
apiKey: result.apiKey
|
|
158
|
+
});
|
|
159
|
+
client.registration = result;
|
|
160
|
+
return client;
|
|
161
|
+
}
|
|
162
|
+
async certify(path, author, fileName, fourW) {
|
|
163
|
+
this.requireAuth();
|
|
164
|
+
const fileHash = await hashFile(path);
|
|
165
|
+
const resolvedName = fileName ?? basename(path);
|
|
166
|
+
return this.certifyHash(fileHash, resolvedName, author, fourW);
|
|
167
|
+
}
|
|
168
|
+
async certifyHash(fileHash, fileName, author, fourW) {
|
|
169
|
+
this.requireAuth();
|
|
170
|
+
const metadata = fourW?.metadata ? { ...fourW.metadata } : {};
|
|
171
|
+
if (fourW?.who !== void 0) metadata.who = fourW.who;
|
|
172
|
+
if (fourW?.what !== void 0) metadata.what = fourW.what;
|
|
173
|
+
if (fourW?.when !== void 0) metadata.when = fourW.when;
|
|
174
|
+
if (fourW?.why !== void 0) metadata.why = fourW.why;
|
|
175
|
+
const payload = {
|
|
176
|
+
filename: fileName,
|
|
177
|
+
file_hash: fileHash,
|
|
178
|
+
author_name: author
|
|
179
|
+
};
|
|
180
|
+
if (Object.keys(metadata).length > 0) {
|
|
181
|
+
payload.metadata = metadata;
|
|
182
|
+
}
|
|
183
|
+
const data = await this.request("POST", "/api/proof", { body: payload });
|
|
184
|
+
return parseCertification(data);
|
|
185
|
+
}
|
|
186
|
+
async batchCertify(files) {
|
|
187
|
+
this.requireAuth();
|
|
188
|
+
if (files.length > 50) {
|
|
189
|
+
throw new Error("Batch certification supports a maximum of 50 files");
|
|
190
|
+
}
|
|
191
|
+
const entries = files.map((f) => {
|
|
192
|
+
const entry = {
|
|
193
|
+
filename: f.fileName ?? "unknown",
|
|
194
|
+
file_hash: f.fileHash
|
|
195
|
+
};
|
|
196
|
+
if (f.metadata) entry.metadata = f.metadata;
|
|
197
|
+
return entry;
|
|
198
|
+
});
|
|
199
|
+
const payload = { files: entries };
|
|
200
|
+
const author = files.find((f) => f.author)?.author;
|
|
201
|
+
if (author) payload.author_name = author;
|
|
202
|
+
const data = await this.request("POST", "/api/batch", { body: payload });
|
|
203
|
+
return parseBatchResult(data);
|
|
204
|
+
}
|
|
205
|
+
async verify(proofId) {
|
|
206
|
+
const data = await this.request("GET", `/api/proof/${proofId}`, {
|
|
207
|
+
authRequired: false
|
|
208
|
+
});
|
|
209
|
+
return parseCertification(data);
|
|
210
|
+
}
|
|
211
|
+
async verifyHash(fileHash) {
|
|
212
|
+
const data = await this.request("GET", `/api/proof/hash/${fileHash}`, {
|
|
213
|
+
authRequired: false
|
|
214
|
+
});
|
|
215
|
+
return parseCertification(data);
|
|
216
|
+
}
|
|
217
|
+
async getPricing() {
|
|
218
|
+
const data = await this.request("GET", "/api/pricing", {
|
|
219
|
+
authRequired: false
|
|
220
|
+
});
|
|
221
|
+
return parsePricingInfo(data);
|
|
222
|
+
}
|
|
223
|
+
requireAuth() {
|
|
224
|
+
if (!this.apiKey) {
|
|
225
|
+
throw new Error(
|
|
226
|
+
"apiKey is required \u2014 call XProofClient.register() or pass an apiKey"
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
async request(method, path, options = {}) {
|
|
231
|
+
const url = `${this.baseUrl}${path}`;
|
|
232
|
+
const { body, authRequired = true } = options;
|
|
233
|
+
const headers = {
|
|
234
|
+
"User-Agent": `xproof-js/${VERSION}`
|
|
235
|
+
};
|
|
236
|
+
if (authRequired && this.apiKey) {
|
|
237
|
+
headers["Authorization"] = `Bearer ${this.apiKey}`;
|
|
238
|
+
}
|
|
239
|
+
if (body) {
|
|
240
|
+
headers["Content-Type"] = "application/json";
|
|
241
|
+
}
|
|
242
|
+
const controller = new AbortController();
|
|
243
|
+
const timer = setTimeout(() => controller.abort(), this.timeout);
|
|
244
|
+
let resp;
|
|
245
|
+
try {
|
|
246
|
+
resp = await fetch(url, {
|
|
247
|
+
method,
|
|
248
|
+
headers,
|
|
249
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
250
|
+
signal: controller.signal
|
|
251
|
+
});
|
|
252
|
+
} catch (err) {
|
|
253
|
+
clearTimeout(timer);
|
|
254
|
+
if (err instanceof DOMException && err.name === "AbortError") {
|
|
255
|
+
throw new XProofError(`Request timed out after ${this.timeout}ms`);
|
|
256
|
+
}
|
|
257
|
+
throw new XProofError(`Request failed: ${err.message}`);
|
|
258
|
+
} finally {
|
|
259
|
+
clearTimeout(timer);
|
|
260
|
+
}
|
|
261
|
+
if (resp.status === 200 || resp.status === 201) {
|
|
262
|
+
let data;
|
|
263
|
+
try {
|
|
264
|
+
data = await resp.json();
|
|
265
|
+
} catch {
|
|
266
|
+
const text = await resp.text().catch(() => "");
|
|
267
|
+
throw new XProofError(
|
|
268
|
+
`Unexpected non-JSON response from ${method} ${url}: ${text.slice(0, 200)}`
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
return data;
|
|
272
|
+
}
|
|
273
|
+
await this.handleError(resp);
|
|
274
|
+
return {};
|
|
275
|
+
}
|
|
276
|
+
async handleError(resp) {
|
|
277
|
+
let body;
|
|
278
|
+
try {
|
|
279
|
+
body = await resp.json();
|
|
280
|
+
} catch {
|
|
281
|
+
body = { message: await resp.text().catch(() => "") };
|
|
282
|
+
}
|
|
283
|
+
const message = body.message || body.error || `HTTP ${resp.status}`;
|
|
284
|
+
const status = resp.status;
|
|
285
|
+
if (status === 400) throw new ValidationError(message, body);
|
|
286
|
+
if (status === 401 || status === 403)
|
|
287
|
+
throw new AuthenticationError(message, body);
|
|
288
|
+
if (status === 404) throw new NotFoundError(message, body);
|
|
289
|
+
if (status === 409)
|
|
290
|
+
throw new ConflictError(
|
|
291
|
+
message,
|
|
292
|
+
body.certificationId || "",
|
|
293
|
+
body
|
|
294
|
+
);
|
|
295
|
+
if (status === 429) throw new RateLimitError(message, body);
|
|
296
|
+
if (status >= 500) throw new ServerError(message, status, body);
|
|
297
|
+
throw new XProofError(message, status, body);
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
export {
|
|
301
|
+
AuthenticationError,
|
|
302
|
+
ConflictError,
|
|
303
|
+
NotFoundError,
|
|
304
|
+
RateLimitError,
|
|
305
|
+
ServerError,
|
|
306
|
+
ValidationError,
|
|
307
|
+
XProofClient,
|
|
308
|
+
XProofError,
|
|
309
|
+
hashBuffer,
|
|
310
|
+
hashFile,
|
|
311
|
+
hashString
|
|
312
|
+
};
|
|
313
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts","../src/errors.ts","../src/hash.ts","../src/parse.ts"],"sourcesContent":["import { basename } from \"path\";\nimport {\n XProofError,\n AuthenticationError,\n ValidationError,\n NotFoundError,\n ConflictError,\n RateLimitError,\n ServerError,\n} from \"./errors.js\";\nimport { hashFile } from \"./hash.js\";\nimport {\n parseCertification,\n parseBatchResult,\n parsePricingInfo,\n parseRegistrationResult,\n} from \"./parse.js\";\nimport type {\n Certification,\n BatchResult,\n PricingInfo,\n RegistrationResult,\n FourWOptions,\n BatchFileEntry,\n XProofClientOptions,\n} from \"./types.js\";\n\nconst VERSION = \"0.1.0\";\nconst DEFAULT_BASE_URL = \"https://xproof.app\";\nconst DEFAULT_TIMEOUT = 30_000;\n\nexport class XProofClient {\n private apiKey: string;\n private baseUrl: string;\n private timeout: number;\n\n registration: RegistrationResult | null = null;\n\n constructor(options: XProofClientOptions = {}) {\n this.apiKey = options.apiKey ?? \"\";\n this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\\/+$/, \"\");\n this.timeout = options.timeout ?? DEFAULT_TIMEOUT;\n }\n\n static async register(\n agentName: string,\n options: Omit<XProofClientOptions, \"apiKey\"> = {}\n ): Promise<XProofClient> {\n const temp = new XProofClient(options);\n const data = await temp.request(\"POST\", \"/api/agent/register\", {\n body: { agent_name: agentName },\n authRequired: false,\n });\n const result = parseRegistrationResult(data);\n const client = new XProofClient({\n ...options,\n apiKey: result.apiKey,\n });\n client.registration = result;\n return client;\n }\n\n async certify(\n path: string,\n author: string,\n fileName?: string,\n fourW?: FourWOptions\n ): Promise<Certification> {\n this.requireAuth();\n const fileHash = await hashFile(path);\n const resolvedName = fileName ?? basename(path);\n return this.certifyHash(fileHash, resolvedName, author, fourW);\n }\n\n async certifyHash(\n fileHash: string,\n fileName: string,\n author: string,\n fourW?: FourWOptions\n ): Promise<Certification> {\n this.requireAuth();\n\n const metadata: Record<string, unknown> = fourW?.metadata\n ? { ...fourW.metadata }\n : {};\n if (fourW?.who !== undefined) metadata.who = fourW.who;\n if (fourW?.what !== undefined) metadata.what = fourW.what;\n if (fourW?.when !== undefined) metadata.when = fourW.when;\n if (fourW?.why !== undefined) metadata.why = fourW.why;\n\n const payload: Record<string, unknown> = {\n filename: fileName,\n file_hash: fileHash,\n author_name: author,\n };\n if (Object.keys(metadata).length > 0) {\n payload.metadata = metadata;\n }\n\n const data = await this.request(\"POST\", \"/api/proof\", { body: payload });\n return parseCertification(data);\n }\n\n async batchCertify(files: BatchFileEntry[]): Promise<BatchResult> {\n this.requireAuth();\n\n if (files.length > 50) {\n throw new Error(\"Batch certification supports a maximum of 50 files\");\n }\n\n const entries = files.map((f) => {\n const entry: Record<string, unknown> = {\n filename: f.fileName ?? \"unknown\",\n file_hash: f.fileHash,\n };\n if (f.metadata) entry.metadata = f.metadata;\n return entry;\n });\n\n const payload: Record<string, unknown> = { files: entries };\n const author = files.find((f) => f.author)?.author;\n if (author) payload.author_name = author;\n\n const data = await this.request(\"POST\", \"/api/batch\", { body: payload });\n return parseBatchResult(data);\n }\n\n async verify(proofId: string): Promise<Certification> {\n const data = await this.request(\"GET\", `/api/proof/${proofId}`, {\n authRequired: false,\n });\n return parseCertification(data);\n }\n\n async verifyHash(fileHash: string): Promise<Certification> {\n const data = await this.request(\"GET\", `/api/proof/hash/${fileHash}`, {\n authRequired: false,\n });\n return parseCertification(data);\n }\n\n async getPricing(): Promise<PricingInfo> {\n const data = await this.request(\"GET\", \"/api/pricing\", {\n authRequired: false,\n });\n return parsePricingInfo(data);\n }\n\n private requireAuth(): void {\n if (!this.apiKey) {\n throw new Error(\n \"apiKey is required — call XProofClient.register() or pass an apiKey\"\n );\n }\n }\n\n private async request(\n method: string,\n path: string,\n options: {\n body?: Record<string, unknown>;\n authRequired?: boolean;\n } = {}\n ): Promise<Record<string, unknown>> {\n const url = `${this.baseUrl}${path}`;\n const { body, authRequired = true } = options;\n\n const headers: Record<string, string> = {\n \"User-Agent\": `xproof-js/${VERSION}`,\n };\n\n if (authRequired && this.apiKey) {\n headers[\"Authorization\"] = `Bearer ${this.apiKey}`;\n }\n\n if (body) {\n headers[\"Content-Type\"] = \"application/json\";\n }\n\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), this.timeout);\n\n let resp: Response;\n try {\n resp = await fetch(url, {\n method,\n headers,\n body: body ? JSON.stringify(body) : undefined,\n signal: controller.signal,\n });\n } catch (err) {\n clearTimeout(timer);\n if (err instanceof DOMException && err.name === \"AbortError\") {\n throw new XProofError(`Request timed out after ${this.timeout}ms`);\n }\n throw new XProofError(`Request failed: ${(err as Error).message}`);\n } finally {\n clearTimeout(timer);\n }\n\n if (resp.status === 200 || resp.status === 201) {\n let data: Record<string, unknown>;\n try {\n data = (await resp.json()) as Record<string, unknown>;\n } catch {\n const text = await resp.text().catch(() => \"\");\n throw new XProofError(\n `Unexpected non-JSON response from ${method} ${url}: ${text.slice(0, 200)}`\n );\n }\n return data;\n }\n\n await this.handleError(resp);\n return {};\n }\n\n private async handleError(resp: Response): Promise<never> {\n let body: Record<string, unknown>;\n try {\n body = (await resp.json()) as Record<string, unknown>;\n } catch {\n body = { message: await resp.text().catch(() => \"\") };\n }\n\n const message =\n (body.message as string) || (body.error as string) || `HTTP ${resp.status}`;\n const status = resp.status;\n\n if (status === 400) throw new ValidationError(message, body);\n if (status === 401 || status === 403)\n throw new AuthenticationError(message, body);\n if (status === 404) throw new NotFoundError(message, body);\n if (status === 409)\n throw new ConflictError(\n message,\n (body.certificationId as string) || \"\",\n body\n );\n if (status === 429) throw new RateLimitError(message, body);\n if (status >= 500) throw new ServerError(message, status, body);\n throw new XProofError(message, status, body);\n }\n}\n","export class XProofError extends Error {\n statusCode: number;\n response: Record<string, unknown> | null;\n\n constructor(\n message: string,\n statusCode = 0,\n response: Record<string, unknown> | null = null\n ) {\n super(message);\n this.name = \"XProofError\";\n this.statusCode = statusCode;\n this.response = response;\n }\n}\n\nexport class AuthenticationError extends XProofError {\n constructor(\n message = \"Invalid or missing API key\",\n response: Record<string, unknown> | null = null\n ) {\n super(message, 401, response);\n this.name = \"AuthenticationError\";\n }\n}\n\nexport class ValidationError extends XProofError {\n constructor(\n message = \"Invalid request data\",\n response: Record<string, unknown> | null = null\n ) {\n super(message, 400, response);\n this.name = \"ValidationError\";\n }\n}\n\nexport class NotFoundError extends XProofError {\n constructor(\n message = \"Resource not found\",\n response: Record<string, unknown> | null = null\n ) {\n super(message, 404, response);\n this.name = \"NotFoundError\";\n }\n}\n\nexport class ConflictError extends XProofError {\n certificationId: string;\n\n constructor(\n message = \"File already certified\",\n certificationId = \"\",\n response: Record<string, unknown> | null = null\n ) {\n super(message, 409, response);\n this.name = \"ConflictError\";\n this.certificationId = certificationId;\n }\n}\n\nexport class RateLimitError extends XProofError {\n constructor(\n message = \"Rate limit exceeded\",\n response: Record<string, unknown> | null = null\n ) {\n super(message, 429, response);\n this.name = \"RateLimitError\";\n }\n}\n\nexport class ServerError extends XProofError {\n constructor(\n message = \"Internal server error\",\n statusCode = 500,\n response: Record<string, unknown> | null = null\n ) {\n super(message, statusCode, response);\n this.name = \"ServerError\";\n }\n}\n","import { createHash } from \"crypto\";\nimport { readFile } from \"fs/promises\";\n\nexport async function hashFile(path: string): Promise<string> {\n const data = await readFile(path);\n return createHash(\"sha256\").update(data).digest(\"hex\");\n}\n\nexport function hashBuffer(data: Buffer | Uint8Array): string {\n return createHash(\"sha256\").update(data).digest(\"hex\");\n}\n\nexport function hashString(data: string): string {\n return createHash(\"sha256\").update(data, \"utf8\").digest(\"hex\");\n}\n","import type {\n Certification,\n BatchResult,\n BatchResultSummary,\n PricingInfo,\n PricingTier,\n RegistrationResult,\n TrialInfo,\n} from \"./types.js\";\n\nexport function parseCertification(data: Record<string, unknown>): Certification {\n const blockchain = (data.blockchain as Record<string, unknown>) || {};\n return {\n id: (data.id as string) || (data.proof_id as string) || \"\",\n fileName:\n (data.fileName as string) ||\n (data.filename as string) ||\n (data.file_name as string) ||\n \"\",\n fileHash:\n (data.fileHash as string) || (data.file_hash as string) || \"\",\n transactionHash:\n (data.transactionHash as string) ||\n (blockchain.transaction_hash as string) ||\n \"\",\n transactionUrl:\n (data.transactionUrl as string) ||\n (blockchain.explorer_url as string) ||\n \"\",\n createdAt:\n (data.createdAt as string) ||\n (data.created_at as string) ||\n (data.timestamp as string) ||\n \"\",\n authorName:\n (data.authorName as string) || (data.author_name as string) || \"\",\n blockchainStatus:\n (data.blockchainStatus as string) || (data.status as string) || \"\",\n isPublic: data.isPublic !== undefined ? !!data.isPublic : (data.is_public !== undefined ? !!data.is_public : true),\n certificateUrl:\n (data.certificateUrl as string) ||\n (data.certificate_url as string) ||\n \"\",\n verifyUrl:\n (data.verifyUrl as string) || (data.verify_url as string) || \"\",\n };\n}\n\nexport function parseBatchResult(data: Record<string, unknown>): BatchResult {\n const rawResults = (data.results as Array<Record<string, unknown>>) || [];\n const results = rawResults.map(parseCertification);\n const total = (data.total as number) ?? rawResults.length;\n const created = (data.created as number) ?? 0;\n const existing = (data.existing as number) ?? 0;\n\n const summary: BatchResultSummary = {\n total,\n created,\n existing,\n get certified() { return created; },\n get failed() { return total - created - existing; },\n };\n\n return {\n batchId: (data.batch_id as string) || \"\",\n results,\n summary,\n };\n}\n\nexport function parsePricingInfo(data: Record<string, unknown>): PricingInfo {\n const rawTiers = (data.tiers as Array<Record<string, unknown>>) || [];\n const tiers: PricingTier[] = rawTiers.map((t) => ({\n minCertifications:\n (t.min_certifications as number) ?? (t.min as number) ?? 0,\n maxCertifications:\n (t.max_certifications as number) ?? (t.max as number) ?? null,\n priceUsd: (t.price_usd as number) ?? (t.price as number) ?? 0,\n }));\n\n return {\n protocol: (data.protocol as string) || \"\",\n version: (data.version as string) || \"\",\n priceUsd:\n (data.price_usd as number) ?? (data.current_price as number) ?? 0,\n tiers,\n paymentMethods:\n (data.payment_methods as Array<Record<string, string>>) || [],\n raw: data,\n };\n}\n\nexport function parseRegistrationResult(\n data: Record<string, unknown>\n): RegistrationResult {\n const trialData = (data.trial as Record<string, unknown>) || {};\n const trial: TrialInfo = {\n quota: (trialData.quota as number) ?? 0,\n used: (trialData.used as number) ?? 0,\n remaining: (trialData.remaining as number) ?? 0,\n };\n return {\n apiKey: (data.api_key as string) || \"\",\n agentName: (data.agent_name as string) || \"\",\n trial,\n endpoints: (data.endpoints as Record<string, string>) || {},\n raw: data,\n };\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;;;ACAlB,IAAM,cAAN,cAA0B,MAAM;AAAA,EAIrC,YACE,SACA,aAAa,GACb,WAA2C,MAC3C;AACA,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,WAAW;AAAA,EAClB;AACF;AAEO,IAAM,sBAAN,cAAkC,YAAY;AAAA,EACnD,YACE,UAAU,8BACV,WAA2C,MAC3C;AACA,UAAM,SAAS,KAAK,QAAQ;AAC5B,SAAK,OAAO;AAAA,EACd;AACF;AAEO,IAAM,kBAAN,cAA8B,YAAY;AAAA,EAC/C,YACE,UAAU,wBACV,WAA2C,MAC3C;AACA,UAAM,SAAS,KAAK,QAAQ;AAC5B,SAAK,OAAO;AAAA,EACd;AACF;AAEO,IAAM,gBAAN,cAA4B,YAAY;AAAA,EAC7C,YACE,UAAU,sBACV,WAA2C,MAC3C;AACA,UAAM,SAAS,KAAK,QAAQ;AAC5B,SAAK,OAAO;AAAA,EACd;AACF;AAEO,IAAM,gBAAN,cAA4B,YAAY;AAAA,EAG7C,YACE,UAAU,0BACV,kBAAkB,IAClB,WAA2C,MAC3C;AACA,UAAM,SAAS,KAAK,QAAQ;AAC5B,SAAK,OAAO;AACZ,SAAK,kBAAkB;AAAA,EACzB;AACF;AAEO,IAAM,iBAAN,cAA6B,YAAY;AAAA,EAC9C,YACE,UAAU,uBACV,WAA2C,MAC3C;AACA,UAAM,SAAS,KAAK,QAAQ;AAC5B,SAAK,OAAO;AAAA,EACd;AACF;AAEO,IAAM,cAAN,cAA0B,YAAY;AAAA,EAC3C,YACE,UAAU,yBACV,aAAa,KACb,WAA2C,MAC3C;AACA,UAAM,SAAS,YA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,KAAK,YAAY,IAAI;AAC3B,WAAO,CAAC;AAAA,EACV;AAAA,EAEA,MAAc,YAAY,MAAgC;AACxD,QAAI;AACJ,QAAI;AACF,aAAQ,MAAM,KAAK,KAAK;AAAA,IAC1B,QAAQ;AACN,aAAO,EAAE,SAAS,MAAM,KAAK,KAAK,EAAE,MAAM,MAAM,EAAE,EAAE;AAAA,IACtD;AAEA,UAAM,UACH,KAAK,WAAuB,KAAK,SAAoB,QAAQ,KAAK,MAAM;AAC3E,UAAM,SAAS,KAAK;AAEpB,QAAI,WAAW,IAAK,OAAM,IAAI,gBAAgB,SAAS,IAAI;AAC3D,QAAI,WAAW,OAAO,WAAW;AAC/B,YAAM,IAAI,oBAAoB,SAAS,IAAI;AAC7C,QAAI,WAAW,IAAK,OAAM,IAAI,cAAc,SAAS,IAAI;AACzD,QAAI,WAAW;AACb,YAAM,IAAI;AAAA,QACR;AAAA,QACC,KAAK,mBAA8B;AAAA,QACpC;AAAA,MACF;AACF,QAAI,WAAW,IAAK,OAAM,IAAI,eAAe,SAAS,IAAI;AAC1D,QAAI,UAAU,IAAK,OAAM,IAAI,YAAY,SAAS,QAAQ,IAAI;AAC9D,UAAM,IAAI,YAAY,SAAS,QAAQ,IAAI;AAAA,EAC7C;AACF;","names":[]}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { X as XProofClient } from '../client-DOH3QHZm.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Vercel AI SDK middleware for automatic xProof certification.
|
|
5
|
+
*
|
|
6
|
+
* Provides two integration modes:
|
|
7
|
+
*
|
|
8
|
+
* 1. **Automatic middleware** — wraps a language model so every
|
|
9
|
+
* `generateText` / `streamText` call is certified automatically.
|
|
10
|
+
*
|
|
11
|
+
* 2. **Manual helpers** — `certifyGeneration` / `certifyStream` for
|
|
12
|
+
* post-hoc certification of individual calls.
|
|
13
|
+
*
|
|
14
|
+
* @example Automatic middleware
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { xproofMiddleware } from "xproof/vercel";
|
|
17
|
+
* import { generateText, wrapLanguageModel } from "ai";
|
|
18
|
+
* import { openai } from "@ai-sdk/openai";
|
|
19
|
+
*
|
|
20
|
+
* const mw = xproofMiddleware({ apiKey: "pm_...", agentName: "my-app" });
|
|
21
|
+
*
|
|
22
|
+
* const model = wrapLanguageModel({
|
|
23
|
+
* model: openai("gpt-4"),
|
|
24
|
+
* middleware: mw.middleware,
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* const { text } = await generateText({ model, prompt: "Hello" });
|
|
28
|
+
* // proof is created automatically
|
|
29
|
+
* console.log(mw.proofs); // [{ proofId, fileHash, transactionHash }]
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @example Manual certification
|
|
33
|
+
* ```typescript
|
|
34
|
+
* import { xproofMiddleware } from "xproof/vercel";
|
|
35
|
+
* import { generateText } from "ai";
|
|
36
|
+
*
|
|
37
|
+
* const mw = xproofMiddleware({ apiKey: "pm_..." });
|
|
38
|
+
*
|
|
39
|
+
* const { text } = await generateText({
|
|
40
|
+
* model: openai("gpt-4"),
|
|
41
|
+
* prompt: "Hello",
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* const proof = await mw.certifyGeneration({
|
|
45
|
+
* model: "gpt-4",
|
|
46
|
+
* prompt: "Hello",
|
|
47
|
+
* result: text,
|
|
48
|
+
* functionId: "chat",
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
interface XProofMiddlewareOptions {
|
|
54
|
+
apiKey?: string;
|
|
55
|
+
client?: XProofClient;
|
|
56
|
+
agentName?: string;
|
|
57
|
+
why?: string;
|
|
58
|
+
metadata?: Record<string, unknown>;
|
|
59
|
+
shouldCertify?: (params: {
|
|
60
|
+
type: string;
|
|
61
|
+
model: string;
|
|
62
|
+
}) => boolean;
|
|
63
|
+
batchMode?: boolean;
|
|
64
|
+
batchFlushSize?: number;
|
|
65
|
+
}
|
|
66
|
+
interface MiddlewareResult {
|
|
67
|
+
proofId: string;
|
|
68
|
+
fileHash: string;
|
|
69
|
+
transactionHash: string;
|
|
70
|
+
}
|
|
71
|
+
declare function xproofMiddleware(options?: XProofMiddlewareOptions): {
|
|
72
|
+
middleware: {
|
|
73
|
+
wrapGenerate: (opts: {
|
|
74
|
+
doGenerate: () => Promise<Record<string, unknown>>;
|
|
75
|
+
params: Record<string, unknown>;
|
|
76
|
+
model: unknown;
|
|
77
|
+
}) => Promise<Record<string, unknown>>;
|
|
78
|
+
wrapStream: (opts: {
|
|
79
|
+
doStream: () => Promise<{
|
|
80
|
+
stream: ReadableStream;
|
|
81
|
+
[key: string]: unknown;
|
|
82
|
+
}>;
|
|
83
|
+
params: Record<string, unknown>;
|
|
84
|
+
model: unknown;
|
|
85
|
+
}) => Promise<{
|
|
86
|
+
[key: string]: unknown;
|
|
87
|
+
stream: ReadableStream;
|
|
88
|
+
}>;
|
|
89
|
+
};
|
|
90
|
+
certifyGeneration(params: {
|
|
91
|
+
model: string;
|
|
92
|
+
prompt: string | Array<unknown>;
|
|
93
|
+
result: string;
|
|
94
|
+
functionId?: string;
|
|
95
|
+
metadata?: Record<string, unknown>;
|
|
96
|
+
}): Promise<MiddlewareResult>;
|
|
97
|
+
certifyStream(params: {
|
|
98
|
+
model: string;
|
|
99
|
+
prompt: string | Array<unknown>;
|
|
100
|
+
fullText: string;
|
|
101
|
+
functionId?: string;
|
|
102
|
+
metadata?: Record<string, unknown>;
|
|
103
|
+
}): Promise<MiddlewareResult>;
|
|
104
|
+
flushBatch: () => Promise<MiddlewareResult[]>;
|
|
105
|
+
readonly proofs: MiddlewareResult[];
|
|
106
|
+
readonly pendingCount: number;
|
|
107
|
+
client: XProofClient;
|
|
108
|
+
agentName: string;
|
|
109
|
+
};
|
|
110
|
+
type XProofVercelMiddleware = ReturnType<typeof xproofMiddleware>;
|
|
111
|
+
|
|
112
|
+
export { type MiddlewareResult, type XProofMiddlewareOptions, type XProofVercelMiddleware, xproofMiddleware };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { X as XProofClient } from '../client-DOH3QHZm.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Vercel AI SDK middleware for automatic xProof certification.
|
|
5
|
+
*
|
|
6
|
+
* Provides two integration modes:
|
|
7
|
+
*
|
|
8
|
+
* 1. **Automatic middleware** — wraps a language model so every
|
|
9
|
+
* `generateText` / `streamText` call is certified automatically.
|
|
10
|
+
*
|
|
11
|
+
* 2. **Manual helpers** — `certifyGeneration` / `certifyStream` for
|
|
12
|
+
* post-hoc certification of individual calls.
|
|
13
|
+
*
|
|
14
|
+
* @example Automatic middleware
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { xproofMiddleware } from "xproof/vercel";
|
|
17
|
+
* import { generateText, wrapLanguageModel } from "ai";
|
|
18
|
+
* import { openai } from "@ai-sdk/openai";
|
|
19
|
+
*
|
|
20
|
+
* const mw = xproofMiddleware({ apiKey: "pm_...", agentName: "my-app" });
|
|
21
|
+
*
|
|
22
|
+
* const model = wrapLanguageModel({
|
|
23
|
+
* model: openai("gpt-4"),
|
|
24
|
+
* middleware: mw.middleware,
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* const { text } = await generateText({ model, prompt: "Hello" });
|
|
28
|
+
* // proof is created automatically
|
|
29
|
+
* console.log(mw.proofs); // [{ proofId, fileHash, transactionHash }]
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @example Manual certification
|
|
33
|
+
* ```typescript
|
|
34
|
+
* import { xproofMiddleware } from "xproof/vercel";
|
|
35
|
+
* import { generateText } from "ai";
|
|
36
|
+
*
|
|
37
|
+
* const mw = xproofMiddleware({ apiKey: "pm_..." });
|
|
38
|
+
*
|
|
39
|
+
* const { text } = await generateText({
|
|
40
|
+
* model: openai("gpt-4"),
|
|
41
|
+
* prompt: "Hello",
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* const proof = await mw.certifyGeneration({
|
|
45
|
+
* model: "gpt-4",
|
|
46
|
+
* prompt: "Hello",
|
|
47
|
+
* result: text,
|
|
48
|
+
* functionId: "chat",
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
interface XProofMiddlewareOptions {
|
|
54
|
+
apiKey?: string;
|
|
55
|
+
client?: XProofClient;
|
|
56
|
+
agentName?: string;
|
|
57
|
+
why?: string;
|
|
58
|
+
metadata?: Record<string, unknown>;
|
|
59
|
+
shouldCertify?: (params: {
|
|
60
|
+
type: string;
|
|
61
|
+
model: string;
|
|
62
|
+
}) => boolean;
|
|
63
|
+
batchMode?: boolean;
|
|
64
|
+
batchFlushSize?: number;
|
|
65
|
+
}
|
|
66
|
+
interface MiddlewareResult {
|
|
67
|
+
proofId: string;
|
|
68
|
+
fileHash: string;
|
|
69
|
+
transactionHash: string;
|
|
70
|
+
}
|
|
71
|
+
declare function xproofMiddleware(options?: XProofMiddlewareOptions): {
|
|
72
|
+
middleware: {
|
|
73
|
+
wrapGenerate: (opts: {
|
|
74
|
+
doGenerate: () => Promise<Record<string, unknown>>;
|
|
75
|
+
params: Record<string, unknown>;
|
|
76
|
+
model: unknown;
|
|
77
|
+
}) => Promise<Record<string, unknown>>;
|
|
78
|
+
wrapStream: (opts: {
|
|
79
|
+
doStream: () => Promise<{
|
|
80
|
+
stream: ReadableStream;
|
|
81
|
+
[key: string]: unknown;
|
|
82
|
+
}>;
|
|
83
|
+
params: Record<string, unknown>;
|
|
84
|
+
model: unknown;
|
|
85
|
+
}) => Promise<{
|
|
86
|
+
[key: string]: unknown;
|
|
87
|
+
stream: ReadableStream;
|
|
88
|
+
}>;
|
|
89
|
+
};
|
|
90
|
+
certifyGeneration(params: {
|
|
91
|
+
model: string;
|
|
92
|
+
prompt: string | Array<unknown>;
|
|
93
|
+
result: string;
|
|
94
|
+
functionId?: string;
|
|
95
|
+
metadata?: Record<string, unknown>;
|
|
96
|
+
}): Promise<MiddlewareResult>;
|
|
97
|
+
certifyStream(params: {
|
|
98
|
+
model: string;
|
|
99
|
+
prompt: string | Array<unknown>;
|
|
100
|
+
fullText: string;
|
|
101
|
+
functionId?: string;
|
|
102
|
+
metadata?: Record<string, unknown>;
|
|
103
|
+
}): Promise<MiddlewareResult>;
|
|
104
|
+
flushBatch: () => Promise<MiddlewareResult[]>;
|
|
105
|
+
readonly proofs: MiddlewareResult[];
|
|
106
|
+
readonly pendingCount: number;
|
|
107
|
+
client: XProofClient;
|
|
108
|
+
agentName: string;
|
|
109
|
+
};
|
|
110
|
+
type XProofVercelMiddleware = ReturnType<typeof xproofMiddleware>;
|
|
111
|
+
|
|
112
|
+
export { type MiddlewareResult, type XProofMiddlewareOptions, type XProofVercelMiddleware, xproofMiddleware };
|