tinfoil 0.2.3 → 0.4.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.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Creates an AI SDK provider with the specified repository and enclave.
3
+ *
4
+ * @param repo - The repository identifier
5
+ * @param enclave - The enclave URL/identifier
6
+ * @returns A TinfoilAI instance
7
+ */
8
+ export declare function createTinfoilAI(repo: string, enclave: string): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>>;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTinfoilAI = createTinfoilAI;
4
+ const openai_compatible_1 = require("@ai-sdk/openai-compatible");
5
+ const secure_client_1 = require("./secure-client");
6
+ const undici_1 = require("undici");
7
+ const node_tls_1 = require("node:tls");
8
+ const node_crypto_1 = require("node:crypto");
9
+ /**
10
+ * Creates an AI SDK provider with the specified repository and enclave.
11
+ *
12
+ * @param repo - The repository identifier
13
+ * @param enclave - The enclave URL/identifier
14
+ * @returns A TinfoilAI instance
15
+ */
16
+ async function createTinfoilAI(repo, enclave) {
17
+ const sc = new secure_client_1.SecureClient(enclave, repo);
18
+ const groundTruth = await sc.verify();
19
+ const connect = (0, undici_1.buildConnector)({
20
+ checkServerIdentity: (host, cert) => {
21
+ if (!cert.pubkey) {
22
+ throw new Error('No public key available in certificate');
23
+ }
24
+ const x509 = new node_crypto_1.X509Certificate(cert.raw);
25
+ const publicKeyDer = x509.publicKey.export({
26
+ type: 'spki',
27
+ format: 'der'
28
+ });
29
+ const pubKeyFP = (0, node_crypto_1.createHash)('sha256').update(publicKeyDer).digest('hex');
30
+ if (pubKeyFP !== groundTruth.publicKeyFP) {
31
+ throw new Error(`Certificate public key fingerprint mismatch. Expected: ${groundTruth.publicKeyFP}, Got: ${pubKeyFP}`);
32
+ }
33
+ return (0, node_tls_1.checkServerIdentity)(host, cert);
34
+ }
35
+ });
36
+ const agent = new undici_1.Agent({ connect });
37
+ return (0, openai_compatible_1.createOpenAICompatible)({
38
+ name: "tinfoil",
39
+ baseURL: `https://${enclave}/v1`,
40
+ fetch: (async (input, init) => {
41
+ if (!init) {
42
+ init = {};
43
+ }
44
+ init.dispatcher = agent;
45
+ return await (0, undici_1.fetch)(input, init);
46
+ }),
47
+ });
48
+ }
package/dist/client.js CHANGED
@@ -96,8 +96,7 @@ class TinfoilAI {
96
96
  catch (error) {
97
97
  throw new Error(`Failed to verify enclave: ${error}`);
98
98
  }
99
- // Convert the expected fingerprint to hex string for comparison
100
- const expectedFingerprint = Buffer.from(this.groundTruth.certFingerprint).toString('hex');
99
+ const expectedFingerprint = this.groundTruth.publicKeyFP;
101
100
  // Create a custom HTTPS agent that verifies certificate fingerprints
102
101
  const httpsAgent = new https_1.default.Agent({
103
102
  rejectUnauthorized: true,
@@ -105,11 +104,15 @@ class TinfoilAI {
105
104
  if (!cert || !cert.raw) {
106
105
  throw new Error('No certificate found');
107
106
  }
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');
107
+ if (!cert.pubkey) {
108
+ throw new Error('No public key found');
109
+ }
110
+ const pemCert = `-----BEGIN CERTIFICATE-----\n${cert.raw.toString('base64')}\n-----END CERTIFICATE-----`;
111
+ const x509Cert = new crypto_1.X509Certificate(pemCert);
112
+ const publicKey = x509Cert.publicKey.export({ format: 'der', type: 'spki' });
113
+ const publicKeyHash = (0, crypto_1.createHash)('sha256').update(publicKey).digest('hex');
114
+ if (publicKeyHash !== expectedFingerprint) {
115
+ throw new Error(`Certificate fingerprint mismatch. Got ${publicKeyHash}, expected ${expectedFingerprint}`);
113
116
  }
114
117
  return undefined; // Validation successful
115
118
  }
@@ -1,17 +1,8 @@
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
1
  /**
11
2
  * Ground truth measurements from verification
12
3
  */
13
4
  export interface GroundTruth {
14
- certFingerprint: Uint8Array;
5
+ publicKeyFP: string;
15
6
  measurement: string;
16
7
  }
17
8
  /**
@@ -11,11 +11,16 @@ const globalThis = global;
11
11
  // Performance API
12
12
  globalThis.performance = {
13
13
  now: () => Date.now(),
14
- markResourceTiming: () => { },
15
- mark: () => { },
16
- measure: () => { },
17
- clearMarks: () => { },
18
- clearMeasures: () => { },
14
+ markResourceTiming: () => {
15
+ },
16
+ mark: () => {
17
+ },
18
+ measure: () => {
19
+ },
20
+ clearMarks: () => {
21
+ },
22
+ clearMeasures: () => {
23
+ },
19
24
  getEntriesByName: () => [],
20
25
  getEntriesByType: () => [],
21
26
  getEntries: () => []
@@ -39,7 +44,8 @@ if (typeof window === 'undefined') {
39
44
  globalThis.window = globalThis;
40
45
  globalThis.document = {
41
46
  createElement: () => ({
42
- setAttribute: () => { }
47
+ setAttribute: () => {
48
+ }
43
49
  })
44
50
  };
45
51
  }
@@ -143,7 +149,7 @@ class SecureClient {
143
149
  throw new Error('Measurements do not match');
144
150
  }
145
151
  return {
146
- certFingerprint: new Uint8Array(Buffer.from(attestationResponse.certificate, 'hex')),
152
+ publicKeyFP: attestationResponse.certificate,
147
153
  measurement: attestationResponse.measurement,
148
154
  };
149
155
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tinfoil",
3
- "version": "0.2.3",
3
+ "version": "0.4.0",
4
4
  "description": "Official Tinfoil secure OpenAI client wrapper",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -28,10 +28,13 @@
28
28
  "author": "Tinfoil",
29
29
  "license": "AGPL-3.0-or-later",
30
30
  "dependencies": {
31
+ "@ai-sdk/openai-compatible": "^0.2.14",
32
+ "ai": "^4.3.15",
31
33
  "dotenv": "^16.4.7",
32
34
  "node-fetch": "^2.7.0",
33
35
  "openai": "^4.87.3",
34
- "ts-node": "^10.9.2"
36
+ "ts-node": "^10.9.2",
37
+ "undici": "^7.9.0"
35
38
  },
36
39
  "devDependencies": {
37
40
  "@types/jest": "^29.5.12",