tinfoil 0.9.1 → 0.10.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 (45) hide show
  1. package/README.md +34 -0
  2. package/dist/ai-sdk-provider.js +8 -3
  3. package/dist/config.d.ts +4 -11
  4. package/dist/config.js +4 -11
  5. package/dist/encrypted-body-fetch.d.ts +3 -5
  6. package/dist/encrypted-body-fetch.js +34 -93
  7. package/dist/esm/{ai-sdk-provider.js → ai-sdk-provider.mjs} +10 -5
  8. package/dist/esm/config.d.ts +4 -11
  9. package/dist/esm/config.mjs +13 -0
  10. package/dist/esm/encrypted-body-fetch.d.ts +3 -5
  11. package/dist/esm/{encrypted-body-fetch.js → encrypted-body-fetch.mjs} +29 -54
  12. package/dist/esm/index.browser.mjs +8 -0
  13. package/dist/esm/index.d.ts +1 -0
  14. package/dist/esm/{index.js → index.mjs} +8 -7
  15. package/dist/esm/router.d.ts +11 -0
  16. package/dist/esm/router.mjs +33 -0
  17. package/dist/esm/secure-client.d.ts +3 -2
  18. package/dist/esm/secure-client.mjs +153 -0
  19. package/dist/esm/{secure-fetch.browser.js → secure-fetch.browser.mjs} +1 -1
  20. package/dist/esm/{secure-fetch.js → secure-fetch.mjs} +3 -3
  21. package/dist/esm/{tinfoilai.js → tinfoilai.mjs} +8 -6
  22. package/dist/esm/unverified-client.d.ts +2 -2
  23. package/dist/esm/unverified-client.mjs +61 -0
  24. package/dist/esm/verifier.d.ts +4 -1
  25. package/dist/esm/{verifier.js → verifier.mjs} +18 -6
  26. package/dist/esm/wasm-exec.mjs +668 -0
  27. package/dist/index.d.ts +1 -0
  28. package/dist/index.js +3 -1
  29. package/dist/router.d.ts +11 -0
  30. package/dist/router.js +36 -0
  31. package/dist/secure-client.d.ts +3 -2
  32. package/dist/secure-client.js +116 -13
  33. package/dist/tinfoilai.js +5 -3
  34. package/dist/unverified-client.d.ts +2 -2
  35. package/dist/unverified-client.js +30 -2
  36. package/dist/verifier.d.ts +4 -1
  37. package/dist/verifier.js +16 -4
  38. package/package.json +15 -15
  39. package/dist/esm/config.js +0 -20
  40. package/dist/esm/index.browser.js +0 -8
  41. package/dist/esm/secure-client.js +0 -50
  42. package/dist/esm/unverified-client.js +0 -33
  43. /package/dist/esm/__tests__/{test-utils.js → test-utils.mjs} +0 -0
  44. /package/dist/esm/{env.js → env.mjs} +0 -0
  45. /package/dist/esm/{pinned-tls-fetch.js → pinned-tls-fetch.mjs} +0 -0
