tinfoil 0.1.3 → 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 +54 -29
- package/dist/client.d.ts +91 -0
- package/dist/client.js +199 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +18 -0
- package/dist/secure-client.d.ts +40 -0
- package/dist/secure-client.js +156 -0
- package/package.json +44 -48
package/README.md
CHANGED
|
@@ -1,52 +1,77 @@
|
|
|
1
|
-
# Tinfoil
|
|
1
|
+
# Tinfoil Node Client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/tinfoilsh/tinfoil-node/actions)
|
|
4
|
+
[](https://npmjs.org/package/tinfoil)
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
A Node.js wrapper around the OpenAI client that verifies enclave attestation and certificate fingerprints when using Tinfoil inference.
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
## Installation
|
|
8
9
|
|
|
9
|
-
```
|
|
10
|
-
|
|
10
|
+
```bash
|
|
11
|
+
npm install tinfoil
|
|
11
12
|
```
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
## Quick Start
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { TinfoilAI } from 'tinfoil';
|
|
18
|
+
|
|
19
|
+
const client = new TinfoilAI({
|
|
20
|
+
enclave: 'models.default.tinfoil.sh', // or use TINFOIL_ENCLAVE env var
|
|
21
|
+
repo: 'tinfoilsh/default-models-nitro', // or use TINFOIL_REPO env var
|
|
22
|
+
apiKey: 'tinfoil' // or use OPENAI_API_KEY env var
|
|
23
|
+
});
|
|
14
24
|
|
|
15
|
-
|
|
16
|
-
|
|
25
|
+
// Uses identical method calls as the OpenAI client
|
|
26
|
+
const completion = await client.chat.completions.create({
|
|
27
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
28
|
+
model: 'llama3.2:1b'
|
|
29
|
+
});
|
|
17
30
|
```
|
|
18
31
|
|
|
19
|
-
##
|
|
32
|
+
## Running the Chat Example
|
|
20
33
|
|
|
21
|
-
To run the
|
|
34
|
+
To run the streaming chat example:
|
|
35
|
+
|
|
36
|
+
1. Clone the repository
|
|
37
|
+
|
|
38
|
+
2. Install dependencies:
|
|
22
39
|
|
|
23
40
|
```bash
|
|
24
|
-
# Install dependencies
|
|
25
41
|
npm install
|
|
42
|
+
```
|
|
26
43
|
|
|
27
|
-
|
|
28
|
-
npm run build
|
|
44
|
+
3. Optionally create a `.env` file with your configuration:
|
|
29
45
|
|
|
30
|
-
|
|
31
|
-
|
|
46
|
+
```bash
|
|
47
|
+
TINFOIL_ENCLAVE=models.default.tinfoil.sh
|
|
48
|
+
TINFOIL_REPO=tinfoilsh/default-models-nitro
|
|
49
|
+
OPENAI_API_KEY=your_api_key
|
|
50
|
+
```
|
|
32
51
|
|
|
33
|
-
|
|
34
|
-
|
|
52
|
+
4. Run the example:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
npx ts-node examples/chat/main.ts
|
|
35
56
|
```
|
|
36
57
|
|
|
37
|
-
|
|
58
|
+
The example demonstrates streaming chat completions with the Tinfoil API wrapper.
|
|
38
59
|
|
|
39
|
-
|
|
60
|
+
## Runtime Support
|
|
40
61
|
|
|
41
|
-
|
|
62
|
+
Supports Node.js 18+, Deno, Bun, Cloudflare Workers, and more. Browser usage is disabled by default for security. See [OpenAI Node.js client](https://github.com/openai/openai-node) for complete runtime compatibility.
|
|
42
63
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
64
|
+
## API Documentation
|
|
65
|
+
|
|
66
|
+
This library is a drop-in replacement for the official OpenAI Node.js client that can be used with Tinfoil. All methods and types are identical. See the [OpenAI client](https://github.com/openai/openai-node) for complete API usage and documentation.
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
## Reporting Vulnerabilities
|
|
70
|
+
|
|
71
|
+
Please report security vulnerabilities by either:
|
|
72
|
+
|
|
73
|
+
- Emailing [security@tinfoil.sh](mailto:security@tinfoil.sh)
|
|
49
74
|
|
|
50
|
-
|
|
75
|
+
- Opening an issue on GitHub on this repository
|
|
51
76
|
|
|
52
|
-
|
|
77
|
+
We aim to respond to security reports within 24 hours and will keep you updated on our progress.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import OpenAI from 'openai';
|
|
2
|
+
import type { Chat, Files, FineTuning, Images, Audio, Embeddings, Models, Moderations, Beta } from 'openai/resources';
|
|
3
|
+
/**
|
|
4
|
+
* TinfoilAI is a wrapper around the OpenAI API client that adds additional
|
|
5
|
+
* security measures through enclave verification and certificate fingerprint validation.
|
|
6
|
+
*
|
|
7
|
+
* It provides:
|
|
8
|
+
* - Automatic verification of Tinfoil secure enclaves
|
|
9
|
+
* - Certificate fingerprint validation for each request
|
|
10
|
+
* - Type-safe access to OpenAI's chat completion APIs
|
|
11
|
+
*/
|
|
12
|
+
export declare class TinfoilAI {
|
|
13
|
+
private client?;
|
|
14
|
+
private enclave;
|
|
15
|
+
private repo;
|
|
16
|
+
private groundTruth?;
|
|
17
|
+
private clientPromise;
|
|
18
|
+
private readyPromise?;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new TinfoilAI instance using environment variables.
|
|
21
|
+
* @param options - Optional OpenAI client configuration options
|
|
22
|
+
* @throws Error if TINFOIL_ENCLAVE or TINFOIL_REPO environment variables are not set
|
|
23
|
+
*/
|
|
24
|
+
constructor(options?: ConstructorParameters<typeof OpenAI>[0]);
|
|
25
|
+
/**
|
|
26
|
+
* Creates a new TinfoilAI instance with explicit enclave and repo values.
|
|
27
|
+
* @param enclave - The enclave URL/identifier
|
|
28
|
+
* @param repo - The repository identifier
|
|
29
|
+
* @param options - Optional OpenAI client configuration options
|
|
30
|
+
*/
|
|
31
|
+
constructor(enclave: string, repo: string, options?: ConstructorParameters<typeof OpenAI>[0]);
|
|
32
|
+
/**
|
|
33
|
+
* Ensures the client is ready to use.
|
|
34
|
+
* @returns Promise that resolves when the client is initialized
|
|
35
|
+
*/
|
|
36
|
+
ready(): Promise<void>;
|
|
37
|
+
private initClient;
|
|
38
|
+
private createOpenAIClient;
|
|
39
|
+
/**
|
|
40
|
+
* Helper method to ensure the OpenAI client is initialized before use.
|
|
41
|
+
* Automatically calls ready() if needed.
|
|
42
|
+
* @returns The initialized OpenAI client
|
|
43
|
+
* @private
|
|
44
|
+
*/
|
|
45
|
+
private ensureReady;
|
|
46
|
+
/**
|
|
47
|
+
* Access to OpenAI's chat completions API for creating chat conversations.
|
|
48
|
+
* Automatically initializes the client if needed.
|
|
49
|
+
*/
|
|
50
|
+
get chat(): Chat;
|
|
51
|
+
/**
|
|
52
|
+
* Access to OpenAI's files API for managing files used with the API.
|
|
53
|
+
* Automatically initializes the client if needed.
|
|
54
|
+
*/
|
|
55
|
+
get files(): Files;
|
|
56
|
+
/**
|
|
57
|
+
* Access to OpenAI's fine-tuning API for creating and managing fine-tuned models.
|
|
58
|
+
* Automatically initializes the client if needed.
|
|
59
|
+
*/
|
|
60
|
+
get fineTuning(): FineTuning;
|
|
61
|
+
/**
|
|
62
|
+
* Access to OpenAI's image generation and editing API.
|
|
63
|
+
* Automatically initializes the client if needed.
|
|
64
|
+
*/
|
|
65
|
+
get images(): Images;
|
|
66
|
+
/**
|
|
67
|
+
* Access to OpenAI's audio API for speech-to-text and text-to-speech.
|
|
68
|
+
* Automatically initializes the client if needed.
|
|
69
|
+
*/
|
|
70
|
+
get audio(): Audio;
|
|
71
|
+
/**
|
|
72
|
+
* Access to OpenAI's embeddings API for creating vector embeddings of text.
|
|
73
|
+
* Automatically initializes the client if needed.
|
|
74
|
+
*/
|
|
75
|
+
get embeddings(): Embeddings;
|
|
76
|
+
/**
|
|
77
|
+
* Access to OpenAI's models API for listing and managing available models.
|
|
78
|
+
* Automatically initializes the client if needed.
|
|
79
|
+
*/
|
|
80
|
+
get models(): Models;
|
|
81
|
+
/**
|
|
82
|
+
* Access to OpenAI's content moderation API.
|
|
83
|
+
* Automatically initializes the client if needed.
|
|
84
|
+
*/
|
|
85
|
+
get moderations(): Moderations;
|
|
86
|
+
/**
|
|
87
|
+
* Access to OpenAI's beta features.
|
|
88
|
+
* Automatically initializes the client if needed.
|
|
89
|
+
*/
|
|
90
|
+
get beta(): Beta;
|
|
91
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
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.TinfoilAI = void 0;
|
|
7
|
+
const openai_1 = __importDefault(require("openai"));
|
|
8
|
+
const crypto_1 = require("crypto");
|
|
9
|
+
const secure_client_1 = require("./secure-client");
|
|
10
|
+
const https_1 = __importDefault(require("https"));
|
|
11
|
+
/**
|
|
12
|
+
* Creates a proxy that allows property access and method calls on a Promise before it resolves.
|
|
13
|
+
* This enables a more ergonomic API where you can chain properties and methods without explicitly
|
|
14
|
+
* awaiting the promise first.
|
|
15
|
+
*
|
|
16
|
+
* @param promise - A Promise that will resolve to an object
|
|
17
|
+
* @returns A proxied version of the promised object that allows immediate property/method access
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // Instead of:
|
|
21
|
+
* const client = await getClient();
|
|
22
|
+
* const result = await client.someProperty.someMethod();
|
|
23
|
+
*
|
|
24
|
+
* // You can write:
|
|
25
|
+
* const client = createAsyncProxy(getClient());
|
|
26
|
+
* const result = await client.someProperty.someMethod();
|
|
27
|
+
*
|
|
28
|
+
* @template T - The type of object that the promise resolves to
|
|
29
|
+
*/
|
|
30
|
+
function createAsyncProxy(promise) {
|
|
31
|
+
return new Proxy({}, {
|
|
32
|
+
get(target, prop) {
|
|
33
|
+
return new Proxy(() => { }, {
|
|
34
|
+
get(_, nestedProp) {
|
|
35
|
+
return (...args) => promise.then(obj => {
|
|
36
|
+
const value = obj[prop][nestedProp];
|
|
37
|
+
return typeof value === 'function' ? value.apply(obj[prop], args) : value;
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
apply(_, __, args) {
|
|
41
|
+
return promise.then(obj => {
|
|
42
|
+
const value = obj[prop];
|
|
43
|
+
return typeof value === 'function' ? value.apply(obj, args) : value;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* TinfoilAI is a wrapper around the OpenAI API client that adds additional
|
|
52
|
+
* security measures through enclave verification and certificate fingerprint validation.
|
|
53
|
+
*
|
|
54
|
+
* It provides:
|
|
55
|
+
* - Automatic verification of Tinfoil secure enclaves
|
|
56
|
+
* - Certificate fingerprint validation for each request
|
|
57
|
+
* - Type-safe access to OpenAI's chat completion APIs
|
|
58
|
+
*/
|
|
59
|
+
class TinfoilAI {
|
|
60
|
+
constructor(enclaveOrOptions, repoOrNothing, options) {
|
|
61
|
+
if (typeof enclaveOrOptions === 'string' && typeof repoOrNothing === 'string') {
|
|
62
|
+
this.enclave = enclaveOrOptions;
|
|
63
|
+
this.repo = repoOrNothing;
|
|
64
|
+
this.clientPromise = this.initClient(options);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
this.enclave = process.env.TINFOIL_ENCLAVE || '';
|
|
68
|
+
this.repo = process.env.TINFOIL_REPO || '';
|
|
69
|
+
if (!this.enclave || !this.repo) {
|
|
70
|
+
throw new Error('tinfoil: TINFOIL_ENCLAVE and TINFOIL_REPO environment variables must be specified');
|
|
71
|
+
}
|
|
72
|
+
this.clientPromise = this.initClient(enclaveOrOptions);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Ensures the client is ready to use.
|
|
77
|
+
* @returns Promise that resolves when the client is initialized
|
|
78
|
+
*/
|
|
79
|
+
async ready() {
|
|
80
|
+
if (!this.readyPromise) {
|
|
81
|
+
this.readyPromise = (async () => {
|
|
82
|
+
this.client = await this.clientPromise;
|
|
83
|
+
})();
|
|
84
|
+
}
|
|
85
|
+
return this.readyPromise;
|
|
86
|
+
}
|
|
87
|
+
async initClient(options) {
|
|
88
|
+
return this.createOpenAIClient(options);
|
|
89
|
+
}
|
|
90
|
+
async createOpenAIClient(options = {}) {
|
|
91
|
+
// Verify the enclave and get the certificate fingerprint
|
|
92
|
+
const secureClient = new secure_client_1.SecureClient(this.enclave, this.repo);
|
|
93
|
+
try {
|
|
94
|
+
this.groundTruth = await secureClient.verify();
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
throw new Error(`Failed to verify enclave: ${error}`);
|
|
98
|
+
}
|
|
99
|
+
// Convert the expected fingerprint to hex string for comparison
|
|
100
|
+
const expectedFingerprint = Buffer.from(this.groundTruth.certFingerprint).toString('hex');
|
|
101
|
+
// Create a custom HTTPS agent that verifies certificate fingerprints
|
|
102
|
+
const httpsAgent = new https_1.default.Agent({
|
|
103
|
+
rejectUnauthorized: true,
|
|
104
|
+
checkServerIdentity: (host, cert) => {
|
|
105
|
+
if (!cert || !cert.raw) {
|
|
106
|
+
throw new Error('No certificate found');
|
|
107
|
+
}
|
|
108
|
+
// Calculate the SHA-256 fingerprint of the certificate and convert to hex
|
|
109
|
+
const certFingerprint = (0, crypto_1.createHash)('sha256').update(cert.raw).digest('hex');
|
|
110
|
+
// Compare hex strings
|
|
111
|
+
if (certFingerprint !== expectedFingerprint) {
|
|
112
|
+
throw new Error('Certificate fingerprint mismatch');
|
|
113
|
+
}
|
|
114
|
+
return undefined; // Validation successful
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
// Create the OpenAI client with our custom configuration
|
|
118
|
+
return new openai_1.default({
|
|
119
|
+
...options,
|
|
120
|
+
baseURL: `https://${this.enclave}/v1/`,
|
|
121
|
+
httpAgent: httpsAgent,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Helper method to ensure the OpenAI client is initialized before use.
|
|
126
|
+
* Automatically calls ready() if needed.
|
|
127
|
+
* @returns The initialized OpenAI client
|
|
128
|
+
* @private
|
|
129
|
+
*/
|
|
130
|
+
async ensureReady() {
|
|
131
|
+
await this.ready();
|
|
132
|
+
// We can safely assert this now because ready() must have completed
|
|
133
|
+
return this.client;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Access to OpenAI's chat completions API for creating chat conversations.
|
|
137
|
+
* Automatically initializes the client if needed.
|
|
138
|
+
*/
|
|
139
|
+
get chat() {
|
|
140
|
+
return createAsyncProxy(this.ensureReady().then(client => client.chat));
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Access to OpenAI's files API for managing files used with the API.
|
|
144
|
+
* Automatically initializes the client if needed.
|
|
145
|
+
*/
|
|
146
|
+
get files() {
|
|
147
|
+
return createAsyncProxy(this.ensureReady().then(client => client.files));
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Access to OpenAI's fine-tuning API for creating and managing fine-tuned models.
|
|
151
|
+
* Automatically initializes the client if needed.
|
|
152
|
+
*/
|
|
153
|
+
get fineTuning() {
|
|
154
|
+
return createAsyncProxy(this.ensureReady().then(client => client.fineTuning));
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Access to OpenAI's image generation and editing API.
|
|
158
|
+
* Automatically initializes the client if needed.
|
|
159
|
+
*/
|
|
160
|
+
get images() {
|
|
161
|
+
return createAsyncProxy(this.ensureReady().then(client => client.images));
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Access to OpenAI's audio API for speech-to-text and text-to-speech.
|
|
165
|
+
* Automatically initializes the client if needed.
|
|
166
|
+
*/
|
|
167
|
+
get audio() {
|
|
168
|
+
return createAsyncProxy(this.ensureReady().then(client => client.audio));
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Access to OpenAI's embeddings API for creating vector embeddings of text.
|
|
172
|
+
* Automatically initializes the client if needed.
|
|
173
|
+
*/
|
|
174
|
+
get embeddings() {
|
|
175
|
+
return createAsyncProxy(this.ensureReady().then(client => client.embeddings));
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Access to OpenAI's models API for listing and managing available models.
|
|
179
|
+
* Automatically initializes the client if needed.
|
|
180
|
+
*/
|
|
181
|
+
get models() {
|
|
182
|
+
return createAsyncProxy(this.ensureReady().then(client => client.models));
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Access to OpenAI's content moderation API.
|
|
186
|
+
* Automatically initializes the client if needed.
|
|
187
|
+
*/
|
|
188
|
+
get moderations() {
|
|
189
|
+
return createAsyncProxy(this.ensureReady().then(client => client.moderations));
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Access to OpenAI's beta features.
|
|
193
|
+
* Automatically initializes the client if needed.
|
|
194
|
+
*/
|
|
195
|
+
get beta() {
|
|
196
|
+
return createAsyncProxy(this.ensureReady().then(client => client.beta));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
exports.TinfoilAI = TinfoilAI;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./client"), exports);
|
|
18
|
+
__exportStar(require("./secure-client"), exports);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
var Go: any;
|
|
3
|
+
var verifyEnclave: (enclaveHostname: string) => Promise<{
|
|
4
|
+
certificate: string;
|
|
5
|
+
measurement: string;
|
|
6
|
+
certFingerprint: string;
|
|
7
|
+
}>;
|
|
8
|
+
var verifyCode: (repo: string, digest: string) => Promise<string>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Ground truth measurements from verification
|
|
12
|
+
*/
|
|
13
|
+
export interface GroundTruth {
|
|
14
|
+
certFingerprint: Uint8Array;
|
|
15
|
+
measurement: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* SecureClient handles verification of code and runtime measurements using WebAssembly
|
|
19
|
+
*/
|
|
20
|
+
export declare class SecureClient {
|
|
21
|
+
private enclave;
|
|
22
|
+
private repo;
|
|
23
|
+
private static goInstance;
|
|
24
|
+
private static initializationPromise;
|
|
25
|
+
constructor(enclave: string, repo: string);
|
|
26
|
+
/**
|
|
27
|
+
* Static method to initialize WASM module
|
|
28
|
+
* This starts automatically when the class is loaded
|
|
29
|
+
*/
|
|
30
|
+
static initializeWasm(): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Initialize the WASM module
|
|
33
|
+
* Now just waits for the static initialization to complete
|
|
34
|
+
*/
|
|
35
|
+
initialize(): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Verifies the integrity of both the code and runtime environment
|
|
38
|
+
*/
|
|
39
|
+
verify(): Promise<GroundTruth>;
|
|
40
|
+
}
|
|
@@ -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);
|