@query-farm/vgi 0.48.4 → 0.48.5

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 (44) hide show
  1. package/README.md +51 -0
  2. package/dist/attach-secret-keyfile.d.ts +11 -0
  3. package/dist/attach-secret-options.d.ts +96 -0
  4. package/dist/catalog/attach-option.d.ts +6 -0
  5. package/dist/catalog/interface.d.ts +15 -0
  6. package/dist/client-entry.js +1 -1
  7. package/dist/client-entry.js.map +4 -4
  8. package/dist/index.core.d.ts +2 -0
  9. package/dist/index.js +5700 -5276
  10. package/dist/index.js.map +24 -22
  11. package/dist/protocol/dispatch.d.ts +11 -0
  12. package/dist/protocol/handlers/catalog/admin.d.ts +1 -1
  13. package/dist/protocol/handlers/catalog/index.d.ts +1 -1
  14. package/dist/protocol/handlers/catalog/index_methods.d.ts +1 -1
  15. package/dist/protocol/handlers/catalog/macro.d.ts +1 -1
  16. package/dist/protocol/handlers/catalog/shared.d.ts +28 -3
  17. package/dist/protocol/handlers/catalog/table.d.ts +1 -1
  18. package/dist/protocol/handlers/catalog/view.d.ts +1 -1
  19. package/dist/protocol/handlers/function.d.ts +2 -0
  20. package/dist/protocol/handlers/table-buffering.d.ts +1 -1
  21. package/dist/serve-entry.d.ts +5 -0
  22. package/dist/serve-entry.js +475 -94
  23. package/dist/serve-entry.js.map +18 -16
  24. package/dist/worker-cf-entry.js +497 -93
  25. package/dist/worker-cf-entry.js.map +18 -16
  26. package/dist/worker.d.ts +10 -0
  27. package/package.json +1 -1
  28. package/src/attach-secret-keyfile.ts +185 -0
  29. package/src/attach-secret-options.ts +418 -0
  30. package/src/catalog/attach-option.ts +6 -0
  31. package/src/catalog/interface.ts +19 -0
  32. package/src/index.core.ts +10 -0
  33. package/src/protocol/dispatch.ts +14 -2
  34. package/src/protocol/handlers/catalog/admin.ts +33 -18
  35. package/src/protocol/handlers/catalog/index.ts +6 -5
  36. package/src/protocol/handlers/catalog/index_methods.ts +5 -3
  37. package/src/protocol/handlers/catalog/macro.ts +7 -5
  38. package/src/protocol/handlers/catalog/shared.ts +52 -6
  39. package/src/protocol/handlers/catalog/table.ts +24 -22
  40. package/src/protocol/handlers/catalog/view.ts +9 -7
  41. package/src/protocol/handlers/function.ts +11 -5
  42. package/src/protocol/handlers/table-buffering.ts +2 -1
  43. package/src/serve-entry.ts +6 -1
  44. package/src/worker.ts +12 -0
package/src/index.core.ts CHANGED
@@ -239,6 +239,16 @@ export {
239
239
  RESERVED_ATTACH_OPTION,
240
240
  ReservedAttachOptionError,
241
241
  } from "./catalog/attach-option.js";
