tinfoil 0.8.0 → 0.8.2

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 (51) hide show
  1. package/README.md +171 -8
  2. package/dist/__tests__/test-utils.d.ts +1 -0
  3. package/dist/__tests__/test-utils.js +44 -0
  4. package/dist/ai-sdk-provider.browser.d.ts +6 -0
  5. package/dist/ai-sdk-provider.browser.js +36 -0
  6. package/dist/ai-sdk-provider.d.ts +9 -1
  7. package/dist/ai-sdk-provider.js +154 -32
  8. package/dist/client.browser.d.ts +51 -0
  9. package/dist/client.browser.js +157 -0
  10. package/dist/client.d.ts +20 -4
  11. package/dist/client.js +175 -57
  12. package/dist/encrypted-body-fetch.d.ts +10 -0
  13. package/dist/encrypted-body-fetch.js +158 -0
  14. package/dist/env.d.ts +5 -0
  15. package/dist/env.js +20 -0
  16. package/dist/esm/__tests__/test-utils.d.ts +1 -0
  17. package/dist/esm/__tests__/test-utils.js +38 -0
  18. package/dist/esm/ai-sdk-provider.browser.d.ts +6 -0
  19. package/dist/esm/ai-sdk-provider.browser.js +33 -0
  20. package/dist/esm/ai-sdk-provider.d.ts +15 -0
  21. package/dist/esm/ai-sdk-provider.js +165 -0
  22. package/dist/esm/client.browser.d.ts +51 -0
  23. package/dist/esm/client.browser.js +150 -0
  24. package/dist/esm/client.d.ts +119 -0
  25. package/dist/esm/client.js +357 -0
  26. package/dist/esm/config.d.ts +13 -0
  27. package/dist/esm/config.js +13 -0
  28. package/dist/esm/encrypted-body-fetch.d.ts +10 -0
  29. package/dist/esm/encrypted-body-fetch.js +118 -0
  30. package/dist/esm/env.d.ts +5 -0
  31. package/dist/esm/env.js +17 -0
  32. package/dist/esm/index.browser.d.ts +6 -0
  33. package/dist/esm/index.browser.js +7 -0
  34. package/dist/esm/index.d.ts +7 -0
  35. package/dist/esm/index.js +11 -0
  36. package/dist/esm/verification-runner.d.ts +100 -0
  37. package/dist/esm/verification-runner.js +224 -0
  38. package/dist/esm/verifier.d.ts +120 -0
  39. package/dist/esm/verifier.js +686 -0
  40. package/dist/esm/wasm-exec.js +668 -0
  41. package/dist/index.browser.d.ts +6 -0
  42. package/dist/index.browser.js +26 -0
  43. package/dist/index.d.ts +2 -1
  44. package/dist/index.js +3 -2
  45. package/dist/verification-runner.d.ts +100 -0
  46. package/dist/verification-runner.js +230 -0
  47. package/dist/verifier.d.ts +120 -0
  48. package/dist/verifier.js +726 -0
  49. package/package.json +50 -9
  50. package/dist/secure-client.d.ts +0 -31
  51. package/dist/secure-client.js +0 -153
package/dist/index.js CHANGED
@@ -20,8 +20,9 @@ var client_1 = require("./client");
20
20
  Object.defineProperty(exports, "TinfoilAI", { enumerable: true, get: function () { return client_1.TinfoilAI; } });
21
21
  var client_2 = require("./client");
22
22
  Object.defineProperty(exports, "default", { enumerable: true, get: function () { return client_2.TinfoilAI; } });
23
- // Export secure client and config
24
- __exportStar(require("./secure-client"), exports);
23
+ // Export verifier and loader
24
+ __exportStar(require("./verifier"), exports);
25
+ __exportStar(require("./verification-runner"), exports);
25
26
  __exportStar(require("./ai-sdk-provider"), exports);
26
27
  __exportStar(require("./config"), exports);
27
28
  // Re-export OpenAI utility types and classes that users might need
