tinfoil 0.1.2 → 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.
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SecureClient = void 0;
7
+ const node_fetch_1 = __importDefault(require("node-fetch"));
8
+ const util_1 = require("util");
9
+ // Set up browser-like globals that the Go WASM runtime expects
10
+ const globalThis = global;
11
+ // Performance API
12
+ globalThis.performance = {
13
+ now: () => Date.now(),
14
+ markResourceTiming: () => { },
15
+ mark: () => { },
16
+ measure: () => { },
17
+ clearMarks: () => { },
18
+ clearMeasures: () => { },
19
+ getEntriesByName: () => [],
20
+ getEntriesByType: () => [],
21
+ getEntries: () => []
22
+ };
23
+ // Text encoding
24
+ globalThis.TextEncoder = util_1.TextEncoder;
25
+ globalThis.TextDecoder = util_1.TextDecoder;
26
+ // Crypto API (needed by Go WASM)
27
+ if (!globalThis.crypto) {
28
+ globalThis.crypto = {
29
+ getRandomValues: (buffer) => {
30
+ const randomBytes = require('crypto').randomBytes(buffer.length);
31
+ buffer.set(new Uint8Array(randomBytes));
32
+ return buffer;
33
+ }
34
+ };
35
+ }
36
+ // Additional browser polyfills that might be needed
37
+ globalThis.window = globalThis;
38
+ globalThis.document = {
39
+ createElement: () => ({
40
+ setAttribute: () => { }
41
+ })
42
+ };
43
+ // Force process to stay running (prevent Go from exiting Node process)
44
+ // This is a common issue with Go WASM in Node - it calls process.exit()
45
+ const originalExit = process.exit;
46
+ process.exit = ((code) => {
47
+ console.log(`Process exit called with code ${code} - ignoring to keep Node.js process alive`);
48
+ return undefined;
49
+ });
50
+ // Load the Go runtime helper
51
+ require('./wasm-exec.js');
52
+ /**
53
+ * SecureClient handles verification of code and runtime measurements using WebAssembly
54
+ */
55
+ class SecureClient {
56
+ constructor(enclave, repo) {
57
+ this.enclave = enclave;
58
+ this.repo = repo;
59
+ }
60
+ /**
61
+ * Static method to initialize WASM module
62
+ * This starts automatically when the class is loaded
63
+ */
64
+ static async initializeWasm() {
65
+ if (SecureClient.initializationPromise) {
66
+ return SecureClient.initializationPromise;
67
+ }
68
+ SecureClient.initializationPromise = (async () => {
69
+ try {
70
+ SecureClient.goInstance = new globalThis.Go();
71
+ const wasmResponse = await (0, node_fetch_1.default)('https://tinfoilsh.github.io/verifier-js/tinfoil-verifier.wasm');
72
+ const wasmBuffer = await wasmResponse.arrayBuffer();
73
+ const result = await WebAssembly.instantiate(wasmBuffer, SecureClient.goInstance.importObject);
74
+ const runPromise = SecureClient.goInstance.run(result.instance);
75
+ let attempts = 0;
76
+ const maxAttempts = 10;
77
+ while (attempts < maxAttempts) {
78
+ attempts++;
79
+ await new Promise(resolve => setTimeout(resolve, 100));
80
+ const hasVerifyCode = typeof globalThis.verifyCode === 'function';
81
+ const hasVerifyEnclave = typeof globalThis.verifyEnclave === 'function';
82
+ if (hasVerifyCode && hasVerifyEnclave) {
83
+ return;
84
+ }
85
+ }
86
+ throw new Error('WASM functions not exposed after multiple attempts');
87
+ }
88
+ catch (error) {
89
+ console.error('WASM initialization error:', error);
90
+ throw error;
91
+ }
92
+ })();
93
+ return SecureClient.initializationPromise;
94
+ }
95
+ /**
96
+ * Initialize the WASM module
97
+ * Now just waits for the static initialization to complete
98
+ */
99
+ async initialize() {
100
+ await SecureClient.initializeWasm();
101
+ }
102
+ /**
103
+ * Verifies the integrity of both the code and runtime environment
104
+ */
105
+ async verify() {
106
+ await this.initialize();
107
+ try {
108
+ if (typeof globalThis.verifyCode !== 'function' || typeof globalThis.verifyEnclave !== 'function') {
109
+ throw new Error('WASM functions not available');
110
+ }
111
+ const releaseResponse = await (0, node_fetch_1.default)(`https://api.github.com/repos/${this.repo}/releases/latest`, {
112
+ headers: {
113
+ 'Accept': 'application/vnd.github.v3+json',
114
+ 'User-Agent': 'tinfoil-node-client'
115
+ }
116
+ });
117
+ if (!releaseResponse.ok) {
118
+ throw new Error(`GitHub API request failed: ${releaseResponse.status} ${releaseResponse.statusText}`);
119
+ }
120
+ const releaseData = await releaseResponse.json();
121
+ const eifRegex = /EIF hash: ([a-f0-9]{64})/i;
122
+ const digestRegex = /Digest: `([a-f0-9]{64})`/;
123
+ let digest;
124
+ const eifMatch = releaseData.body?.match(eifRegex);
125
+ const digestMatch = releaseData.body?.match(digestRegex);
126
+ if (eifMatch) {
127
+ digest = eifMatch[1];
128
+ }
129
+ else if (digestMatch) {
130
+ digest = digestMatch[1];
131
+ }
132
+ else {
133
+ throw new Error('Could not find digest in release notes');
134
+ }
135
+ const [measurement, attestationResponse] = await Promise.all([
136
+ globalThis.verifyCode(this.repo, digest),
137
+ globalThis.verifyEnclave(this.enclave)
138
+ ]);
139
+ if (measurement !== attestationResponse.measurement) {
140
+ throw new Error('Measurements do not match');
141
+ }
142
+ return {
143
+ certFingerprint: new Uint8Array(Buffer.from(attestationResponse.certificate, 'hex')),
144
+ measurement: attestationResponse.measurement,
145
+ };
146
+ }
147
+ catch (error) {
148
+ throw error;
149
+ }
150
+ }
151
+ }
152
+ exports.SecureClient = SecureClient;
153
+ SecureClient.goInstance = null;
154
+ SecureClient.initializationPromise = null;
155
+ // Start initialization as soon as the module loads
156
+ SecureClient.initializeWasm().catch(console.error);
package/package.json CHANGED
@@ -1,48 +1,46 @@
1
1
  {
2
- "name": "tinfoil",
3
- "version": "0.1.2",
4
- "main": "dist/index.js",
5
- "types": "dist/index.d.ts",
6
- "browser": "dist/browser.js",
7
- "type": "module",
8
- "license": "MIT",
9
- "scripts": {
10
- "build": "npm run clean && npm run build:lib && npm run build:browser",
11
- "build:lib": "tsc",
12
- "build:browser": "esbuild src/browser.ts --minify --bundle --sourcemap --outfile=dist/browser.js --tree-shaking=true --main-fields=main,browser",
13
- "clean": "rimraf dist",
14
- "test": "vitest",
15
- "test:cover": "vitest run --coverage",
16
- "test:e2e": "playwright test",
17
- "test:e2e:ui": "playwright test --ui",
18
- "lint": "eslint ./src ./tests",
19
- "serve": "python3 -m http.server 8223"
20
- },
21
- "dependencies": {
22
- "@divviup/common": "^0.2.2",
23
- "@divviup/dap": "^0.9.0",
24
- "@divviup/prio3": "^0.8.0",
25
- "@divviup/vdaf": "^0.8.0",
26
- "ua-parser-js": "^1.0.39"
27
- },
28
- "devDependencies": {
29
- "@eslint/js": "^9.13.0",
30
- "@playwright/test": "^1.48.2",
31
- "@tsconfig/node20": "^20.1.4",
32
- "@types/node": "^22.8.1",
33
- "@types/ua-parser-js": "^0.7.39",
34
- "@vitest/coverage-v8": "^2.1.4",
35
- "@vitest/ui": "^2.1.4",
36
- "esbuild": "^0.24.0",
37
- "eslint": "^9.13.0",
38
- "globals": "^15.11.0",
39
- "rimraf": "^5.0.0",
40
- "ts-node": "^10.9.2",
41
- "typescript": "^5.2.2",
42
- "typescript-eslint": "^8.11.0",
43
- "vitest": "^2.1.4"
44
- },
45
- "directories": {
46
- "test": "tests"
47
- }
2
+ "name": "tinfoil",
3
+ "version": "0.2.0",
4
+ "description": "Official Tinfoil secure OpenAI client wrapper",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "test": "jest --config jest.config.cjs",
10
+ "lint": "eslint src/**/*.ts",
11
+ "prepublishOnly": "npm run build"
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "README.md",
16
+ "LICENSE"
17
+ ],
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/tinfoilsh/tinfoil-node.git"
21
+ },
22
+ "keywords": [
23
+ "openai",
24
+ "security",
25
+ "AI",
26
+ "tinfoil"
27
+ ],
28
+ "author": "Tinfoil",
29
+ "license": "AGPL-3.0-or-later",
30
+ "dependencies": {
31
+ "dotenv": "^16.4.7",
32
+ "node-fetch": "^2.7.0",
33
+ "openai": "^4.87.3",
34
+ "ts-node": "^10.9.2"
35
+ },
36
+ "devDependencies": {
37
+ "@types/jest": "^29.5.12",
38
+ "@types/node": "^20.11.24",
39
+ "@typescript-eslint/eslint-plugin": "^8.26.1",
40
+ "@typescript-eslint/parser": "^8.26.1",
41
+ "eslint": "^8.57.0",
42
+ "jest": "^29.7.0",
43
+ "ts-jest": "^29.1.2",
44
+ "typescript": "~5.5.0"
45
+ }
48
46
  }
package/dist/browser.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './index';
2
- import './script';
3
- //# sourceMappingURL=browser.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AAExB,OAAO,UAAU,CAAC"}