242
+ // ATTACH options (secret ones included) as functions and transaction hooks see them.
243
+ export {
244
+ attachOptions,
245
+ attachOption,
246
+ type AttachOptionValues,
247
+ AttachOptionsUnavailableError,
248
+ AttachSecretKeyUnavailableError,
249
+ ATTACH_SECRET_OPTIONS_INFO,
250
+ } from "./attach-secret-options.js";
251
+ export { hostLocalAttachSecretKeyPath, ATTACH_SECRET_KEY_FILE_ENV } from "./attach-secret-keyfile.js";
242
252
  export {
243
253
  ATTACH_TICKET_OPTION,
244
254
  ATTACH_TICKET_PREFIX,
@@ -30,6 +30,17 @@ export interface ProtocolConfig {
30
30
  * unsealed — OS process ownership already enforces identity there.
31
31
  */
32
32
  signingKey?: Uint8Array | undefined;
33
+ /**
34
+ * Optional root key for secret ATTACH options (`AttachOptionSpec.secret`),
35
+ * which the framework encrypts inside `attach_opaque_data` so functions and
36
+ * transaction hooks can read them with `attachOptions()`. Defaults to the
37
+ * signing key when there is one (HTTP), else the host-local key file
38
+ * (`$VGI_ATTACH_SECRET_KEY_FILE`, or `vgi/attach-secret-options.key` under
39
+ * `$XDG_RUNTIME_DIR` / the per-user temp dir). Any length; the AEAD key is
40
+ * always derived from it with HKDF-SHA256. Every worker process that may
41
+ * receive a given attach must use the same key.
42
+ */
43
+ attachSecretKey?: Uint8Array | undefined;
33
44
  }
34
45
 
35
46
  /**
@@ -85,11 +96,12 @@ export function buildVgiService(config: ProtocolConfig): VgiService {
85
96
  ...functionHandlers({
86
97
  registry: config.registry,
87
98
  signingKey: config.signingKey,
99
+ attachSecretKey: config.attachSecretKey,
88
100
  catalogInterface: config.catalogInterface,
89
101
  }),
90
102
  ...aggregateHandlers(config.registry),
91
- ...tableBufferingHandlers(config.registry, config.signingKey),
92
- ...catalogHandlers(config.catalogInterface, config.signingKey),
103
+ ...tableBufferingHandlers(config.registry, config.signingKey, config.attachSecretKey),
104
+ ...catalogHandlers(config.catalogInterface, config.signingKey, config.attachSecretKey),
93
105
  };
94
106
  }
95
107
 
@@ -33,6 +33,8 @@ import {
33
33
  import { toUint8Array } from "../../../util/bytes.js";
34
34
  import { redeemAttachTicket } from "../../../attach-ticket.js";
35
35
  import { currentRequestAuth } from "../../../request-auth.js";
36
+ import { secretAttachOptionNames } from "../../../attach-secret-options.js";
37
+ import { deserializeAttachOptionSpec } from "../../../catalog/attach-option.js";
36
38
  import { decodeDictValue } from "../../../util/arrow/index.js";
37
39
  import {
38
40
  unwrapRequest,
@@ -45,11 +47,13 @@ import {
45
47
  sealAttach,
46
48
  openAttach,
47
49
  sealTransaction,
50
+ type OpaqueKeys,
48
51
  } from "./shared.js";
49
52
 
50
- export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array): Partial<VgiService> {
53
+ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array, attachSecretKey?: Uint8Array): Partial<VgiService> {
54
+ const keys: OpaqueKeys = { signingKey, attachSecretKey };
51
55
  // catalog_catalogs
52
- const catalogCatalogs = catalogHandler(signingKey, async () => {
56
+ const catalogCatalogs = catalogHandler(keys, async () => {
53
57
  const cat = getCatalog();
54
58
  // Each catalog advertised as an IPC-serialized CatalogInfo
55
59
  // {name, implementation_version?, data_version_spec?}. Versioned workers
@@ -121,10 +125,21 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
121
125
  // Seal the attach value into an AEAD envelope bound to the caller's
122
126
  // identity before it leaves the worker (HTTP transport; pass-through
123
127
  // when there is no signing key).
128
+ // The framework packs the attach's options after the catalog's bytes —
129
+ // secret ones (declared `secret: true`) encrypted with a worker-held key —
130
+ // so every function call and transaction hook of this attach can read them
131
+ // with attachOptions(), on every transport, without a catalog ever putting
132
+ // a credential into its own attach bytes.
133
+ const optionsIpc = innerParams.options == null ? null : toUint8Array(innerParams.options);
134
+ const secretNames =
135
+ optionsIpc && optionsIpc.length > 0
136
+ ? await secretAttachOptionNames(cat as never, innerParams.name, deserializeAttachOptionSpec)
137
+ : new Set<string>();
124
138
  const sealedAttach = await sealAttach(
125
139
  toUint8Array(result.attach_opaque_data),
126
140
  (ctx as CallContext | undefined)?.auth,
127
141
  signingKey,
142
+ { optionsIpc, secretNames, attachSecretKey },
128
143
  );
129
144
  // The generated builder emits every column of the pinned
130
145
  // CatalogAttachResult schema, filling vgi-python's defaults for any a
@@ -142,28 +157,28 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
142
157
  };
143
158
 
144
159
  // catalog_detach
145
- const catalogDetach = catalogHandler(signingKey, async (params) => {
160
+ const catalogDetach = catalogHandler(keys, async (params) => {
146
161
  const cat = getCatalog();
147
162
  await cat.detach(toUint8Array(params.attach_opaque_data));
148
163
  return {};
149
164
  });
150
165
 
151
166
  // catalog_create
152
- const catalogCreate = catalogHandler(signingKey, async (params) => {
167
+ const catalogCreate = catalogHandler(keys, async (params) => {
153
168
  const cat = getCatalog();
154
169
  await cat.create(params.name, params.on_conflict, params.options);
155
170
  return {};
156
171
  });
157
172
 
158
173
  // catalog_drop
159
- const catalogDrop = catalogHandler(signingKey, async (params) => {
174
+ const catalogDrop = catalogHandler(keys, async (params) => {
160
175
  const cat = getCatalog();
161
176
  await cat.drop(params.name);
162
177
  return {};
163
178
  });
164
179
 
165
180
  // catalog_version
166
- const catalogVersion = catalogHandler(signingKey, async (params) => {
181
+ const catalogVersion = catalogHandler(keys, async (params) => {
167
182
  const cat = getCatalog();
168
183
  const version = await cat.version(
169
184
  toUint8Array(params.attach_opaque_data),
@@ -179,7 +194,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
179
194
  const auth = (ctx as CallContext | undefined)?.auth;
180
195
  const sealedAttach =
181
196
  params.attach_opaque_data != null ? toUint8Array(params.attach_opaque_data) : new Uint8Array(0);
182
- const attachPlain = await openAttach(sealedAttach, auth, signingKey);
197
+ const attachPlain = await openAttach(sealedAttach, auth, signingKey, attachSecretKey);
183
198
  const cat = getCatalog();
184
199
  const txPlain = await cat.transactionBegin(attachPlain);
185
200
  // Seal the transaction value, binding it to the caller's identity and
@@ -190,7 +205,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
190
205
  };
191
206
 
192
207
  // catalog_transaction_commit
193
- const catalogTransactionCommit = catalogHandler(signingKey, async (params) => {
208
+ const catalogTransactionCommit = catalogHandler(keys, async (params) => {
194
209
  const cat = getCatalog();
195
210
  await cat.transactionCommit(
196
211
  toUint8Array(params.attach_opaque_data),
@@ -200,7 +215,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
200
215
  });
201
216
 
202
217
  // catalog_transaction_rollback
203
- const catalogTransactionRollback = catalogHandler(signingKey, async (params) => {
218
+ const catalogTransactionRollback = catalogHandler(keys, async (params) => {
204
219
  const cat = getCatalog();
205
220
  await cat.transactionRollback(
206
221
  toUint8Array(params.attach_opaque_data),
@@ -210,7 +225,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
210
225
  });
211
226
 
212
227
  // catalog_schemas
213
- const catalogSchemas = catalogHandler(signingKey, async (params) => {
228
+ const catalogSchemas = catalogHandler(keys, async (params) => {
214
229
  const cat = getCatalog();
215
230
  const schemas = await cat.schemas(
216
231
  toUint8Array(params.attach_opaque_data),
@@ -229,7 +244,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
229
244
  // rules, the content-hash etag and the frozen-catalog cache.
230
245
  const catalogContents: VgiService["catalogContents"] = async (params, ctx) => {
231
246
  const sealedAttach = toUint8Array(params.attach_opaque_data).slice();
232
- return catalogHandler(signingKey, async (opened, context) => {
247
+ return catalogHandler(keys, async (opened, context) => {
233
248
  const ifNoneMatch = opened.if_none_match == null ? null : String(opened.if_none_match);
234
249
  return serveCatalogContents(getCatalog(), toUint8Array(opened.attach_opaque_data), ifNoneMatch, context, sealedAttach);
235
250
  })(params, ctx);
@@ -237,7 +252,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
237
252
 
238
253
  // catalog_copy_from_formats — catalog-level (not schema-scoped). Lists the
239
254
  // custom COPY ... FROM formats this catalog advertises; empty list when none.
240
- const catalogCopyFromFormats = catalogHandler(signingKey, async (params) => {
255
+ const catalogCopyFromFormats = catalogHandler(keys, async (params) => {
241
256
  const cat = getCatalog();
242
257
  const formats = await cat.copyFromFormats(
243
258
  toUint8Array(params.attach_opaque_data),
@@ -249,7 +264,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
249
264
  });
250
265
 
251
266
  // catalog_schema_get
252
- const catalogSchemaGet = catalogHandler(signingKey, async (params) => {
267
+ const catalogSchemaGet = catalogHandler(keys, async (params) => {
253
268
  const cat = getCatalog();
254
269
  const info = await cat.schemaGet(
255
270
  toUint8Array(params.attach_opaque_data),
@@ -262,7 +277,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
262
277
  });
263
278
 
264
279
  // catalog_schema_create
265
- const catalogSchemaCreate = catalogHandler(signingKey, async (params) => {
280
+ const catalogSchemaCreate = catalogHandler(keys, async (params) => {
266
281
  if (params.options != null) throw new MethodNotImplementedError("catalog_schema_create requires negotiated vgi.schema.options.create.v1");
267
282
  const cat = getCatalog();
268
283
  await cat.schemaCreate(
@@ -277,7 +292,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
277
292
  });
278
293
 
279
294
  // catalog_schema_drop
280
- const catalogSchemaDrop = catalogHandler(signingKey, async (params) => {
295
+ const catalogSchemaDrop = catalogHandler(keys, async (params) => {
281
296
  const cat = getCatalog();
282
297
  await cat.schemaDrop(
283
298
  toUint8Array(params.attach_opaque_data),
@@ -290,7 +305,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
290
305
  });
291
306
 
292
307
  // catalog_schema_contents_tables
293
- const catalogSchemaContentsTables = catalogHandler(signingKey, async (params) => {
308
+ const catalogSchemaContentsTables = catalogHandler(keys, async (params) => {
294
309
  const cat = getCatalog();
295
310
  const tables = await cat.schemaContentsTables(
296
311
  toUint8Array(params.attach_opaque_data),
@@ -303,7 +318,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
303
318
  });
304
319
 
305
320
  // catalog_schema_contents_views
306
- const catalogSchemaContentsViews = catalogHandler(signingKey, async (params) => {
321
+ const catalogSchemaContentsViews = catalogHandler(keys, async (params) => {
307
322
  const cat = getCatalog();
308
323
  const views = await cat.schemaContentsViews(
309
324
  toUint8Array(params.attach_opaque_data),
@@ -316,7 +331,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
316
331
  });
317
332
 
318
333
  // catalog_schema_contents_functions
319
- const catalogSchemaContentsFunctions = catalogHandler(signingKey, async (params) => {
334
+ const catalogSchemaContentsFunctions = catalogHandler(keys, async (params) => {
320
335
  const cat = getCatalog();
321
336
  const funcs = await cat.schemaContentsFunctions(
322
337
  toUint8Array(params.attach_opaque_data),
@@ -14,13 +14,14 @@ import { catalogIndexHandlers } from "./index_methods.js";
14
14
  export function catalogHandlers(
15
15
  catalog: CatalogInterface | undefined,
16
16
  signingKey?: Uint8Array,
17
+ attachSecretKey?: Uint8Array,
17
18
  ): Partial<VgiService> {
18
19
  const getCatalog = makeGetCatalog(catalog);
19
20
  return {
20
- ...catalogAdminHandlers(getCatalog, signingKey),
21
- ...catalogTableHandlers(getCatalog, signingKey),
22
- ...catalogViewHandlers(getCatalog, signingKey),
23
- ...catalogMacroHandlers(getCatalog, signingKey),
24
- ...catalogIndexHandlers(getCatalog, signingKey),
21
+ ...catalogAdminHandlers(getCatalog, signingKey, attachSecretKey),
22
+ ...catalogTableHandlers(getCatalog, signingKey, attachSecretKey),
23
+ ...catalogViewHandlers(getCatalog, signingKey, attachSecretKey),
24
+ ...catalogMacroHandlers(getCatalog, signingKey, attachSecretKey),
25
+ ...catalogIndexHandlers(getCatalog, signingKey, attachSecretKey),
25
26
  };
26
27
  }
@@ -14,11 +14,13 @@ import {
14
14
  import {
15
15
  type GetCatalog,
16
16
  catalogHandler,
17
+ type OpaqueKeys,
17
18
  } from "./shared.js";
18
19
 
19
- export function catalogIndexHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array): Partial<VgiService> {
20
+ export function catalogIndexHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array, attachSecretKey?: Uint8Array): Partial<VgiService> {
21
+ const keys: OpaqueKeys = { signingKey, attachSecretKey };
20
22
  // catalog_schema_contents_indexes
21
- const catalogSchemaContentsIndexes = catalogHandler(signingKey, async (params) => {
23
+ const catalogSchemaContentsIndexes = catalogHandler(keys, async (params) => {
22
24
  const cat = getCatalog();
23
25
  const indexes = await cat.schemaContentsIndexes(
24
26
  toUint8Array(params.attach_opaque_data),
@@ -31,7 +33,7 @@ export function catalogIndexHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
31
33
  });
32
34
 
33
35
  // catalog_index_get
34
- const catalogIndexGet = catalogHandler(signingKey, async (params) => {
36
+ const catalogIndexGet = catalogHandler(keys, async (params) => {
35
37
  const cat = getCatalog();
36
38
  const info = await cat.indexGet(
37
39
  toUint8Array(params.attach_opaque_data),
@@ -19,11 +19,13 @@ import {
19
19
  import {
20
20
  type GetCatalog,
21
21
  catalogHandler,
22
+ type OpaqueKeys,
22
23
  } from "./shared.js";
23
24
 
24
- export function catalogMacroHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array): Partial<VgiService> {
25
+ export function catalogMacroHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array, attachSecretKey?: Uint8Array): Partial<VgiService> {
26
+ const keys: OpaqueKeys = { signingKey, attachSecretKey };
25
27
  // catalog_macro_get
26
- const catalogMacroGet = catalogHandler(signingKey, async (params) => {
28
+ const catalogMacroGet = catalogHandler(keys, async (params) => {
27
29
  const cat = getCatalog();
28
30
  const info = await cat.macroGet(
29
31
  toUint8Array(params.attach_opaque_data),
@@ -37,7 +39,7 @@ export function catalogMacroHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
37
39
  });
38
40
 
39
41
  // catalog_macro_create
40
- const catalogMacroCreate = catalogHandler(signingKey, async (params) => {
42
+ const catalogMacroCreate = catalogHandler(keys, async (params) => {
41
43
  const cat = getCatalog();
42
44
  // catalogHandler already flattened the wrapped request.
43
45
  const innerParams = decodeASD<MacroCreateRequest>(MacroCreateRequestSchema, encodeASD(MacroCreateRequestSchema, params));
@@ -55,7 +57,7 @@ export function catalogMacroHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
55
57
  });
56
58
 
57
59
  // catalog_macro_drop
58
- const catalogMacroDrop = catalogHandler(signingKey, async (params) => {
60
+ const catalogMacroDrop = catalogHandler(keys, async (params) => {
59
61
  const cat = getCatalog();
60
62
  await cat.macroDrop(
61
63
  toUint8Array(params.attach_opaque_data),
@@ -68,7 +70,7 @@ export function catalogMacroHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
68
70
  });
69
71
 
70
72
  // catalog_schema_contents_macros
71
- const catalogSchemaContentsMacros = catalogHandler(signingKey, async (params) => {
73
+ const catalogSchemaContentsMacros = catalogHandler(keys, async (params) => {
72
74
  const cat = getCatalog();
73
75
  const macros = await cat.schemaContentsMacros(
74
76
  toUint8Array(params.attach_opaque_data),
@@ -17,6 +17,30 @@ import {
17
17
  ATTACH_UUID_LEN,
18
18
  TRANSACTION_ENVELOPE_VERSION,
19
19
  } from "../../../crypto.js";
20
+ import { openFrameworkAttach, packAttachOptionsTrailer } from "../../../attach-secret-options.js";
21
+
22
+ /**
23
+ * The keys a catalog handler group opens opaque values with: the HTTP signing
24
+ * key (absent on stdio / unix) and the optional explicit root key for secret
25
+ * ATTACH options (`attachSecretKey`; see ../../../attach-secret-options.ts).
26
+ */
27
+ export interface OpaqueKeys {
28
+ signingKey?: Uint8Array | undefined;
29
+ attachSecretKey?: Uint8Array | undefined;
30
+ }
31
+
32
+ /**
33
+ * The options a catalog_attach packs into the attach value after the catalog's
34
+ * own bytes (secret ones encrypted). See ../../../attach-secret-options.ts.
35
+ */
36
+ export interface AttachOptionsToPack {
37
+ /** The request's `options` IPC batch (null / empty: no options, no trailer). */
38
+ optionsIpc: Uint8Array | null | undefined;
39
+ /** Lower-cased names of the options declared `secret: true`. */
40
+ secretNames: ReadonlySet<string>;
41
+ /** Explicit root key for the secret options, if configured. */
42
+ attachSecretKey?: Uint8Array | undefined;
43
+ }
20
44
 
21
45
  export type GetCatalog = () => CatalogInterface;
22
46
 
@@ -36,17 +60,31 @@ export type GetCatalog = () => CatalogInterface;
36
60
  * across re-seals and globally unique, unlike the random-nonce ciphertext or
37
61
  * the (possibly non-unique) catalog bytes. openAttach strips the UUID back off
38
62
  * so the catalog only ever sees its own bytes. Mirrors vgi-python/-go.
63
+ *
64
+ * With `options`, the attach's ATTACH options are packed after the catalog's
65
+ * bytes (secret ones encrypted, never plaintext) so `attachOptions()` can hand
66
+ * them to every function call and transaction hook of this attach.
39
67
  */
40
68
  export async function sealAttach(
41
69
  plaintext: Uint8Array,
42
70
  auth: AuthContext | undefined,
43
71
  signingKey: Uint8Array | undefined,
72
+ options?: AttachOptionsToPack,
44
73
  ): Promise<Uint8Array> {
45
74
  const uuid = new Uint8Array(ATTACH_UUID_LEN);
46
75
  crypto.getRandomValues(uuid);
47
- const minted = new Uint8Array(ATTACH_UUID_LEN + plaintext.length);
76
+ // The attach's options ride after the catalog's bytes, the secret ones
77
+ // encrypted under a key bound to this uuid (empty when there are none).
78
+ const trailer = options
79
+ ? await packAttachOptionsTrailer(uuid, options.optionsIpc, options.secretNames, {
80
+ explicit: options.attachSecretKey,
81
+ signingKey,
82
+ })
83
+ : new Uint8Array(0);
84
+ const minted = new Uint8Array(ATTACH_UUID_LEN + plaintext.length + trailer.length);
48
85
  minted.set(uuid, 0);
49
86
  minted.set(plaintext, ATTACH_UUID_LEN);
87
+ minted.set(trailer, ATTACH_UUID_LEN + plaintext.length);
50
88
  if (!signingKey) return minted;
51
89
  return sealBytes(minted, signingKey, attachAad(auth), ATTACH_ENVELOPE_VERSION);
52
90
  }
@@ -73,9 +111,12 @@ export async function openAttach(
73
111
  envelope: Uint8Array,
74
112
  auth: AuthContext | undefined,
75
113
  signingKey: Uint8Array | undefined,
114
+ attachSecretKey?: Uint8Array,
76
115
  ): Promise<Uint8Array> {
77
116
  const full = await openAttachFull(envelope, auth, signingKey);
78
- return full.length < ATTACH_UUID_LEN ? full : full.subarray(ATTACH_UUID_LEN);
117
+ // Strips the uuid and the options trailer, decrypting secret options and
118
+ // recording all of them for attachOptions().
119
+ return openFrameworkAttach(full, { explicit: attachSecretKey, signingKey });
79
120
  }
80
121
 
81
122
  /** Hex of a byte array (lowercase). */
@@ -154,8 +195,9 @@ export async function openTransaction(
154
195
  async function unwrapParamsOpaque(
155
196
  params: Record<string, any>,
156
197
  ctx: CallContext | undefined,
157
- signingKey: Uint8Array | undefined,
198
+ keys: OpaqueKeys,
158
199
  ): Promise<void> {
200
+ const { signingKey, attachSecretKey } = keys;
159
201
  const auth = ctx?.auth;
160
202
  let sealedAttach: Uint8Array = new Uint8Array(0);
161
203
  if (params.attach_opaque_data != null) {
@@ -171,7 +213,7 @@ async function unwrapParamsOpaque(
171
213
  const full = signingKey
172
214
  ? await openBytes(env, signingKey, attachAad(auth), ATTACH_ENVELOPE_VERSION, "attach_opaque_data")
173
215
  : env;
174
- params.attach_opaque_data = full.length < ATTACH_UUID_LEN ? full : full.subarray(ATTACH_UUID_LEN);
216
+ params.attach_opaque_data = await openFrameworkAttach(full, { explicit: attachSecretKey, signingKey });
175
217
  }
176
218
  }
177
219
  // transaction_opaque_data is sealed only on HTTP (no UUID prefix); subprocess
@@ -197,9 +239,13 @@ async function unwrapParamsOpaque(
197
239
  * bespoke. The body sees the flattened params as a plain record.
198
240
  */
199
241
  export function catalogHandler<R>(
200
- signingKey: Uint8Array | undefined,
242
+ keysOrSigningKey: OpaqueKeys | Uint8Array | undefined,
201
243
  handler: (params: Record<string, any>, ctx: CallContext) => R | Promise<R>,
202
244
  ): (params: object, ctx: CallContext) => Promise<R> {
245
+ const keys: OpaqueKeys =
246
+ keysOrSigningKey instanceof Uint8Array || keysOrSigningKey === undefined
247
+ ? { signingKey: keysOrSigningKey }
248
+ : keysOrSigningKey;
203
249
  return async (wire: object, ctx: CallContext) => {
204
250
  let params = wire as Record<string, any>;
205
251
  // catalog_create / catalog_table_create / catalog_macro_create (and
@@ -214,7 +260,7 @@ export function catalogHandler<R>(
214
260
  params = unwrapRequest(params.request);
215
261
  }
216
262
  normalizeRequestSchemaPaths(params);
217
- await unwrapParamsOpaque(params, ctx, signingKey);
263
+ await unwrapParamsOpaque(params, ctx, keys);
218
264
  return handler(params, ctx);
219
265
  };
220
266
  }
@@ -22,6 +22,7 @@ import {
22
22
  import {
23
23
  type GetCatalog,
24
24
  catalogHandler,
25
+ type OpaqueKeys,
25
26
  } from "./shared.js";
26
27
 
27
28
  function requireFlatColumnPath(value: unknown, operation: string): string[] {
@@ -35,9 +36,10 @@ function requireFlatColumnPath(value: unknown, operation: string): string[] {
35
36
  return path as string[];
36
37
  }
37
38
 
38
- export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array): Partial<VgiService> {
39
+ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array, attachSecretKey?: Uint8Array): Partial<VgiService> {
40
+ const keys: OpaqueKeys = { signingKey, attachSecretKey };
39
41
  // catalog_table_get
40
- const catalogTableGet = catalogHandler(signingKey, async (params) => {
42
+ const catalogTableGet = catalogHandler(keys, async (params) => {
41
43
  const cat = getCatalog();
42
44
  const info = await cat.tableGet(
43
45
  toUint8Array(params.attach_opaque_data),
@@ -53,7 +55,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
53
55
  });
54
56
 
55
57
  // catalog_table_create
56
- const catalogTableCreate = catalogHandler(signingKey, async (params) => {
58
+ const catalogTableCreate = catalogHandler(keys, async (params) => {
57
59
  if (params.constraint_definitions == null) throw new Error("constraint_definitions is required");
58
60
  const definitions = Array.from(params.constraint_definitions as Iterable<unknown>, (d) => toUint8Array(d as Uint8Array));
59
61
  if (params.comment != null || params.tags != null || params.options != null || params.layout != null) {
@@ -89,7 +91,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
89
91
 
90
92
  // catalog_table_constraints_apply (ALTER TABLE ... SET/DROP NOT NULL and
91
93
  // every other canonical constraint change).
92
- const catalogTableConstraintsApply = catalogHandler(signingKey, async (params) => {
94
+ const catalogTableConstraintsApply = catalogHandler(keys, async (params) => {
93
95
  const attach = toUint8Array(params.attach_opaque_data);
94
96
  const cat = getCatalog();
95
97
  const caps = await cat.catalogCapabilities(attach);
@@ -124,7 +126,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
124
126
  });
125
127
 
126
128
  // catalog_table_drop
127
- const catalogTableDrop = catalogHandler(signingKey, async (params) => {
129
+ const catalogTableDrop = catalogHandler(keys, async (params) => {
128
130
  const cat = getCatalog();
129
131
  await cat.tableDrop(
130
132
  toUint8Array(params.attach_opaque_data),
@@ -137,7 +139,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
137
139
  });
138
140
 
139
141
  // catalog_table_column_statistics_get
140
- const catalogTableColumnStatisticsGet = catalogHandler(signingKey, async (params) => {
142
+ const catalogTableColumnStatisticsGet = catalogHandler(keys, async (params) => {
141
143
  const cat = getCatalog();
142
144
  const stats = await cat.tableColumnStatisticsGet(
143
145
  toUint8Array(params.attach_opaque_data),
@@ -149,7 +151,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
149
151
  });
150
152
 
151
153
  // catalog_table_scan_function_get
152
- const catalogTableScanFunctionGet = catalogHandler(signingKey, async (params) => {
154
+ const catalogTableScanFunctionGet = catalogHandler(keys, async (params) => {
153
155
  const cat = getCatalog();
154
156
  const scanResult = await cat.tableScanFunctionGet(
155
157
  toUint8Array(params.attach_opaque_data),
@@ -167,7 +169,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
167
169
  // into TableInfo is only asked when the client skipped the inline value.
168
170
  const optionalTx = (params: Record<string, any>): Uint8Array | undefined =>
169
171
  params.transaction_opaque_data ? toUint8Array(params.transaction_opaque_data) : undefined;
170
- const catalogTableInsertFunctionGet = catalogHandler(signingKey, async (params) => {
172
+ const catalogTableInsertFunctionGet = catalogHandler(keys, async (params) => {
171
173
  const result = await getCatalog().tableInsertFunctionGet(
172
174
  toUint8Array(params.attach_opaque_data),
173
175
  params.schema_path,
@@ -177,7 +179,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
177
179
  );
178
180
  return wrapResult(result, ScanFunctionResultSchema);
179
181
  });
180
- const catalogTableUpdateFunctionGet = catalogHandler(signingKey, async (params) => {
182
+ const catalogTableUpdateFunctionGet = catalogHandler(keys, async (params) => {
181
183
  const result = await getCatalog().tableUpdateFunctionGet(
182
184
  toUint8Array(params.attach_opaque_data),
183
185
  params.schema_path,
@@ -186,7 +188,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
186
188
  );
187
189
  return wrapResult(result, ScanFunctionResultSchema);
188
190
  });
189
- const catalogTableDeleteFunctionGet = catalogHandler(signingKey, async (params) => {
191
+ const catalogTableDeleteFunctionGet = catalogHandler(keys, async (params) => {
190
192
  const result = await getCatalog().tableDeleteFunctionGet(
191
193
  toUint8Array(params.attach_opaque_data),
192
194
  params.schema_path,
@@ -199,7 +201,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
199
201
  // catalog_table_scan_branches_get — branches-aware variant of
200
202
  // scan_function_get. The branches-aware C++ extension calls this for every
201
203
  // table scan; single-source catalogs synthesise a one-branch result.
202
- const catalogTableScanBranchesGet = catalogHandler(signingKey, async (params) => {
204
+ const catalogTableScanBranchesGet = catalogHandler(keys, async (params) => {
203
205
  const cat = getCatalog();
204
206
  const branchesResult = await cat.tableScanBranchesGet(
205
207
  toUint8Array(params.attach_opaque_data),
@@ -213,7 +215,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
213
215
  });
214
216
 
215
217
  // catalog_table_comment_set
216
- const catalogTableCommentSet = catalogHandler(signingKey, async (params) => {
218
+ const catalogTableCommentSet = catalogHandler(keys, async (params) => {
217
219
  const cat = getCatalog();
218
220
  await cat.tableCommentSet(
219
221
  toUint8Array(params.attach_opaque_data),
@@ -227,7 +229,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
227
229
  });
228
230
 
229
231
  // catalog_table_rename
230
- const catalogTableRename = catalogHandler(signingKey, async (params) => {
232
+ const catalogTableRename = catalogHandler(keys, async (params) => {
231
233
  const cat = getCatalog();
232
234
  await cat.tableRename(
233
235
  toUint8Array(params.attach_opaque_data),
@@ -241,7 +243,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
241
243
  });
242
244
 
243
245
  // catalog_table_column_add
244
- const catalogTableColumnAdd = catalogHandler(signingKey, async (params) => {
246
+ const catalogTableColumnAdd = catalogHandler(keys, async (params) => {
245
247
  params.column_path = requireFlatColumnPath(params.column_path, "catalog_table_column_add");
246
248
  validateColumnMetadataIpc(toUint8Array(params.column_definition));
247
249
  validateColumnExpressionsIpc(toUint8Array(params.column_definition), params.schema_path);
@@ -259,7 +261,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
259
261
  return {};
260
262
  });
261
263
  // catalog_table_column_drop
262
- const catalogTableColumnDrop = catalogHandler(signingKey, async (params) => {
264
+ const catalogTableColumnDrop = catalogHandler(keys, async (params) => {
263
265
  params.column_path = requireFlatColumnPath(params.column_path, "catalog_table_column_drop");
264
266
  const cat = getCatalog();
265
267
  await cat.tableColumnDrop(
@@ -275,7 +277,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
275
277
  return {};
276
278
  });
277
279
  // catalog_table_column_rename
278
- const catalogTableColumnRename = catalogHandler(signingKey, async (params) => {
280
+ const catalogTableColumnRename = catalogHandler(keys, async (params) => {
279
281
  params.column_path = requireFlatColumnPath(params.column_path, "catalog_table_column_rename");
280
282
  const cat = getCatalog();
281
283
  await cat.tableColumnRename(
@@ -290,7 +292,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
290
292
  return {};
291
293
  });
292
294
  // catalog_table_column_comment_set
293
- const catalogTableColumnCommentSet = catalogHandler(signingKey, async (params) => {
295
+ const catalogTableColumnCommentSet = catalogHandler(keys, async (params) => {
294
296
  params.column_path = requireFlatColumnPath(params.column_path, "catalog_table_column_comment_set");
295
297
  const cat = getCatalog();
296
298
  await cat.tableColumnCommentSet(
@@ -305,7 +307,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
305
307
  return {};
306
308
  });
307
309
  // catalog_table_column_default_set
308
- const catalogTableColumnDefaultSet = catalogHandler(signingKey, async (params) => {
310
+ const catalogTableColumnDefaultSet = catalogHandler(keys, async (params) => {
309
311
  params.column_path = requireFlatColumnPath(params.column_path, "catalog_table_column_default_set");
310
312
  const cat = getCatalog();
311
313
  await cat.tableColumnDefaultSet(
@@ -320,7 +322,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
320
322
  return {};
321
323
  });
322
324
  // catalog_table_column_default_drop
323
- const catalogTableColumnDefaultDrop = catalogHandler(signingKey, async (params) => {
325
+ const catalogTableColumnDefaultDrop = catalogHandler(keys, async (params) => {
324
326
  params.column_path = requireFlatColumnPath(params.column_path, "catalog_table_column_default_drop");
325
327
  const cat = getCatalog();
326
328
  await cat.tableColumnDefaultDrop(
@@ -334,7 +336,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
334
336
  return {};
335
337
  });
336
338
  // catalog_table_column_type_change
337
- const catalogTableColumnTypeChange = catalogHandler(signingKey, async (params) => {
339
+ const catalogTableColumnTypeChange = catalogHandler(keys, async (params) => {
338
340
  params.column_path = requireFlatColumnPath(params.column_path, "catalog_table_column_type_change");
339
341
  const cat = getCatalog();
340
342
  await cat.tableColumnTypeChange(
@@ -350,7 +352,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
350
352
  return {};
351
353
  });
352
354
  // catalog_table_not_null_set
353
- const catalogTableNotNullSet = catalogHandler(signingKey, async (params) => {
355
+ const catalogTableNotNullSet = catalogHandler(keys, async (params) => {
354
356
  params.column_path = requireFlatColumnPath(params.column_path, "catalog_table_not_null_set");
355
357
  const cat = getCatalog();
356
358
  await cat.tableNotNullSet(
@@ -364,7 +366,7 @@ export function catalogTableHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
364
366
  return {};
365
367
  });
366
368
  // catalog_table_not_null_drop
367
- const catalogTableNotNullDrop = catalogHandler(signingKey, async (params) => {
369
+ const catalogTableNotNullDrop = catalogHandler(keys, async (params) => {
368
370
  params.column_path = requireFlatColumnPath(params.column_path, "catalog_table_not_null_drop");
369
371
  const cat = getCatalog();
370
372
  await cat.tableNotNullDrop(