tinfoil 0.7.2 → 0.8.1

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.
Files changed (51) hide show
  1. package/README.md +171 -8
  2. package/dist/__tests__/test-utils.d.ts +1 -0
  3. package/dist/__tests__/test-utils.js +44 -0
  4. package/dist/ai-sdk-provider.browser.d.ts +6 -0
  5. package/dist/ai-sdk-provider.browser.js +36 -0
  6. package/dist/ai-sdk-provider.d.ts +9 -1
  7. package/dist/ai-sdk-provider.js +154 -32
  8. package/dist/client.browser.d.ts +51 -0
  9. package/dist/client.browser.js +157 -0
  10. package/dist/client.d.ts +20 -4
  11. package/dist/client.js +175 -57
  12. package/dist/encrypted-body-fetch.d.ts +10 -0
  13. package/dist/encrypted-body-fetch.js +158 -0
  14. package/dist/env.d.ts +5 -0
  15. package/dist/env.js +20 -0
  16. package/dist/esm/__tests__/test-utils.d.ts +1 -0
  17. package/dist/esm/__tests__/test-utils.js +38 -0
  18. package/dist/esm/ai-sdk-provider.browser.d.ts +6 -0
  19. package/dist/esm/ai-sdk-provider.browser.js +33 -0
  20. package/dist/esm/ai-sdk-provider.d.ts +15 -0
  21. package/dist/esm/ai-sdk-provider.js +165 -0
  22. package/dist/esm/client.browser.d.ts +51 -0
  23. package/dist/esm/client.browser.js +150 -0
  24. package/dist/esm/client.d.ts +119 -0
  25. package/dist/esm/client.js +357 -0
  26. package/dist/esm/config.d.ts +13 -0
  27. package/dist/esm/config.js +13 -0
  28. package/dist/esm/encrypted-body-fetch.d.ts +10 -0
  29. package/dist/esm/encrypted-body-fetch.js +118 -0
  30. package/dist/esm/env.d.ts +5 -0
  31. package/dist/esm/env.js +17 -0
  32. package/dist/esm/index.browser.d.ts +6 -0
  33. package/dist/esm/index.browser.js +7 -0
  34. package/dist/esm/index.d.ts +7 -0
  35. package/dist/esm/index.js +11 -0
  36. package/dist/esm/verification-runner.d.ts +100 -0
  37. package/dist/esm/verification-runner.js +224 -0
  38. package/dist/esm/verifier.d.ts +120 -0
  39. package/dist/esm/verifier.js +685 -0
  40. package/dist/esm/wasm-exec.js +668 -0
  41. package/dist/index.browser.d.ts +6 -0
  42. package/dist/index.browser.js +26 -0
  43. package/dist/index.d.ts +2 -1
  44. package/dist/index.js +3 -2
  45. package/dist/verification-runner.d.ts +100 -0
  46. package/dist/verification-runner.js +230 -0
  47. package/dist/verifier.d.ts +120 -0
  48. package/dist/verifier.js +725 -0
  49. package/package.json +52 -8
  50. package/dist/secure-client.d.ts +0 -31
  51. package/dist/secure-client.js +0 -153
package/package.json CHANGED
@@ -1,12 +1,55 @@
1
1
  {
2
2
  "name": "tinfoil",
3
- "version": "0.7.2",
3
+ "version": "0.8.1",
4
4
  "description": "Tinfoil secure OpenAI client wrapper",
5
5
  "main": "dist/index.js",
6
+ "module": "dist/esm/index.js",
6
7
  "types": "dist/index.d.ts",
8
+ "browser": {
9
+ "./dist/index.js": "./dist/esm/index.browser.js",
10
+ "./dist/esm/index.js": "./dist/esm/index.browser.js"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "browser": "./dist/esm/index.browser.js",
16
+ "import": "./dist/esm/index.js",
17
+ "require": "./dist/index.js",
18
+ "default": "./dist/index.js"
19
+ },
20
+ "./client": {
21
+ "types": "./dist/client.d.ts",
22
+ "browser": "./dist/esm/client.browser.js",
23
+ "import": "./dist/esm/client.js",
24
+ "require": "./dist/client.js"
25
+ },
26
+ "./ai-sdk-provider": {
27
+ "types": "./dist/ai-sdk-provider.d.ts",
28
+ "browser": "./dist/esm/ai-sdk-provider.browser.js",
29
+ "import": "./dist/esm/ai-sdk-provider.js",
30
+ "require": "./dist/ai-sdk-provider.js"
31
+ },
32
+ "./verifier": {
33
+ "types": "./dist/verifier.d.ts",
34
+ "import": "./dist/esm/verifier.js",
35
+ "require": "./dist/verifier.js"
36
+ },
37
+ "./verification-runner": {
38
+ "types": "./dist/verification-runner.d.ts",
39
+ "import": "./dist/esm/verification-runner.js",
40
+ "require": "./dist/verification-runner.js"
41
+ },
42
+ "./config": {
43
+ "types": "./dist/config.d.ts",
44
+ "import": "./dist/esm/config.js",
45
+ "require": "./dist/config.js"
46
+ }
47
+ },
7
48
  "scripts": {
8
- "build": "tsc && cp src/wasm-exec.js dist/",
9
- "test": "jest --config jest.config.cjs",
49
+ "build": "npm run build:cjs && npm run build:esm",
50
+ "build:cjs": "tsc && cp src/wasm-exec.js dist/",
51
+ "build:esm": "tsc -p tsconfig.esm.json && cp src/wasm-exec.js dist/esm/",
52
+ "test": "bash -lc 'rm -rf .node-tests && tsc -p tsconfig.node-test.json && cp src/wasm-exec.js .node-tests/ && node --test $(find .node-tests -name \"*.test.js\" -print)'",
10
53
  "lint": "eslint src/**/*.ts",
11
54
  "prepublishOnly": "npm run build"
12
55
  },