@@ -0,0 +1,100 @@
1
+ import { Verifier, suppressWasmLogs } from "./verifier";
2
+ import type { AttestationMeasurement } from "./verifier";
3
+ import { TINFOIL_CONFIG } from "./config";
4
+ /**
5
+ * Step status for verification progress
6
+ */
7
+ export type StepStatus = "pending" | "loading" | "success" | "error";
8
+ /**
9
+ * State for individual verification steps
10
+ */
11
+ export interface StepState {
12
+ status: StepStatus;
13
+ measurement?: AttestationMeasurement;
14
+ error?: string;
15
+ }
16
+ /**
17
+ * Runtime verification state with additional key information
18
+ */
19
+ export interface RuntimeStepState extends StepState {
20
+ tlsPublicKeyFingerprint?: string;
21
+ hpkePublicKey?: string;
22
+ }
23
+ /**
24
+ * Final verification summary
25
+ */
26
+ export interface VerificationSummary {
27
+ status: StepStatus;
28
+ securityVerified?: boolean;
29
+ error?: string;
30
+ }
31
+ /**
32
+ * Overall verification state
33
+ */
34
+ export interface VerificationState {
35
+ releaseDigest: string;
36
+ runtime: RuntimeStepState;
37
+ code: StepState;
38
+ verification: VerificationSummary;
39
+ }
40
+ /**
41
+ * Verification result returned by runVerification
42
+ */
43
+ export interface VerificationResult extends VerificationState {
44
+ }
45
+ /**
46
+ * Options for runVerification method
47
+ */
48
+ export interface RunVerificationOptions {
49
+ /** GitHub repository to verify. Defaults to TINFOIL_CONFIG.INFERENCE_PROXY_REPO */
50
+ configRepo?: string;
51
+ /** Enclave hostname to verify. Defaults to hostname from TINFOIL_CONFIG.INFERENCE_BASE_URL */
52
+ serverURL?: string;
53
+ /** Specific release digest to verify. If not provided, fetches latest release digest */
54
+ releaseDigest?: string;
55
+ /** Callback for receiving verification state updates */
56
+ onUpdate?: (state: VerificationState) => void;
57
+ }
58
+ /**
59
+ * Enhanced verifier with state management and subscription support
60
+ */
61
+ export declare class VerifierWithState extends Verifier {
62
+ private state;
63
+ private subscribers;
64
+ private static runnerVerificationCache;
65
+ /**
66
+ * Clear the verification cache
67
+ */
68
+ static clearVerificationCache(): void;
69
+ /**
70
+ * Subscribe to verification state updates
71
+ * @param subscriber - Callback function to receive state updates
72
+ * @returns Unsubscribe function
73
+ */
74
+ subscribe(subscriber: (state: VerificationState) => void): () => void;
75
+ /**
76
+ * Update state and notify subscribers
77
+ */
78
+ private updateState;
79
+ /**
80
+ * Update a specific step state
81
+ */
82
+ private updateStepState;
83
+ /**
84
+ * Run full verification flow with state updates
85
+ * @param options - Verification options
86
+ * @returns Final verification result
87
+ */
88
+ runVerification(options?: RunVerificationOptions): Promise<VerificationResult>;
89
+ private _runVerificationInternal;
90
+ }
91
+ /**
92
+ * Load and initialize a verifier instance with state management
93
+ * @returns Initialized verifier instance with state management
94
+ */
95
+ export declare function loadVerifier(): Promise<VerifierWithState>;
96
+ export { suppressWasmLogs, TINFOIL_CONFIG };
97
+ /**
98
+ * Clear the verification cache
99
+ */
100
+ export declare function clearVerificationCache(): void;
@@ -0,0 +1,230 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TINFOIL_CONFIG = exports.suppressWasmLogs = exports.VerifierWithState = void 0;
4
+ exports.loadVerifier = loadVerifier;
5
+ exports.clearVerificationCache = clearVerificationCache;
6
+ const verifier_1 = require("./verifier");
7
+ Object.defineProperty(exports, "suppressWasmLogs", { enumerable: true, get: function () { return verifier_1.suppressWasmLogs; } });
8
+ const config_1 = require("./config");
9
+ Object.defineProperty(exports, "TINFOIL_CONFIG", { enumerable: true, get: function () { return config_1.TINFOIL_CONFIG; } });
10
+ /**
11
+ * Enhanced verifier with state management and subscription support
12
+ */
13
+ class VerifierWithState extends verifier_1.Verifier {
14
+ constructor() {
15
+ super(...arguments);
16
+ // State management
17
+ this.state = {
18
+ releaseDigest: "",
19
+ runtime: { status: "pending" },
20
+ code: { status: "pending" },
21
+ verification: { status: "pending" },
22
+ };
23
+ // Subscribers
24
+ this.subscribers = new Set();
25
+ }
26
+ /**
27
+ * Clear the verification cache
28
+ */
29
+ static clearVerificationCache() {
30
+ VerifierWithState.runnerVerificationCache.clear();
31
+ }
32
+ /**
33
+ * Subscribe to verification state updates
34
+ * @param subscriber - Callback function to receive state updates
35
+ * @returns Unsubscribe function
36
+ */
37
+ subscribe(subscriber) {
38
+ this.subscribers.add(subscriber);
39
+ // Send current state immediately
40
+ subscriber(this.state);
41
+ return () => {
42
+ this.subscribers.delete(subscriber);
43
+ };
44
+ }
45
+ /**
46
+ * Update state and notify subscribers
47
+ */
48
+ updateState(updates) {
49
+ this.state = { ...this.state, ...updates };
50
+ this.subscribers.forEach(subscriber => subscriber(this.state));
51
+ }
52
+ /**
53
+ * Update a specific step state
54
+ */
55
+ updateStepState(step, updates) {
56
+ this.state = {
57
+ ...this.state,
58
+ [step]: { ...this.state[step], ...updates }
59
+ };
60
+ this.subscribers.forEach(subscriber => subscriber(this.state));
61
+ }
62
+ /**
63
+ * Run full verification flow with state updates
64
+ * @param options - Verification options
65
+ * @returns Final verification result
66
+ */
67
+ async runVerification(options) {
68
+ const configRepo = options?.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
69
+ const enclaveURL = options?.serverURL || new URL(config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL).hostname;
70
+ let releaseDigest = options?.releaseDigest || "";
71
+ // If no digest provided, we need to fetch it first before checking cache
72
+ if (!releaseDigest) {
73
+ // Reset state for fresh verification
74
+ this.updateState({
75
+ releaseDigest: "resolving...",
76
+ runtime: { status: "pending" },
77
+ code: { status: "pending" },
78
+ verification: { status: "pending" },
79
+ });
80
+ try {
81
+ releaseDigest = await this.fetchLatestDigest(configRepo);
82
+ this.updateState({ releaseDigest: releaseDigest });
83
+ }
84
+ catch (error) {
85
+ // If we can't fetch the digest, mark all steps as error
86
+ this.updateState({ releaseDigest: "" });
87
+ this.updateStepState("code", {
88
+ status: "error",
89
+ error: error instanceof Error ? error.message : "Failed to fetch digest",
90
+ });
91
+ this.updateStepState("runtime", {
92
+ status: "error",
93
+ error: "Cannot proceed without digest",
94
+ });
95
+ this.updateStepState("verification", {
96
+ status: "error",
97
+ error: "Cannot proceed without digest",
98
+ });
99
+ return this.state;
100
+ }
101
+ }
102
+ // Now we have a digest, check cache
103
+ const cacheKey = `${configRepo}::${enclaveURL}::${releaseDigest}`;
104
+ const cachedPromise = VerifierWithState.runnerVerificationCache.get(cacheKey);
105
+ if (cachedPromise) {
106
+ // Reuse cached promise, but publish state updates for this instance
107
+ if (options?.onUpdate) {
108
+ const unsubscribe = this.subscribe(options.onUpdate);
109
+ // Get the cached result and update this instance's state to match
110
+ cachedPromise.then(cachedResult => {
111
+ // Update this instance's state to match the cached result
112
+ this.updateState(cachedResult);
113
+ }).finally(() => unsubscribe());
114
+ }
115
+ return cachedPromise;
116
+ }
117
+ // No cached result, create new verification promise
118
+ const verificationPromise = this._runVerificationInternal(configRepo, enclaveURL, releaseDigest)
119
+ .then(result => {
120
+ // Successful verification stays in cache
121
+ return result;
122
+ })
123
+ .catch(error => {
124
+ // On error, evict from cache
125
+ VerifierWithState.runnerVerificationCache.delete(cacheKey);
126
+ throw error;
127
+ });
128
+ // Store in cache
129
+ VerifierWithState.runnerVerificationCache.set(cacheKey, verificationPromise);
130
+ // Handle updates if requested
131
+ if (options?.onUpdate) {
132
+ const unsubscribe = this.subscribe(options.onUpdate);
133
+ verificationPromise.finally(() => unsubscribe());
134
+ }
135
+ return verificationPromise;
136
+ }
137
+ async _runVerificationInternal(configRepo, enclaveURL, releaseDigest) {
138
+ try {
139
+ // Reset state for this verification
140
+ this.updateState({
141
+ releaseDigest: releaseDigest,
142
+ runtime: { status: "pending" },
143
+ code: { status: "pending" },
144
+ verification: { status: "pending" },
145
+ });
146
+ // Step 2: Runtime attestation
147
+ this.updateStepState("runtime", { status: "loading" });
148
+ try {
149
+ const runtimeResult = await this.verifyEnclave(enclaveURL);
150
+ this.updateStepState("runtime", {
151
+ status: "success",
152
+ measurement: runtimeResult.measurement,
153
+ tlsPublicKeyFingerprint: runtimeResult.tlsPublicKeyFingerprint,
154
+ hpkePublicKey: runtimeResult.hpkePublicKey,
155
+ });
156
+ }
157
+ catch (error) {
158
+ this.updateStepState("runtime", {
159
+ status: "error",
160
+ error: error instanceof Error ? error.message : "Runtime attestation failed",
161
+ });
162
+ this.updateStepState("verification", {
163
+ status: "error",
164
+ error: "Cannot verify security without runtime attestation",
165
+ });
166
+ return this.state;
167
+ }
168
+ // Step 3: Code attestation
169
+ this.updateStepState("code", { status: "loading" });
170
+ try {
171
+ const codeResult = await this.verifyCode(configRepo, releaseDigest);
172
+ this.updateStepState("code", {
173
+ status: "success",
174
+ measurement: codeResult.measurement,
175
+ });
176
+ }
177
+ catch (error) {
178
+ this.updateStepState("code", {
179
+ status: "error",
180
+ error: error instanceof Error ? error.message : "Code attestation failed",
181
+ });
182
+ this.updateStepState("verification", {
183
+ status: "error",
184
+ error: "Cannot verify security without code attestation",
185
+ });
186
+ return this.state;
187
+ }
188
+ // Step 4: Compare measurements
189
+ this.updateStepState("verification", { status: "loading" });
190
+ const codeMeasurement = this.state.code.measurement;
191
+ const runtimeMeasurement = this.state.runtime.measurement;
192
+ // Use the compareMeasurements function
193
+ const measurementsMatch = (0, verifier_1.compareMeasurements)(codeMeasurement, runtimeMeasurement);
194
+ this.updateStepState("verification", {
195
+ status: "success",
196
+ securityVerified: measurementsMatch,
197
+ });
198
+ return this.state;
199
+ }
200
+ catch (error) {
201
+ // Catch any unexpected errors
202
+ if (this.state.verification.status === "pending" || this.state.verification.status === "loading") {
203
+ this.updateStepState("verification", {
204
+ status: "error",
205
+ error: error instanceof Error ? error.message : "Verification failed",
206
+ });
207
+ }
208
+ return this.state;
209
+ }
210
+ }
211
+ }
212
+ exports.VerifierWithState = VerifierWithState;
213
+ // Static cache for deduplicating verifications
214
+ VerifierWithState.runnerVerificationCache = new Map();
215
+ /**
216
+ * Load and initialize a verifier instance with state management
217
+ * @returns Initialized verifier instance with state management
218
+ */
219
+ async function loadVerifier() {
220
+ // Ensure WASM is initialized with the default runtime
221
+ await verifier_1.Verifier.initializeWasm();
222
+ // Return a new verifier instance with state management
223
+ return new VerifierWithState();
224
+ }
225
+ /**
226
+ * Clear the verification cache
227
+ */
228
+ function clearVerificationCache() {
229
+ VerifierWithState.clearVerificationCache();
230
+ }
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Attestation measurement containing platform type and register values
3
+ */
4
+ export interface AttestationMeasurement {
5
+ type: string;
6
+ registers: string[];
7
+ }
8
+ /**
9
+ * Attestation response containing cryptographic keys and measurements
10
+ * At least one of tlsPublicKeyFingerprint or hpkePublicKey must be present
11
+ */
12
+ export interface AttestationResponse {
13
+ tlsPublicKeyFingerprint?: string;
14
+ hpkePublicKey?: string;
15
+ measurement: AttestationMeasurement;
16
+ }
17
+ /**
18
+ * Full verification document produced by a successful verify() call
19
+ */
20
+ export interface VerificationDocument {
21
+ configRepo: string;
22
+ enclaveHost: string;
23
+ releaseDigest: string;
24
+ codeMeasurement: AttestationMeasurement;
25
+ enclaveMeasurement: AttestationResponse;
26
+ match: boolean;
27
+ }
28
+ export interface MeasurementComparisonResult {
29
+ match: boolean;
30
+ error?: Error;
31
+ }
32
+ export declare function compareMeasurementsDetailed(codeMeasurement: AttestationMeasurement, runtimeMeasurement: AttestationMeasurement): MeasurementComparisonResult;
33
+ /**
34
+ * Compare two measurements according to platform-specific rules
35
+ * This is predicate function for comparing attestation measurements
36
+ * taken from https://github.com/tinfoilsh/verifier/blob/main/attestation/attestation.go
37
+ *
38
+ * @param codeMeasurement - Expected measurement from code attestation
39
+ * @param runtimeMeasurement - Actual measurement from runtime attestation
40
+ * @returns true if measurements match according to platform rules
41
+ */
42
+ export declare function compareMeasurements(codeMeasurement: AttestationMeasurement, runtimeMeasurement: AttestationMeasurement): boolean;
43
+ /**
44
+ * Verifier performs attestation verification for Tinfoil enclaves
45
+ *
46
+ * The verifier loads a WebAssembly module that:
47
+ * 1. Fetches the latest code release digest from GitHub
48
+ * 2. Performs runtime attestation against the enclave
49
+ * 3. Performs code attestation using the digest
50
+ * 4. Compares measurements using platform-specific logic
51
+ */
52
+ export declare class Verifier {
53
+ private static goInstance;
54
+ private static initializationPromise;
55
+ private static verificationCache;
56
+ private static readonly defaultWasmUrl;
57
+ static originalFsWriteSync: ((fd: number, buf: Uint8Array) => number) | null;
58
+ static wasmLogsSuppressed: boolean;
59
+ static globalsInitialized: boolean;
60
+ private lastVerificationDocument?;
61
+ static clearVerificationCache(): void;
62
+ protected readonly serverURL: string;
63
+ protected readonly configRepo: string;
64
+ constructor(options?: {
65
+ serverURL?: string;
66
+ configRepo?: string;
67
+ });
68
+ /**
69
+ * Initialize the WebAssembly module
70
+ * This loads the Go runtime and makes verification functions available
71
+ */
72
+ static initializeWasm(): Promise<void>;
73
+ /**
74
+ * Fetch the latest release digest from GitHub
75
+ * @param configRepo - Repository name (e.g., "tinfoilsh/confidential-inference-proxy")
76
+ * @returns The digest hash
77
+ */
78
+ fetchLatestDigest(configRepo?: string): Promise<string>;
79
+ /**
80
+ * Perform runtime attestation on the enclave
81
+ * @param enclaveHost - The enclave hostname
82
+ * @returns Attestation response with measurement and keys
83
+ */
84
+ verifyEnclave(enclaveHost?: string): Promise<AttestationResponse>;
85
+ /**
86
+ * Perform code attestation
87
+ * @param configRepo - Repository name
88
+ * @param digest - Code digest hash
89
+ * @returns Code measurement
90
+ */
91
+ verifyCode(configRepo: string, digest: string): Promise<{
92
+ measurement: AttestationMeasurement;
93
+ }>;
94
+ /**
95
+ * Perform attestation verification
96
+ *
97
+ * This method:
98
+ * 1. Fetches the latest code digest from GitHub releases
99
+ * 2. Calls verifyCode to get the expected measurement for the code
100
+ * 3. Calls verifyEnclave to get the actual runtime measurement
101
+ * 4. Compares measurements using platform-specific logic (see `compareMeasurements()`)
102
+ * 5. Returns the attestation response if verification succeeds
103
+ *
104
+ * @throws Error if measurements don't match or verification fails
105
+ */
106
+ verify(): Promise<AttestationResponse>;
107
+ /**
108
+ * Returns the full verification document from the last successful verify() call
109
+ */
110
+ getVerificationDocument(): VerificationDocument | undefined;
111
+ }
112
+ /**
113
+ * Control WASM log output
114
+ *
115
+ * The Go WASM runtime outputs logs through a polyfilled fs.writeSync.
116
+ * This function allows suppressing those logs without affecting other console output.
117
+ *
118
+ * @param suppress - Whether to suppress WASM logs (default: true)
119
+ */
120
+ export declare function suppressWasmLogs(suppress?: boolean): void;