// The bus as the forge module's token needs it, in memory (design 47 H2 §808, §822): the vault seat's `own-put` in its // two sealed steps and its `get` sealed to the asker's key, and the runtime's state with a create-only key that lives // its TTL, as the runtime that launched the bundle answers them. `order` records each own-put and each forge delete a // test also writes into it, so the delete's place after the own-put can be read. import nacl from "tweetnacl"; import blakejs from "blakejs"; import { seal } from "@novox/mesh-sdk/seal"; import { useBroker, type Broker } from "@novox/mesh-sdk/messaging"; import { useRuntime } from "@novox/mesh-sdk/runtime"; import { useStateChannel, type StateChange } from "@novox/mesh-sdk/state"; export interface Held { value: string; generation: number; changedAt: string } export function fakeBus(now: () => Date = () => new Date(), order: string[] = []) { const vault = new Map(); const calls = new Map(); const kv = new Map(); const watchers: { state: string; key: string; deliver: (c: StateChange) => Promise }[] = []; let revision = 0; const keyOf = (state: string, key: string) => `${state}/${key}`; const live = (k: string) => { const e = kv.get(k); if (e && e.until !== null && e.until <= now().getTime()) kv.delete(k); return kv.get(k); }; const tell = async (state: string, key: string, op: "put" | "delete", value?: unknown) => { revision++; for (const w of watchers) if (w.state === state && (w.key === "" || w.key === key)) await w.deliver({ state, key, op, value, revision, current: false }); }; const broker: Broker = { async request(key: string, body: Req): Promise { const b = body as Record; if (key === "seat:mesh-vault.own-put" && b.call === undefined) { const keys = nacl.box.keyPair(); const call = `call-${calls.size + 1}`; calls.set(call, { name: `module/gitea/${b.name}`, secretKey: keys.secretKey }); return { call, seal_to: Buffer.from(keys.publicKey).toString("base64"), secret: `module/gitea/${b.name}` } as Res; } if (key === "seat:mesh-vault.own-put") { const c = calls.get(b.call); if (!c) throw new Error(`no own-put call ${b.call}`); calls.delete(b.call); const boxed = new Uint8Array(Buffer.from(b.sealed, "base64")); const eph = boxed.subarray(0, 32); const pub = nacl.box.keyPair.fromSecretKey(c.secretKey).publicKey; const nonce = blakejs.blake2b(Buffer.concat([eph, pub]), undefined, 24); const opened = nacl.box.open(boxed.subarray(32), nonce, eph, c.secretKey); if (!opened) throw new Error("the sealed value does not open"); const generation = (vault.get(c.name)?.generation ?? 0) + 1; vault.set(c.name, { value: Buffer.from(opened).toString("utf8"), generation, changedAt: now().toISOString() }); order.push(`own-put ${c.name} ${generation}`); return { name: c.name, generation } as Res; } if (key === "seat:mesh-vault.get") { const held = vault.get(b.name); if (!held) throw new Error(`the vault holds no secret named ${b.name}`); return { name: b.name, generation: held.generation, "changed-at": held.changedAt, sealed: seal(held.value, b.public_key) } as Res; } throw new Error(`not played here: ${key}`); }, handle: async () => () => {}, publish: async () => {}, subscribe: async () => () => {}, close: async () => {}, }; const ask = async (method: string, params: unknown): Promise => { const p = params as { state: string; key: string; value?: unknown; ttl_seconds?: number }; const k = keyOf(p.state, p.key); switch (method) { case "mesh/state.create": if (live(k)) throw Object.assign(new Error("the key has a value"), { code: -32010 }); kv.set(k, { value: p.value, until: p.ttl_seconds ? now().getTime() + p.ttl_seconds * 1000 : null }); await tell(p.state, p.key, "put", p.value); return { revision }; case "mesh/state.get": { const e = live(k); return e ? { key: p.key, value: e.value, revision } : null; } case "mesh/state.put": kv.set(k, { value: p.value, until: null }); await tell(p.state, p.key, "put", p.value); return { revision }; case "mesh/state.delete": kv.delete(k); await tell(p.state, p.key, "delete"); return null; default: throw new Error(`not played here: ${method}`); } }; useBroker(() => broker); useRuntime(ask); useStateChannel({ ask, watch: async (params, deliver) => { const w = params as { state: string; key: string }; watchers.push({ ...w, deliver }); } }); return { vault, kv, order, ask }; }