@@ -0,0 +1,153 @@
1
+ import { Verifier } from "./verifier.mjs";
2
+ import { TINFOIL_CONFIG } from "./config.mjs";
3
+ import { createSecureFetch } from "tinfoil/secure-fetch";
4
+ import { fetchRouter } from "./router.mjs";
5
+ export class SecureClient {
6
+ constructor(options = {}) {
7
+ this.initPromise = null;
8
+ this.verificationDocument = null;
9
+ this._fetch = null;
10
+ this.baseURL = options.baseURL;
11
+ this.enclaveURL = options.enclaveURL;
12
+ this.configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
13
+ }
14
+ async ready() {
15
+ if (!this.initPromise) {
16
+ this.initPromise = this.initSecureClient();
17
+ }
18
+ return this.initPromise;
19
+ }
20
+ async initSecureClient() {
21
+ // Only fetch router if neither baseURL nor enclaveURL is provided
22
+ if (!this.baseURL && !this.enclaveURL) {
23
+ const routerAddress = await fetchRouter();
24
+ this.enclaveURL = `https://${routerAddress}`;
25
+ this.baseURL = `https://${routerAddress}/v1/`;
26
+ }
27
+ // Ensure both baseURL and enclaveURL are initialized
28
+ if (!this.baseURL) {
29
+ if (this.enclaveURL) {
30
+ // If enclaveURL is provided but baseURL is not, derive baseURL from enclaveURL
31
+ const enclaveUrl = new URL(this.enclaveURL);
32
+ this.baseURL = `${enclaveUrl.origin}/v1/`;
33
+ }
34
+ else {
35
+ throw new Error("Unable to determine baseURL: neither baseURL nor enclaveURL provided");
36
+ }
37
+ }
38
+ if (!this.enclaveURL) {
39
+ if (this.baseURL) {
40
+ // If baseURL is provided but enclaveURL is not, derive enclaveURL from baseURL
41
+ const baseUrl = new URL(this.baseURL);
42
+ this.enclaveURL = baseUrl.origin;
43
+ }
44
+ else {
45
+ throw new Error("Unable to determine enclaveURL: neither baseURL nor enclaveURL provided");
46
+ }
47
+ }
48
+ const verifier = new Verifier({
49
+ serverURL: this.enclaveURL,
50
+ configRepo: this.configRepo,
51
+ });
52
+ try {
53
+ await verifier.verify();
54
+ const doc = verifier.getVerificationDocument();
55
+ if (!doc) {
56
+ throw new Error("Verification document not available after successful verification");
57
+ }
58
+ this.verificationDocument = doc;
59
+ // Extract keys from the verification document
60
+ const { hpkePublicKey, tlsPublicKeyFingerprint } = this.verificationDocument.enclaveMeasurement;
61
+ try {
62
+ this._fetch = createSecureFetch(this.baseURL, this.enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint);
63
+ }
64
+ catch (transportError) {
65
+ this.verificationDocument.steps.createTransport = {
66
+ status: 'failed',
67
+ error: transportError.message
68
+ };
69
+ this.verificationDocument.securityVerified = false;
70
+ throw transportError;
71
+ }
72
+ }
73
+ catch (error) {
74
+ const doc = verifier.getVerificationDocument();
75
+ if (doc) {
76
+ this.verificationDocument = doc;
77
+ }
78
+ else {
79
+ this.verificationDocument = {
80
+ configRepo: this.configRepo,
81
+ enclaveHost: new URL(this.enclaveURL).hostname,
82
+ releaseDigest: '',
83
+ codeMeasurement: { type: '', registers: [] },
84
+ enclaveMeasurement: { measurement: { type: '', registers: [] } },
85
+ securityVerified: false,
86
+ steps: {
87
+ fetchDigest: { status: 'pending' },
88
+ verifyCode: { status: 'pending' },
89
+ verifyEnclave: { status: 'pending' },
90
+ compareMeasurements: { status: 'pending' },
91
+ otherError: { status: 'failed', error: error.message }
92
+ }
93
+ };
94
+ }
95
+ throw error;
96
+ }
97
+ }
98
+ async getVerificationDocument() {
99
+ if (!this.initPromise) {
100
+ await this.ready();
101
+ }
102
+ await this.initPromise.catch(() => { });
103
+ if (!this.verificationDocument) {
104
+ throw new Error("Verification document unavailable: client not verified yet");
105
+ }
106
+ return this.verificationDocument;
107
+ }
108
+ getBaseURL() {
109
+ return this.baseURL;
110
+ }
111
+ get fetch() {
112
+ return async (input, init) => {
113
+ await this.ready();
114
+ try {
115
+ return await this._fetch(input, init);
116
+ }
117
+ catch (error) {
118
+ if (this.verificationDocument) {
119
+ const errorMessage = error.message;
120
+ if (errorMessage.includes('HPKE public key mismatch')) {
121
+ this.verificationDocument.steps.verifyHPKEKey = {
122
+ status: 'failed',
123
+ error: errorMessage
124
+ };
125
+ this.verificationDocument.securityVerified = false;
126
+ }
127
+ else if (errorMessage.includes('Transport initialization failed') || errorMessage.includes('Request initialization failed')) {
128
+ this.verificationDocument.steps.createTransport = {
129
+ status: 'failed',
130
+ error: errorMessage
131
+ };
132
+ this.verificationDocument.securityVerified = false;
133
+ }
134
+ else if (errorMessage.includes('Failed to get HPKE key')) {
135
+ this.verificationDocument.steps.verifyHPKEKey = {
136
+ status: 'failed',
137
+ error: errorMessage
138
+ };
139
+ this.verificationDocument.securityVerified = false;
140
+ }
141
+ else {
142
+ this.verificationDocument.steps.otherError = {
143
+ status: 'failed',
144
+ error: errorMessage
145
+ };
146
+ this.verificationDocument.securityVerified = false;
147
+ }
148
+ }
149
+ throw error;
150
+ }
151
+ };
152
+ }
153
+ }
@@ -1,4 +1,4 @@
1
- import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
1
+ import { createEncryptedBodyFetch } from "./encrypted-body-fetch.mjs";
2
2
  export function createSecureFetch(baseURL, enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint) {
3
3
  if (hpkePublicKey) {
4
4
  return createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL);
@@ -1,6 +1,6 @@
1
- import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
2
- import { createPinnedTlsFetch } from "./pinned-tls-fetch";
3
- import { isRealBrowser } from "./env";
1
+ import { createEncryptedBodyFetch } from "./encrypted-body-fetch.mjs";
2
+ import { createPinnedTlsFetch } from "./pinned-tls-fetch.mjs";
3
+ import { isRealBrowser } from "./env.mjs";
4
4
  export function createSecureFetch(baseURL, enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint) {
5
5
  let fetchFunction;
6
6
  if (hpkePublicKey) {
@@ -1,7 +1,7 @@
1
1
  import OpenAI from "openai";
2
- import { SecureClient } from "./secure-client";
3
- import { TINFOIL_CONFIG } from "./config";
4
- import { isRealBrowser } from "./env";
2
+ import { SecureClient } from "./secure-client.mjs";
3
+ import { TINFOIL_CONFIG } from "./config.mjs";
4
+ import { isRealBrowser } from "./env.mjs";
5
5
  function createAsyncProxy(promise) {
6
6
  return new Proxy({}, {
7
7
  get(target, prop) {
@@ -36,8 +36,8 @@ export class TinfoilAI {
36
36
  openAIOptions.apiKey = process.env.TINFOIL_API_KEY;
37
37
  }
38
38
  this.apiKey = openAIOptions.apiKey;
39
- this.baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
40
- this.enclaveURL = options.enclaveURL || TINFOIL_CONFIG.ENCLAVE_URL;
39
+ this.baseURL = options.baseURL;
40
+ this.enclaveURL = options.enclaveURL;
41
41
  this.configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
42
42
  this.secureClient = new SecureClient({
43
43
  baseURL: this.baseURL,
@@ -63,9 +63,11 @@ export class TinfoilAI {
63
63
  if (!this.verificationDocument) {
64
64
  throw new Error("Verification document not available after successful verification");
65
65
  }
66
+ // Use the provided baseURL, or get it from SecureClient after initialization
67
+ const baseURL = this.baseURL || this.secureClient.getBaseURL();
66
68
  const clientOptions = {
67
69
  ...options,
68
- baseURL: this.baseURL,
70
+ baseURL: baseURL,
69
71
  fetch: this.secureClient.fetch,
70
72
  };
71
73
  if (isRealBrowser() || options.dangerouslyAllowBrowser === true) {
@@ -6,8 +6,8 @@ interface UnverifiedClientOptions {
6
6
  export declare class UnverifiedClient {
7
7
  private initPromise;
8
8
  private _fetch;
9
- private readonly baseURL;
10
- private readonly enclaveURL;
9
+ private baseURL?;
10
+ private enclaveURL?;
11
11
  private readonly configRepo;
12
12
  constructor(options?: UnverifiedClientOptions);
13
13
  ready(): Promise<void>;
@@ -0,0 +1,61 @@
1
+ import { TINFOIL_CONFIG } from "./config.mjs";
2
+ import { createEncryptedBodyFetch } from "./encrypted-body-fetch.mjs";
3
+ import { fetchRouter } from "./router.mjs";
4
+ export class UnverifiedClient {
5
+ constructor(options = {}) {
6
+ this.initPromise = null;
7
+ this._fetch = null;
8
+ this.baseURL = options.baseURL;
9
+ this.enclaveURL = options.enclaveURL;
10
+ this.configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
11
+ }
12
+ async ready() {
13
+ if (!this.initPromise) {
14
+ this.initPromise = this.initUnverifiedClient();
15
+ }
16
+ return this.initPromise;
17
+ }
18
+ async initUnverifiedClient() {
19
+ // Only fetch router if neither baseURL nor enclaveURL is provided
20
+ if (!this.baseURL && !this.enclaveURL) {
21
+ const routerAddress = await fetchRouter();
22
+ this.enclaveURL = `https://${routerAddress}`;
23
+ this.baseURL = `https://${routerAddress}/v1/`;
24
+ }
25
+ // Ensure both baseURL and enclaveURL are initialized
26
+ if (!this.baseURL) {
27
+ if (this.enclaveURL) {
28
+ // If enclaveURL is provided but baseURL is not, derive baseURL from enclaveURL
29
+ const enclaveUrl = new URL(this.enclaveURL);
30
+ this.baseURL = `${enclaveUrl.origin}/v1/`;
31
+ }
32
+ else {
33
+ throw new Error("Unable to determine baseURL: neither baseURL nor enclaveURL provided");
34
+ }
35
+ }
36
+ if (!this.enclaveURL) {
37
+ if (this.baseURL) {
38
+ // If baseURL is provided but enclaveURL is not, derive enclaveURL from baseURL
39
+ const baseUrl = new URL(this.baseURL);
40
+ this.enclaveURL = baseUrl.origin;
41
+ }
42
+ else {
43
+ throw new Error("Unable to determine enclaveURL: neither baseURL nor enclaveURL provided");
44
+ }
45
+ }
46
+ this._fetch = createEncryptedBodyFetch(this.baseURL, undefined, this.enclaveURL);
47
+ }
48
+ async getVerificationDocument() {
49
+ if (!this.initPromise) {
50
+ await this.ready();
51
+ }
52
+ await this.initPromise;
53
+ throw new Error("Verification document unavailable: this version of the client is unverified");
54
+ }
55
+ get fetch() {
56
+ return async (input, init) => {
57
+ await this.ready();
58
+ return this._fetch(input, init);
59
+ };
60
+ }
61
+ }
@@ -37,6 +37,9 @@ export interface VerificationDocument {
37
37
  verifyCode: VerificationStepState;
38
38
  verifyEnclave: VerificationStepState;
39
39
  compareMeasurements: VerificationStepState;
40
+ createTransport?: VerificationStepState;
41
+ verifyHPKEKey?: VerificationStepState;
42
+ otherError?: VerificationStepState;
40
43
  };
41
44
  }
42
45
  export interface MeasurementComparisonResult {
@@ -84,7 +87,7 @@ export declare class Verifier {
84
87
  private static executeWithWasm;
85
88
  /**
86
89
  * Fetch the latest release digest from GitHub
87
- * @param configRepo - Repository name (e.g., "tinfoilsh/confidential-inference-proxy")
90
+ * @param configRepo - Repository name (e.g., "tinfoilsh/confidential-model-router")
88
91
  * @returns The digest hash
89
92
  */
90
93
  fetchLatestDigest(configRepo?: string): Promise<string>;
@@ -50,7 +50,7 @@
50
50
  * - `fetchLatestDigest(configRepo?)` → release digest via proxy
51
51
  * - `suppressWasmLogs(suppress?)` → control WASM log output
52
52
  */
53
- import { TINFOIL_CONFIG } from "./config";
53
+ import { TINFOIL_CONFIG } from "./config.mjs";
54
54
  // Use native fetch and TextEncoder/TextDecoder
55
55
  // In Node.js, these are available globally since v18
56
56
  // In browsers, they're also available globally
@@ -124,7 +124,8 @@ function compareMeasurementsError(codeMeasurement, runtimeMeasurement) {
124
124
  }
125
125
  if (codeMeasurement.type === PLATFORM_TYPES.SNP_TDX_MULTI_PLATFORM_V1) {
126
126
  switch (runtimeMeasurement.type) {
127
- case PLATFORM_TYPES.TDX_GUEST_V1: {
127
+ case PLATFORM_TYPES.TDX_GUEST_V1:
128
+ case PLATFORM_TYPES.TDX_GUEST_V2: {
128
129
  if (codeMeasurement.registers.length < 3 || runtimeMeasurement.registers.length < 4) {
129
130
  return new Error(MEASUREMENT_ERROR_MESSAGES.FEW_REGISTERS);
130
131
  }
@@ -194,8 +195,10 @@ export function compareMeasurements(codeMeasurement, runtimeMeasurement) {
194
195
  */
195
196
  export class Verifier {
196
197
  constructor(options) {
197
- const serverURL = options?.serverURL ?? TINFOIL_CONFIG.INFERENCE_BASE_URL;
198
- this.serverURL = new URL(serverURL).hostname;
198
+ if (!options?.serverURL) {
199
+ throw new Error("serverURL is required for Verifier");
200
+ }
201
+ this.serverURL = new URL(options.serverURL).hostname;
199
202
  this.configRepo = options?.configRepo ?? TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
200
203
  }
201
204
  /**
@@ -256,7 +259,7 @@ export class Verifier {
256
259
  }
257
260
  /**
258
261
  * Fetch the latest release digest from GitHub
259
- * @param configRepo - Repository name (e.g., "tinfoilsh/confidential-inference-proxy")
262
+ * @param configRepo - Repository name (e.g., "tinfoilsh/confidential-model-router")
260
263
  * @returns The digest hash
261
264
  */
262
265
  async fetchLatestDigest(configRepo) {
@@ -439,6 +442,15 @@ export class Verifier {
439
442
  }
440
443
  catch (error) {
441
444
  steps.fetchDigest = { status: 'failed', error: error.message };
445
+ this.lastVerificationDocument = {
446
+ configRepo: this.configRepo,
447
+ enclaveHost: this.serverURL,
448
+ releaseDigest: '',
449
+ codeMeasurement: { type: '', registers: [] },
450
+ enclaveMeasurement: { measurement: { type: '', registers: [] } },
451
+ securityVerified: false,
452
+ steps,
453
+ };
442
454
  throw error;
443
455
  }
444
456
  try {
@@ -693,7 +705,7 @@ function loadWasmExec() {
693
705
  // If that fails (e.g., pure Node without bundler), fall back to require.
694
706
  try {
695
707
  // @ts-expect-error: Local JS helper has no TS types; ambient module declared in wasm-exec.d.ts
696
- await import("./wasm-exec.js");
708
+ await import("./wasm-exec.mjs");
697
709
  }
698
710
  catch {
699
711
  if (nodeRequire) {