agent-canvas 0.1.33 → 0.1.36

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,123 @@
1
+ /**
2
+ * AES-GCM-256 helpers for end-to-end encrypted canvas shares.
3
+ *
4
+ * The same module is loaded in the browser (decrypts payloads) and in the
5
+ * daemon's Bun runtime (encrypts payloads + decrypts polled feedback). It
6
+ * only uses `crypto.subtle`, `TextEncoder`/`TextDecoder`, and
7
+ * `btoa`/`atob` — all available in both environments.
8
+ *
9
+ * Wire format
10
+ * -----------
11
+ * - Key: 32 random bytes, transported in the URL fragment as base64url
12
+ * (e.g. `#k=<key>`). Fragments are never sent to the worker, so the
13
+ * worker cannot decrypt the payloads it stores.
14
+ * - Each encrypted string is `base64(IV || ciphertext-with-tag)` where the
15
+ * IV is 12 fresh random bytes. This is the standard AES-GCM packing.
16
+ * - The presence of an `encryption: { alg: "AES-GCM", v: 1 }` marker on a
17
+ * SharePayload / FeedbackPostBody / ShareRecord tells the consumer that
18
+ * the marked fields contain ciphertext rather than plaintext.
19
+ */
20
+
21
+ export const CRYPTO_ALG = "AES-GCM" as const;
22
+ export const CRYPTO_VERSION = 1 as const;
23
+
24
+ export interface EncryptionMeta {
25
+ alg: typeof CRYPTO_ALG;
26
+ v: typeof CRYPTO_VERSION;
27
+ }
28
+
29
+ export const ENCRYPTION_META: EncryptionMeta = { alg: CRYPTO_ALG, v: CRYPTO_VERSION };
30
+
31
+ /** Generate a fresh 256-bit AES-GCM key. */
32
+ export async function generateShareKey(): Promise<CryptoKey> {
33
+ return crypto.subtle.generateKey(
34
+ { name: CRYPTO_ALG, length: 256 },
35
+ true,
36
+ ["encrypt", "decrypt"],
37
+ );
38
+ }
39
+
40
+ /** Export a CryptoKey to a base64url string suitable for URL fragments. */
41
+ export async function exportShareKey(key: CryptoKey): Promise<string> {
42
+ const raw = await crypto.subtle.exportKey("raw", key);
43
+ return b64urlFromBytes(new Uint8Array(raw));
44
+ }
45
+
46
+ /** Import a base64url-encoded key for AES-GCM. */
47
+ export async function importShareKey(encoded: string): Promise<CryptoKey> {
48
+ const raw = bytesFromB64url(encoded);
49
+ return crypto.subtle.importKey(
50
+ "raw",
51
+ raw as BufferSource,
52
+ { name: CRYPTO_ALG, length: 256 },
53
+ false,
54
+ ["encrypt", "decrypt"],
55
+ );
56
+ }
57
+
58
+ /** Encrypt a UTF-8 string. Returns base64(IV || ciphertext+tag). */
59
+ export async function encryptString(key: CryptoKey, plaintext: string): Promise<string> {
60
+ const iv = crypto.getRandomValues(new Uint8Array(12));
61
+ const data = new TextEncoder().encode(plaintext);
62
+ const ct = await crypto.subtle.encrypt(
63
+ { name: CRYPTO_ALG, iv: iv as BufferSource },
64
+ key,
65
+ data as BufferSource,
66
+ );
67
+ const combined = new Uint8Array(iv.byteLength + ct.byteLength);
68
+ combined.set(iv, 0);
69
+ combined.set(new Uint8Array(ct), iv.byteLength);
70
+ return b64FromBytes(combined);
71
+ }
72
+
73
+ /** Decrypt a base64-encoded ciphertext back to a UTF-8 string. */
74
+ export async function decryptString(key: CryptoKey, ciphertext: string): Promise<string> {
75
+ const combined = bytesFromB64(ciphertext);
76
+ if (combined.byteLength < 12 + 16) throw new Error("Ciphertext too short");
77
+ const iv = combined.subarray(0, 12);
78
+ const ct = combined.subarray(12);
79
+ const pt = await crypto.subtle.decrypt(
80
+ { name: CRYPTO_ALG, iv: iv as BufferSource },
81
+ key,
82
+ ct as BufferSource,
83
+ );
84
+ return new TextDecoder().decode(pt);
85
+ }
86
+
87
+ /** Convenience: encrypt a JSON-serializable value as a single ciphertext blob. */
88
+ export async function encryptJson(key: CryptoKey, value: unknown): Promise<string> {
89
+ return encryptString(key, JSON.stringify(value));
90
+ }
91
+
92
+ /** Convenience: decrypt a ciphertext blob into a JSON value. */
93
+ export async function decryptJson<T = unknown>(key: CryptoKey, ciphertext: string): Promise<T> {
94
+ return JSON.parse(await decryptString(key, ciphertext)) as T;
95
+ }
96
+
97
+ // --- base64 helpers ---------------------------------------------------------
98
+
99
+ function b64FromBytes(bytes: Uint8Array): string {
100
+ let bin = "";
101
+ // Chunk to avoid stack overflow on large inputs.
102
+ const CHUNK = 0x8000;
103
+ for (let i = 0; i < bytes.byteLength; i += CHUNK) {
104
+ bin += String.fromCharCode.apply(null, Array.from(bytes.subarray(i, i + CHUNK)));
105
+ }
106
+ return btoa(bin);
107
+ }
108
+
109
+ function bytesFromB64(s: string): Uint8Array {
110
+ const bin = atob(s);
111
+ const out = new Uint8Array(bin.length);
112
+ for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
113
+ return out;
114
+ }
115
+
116
+ function b64urlFromBytes(bytes: Uint8Array): string {
117
+ return b64FromBytes(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
118
+ }
119
+
120
+ function bytesFromB64url(s: string): Uint8Array {
121
+ const padded = s.replace(/-/g, "+").replace(/_/g, "/") + "===".slice((s.length + 3) % 4);
122
+ return bytesFromB64(padded);
123
+ }