tinfoil 1.0.4 → 1.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/dist/ai-sdk-provider.d.ts +1 -1
- package/dist/ai-sdk-provider.d.ts.map +1 -1
- package/dist/ai-sdk-provider.js +5 -12
- package/dist/ai-sdk-provider.js.map +1 -1
- package/dist/atc.d.ts +4 -5
- package/dist/atc.d.ts.map +1 -1
- package/dist/atc.js +8 -8
- package/dist/atc.js.map +1 -1
- package/dist/config.d.ts +1 -1
- package/dist/config.js +1 -1
- package/dist/encrypted-body-fetch.d.ts +0 -1
- package/dist/encrypted-body-fetch.d.ts.map +1 -1
- package/dist/encrypted-body-fetch.js +5 -20
- package/dist/encrypted-body-fetch.js.map +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -4
- package/dist/index.js.map +1 -1
- package/dist/pinned-tls-fetch.browser.d.ts +0 -6
- package/dist/pinned-tls-fetch.browser.d.ts.map +1 -1
- package/dist/pinned-tls-fetch.browser.js +2 -1
- package/dist/pinned-tls-fetch.browser.js.map +1 -1
- package/dist/pinned-tls-fetch.d.ts.map +1 -1
- package/dist/pinned-tls-fetch.js +6 -5
- package/dist/pinned-tls-fetch.js.map +1 -1
- package/dist/secure-client.d.ts +47 -11
- package/dist/secure-client.d.ts.map +1 -1
- package/dist/secure-client.js +134 -114
- package/dist/secure-client.js.map +1 -1
- package/dist/secure-fetch.d.ts +1 -1
- package/dist/secure-fetch.d.ts.map +1 -1
- package/dist/secure-fetch.js +5 -5
- package/dist/secure-fetch.js.map +1 -1
- package/dist/tinfoil-ai.d.ts +12 -16
- package/dist/tinfoil-ai.d.ts.map +1 -1
- package/dist/tinfoil-ai.js +21 -55
- package/dist/tinfoil-ai.js.map +1 -1
- package/dist/unverified-client.d.ts.map +1 -1
- package/dist/unverified-client.js +6 -11
- package/dist/unverified-client.js.map +1 -1
- package/package.json +20 -16
package/dist/secure-client.js
CHANGED
|
@@ -1,7 +1,37 @@
|
|
|
1
|
-
import { Verifier } from "./verifier.js";
|
|
1
|
+
import { Verifier, assembleAttestationBundle, ConfigurationError, FetchError, AttestationError } from "./verifier.js";
|
|
2
2
|
import { TINFOIL_CONFIG } from "./config.js";
|
|
3
3
|
import { createSecureFetch } from "./secure-fetch.js";
|
|
4
4
|
import { fetchAttestationBundle } from "./atc.js";
|
|
5
|
+
/** Delay before retrying init on transient failure (ms). */
|
|
6
|
+
const INIT_RETRY_DELAY_MS = 1000;
|
|
7
|
+
/**
|
|
8
|
+
* Duck-typed check for EHBP KeyConfigMismatchError.
|
|
9
|
+
* TODO: Replace with direct import once ehbp publishes typed errors.
|
|
10
|
+
*/
|
|
11
|
+
function isKeyConfigMismatchError(error) {
|
|
12
|
+
return error instanceof Error && error.name === 'KeyConfigMismatchError';
|
|
13
|
+
}
|
|
14
|
+
function createPendingVerificationDocument(configRepo) {
|
|
15
|
+
return {
|
|
16
|
+
configRepo,
|
|
17
|
+
enclaveHost: '',
|
|
18
|
+
releaseDigest: '',
|
|
19
|
+
codeMeasurement: { type: '', registers: [] },
|
|
20
|
+
enclaveMeasurement: { measurement: { type: '', registers: [] } },
|
|
21
|
+
tlsPublicKey: '',
|
|
22
|
+
hpkePublicKey: '',
|
|
23
|
+
codeFingerprint: '',
|
|
24
|
+
enclaveFingerprint: '',
|
|
25
|
+
selectedRouterEndpoint: '',
|
|
26
|
+
securityVerified: false,
|
|
27
|
+
steps: {
|
|
28
|
+
fetchDigest: { status: 'pending' },
|
|
29
|
+
verifyCode: { status: 'pending' },
|
|
30
|
+
verifyEnclave: { status: 'pending' },
|
|
31
|
+
compareMeasurements: { status: 'pending' },
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
5
35
|
/**
|
|
6
36
|
* Low-level secure client providing a verified fetch function for custom HTTP requests.
|
|
7
37
|
*
|
|
@@ -40,19 +70,24 @@ import { fetchAttestationBundle } from "./atc.js";
|
|
|
40
70
|
* @see https://docs.tinfoil.sh/guides/proxy-server - Proxy server setup
|
|
41
71
|
*/
|
|
42
72
|
export class SecureClient {
|
|
43
|
-
initPromise = null;
|
|
44
|
-
verificationDocument = null;
|
|
45
|
-
_fetch = null;
|
|
46
|
-
baseURL;
|
|
47
|
-
enclaveURL;
|
|
48
|
-
configRepo;
|
|
49
|
-
transport;
|
|
50
|
-
attestationBundleURL;
|
|
51
73
|
constructor(options = {}) {
|
|
52
|
-
|
|
53
|
-
this.
|
|
54
|
-
this.
|
|
55
|
-
|
|
74
|
+
// --- Derived state (cleared on reset) ---
|
|
75
|
+
this.initPromise = null;
|
|
76
|
+
this._fetch = null;
|
|
77
|
+
if (options.configRepo && !options.enclaveURL) {
|
|
78
|
+
throw new ConfigurationError("configRepo requires enclaveURL — without it, ATC always uses the default router repo.");
|
|
79
|
+
}
|
|
80
|
+
else if (options.enclaveURL && !options.configRepo) {
|
|
81
|
+
console.warn(`[tinfoil] No configRepo specified, verifying against "${TINFOIL_CONFIG.DEFAULT_ROUTER_REPO}".`);
|
|
82
|
+
}
|
|
83
|
+
this.config = {
|
|
84
|
+
baseURL: options.baseURL,
|
|
85
|
+
enclaveURL: options.enclaveURL,
|
|
86
|
+
configRepo: options.configRepo ?? TINFOIL_CONFIG.DEFAULT_ROUTER_REPO,
|
|
87
|
+
transport: options.transport || 'ehbp',
|
|
88
|
+
attestationBundleURL: options.attestationBundleURL,
|
|
89
|
+
};
|
|
90
|
+
this.verificationDocument = createPendingVerificationDocument(this.config.configRepo);
|
|
56
91
|
}
|
|
57
92
|
/**
|
|
58
93
|
* Wait for the client to complete verification and be ready for requests.
|
|
@@ -64,104 +99,111 @@ export class SecureClient {
|
|
|
64
99
|
*/
|
|
65
100
|
async ready() {
|
|
66
101
|
if (!this.initPromise) {
|
|
67
|
-
this.initPromise = this.initSecureClient()
|
|
102
|
+
this.initPromise = this.initSecureClient().catch(async (err) => {
|
|
103
|
+
// Only try recovery if the error is transient (network I/O, attestation errors)
|
|
104
|
+
if (err instanceof FetchError || err instanceof AttestationError) {
|
|
105
|
+
this.clearDerivedState(); // Start with a new enclave
|
|
106
|
+
await new Promise(r => setTimeout(r, INIT_RETRY_DELAY_MS));
|
|
107
|
+
return this.initSecureClient().catch(retryErr => {
|
|
108
|
+
this.reset();
|
|
109
|
+
throw retryErr;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
// Everything else (ConfigurationError, bugs) — propagate immediately
|
|
113
|
+
this.reset();
|
|
114
|
+
throw err;
|
|
115
|
+
});
|
|
68
116
|
}
|
|
69
117
|
return this.initPromise;
|
|
70
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* Clear derived state without touching initPromise (preserves deduplication).
|
|
121
|
+
*/
|
|
122
|
+
clearDerivedState() {
|
|
123
|
+
this._fetch = null;
|
|
124
|
+
this.verificationDocument = createPendingVerificationDocument(this.config.configRepo);
|
|
125
|
+
this.resolvedEnclaveURL = undefined;
|
|
126
|
+
this.resolvedBaseURL = undefined;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Reset the client, clearing all verification state and transport.
|
|
130
|
+
*
|
|
131
|
+
* After calling reset(), the next call to `ready()` or `fetch()` will
|
|
132
|
+
* perform a fresh attestation and establish a new secure transport.
|
|
133
|
+
*
|
|
134
|
+
* Use this for retry logic when the enclave may have restarted with new keys,
|
|
135
|
+
* or when you want to force re-verification.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```typescript
|
|
139
|
+
* // Force re-attestation
|
|
140
|
+
* client.reset();
|
|
141
|
+
* await client.ready();
|
|
142
|
+
*
|
|
143
|
+
* // Or let it re-attest lazily on next request
|
|
144
|
+
* client.reset();
|
|
145
|
+
* await client.fetch("/v1/chat/completions", { ... });
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
reset() {
|
|
149
|
+
this.initPromise = null;
|
|
150
|
+
this.clearDerivedState();
|
|
151
|
+
}
|
|
71
152
|
async initSecureClient() {
|
|
72
|
-
|
|
73
|
-
this.enclaveURL
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
153
|
+
let bundle;
|
|
154
|
+
if (this.config.enclaveURL) {
|
|
155
|
+
// Custom enclave: assemble the bundle locally
|
|
156
|
+
const host = new URL(this.config.enclaveURL).host;
|
|
157
|
+
bundle = await assembleAttestationBundle(host, this.config.configRepo);
|
|
77
158
|
}
|
|
159
|
+
else {
|
|
160
|
+
// Default router: fetch pre-assembled bundle from ATC
|
|
161
|
+
bundle = await fetchAttestationBundle(this.config.attestationBundleURL);
|
|
162
|
+
}
|
|
163
|
+
// Resolve enclaveURL: user-provided config takes precedence, otherwise from bundle
|
|
164
|
+
this.resolvedEnclaveURL = this.config.enclaveURL ?? `https://${bundle.domain}`;
|
|
165
|
+
// Resolve baseURL: user-provided config (proxy) takes precedence, otherwise from enclave
|
|
166
|
+
this.resolvedBaseURL = this.config.baseURL ?? `${this.resolvedEnclaveURL}/v1/`;
|
|
78
167
|
const verifier = new Verifier({
|
|
79
|
-
configRepo: this.configRepo,
|
|
168
|
+
configRepo: this.config.configRepo,
|
|
80
169
|
});
|
|
81
170
|
try {
|
|
82
|
-
await verifier.verifyBundle(bundle);
|
|
83
|
-
|
|
84
|
-
if (!doc) {
|
|
85
|
-
throw new Error("Verification document not available after successful verification");
|
|
86
|
-
}
|
|
87
|
-
this.verificationDocument = doc;
|
|
88
|
-
// Extract keys from the verification document
|
|
89
|
-
const { hpkePublicKey, tlsPublicKeyFingerprint } = this.verificationDocument.enclaveMeasurement;
|
|
90
|
-
try {
|
|
91
|
-
this._fetch = await this.createTransport(hpkePublicKey, tlsPublicKeyFingerprint);
|
|
92
|
-
}
|
|
93
|
-
catch (transportError) {
|
|
94
|
-
this.verificationDocument.steps.createTransport = {
|
|
95
|
-
status: 'failed',
|
|
96
|
-
error: transportError.message
|
|
97
|
-
};
|
|
98
|
-
this.verificationDocument.securityVerified = false;
|
|
99
|
-
throw transportError;
|
|
100
|
-
}
|
|
171
|
+
const attestation = await verifier.verifyBundle(bundle);
|
|
172
|
+
this._fetch = await this.createTransport(attestation.hpkePublicKey, attestation.tlsPublicKeyFingerprint);
|
|
101
173
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
this.verificationDocument = doc;
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
this.verificationDocument = {
|
|
109
|
-
configRepo: this.configRepo,
|
|
110
|
-
enclaveHost: bundle.domain,
|
|
111
|
-
releaseDigest: '',
|
|
112
|
-
codeMeasurement: { type: '', registers: [] },
|
|
113
|
-
enclaveMeasurement: { measurement: { type: '', registers: [] } },
|
|
114
|
-
tlsPublicKey: '',
|
|
115
|
-
hpkePublicKey: '',
|
|
116
|
-
hardwareMeasurement: undefined,
|
|
117
|
-
codeFingerprint: '',
|
|
118
|
-
enclaveFingerprint: '',
|
|
119
|
-
selectedRouterEndpoint: bundle.domain,
|
|
120
|
-
securityVerified: false,
|
|
121
|
-
steps: {
|
|
122
|
-
fetchDigest: { status: 'pending' },
|
|
123
|
-
verifyCode: { status: 'pending' },
|
|
124
|
-
verifyEnclave: { status: 'pending' },
|
|
125
|
-
compareMeasurements: { status: 'pending' },
|
|
126
|
-
createTransport: undefined,
|
|
127
|
-
verifyHPKEKey: undefined,
|
|
128
|
-
otherError: { status: 'failed', error: error.message },
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
throw error;
|
|
174
|
+
finally {
|
|
175
|
+
// Always capture the verifier's doc (success or partial-failure)
|
|
176
|
+
this.verificationDocument = verifier.getVerificationDocument() ?? this.verificationDocument;
|
|
133
177
|
}
|
|
134
178
|
}
|
|
135
179
|
/**
|
|
136
180
|
* Get the verification document containing attestation details.
|
|
137
181
|
*
|
|
138
182
|
* @returns The verification document with attestation results
|
|
139
|
-
* @throws Error if verification has not completed
|
|
140
183
|
* @see https://docs.tinfoil.sh/verification/attestation-architecture
|
|
141
184
|
*/
|
|
142
|
-
|
|
143
|
-
if (!this.initPromise) {
|
|
144
|
-
await this.ready();
|
|
145
|
-
}
|
|
146
|
-
await this.initPromise.catch(() => { });
|
|
147
|
-
if (!this.verificationDocument) {
|
|
148
|
-
throw new Error("Verification document unavailable: client not verified yet");
|
|
149
|
-
}
|
|
185
|
+
getVerificationDocument() {
|
|
150
186
|
return this.verificationDocument;
|
|
151
187
|
}
|
|
152
188
|
/**
|
|
153
189
|
* Get the base URL for API requests.
|
|
154
190
|
*
|
|
155
|
-
*
|
|
191
|
+
* Returns the base URL requests will be sent to.
|
|
156
192
|
*/
|
|
157
193
|
getBaseURL() {
|
|
158
|
-
return this.
|
|
194
|
+
return this.resolvedBaseURL;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Get the URL of the enclave endpoint, or undefined before ready().
|
|
198
|
+
*/
|
|
199
|
+
getEnclaveURL() {
|
|
200
|
+
return this.resolvedEnclaveURL;
|
|
159
201
|
}
|
|
160
202
|
async createTransport(hpkePublicKey, tlsPublicKeyFingerprint) {
|
|
161
|
-
if (this.transport === 'tls') {
|
|
162
|
-
return await createSecureFetch(this.
|
|
203
|
+
if (this.config.transport === 'tls') {
|
|
204
|
+
return await createSecureFetch(this.resolvedBaseURL, undefined, tlsPublicKeyFingerprint, this.resolvedEnclaveURL);
|
|
163
205
|
}
|
|
164
|
-
return await createSecureFetch(this.
|
|
206
|
+
return await createSecureFetch(this.resolvedBaseURL, hpkePublicKey, undefined, this.resolvedEnclaveURL);
|
|
165
207
|
}
|
|
166
208
|
/**
|
|
167
209
|
* Secure fetch function that encrypts request bodies end-to-end.
|
|
@@ -170,6 +212,9 @@ export class SecureClient {
|
|
|
170
212
|
* encrypted using HPKE (or TLS pinning if configured) so only the verified
|
|
171
213
|
* enclave can decrypt them.
|
|
172
214
|
*
|
|
215
|
+
* On `KeyConfigMismatchError` (server key rotation), automatically re-attests
|
|
216
|
+
* and retries the request once. All other errors propagate to the caller.
|
|
217
|
+
*
|
|
173
218
|
* @example
|
|
174
219
|
* ```typescript
|
|
175
220
|
* const response = await client.fetch("/v1/chat/completions", {
|
|
@@ -186,36 +231,11 @@ export class SecureClient {
|
|
|
186
231
|
return await this._fetch(input, init);
|
|
187
232
|
}
|
|
188
233
|
catch (error) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
error: errorMessage
|
|
195
|
-
};
|
|
196
|
-
this.verificationDocument.securityVerified = false;
|
|
197
|
-
}
|
|
198
|
-
else if (errorMessage.includes('Transport initialization failed') || errorMessage.includes('Request initialization failed')) {
|
|
199
|
-
this.verificationDocument.steps.createTransport = {
|
|
200
|
-
status: 'failed',
|
|
201
|
-
error: errorMessage
|
|
202
|
-
};
|
|
203
|
-
this.verificationDocument.securityVerified = false;
|
|
204
|
-
}
|
|
205
|
-
else if (errorMessage.includes('Failed to get HPKE key')) {
|
|
206
|
-
this.verificationDocument.steps.verifyHPKEKey = {
|
|
207
|
-
status: 'failed',
|
|
208
|
-
error: errorMessage
|
|
209
|
-
};
|
|
210
|
-
this.verificationDocument.securityVerified = false;
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
this.verificationDocument.steps.otherError = {
|
|
214
|
-
status: 'failed',
|
|
215
|
-
error: errorMessage
|
|
216
|
-
};
|
|
217
|
-
this.verificationDocument.securityVerified = false;
|
|
218
|
-
}
|
|
234
|
+
// Channel recovery: server rotated keys, request was never processed — safe to retry
|
|
235
|
+
if (isKeyConfigMismatchError(error)) {
|
|
236
|
+
this.reset();
|
|
237
|
+
await this.ready();
|
|
238
|
+
return await this._fetch(input, init);
|
|
219
239
|
}
|
|
220
240
|
throw error;
|
|
221
241
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"secure-client.js","sourceRoot":"","sources":["../src/secure-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAA6B,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"secure-client.js","sourceRoot":"","sources":["../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AAC;IAED;;;;;;;;;;;;;;;;;;OAkBG;IACH,IAAI,KAAK;QACP,OAAO,KAAK,EAAE,KAAwB,EAAE,IAAkB,EAAE,EAAE;YAC5D,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;YAEnB,IAAI,CAAC;gBACH,OAAO,MAAM,IAAI,CAAC,MAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;YACzC,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,qFAAqF;gBACrF,IAAI,wBAAwB,CAAC,KAAK,CAAC,EAAE,CAAC;oBACpC,IAAI,CAAC,KAAK,EAAE,CAAC;oBACb,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;oBACnB,OAAO,MAAM,IAAI,CAAC,MAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;gBACzC,CAAC;gBACD,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC,CAAC;IACJ,CAAC;CACF"}
|
package/dist/secure-fetch.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* This is the unified implementation for both browser and server environments:
|
|
5
5
|
* - In browsers: Only HPKE encryption is supported (requires hpkePublicKey)
|
|
6
|
-
* - In Node.js/Bun:
|
|
6
|
+
* - In Node.js/Bun: Also supports TLS certificate pinning when configured
|
|
7
7
|
*
|
|
8
8
|
* All imports are dynamic to enable tree-shaking in browser bundles.
|
|
9
9
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"secure-fetch.d.ts","sourceRoot":"","sources":["../src/secure-fetch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"secure-fetch.d.ts","sourceRoot":"","sources":["../src/secure-fetch.ts"],"names":[],"mappings":"AAGA;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,MAAM,EACf,aAAa,CAAC,EAAE,MAAM,EACtB,uBAAuB,CAAC,EAAE,MAAM,EAChC,UAAU,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC,OAAO,KAAK,CAAC,CAuBvB"}
|
package/dist/secure-fetch.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
import { isRealBrowser } from "./env.js";
|
|
2
|
+
import { ConfigurationError } from "./verifier.js";
|
|
2
3
|
/**
|
|
3
4
|
* Creates a secure fetch function with either HPKE encryption or TLS pinning.
|
|
4
5
|
*
|
|
5
6
|
* This is the unified implementation for both browser and server environments:
|
|
6
7
|
* - In browsers: Only HPKE encryption is supported (requires hpkePublicKey)
|
|
7
|
-
* - In Node.js/Bun:
|
|
8
|
+
* - In Node.js/Bun: Also supports TLS certificate pinning when configured
|
|
8
9
|
*
|
|
9
10
|
* All imports are dynamic to enable tree-shaking in browser bundles.
|
|
10
11
|
*/
|
|
11
12
|
export async function createSecureFetch(baseURL, hpkePublicKey, tlsPublicKeyFingerprint, enclaveURL) {
|
|
12
13
|
if (hpkePublicKey) {
|
|
13
|
-
// Dynamic import to
|
|
14
|
-
// This prevents WebCrypto X25519 errors in runtimes that don't support it (like Bun).
|
|
14
|
+
// Dynamic import to enable tree-shaking in browser bundles.
|
|
15
15
|
const { createEncryptedBodyFetch } = await import("./encrypted-body-fetch.js");
|
|
16
16
|
return createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL);
|
|
17
17
|
}
|
|
18
18
|
if (isRealBrowser()) {
|
|
19
|
-
throw new
|
|
19
|
+
throw new ConfigurationError("HPKE public key not available and TLS-only verification is not supported in browsers. " +
|
|
20
20
|
"Only HPKE-enabled enclaves can be used in browser environments.");
|
|
21
21
|
}
|
|
22
22
|
if (!tlsPublicKeyFingerprint) {
|
|
23
|
-
throw new
|
|
23
|
+
throw new ConfigurationError("Neither HPKE public key nor TLS public key fingerprint available for verification");
|
|
24
24
|
}
|
|
25
25
|
// Dynamic import to avoid including Node.js crypto module in browser bundles
|
|
26
26
|
const { createPinnedTlsFetch } = await import("./pinned-tls-fetch.js");
|
package/dist/secure-fetch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"secure-fetch.js","sourceRoot":"","sources":["../src/secure-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"secure-fetch.js","sourceRoot":"","sources":["../src/secure-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAEnD;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,OAAe,EACf,aAAsB,EACtB,uBAAgC,EAChC,UAAmB;IAEnB,IAAI,aAAa,EAAE,CAAC;QAClB,4DAA4D;QAC5D,MAAM,EAAE,wBAAwB,EAAE,GAAG,MAAM,MAAM,CAAC,2BAA2B,CAAC,CAAC;QAC/E,OAAO,wBAAwB,CAAC,OAAO,EAAE,aAAa,EAAE,UAAU,CAAC,CAAC;IACtE,CAAC;IAED,IAAI,aAAa,EAAE,EAAE,CAAC;QACpB,MAAM,IAAI,kBAAkB,CAC1B,wFAAwF;YACxF,iEAAiE,CAClE,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,uBAAuB,EAAE,CAAC;QAC7B,MAAM,IAAI,kBAAkB,CAC1B,mFAAmF,CACpF,CAAC;IACJ,CAAC;IAED,6EAA6E;IAC7E,MAAM,EAAE,oBAAoB,EAAE,GAAG,MAAM,MAAM,CAAC,uBAAuB,CAAC,CAAC;IACvE,OAAO,oBAAoB,CAAC,OAAO,EAAE,uBAAuB,CAAC,CAAC;AAChE,CAAC"}
|
package/dist/tinfoil-ai.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import OpenAI from "openai";
|
|
2
2
|
import type { Audio, Beta, Chat, Embeddings, Files, FineTuning, Images, Models, Moderations, Responses } from "openai/resources";
|
|
3
3
|
import { type TransportMode } from "./secure-client.js";
|
|
4
|
-
import type
|
|
4
|
+
import { type VerificationDocument } from "./verifier.js";
|
|
5
5
|
/**
|
|
6
6
|
* Configuration options for TinfoilAI client.
|
|
7
7
|
*
|
|
@@ -35,16 +35,21 @@ export interface TinfoilAIOptions {
|
|
|
35
35
|
* @see https://docs.tinfoil.sh/guides/proxy-server
|
|
36
36
|
*/
|
|
37
37
|
baseURL?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Explicit enclave URL.
|
|
40
|
+
* Use this when connecting to a custom enclave rather than the default routers.
|
|
41
|
+
*/
|
|
42
|
+
enclaveURL?: string;
|
|
38
43
|
/** GitHub repo for code verification. Defaults to tinfoilsh/confidential-model-router. */
|
|
39
44
|
configRepo?: string;
|
|
40
45
|
/**
|
|
41
46
|
* Transport mode for secure communication.
|
|
42
47
|
* - 'ehbp': HPKE encryption via EHBP protocol (default)
|
|
43
|
-
* - 'tls': TLS certificate pinning
|
|
48
|
+
* - 'tls': TLS certificate pinning
|
|
44
49
|
* @default 'ehbp'
|
|
45
50
|
*/
|
|
46
51
|
transport?: TransportMode;
|
|
47
|
-
/** URL to fetch the attestation bundle from.
|
|
52
|
+
/** URL to fetch the attestation bundle from. */
|
|
48
53
|
attestationBundleURL?: string;
|
|
49
54
|
/** Additional OpenAI client options (passed through to underlying client) */
|
|
50
55
|
[key: string]: any;
|
|
@@ -74,31 +79,22 @@ export interface TinfoilAIOptions {
|
|
|
74
79
|
*/
|
|
75
80
|
export declare class TinfoilAI {
|
|
76
81
|
private client?;
|
|
77
|
-
private clientPromise;
|
|
78
|
-
private readyPromise?;
|
|
79
|
-
private configRepo?;
|
|
80
82
|
private secureClient;
|
|
81
|
-
private
|
|
82
|
-
private useBearerToken;
|
|
83
|
-
apiKey?: string;
|
|
84
|
-
bearerToken?: string;
|
|
85
|
-
baseURL?: string;
|
|
83
|
+
private openAIOptions;
|
|
86
84
|
constructor(options?: TinfoilAIOptions);
|
|
87
85
|
/**
|
|
88
86
|
* Wait for the client to complete verification and be ready for requests.
|
|
89
87
|
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
88
|
+
* All API methods (chat, audio, etc.) call this automatically — you only
|
|
89
|
+
* need this for UI loading states during verification.
|
|
92
90
|
*
|
|
93
91
|
* @example
|
|
94
92
|
* ```typescript
|
|
95
93
|
* const client = new TinfoilAI({ bearerToken: jwt });
|
|
96
|
-
* await client.ready(); //
|
|
97
|
-
* // Now make requests
|
|
94
|
+
* await client.ready(); // Show spinner while verifying
|
|
98
95
|
* ```
|
|
99
96
|
*/
|
|
100
97
|
ready(): Promise<void>;
|
|
101
|
-
private createOpenAIClient;
|
|
102
98
|
private ensureReady;
|
|
103
99
|
/**
|
|
104
100
|
* Get the verification document containing attestation details.
|
package/dist/tinfoil-ai.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tinfoil-ai.d.ts","sourceRoot":"","sources":["../src/tinfoil-ai.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,KAAK,EACV,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,UAAU,EACV,KAAK,EACL,UAAU,EACV,MAAM,EACN,MAAM,EACN,WAAW,EACX,SAAS,EACV,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAgB,KAAK,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"tinfoil-ai.d.ts","sourceRoot":"","sources":["../src/tinfoil-ai.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,KAAK,EACV,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,UAAU,EACV,KAAK,EACL,UAAU,EACV,MAAM,EACN,MAAM,EACN,WAAW,EACX,SAAS,EACV,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAgB,KAAK,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,KAAK,oBAAoB,EAAE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AS,CAAC;IACzB,MAAM,QAAQ,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;IACjC,MAAM,QAAQ,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;IACnC,MAAM,QAAQ,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;IACjC,MAAM,QAAQ,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IACvC,MAAM,QAAQ,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;IAC/C,MAAM,QAAQ,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;IAC7C,MAAM,QAAQ,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;IACnC,MAAM,QAAQ,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;IAC7C,MAAM,QAAQ,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IACrC,MAAM,QAAQ,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IACrC,MAAM,QAAQ,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;IAC/C,MAAM,QAAQ,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;IAC3C,MAAM,QAAQ,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IACvC,MAAM,QAAQ,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;CAClD"}
|
package/dist/tinfoil-ai.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import OpenAI from "openai";
|
|
2
2
|
import { SecureClient } from "./secure-client.js";
|
|
3
|
-
import {
|
|
3
|
+
import {} from "./verifier.js";
|
|
4
4
|
import { isRealBrowser } from "./env.js";
|
|
5
5
|
function createAsyncProxy(promise) {
|
|
6
6
|
return new Proxy({}, {
|
|
@@ -48,26 +48,13 @@ function createAsyncProxy(promise) {
|
|
|
48
48
|
* @see https://docs.tinfoil.sh/cc/how-it-works - How verification works
|
|
49
49
|
*/
|
|
50
50
|
export class TinfoilAI {
|
|
51
|
-
client;
|
|
52
|
-
clientPromise;
|
|
53
|
-
readyPromise;
|
|
54
|
-
configRepo;
|
|
55
|
-
secureClient;
|
|
56
|
-
verificationDocument;
|
|
57
|
-
useBearerToken;
|
|
58
|
-
apiKey;
|
|
59
|
-
bearerToken;
|
|
60
|
-
baseURL;
|
|
61
51
|
constructor(options = {}) {
|
|
62
52
|
const openAIOptions = { ...options };
|
|
63
|
-
// bearerToken is used for browser auth (e.g., JWT from your auth system)
|
|
64
|
-
// It automatically enables browser usage without dangerouslyAllowBrowser
|
|
65
|
-
this.useBearerToken = !!options.bearerToken;
|
|
66
53
|
// In browser builds, never read secrets from process.env to avoid
|
|
67
54
|
// leaking credentials into client bundles. Require explicit apiKey or bearerToken.
|
|
68
55
|
if (options.bearerToken) {
|
|
69
56
|
openAIOptions.apiKey = options.bearerToken;
|
|
70
|
-
|
|
57
|
+
openAIOptions.dangerouslyAllowBrowser = true;
|
|
71
58
|
}
|
|
72
59
|
else if (options.apiKey) {
|
|
73
60
|
openAIOptions.apiKey = options.apiKey;
|
|
@@ -75,60 +62,42 @@ export class TinfoilAI {
|
|
|
75
62
|
else if (!isRealBrowser() && typeof process !== 'undefined' && process.env.TINFOIL_API_KEY) {
|
|
76
63
|
openAIOptions.apiKey = process.env.TINFOIL_API_KEY;
|
|
77
64
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
65
|
+
if (options.dangerouslyAllowBrowser === true) {
|
|
66
|
+
openAIOptions.dangerouslyAllowBrowser = true;
|
|
67
|
+
}
|
|
68
|
+
this.openAIOptions = openAIOptions;
|
|
81
69
|
this.secureClient = new SecureClient({
|
|
82
|
-
baseURL:
|
|
83
|
-
|
|
70
|
+
baseURL: options.baseURL,
|
|
71
|
+
enclaveURL: options.enclaveURL,
|
|
72
|
+
configRepo: options.configRepo,
|
|
84
73
|
transport: options.transport,
|
|
85
74
|
attestationBundleURL: options.attestationBundleURL,
|
|
86
75
|
});
|
|
87
|
-
this.clientPromise = this.createOpenAIClient(openAIOptions);
|
|
88
76
|
}
|
|
89
77
|
/**
|
|
90
78
|
* Wait for the client to complete verification and be ready for requests.
|
|
91
79
|
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
80
|
+
* All API methods (chat, audio, etc.) call this automatically — you only
|
|
81
|
+
* need this for UI loading states during verification.
|
|
94
82
|
*
|
|
95
83
|
* @example
|
|
96
84
|
* ```typescript
|
|
97
85
|
* const client = new TinfoilAI({ bearerToken: jwt });
|
|
98
|
-
* await client.ready(); //
|
|
99
|
-
* // Now make requests
|
|
86
|
+
* await client.ready(); // Show spinner while verifying
|
|
100
87
|
* ```
|
|
101
88
|
*/
|
|
102
89
|
async ready() {
|
|
103
|
-
|
|
104
|
-
this.readyPromise = (async () => {
|
|
105
|
-
this.client = await this.clientPromise;
|
|
106
|
-
})();
|
|
107
|
-
}
|
|
108
|
-
return this.readyPromise;
|
|
90
|
+
await this.ensureReady();
|
|
109
91
|
}
|
|
110
|
-
async
|
|
92
|
+
async ensureReady() {
|
|
111
93
|
await this.secureClient.ready();
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const clientOptions = {
|
|
119
|
-
...options,
|
|
120
|
-
baseURL: baseURL,
|
|
121
|
-
fetch: this.secureClient.fetch,
|
|
122
|
-
};
|
|
123
|
-
// Automatically allow browser usage when bearerToken is used (e.g., JWT auth)
|
|
124
|
-
// or when explicitly requested via dangerouslyAllowBrowser
|
|
125
|
-
if (this.useBearerToken || options.dangerouslyAllowBrowser === true) {
|
|
126
|
-
clientOptions.dangerouslyAllowBrowser = true;
|
|
94
|
+
if (!this.client) {
|
|
95
|
+
this.client = new OpenAI({
|
|
96
|
+
...this.openAIOptions,
|
|
97
|
+
baseURL: this.secureClient.getBaseURL(),
|
|
98
|
+
fetch: this.secureClient.fetch,
|
|
99
|
+
});
|
|
127
100
|
}
|
|
128
|
-
return new OpenAI(clientOptions);
|
|
129
|
-
}
|
|
130
|
-
async ensureReady() {
|
|
131
|
-
await this.ready();
|
|
132
101
|
return this.client;
|
|
133
102
|
}
|
|
134
103
|
/**
|
|
@@ -151,10 +120,7 @@ export class TinfoilAI {
|
|
|
151
120
|
*/
|
|
152
121
|
async getVerificationDocument() {
|
|
153
122
|
await this.ready();
|
|
154
|
-
|
|
155
|
-
throw new Error("Verification document unavailable: client not verified yet");
|
|
156
|
-
}
|
|
157
|
-
return this.verificationDocument;
|
|
123
|
+
return this.secureClient.getVerificationDocument();
|
|
158
124
|
}
|
|
159
125
|
get chat() {
|
|
160
126
|
return createAsyncProxy(this.ensureReady().then((client) => client.chat));
|
package/dist/tinfoil-ai.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tinfoil-ai.js","sourceRoot":"","sources":["../src/tinfoil-ai.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAa5B,OAAO,EAAE,YAAY,EAAsB,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"tinfoil-ai.js","sourceRoot":"","sources":["../src/tinfoil-ai.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAa5B,OAAO,EAAE,YAAY,EAAsB,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAA6B,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,SAAS,gBAAgB,CAAmB,OAAmB;IAC7D,OAAO,IAAI,KAAK,CAAC,EAAO,EAAE;QACxB,GAAG,CAAC,MAAM,EAAE,IAAI;YACd,OAAO,IAAI,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,EAAE;gBACzB,GAAG,CAAC,CAAC,EAAE,UAAU;oBACf,OAAO,CAAC,GAAG,IAAW,EAAE,EAAE,CACxB,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE;wBACnB,MAAM,KAAK,GAAI,GAAW,CAAC,IAAI,CAAC,CAAC,UAAU,CAAC,CAAC;wBAC7C,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