@coolwithakay/uatp 0.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/README.md ADDED
@@ -0,0 +1,236 @@
1
+ # UATP TypeScript SDK
2
+
3
+ Cryptographic audit trails for AI decisions.
4
+
5
+ [![npm](https://img.shields.io/npm/v/uatp)](https://www.npmjs.com/package/uatp)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install @coolwithakay/uatp
12
+ ```
13
+
14
+ ## Quick Start
15
+
16
+ ```typescript
17
+ import { UATP } from '@coolwithakay/uatp';
18
+
19
+ const client = new UATP();
20
+
21
+ // Create and sign a capsule (keys generated locally, never transmitted)
22
+ const result = await client.certify({
23
+ task: 'Loan decision',
24
+ decision: 'Approved for $50,000 at 5.2% APR',
25
+ reasoning: [
26
+ { step: 1, thought: 'Credit score 720 (excellent)', confidence: 0.95 },
27
+ { step: 2, thought: 'Debt-to-income 0.28 (acceptable)', confidence: 0.90 }
28
+ ]
29
+ });
30
+
31
+ console.log(result.capsuleId); // cap_xyz123...
32
+ console.log(result.signature); // Ed25519 signature
33
+ console.log(result.publicKey); // Your verification key
34
+ ```
35
+
36
+ ## Zero-Trust Architecture
37
+
38
+ Your private key **never leaves your device**:
39
+
40
+ ```
41
+ ┌─────────────────────────────┐
42
+ │ YOUR DEVICE │
43
+ │ ✓ Private key (derived) │
44
+ │ ✓ ALL signing happens here │
45
+ │ ✓ Ed25519 + PBKDF2 480K │
46
+ └──────────────┬──────────────┘
47
+ │ Only hash sent
48
+
49
+ ┌─────────────────────────────┐
50
+ │ UATP SERVER (optional) │
51
+ │ - Timestamp service │
52
+ │ - Capsule storage │
53
+ └─────────────────────────────┘
54
+ ```
55
+
56
+ ## Usage Examples
57
+
58
+ ### Basic Usage
59
+
60
+ ```typescript
61
+ import { UATP } from '@coolwithakay/uatp';
62
+
63
+ const client = new UATP();
64
+
65
+ const result = await client.certify({
66
+ task: 'Product recommendation',
67
+ decision: 'Recommended iPhone 15 Pro',
68
+ reasoning: [
69
+ { step: 1, thought: 'User prefers Apple ecosystem', confidence: 0.92 },
70
+ { step: 2, thought: 'Budget matches Pro pricing', confidence: 0.88 }
71
+ ]
72
+ });
73
+ ```
74
+
75
+ ### With Custom Passphrase (Production)
76
+
77
+ ```typescript
78
+ const result = await client.certify({
79
+ task: 'Medical diagnosis',
80
+ decision: 'Recommend follow-up tests',
81
+ reasoning: [
82
+ { step: 1, thought: 'Symptoms consistent with condition X', confidence: 0.85 }
83
+ ],
84
+ passphrase: 'your-secure-passphrase',
85
+ deviceBound: false
86
+ });
87
+ ```
88
+
89
+ ### Verify Locally (No Server Needed)
90
+
91
+ ```typescript
92
+ const verification = client.verifyLocal(capsule);
93
+
94
+ if (verification.valid) {
95
+ console.log('Capsule is authentic!');
96
+ } else {
97
+ console.log('Verification failed:', verification.errors);
98
+ }
99
+ ```
100
+
101
+ ### Store on Server
102
+
103
+ ```typescript
104
+ const result = await client.certify({
105
+ task: 'Insurance claim',
106
+ decision: 'Approved: $5,000',
107
+ reasoning: [...],
108
+ storeOnServer: true
109
+ });
110
+
111
+ console.log(result.proofUrl); // https://...
112
+ ```
113
+
114
+ ### Retrieve Proof
115
+
116
+ ```typescript
117
+ const proof = await client.getProof('cap_abc123');
118
+ console.log(proof.payload.task);
119
+ console.log(proof.payload.decision);
120
+ ```
121
+
122
+ ## API Reference
123
+
124
+ ### `new UATP(config?)`
125
+
126
+ Create a new UATP client.
127
+
128
+ | Option | Type | Default | Description |
129
+ |--------|------|---------|-------------|
130
+ | `apiKey` | `string` | - | API key for authenticated requests |
131
+ | `baseUrl` | `string` | `http://localhost:8000` | UATP server URL |
132
+ | `timeout` | `number` | `30000` | Request timeout (ms) |
133
+
134
+ ### `client.certify(options)`
135
+
136
+ Create a signed capsule.
137
+
138
+ | Option | Type | Required | Description |
139
+ |--------|------|----------|-------------|
140
+ | `task` | `string` | Yes | Description of the task |
141
+ | `decision` | `string` | Yes | The AI's decision |
142
+ | `reasoning` | `ReasoningStep[]` | Yes | Reasoning steps |
143
+ | `passphrase` | `string` | No | Key encryption passphrase |
144
+ | `deviceBound` | `boolean` | No | Derive passphrase from device (default: true) |
145
+ | `confidence` | `number` | No | Overall confidence (0-1) |
146
+ | `metadata` | `object` | No | Additional metadata |
147
+ | `requestTimestamp` | `boolean` | No | Get RFC 3161 timestamp (default: true) |
148
+ | `storeOnServer` | `boolean` | No | Store on UATP server (default: false) |
149
+
150
+ ### `client.verifyLocal(capsule)`
151
+
152
+ Verify a capsule cryptographically without server.
153
+
154
+ ### `client.getProof(capsuleId)`
155
+
156
+ Retrieve proof from server.
157
+
158
+ ### `client.listCapsules(limit?)`
159
+
160
+ List capsules from server.
161
+
162
+ ### `client.recordOutcome(capsuleId, outcome)`
163
+
164
+ Record actual outcome for a decision.
165
+
166
+ ## Framework Integration
167
+
168
+ ### React
169
+
170
+ ```typescript
171
+ import { UATP } from '@coolwithakay/uatp';
172
+ import { useState } from 'react';
173
+
174
+ function useUATP() {
175
+ const [client] = useState(() => new UATP());
176
+
177
+ const certify = async (task: string, decision: string, reasoning: any[]) => {
178
+ return client.certify({ task, decision, reasoning });
179
+ };
180
+
181
+ return { certify };
182
+ }
183
+ ```
184
+
185
+ ### Next.js API Route
186
+
187
+ ```typescript
188
+ // pages/api/certify.ts
189
+ import { UATP } from '@coolwithakay/uatp';
190
+
191
+ const client = new UATP({ baseUrl: process.env.UATP_URL });
192
+
193
+ export default async function handler(req, res) {
194
+ const result = await client.certify(req.body);
195
+ res.json({ capsuleId: result.capsuleId });
196
+ }
197
+ ```
198
+
199
+ ### Lovable / AI App Builders
200
+
201
+ ```typescript
202
+ // In your Lovable-generated app
203
+ import { UATP } from '@coolwithakay/uatp';
204
+
205
+ const client = new UATP();
206
+
207
+ // Audit every AI decision
208
+ async function makeDecision(userQuery: string) {
209
+ const aiResponse = await callYourAI(userQuery);
210
+
211
+ // Create audit trail
212
+ const proof = await client.certify({
213
+ task: userQuery,
214
+ decision: aiResponse.decision,
215
+ reasoning: aiResponse.steps.map((s, i) => ({
216
+ step: i + 1,
217
+ thought: s.thought,
218
+ confidence: s.confidence
219
+ }))
220
+ });
221
+
222
+ return { ...aiResponse, proofId: proof.capsuleId };
223
+ }
224
+ ```
225
+
226
+ ## Security
227
+
228
+ - **Ed25519** signatures (128-bit security)
229
+ - **PBKDF2-HMAC-SHA256** key derivation (480,000 iterations)
230
+ - **SHA-256** content hashing
231
+ - Keys derived from passphrase, never stored in plaintext
232
+ - Optional **RFC 3161** timestamps from external authority
233
+
234
+ ## License
235
+
236
+ MIT
@@ -0,0 +1,268 @@
1
+ export { bytesToHex, hexToBytes } from '@noble/hashes/utils';
2
+
3
+ /**
4
+ * UATP TypeScript SDK - Type Definitions
5
+ */
6
+ interface ReasoningStep {
7
+ step: number;
8
+ thought: string;
9
+ confidence: number;
10
+ action?: string;
11
+ data_sources?: DataSource[];
12
+ reasoning?: string;
13
+ plain_language?: string;
14
+ }
15
+ interface DataSource {
16
+ source: string;
17
+ value: unknown;
18
+ timestamp?: string;
19
+ api_endpoint?: string;
20
+ }
21
+ interface CertifyOptions {
22
+ /** Description of the task */
23
+ task: string;
24
+ /** The AI's final decision */
25
+ decision: string;
26
+ /** List of reasoning steps */
27
+ reasoning: ReasoningStep[];
28
+ /** Optional passphrase for key encryption (recommended for production) */
29
+ passphrase?: string;
30
+ /** If true, derive passphrase from browser/device info (default: true) */
31
+ deviceBound?: boolean;
32
+ /** Overall confidence score (0-1). Auto-calculated if not provided */
33
+ confidence?: number;
34
+ /** Additional metadata to attach */
35
+ metadata?: Record<string, unknown>;
36
+ /** Request RFC 3161 timestamp from server (default: true) */
37
+ requestTimestamp?: boolean;
38
+ /** Store capsule on UATP server (default: false) */
39
+ storeOnServer?: boolean;
40
+ }
41
+ interface SignedCapsule {
42
+ /** Unique capsule identifier */
43
+ capsuleId: string;
44
+ /** Ed25519 signature (hex) */
45
+ signature: string;
46
+ /** Public key for verification (hex) */
47
+ publicKey: string;
48
+ /** SHA-256 hash of content (hex) */
49
+ contentHash: string;
50
+ /** ISO timestamp of signing */
51
+ timestamp: string;
52
+ /** Original content that was signed */
53
+ content: CapsuleContent;
54
+ /** RFC 3161 timestamp token (if requested) */
55
+ timestampToken?: string;
56
+ /** Timestamp authority used */
57
+ timestampTsa?: string;
58
+ /** Whether stored on server */
59
+ serverStored?: boolean;
60
+ /** URL to verify on server */
61
+ proofUrl?: string;
62
+ }
63
+ interface CapsuleContent {
64
+ task: string;
65
+ decision: string;
66
+ reasoning_chain: ReasoningStep[];
67
+ confidence: number;
68
+ metadata: Record<string, unknown>;
69
+ }
70
+ interface CapsuleProof {
71
+ capsuleId: string;
72
+ capsuleType: string;
73
+ status: string;
74
+ timestamp: Date;
75
+ payload: Record<string, unknown>;
76
+ rawData: Record<string, unknown>;
77
+ }
78
+ interface VerificationResult {
79
+ valid: boolean;
80
+ signatureValid: boolean;
81
+ hashValid: boolean;
82
+ timestampValid?: boolean;
83
+ errors?: string[];
84
+ }
85
+ interface UATPConfig {
86
+ /** API key for authenticated requests */
87
+ apiKey?: string;
88
+ /** Base URL of UATP server (default: http://localhost:8000) */
89
+ baseUrl?: string;
90
+ /** Request timeout in milliseconds (default: 30000) */
91
+ timeout?: number;
92
+ }
93
+ interface KeyPair {
94
+ privateKey: Uint8Array;
95
+ publicKey: Uint8Array;
96
+ }
97
+
98
+ /**
99
+ * UATP TypeScript SDK - Client
100
+ *
101
+ * Zero-Trust Architecture:
102
+ * - Private keys NEVER leave your device
103
+ * - All signing happens locally using Ed25519
104
+ * - Only content hash sent to server for RFC 3161 timestamping
105
+ * - Capsules can be verified independently without UATP
106
+ */
107
+
108
+ declare class UATP {
109
+ private apiKey?;
110
+ private baseUrl;
111
+ private timeout;
112
+ /**
113
+ * Initialize UATP client.
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * // Local development
118
+ * const client = new UATP();
119
+ *
120
+ * // Production
121
+ * const client = new UATP({
122
+ * apiKey: 'your-api-key',
123
+ * baseUrl: 'https://api.uatp.io'
124
+ * });
125
+ * ```
126
+ */
127
+ constructor(config?: UATPConfig);
128
+ /**
129
+ * Create a cryptographically certified capsule for an AI decision.
130
+ *
131
+ * ZERO-TRUST: Private key NEVER leaves your device.
132
+ * - All signing happens locally using Ed25519
133
+ * - Only the content hash is sent to UATP for timestamping
134
+ * - Capsules can be verified independently without UATP
135
+ *
136
+ * @example
137
+ * ```typescript
138
+ * const result = await client.certify({
139
+ * task: 'Loan decision',
140
+ * decision: 'Approved for $50,000 at 5.2% APR',
141
+ * reasoning: [
142
+ * { step: 1, thought: 'Credit score 720 (excellent)', confidence: 0.95 },
143
+ * { step: 2, thought: 'Debt-to-income 0.28 (acceptable)', confidence: 0.90 }
144
+ * ]
145
+ * });
146
+ *
147
+ * console.log(result.capsuleId);
148
+ * console.log(result.signature);
149
+ * ```
150
+ */
151
+ certify(options: CertifyOptions): Promise<SignedCapsule>;
152
+ /**
153
+ * Retrieve full cryptographic proof for a capsule.
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * const proof = await client.getProof('cap_abc123');
158
+ * console.log(proof.payload.task);
159
+ * ```
160
+ */
161
+ getProof(capsuleId: string): Promise<CapsuleProof>;
162
+ /**
163
+ * List capsules from the server.
164
+ *
165
+ * @example
166
+ * ```typescript
167
+ * const capsules = await client.listCapsules(10);
168
+ * for (const cap of capsules) {
169
+ * console.log(cap.capsuleId, cap.status);
170
+ * }
171
+ * ```
172
+ */
173
+ listCapsules(limit?: number): Promise<CapsuleProof[]>;
174
+ /**
175
+ * Verify a capsule locally without server.
176
+ *
177
+ * This can be done by anyone - no UATP infrastructure required.
178
+ * Only uses cryptographic data embedded in the capsule.
179
+ *
180
+ * @example
181
+ * ```typescript
182
+ * const result = client.verifyLocal(capsule);
183
+ * if (result.valid) {
184
+ * console.log('Capsule is authentic!');
185
+ * }
186
+ * ```
187
+ */
188
+ verifyLocal(capsule: SignedCapsule): VerificationResult;
189
+ /**
190
+ * Record the actual outcome of an AI decision (ground truth).
191
+ *
192
+ * @example
193
+ * ```typescript
194
+ * await client.recordOutcome('cap_abc123', {
195
+ * result: 'successful',
196
+ * aiWasCorrect: true,
197
+ * financialImpact: 2500,
198
+ * notes: 'Loan fully paid on time'
199
+ * });
200
+ * ```
201
+ */
202
+ recordOutcome(capsuleId: string, outcome: {
203
+ result?: string;
204
+ aiWasCorrect?: boolean;
205
+ financialImpact?: number;
206
+ customerSatisfaction?: number;
207
+ notes?: string;
208
+ }): Promise<boolean>;
209
+ /**
210
+ * Internal fetch wrapper with timeout and headers.
211
+ */
212
+ private fetch;
213
+ }
214
+
215
+ /**
216
+ * UATP TypeScript SDK - Cryptographic Operations
217
+ *
218
+ * Zero-Trust Architecture:
219
+ * - Private keys NEVER leave your device
220
+ * - All signing happens locally using Ed25519
221
+ * - Keys are derived from passphrase using PBKDF2 (480,000 iterations)
222
+ */
223
+
224
+ /**
225
+ * Derive an Ed25519 key pair from a passphrase.
226
+ * Uses PBKDF2-HMAC-SHA256 with 480,000 iterations.
227
+ */
228
+ declare function deriveKeyPair(passphrase: string): KeyPair;
229
+ /**
230
+ * Generate a device-bound passphrase.
231
+ *
232
+ * SECURITY BOUNDARY: This is a CONVENIENCE feature, not a security feature.
233
+ * For production, use an explicit passphrase.
234
+ */
235
+ declare function deriveDevicePassphrase(): string;
236
+ /**
237
+ * Canonicalize content for signing.
238
+ * Ensures deterministic JSON serialization.
239
+ */
240
+ declare function canonicalize(obj: unknown): string;
241
+ /**
242
+ * Hash content using SHA-256.
243
+ */
244
+ declare function hashContent(content: CapsuleContent): string;
245
+ /**
246
+ * Sign content with Ed25519.
247
+ */
248
+ declare function sign(content: CapsuleContent, privateKey: Uint8Array): string;
249
+ /**
250
+ * Verify an Ed25519 signature.
251
+ */
252
+ declare function verify(content: CapsuleContent, signature: string, publicKey: string): boolean;
253
+ /**
254
+ * Create a signed capsule.
255
+ */
256
+ declare function createSignedCapsule(content: CapsuleContent, keyPair: KeyPair): SignedCapsule;
257
+ /**
258
+ * Verify a signed capsule locally.
259
+ * No server required - pure cryptographic verification.
260
+ */
261
+ declare function verifyCapsule(capsule: SignedCapsule): {
262
+ valid: boolean;
263
+ signatureValid: boolean;
264
+ hashValid: boolean;
265
+ errors: string[];
266
+ };
267
+
268
+ export { type CapsuleContent, type CapsuleProof, type CertifyOptions, type DataSource, type KeyPair, type ReasoningStep, type SignedCapsule, UATP, type UATPConfig, type VerificationResult, canonicalize, createSignedCapsule, deriveDevicePassphrase, deriveKeyPair, hashContent, sign, verify, verifyCapsule };