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.
- package/LICENSE +661 -21
- package/README.md +52 -32
- package/dist/client.d.ts +90 -23
- package/dist/client.js +180 -336
- package/dist/index.d.ts +1 -4
- package/dist/index.js +18 -5
- package/dist/secure-client.d.ts +40 -0
- package/dist/secure-client.js +156 -0
- package/package.json +44 -46
- package/dist/browser.d.ts +0 -3
- package/dist/browser.d.ts.map +0 -1
- package/dist/browser.js +0 -47
- package/dist/browser.js.map +0 -7
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/config.d.ts +0 -4
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js +0 -110
- package/dist/config.js.map +0 -1
- package/dist/debugLogger.d.ts +0 -12
- package/dist/debugLogger.d.ts.map +0 -1
- package/dist/debugLogger.js +0 -34
- package/dist/debugLogger.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/script.d.ts +0 -7
- package/dist/script.d.ts.map +0 -1
- package/dist/script.js +0 -20
- package/dist/script.js.map +0 -1
- package/dist/types.d.ts +0 -49
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
|
@@ -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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
package/dist/browser.d.ts.map
DELETED
|
@@ -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"}
|