tinfoil 0.7.2 → 0.8.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 (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 +685 -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 +725 -0
  49. package/package.json +52 -8
  50. package/dist/secure-client.d.ts +0 -31
  51. package/dist/secure-client.js +0 -153
@@ -0,0 +1,224 @@
1
+ import { Verifier, compareMeasurements, suppressWasmLogs } from "./verifier";
2
+ import { TINFOIL_CONFIG } from "./config";
3
+ /**
4
+ * Enhanced verifier with state management and subscription support
5
+ */
6
+ export class VerifierWithState extends Verifier {
7
+ constructor() {
8
+ super(...arguments);
9
+ // State management
10
+ this.state = {
11
+ releaseDigest: "",
12
+ runtime: { status: "pending" },
13
+ code: { status: "pending" },
14
+ verification: { status: "pending" },
15
+ };
16
+ // Subscribers
17
+ this.subscribers = new Set();
18
+ }
19
+ /**
20
+ * Clear the verification cache
21
+ */
22
+ static clearVerificationCache() {
23
+ VerifierWithState.runnerVerificationCache.clear();
24
+ }
25
+ /**
26
+ * Subscribe to verification state updates
27
+ * @param subscriber - Callback function to receive state updates
28
+ * @returns Unsubscribe function
29
+ */
30
+ subscribe(subscriber) {
31
+ this.subscribers.add(subscriber);
32
+ // Send current state immediately
33
+ subscriber(this.state);
34
+ return () => {
35
+ this.subscribers.delete(subscriber);
36
+ };
37
+ }
38
+ /**
39
+ * Update state and notify subscribers
40
+ */
41
+ updateState(updates) {
42
+ this.state = { ...this.state, ...updates };
43
+ this.subscribers.forEach(subscriber => subscriber(this.state));
44
+ }
45
+ /**
46
+ * Update a specific step state
47
+ */
48
+ updateStepState(step, updates) {
49
+ this.state = {
50
+ ...this.state,
51
+ [step]: { ...this.state[step], ...updates }
52
+ };
53
+ this.subscribers.forEach(subscriber => subscriber(this.state));
54
+ }
55
+ /**
56
+ * Run full verification flow with state updates
57
+ * @param options - Verification options
58
+ * @returns Final verification result
59
+ */
60
+ async runVerification(options) {
61
+ const configRepo = options?.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
62
+ const enclaveURL = options?.serverURL || new URL(TINFOIL_CONFIG.INFERENCE_BASE_URL).hostname;
63
+ let releaseDigest = options?.releaseDigest || "";
64
+ // If no digest provided, we need to fetch it first before checking cache
65
+ if (!releaseDigest) {
66
+ // Reset state for fresh verification
67
+ this.updateState({
68
+ releaseDigest: "resolving...",
69
+ runtime: { status: "pending" },
70
+ code: { status: "pending" },
71
+ verification: { status: "pending" },
72
+ });
73
+ try {
74
+ releaseDigest = await this.fetchLatestDigest(configRepo);
75
+ this.updateState({ releaseDigest: releaseDigest });
76
+ }
77
+ catch (error) {
78
+ // If we can't fetch the digest, mark all steps as error
79
+ this.updateState({ releaseDigest: "" });
80
+ this.updateStepState("code", {
81
+ status: "error",
82
+ error: error instanceof Error ? error.message : "Failed to fetch digest",
83
+ });
84
+ this.updateStepState("runtime", {
85
+ status: "error",
86
+ error: "Cannot proceed without digest",
87
+ });
88
+ this.updateStepState("verification", {
89
+ status: "error",
90
+ error: "Cannot proceed without digest",
91
+ });
92
+ return this.state;
93
+ }
94
+ }
95
+ // Now we have a digest, check cache
96
+ const cacheKey = `${configRepo}::${enclaveURL}::${releaseDigest}`;
97
+ const cachedPromise = VerifierWithState.runnerVerificationCache.get(cacheKey);
98
+ if (cachedPromise) {
99
+ // Reuse cached promise, but publish state updates for this instance
100
+ if (options?.onUpdate) {
101
+ const unsubscribe = this.subscribe(options.onUpdate);
102
+ // Get the cached result and update this instance's state to match
103
+ cachedPromise.then(cachedResult => {
104
+ // Update this instance's state to match the cached result
105
+ this.updateState(cachedResult);
106
+ }).finally(() => unsubscribe());
107
+ }
108
+ return cachedPromise;
109
+ }
110
+ // No cached result, create new verification promise
111
+ const verificationPromise = this._runVerificationInternal(configRepo, enclaveURL, releaseDigest)
112
+ .then(result => {
113
+ // Successful verification stays in cache
114
+ return result;
115
+ })
116
+ .catch(error => {
117
+ // On error, evict from cache
118
+ VerifierWithState.runnerVerificationCache.delete(cacheKey);
119
+ throw error;
120
+ });
121
+ // Store in cache
122
+ VerifierWithState.runnerVerificationCache.set(cacheKey, verificationPromise);
123
+ // Handle updates if requested
124
+ if (options?.onUpdate) {
125
+ const unsubscribe = this.subscribe(options.onUpdate);
126
+ verificationPromise.finally(() => unsubscribe());
127
+ }
128
+ return verificationPromise;
129
+ }
130
+ async _runVerificationInternal(configRepo, enclaveURL, releaseDigest) {
131
+ try {
132
+ // Reset state for this verification
133
+ this.updateState({
134
+ releaseDigest: releaseDigest,
135
+ runtime: { status: "pending" },
136
+ code: { status: "pending" },
137
+ verification: { status: "pending" },
138
+ });
139
+ // Step 2: Runtime attestation
140
+ this.updateStepState("runtime", { status: "loading" });
141
+ try {
142
+ const runtimeResult = await this.verifyEnclave(enclaveURL);
143
+ this.updateStepState("runtime", {
144
+ status: "success",
145
+ measurement: runtimeResult.measurement,
146
+ tlsPublicKeyFingerprint: runtimeResult.tlsPublicKeyFingerprint,
147
+ hpkePublicKey: runtimeResult.hpkePublicKey,
148
+ });
149
+ }
150
+ catch (error) {
151
+ this.updateStepState("runtime", {
152
+ status: "error",
153
+ error: error instanceof Error ? error.message : "Runtime attestation failed",
154
+ });
155
+ this.updateStepState("verification", {
156
+ status: "error",
157
+ error: "Cannot verify security without runtime attestation",
158
+ });
159
+ return this.state;
160
+ }
161
+ // Step 3: Code attestation
162
+ this.updateStepState("code", { status: "loading" });
163
+ try {
164
+ const codeResult = await this.verifyCode(configRepo, releaseDigest);
165
+ this.updateStepState("code", {
166
+ status: "success",
167
+ measurement: codeResult.measurement,
168
+ });
169
+ }
170
+ catch (error) {
171
+ this.updateStepState("code", {
172
+ status: "error",
173
+ error: error instanceof Error ? error.message : "Code attestation failed",
174
+ });
175
+ this.updateStepState("verification", {
176
+ status: "error",
177
+ error: "Cannot verify security without code attestation",
178
+ });
179
+ return this.state;
180
+ }
181
+ // Step 4: Compare measurements
182
+ this.updateStepState("verification", { status: "loading" });
183
+ const codeMeasurement = this.state.code.measurement;
184
+ const runtimeMeasurement = this.state.runtime.measurement;
185
+ // Use the compareMeasurements function
186
+ const measurementsMatch = compareMeasurements(codeMeasurement, runtimeMeasurement);
187
+ this.updateStepState("verification", {
188
+ status: "success",
189
+ securityVerified: measurementsMatch,
190
+ });
191
+ return this.state;
192
+ }
193
+ catch (error) {
194
+ // Catch any unexpected errors
195
+ if (this.state.verification.status === "pending" || this.state.verification.status === "loading") {
196
+ this.updateStepState("verification", {
197
+ status: "error",
198
+ error: error instanceof Error ? error.message : "Verification failed",
199
+ });
200
+ }
201
+ return this.state;
202
+ }
203
+ }
204
+ }
205
+ // Static cache for deduplicating verifications
206
+ VerifierWithState.runnerVerificationCache = new Map();
207
+ /**
208
+ * Load and initialize a verifier instance with state management
209
+ * @returns Initialized verifier instance with state management
210
+ */
211
+ export async function loadVerifier() {
212
+ // Ensure WASM is initialized with the default runtime
213
+ await Verifier.initializeWasm();
214
+ // Return a new verifier instance with state management
215
+ return new VerifierWithState();
216
+ }
217
+ // Re-export utilities
218
+ export { suppressWasmLogs, TINFOIL_CONFIG };
219
+ /**
220
+ * Clear the verification cache
221
+ */
222
+ export function clearVerificationCache() {
223
+ VerifierWithState.clearVerificationCache();
224
+ }
@@ -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;