@tokenoftrust/storefront-runner 6.12.0 → 6.12.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.
@@ -14,7 +14,7 @@
14
14
  * bound, the release assigns it and queues the processor's assignment webhook.
15
15
  */
16
16
  import type { Queryable } from "../catalog/d1/catalog.js";
17
- import { orderContactKeysAvailable, sealOrderContact } from "../orders/vault/orderContactVault.js";
17
+ import { prepareOrderContactKeys, sealOrderContact } from "../orders/vault/orderContactVault.js";
18
18
  import { readOrderContactCommand, type OrderContactCommand } from "../orders/vault/orderContact.js";
19
19
  import { grantConsent, grantedByHash } from "../integrations/apps/appContactConsents.js";
20
20
  import { readProcessor, setProcessor } from "./assignmentStore.js";
@@ -29,6 +29,52 @@ const SYNTHETIC_DIGEST = "0".repeat(64);
29
29
  const CONSENT_DAYS = 30;
30
30
 
31
31
  type VaultKms = Parameters<typeof sealOrderContact>[1];
32
+ type GeneratedKey = Awaited<ReturnType<VaultKms["generateDataKey"]>>;
33
+
34
+ /**
35
+ * A KMS client that answers every repeat of a request from its first answer: a repeated data-key
36
+ * generation for the same context, or a decrypt of a blob it generated or already decrypted, makes no
37
+ * further KMS call. Key bytes are handed out as fresh copies because the vault zeroes what it imports;
38
+ * `clear` zeroes the copies it holds.
39
+ */
40
+ function replayingKms(kms: VaultKms): { client: VaultKms; clear(): void } {
41
+ const generated = new Map<string, GeneratedKey>();
42
+ const decrypted = new Map<string, Uint8Array>();
43
+ const keep = (bytes: Uint8Array): Uint8Array => {
44
+ const copy = new Uint8Array(bytes);
45
+ bytes.fill(0);
46
+ return copy;
47
+ };
48
+ const client: VaultKms = {
49
+ async generateDataKey(ctx) {
50
+ const id = JSON.stringify(ctx);
51
+ let first = generated.get(id);
52
+ if (!first) {
53
+ const answer = await kms.generateDataKey(ctx);
54
+ first = { ...answer, plaintext: keep(answer.plaintext) };
55
+ generated.set(id, first);
56
+ decrypted.set(answer.ciphertextBlob, first.plaintext);
57
+ }
58
+ return { ...first, plaintext: new Uint8Array(first.plaintext) };
59
+ },
60
+ async decrypt(blob, ctx, keyArn) {
61
+ let first = decrypted.get(blob);
62
+ if (!first) {
63
+ first = keep(await kms.decrypt(blob, ctx, keyArn));
64
+ decrypted.set(blob, first);
65
+ }
66
+ return new Uint8Array(first);
67
+ },
68
+ };
69
+ return {
70
+ client,
71
+ clear() {
72
+ for (const bytes of decrypted.values()) bytes.fill(0);
73
+ generated.clear();
74
+ decrypted.clear();
75
+ },
76
+ };
77
+ }
32
78
 
33
79
  export interface SyntheticOrderDeps {
34
80
  /** Vault, consent and install reads. */
@@ -147,7 +193,12 @@ export async function bindTestProcessor(
147
193
  };
148
194
  }
149
195
 
150
- /** Write one synthetic preview order, its source authority, and its sealed RFC-reserved recipient. */
196
+ /**
197
+ * Write one synthetic preview order, its source authority, and its sealed RFC-reserved recipient.
198
+ * The vault keys are made durable and opened before the order is written, and the seal reuses the
199
+ * KMS answers from that step, so a KMS failure refuses with nothing written and cannot arrive after
200
+ * the order exists.
201
+ */
151
202
  export async function createSyntheticOrder(
152
203
  deps: SyntheticOrderDeps,
153
204
  scope: SyntheticScope,
@@ -158,9 +209,21 @@ export async function createSyntheticOrder(
158
209
  } catch {
159
210
  return { ok: false, code: "vault-key-unavailable" };
160
211
  }
212
+ const replaying = replayingKms(kms);
213
+ try {
214
+ return await createWithKeys(deps, scope, replaying.client);
215
+ } finally {
216
+ replaying.clear();
217
+ }
218
+ }
219
+
220
+ async function createWithKeys(
221
+ deps: SyntheticOrderDeps,
222
+ scope: SyntheticScope,
223
+ kms: VaultKms,
224
+ ): Promise<SyntheticResult<{ orderId: string; stage: typeof SYNTHETIC_STAGE }>> {
161
225
  const now = nowOf(deps);
162
- // Prove the vault can seal before writing anything: an unconfigured or unreachable KMS refuses here.
163
- if (!(await orderContactKeysAvailable(deps.db, kms, scope.tenantId, SYNTHETIC_STAGE, now))) {
226
+ if (!(await prepareOrderContactKeys(deps.db, kms, scope.tenantId, SYNTHETIC_STAGE, now))) {
164
227
  return { ok: false, code: "vault-key-unavailable" };
165
228
  }
166
229
  const nowIso = now.toISOString();
@@ -395,16 +395,23 @@ function placedMonth(placedAt: Date): string {
395
395
  }
396
396
 
397
397
  /**
398
- * Whether both order-contact keys for this tenant and stage can be opened or generated right now.
399
- * Writes nothing ({@link activeKey} never persists a generated key), so a caller that must refuse
400
- * before its own writes when the vault is unavailable checks this first.
398
+ * Make both order-contact keys for this tenant and stage durable and open, for a caller that must
399
+ * refuse before its own writes when the vault cannot seal. When either key cannot be opened or
400
+ * generated it returns false having written nothing. Otherwise it stores any newly generated key
401
+ * (the insert yields to an active key another writer stored first) and opens the stored keys, so a
402
+ * following {@link sealOrderContact} reads keys that already exist rather than generating them.
401
403
  */
402
- export async function orderContactKeysAvailable(
404
+ export async function prepareOrderContactKeys(
403
405
  db: Queryable, kms: KmsClient, tenantId: string, stage: VaultStage, now: Date,
404
406
  ): Promise<boolean> {
407
+ const contexts = [keyContext(tenantId, stage, ORDER_KEY), keyContext(tenantId, stage, BIDX_KEY)];
405
408
  try {
406
- await activeKey(db, kms, keyContext(tenantId, stage, ORDER_KEY), now);
407
- await activeKey(db, kms, keyContext(tenantId, stage, BIDX_KEY), now);
409
+ const opened = [];
410
+ for (const ctx of contexts) opened.push(await activeKey(db, kms, ctx, now));
411
+ const pending = opened.flatMap((key) => key.pending);
412
+ if (pending.length === 0) return true;
413
+ await requireBatch(db)(pending);
414
+ for (const ctx of contexts) await activeKey(db, kms, ctx, now);
408
415
  return true;
409
416
  } catch (error) {
410
417
  if (error instanceof VaultKeyUnavailableError) return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/storefront-runner",
3
- "version": "6.12.0",
3
+ "version": "6.12.1",
4
4
  "license": "SEE LICENSE IN LICENSE",
5
5
  "description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
6
6
  "packageManager": "pnpm@11.9.0",