@@ -27,22 +70,23 @@
27
70
  ],
28
71
  "author": "Tinfoil",
29
72
  "license": "AGPL-3.0-or-later",
73
+ "engines": {
74
+ "node": ">=20"
75
+ },
30
76
  "dependencies": {
31
77
  "@ai-sdk/openai-compatible": "^1.0.10",
32
78
  "ai": "^5.0.19",
33
79
  "dotenv": "^17.2.1",
80
+ "ehbp": "^0.0.2",
34
81
  "openai": "^5.13.1",
35
- "ts-node": "^10.9.2",
36
- "undici": "^7.14.0"
82
+ "ts-node": "^10.9.2"
37
83
  },
38
84
  "devDependencies": {
39
- "@types/jest": "^29.5.12",
40
85
  "@types/node": "^24.3.0",
41
86
  "@typescript-eslint/eslint-plugin": "^8.40.0",
42
87
  "@typescript-eslint/parser": "^8.40.0",
43
88
  "eslint": "^8.57.0",
44
- "jest": "^29.7.0",
45
- "ts-jest": "^29.4.1",
89
+ "jsdom": "^25.0.0",
46
90
  "typescript": "^5.9.2"
47
91
  }
48
92
  }
@@ -1,31 +0,0 @@
1
- /**
2
- * Ground truth measurements from verification
3
- */
4
- export interface GroundTruth {
5
- publicKeyFP: string;
6
- measurement: string;
7
- }
8
- /**
9
- * SecureClient handles verification of code and runtime measurements using WebAssembly
10
- */
11
- export declare class SecureClient {
12
- private static goInstance;
13
- private static initializationPromise;
14
- private readonly enclave;
15
- private readonly repo;
16
- constructor();
17
- /**
18
- * Static method to initialize WASM module
19
- * This starts automatically when the class is loaded
20
- */
21
- static initializeWasm(): Promise<void>;
22
- /**
23
- * Initialize the WASM module
24
- * Now just waits for the static initialization to complete
25
- */
26
- initialize(): Promise<void>;
27
- /**
28
- * Verifies the integrity of both the code and runtime environment
29
- */
30
- verify(): Promise<GroundTruth>;
31
- }
@@ -1,153 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SecureClient = void 0;
4
- const undici_1 = require("undici");
5
- const util_1 = require("util");
6
- const config_1 = require("./config");
7
- // Set up browser-like globals that the Go WASM runtime expects
8
- const globalThis = global;
9
- // Performance API
10
- globalThis.performance = {
11
- now: () => Date.now(),
12
- markResourceTiming: () => { },
13
- mark: () => { },
14
- measure: () => { },
15
- clearMarks: () => { },
16
- clearMeasures: () => { },
17
- getEntriesByName: () => [],
18
- getEntriesByType: () => [],
19
- getEntries: () => [],
20
- };
21
- // Text encoding
22
- globalThis.TextEncoder = util_1.TextEncoder;
23
- globalThis.TextDecoder = util_1.TextDecoder;
24
- // Crypto API (needed by Go WASM)
25
- if (!globalThis.crypto) {
26
- globalThis.crypto = {
27
- getRandomValues: (buffer) => {
28
- const randomBytes = require("crypto").randomBytes(buffer.length);
29
- buffer.set(new Uint8Array(randomBytes));
30
- return buffer;
31
- },
32
- };
33
- }
34
- // Force process to stay running (prevent Go from exiting Node process)
35
- // This is a common issue with Go WASM in Node - it calls process.exit()
36
- const originalExit = process.exit;
37
- process.exit = ((code) => {
38
- console.log(`Process exit called with code ${code} - ignoring to keep Node.js process alive`);
39
- return undefined;
40
- });
41
- // Load the Go runtime helper
42
- require("./wasm-exec.js");
43
- /**
44
- * SecureClient handles verification of code and runtime measurements using WebAssembly
45
- */
46
- class SecureClient {
47
- constructor() {
48
- // Values for the Tinfoil inference proxy from config
49
- this.enclave = new URL(config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL)
50
- .hostname;
51
- this.repo = config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
52
- }
53
- /**
54
- * Static method to initialize WASM module
55
- * This starts automatically when the class is loaded
56
- */
57
- static async initializeWasm() {
58
- if (SecureClient.initializationPromise) {
59
- return SecureClient.initializationPromise;
60
- }
61
- SecureClient.initializationPromise = (async () => {
62
- try {
63
- SecureClient.goInstance = new globalThis.Go();
64
- const wasmResponse = await (0, undici_1.fetch)("https://tinfoilsh.github.io/verifier-js/tinfoil-verifier.wasm");
65
- const wasmBuffer = await wasmResponse.arrayBuffer();
66
- const result = await WebAssembly.instantiate(wasmBuffer, SecureClient.goInstance.importObject);
67
- SecureClient.goInstance.run(result.instance).catch((error) => {
68
- console.error("Go instance failed to run:", error);
69
- throw error;
70
- });
71
- let attempts = 0;
72
- const maxAttempts = 10;
73
- while (attempts < maxAttempts) {
74
- attempts++;
75
- await new Promise((resolve) => setTimeout(resolve, 100));
76
- const hasVerifyCode = typeof globalThis.verifyCode === "function";
77
- const hasVerifyEnclave = typeof globalThis.verifyEnclave === "function";
78
- if (hasVerifyCode && hasVerifyEnclave) {
79
- return;
80
- }
81
- }
82
- throw new Error("WASM functions not exposed after multiple attempts");
83
- }
84
- catch (error) {
85
- console.error("WASM initialization error:", error);
86
- throw error;
87
- }
88
- })();
89
- return SecureClient.initializationPromise;
90
- }
91
- /**
92
- * Initialize the WASM module
93
- * Now just waits for the static initialization to complete
94
- */
95
- async initialize() {
96
- await SecureClient.initializeWasm();
97
- }
98
- /**
99
- * Verifies the integrity of both the code and runtime environment
100
- */
101
- async verify() {
102
- await this.initialize();
103
- try {
104
- if (typeof globalThis.verifyCode !== "function" ||
105
- typeof globalThis.verifyEnclave !== "function") {
106
- throw new Error("WASM functions not available");
107
- }
108
- const releaseResponse = await (0, undici_1.fetch)(`https://api-github-proxy.tinfoil.sh/repos/${this.repo}/releases/latest`, {
109
- headers: {
110
- Accept: "application/vnd.github.v3+json",
111
- "User-Agent": "tinfoil-node-client",
112
- },
113
- });
114
- if (!releaseResponse.ok) {
115
- throw new Error(`GitHub API request failed: ${releaseResponse.status} ${releaseResponse.statusText}`);
116
- }
117
- const releaseData = (await releaseResponse.json());
118
- const eifRegex = /EIF hash: ([a-f0-9]{64})/i;
119
- const digestRegex = /Digest: `([a-f0-9]{64})`/;
120
- let digest;
121
- const eifMatch = releaseData.body?.match(eifRegex);
122
- const digestMatch = releaseData.body?.match(digestRegex);
123
- if (eifMatch) {
124
- digest = eifMatch[1];
125
- }
126
- else if (digestMatch) {
127
- digest = digestMatch[1];
128
- }
129
- else {
130
- throw new Error("Could not find digest in release notes");
131
- }
132
- const [measurement, attestationResponse] = await Promise.all([
133
- globalThis.verifyCode(this.repo, digest),
134
- globalThis.verifyEnclave(this.enclave),
135
- ]);
136
- if (measurement !== attestationResponse.measurement) {
137
- throw new Error("Measurements do not match");
138
- }
139
- return {
140
- publicKeyFP: attestationResponse.certificate,
141
- measurement: attestationResponse.measurement,
142
- };
143
- }
144
- catch (error) {
145
- throw error;
146
- }
147
- }
148
- }
149
- exports.SecureClient = SecureClient;
150
- SecureClient.goInstance = null;
151
- SecureClient.initializationPromise = null;
152
- // Start initialization as soon as the module loads
153
- SecureClient.initializeWasm().catch(console.error);